]> git.saurik.com Git - wxWidgets.git/commitdiff
Alloc() doesn't clear the array any more, for consistency with reserve()
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 28 Oct 2006 14:16:20 +0000 (14:16 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 28 Oct 2006 14:16:20 +0000 (14:16 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@42571 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/changes.txt
docs/latex/wx/array.tex
include/wx/dynarray.h
src/common/dynarray.cpp
tests/arrays/arrays.cpp

index 52b01af0f9de3032d5386c5a854d8f5352bf3efb..94f7365ca7aca7eeeacd7a3db10bbe3364197614 100644 (file)
@@ -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.
 
index e4415b39c6624bfa336c3818d308c022abde7c67..9fa90ec11e349c98105edf0857b45acfabc5ba2f 100644 (file)
@@ -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}
 
index 4c2584fb49662abcfcc1cd5f0ee321645aeba90e..b99ed6a64bee34baec62a2b258dbb141ff4b89ef 100644 (file)
@@ -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); }                                             \
                                                                     \
index c856a651f64fbb57327357eb5eb55d3eb532b946..954326986ef1763c09929e91167c99b843457599 100644 (file)
@@ -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()                                                         \
 {                                                                           \
index aaa0138cd64b98d556fdbac591c5e3606cecf7e4..c6fa3f3bd40e70e253fb2ecf066a244696a8a32b 100644 (file)
@@ -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;