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"
29 #include <string.h> // for memmove
32 #define max(a, b) (((a) > (b)) ? (a) : (b))
35 // ============================================================================
37 // ============================================================================
39 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
40 #define ARRAY_MAXSIZE_INCREMENT 4096
42 // ============================================================================
44 // ============================================================================
46 // ----------------------------------------------------------------------------
47 // wxBaseArray - dynamice array of 'long's
48 // ----------------------------------------------------------------------------
51 wxBaseArray::wxBaseArray()
59 wxBaseArray::wxBaseArray(const wxBaseArray
& src
)
61 m_uiSize
= // not src.m_uiSize to save memory
62 m_uiCount
= src
.m_uiCount
;
64 if ( m_uiSize
!= 0 ) {
65 m_pItems
= new long[m_uiSize
];
66 memcpy(m_pItems
, src
.m_pItems
, m_uiCount
*sizeof(long));
72 // assignment operator
73 wxBaseArray
& wxBaseArray::operator=(const wxBaseArray
& src
)
77 m_uiSize
= // not src.m_uiSize to save memory
78 m_uiCount
= src
.m_uiCount
;
80 if ( m_uiSize
!= 0 ) {
81 m_pItems
= new long[m_uiSize
];
82 memcpy(m_pItems
, src
.m_pItems
, m_uiCount
*sizeof(long));
91 void wxBaseArray::Grow()
93 // only do it if no more place
94 if( m_uiCount
== m_uiSize
) {
96 // was empty, alloc some memory
97 m_uiSize
= WX_ARRAY_DEFAULT_INITIAL_SIZE
;
98 m_pItems
= new long[m_uiSize
];
102 // add 50% but not too much
103 uint uiIncrement
= m_uiSize
>> 1;
104 if ( uiIncrement
> ARRAY_MAXSIZE_INCREMENT
)
105 uiIncrement
= ARRAY_MAXSIZE_INCREMENT
;
106 m_uiSize
+= uiIncrement
;
107 long *pNew
= new long[m_uiSize
];
109 // copy data to new location
110 memcpy(pNew
, m_pItems
, m_uiCount
*sizeof(long));
118 wxBaseArray::~wxBaseArray()
124 void wxBaseArray::Clear()
132 // pre-allocates memory (frees the previous data!)
133 void wxBaseArray::Alloc(uint uiSize
)
135 wxASSERT( uiSize
> 0 );
137 // only if old buffer was not big enough
138 if ( uiSize
> m_uiSize
) {
140 m_pItems
= new long[uiSize
];
147 // searches the array for an item (forward or backwards)
148 int wxBaseArray::Index(long lItem
, bool bFromEnd
) const
151 if ( m_uiCount
> 0 ) {
154 if ( m_pItems
[--ui
] == lItem
)
161 for( uint ui
= 0; ui
< m_uiCount
; ui
++ ) {
162 if( m_pItems
[ui
] == lItem
)
170 // search for an item in a sorted array (binary search)
171 int wxBaseArray::Index(long lItem
, CMPFUNC fnCompare
) const
181 res
= (*fnCompare
)((const void *)lItem
, (const void *)m_pItems
[i
]);
192 // add item at the end
193 void wxBaseArray::Add(long lItem
)
196 m_pItems
[m_uiCount
++] = lItem
;
199 // add item assuming the array is sorted with fnCompare function
200 void wxBaseArray::Add(long lItem
, CMPFUNC fnCompare
)
210 res
= (*fnCompare
)((const void *)lItem
, (const void *)m_pItems
[i
]);
221 wxASSERT( lo
== hi
); // I hope I got binary search right :-)
226 // add item at the given position
227 void wxBaseArray::Insert(long lItem
, uint uiIndex
)
229 wxCHECK_RET( uiIndex
<= m_uiCount
, "bad index in wxArray::Insert" );
233 memmove(&m_pItems
[uiIndex
+ 1], &m_pItems
[uiIndex
],
234 (m_uiCount
- uiIndex
)*sizeof(long));
235 m_pItems
[uiIndex
] = lItem
;
239 // removes item from array (by index)
240 void wxBaseArray::Remove(uint uiIndex
)
242 wxCHECK_RET( uiIndex
<= m_uiCount
, "bad index in wxArray::Remove" );
244 memmove(&m_pItems
[uiIndex
], &m_pItems
[uiIndex
+ 1],
245 (m_uiCount
- uiIndex
- 1)*sizeof(long));
249 // removes item from array (by value)
250 void wxBaseArray::Remove(long lItem
)
252 int iIndex
= Index(lItem
);
254 wxCHECK_RET( iIndex
!= NOT_FOUND
,
255 "removing inexistent item in wxArray::Remove" );
257 Remove((uint
)iIndex
);
260 // sort array elements using passed comparaison function
261 void wxBaseArray::Sort(CMPFUNC fCmp
)
263 qsort(m_pItems
, m_uiCount
, sizeof(long), fCmp
);