From 7788fc4046f6ff5fb6a2ef67906cecc32e7b4df8 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 28 Oct 2006 14:16:20 +0000 Subject: [PATCH] Alloc() doesn't clear the array any more, for consistency with reserve() git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@42571 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + docs/latex/wx/array.tex | 3 ++- include/wx/dynarray.h | 4 ++-- src/common/dynarray.cpp | 17 ----------------- tests/arrays/arrays.cpp | 16 ++++++++++++++++ 5 files changed, 21 insertions(+), 20 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 52b01af0f9..94f7365ca7 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -14,6 +14,7 @@ Changes in behaviour not resulting in compilation errors, please read this! if the whole name is a directory. - wxDialog::CreateButtonSizer() may return NULL now, please see the manual. - wxStaticBoxSizer now deletes the associated wxStaticBox when it is deleted. +- wxArray::Alloc() now works as reserve() and does not clear the array any more - Windows calling CaptureMouse() are now required to handle the wxEVT_MOUSE_CAPTURE_LOST event and not call ReleaseMouse() in this case. diff --git a/docs/latex/wx/array.tex b/docs/latex/wx/array.tex index e4415b39c6..9fa90ec11e 100644 --- a/docs/latex/wx/array.tex +++ b/docs/latex/wx/array.tex @@ -493,7 +493,8 @@ append a lot of items. Preallocates memory for a given number of array elements. It is worth calling when the number of items which are going to be added to the array is known in advance because it will save unneeded memory reallocation. If the array already -has enough memory for the given number of items, nothing happens. +has enough memory for the given number of items, nothing happens. In any case, +the existing contents of the array is not modified. \membersection{wxArray::Clear}\label{wxarrayclear} diff --git a/include/wx/dynarray.h b/include/wx/dynarray.h index 4c2584fb49..b99ed6a64b 100644 --- a/include/wx/dynarray.h +++ b/include/wx/dynarray.h @@ -169,7 +169,7 @@ public: \ \ void Empty() { m_nCount = 0; } \ void Clear(); \ - void Alloc(size_t uiSize); \ + void Alloc(size_t n) { if ( n > m_nSize ) Realloc(n); } \ void Shrink(); \ \ size_t GetCount() const { return m_nCount; } \ @@ -225,7 +225,7 @@ protected: \ void insert(iterator it, const_iterator first, const_iterator last);\ void pop_back() { RemoveAt(size() - 1); } \ void push_back(const value_type& v) { Add(v); } \ - void reserve(size_type n) { if(n > m_nSize) Realloc(n); } \ + void reserve(size_type n) { Alloc(n); } \ void resize(size_type n, value_type v = value_type()) \ { SetCount(n, v); } \ \ diff --git a/src/common/dynarray.cpp b/src/common/dynarray.cpp index c856a651f6..954326986e 100644 --- a/src/common/dynarray.cpp +++ b/src/common/dynarray.cpp @@ -244,23 +244,6 @@ void name::Clear() \ wxDELETEA(m_pItems); \ } \ \ -/* pre-allocates memory (frees the previous data!) */ \ -void name::Alloc(size_t nSize) \ -{ \ - /* only if old buffer was not big enough */ \ - if ( nSize > m_nSize ) { \ - wxDELETEA(m_pItems); \ - m_nSize = 0; \ - m_pItems = new T[nSize]; \ - /* only alloc if allocation succeeded */ \ - if ( m_pItems ) { \ - m_nSize = nSize; \ - } \ - } \ - \ - m_nCount = 0; \ -} \ - \ /* minimizes the memory usage by freeing unused memory */ \ void name::Shrink() \ { \ diff --git a/tests/arrays/arrays.cpp b/tests/arrays/arrays.cpp index aaa0138cd6..c6fa3f3bd4 100644 --- a/tests/arrays/arrays.cpp +++ b/tests/arrays/arrays.cpp @@ -149,6 +149,7 @@ private: CPPUNIT_TEST( wxArrayUShortTest ); CPPUNIT_TEST( wxArrayIntTest ); CPPUNIT_TEST( TestSTL ); + CPPUNIT_TEST( Alloc ); CPPUNIT_TEST_SUITE_END(); void wxStringArrayTest(); @@ -156,6 +157,7 @@ private: void wxArrayUShortTest(); void wxArrayIntTest(); void TestSTL(); + void Alloc(); DECLARE_NO_COPY_CLASS(ArraysTestCase) }; @@ -359,6 +361,20 @@ TestArrayOf(UShort); TestArrayOf(Int); +void ArraysTestCase::Alloc() +{ + wxArrayInt a; + a.Add(17); + a.Add(9); + CPPUNIT_ASSERT_EQUAL( 2u, a.GetCount() ); + + a.Alloc(1000); + + CPPUNIT_ASSERT_EQUAL( 2u, a.GetCount() ); + CPPUNIT_ASSERT_EQUAL( 17, a[0] ); + CPPUNIT_ASSERT_EQUAL( 9, a[1] ); +} + void ArraysTestCase::TestSTL() { wxArrayInt list1; -- 2.47.2