]>
Commit | Line | Data |
---|---|---|
4b123bb9 HH |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: No names yet. | |
3 | // Purpose: Contrib. demo | |
4 | // Author: Aleksandras Gluchovas | |
5 | // Modified by: | |
6 | // Created: 27/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Aleskandars Gluchovas | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef __WXSTLVEC_G__ | |
13 | #define __WXSTLVEC_G__ | |
14 | ||
15 | #include <memory.h> | |
16 | #include <string.h> // imports memmove() | |
17 | #include <stddef.h> | |
18 | #include <sys/types.h> | |
19 | #include <limits.h> | |
20 | #include <new> | |
21 | ||
22 | // the below macro used internally (see actual interface after this macro) | |
23 | ||
24 | #define __DEFINE_STL_VECTOR_DEEP( vectorClass, Type ) class vectorClass {\ | |
25 | \ | |
26 | public:\ | |
27 | typedef Type value_type;\ | |
28 | typedef value_type* iterator;\ | |
29 | typedef const value_type* const_iterator;\ | |
30 | typedef iterator pointer;\ | |
31 | typedef const iterator const_pointer;\ | |
32 | typedef value_type& reference;\ | |
33 | typedef const value_type& const_reference;\ | |
34 | typedef size_t size_type;\ | |
35 | typedef ptrdiff_t difference_type;\ | |
36 | \ | |
37 | typedef iterator OutputIterator;\ | |
38 | typedef const_iterator InputIterator;\ | |
39 | \ | |
40 | protected:\ | |
41 | \ | |
42 | inline void PlacementCopy( const_iterator first, const_iterator last, iterator result )\ | |
43 | {\ | |
44 | while ( first != last ) \ | |
45 | new (result++) value_type(*first++);\ | |
46 | }\ | |
47 | \ | |
48 | inline void ConstructObjects( iterator first, iterator last, const value_type& pattern )\ | |
49 | {\ | |
50 | while( first != last ) \ | |
51 | new (first++) value_type(pattern);\ | |
52 | }\ | |
53 | \ | |
54 | inline void CopyObjects( iterator first, iterator last, iterator result )\ | |
55 | {\ | |
56 | while( first != last ) \ | |
57 | *result++ = *first++;\ | |
58 | }\ | |
59 | \ | |
60 | inline void CopyObjectsBack( iterator first, iterator last, iterator result )\ | |
61 | {\ | |
62 | result += difference_type(last,first);\ | |
63 | \ | |
64 | while( first != last ) \ | |
65 | *(--result) = *(--last);\ | |
66 | }\ | |
67 | \ | |
68 | public:\ | |
69 | \ | |
70 | class reverse_iterator \ | |
71 | {\ | |
72 | friend class vectorClass;\ | |
73 | friend class const_reverse_iterator;\ | |
74 | \ | |
75 | public:\ | |
76 | iterator mpPos;\ | |
77 | \ | |
78 | public:\ | |
79 | \ | |
80 | reverse_iterator() {}\ | |
81 | \ | |
82 | reverse_iterator ( iterator pPos )\ | |
83 | {\ | |
84 | mpPos = pPos;\ | |
85 | }\ | |
86 | \ | |
87 | int operator==( const reverse_iterator& rhs ) const { return (mpPos == rhs.mpPos); }\ | |
88 | int operator!=( const reverse_iterator& rhs ) const { return (mpPos != rhs.mpPos); }\ | |
89 | \ | |
90 | inline reverse_iterator( const reverse_iterator& other )\ | |
91 | {\ | |
92 | mpPos = other.mpPos;\ | |
93 | }\ | |
94 | \ | |
95 | inline const reverse_iterator& operator--() \ | |
96 | {\ | |
97 | --mpPos;\ | |
98 | return *this;\ | |
99 | }\ | |
100 | \ | |
101 | inline reverse_iterator operator--(int)\ | |
102 | {\ | |
103 | reverse_iterator tmp = *this;\ | |
104 | --mpPos;\ | |
105 | return tmp;\ | |
106 | }\ | |
107 | \ | |
108 | inline const reverse_iterator & operator++() \ | |
109 | {\ | |
110 | ++mpPos;\ | |
111 | return *this;\ | |
112 | }\ | |
113 | \ | |
114 | inline reverse_iterator operator++(int)\ | |
115 | {\ | |
116 | reverse_iterator tmp = *this;\ | |
117 | ++mpPos;\ | |
118 | return tmp;\ | |
119 | }\ | |
120 | \ | |
121 | inline const_reference operator*() const { return *mpPos; }\ | |
122 | };\ | |
123 | \ | |
124 | \ | |
125 | class const_reverse_iterator \ | |
126 | {\ | |
127 | protected:\ | |
128 | iterator mpPos;\ | |
129 | public:\ | |
130 | \ | |
131 | const_reverse_iterator() {}\ | |
132 | \ | |
133 | const_reverse_iterator( const iterator pPos )\ | |
134 | {\ | |
135 | mpPos = pPos;\ | |
136 | }\ | |
137 | \ | |
138 | int operator==( const const_reverse_iterator& rhs ) const { return (mpPos == rhs.mpPos); }\ | |
139 | int operator!=( const const_reverse_iterator& rhs ) const { return (mpPos != rhs.mpPos); }\ | |
140 | \ | |
141 | inline const_reverse_iterator( const reverse_iterator& other )\ | |
142 | {\ | |
143 | mpPos = other.mpPos;\ | |
144 | }\ | |
145 | \ | |
146 | inline const const_reverse_iterator& operator--() \ | |
147 | {\ | |
148 | --mpPos;\ | |
149 | return *this;\ | |
150 | }\ | |
151 | \ | |
152 | inline const_reverse_iterator operator--(int)\ | |
153 | {\ | |
154 | const_reverse_iterator tmp = *this;\ | |
155 | --mpPos;\ | |
156 | return tmp;\ | |
157 | }\ | |
158 | \ | |
159 | inline const const_reverse_iterator & operator++() \ | |
160 | {\ | |
161 | ++mpPos;\ | |
162 | return *this;\ | |
163 | }\ | |
164 | \ | |
165 | inline const_reverse_iterator operator++(int)\ | |
166 | {\ | |
167 | const_reverse_iterator tmp = *this;\ | |
168 | ++mpPos;\ | |
169 | return tmp;\ | |
170 | }\ | |
171 | \ | |
172 | inline const_reference operator*() const { return *mpPos; }\ | |
173 | };\ | |
174 | \ | |
175 | protected:\ | |
176 | \ | |
177 | pointer mpStart;\ | |
178 | pointer mpEnd;\ | |
179 | pointer mpEndOfBuf;\ | |
180 | \ | |
181 | protected:\ | |
182 | \ | |
183 | inline void quick_sort(int low, int hi) \ | |
184 | {\ | |
185 | int pivot_index;\ | |
186 | int left, right;\ | |
187 | \ | |
188 | pivot_index = ( !(mpStart[low] < mpStart[low+1])) ? low : (low+1);\ | |
189 | value_type pivot_value = mpStart[pivot_index];\ | |
190 | \ | |
191 | left = low; right = hi;\ | |
192 | do \ | |
193 | {\ | |
194 | while ((left <= hi) && (mpStart[left] < pivot_value)) left++;\ | |
195 | \ | |
196 | while ((right >= low) && (pivot_value < mpStart[right])) right--;\ | |
197 | \ | |
198 | if (left <= right) \ | |
199 | {\ | |
200 | value_type tmp = mpStart[left];\ | |
201 | mpStart[left] = mpStart[right];\ | |
202 | mpStart[right] = tmp;\ | |
203 | \ | |
204 | left++;\ | |
205 | right--;\ | |
206 | }\ | |
207 | \ | |
208 | } while (left <= right);\ | |
209 | if (low < right) quick_sort(low, right);\ | |
210 | if (left < hi) quick_sort(left, hi);\ | |
211 | }\ | |
212 | \ | |
213 | inline void DestructRange( iterator first, iterator last )\ | |
214 | {\ | |
215 | typedef value_type value_type_local;\ | |
216 | \ | |
217 | while ( first != last ) \ | |
218 | {\ | |
219 | first->value_type_local::~value_type_local();\ | |
220 | ++first;\ | |
221 | }\ | |
222 | }\ | |
223 | \ | |
224 | inline iterator DoInsert(iterator position, const value_type& x)\ | |
225 | {\ | |
226 | if ( mpEnd < mpEndOfBuf )\ | |
227 | {\ | |
228 | new (mpEnd) value_type(*(mpEnd-1) );\ | |
229 | \ | |
230 | CopyObjectsBack( position, mpEnd, position + 1 );\ | |
231 | \ | |
232 | *position = x;\ | |
233 | \ | |
234 | ++mpEnd;\ | |
235 | \ | |
236 | return position;\ | |
237 | }\ | |
238 | \ | |
239 | size_type minBufLen = WXSTL_VECTOR_MIN_BUF_SIZE/sizeof(value_type);\ | |
240 | \ | |
241 | size_type doubledSize = size()*2;\ | |
242 | \ | |
243 | size_type newLen = ( doubledSize < minBufLen ) ? minBufLen : doubledSize;\ | |
244 | \ | |
245 | iterator pNewStart = (iterator)( new char[newLen*sizeof(value_type)] );\ | |
246 | \ | |
247 | PlacementCopy( mpStart, position, pNewStart );\ | |
248 | \ | |
249 | iterator atPosition = pNewStart + difference_type( position - mpStart );\ | |
250 | \ | |
251 | new (atPosition) value_type(x);\ | |
252 | \ | |
253 | iterator newPos = atPosition;\ | |
254 | \ | |
255 | ++atPosition;\ | |
256 | \ | |
257 | if ( mpStart ) \ | |
258 | {\ | |
259 | PlacementCopy( position, mpEnd, atPosition );\ | |
260 | DestructRange( mpStart, mpEnd );\ | |
261 | delete [](char*)mpStart;\ | |
262 | }\ | |
263 | \ | |
264 | mpEnd = atPosition + difference_type( mpEnd - position );\ | |
265 | \ | |
266 | mpStart = pNewStart;\ | |
267 | mpEndOfBuf = pNewStart + newLen;\ | |
268 | \ | |
269 | return newPos;\ | |
270 | }\ | |
271 | \ | |
272 | public:\ | |
273 | \ | |
274 | inline vectorClass() : mpStart(0), \ | |
275 | mpEnd(0),\ | |
276 | mpEndOfBuf(0)\ | |
277 | {}\ | |
278 | \ | |
279 | inline vectorClass( const_iterator first, const_iterator last )\ | |
280 | : mpStart(0),\ | |
281 | mpEnd(0),\ | |
282 | mpEndOfBuf(0)\ | |
283 | \ | |
284 | { while( first != last ) push_back( *first++ ); }\ | |
285 | \ | |
286 | inline vectorClass( size_type n, const value_type& value = value_type() )\ | |
287 | : mpStart(0),\ | |
288 | mpEnd(0),\ | |
289 | mpEndOfBuf(0)\ | |
290 | \ | |
291 | { for( size_type i = 0; i != n; ++i ) push_back( value ); }\ | |
292 | \ | |
293 | inline int operator==( const vectorClass& other )\ | |
294 | {\ | |
295 | size_type sz = size();\ | |
296 | \ | |
297 | if ( sz != other.size() ) return 0;\ | |
298 | \ | |
299 | for( size_type i = 0; i != sz; ++i )\ | |
300 | \ | |
301 | if ( !( (*this)[i] == other[i] ) ) return 0;\ | |
302 | \ | |
303 | return 1;\ | |
304 | \ | |
305 | }\ | |
306 | \ | |
307 | inline const vectorClass& operator=( const vectorClass& other )\ | |
308 | {\ | |
309 | if (mpStart) \ | |
310 | {\ | |
311 | DestructRange( begin(), end() );\ | |
312 | delete [](char*)mpStart; \ | |
313 | }\ | |
314 | \ | |
315 | size_t newLen = difference_type( other.mpEndOfBuf - other.mpStart );\ | |
316 | \ | |
317 | mpStart = (iterator)( new char[newLen*sizeof(value_type)] );\ | |
318 | \ | |
319 | PlacementCopy( other.begin(), other.end(), mpStart );\ | |
320 | \ | |
321 | mpEnd = mpStart + other.size();\ | |
322 | \ | |
323 | mpEndOfBuf = mpStart + newLen;\ | |
324 | \ | |
325 | return *this;\ | |
326 | }\ | |
327 | \ | |
328 | inline vectorClass( const vectorClass& other )\ | |
329 | : mpStart(0),\ | |
330 | mpEnd(0),\ | |
331 | mpEndOfBuf(0)\ | |
332 | {\ | |
333 | this->operator=( other );\ | |
334 | }\ | |
335 | \ | |
336 | inline ~vectorClass() \ | |
337 | { \ | |
338 | if (mpStart) \ | |
339 | {\ | |
340 | DestructRange( begin(), end() );\ | |
341 | delete [](char*)mpStart; \ | |
342 | }\ | |
343 | }\ | |
344 | \ | |
345 | inline iterator begin() { return mpStart; }\ | |
346 | \ | |
347 | inline const_iterator begin() const { return mpStart; }\ | |
348 | \ | |
349 | inline iterator end() { return mpEnd; }\ | |
350 | \ | |
351 | inline const_iterator end() const { return mpEnd; }\ | |
352 | \ | |
353 | inline size_type size() const { return (size_type)difference_type(mpEnd-mpStart); }\ | |
354 | \ | |
355 | inline size_type max_size() const { return UINT_MAX/sizeof(value_type); }\ | |
356 | \ | |
357 | inline size_type capacity() const \ | |
358 | { return difference_type(mpEndOfBuf-mpStart)/sizeof(value_type); }\ | |
359 | \ | |
360 | inline int empty() const { return mpStart == mpEnd; }\ | |
361 | \ | |
362 | inline reference operator[](size_type n) { return *(mpStart+n); }\ | |
363 | \ | |
364 | inline const_reference operator[](size_type n) const { return *(mpStart+n); }\ | |
365 | \ | |
366 | inline reference front() { return (*mpStart); }\ | |
367 | \ | |
368 | inline const_reference front() const { return (*mpStart); }\ | |
369 | \ | |
370 | inline reference back() { return (*(mpEnd-1)); }\ | |
371 | \ | |
372 | inline const_reference back() const { return (*(mpEnd-1)); }\ | |
373 | \ | |
374 | inline void reserve(size_type n) {}\ | |
375 | \ | |
376 | inline void push_back(const value_type& x)\ | |
377 | {\ | |
378 | if ( mpEnd != mpEndOfBuf ) \ | |
379 | {\ | |
380 | new (mpEnd) value_type(x);\ | |
381 | ++mpEnd;\ | |
382 | }\ | |
383 | else\ | |
384 | DoInsert( mpEnd, x );\ | |
385 | }\ | |
386 | \ | |
387 | inline iterator insert(iterator position, const value_type& x = value_type())\ | |
388 | {\ | |
389 | if ( position == mpEnd && mpEnd != mpEndOfBuf )\ | |
390 | {\ | |
391 | new (mpEnd) value_type(x);\ | |
392 | ++mpEnd;\ | |
393 | return (mpEnd-1);\ | |
394 | }\ | |
395 | else return DoInsert( position, x );\ | |
396 | }\ | |
397 | \ | |
398 | inline void pop_back()\ | |
399 | {\ | |
400 | DestructRange( mpEnd-1, mpEnd );\ | |
401 | \ | |
402 | --mpEnd;\ | |
403 | }\ | |
404 | \ | |
405 | inline void erase(iterator first, iterator last)\ | |
406 | {\ | |
407 | if ( last == mpEnd )\ | |
408 | {\ | |
409 | DestructRange( first, last );\ | |
410 | mpEnd = first;\ | |
411 | return;\ | |
412 | }\ | |
413 | \ | |
414 | CopyObjects( last, last + difference_type( mpEnd - last ), first );\ | |
415 | \ | |
416 | iterator newEnd = mpEnd - difference_type( last - first );\ | |
417 | DestructRange( newEnd, mpEnd );\ | |
418 | \ | |
419 | mpEnd = newEnd;\ | |
420 | }\ | |
421 | \ | |
422 | inline void erase( iterator position )\ | |
423 | {\ | |
424 | erase( position, position + 1 );\ | |
425 | }\ | |
426 | \ | |
427 | inline void sort()\ | |
428 | {\ | |
429 | if ( size() < 2 ) return;\ | |
430 | quick_sort( 0, size()-1 );\ | |
431 | }\ | |
432 | } | |
433 | ||
434 | /////////////////////////////// shallow-copy container /////////////////////// | |
435 | ||
436 | #define __DEFINE_STL_VECTOR_SHALLOW( vectorClass, Type ) class vectorClass {\ | |
437 | \ | |
438 | public:\ | |
439 | typedef Type value_type;\ | |
440 | typedef value_type* iterator;\ | |
441 | typedef const value_type* const_iterator;\ | |
442 | typedef iterator pointer;\ | |
443 | typedef const iterator const_pointer;\ | |
444 | typedef value_type& reference;\ | |
445 | typedef const value_type& const_reference;\ | |
446 | typedef size_t size_type;\ | |
447 | typedef ptrdiff_t difference_type;\ | |
448 | \ | |
449 | typedef iterator OutputIterator;\ | |
450 | typedef const_iterator InputIterator;\ | |
451 | \ | |
452 | protected:\ | |
453 | \ | |
454 | inline void PlacementCopy( const_iterator first, const_iterator last, iterator result )\ | |
455 | {\ | |
456 | memcpy(result, first, int(difference_type(last-first)*sizeof(value_type)) );\ | |
457 | }\ | |
458 | \ | |
459 | inline void ConstructObjects( iterator first, iterator last, const value_type& pattern )\ | |
460 | {\ | |
461 | if ( sizeof(pattern) == 1 )\ | |
462 | \ | |
463 | memset( first, int(difference_type(last-first)/sizeof(value_type)), \ | |
464 | int(*((char*)&pattern)) );\ | |
465 | else\ | |
466 | while( first != last ) \ | |
467 | *first++ = pattern;\ | |
468 | }\ | |
469 | \ | |
470 | inline void CopyObjects( iterator first, iterator last, iterator result )\ | |
471 | {\ | |
472 | memcpy(result, first, int(difference_type(last-first)*sizeof(value_type)) );\ | |
473 | }\ | |
474 | \ | |
475 | inline void CopyObjectsBack( iterator first, iterator last, iterator result )\ | |
476 | {\ | |
477 | memmove(result, first, int(difference_type(last-first)*sizeof(value_type)) );\ | |
478 | }\ | |
479 | \ | |
480 | public:\ | |
481 | \ | |
482 | class reverse_iterator \ | |
483 | {\ | |
484 | friend class vectorClass;\ | |
485 | friend class const_reverse_iterator;\ | |
486 | \ | |
487 | public:\ | |
488 | iterator mpPos;\ | |
489 | \ | |
490 | public:\ | |
491 | \ | |
492 | reverse_iterator() {}\ | |
493 | \ | |
494 | reverse_iterator ( iterator pPos )\ | |
495 | {\ | |
496 | mpPos = pPos;\ | |
497 | }\ | |
498 | \ | |
499 | int operator==( const reverse_iterator& rhs ) const { return (mpPos == rhs.mpPos); }\ | |
500 | int operator!=( const reverse_iterator& rhs ) const { return (mpPos != rhs.mpPos); }\ | |
501 | \ | |
502 | inline reverse_iterator( const reverse_iterator& other )\ | |
503 | {\ | |
504 | mpPos = other.mpPos;\ | |
505 | }\ | |
506 | \ | |
507 | inline const reverse_iterator& operator--() \ | |
508 | {\ | |
509 | --mpPos;\ | |
510 | return *this;\ | |
511 | }\ | |
512 | \ | |
513 | inline reverse_iterator operator--(int)\ | |
514 | {\ | |
515 | reverse_iterator tmp = *this;\ | |
516 | --mpPos;\ | |
517 | return tmp;\ | |
518 | }\ | |
519 | \ | |
520 | inline const reverse_iterator & operator++() \ | |
521 | {\ | |
522 | ++mpPos;\ | |
523 | return *this;\ | |
524 | }\ | |
525 | \ | |
526 | inline reverse_iterator operator++(int)\ | |
527 | {\ | |
528 | reverse_iterator tmp = *this;\ | |
529 | ++mpPos;\ | |
530 | return tmp;\ | |
531 | }\ | |
532 | \ | |
533 | inline const_reference operator*() const { return *mpPos; }\ | |
534 | };\ | |
535 | \ | |
536 | \ | |
537 | class const_reverse_iterator \ | |
538 | {\ | |
539 | protected:\ | |
540 | iterator mpPos;\ | |
541 | public:\ | |
542 | \ | |
543 | const_reverse_iterator() {}\ | |
544 | \ | |
545 | const_reverse_iterator( const iterator pPos )\ | |
546 | {\ | |
547 | mpPos = pPos;\ | |
548 | }\ | |
549 | \ | |
550 | int operator==( const const_reverse_iterator& rhs ) const { return (mpPos == rhs.mpPos); }\ | |
551 | int operator!=( const const_reverse_iterator& rhs ) const { return (mpPos != rhs.mpPos); }\ | |
552 | \ | |
553 | inline const_reverse_iterator( const reverse_iterator& other )\ | |
554 | {\ | |
555 | mpPos = other.mpPos;\ | |
556 | }\ | |
557 | \ | |
558 | inline const const_reverse_iterator& operator--() \ | |
559 | {\ | |
560 | --mpPos;\ | |
561 | return *this;\ | |
562 | }\ | |
563 | \ | |
564 | inline const_reverse_iterator operator--(int)\ | |
565 | {\ | |
566 | const_reverse_iterator tmp = *this;\ | |
567 | --mpPos;\ | |
568 | return tmp;\ | |
569 | }\ | |
570 | \ | |
571 | inline const const_reverse_iterator & operator++() \ | |
572 | {\ | |
573 | ++mpPos;\ | |
574 | return *this;\ | |
575 | }\ | |
576 | \ | |
577 | inline const_reverse_iterator operator++(int)\ | |
578 | {\ | |
579 | const_reverse_iterator tmp = *this;\ | |
580 | ++mpPos;\ | |
581 | return tmp;\ | |
582 | }\ | |
583 | \ | |
584 | inline const_reference operator*() const { return *mpPos; }\ | |
585 | };\ | |
586 | \ | |
587 | protected:\ | |
588 | \ | |
589 | pointer mpStart;\ | |
590 | pointer mpEnd;\ | |
591 | pointer mpEndOfBuf;\ | |
592 | \ | |
593 | protected:\ | |
594 | \ | |
595 | inline void quick_sort(int low, int hi) \ | |
596 | {\ | |
597 | int pivot_index;\ | |
598 | int left, right;\ | |
599 | \ | |
600 | pivot_index = ( !(mpStart[low] < mpStart[low+1])) ? low : (low+1);\ | |
601 | value_type pivot_value = mpStart[pivot_index];\ | |
602 | \ | |
603 | left = low; right = hi;\ | |
604 | do \ | |
605 | {\ | |
606 | while ((left <= hi) && (mpStart[left] < pivot_value)) left++;\ | |
607 | \ | |
608 | while ((right >= low) && (pivot_value < mpStart[right])) right--;\ | |
609 | \ | |
610 | if (left <= right) \ | |
611 | {\ | |
612 | value_type tmp = mpStart[left];\ | |
613 | mpStart[left] = mpStart[right];\ | |
614 | mpStart[right] = tmp;\ | |
615 | \ | |
616 | left++;\ | |
617 | right--;\ | |
618 | }\ | |
619 | \ | |
620 | } while (left <= right);\ | |
621 | if (low < right) quick_sort(low, right);\ | |
622 | if (left < hi) quick_sort(left, hi);\ | |
623 | }\ | |
624 | \ | |
625 | inline void DestructRange( iterator first, iterator last )\ | |
626 | {\ | |
627 | }\ | |
628 | \ | |
629 | inline iterator DoInsert(iterator position, const value_type& x)\ | |
630 | {\ | |
631 | if ( mpEnd < mpEndOfBuf )\ | |
632 | {\ | |
633 | new (mpEnd) value_type(*(mpEnd-1) );\ | |
634 | \ | |
635 | CopyObjectsBack( position, mpEnd, position + 1 );\ | |
636 | \ | |
637 | *position = x;\ | |
638 | \ | |
639 | ++mpEnd;\ | |
640 | \ | |
641 | return position;\ | |
642 | }\ | |
643 | \ | |
644 | size_type minBufLen = WXSTL_VECTOR_MIN_BUF_SIZE/sizeof(value_type);\ | |
645 | \ | |
646 | size_type doubledSize = size()*2;\ | |
647 | \ | |
648 | size_type newLen = ( doubledSize < minBufLen ) ? minBufLen : doubledSize;\ | |
649 | \ | |
650 | iterator pNewStart = (iterator)( new char[newLen*sizeof(value_type)] );\ | |
651 | \ | |
652 | PlacementCopy( mpStart, position, pNewStart );\ | |
653 | \ | |
654 | iterator atPosition = pNewStart + difference_type( position - mpStart );\ | |
655 | \ | |
656 | new (atPosition) value_type(x);\ | |
657 | \ | |
658 | iterator newPos = atPosition;\ | |
659 | \ | |
660 | ++atPosition;\ | |
661 | \ | |
662 | if ( mpStart ) \ | |
663 | {\ | |
664 | PlacementCopy( position, mpEnd, atPosition );\ | |
665 | DestructRange( mpStart, mpEnd );\ | |
666 | delete [](char*)mpStart;\ | |
667 | }\ | |
668 | \ | |
669 | mpEnd = atPosition + difference_type( mpEnd - position );\ | |
670 | \ | |
671 | mpStart = pNewStart;\ | |
672 | mpEndOfBuf = pNewStart + newLen;\ | |
673 | \ | |
674 | return newPos;\ | |
675 | }\ | |
676 | \ | |
677 | public:\ | |
678 | \ | |
679 | inline vectorClass() : mpStart(0), \ | |
680 | mpEnd(0),\ | |
681 | mpEndOfBuf(0)\ | |
682 | {}\ | |
683 | \ | |
684 | inline vectorClass( const_iterator first, const_iterator last )\ | |
685 | : mpStart(0),\ | |
686 | mpEnd(0),\ | |
687 | mpEndOfBuf(0)\ | |
688 | \ | |
689 | { while( first != last ) push_back( *first++ ); }\ | |
690 | \ | |
691 | inline vectorClass( size_type n, const value_type& value = value_type() )\ | |
692 | : mpStart(0),\ | |
693 | mpEnd(0),\ | |
694 | mpEndOfBuf(0)\ | |
695 | \ | |
696 | { for( size_type i = 0; i != n; ++i ) push_back( value ); }\ | |
697 | \ | |
698 | inline int operator==( const vectorClass& other )\ | |
699 | {\ | |
700 | size_type sz = size();\ | |
701 | \ | |
702 | if ( sz != other.size() ) return 0;\ | |
703 | \ | |
704 | for( size_type i = 0; i != sz; ++i )\ | |
705 | \ | |
706 | if ( !( (*this)[i] == other[i] ) ) return 0;\ | |
707 | \ | |
708 | return 1;\ | |
709 | \ | |
710 | }\ | |
711 | \ | |
712 | inline const vectorClass& operator=( const vectorClass& other )\ | |
713 | {\ | |
714 | if (mpStart) \ | |
715 | {\ | |
716 | DestructRange( begin(), end() );\ | |
717 | delete [](char*)mpStart; \ | |
718 | }\ | |
719 | \ | |
720 | size_t newLen = difference_type( other.mpEndOfBuf - other.mpStart );\ | |
721 | \ | |
722 | mpStart = (iterator)( new char[newLen*sizeof(value_type)] );\ | |
723 | \ | |
724 | PlacementCopy( other.begin(), other.end(), mpStart );\ | |
725 | \ | |
726 | mpEnd = mpStart + other.size();\ | |
727 | \ | |
728 | mpEndOfBuf = mpStart + newLen;\ | |
729 | \ | |
730 | return *this;\ | |
731 | }\ | |
732 | \ | |
733 | inline vectorClass( const vectorClass& other )\ | |
734 | : mpStart(0),\ | |
735 | mpEnd(0),\ | |
736 | mpEndOfBuf(0)\ | |
737 | {\ | |
738 | this->operator=( other );\ | |
739 | }\ | |
740 | \ | |
741 | inline ~vectorClass() \ | |
742 | { \ | |
743 | if (mpStart) \ | |
744 | {\ | |
745 | DestructRange( begin(), end() );\ | |
746 | delete [](char*)mpStart; \ | |
747 | }\ | |
748 | }\ | |
749 | \ | |
750 | inline iterator begin() { return mpStart; }\ | |
751 | \ | |
752 | inline const_iterator begin() const { return mpStart; }\ | |
753 | \ | |
754 | inline iterator end() { return mpEnd; }\ | |
755 | \ | |
756 | inline const_iterator end() const { return mpEnd; }\ | |
757 | \ | |
758 | inline size_type size() const { return (size_type)difference_type(mpEnd-mpStart); }\ | |
759 | \ | |
760 | inline size_type max_size() const { return UINT_MAX/sizeof(value_type); }\ | |
761 | \ | |
762 | inline size_type capacity() const \ | |
763 | { return difference_type(mpEndOfBuf-mpStart)/sizeof(value_type); }\ | |
764 | \ | |
765 | inline int empty() const { return mpStart == mpEnd; }\ | |
766 | \ | |
767 | inline reference operator[](size_type n) { return *(mpStart+n); }\ | |
768 | \ | |
769 | inline const_reference operator[](size_type n) const { return *(mpStart+n); }\ | |
770 | \ | |
771 | inline reference front() { return (*mpStart); }\ | |
772 | \ | |
773 | inline const_reference front() const { return (*mpStart); }\ | |
774 | \ | |
775 | inline reference back() { return (*(mpEnd-1)); }\ | |
776 | \ | |
777 | inline const_reference back() const { return (*(mpEnd-1)); }\ | |
778 | \ | |
779 | inline void reserve(size_type n) {}\ | |
780 | \ | |
781 | inline void push_back(const value_type& x)\ | |
782 | {\ | |
783 | if ( mpEnd != mpEndOfBuf ) \ | |
784 | {\ | |
785 | new (mpEnd) value_type(x);\ | |
786 | ++mpEnd;\ | |
787 | }\ | |
788 | else\ | |
789 | DoInsert( mpEnd, x );\ | |
790 | }\ | |
791 | \ | |
792 | inline iterator insert(iterator position, const value_type& x = value_type())\ | |
793 | {\ | |
794 | if ( position == mpEnd && mpEnd != mpEndOfBuf )\ | |
795 | {\ | |
796 | new (mpEnd) value_type(x);\ | |
797 | ++mpEnd;\ | |
798 | return (mpEnd-1);\ | |
799 | }\ | |
800 | else return DoInsert( position, x );\ | |
801 | }\ | |
802 | \ | |
803 | inline void pop_back()\ | |
804 | {\ | |
805 | DestructRange( mpEnd-1, mpEnd );\ | |
806 | \ | |
807 | --mpEnd;\ | |
808 | }\ | |
809 | \ | |
810 | inline void erase(iterator first, iterator last)\ | |
811 | {\ | |
812 | if ( last == mpEnd )\ | |
813 | {\ | |
814 | DestructRange( first, last );\ | |
815 | mpEnd = first;\ | |
816 | return;\ | |
817 | }\ | |
818 | \ | |
819 | CopyObjects( last, last + difference_type( mpEnd - last ), first );\ | |
820 | \ | |
821 | iterator newEnd = mpEnd - difference_type( last - first );\ | |
822 | DestructRange( newEnd, mpEnd );\ | |
823 | \ | |
824 | mpEnd = newEnd;\ | |
825 | }\ | |
826 | \ | |
827 | inline void erase( iterator position )\ | |
828 | {\ | |
829 | erase( position, position + 1 );\ | |
830 | }\ | |
831 | \ | |
832 | inline void sort()\ | |
833 | {\ | |
834 | if ( size() < 2 ) return;\ | |
835 | quick_sort( 0, size()-1 );\ | |
836 | }\ | |
837 | } | |
838 | ||
839 | ||
840 | ||
841 | // redefine below symbol to change the default allocation unit of vector content buffer | |
842 | #define WXSTL_VECTOR_MIN_BUF_SIZE 64 | |
843 | ||
844 | // defines vector class, where objects are copied | |
845 | // using "deep-copy" sematics (i.e. by calling their copy constructors) | |
846 | ||
847 | #define WXSTL_VECTOR(ELEMENT_CLASS) \ | |
848 | __DEFINE_STL_VECTOR_DEEP(_WXSTL_VECTOR_##ELEMENT_CLASS, ELEMENT_CLASS) | |
849 | ||
850 | // defines vector class, where objects are copied | |
851 | // using "shallow-copy" sematics (i.e. instead of calling | |
852 | // their constructors, memcpy() and memmove() are used to copy their raw data) | |
853 | ||
854 | ||
855 | #define WXSTL_VECTOR_SHALLOW_COPY(ELEMENT_CLASS) __DEFINE_STL_VECTOR_SHALLOW(_WXSTL_VECTORSC_##ELEMENT_CLASS, ELEMENT_CLASS) | |
856 | ||
857 | #endif |