]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/vector.h | |
3 | // Purpose: STL vector clone | |
4 | // Author: Lindsay Mathieson | |
5 | // Modified by: Vaclav Slavik - make it a template | |
6 | // Created: 30.07.2001 | |
7 | // Copyright: (c) 2001 Lindsay Mathieson <lindsay@mathieson.org>, | |
8 | // 2007 Vaclav Slavik <vslavik@fastmail.fm> | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_VECTOR_H_ | |
13 | #define _WX_VECTOR_H_ | |
14 | ||
15 | #include "wx/defs.h" | |
16 | ||
17 | #if wxUSE_STD_CONTAINERS | |
18 | ||
19 | #include <vector> | |
20 | #include <algorithm> | |
21 | ||
22 | #define wxVector std::vector | |
23 | template<typename T> | |
24 | inline void wxVectorSort(wxVector<T>& v) | |
25 | { | |
26 | std::sort(v.begin(), v.end()); | |
27 | } | |
28 | ||
29 | #else // !wxUSE_STD_CONTAINERS | |
30 | ||
31 | #include "wx/scopeguard.h" | |
32 | #include "wx/meta/movable.h" | |
33 | #include "wx/meta/if.h" | |
34 | ||
35 | #include "wx/beforestd.h" | |
36 | #include <new> // for placement new | |
37 | #include "wx/afterstd.h" | |
38 | ||
39 | // wxQsort is declared in wx/utils.h, but can't include that file here, | |
40 | // it indirectly includes this file. Just lovely... | |
41 | typedef int (*wxSortCallback)(const void* pItem1, | |
42 | const void* pItem2, | |
43 | const void* user_data); | |
44 | WXDLLIMPEXP_BASE void wxQsort(void* pbase, size_t total_elems, | |
45 | size_t size, wxSortCallback cmp, | |
46 | const void* user_data); | |
47 | ||
48 | namespace wxPrivate | |
49 | { | |
50 | ||
51 | // These templates encapsulate memory operations for use by wxVector; there are | |
52 | // two implementations, both in generic way for any C++ types and as an | |
53 | // optimized version for "movable" types that uses realloc() and memmove(). | |
54 | ||
55 | // version for movable types: | |
56 | template<typename T> | |
57 | struct wxVectorMemOpsMovable | |
58 | { | |
59 | static void Free(T* array) | |
60 | { free(array); } | |
61 | ||
62 | static T* Realloc(T* old, size_t newCapacity, size_t WXUNUSED(occupiedSize)) | |
63 | { return (T*)realloc(old, newCapacity * sizeof(T)); } | |
64 | ||
65 | static void MemmoveBackward(T* dest, T* source, size_t count) | |
66 | { memmove(dest, source, count * sizeof(T)); } | |
67 | ||
68 | static void MemmoveForward(T* dest, T* source, size_t count) | |
69 | { memmove(dest, source, count * sizeof(T)); } | |
70 | }; | |
71 | ||
72 | // generic version for non-movable types: | |
73 | template<typename T> | |
74 | struct wxVectorMemOpsGeneric | |
75 | { | |
76 | static void Free(T* array) | |
77 | { ::operator delete(array); } | |
78 | ||
79 | static T* Realloc(T* old, size_t newCapacity, size_t occupiedSize) | |
80 | { | |
81 | T *mem = (T*)::operator new(newCapacity * sizeof(T)); | |
82 | for ( size_t i = 0; i < occupiedSize; i++ ) | |
83 | { | |
84 | ::new(mem + i) T(old[i]); | |
85 | old[i].~T(); | |
86 | } | |
87 | ::operator delete(old); | |
88 | return mem; | |
89 | } | |
90 | ||
91 | static void MemmoveBackward(T* dest, T* source, size_t count) | |
92 | { | |
93 | wxASSERT( dest < source ); | |
94 | T* destptr = dest; | |
95 | T* sourceptr = source; | |
96 | for ( size_t i = count; i > 0; --i, ++destptr, ++sourceptr ) | |
97 | { | |
98 | ::new(destptr) T(*sourceptr); | |
99 | sourceptr->~T(); | |
100 | } | |
101 | } | |
102 | ||
103 | static void MemmoveForward(T* dest, T* source, size_t count) | |
104 | { | |
105 | wxASSERT( dest > source ); | |
106 | T* destptr = dest + count - 1; | |
107 | T* sourceptr = source + count - 1; | |
108 | for ( size_t i = count; i > 0; --i, --destptr, --sourceptr ) | |
109 | { | |
110 | ::new(destptr) T(*sourceptr); | |
111 | sourceptr->~T(); | |
112 | } | |
113 | } | |
114 | }; | |
115 | ||
116 | ||
117 | } // namespace wxPrivate | |
118 | ||
119 | template<typename T> | |
120 | class wxVector | |
121 | { | |
122 | private: | |
123 | // This cryptic expression means "typedef Ops to wxVectorMemOpsMovable if | |
124 | // type T is movable type, otherwise to wxVectorMemOpsGeneric". | |
125 | // | |
126 | // Note that we use typedef instead of privately deriving from this (which | |
127 | // would allowed us to omit "Ops::" prefixes below) to keep VC6 happy, | |
128 | // it can't compile code that derives from wxIf<...>::value. | |
129 | // | |
130 | // Note that bcc needs the extra parentheses for non-type template | |
131 | // arguments to compile this expression. | |
132 | typedef typename wxIf< (wxIsMovable<T>::value), | |
133 | wxPrivate::wxVectorMemOpsMovable<T>, | |
134 | wxPrivate::wxVectorMemOpsGeneric<T> >::value | |
135 | Ops; | |
136 | ||
137 | public: | |
138 | typedef size_t size_type; | |
139 | typedef size_t difference_type; | |
140 | typedef T value_type; | |
141 | typedef value_type* pointer; | |
142 | typedef const value_type* const_pointer; | |
143 | typedef value_type* iterator; | |
144 | typedef const value_type* const_iterator; | |
145 | typedef value_type& reference; | |
146 | typedef const value_type& const_reference; | |
147 | ||
148 | class reverse_iterator | |
149 | { | |
150 | public: | |
151 | reverse_iterator() : m_ptr(NULL) { } | |
152 | wxEXPLICIT reverse_iterator(iterator it) : m_ptr(it) { } | |
153 | reverse_iterator(const reverse_iterator& it) : m_ptr(it.m_ptr) { } | |
154 | ||
155 | reference operator*() const { return *m_ptr; } | |
156 | pointer operator->() const { return m_ptr; } | |
157 | ||
158 | iterator base() const { return m_ptr; } | |
159 | ||
160 | reverse_iterator& operator++() | |
161 | { --m_ptr; return *this; } | |
162 | reverse_iterator operator++(int) | |
163 | { reverse_iterator tmp = *this; --m_ptr; return tmp; } | |
164 | reverse_iterator& operator--() | |
165 | { ++m_ptr; return *this; } | |
166 | reverse_iterator operator--(int) | |
167 | { reverse_iterator tmp = *this; ++m_ptr; return tmp; } | |
168 | ||
169 | reverse_iterator operator+(difference_type n) const | |
170 | { return reverse_iterator(m_ptr - n); } | |
171 | reverse_iterator& operator+=(difference_type n) | |
172 | { m_ptr -= n; return *this; } | |
173 | reverse_iterator operator-(difference_type n) const | |
174 | { return reverse_iterator(m_ptr + n); } | |
175 | reverse_iterator& operator-=(difference_type n) | |
176 | { m_ptr += n; return *this; } | |
177 | ||
178 | reference operator[](difference_type n) const | |
179 | { return *(*this + n); } | |
180 | ||
181 | bool operator ==(const reverse_iterator& it) const | |
182 | { return m_ptr == it.m_ptr; } | |
183 | bool operator !=(const reverse_iterator& it) const | |
184 | { return m_ptr != it.m_ptr; } | |
185 | ||
186 | private: | |
187 | value_type *m_ptr; | |
188 | ||
189 | friend class const_reverse_iterator; | |
190 | }; | |
191 | ||
192 | class const_reverse_iterator | |
193 | { | |
194 | public: | |
195 | const_reverse_iterator() : m_ptr(NULL) { } | |
196 | wxEXPLICIT const_reverse_iterator(const_iterator it) : m_ptr(it) { } | |
197 | const_reverse_iterator(const reverse_iterator& it) : m_ptr(it.m_ptr) { } | |
198 | const_reverse_iterator(const const_reverse_iterator& it) : m_ptr(it.m_ptr) { } | |
199 | ||
200 | const_reference operator*() const { return *m_ptr; } | |
201 | const_pointer operator->() const { return m_ptr; } | |
202 | ||
203 | const_iterator base() const { return m_ptr; } | |
204 | ||
205 | const_reverse_iterator& operator++() | |
206 | { --m_ptr; return *this; } | |
207 | const_reverse_iterator operator++(int) | |
208 | { const_reverse_iterator tmp = *this; --m_ptr; return tmp; } | |
209 | const_reverse_iterator& operator--() | |
210 | { ++m_ptr; return *this; } | |
211 | const_reverse_iterator operator--(int) | |
212 | { const_reverse_iterator tmp = *this; ++m_ptr; return tmp; } | |
213 | ||
214 | const_reverse_iterator operator+(difference_type n) const | |
215 | { return const_reverse_iterator(m_ptr - n); } | |
216 | const_reverse_iterator& operator+=(difference_type n) | |
217 | { m_ptr -= n; return *this; } | |
218 | const_reverse_iterator operator-(difference_type n) const | |
219 | { return const_reverse_iterator(m_ptr + n); } | |
220 | const_reverse_iterator& operator-=(difference_type n) | |
221 | { m_ptr += n; return *this; } | |
222 | ||
223 | const_reference operator[](difference_type n) const | |
224 | { return *(*this + n); } | |
225 | ||
226 | bool operator ==(const const_reverse_iterator& it) const | |
227 | { return m_ptr == it.m_ptr; } | |
228 | bool operator !=(const const_reverse_iterator& it) const | |
229 | { return m_ptr != it.m_ptr; } | |
230 | ||
231 | protected: | |
232 | const value_type *m_ptr; | |
233 | }; | |
234 | ||
235 | wxVector() : m_size(0), m_capacity(0), m_values(NULL) {} | |
236 | ||
237 | wxVector(size_type p_size) | |
238 | : m_size(0), m_capacity(0), m_values(NULL) | |
239 | { | |
240 | reserve(p_size); | |
241 | for ( size_t n = 0; n < p_size; n++ ) | |
242 | push_back(value_type()); | |
243 | } | |
244 | ||
245 | wxVector(size_type p_size, const value_type& v) | |
246 | : m_size(0), m_capacity(0), m_values(NULL) | |
247 | { | |
248 | reserve(p_size); | |
249 | for ( size_t n = 0; n < p_size; n++ ) | |
250 | push_back(v); | |
251 | } | |
252 | ||
253 | wxVector(const wxVector& c) : m_size(0), m_capacity(0), m_values(NULL) | |
254 | { | |
255 | Copy(c); | |
256 | } | |
257 | ||
258 | ~wxVector() | |
259 | { | |
260 | clear(); | |
261 | } | |
262 | ||
263 | void assign(size_type p_size, const value_type& v) | |
264 | { | |
265 | clear(); | |
266 | reserve(p_size); | |
267 | for ( size_t n = 0; n < p_size; n++ ) | |
268 | push_back(v); | |
269 | } | |
270 | ||
271 | void swap(wxVector& v) | |
272 | { | |
273 | wxSwap(m_size, v.m_size); | |
274 | wxSwap(m_capacity, v.m_capacity); | |
275 | wxSwap(m_values, v.m_values); | |
276 | } | |
277 | ||
278 | void clear() | |
279 | { | |
280 | // call destructors of stored objects: | |
281 | for ( size_type i = 0; i < m_size; i++ ) | |
282 | { | |
283 | m_values[i].~T(); | |
284 | } | |
285 | ||
286 | Ops::Free(m_values); | |
287 | m_values = NULL; | |
288 | m_size = | |
289 | m_capacity = 0; | |
290 | } | |
291 | ||
292 | void reserve(size_type n) | |
293 | { | |
294 | if ( n <= m_capacity ) | |
295 | return; | |
296 | ||
297 | // increase the size twice, unless we're already too big or unless | |
298 | // more is requested | |
299 | // | |
300 | // NB: casts to size_type are needed to suppress warnings about | |
301 | // mixing enumeral and non-enumeral type in conditional expression | |
302 | const size_type increment = m_size > 0 | |
303 | ? m_size < ALLOC_MAX_SIZE | |
304 | ? m_size | |
305 | : (size_type)ALLOC_MAX_SIZE | |
306 | : (size_type)ALLOC_INITIAL_SIZE; | |
307 | if ( m_capacity + increment > n ) | |
308 | n = m_capacity + increment; | |
309 | ||
310 | m_values = Ops::Realloc(m_values, n * sizeof(value_type), m_size); | |
311 | m_capacity = n; | |
312 | } | |
313 | ||
314 | void resize(size_type n) | |
315 | { | |
316 | if ( n < m_size ) | |
317 | Shrink(n); | |
318 | else if ( n > m_size ) | |
319 | Extend(n, value_type()); | |
320 | } | |
321 | ||
322 | void resize(size_type n, const value_type& v) | |
323 | { | |
324 | if ( n < m_size ) | |
325 | Shrink(n); | |
326 | else if ( n > m_size ) | |
327 | Extend(n, v); | |
328 | } | |
329 | ||
330 | size_type size() const | |
331 | { | |
332 | return m_size; | |
333 | } | |
334 | ||
335 | size_type capacity() const | |
336 | { | |
337 | return m_capacity; | |
338 | } | |
339 | ||
340 | bool empty() const | |
341 | { | |
342 | return size() == 0; | |
343 | } | |
344 | ||
345 | wxVector& operator=(const wxVector& vb) | |
346 | { | |
347 | if (this != &vb) | |
348 | { | |
349 | clear(); | |
350 | Copy(vb); | |
351 | } | |
352 | return *this; | |
353 | } | |
354 | ||
355 | void push_back(const value_type& v) | |
356 | { | |
357 | reserve(size() + 1); | |
358 | ||
359 | // use placement new to initialize new object in preallocated place in | |
360 | // m_values and store 'v' in it: | |
361 | void* const place = m_values + m_size; | |
362 | ::new(place) value_type(v); | |
363 | ||
364 | // only increase m_size if the ctor didn't throw an exception; notice | |
365 | // that if it _did_ throw, everything is OK, because we only increased | |
366 | // vector's capacity so far and possibly written some data to | |
367 | // uninitialized memory at the end of m_values | |
368 | m_size++; | |
369 | } | |
370 | ||
371 | void pop_back() | |
372 | { | |
373 | erase(end() - 1); | |
374 | } | |
375 | ||
376 | const value_type& at(size_type idx) const | |
377 | { | |
378 | wxASSERT(idx < m_size); | |
379 | return m_values[idx]; | |
380 | } | |
381 | ||
382 | value_type& at(size_type idx) | |
383 | { | |
384 | wxASSERT(idx < m_size); | |
385 | return m_values[idx]; | |
386 | } | |
387 | ||
388 | const value_type& operator[](size_type idx) const { return at(idx); } | |
389 | value_type& operator[](size_type idx) { return at(idx); } | |
390 | const value_type& front() const { return at(0); } | |
391 | value_type& front() { return at(0); } | |
392 | const value_type& back() const { return at(size() - 1); } | |
393 | value_type& back() { return at(size() - 1); } | |
394 | ||
395 | const_iterator begin() const { return m_values; } | |
396 | iterator begin() { return m_values; } | |
397 | const_iterator end() const { return m_values + size(); } | |
398 | iterator end() { return m_values + size(); } | |
399 | ||
400 | reverse_iterator rbegin() { return reverse_iterator(end() - 1); } | |
401 | reverse_iterator rend() { return reverse_iterator(begin() - 1); } | |
402 | ||
403 | const_reverse_iterator rbegin() const { return const_reverse_iterator(end() - 1); } | |
404 | const_reverse_iterator rend() const { return const_reverse_iterator(begin() - 1); } | |
405 | ||
406 | iterator insert(iterator it, const value_type& v = value_type()) | |
407 | { | |
408 | // NB: this must be done before reserve(), because reserve() | |
409 | // invalidates iterators! | |
410 | const size_t idx = it - begin(); | |
411 | const size_t after = end() - it; | |
412 | ||
413 | reserve(size() + 1); | |
414 | ||
415 | // the place where the new element is going to be inserted | |
416 | value_type * const place = m_values + idx; | |
417 | ||
418 | // unless we're inserting at the end, move following elements out of | |
419 | // the way: | |
420 | if ( after > 0 ) | |
421 | Ops::MemmoveForward(place + 1, place, after); | |
422 | ||
423 | // if the ctor called below throws an exception, we need to move all | |
424 | // the elements back to their original positions in m_values | |
425 | wxScopeGuard moveBack = wxMakeGuard( | |
426 | Ops::MemmoveBackward, place, place + 1, after); | |
427 | if ( !after ) | |
428 | moveBack.Dismiss(); | |
429 | ||
430 | // use placement new to initialize new object in preallocated place in | |
431 | // m_values and store 'v' in it: | |
432 | ::new(place) value_type(v); | |
433 | ||
434 | // now that we did successfully add the new element, increment the size | |
435 | // and disable moving the items back | |
436 | moveBack.Dismiss(); | |
437 | m_size++; | |
438 | ||
439 | return begin() + idx; | |
440 | } | |
441 | ||
442 | iterator erase(iterator it) | |
443 | { | |
444 | return erase(it, it + 1); | |
445 | } | |
446 | ||
447 | iterator erase(iterator first, iterator last) | |
448 | { | |
449 | if ( first == last ) | |
450 | return first; | |
451 | wxASSERT( first < end() && last <= end() ); | |
452 | ||
453 | const size_type idx = first - begin(); | |
454 | const size_type count = last - first; | |
455 | const size_type after = end() - last; | |
456 | ||
457 | // erase elements by calling their destructors: | |
458 | for ( iterator i = first; i < last; ++i ) | |
459 | i->~T(); | |
460 | ||
461 | // once that's done, move following elements over to the freed space: | |
462 | if ( after > 0 ) | |
463 | { | |
464 | Ops::MemmoveBackward(m_values + idx, m_values + idx + count, after); | |
465 | } | |
466 | ||
467 | m_size -= count; | |
468 | ||
469 | return begin() + idx; | |
470 | } | |
471 | ||
472 | #if WXWIN_COMPATIBILITY_2_8 | |
473 | wxDEPRECATED( size_type erase(size_type n) ); | |
474 | #endif // WXWIN_COMPATIBILITY_2_8 | |
475 | ||
476 | private: | |
477 | // VC6 can't compile static const int members | |
478 | enum { ALLOC_INITIAL_SIZE = 16 }; | |
479 | enum { ALLOC_MAX_SIZE = 4096 }; | |
480 | ||
481 | void Copy(const wxVector& vb) | |
482 | { | |
483 | reserve(vb.size()); | |
484 | ||
485 | for ( const_iterator i = vb.begin(); i != vb.end(); ++i ) | |
486 | push_back(*i); | |
487 | } | |
488 | ||
489 | private: | |
490 | void Shrink(size_type n) | |
491 | { | |
492 | for ( size_type i = n; i < m_size; i++ ) | |
493 | m_values[i].~T(); | |
494 | m_size = n; | |
495 | } | |
496 | ||
497 | void Extend(size_type n, const value_type& v) | |
498 | { | |
499 | reserve(n); | |
500 | for ( size_type i = m_size; i < n; i++ ) | |
501 | push_back(v); | |
502 | } | |
503 | ||
504 | size_type m_size, | |
505 | m_capacity; | |
506 | value_type *m_values; | |
507 | }; | |
508 | ||
509 | #if WXWIN_COMPATIBILITY_2_8 | |
510 | template<typename T> | |
511 | inline typename wxVector<T>::size_type wxVector<T>::erase(size_type n) | |
512 | { | |
513 | erase(begin() + n); | |
514 | return n; | |
515 | } | |
516 | #endif // WXWIN_COMPATIBILITY_2_8 | |
517 | ||
518 | ||
519 | ||
520 | namespace wxPrivate | |
521 | { | |
522 | ||
523 | // This is a helper for the wxVectorSort function, and should not be used | |
524 | // directly in user's code. | |
525 | template<typename T> | |
526 | struct wxVectorComparator | |
527 | { | |
528 | static int | |
529 | Compare(const void* pitem1, const void* pitem2, const void* ) | |
530 | { | |
531 | const T& item1 = *reinterpret_cast<const T*>(pitem1); | |
532 | const T& item2 = *reinterpret_cast<const T*>(pitem2); | |
533 | ||
534 | if (item1 < item2) | |
535 | return -1; | |
536 | else if (item2 < item1) | |
537 | return 1; | |
538 | else | |
539 | return 0; | |
540 | } | |
541 | }; | |
542 | ||
543 | } // namespace wxPrivate | |
544 | ||
545 | ||
546 | ||
547 | template<typename T> | |
548 | void wxVectorSort(wxVector<T>& v) | |
549 | { | |
550 | wxQsort(v.begin(), v.size(), sizeof(T), | |
551 | wxPrivate::wxVectorComparator<T>::Compare, NULL); | |
552 | } | |
553 | ||
554 | ||
555 | ||
556 | #endif // wxUSE_STD_CONTAINERS/!wxUSE_STD_CONTAINERS | |
557 | ||
558 | #if WXWIN_COMPATIBILITY_2_8 | |
559 | #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls | |
560 | #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls) | |
561 | #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls) | |
562 | #endif // WXWIN_COMPATIBILITY_2_8 | |
563 | ||
564 | #endif // _WX_VECTOR_H_ |