1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxBaseArray class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
17 #pragma implementation "dynarray.h"
20 #include <wx/wxprec.h>
26 #include "wx/dynarray.h"
30 #include <string.h> // for memmove
33 #define max(a, b) (((a) > (b)) ? (a) : (b))
36 // ============================================================================
38 // ============================================================================
40 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
41 #define ARRAY_MAXSIZE_INCREMENT 4096
43 // ============================================================================
45 // ============================================================================
47 // ----------------------------------------------------------------------------
48 // wxBaseArray - dynamice array of 'long's
49 // ----------------------------------------------------------------------------
52 wxBaseArray::wxBaseArray()
56 m_pItems
= (long *) NULL
;
60 wxBaseArray::wxBaseArray(const wxBaseArray
& src
)
62 m_uiSize
= // not src.m_uiSize to save memory
63 m_uiCount
= src
.m_uiCount
;
65 if ( m_uiSize
!= 0 ) {
66 m_pItems
= new long[m_uiSize
];
67 memcpy(m_pItems
, src
.m_pItems
, m_uiCount
*sizeof(long));
70 m_pItems
= (long *) NULL
;
73 // assignment operator
74 wxBaseArray
& wxBaseArray::operator=(const wxBaseArray
& src
)
85 m_uiSize
= // not src.m_uiSize to save memory
86 m_uiCount
= src
.m_uiCount
;
88 if ( m_uiSize
!= 0 ) {
89 m_pItems
= new long[m_uiSize
];
90 memcpy(m_pItems
, src
.m_pItems
, m_uiCount
*sizeof(long));
93 m_pItems
= (long *) NULL
;
99 void wxBaseArray::Grow()
101 // only do it if no more place
102 if( m_uiCount
== m_uiSize
) {
103 if( m_uiSize
== 0 ) {
104 // was empty, alloc some memory
105 m_uiSize
= WX_ARRAY_DEFAULT_INITIAL_SIZE
;
106 m_pItems
= new long[m_uiSize
];
110 // add 50% but not too much
111 size_t uiIncrement
= m_uiSize
< WX_ARRAY_DEFAULT_INITIAL_SIZE
112 ? WX_ARRAY_DEFAULT_INITIAL_SIZE
: m_uiSize
>> 1;
113 if ( uiIncrement
> ARRAY_MAXSIZE_INCREMENT
)
114 uiIncrement
= ARRAY_MAXSIZE_INCREMENT
;
115 m_uiSize
+= uiIncrement
;
116 long *pNew
= new long[m_uiSize
];
118 // copy data to new location
119 memcpy(pNew
, m_pItems
, m_uiCount
*sizeof(long));
127 wxBaseArray::~wxBaseArray()
133 void wxBaseArray::Clear()
141 // pre-allocates memory (frees the previous data!)
142 void wxBaseArray::Alloc(size_t uiSize
)
144 wxASSERT( uiSize
> 0 );
146 // only if old buffer was not big enough
147 if ( uiSize
> m_uiSize
) {
149 m_pItems
= new long[uiSize
];
156 // searches the array for an item (forward or backwards)
157 int wxBaseArray::Index(long lItem
, bool bFromEnd
) const
160 if ( m_uiCount
> 0 ) {
161 size_t ui
= m_uiCount
;
163 if ( m_pItems
[--ui
] == lItem
)
170 for( size_t ui
= 0; ui
< m_uiCount
; ui
++ ) {
171 if( m_pItems
[ui
] == lItem
)
179 // search for an item in a sorted array (binary search)
180 int wxBaseArray::Index(long lItem
, CMPFUNC fnCompare
) const
190 res
= (*fnCompare
)((const void *)lItem
, (const void *)m_pItems
[i
]);
201 // add item at the end
202 void wxBaseArray::Add(long lItem
)
205 m_pItems
[m_uiCount
++] = lItem
;
208 // add item assuming the array is sorted with fnCompare function
209 void wxBaseArray::Add(long lItem
, CMPFUNC fnCompare
)
219 res
= (*fnCompare
)((const void *)lItem
, (const void *)m_pItems
[i
]);
230 wxASSERT( lo
== hi
); // I hope I got binary search right :-)
235 // add item at the given position
236 void wxBaseArray::Insert(long lItem
, size_t uiIndex
)
238 wxCHECK_RET( uiIndex
<= m_uiCount
, _("bad index in wxArray::Insert") );
242 memmove(&m_pItems
[uiIndex
+ 1], &m_pItems
[uiIndex
],
243 (m_uiCount
- uiIndex
)*sizeof(long));
244 m_pItems
[uiIndex
] = lItem
;
248 // removes item from array (by index)
249 void wxBaseArray::Remove(size_t uiIndex
)
251 wxCHECK_RET( uiIndex
<= m_uiCount
, _("bad index in wxArray::Remove") );
253 memmove(&m_pItems
[uiIndex
], &m_pItems
[uiIndex
+ 1],
254 (m_uiCount
- uiIndex
- 1)*sizeof(long));
258 // removes item from array (by value)
259 void wxBaseArray::Remove(long lItem
)
261 int iIndex
= Index(lItem
);
263 wxCHECK_RET( iIndex
!= NOT_FOUND
,
264 _("removing inexistent item in wxArray::Remove") );
266 Remove((size_t)iIndex
);
269 // sort array elements using passed comparaison function
270 void wxBaseArray::Sort(CMPFUNC fCmp
)
272 qsort(m_pItems
, m_uiCount
, sizeof(long), fCmp
);