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"
31 #define max(a, b) (((a) > (b)) ? (a) : (b))
34 // ============================================================================
36 // ============================================================================
38 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
39 #define ARRAY_MAXSIZE_INCREMENT 4096
41 // ============================================================================
43 // ============================================================================
45 // ----------------------------------------------------------------------------
46 // wxBaseArray - dynamice array of 'long's
47 // ----------------------------------------------------------------------------
50 wxBaseArray::wxBaseArray()
58 wxBaseArray::wxBaseArray(const wxBaseArray
& src
)
60 m_uiSize
= src
.m_uiSize
;
61 m_uiCount
= src
.m_uiCount
;
64 m_pItems
= new long[m_uiSize
];
69 memcpy(m_pItems
, src
.m_pItems
, m_uiCount
*sizeof(long));
73 wxBaseArray
& wxBaseArray::operator=(const wxBaseArray
& src
)
77 m_uiSize
= src
.m_uiSize
;
78 m_uiCount
= src
.m_uiCount
;
81 m_pItems
= new long[m_uiSize
];
86 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
>> 1;
105 if ( uiIncrement
> ARRAY_MAXSIZE_INCREMENT
)
106 uiIncrement
= ARRAY_MAXSIZE_INCREMENT
;
107 m_uiSize
+= uiIncrement
;
108 long *pNew
= new long[m_uiSize
];
110 // copy data to new location
111 memcpy(pNew
, m_pItems
, m_uiCount
*sizeof(long));
119 wxBaseArray::~wxBaseArray()
125 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 // add item at the end
173 void wxBaseArray::Add(long lItem
)
176 m_pItems
[m_uiCount
++] = lItem
;
179 // add item at the given position
180 void wxBaseArray::Insert(long lItem
, uint uiIndex
)
182 wxCHECK( uiIndex
<= m_uiCount
);
186 memmove(&m_pItems
[uiIndex
+ 1], &m_pItems
[uiIndex
],
187 (m_uiCount
- uiIndex
)*sizeof(long));
188 m_pItems
[uiIndex
] = lItem
;
192 // removes item from array (by index)
193 void wxBaseArray::Remove(uint uiIndex
)
195 wxCHECK( uiIndex
<= m_uiCount
);
197 memmove(&m_pItems
[uiIndex
], &m_pItems
[uiIndex
+ 1],
198 (m_uiCount
- uiIndex
- 1)*sizeof(long));
202 // removes item from array (by value)
203 void wxBaseArray::Remove(long lItem
)
205 int iIndex
= Index(lItem
);
207 wxCHECK( iIndex
!= NOT_FOUND
);
209 Remove((uint
)iIndex
);
212 // sort array elements using passed comparaison function
213 void wxBaseArray::Sort(CMPFUNC fCmp
)
215 qsort(m_pItems
, m_uiCount
, sizeof(long), fCmp
);