]>
git.saurik.com Git - wxWidgets.git/blob - utils/HelpGen/src/wxstlvec.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Contrib. demo
4 // Author: Aleksandras Gluchovas
8 // Copyright: (c) Aleskandars Gluchovas
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef __WXSTLVEC_G__
13 #define __WXSTLVEC_G__
20 #include <string.h> // imports memmove()
22 #if !defined(__WXMAC__) || defined(__DARWIN__)
23 # include <sys/types.h>
28 // the below macro used internally (see actual interface after this macro)
30 #define __DEFINE_STL_VECTOR_DEEP( vectorClass, Type ) class vectorClass {\
33 typedef Type value_type;\
34 typedef value_type* iterator;\
35 typedef const value_type* const_iterator;\
36 typedef iterator pointer;\
37 typedef const iterator const_pointer;\
38 typedef value_type& reference;\
39 typedef const value_type& const_reference;\
40 typedef size_t size_type;\
41 typedef ptrdiff_t difference_type;\
43 typedef iterator OutputIterator;\
44 typedef const_iterator InputIterator;\
48 inline void PlacementCopy( const_iterator first, const_iterator last, iterator result )\
50 while ( first != last ) \
51 new (result++) value_type(*first++);\
54 inline void ConstructObjects( iterator first, iterator last, const value_type& pattern )\
56 while( first != last ) \
57 new (first++) value_type(pattern);\
60 inline void CopyObjects( iterator first, iterator last, iterator result )\
62 while( first != last ) \
63 *result++ = *first++;\
66 inline void CopyObjectsBack( iterator first, iterator last, iterator result )\
68 result += difference_type(last,first);\
70 while( first != last ) \
71 *(--result) = *(--last);\
76 class reverse_iterator \
78 friend class vectorClass;\
79 friend class const_reverse_iterator;\
86 reverse_iterator() {}\
88 reverse_iterator ( iterator pPos )\
93 int operator==( const reverse_iterator& rhs ) const { return (mpPos == rhs.mpPos); }\
94 int operator!=( const reverse_iterator& rhs ) const { return (mpPos != rhs.mpPos); }\
96 inline reverse_iterator( const reverse_iterator& other )\
101 inline const reverse_iterator& operator--() \
107 inline reverse_iterator operator--(int)\
109 reverse_iterator tmp = *this;\
114 inline const reverse_iterator & operator++() \
120 inline reverse_iterator operator++(int)\
122 reverse_iterator tmp = *this;\
127 inline const_reference operator*() const { return *mpPos; }\
131 class const_reverse_iterator \
137 const_reverse_iterator() {}\
139 const_reverse_iterator( const iterator pPos )\
144 int operator==( const const_reverse_iterator& rhs ) const { return (mpPos == rhs.mpPos); }\
145 int operator!=( const const_reverse_iterator& rhs ) const { return (mpPos != rhs.mpPos); }\
147 inline const_reverse_iterator( const reverse_iterator& other )\
149 mpPos = other.mpPos;\
152 inline const const_reverse_iterator& operator--() \
158 inline const_reverse_iterator operator--(int)\
160 const_reverse_iterator tmp = *this;\
165 inline const const_reverse_iterator & operator++() \
171 inline const_reverse_iterator operator++(int)\
173 const_reverse_iterator tmp = *this;\
178 inline const_reference operator*() const { return *mpPos; }\
189 inline void quick_sort(int WXUNUSED(low), int WXUNUSED(hi)) \
193 inline void DestructRange( iterator first, iterator last )\
195 typedef value_type value_type_local;\
197 while ( first != last ) \
199 first->value_type_local::~value_type_local();\
204 inline iterator DoInsert(iterator position, const value_type& x)\
206 if ( mpEnd < mpEndOfBuf )\
208 new (mpEnd) value_type(*(mpEnd-1) );\
210 CopyObjectsBack( position, mpEnd, position + 1 );\
219 size_type minBufLen = WXSTL_VECTOR_MIN_BUF_SIZE/sizeof(value_type);\
221 size_type doubledSize = size()*2;\
223 size_type newLen = ( doubledSize < minBufLen ) ? minBufLen : doubledSize;\
225 iterator pNewStart = (iterator)( new char[newLen*sizeof(value_type)] );\
227 PlacementCopy( mpStart, position, pNewStart );\
229 iterator atPosition = pNewStart + difference_type( position - mpStart );\
231 new (atPosition) value_type(x);\
233 iterator newPos = atPosition;\
239 PlacementCopy( position, mpEnd, atPosition );\
240 DestructRange( mpStart, mpEnd );\
241 delete [](char*)mpStart;\
244 mpEnd = atPosition + difference_type( mpEnd - position );\
246 mpStart = pNewStart;\
247 mpEndOfBuf = pNewStart + newLen;\
254 inline vectorClass() : mpStart(0), \
259 inline vectorClass( const_iterator first, const_iterator last )\
264 { while( first != last ) push_back( *first++ ); }\
266 inline vectorClass( size_type n, const value_type& value = value_type() )\
271 { for( size_type i = 0; i != n; ++i ) push_back( value ); }\
273 inline const vectorClass& operator=( const vectorClass& other )\
277 DestructRange( begin(), end() );\
278 delete [](char*)mpStart; \
281 size_t newLen = difference_type( other.mpEndOfBuf - other.mpStart );\
283 mpStart = (iterator)( new char[newLen*sizeof(value_type)] );\
285 PlacementCopy( other.begin(), other.end(), mpStart );\
287 mpEnd = mpStart + other.size();\
289 mpEndOfBuf = mpStart + newLen;\
294 inline vectorClass( const vectorClass& other )\
299 this->operator=( other );\
302 inline ~vectorClass() \
306 DestructRange( begin(), end() );\
307 delete [](char*)mpStart; \
311 inline iterator begin() { return mpStart; }\
313 inline const_iterator begin() const { return mpStart; }\
315 inline iterator end() { return mpEnd; }\
317 inline const_iterator end() const { return mpEnd; }\
319 inline size_type size() const { return (size_type)difference_type(mpEnd-mpStart); }\
321 inline size_type max_size() const { return UINT_MAX/sizeof(value_type); }\
323 inline size_type capacity() const \
324 { return difference_type(mpEndOfBuf-mpStart)/sizeof(value_type); }\
326 inline int empty() const { return mpStart == mpEnd; }\
328 inline reference operator[](size_type n) { return *(mpStart+n); }\
330 inline const_reference operator[](size_type n) const { return *(mpStart+n); }\
332 inline reference front() { return (*mpStart); }\
334 inline const_reference front() const { return (*mpStart); }\
336 inline reference back() { return (*(mpEnd-1)); }\
338 inline const_reference back() const { return (*(mpEnd-1)); }\
340 inline void reserve(size_type WXUNUSED(n)) {}\
342 inline void push_back(const value_type& x)\
344 if ( mpEnd != mpEndOfBuf ) \
346 new (mpEnd) value_type(x);\
350 DoInsert( mpEnd, x );\
353 inline iterator insert(iterator position, const value_type& x = value_type())\
355 if ( position == mpEnd && mpEnd != mpEndOfBuf )\
357 new (mpEnd) value_type(x);\
361 else return DoInsert( position, x );\
364 inline void pop_back()\
366 DestructRange( mpEnd-1, mpEnd );\
371 inline void erase(iterator first, iterator last)\
373 if ( last == mpEnd )\
375 DestructRange( first, last );\
380 CopyObjects( last, last + difference_type( mpEnd - last ), first );\
382 iterator newEnd = mpEnd - difference_type( last - first );\
383 DestructRange( newEnd, mpEnd );\
388 inline void erase( iterator position )\
390 erase( position, position + 1 );\
395 if ( size() < 2 ) return;\
396 quick_sort( 0, size()-1 );\
400 /////////////////////////////// shallow-copy container ///////////////////////
402 #define __DEFINE_STL_VECTOR_SHALLOW( vectorClass, Type ) class vectorClass {\
405 typedef Type value_type;\
406 typedef value_type* iterator;\
407 typedef const value_type* const_iterator;\
408 typedef iterator pointer;\
409 typedef const iterator const_pointer;\
410 typedef value_type& reference;\
411 typedef const value_type& const_reference;\
412 typedef size_t size_type;\
413 typedef ptrdiff_t difference_type;\
415 typedef iterator OutputIterator;\
416 typedef const_iterator InputIterator;\
420 inline void PlacementCopy( const_iterator first, const_iterator last, iterator result )\
422 memcpy(result, first, int(difference_type(last-first)*sizeof(value_type)) );\
425 inline void ConstructObjects( iterator first, iterator last, const value_type& pattern )\
427 if ( sizeof(pattern) == 1 )\
429 memset( first, int(difference_type(last-first)/sizeof(value_type)), \
430 int(*((char*)&pattern)) );\
432 while( first != last ) \
436 inline void CopyObjects( iterator first, iterator last, iterator result )\
438 memcpy(result, first, int(difference_type(last-first)*sizeof(value_type)) );\
441 inline void CopyObjectsBack( iterator first, iterator last, iterator result )\
443 memmove(result, first, int(difference_type(last-first)*sizeof(value_type)) );\
448 class reverse_iterator \
450 friend class vectorClass;\
451 friend class const_reverse_iterator;\
458 reverse_iterator() {}\
460 reverse_iterator ( iterator pPos )\
465 int operator==( const reverse_iterator& rhs ) const { return (mpPos == rhs.mpPos); }\
466 int operator!=( const reverse_iterator& rhs ) const { return (mpPos != rhs.mpPos); }\
468 inline reverse_iterator( const reverse_iterator& other )\
470 mpPos = other.mpPos;\
473 inline const reverse_iterator& operator--() \
479 inline reverse_iterator operator--(int)\
481 reverse_iterator tmp = *this;\
486 inline const reverse_iterator & operator++() \
492 inline reverse_iterator operator++(int)\
494 reverse_iterator tmp = *this;\
499 inline const_reference operator*() const { return *mpPos; }\
503 class const_reverse_iterator \
509 const_reverse_iterator() {}\
511 const_reverse_iterator( const iterator pPos )\
516 int operator==( const const_reverse_iterator& rhs ) const { return (mpPos == rhs.mpPos); }\
517 int operator!=( const const_reverse_iterator& rhs ) const { return (mpPos != rhs.mpPos); }\
519 inline const_reverse_iterator( const reverse_iterator& other )\
521 mpPos = other.mpPos;\
524 inline const const_reverse_iterator& operator--() \
530 inline const_reverse_iterator operator--(int)\
532 const_reverse_iterator tmp = *this;\
537 inline const const_reverse_iterator & operator++() \
543 inline const_reverse_iterator operator++(int)\
545 const_reverse_iterator tmp = *this;\
550 inline const_reference operator*() const { return *mpPos; }\
561 inline void quick_sort(int WXUNUSED(low), int WXUNUSED(hi)) \
565 inline void DestructRange( iterator WXUNUSED(first), iterator WXUNUSED(last))\
569 inline iterator DoInsert(iterator position, const value_type& x)\
571 if ( mpEnd < mpEndOfBuf )\
573 new (mpEnd) value_type(*(mpEnd-1) );\
575 CopyObjectsBack( position, mpEnd, position + 1 );\
584 size_type minBufLen = WXSTL_VECTOR_MIN_BUF_SIZE/sizeof(value_type);\
586 size_type doubledSize = size()*2;\
588 size_type newLen = ( doubledSize < minBufLen ) ? minBufLen : doubledSize;\
590 iterator pNewStart = (iterator)( new char[newLen*sizeof(value_type)] );\
592 PlacementCopy( mpStart, position, pNewStart );\
594 iterator atPosition = pNewStart + difference_type( position - mpStart );\
596 new (atPosition) value_type(x);\
598 iterator newPos = atPosition;\
604 PlacementCopy( position, mpEnd, atPosition );\
605 DestructRange( mpStart, mpEnd );\
606 delete [](char*)mpStart;\
609 mpEnd = atPosition + difference_type( mpEnd - position );\
611 mpStart = pNewStart;\
612 mpEndOfBuf = pNewStart + newLen;\
619 inline vectorClass() : mpStart(0), \
624 inline vectorClass( const_iterator first, const_iterator last )\
629 { while( first != last ) push_back( *first++ ); }\
631 inline vectorClass( size_type n, const value_type& value = value_type() )\
636 { for( size_type i = 0; i != n; ++i ) push_back( value ); }\
638 inline const vectorClass& operator=( const vectorClass& other )\
642 DestructRange( begin(), end() );\
643 delete [](char*)mpStart; \
646 size_t newLen = difference_type( other.mpEndOfBuf - other.mpStart );\
648 mpStart = (iterator)( new char[newLen*sizeof(value_type)] );\
650 PlacementCopy( other.begin(), other.end(), mpStart );\
652 mpEnd = mpStart + other.size();\
654 mpEndOfBuf = mpStart + newLen;\
659 inline vectorClass( const vectorClass& other )\
664 this->operator=( other );\
667 inline ~vectorClass() \
671 DestructRange( begin(), end() );\
672 delete [](char*)mpStart; \
676 inline iterator begin() { return mpStart; }\
678 inline const_iterator begin() const { return mpStart; }\
680 inline iterator end() { return mpEnd; }\
682 inline const_iterator end() const { return mpEnd; }\
684 inline size_type size() const { return (size_type)difference_type(mpEnd-mpStart); }\
686 inline size_type max_size() const { return UINT_MAX/sizeof(value_type); }\
688 inline size_type capacity() const \
689 { return difference_type(mpEndOfBuf-mpStart)/sizeof(value_type); }\
691 inline int empty() const { return mpStart == mpEnd; }\
693 inline reference operator[](size_type n) { return *(mpStart+n); }\
695 inline const_reference operator[](size_type n) const { return *(mpStart+n); }\
697 inline reference front() { return (*mpStart); }\
699 inline const_reference front() const { return (*mpStart); }\
701 inline reference back() { return (*(mpEnd-1)); }\
703 inline const_reference back() const { return (*(mpEnd-1)); }\
705 inline void reserve(size_type WXUNUSED(n)) {}\
707 inline void push_back(const value_type& x)\
709 if ( mpEnd != mpEndOfBuf ) \
711 new (mpEnd) value_type(x);\
715 DoInsert( mpEnd, x );\
718 inline iterator insert(iterator position, const value_type& x = value_type())\
720 if ( position == mpEnd && mpEnd != mpEndOfBuf )\
722 new (mpEnd) value_type(x);\
726 else return DoInsert( position, x );\
729 inline void pop_back()\
731 DestructRange( mpEnd-1, mpEnd );\
736 inline void erase(iterator first, iterator last)\
738 if ( last == mpEnd )\
740 DestructRange( first, last );\
745 CopyObjects( last, last + difference_type( mpEnd - last ), first );\
747 iterator newEnd = mpEnd - difference_type( last - first );\
748 DestructRange( newEnd, mpEnd );\
753 inline void erase( iterator position )\
755 erase( position, position + 1 );\
760 if ( size() < 2 ) return;\
761 quick_sort( 0, size()-1 );\
767 // redefine below symbol to change the default allocation unit of vector content buffer
768 #define WXSTL_VECTOR_MIN_BUF_SIZE 64
770 // defines vector class, where objects are copied
771 // using "deep-copy" sematics (i.e. by calling their copy constructors)
773 #define WXSTL_VECTOR(ELEMENT_CLASS) \
774 __DEFINE_STL_VECTOR_DEEP(_WXSTL_VECTOR_##ELEMENT_CLASS, ELEMENT_CLASS)
776 // defines vector class, where objects are copied
777 // using "shallow-copy" sematics (i.e. instead of calling
778 // their constructors, memcpy() and memmove() are used to copy their raw data)
781 #define WXSTL_VECTOR_SHALLOW_COPY(ELEMENT_CLASS) __DEFINE_STL_VECTOR_SHALLOW(_WXSTL_VECTORSC_##ELEMENT_CLASS, ELEMENT_CLASS)