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()
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));
73 // assignment operator
74 wxBaseArray
& wxBaseArray::operator=(const wxBaseArray
& src
)
78 m_uiSize
= // not src.m_uiSize to save memory
79 m_uiCount
= src
.m_uiCount
;
81 if ( m_uiSize
!= 0 ) {
82 m_pItems
= new long[m_uiSize
];
83 memcpy(m_pItems
, src
.m_pItems
, m_uiCount
*sizeof(long));
92 void wxBaseArray::Grow()
94 // only do it if no more place
95 if( m_uiCount
== m_uiSize
) {
97 // was empty, alloc some memory
98 m_uiSize
= WX_ARRAY_DEFAULT_INITIAL_SIZE
;
99 m_pItems
= new long[m_uiSize
];
103 // add 50% but not too much
104 uint uiIncrement
= m_uiSize
< WX_ARRAY_DEFAULT_INITIAL_SIZE
105 ? WX_ARRAY_DEFAULT_INITIAL_SIZE
: m_uiSize
>> 1;
106 if ( uiIncrement
> ARRAY_MAXSIZE_INCREMENT
)
107 uiIncrement
= ARRAY_MAXSIZE_INCREMENT
;
108 m_uiSize
+= uiIncrement
;
109 long *pNew
= new long[m_uiSize
];
111 // copy data to new location
112 memcpy(pNew
, m_pItems
, m_uiCount
*sizeof(long));
120 wxBaseArray::~wxBaseArray()
126 void wxBaseArray::Clear()
134 // pre-allocates memory (frees the previous data!)
135 void wxBaseArray::Alloc(uint uiSize
)
137 wxASSERT( uiSize
> 0 );
139 // only if old buffer was not big enough
140 if ( uiSize
> m_uiSize
) {
142 m_pItems
= new long[uiSize
];
149 // searches the array for an item (forward or backwards)
150 int wxBaseArray::Index(long lItem
, bool bFromEnd
) const
153 if ( m_uiCount
> 0 ) {
156 if ( m_pItems
[--ui
] == lItem
)
163 for( uint ui
= 0; ui
< m_uiCount
; ui
++ ) {
164 if( m_pItems
[ui
] == lItem
)
172 // search for an item in a sorted array (binary search)
173 int wxBaseArray::Index(long lItem
, CMPFUNC fnCompare
) const
183 res
= (*fnCompare
)((const void *)lItem
, (const void *)m_pItems
[i
]);
194 // add item at the end
195 void wxBaseArray::Add(long lItem
)
198 m_pItems
[m_uiCount
++] = lItem
;
201 // add item assuming the array is sorted with fnCompare function
202 void wxBaseArray::Add(long lItem
, CMPFUNC fnCompare
)
212 res
= (*fnCompare
)((const void *)lItem
, (const void *)m_pItems
[i
]);
223 wxASSERT( lo
== hi
); // I hope I got binary search right :-)
228 // add item at the given position
229 void wxBaseArray::Insert(long lItem
, uint uiIndex
)
231 wxCHECK_RET( uiIndex
<= m_uiCount
, _("bad index in wxArray::Insert") );
235 memmove(&m_pItems
[uiIndex
+ 1], &m_pItems
[uiIndex
],
236 (m_uiCount
- uiIndex
)*sizeof(long));
237 m_pItems
[uiIndex
] = lItem
;
241 // removes item from array (by index)
242 void wxBaseArray::Remove(uint uiIndex
)
244 wxCHECK_RET( uiIndex
<= m_uiCount
, _("bad index in wxArray::Remove") );
246 memmove(&m_pItems
[uiIndex
], &m_pItems
[uiIndex
+ 1],
247 (m_uiCount
- uiIndex
- 1)*sizeof(long));
251 // removes item from array (by value)
252 void wxBaseArray::Remove(long lItem
)
254 int iIndex
= Index(lItem
);
256 wxCHECK_RET( iIndex
!= NOT_FOUND
,
257 _("removing inexistent item in wxArray::Remove") );
259 Remove((uint
)iIndex
);
262 // sort array elements using passed comparaison function
263 void wxBaseArray::Sort(CMPFUNC fCmp
)
265 qsort(m_pItems
, m_uiCount
, sizeof(long), fCmp
);