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