]> git.saurik.com Git - wxWidgets.git/blob - utils/HelpGen/include/wxstlvec.h
wxComboBox included (removed dependency on wxUSE_COMBOBOX which was never
[wxWidgets.git] / utils / HelpGen / include / wxstlvec.h
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 }\
186 \
187 inline void DestructRange( iterator first, iterator last )\
188 {\
189 typedef value_type value_type_local;\
190 \
191 while ( first != last ) \
192 {\
193 first->value_type_local::~value_type_local();\
194 ++first;\
195 }\
196 }\
197 \
198 inline iterator DoInsert(iterator position, const value_type& x)\
199 {\
200 if ( mpEnd < mpEndOfBuf )\
201 {\
202 new (mpEnd) value_type(*(mpEnd-1) );\
203 \
204 CopyObjectsBack( position, mpEnd, position + 1 );\
205 \
206 *position = x;\
207 \
208 ++mpEnd;\
209 \
210 return position;\
211 }\
212 \
213 size_type minBufLen = WXSTL_VECTOR_MIN_BUF_SIZE/sizeof(value_type);\
214 \
215 size_type doubledSize = size()*2;\
216 \
217 size_type newLen = ( doubledSize < minBufLen ) ? minBufLen : doubledSize;\
218 \
219 iterator pNewStart = (iterator)( new char[newLen*sizeof(value_type)] );\
220 \
221 PlacementCopy( mpStart, position, pNewStart );\
222 \
223 iterator atPosition = pNewStart + difference_type( position - mpStart );\
224 \
225 new (atPosition) value_type(x);\
226 \
227 iterator newPos = atPosition;\
228 \
229 ++atPosition;\
230 \
231 if ( mpStart ) \
232 {\
233 PlacementCopy( position, mpEnd, atPosition );\
234 DestructRange( mpStart, mpEnd );\
235 delete [](char*)mpStart;\
236 }\
237 \
238 mpEnd = atPosition + difference_type( mpEnd - position );\
239 \
240 mpStart = pNewStart;\
241 mpEndOfBuf = pNewStart + newLen;\
242 \
243 return newPos;\
244 }\
245 \
246 public:\
247 \
248 inline vectorClass() : mpStart(0), \
249 mpEnd(0),\
250 mpEndOfBuf(0)\
251 {}\
252 \
253 inline vectorClass( const_iterator first, const_iterator last )\
254 : mpStart(0),\
255 mpEnd(0),\
256 mpEndOfBuf(0)\
257 \
258 { while( first != last ) push_back( *first++ ); }\
259 \
260 inline vectorClass( size_type n, const value_type& value = value_type() )\
261 : mpStart(0),\
262 mpEnd(0),\
263 mpEndOfBuf(0)\
264 \
265 { for( size_type i = 0; i != n; ++i ) push_back( value ); }\
266 \
267 inline const vectorClass& operator=( const vectorClass& other )\
268 {\
269 if (mpStart) \
270 {\
271 DestructRange( begin(), end() );\
272 delete [](char*)mpStart; \
273 }\
274 \
275 size_t newLen = difference_type( other.mpEndOfBuf - other.mpStart );\
276 \
277 mpStart = (iterator)( new char[newLen*sizeof(value_type)] );\
278 \
279 PlacementCopy( other.begin(), other.end(), mpStart );\
280 \
281 mpEnd = mpStart + other.size();\
282 \
283 mpEndOfBuf = mpStart + newLen;\
284 \
285 return *this;\
286 }\
287 \
288 inline vectorClass( const vectorClass& other )\
289 : mpStart(0),\
290 mpEnd(0),\
291 mpEndOfBuf(0)\
292 {\
293 this->operator=( other );\
294 }\
295 \
296 inline ~vectorClass() \
297 { \
298 if (mpStart) \
299 {\
300 DestructRange( begin(), end() );\
301 delete [](char*)mpStart; \
302 }\
303 }\
304 \
305 inline iterator begin() { return mpStart; }\
306 \
307 inline const_iterator begin() const { return mpStart; }\
308 \
309 inline iterator end() { return mpEnd; }\
310 \
311 inline const_iterator end() const { return mpEnd; }\
312 \
313 inline size_type size() const { return (size_type)difference_type(mpEnd-mpStart); }\
314 \
315 inline size_type max_size() const { return UINT_MAX/sizeof(value_type); }\
316 \
317 inline size_type capacity() const \
318 { return difference_type(mpEndOfBuf-mpStart)/sizeof(value_type); }\
319 \
320 inline int empty() const { return mpStart == mpEnd; }\
321 \
322 inline reference operator[](size_type n) { return *(mpStart+n); }\
323 \
324 inline const_reference operator[](size_type n) const { return *(mpStart+n); }\
325 \
326 inline reference front() { return (*mpStart); }\
327 \
328 inline const_reference front() const { return (*mpStart); }\
329 \
330 inline reference back() { return (*(mpEnd-1)); }\
331 \
332 inline const_reference back() const { return (*(mpEnd-1)); }\
333 \
334 inline void reserve(size_type n) {}\
335 \
336 inline void push_back(const value_type& x)\
337 {\
338 if ( mpEnd != mpEndOfBuf ) \
339 {\
340 new (mpEnd) value_type(x);\
341 ++mpEnd;\
342 }\
343 else\
344 DoInsert( mpEnd, x );\
345 }\
346 \
347 inline iterator insert(iterator position, const value_type& x = value_type())\
348 {\
349 if ( position == mpEnd && mpEnd != mpEndOfBuf )\
350 {\
351 new (mpEnd) value_type(x);\
352 ++mpEnd;\
353 return (mpEnd-1);\
354 }\
355 else return DoInsert( position, x );\
356 }\
357 \
358 inline void pop_back()\
359 {\
360 DestructRange( mpEnd-1, mpEnd );\
361 \
362 --mpEnd;\
363 }\
364 \
365 inline void erase(iterator first, iterator last)\
366 {\
367 if ( last == mpEnd )\
368 {\
369 DestructRange( first, last );\
370 mpEnd = first;\
371 return;\
372 }\
373 \
374 CopyObjects( last, last + difference_type( mpEnd - last ), first );\
375 \
376 iterator newEnd = mpEnd - difference_type( last - first );\
377 DestructRange( newEnd, mpEnd );\
378 \
379 mpEnd = newEnd;\
380 }\
381 \
382 inline void erase( iterator position )\
383 {\
384 erase( position, position + 1 );\
385 }\
386 \
387 inline void sort()\
388 {\
389 if ( size() < 2 ) return;\
390 quick_sort( 0, size()-1 );\
391 }\
392 }
393
394 /////////////////////////////// shallow-copy container ///////////////////////
395
396 #define __DEFINE_STL_VECTOR_SHALLOW( vectorClass, Type ) class vectorClass {\
397 \
398 public:\
399 typedef Type value_type;\
400 typedef value_type* iterator;\
401 typedef const value_type* const_iterator;\
402 typedef iterator pointer;\
403 typedef const iterator const_pointer;\
404 typedef value_type& reference;\
405 typedef const value_type& const_reference;\
406 typedef size_t size_type;\
407 typedef ptrdiff_t difference_type;\
408 \
409 typedef iterator OutputIterator;\
410 typedef const_iterator InputIterator;\
411 \
412 protected:\
413 \
414 inline void PlacementCopy( const_iterator first, const_iterator last, iterator result )\
415 {\
416 memcpy(result, first, int(difference_type(last-first)*sizeof(value_type)) );\
417 }\
418 \
419 inline void ConstructObjects( iterator first, iterator last, const value_type& pattern )\
420 {\
421 if ( sizeof(pattern) == 1 )\
422 \
423 memset( first, int(difference_type(last-first)/sizeof(value_type)), \
424 int(*((char*)&pattern)) );\
425 else\
426 while( first != last ) \
427 *first++ = pattern;\
428 }\
429 \
430 inline void CopyObjects( iterator first, iterator last, iterator result )\
431 {\
432 memcpy(result, first, int(difference_type(last-first)*sizeof(value_type)) );\
433 }\
434 \
435 inline void CopyObjectsBack( iterator first, iterator last, iterator result )\
436 {\
437 memmove(result, first, int(difference_type(last-first)*sizeof(value_type)) );\
438 }\
439 \
440 public:\
441 \
442 class reverse_iterator \
443 {\
444 friend class vectorClass;\
445 friend class const_reverse_iterator;\
446 \
447 public:\
448 iterator mpPos;\
449 \
450 public:\
451 \
452 reverse_iterator() {}\
453 \
454 reverse_iterator ( iterator pPos )\
455 {\
456 mpPos = pPos;\
457 }\
458 \
459 int operator==( const reverse_iterator& rhs ) const { return (mpPos == rhs.mpPos); }\
460 int operator!=( const reverse_iterator& rhs ) const { return (mpPos != rhs.mpPos); }\
461 \
462 inline reverse_iterator( const reverse_iterator& other )\
463 {\
464 mpPos = other.mpPos;\
465 }\
466 \
467 inline const reverse_iterator& operator--() \
468 {\
469 --mpPos;\
470 return *this;\
471 }\
472 \
473 inline reverse_iterator operator--(int)\
474 {\
475 reverse_iterator tmp = *this;\
476 --mpPos;\
477 return tmp;\
478 }\
479 \
480 inline const reverse_iterator & operator++() \
481 {\
482 ++mpPos;\
483 return *this;\
484 }\
485 \
486 inline reverse_iterator operator++(int)\
487 {\
488 reverse_iterator tmp = *this;\
489 ++mpPos;\
490 return tmp;\
491 }\
492 \
493 inline const_reference operator*() const { return *mpPos; }\
494 };\
495 \
496 \
497 class const_reverse_iterator \
498 {\
499 protected:\
500 iterator mpPos;\
501 public:\
502 \
503 const_reverse_iterator() {}\
504 \
505 const_reverse_iterator( const iterator pPos )\
506 {\
507 mpPos = pPos;\
508 }\
509 \
510 int operator==( const const_reverse_iterator& rhs ) const { return (mpPos == rhs.mpPos); }\
511 int operator!=( const const_reverse_iterator& rhs ) const { return (mpPos != rhs.mpPos); }\
512 \
513 inline const_reverse_iterator( const reverse_iterator& other )\
514 {\
515 mpPos = other.mpPos;\
516 }\
517 \
518 inline const const_reverse_iterator& operator--() \
519 {\
520 --mpPos;\
521 return *this;\
522 }\
523 \
524 inline const_reverse_iterator operator--(int)\
525 {\
526 const_reverse_iterator tmp = *this;\
527 --mpPos;\
528 return tmp;\
529 }\
530 \
531 inline const const_reverse_iterator & operator++() \
532 {\
533 ++mpPos;\
534 return *this;\
535 }\
536 \
537 inline const_reverse_iterator operator++(int)\
538 {\
539 const_reverse_iterator tmp = *this;\
540 ++mpPos;\
541 return tmp;\
542 }\
543 \
544 inline const_reference operator*() const { return *mpPos; }\
545 };\
546 \
547 protected:\
548 \
549 pointer mpStart;\
550 pointer mpEnd;\
551 pointer mpEndOfBuf;\
552 \
553 protected:\
554 \
555 inline void quick_sort(int low, int hi) \
556 {\
557 }\
558 \
559 inline void DestructRange( iterator first, iterator last )\
560 {\
561 }\
562 \
563 inline iterator DoInsert(iterator position, const value_type& x)\
564 {\
565 if ( mpEnd < mpEndOfBuf )\
566 {\
567 new (mpEnd) value_type(*(mpEnd-1) );\
568 \
569 CopyObjectsBack( position, mpEnd, position + 1 );\
570 \
571 *position = x;\
572 \
573 ++mpEnd;\
574 \
575 return position;\
576 }\
577 \
578 size_type minBufLen = WXSTL_VECTOR_MIN_BUF_SIZE/sizeof(value_type);\
579 \
580 size_type doubledSize = size()*2;\
581 \
582 size_type newLen = ( doubledSize < minBufLen ) ? minBufLen : doubledSize;\
583 \
584 iterator pNewStart = (iterator)( new char[newLen*sizeof(value_type)] );\
585 \
586 PlacementCopy( mpStart, position, pNewStart );\
587 \
588 iterator atPosition = pNewStart + difference_type( position - mpStart );\
589 \
590 new (atPosition) value_type(x);\
591 \
592 iterator newPos = atPosition;\
593 \
594 ++atPosition;\
595 \
596 if ( mpStart ) \
597 {\
598 PlacementCopy( position, mpEnd, atPosition );\
599 DestructRange( mpStart, mpEnd );\
600 delete [](char*)mpStart;\
601 }\
602 \
603 mpEnd = atPosition + difference_type( mpEnd - position );\
604 \
605 mpStart = pNewStart;\
606 mpEndOfBuf = pNewStart + newLen;\
607 \
608 return newPos;\
609 }\
610 \
611 public:\
612 \
613 inline vectorClass() : mpStart(0), \
614 mpEnd(0),\
615 mpEndOfBuf(0)\
616 {}\
617 \
618 inline vectorClass( const_iterator first, const_iterator last )\
619 : mpStart(0),\
620 mpEnd(0),\
621 mpEndOfBuf(0)\
622 \
623 { while( first != last ) push_back( *first++ ); }\
624 \
625 inline vectorClass( size_type n, const value_type& value = value_type() )\
626 : mpStart(0),\
627 mpEnd(0),\
628 mpEndOfBuf(0)\
629 \
630 { for( size_type i = 0; i != n; ++i ) push_back( value ); }\
631 \
632 inline const vectorClass& operator=( const vectorClass& other )\
633 {\
634 if (mpStart) \
635 {\
636 DestructRange( begin(), end() );\
637 delete [](char*)mpStart; \
638 }\
639 \
640 size_t newLen = difference_type( other.mpEndOfBuf - other.mpStart );\
641 \
642 mpStart = (iterator)( new char[newLen*sizeof(value_type)] );\
643 \
644 PlacementCopy( other.begin(), other.end(), mpStart );\
645 \
646 mpEnd = mpStart + other.size();\
647 \
648 mpEndOfBuf = mpStart + newLen;\
649 \
650 return *this;\
651 }\
652 \
653 inline vectorClass( const vectorClass& other )\
654 : mpStart(0),\
655 mpEnd(0),\
656 mpEndOfBuf(0)\
657 {\
658 this->operator=( other );\
659 }\
660 \
661 inline ~vectorClass() \
662 { \
663 if (mpStart) \
664 {\
665 DestructRange( begin(), end() );\
666 delete [](char*)mpStart; \
667 }\
668 }\
669 \
670 inline iterator begin() { return mpStart; }\
671 \
672 inline const_iterator begin() const { return mpStart; }\
673 \
674 inline iterator end() { return mpEnd; }\
675 \
676 inline const_iterator end() const { return mpEnd; }\
677 \
678 inline size_type size() const { return (size_type)difference_type(mpEnd-mpStart); }\
679 \
680 inline size_type max_size() const { return UINT_MAX/sizeof(value_type); }\
681 \
682 inline size_type capacity() const \
683 { return difference_type(mpEndOfBuf-mpStart)/sizeof(value_type); }\
684 \
685 inline int empty() const { return mpStart == mpEnd; }\
686 \
687 inline reference operator[](size_type n) { return *(mpStart+n); }\
688 \
689 inline const_reference operator[](size_type n) const { return *(mpStart+n); }\
690 \
691 inline reference front() { return (*mpStart); }\
692 \
693 inline const_reference front() const { return (*mpStart); }\
694 \
695 inline reference back() { return (*(mpEnd-1)); }\
696 \
697 inline const_reference back() const { return (*(mpEnd-1)); }\
698 \
699 inline void reserve(size_type n) {}\
700 \
701 inline void push_back(const value_type& x)\
702 {\
703 if ( mpEnd != mpEndOfBuf ) \
704 {\
705 new (mpEnd) value_type(x);\
706 ++mpEnd;\
707 }\
708 else\
709 DoInsert( mpEnd, x );\
710 }\
711 \
712 inline iterator insert(iterator position, const value_type& x = value_type())\
713 {\
714 if ( position == mpEnd && mpEnd != mpEndOfBuf )\
715 {\
716 new (mpEnd) value_type(x);\
717 ++mpEnd;\
718 return (mpEnd-1);\
719 }\
720 else return DoInsert( position, x );\
721 }\
722 \
723 inline void pop_back()\
724 {\
725 DestructRange( mpEnd-1, mpEnd );\
726 \
727 --mpEnd;\
728 }\
729 \
730 inline void erase(iterator first, iterator last)\
731 {\
732 if ( last == mpEnd )\
733 {\
734 DestructRange( first, last );\
735 mpEnd = first;\
736 return;\
737 }\
738 \
739 CopyObjects( last, last + difference_type( mpEnd - last ), first );\
740 \
741 iterator newEnd = mpEnd - difference_type( last - first );\
742 DestructRange( newEnd, mpEnd );\
743 \
744 mpEnd = newEnd;\
745 }\
746 \
747 inline void erase( iterator position )\
748 {\
749 erase( position, position + 1 );\
750 }\
751 \
752 inline void sort()\
753 {\
754 if ( size() < 2 ) return;\
755 quick_sort( 0, size()-1 );\
756 }\
757 }
758
759
760
761 // redefine below symbol to change the default allocation unit of vector content buffer
762 #define WXSTL_VECTOR_MIN_BUF_SIZE 64
763
764 // defines vector class, where objects are copied
765 // using "deep-copy" sematics (i.e. by calling their copy constructors)
766
767 #define WXSTL_VECTOR(ELEMENT_CLASS) \
768 __DEFINE_STL_VECTOR_DEEP(_WXSTL_VECTOR_##ELEMENT_CLASS, ELEMENT_CLASS)
769
770 // defines vector class, where objects are copied
771 // using "shallow-copy" sematics (i.e. instead of calling
772 // their constructors, memcpy() and memmove() are used to copy their raw data)
773
774
775 #define WXSTL_VECTOR_SHALLOW_COPY(ELEMENT_CLASS) __DEFINE_STL_VECTOR_SHALLOW(_WXSTL_VECTORSC_##ELEMENT_CLASS, ELEMENT_CLASS)
776
777 #endif