]>
git.saurik.com Git - wxWidgets.git/blob - utils/HelpGen/include/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 #include <sys/types.h>
26 // the below macro used internally (see actual interface after this macro)
28 #define __DEFINE_STL_VECTOR_DEEP( vectorClass, Type ) class vectorClass {\
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;\
41 typedef iterator OutputIterator;\
42 typedef const_iterator InputIterator;\
46 inline void PlacementCopy( const_iterator first, const_iterator last, iterator result )\
48 while ( first != last ) \
49 new (result++) value_type(*first++);\
52 inline void ConstructObjects( iterator first, iterator last, const value_type& pattern )\
54 while( first != last ) \
55 new (first++) value_type(pattern);\
58 inline void CopyObjects( iterator first, iterator last, iterator result )\
60 while( first != last ) \
61 *result++ = *first++;\
64 inline void CopyObjectsBack( iterator first, iterator last, iterator result )\
66 result += difference_type(last,first);\
68 while( first != last ) \
69 *(--result) = *(--last);\
74 class reverse_iterator \
76 friend class vectorClass;\
77 friend class const_reverse_iterator;\
84 reverse_iterator() {}\
86 reverse_iterator ( iterator pPos )\
91 int operator==( const reverse_iterator& rhs ) const { return (mpPos == rhs.mpPos); }\
92 int operator!=( const reverse_iterator& rhs ) const { return (mpPos != rhs.mpPos); }\
94 inline reverse_iterator( const reverse_iterator& other )\
99 inline const reverse_iterator& operator--() \
105 inline reverse_iterator operator--(int)\
107 reverse_iterator tmp = *this;\
112 inline const reverse_iterator & operator++() \
118 inline reverse_iterator operator++(int)\
120 reverse_iterator tmp = *this;\
125 inline const_reference operator*() const { return *mpPos; }\
129 class const_reverse_iterator \
135 const_reverse_iterator() {}\
137 const_reverse_iterator( const iterator pPos )\
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); }\
145 inline const_reverse_iterator( const reverse_iterator& other )\
147 mpPos = other.mpPos;\
150 inline const const_reverse_iterator& operator--() \
156 inline const_reverse_iterator operator--(int)\
158 const_reverse_iterator tmp = *this;\
163 inline const const_reverse_iterator & operator++() \
169 inline const_reverse_iterator operator++(int)\
171 const_reverse_iterator tmp = *this;\
176 inline const_reference operator*() const { return *mpPos; }\
187 inline void quick_sort(int low, int hi) \
191 inline void DestructRange( iterator first, iterator last )\
193 typedef value_type value_type_local;\
195 while ( first != last ) \
197 first->value_type_local::~value_type_local();\
202 inline iterator DoInsert(iterator position, const value_type& x)\
204 if ( mpEnd < mpEndOfBuf )\
206 new (mpEnd) value_type(*(mpEnd-1) );\
208 CopyObjectsBack( position, mpEnd, position + 1 );\
217 size_type minBufLen = WXSTL_VECTOR_MIN_BUF_SIZE/sizeof(value_type);\
219 size_type doubledSize = size()*2;\
221 size_type newLen = ( doubledSize < minBufLen ) ? minBufLen : doubledSize;\
223 iterator pNewStart = (iterator)( new char[newLen*sizeof(value_type)] );\
225 PlacementCopy( mpStart, position, pNewStart );\
227 iterator atPosition = pNewStart + difference_type( position - mpStart );\
229 new (atPosition) value_type(x);\
231 iterator newPos = atPosition;\
237 PlacementCopy( position, mpEnd, atPosition );\
238 DestructRange( mpStart, mpEnd );\
239 delete [](char*)mpStart;\
242 mpEnd = atPosition + difference_type( mpEnd - position );\
244 mpStart = pNewStart;\
245 mpEndOfBuf = pNewStart + newLen;\
252 inline vectorClass() : mpStart(0), \
257 inline vectorClass( const_iterator first, const_iterator last )\
262 { while( first != last ) push_back( *first++ ); }\
264 inline vectorClass( size_type n, const value_type& value = value_type() )\
269 { for( size_type i = 0; i != n; ++i ) push_back( value ); }\
271 inline const vectorClass& operator=( const vectorClass& other )\
275 DestructRange( begin(), end() );\
276 delete [](char*)mpStart; \
279 size_t newLen = difference_type( other.mpEndOfBuf - other.mpStart );\
281 mpStart = (iterator)( new char[newLen*sizeof(value_type)] );\
283 PlacementCopy( other.begin(), other.end(), mpStart );\
285 mpEnd = mpStart + other.size();\
287 mpEndOfBuf = mpStart + newLen;\
292 inline vectorClass( const vectorClass& other )\
297 this->operator=( other );\
300 inline ~vectorClass() \
304 DestructRange( begin(), end() );\
305 delete [](char*)mpStart; \
309 inline iterator begin() { return mpStart; }\
311 inline const_iterator begin() const { return mpStart; }\
313 inline iterator end() { return mpEnd; }\
315 inline const_iterator end() const { return mpEnd; }\
317 inline size_type size() const { return (size_type)difference_type(mpEnd-mpStart); }\
319 inline size_type max_size() const { return UINT_MAX/sizeof(value_type); }\
321 inline size_type capacity() const \
322 { return difference_type(mpEndOfBuf-mpStart)/sizeof(value_type); }\
324 inline int empty() const { return mpStart == mpEnd; }\
326 inline reference operator[](size_type n) { return *(mpStart+n); }\
328 inline const_reference operator[](size_type n) const { return *(mpStart+n); }\
330 inline reference front() { return (*mpStart); }\
332 inline const_reference front() const { return (*mpStart); }\
334 inline reference back() { return (*(mpEnd-1)); }\
336 inline const_reference back() const { return (*(mpEnd-1)); }\
338 inline void reserve(size_type n) {}\
340 inline void push_back(const value_type& x)\
342 if ( mpEnd != mpEndOfBuf ) \
344 new (mpEnd) value_type(x);\
348 DoInsert( mpEnd, x );\
351 inline iterator insert(iterator position, const value_type& x = value_type())\
353 if ( position == mpEnd && mpEnd != mpEndOfBuf )\
355 new (mpEnd) value_type(x);\
359 else return DoInsert( position, x );\
362 inline void pop_back()\
364 DestructRange( mpEnd-1, mpEnd );\
369 inline void erase(iterator first, iterator last)\
371 if ( last == mpEnd )\
373 DestructRange( first, last );\
378 CopyObjects( last, last + difference_type( mpEnd - last ), first );\
380 iterator newEnd = mpEnd - difference_type( last - first );\
381 DestructRange( newEnd, mpEnd );\
386 inline void erase( iterator position )\
388 erase( position, position + 1 );\
393 if ( size() < 2 ) return;\
394 quick_sort( 0, size()-1 );\
398 /////////////////////////////// shallow-copy container ///////////////////////
400 #define __DEFINE_STL_VECTOR_SHALLOW( vectorClass, Type ) class vectorClass {\
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;\
413 typedef iterator OutputIterator;\
414 typedef const_iterator InputIterator;\
418 inline void PlacementCopy( const_iterator first, const_iterator last, iterator result )\
420 memcpy(result, first, int(difference_type(last-first)*sizeof(value_type)) );\
423 inline void ConstructObjects( iterator first, iterator last, const value_type& pattern )\
425 if ( sizeof(pattern) == 1 )\
427 memset( first, int(difference_type(last-first)/sizeof(value_type)), \
428 int(*((char*)&pattern)) );\
430 while( first != last ) \
434 inline void CopyObjects( iterator first, iterator last, iterator result )\
436 memcpy(result, first, int(difference_type(last-first)*sizeof(value_type)) );\
439 inline void CopyObjectsBack( iterator first, iterator last, iterator result )\
441 memmove(result, first, int(difference_type(last-first)*sizeof(value_type)) );\
446 class reverse_iterator \
448 friend class vectorClass;\
449 friend class const_reverse_iterator;\
456 reverse_iterator() {}\
458 reverse_iterator ( iterator pPos )\
463 int operator==( const reverse_iterator& rhs ) const { return (mpPos == rhs.mpPos); }\
464 int operator!=( const reverse_iterator& rhs ) const { return (mpPos != rhs.mpPos); }\
466 inline reverse_iterator( const reverse_iterator& other )\
468 mpPos = other.mpPos;\
471 inline const reverse_iterator& operator--() \
477 inline reverse_iterator operator--(int)\
479 reverse_iterator tmp = *this;\
484 inline const reverse_iterator & operator++() \
490 inline reverse_iterator operator++(int)\
492 reverse_iterator tmp = *this;\
497 inline const_reference operator*() const { return *mpPos; }\
501 class const_reverse_iterator \
507 const_reverse_iterator() {}\
509 const_reverse_iterator( const iterator pPos )\
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); }\
517 inline const_reverse_iterator( const reverse_iterator& other )\
519 mpPos = other.mpPos;\
522 inline const const_reverse_iterator& operator--() \
528 inline const_reverse_iterator operator--(int)\
530 const_reverse_iterator tmp = *this;\
535 inline const const_reverse_iterator & operator++() \
541 inline const_reverse_iterator operator++(int)\
543 const_reverse_iterator tmp = *this;\
548 inline const_reference operator*() const { return *mpPos; }\
559 inline void quick_sort(int low, int hi) \
563 inline void DestructRange( iterator first, iterator last )\
567 inline iterator DoInsert(iterator position, const value_type& x)\
569 if ( mpEnd < mpEndOfBuf )\
571 new (mpEnd) value_type(*(mpEnd-1) );\
573 CopyObjectsBack( position, mpEnd, position + 1 );\
582 size_type minBufLen = WXSTL_VECTOR_MIN_BUF_SIZE/sizeof(value_type);\
584 size_type doubledSize = size()*2;\
586 size_type newLen = ( doubledSize < minBufLen ) ? minBufLen : doubledSize;\
588 iterator pNewStart = (iterator)( new char[newLen*sizeof(value_type)] );\
590 PlacementCopy( mpStart, position, pNewStart );\
592 iterator atPosition = pNewStart + difference_type( position - mpStart );\
594 new (atPosition) value_type(x);\
596 iterator newPos = atPosition;\
602 PlacementCopy( position, mpEnd, atPosition );\
603 DestructRange( mpStart, mpEnd );\
604 delete [](char*)mpStart;\
607 mpEnd = atPosition + difference_type( mpEnd - position );\
609 mpStart = pNewStart;\
610 mpEndOfBuf = pNewStart + newLen;\
617 inline vectorClass() : mpStart(0), \
622 inline vectorClass( const_iterator first, const_iterator last )\
627 { while( first != last ) push_back( *first++ ); }\
629 inline vectorClass( size_type n, const value_type& value = value_type() )\
634 { for( size_type i = 0; i != n; ++i ) push_back( value ); }\
636 inline const vectorClass& operator=( const vectorClass& other )\
640 DestructRange( begin(), end() );\
641 delete [](char*)mpStart; \
644 size_t newLen = difference_type( other.mpEndOfBuf - other.mpStart );\
646 mpStart = (iterator)( new char[newLen*sizeof(value_type)] );\
648 PlacementCopy( other.begin(), other.end(), mpStart );\
650 mpEnd = mpStart + other.size();\
652 mpEndOfBuf = mpStart + newLen;\
657 inline vectorClass( const vectorClass& other )\
662 this->operator=( other );\
665 inline ~vectorClass() \
669 DestructRange( begin(), end() );\
670 delete [](char*)mpStart; \
674 inline iterator begin() { return mpStart; }\
676 inline const_iterator begin() const { return mpStart; }\
678 inline iterator end() { return mpEnd; }\
680 inline const_iterator end() const { return mpEnd; }\
682 inline size_type size() const { return (size_type)difference_type(mpEnd-mpStart); }\
684 inline size_type max_size() const { return UINT_MAX/sizeof(value_type); }\
686 inline size_type capacity() const \
687 { return difference_type(mpEndOfBuf-mpStart)/sizeof(value_type); }\
689 inline int empty() const { return mpStart == mpEnd; }\
691 inline reference operator[](size_type n) { return *(mpStart+n); }\
693 inline const_reference operator[](size_type n) const { return *(mpStart+n); }\
695 inline reference front() { return (*mpStart); }\
697 inline const_reference front() const { return (*mpStart); }\
699 inline reference back() { return (*(mpEnd-1)); }\
701 inline const_reference back() const { return (*(mpEnd-1)); }\
703 inline void reserve(size_type n) {}\
705 inline void push_back(const value_type& x)\
707 if ( mpEnd != mpEndOfBuf ) \
709 new (mpEnd) value_type(x);\
713 DoInsert( mpEnd, x );\
716 inline iterator insert(iterator position, const value_type& x = value_type())\
718 if ( position == mpEnd && mpEnd != mpEndOfBuf )\
720 new (mpEnd) value_type(x);\
724 else return DoInsert( position, x );\
727 inline void pop_back()\
729 DestructRange( mpEnd-1, mpEnd );\
734 inline void erase(iterator first, iterator last)\
736 if ( last == mpEnd )\
738 DestructRange( first, last );\
743 CopyObjects( last, last + difference_type( mpEnd - last ), first );\
745 iterator newEnd = mpEnd - difference_type( last - first );\
746 DestructRange( newEnd, mpEnd );\
751 inline void erase( iterator position )\
753 erase( position, position + 1 );\
758 if ( size() < 2 ) return;\
759 quick_sort( 0, size()-1 );\
765 // redefine below symbol to change the default allocation unit of vector content buffer
766 #define WXSTL_VECTOR_MIN_BUF_SIZE 64
768 // defines vector class, where objects are copied
769 // using "deep-copy" sematics (i.e. by calling their copy constructors)
771 #define WXSTL_VECTOR(ELEMENT_CLASS) \
772 __DEFINE_STL_VECTOR_DEEP(_WXSTL_VECTOR_##ELEMENT_CLASS, ELEMENT_CLASS)
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)
779 #define WXSTL_VECTOR_SHALLOW_COPY(ELEMENT_CLASS) __DEFINE_STL_VECTOR_SHALLOW(_WXSTL_VECTORSC_##ELEMENT_CLASS, ELEMENT_CLASS)