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()
133 // pre-allocates memory (frees the previous data!)
134 void wxBaseArray::Alloc(uint uiSize
)
136 wxASSERT( uiSize
> 0 );
138 // only if old buffer was not big enough
139 if ( uiSize
> m_uiSize
) {
141 m_pItems
= new long[uiSize
];
148 // searches the array for an item (forward or backwards)
149 int wxBaseArray::Index(long lItem
, bool bFromEnd
) const
152 if ( m_uiCount
> 0 ) {
155 if ( m_pItems
[--ui
] == lItem
)
162 for( uint ui
= 0; ui
< m_uiCount
; ui
++ ) {
163 if( m_pItems
[ui
] == lItem
)
171 // search for an item in a sorted array (binary search)
172 int wxBaseArray::Index(long lItem
, CMPFUNC fnCompare
) const
182 res
= (*fnCompare
)((const void *)lItem
, (const void *)m_pItems
[i
]);
193 // add item at the end
194 void wxBaseArray::Add(long lItem
)
197 m_pItems
[m_uiCount
++] = lItem
;
200 // add item assuming the array is sorted with fnCompare function
201 void wxBaseArray::Add(long lItem
, CMPFUNC fnCompare
)
211 res
= (*fnCompare
)((const void *)lItem
, (const void *)m_pItems
[i
]);
222 wxASSERT( lo
== hi
); // I hope I got binary search right :-)
227 // add item at the given position
228 void wxBaseArray::Insert(long lItem
, uint uiIndex
)
230 wxCHECK_RET( uiIndex
<= m_uiCount
, "bad index in wxArray::Insert" );
234 memmove(&m_pItems
[uiIndex
+ 1], &m_pItems
[uiIndex
],
235 (m_uiCount
- uiIndex
)*sizeof(long));
236 m_pItems
[uiIndex
] = lItem
;
240 // removes item from array (by index)
241 void wxBaseArray::Remove(uint uiIndex
)
243 wxCHECK_RET( uiIndex
<= m_uiCount
, "bad index in wxArray::Remove" );
245 memmove(&m_pItems
[uiIndex
], &m_pItems
[uiIndex
+ 1],
246 (m_uiCount
- uiIndex
- 1)*sizeof(long));
250 // removes item from array (by value)
251 void wxBaseArray::Remove(long lItem
)
253 int iIndex
= Index(lItem
);
255 wxCHECK_RET( iIndex
!= NOT_FOUND
,
256 "removing inexistent item in wxArray::Remove" );
258 Remove((uint
)iIndex
);
261 // sort array elements using passed comparaison function
262 void wxBaseArray::Sort(CMPFUNC fCmp
)
264 qsort(m_pItems
, m_uiCount
, sizeof(long), fCmp
);