]>
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__
16 #include <string.h> // imports memmove()
18 #include <sys/types.h>
22 // the below macro used internally (see actual interface after this macro)
24 #define __DEFINE_STL_VECTOR_DEEP( vectorClass, Type ) class vectorClass {\
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;\
37 typedef iterator OutputIterator;\
38 typedef const_iterator InputIterator;\
42 inline void PlacementCopy( const_iterator first, const_iterator last, iterator result )\
44 while ( first != last ) \
45 new (result++) value_type(*first++);\
48 inline void ConstructObjects( iterator first, iterator last, const value_type& pattern )\
50 while( first != last ) \
51 new (first++) value_type(pattern);\
54 inline void CopyObjects( iterator first, iterator last, iterator result )\
56 while( first != last ) \
57 *result++ = *first++;\
60 inline void CopyObjectsBack( iterator first, iterator last, iterator result )\
62 result += difference_type(last,first);\
64 while( first != last ) \
65 *(--result) = *(--last);\
70 class reverse_iterator \
72 friend class vectorClass;\
73 friend class const_reverse_iterator;\
80 reverse_iterator() {}\
82 reverse_iterator ( iterator pPos )\
87 int operator==( const reverse_iterator& rhs ) const { return (mpPos == rhs.mpPos); }\
88 int operator!=( const reverse_iterator& rhs ) const { return (mpPos != rhs.mpPos); }\
90 inline reverse_iterator( const reverse_iterator& other )\
95 inline const reverse_iterator& operator--() \
101 inline reverse_iterator operator--(int)\
103 reverse_iterator tmp = *this;\
108 inline const reverse_iterator & operator++() \
114 inline reverse_iterator operator++(int)\
116 reverse_iterator tmp = *this;\
121 inline const_reference operator*() const { return *mpPos; }\
125 class const_reverse_iterator \
131 const_reverse_iterator() {}\
133 const_reverse_iterator( const iterator pPos )\
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); }\
141 inline const_reverse_iterator( const reverse_iterator& other )\
143 mpPos = other.mpPos;\
146 inline const const_reverse_iterator& operator--() \
152 inline const_reverse_iterator operator--(int)\
154 const_reverse_iterator tmp = *this;\
159 inline const const_reverse_iterator & operator++() \
165 inline const_reverse_iterator operator++(int)\
167 const_reverse_iterator tmp = *this;\
172 inline const_reference operator*() const { return *mpPos; }\
183 inline void quick_sort(int low, int hi) \
187 inline void DestructRange( iterator first, iterator last )\
189 typedef value_type value_type_local;\
191 while ( first != last ) \
193 first->value_type_local::~value_type_local();\
198 inline iterator DoInsert(iterator position, const value_type& x)\
200 if ( mpEnd < mpEndOfBuf )\
202 new (mpEnd) value_type(*(mpEnd-1) );\
204 CopyObjectsBack( position, mpEnd, position + 1 );\
213 size_type minBufLen = WXSTL_VECTOR_MIN_BUF_SIZE/sizeof(value_type);\
215 size_type doubledSize = size()*2;\
217 size_type newLen = ( doubledSize < minBufLen ) ? minBufLen : doubledSize;\
219 iterator pNewStart = (iterator)( new char[newLen*sizeof(value_type)] );\
221 PlacementCopy( mpStart, position, pNewStart );\
223 iterator atPosition = pNewStart + difference_type( position - mpStart );\
225 new (atPosition) value_type(x);\
227 iterator newPos = atPosition;\
233 PlacementCopy( position, mpEnd, atPosition );\
234 DestructRange( mpStart, mpEnd );\
235 delete [](char*)mpStart;\
238 mpEnd = atPosition + difference_type( mpEnd - position );\
240 mpStart = pNewStart;\
241 mpEndOfBuf = pNewStart + newLen;\
248 inline vectorClass() : mpStart(0), \
253 inline vectorClass( const_iterator first, const_iterator last )\
258 { while( first != last ) push_back( *first++ ); }\
260 inline vectorClass( size_type n, const value_type& value = value_type() )\
265 { for( size_type i = 0; i != n; ++i ) push_back( value ); }\
267 inline const vectorClass& operator=( const vectorClass& other )\
271 DestructRange( begin(), end() );\
272 delete [](char*)mpStart; \
275 size_t newLen = difference_type( other.mpEndOfBuf - other.mpStart );\
277 mpStart = (iterator)( new char[newLen*sizeof(value_type)] );\
279 PlacementCopy( other.begin(), other.end(), mpStart );\
281 mpEnd = mpStart + other.size();\
283 mpEndOfBuf = mpStart + newLen;\
288 inline vectorClass( const vectorClass& other )\
293 this->operator=( other );\
296 inline ~vectorClass() \
300 DestructRange( begin(), end() );\
301 delete [](char*)mpStart; \
305 inline iterator begin() { return mpStart; }\
307 inline const_iterator begin() const { return mpStart; }\
309 inline iterator end() { return mpEnd; }\
311 inline const_iterator end() const { return mpEnd; }\
313 inline size_type size() const { return (size_type)difference_type(mpEnd-mpStart); }\
315 inline size_type max_size() const { return UINT_MAX/sizeof(value_type); }\
317 inline size_type capacity() const \
318 { return difference_type(mpEndOfBuf-mpStart)/sizeof(value_type); }\
320 inline int empty() const { return mpStart == mpEnd; }\
322 inline reference operator[](size_type n) { return *(mpStart+n); }\
324 inline const_reference operator[](size_type n) const { return *(mpStart+n); }\
326 inline reference front() { return (*mpStart); }\
328 inline const_reference front() const { return (*mpStart); }\
330 inline reference back() { return (*(mpEnd-1)); }\
332 inline const_reference back() const { return (*(mpEnd-1)); }\
334 inline void reserve(size_type n) {}\
336 inline void push_back(const value_type& x)\
338 if ( mpEnd != mpEndOfBuf ) \
340 new (mpEnd) value_type(x);\
344 DoInsert( mpEnd, x );\
347 inline iterator insert(iterator position, const value_type& x = value_type())\
349 if ( position == mpEnd && mpEnd != mpEndOfBuf )\
351 new (mpEnd) value_type(x);\
355 else return DoInsert( position, x );\
358 inline void pop_back()\
360 DestructRange( mpEnd-1, mpEnd );\
365 inline void erase(iterator first, iterator last)\
367 if ( last == mpEnd )\
369 DestructRange( first, last );\
374 CopyObjects( last, last + difference_type( mpEnd - last ), first );\
376 iterator newEnd = mpEnd - difference_type( last - first );\
377 DestructRange( newEnd, mpEnd );\
382 inline void erase( iterator position )\
384 erase( position, position + 1 );\
389 if ( size() < 2 ) return;\
390 quick_sort( 0, size()-1 );\
394 /////////////////////////////// shallow-copy container ///////////////////////
396 #define __DEFINE_STL_VECTOR_SHALLOW( vectorClass, Type ) class vectorClass {\
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;\
409 typedef iterator OutputIterator;\
410 typedef const_iterator InputIterator;\
414 inline void PlacementCopy( const_iterator first, const_iterator last, iterator result )\
416 memcpy(result, first, int(difference_type(last-first)*sizeof(value_type)) );\
419 inline void ConstructObjects( iterator first, iterator last, const value_type& pattern )\
421 if ( sizeof(pattern) == 1 )\
423 memset( first, int(difference_type(last-first)/sizeof(value_type)), \
424 int(*((char*)&pattern)) );\
426 while( first != last ) \
430 inline void CopyObjects( iterator first, iterator last, iterator result )\
432 memcpy(result, first, int(difference_type(last-first)*sizeof(value_type)) );\
435 inline void CopyObjectsBack( iterator first, iterator last, iterator result )\
437 memmove(result, first, int(difference_type(last-first)*sizeof(value_type)) );\
442 class reverse_iterator \
444 friend class vectorClass;\
445 friend class const_reverse_iterator;\
452 reverse_iterator() {}\
454 reverse_iterator ( iterator pPos )\
459 int operator==( const reverse_iterator& rhs ) const { return (mpPos == rhs.mpPos); }\
460 int operator!=( const reverse_iterator& rhs ) const { return (mpPos != rhs.mpPos); }\
462 inline reverse_iterator( const reverse_iterator& other )\
464 mpPos = other.mpPos;\
467 inline const reverse_iterator& operator--() \
473 inline reverse_iterator operator--(int)\
475 reverse_iterator tmp = *this;\
480 inline const reverse_iterator & operator++() \
486 inline reverse_iterator operator++(int)\
488 reverse_iterator tmp = *this;\
493 inline const_reference operator*() const { return *mpPos; }\
497 class const_reverse_iterator \
503 const_reverse_iterator() {}\
505 const_reverse_iterator( const iterator pPos )\
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); }\
513 inline const_reverse_iterator( const reverse_iterator& other )\
515 mpPos = other.mpPos;\
518 inline const const_reverse_iterator& operator--() \
524 inline const_reverse_iterator operator--(int)\
526 const_reverse_iterator tmp = *this;\
531 inline const const_reverse_iterator & operator++() \
537 inline const_reverse_iterator operator++(int)\
539 const_reverse_iterator tmp = *this;\
544 inline const_reference operator*() const { return *mpPos; }\
555 inline void quick_sort(int low, int hi) \
559 inline void DestructRange( iterator first, iterator last )\
563 inline iterator DoInsert(iterator position, const value_type& x)\
565 if ( mpEnd < mpEndOfBuf )\
567 new (mpEnd) value_type(*(mpEnd-1) );\
569 CopyObjectsBack( position, mpEnd, position + 1 );\
578 size_type minBufLen = WXSTL_VECTOR_MIN_BUF_SIZE/sizeof(value_type);\
580 size_type doubledSize = size()*2;\
582 size_type newLen = ( doubledSize < minBufLen ) ? minBufLen : doubledSize;\
584 iterator pNewStart = (iterator)( new char[newLen*sizeof(value_type)] );\
586 PlacementCopy( mpStart, position, pNewStart );\
588 iterator atPosition = pNewStart + difference_type( position - mpStart );\
590 new (atPosition) value_type(x);\
592 iterator newPos = atPosition;\
598 PlacementCopy( position, mpEnd, atPosition );\
599 DestructRange( mpStart, mpEnd );\
600 delete [](char*)mpStart;\
603 mpEnd = atPosition + difference_type( mpEnd - position );\
605 mpStart = pNewStart;\
606 mpEndOfBuf = pNewStart + newLen;\
613 inline vectorClass() : mpStart(0), \
618 inline vectorClass( const_iterator first, const_iterator last )\
623 { while( first != last ) push_back( *first++ ); }\
625 inline vectorClass( size_type n, const value_type& value = value_type() )\
630 { for( size_type i = 0; i != n; ++i ) push_back( value ); }\
632 inline const vectorClass& operator=( const vectorClass& other )\
636 DestructRange( begin(), end() );\
637 delete [](char*)mpStart; \
640 size_t newLen = difference_type( other.mpEndOfBuf - other.mpStart );\
642 mpStart = (iterator)( new char[newLen*sizeof(value_type)] );\
644 PlacementCopy( other.begin(), other.end(), mpStart );\
646 mpEnd = mpStart + other.size();\
648 mpEndOfBuf = mpStart + newLen;\
653 inline vectorClass( const vectorClass& other )\
658 this->operator=( other );\
661 inline ~vectorClass() \
665 DestructRange( begin(), end() );\
666 delete [](char*)mpStart; \
670 inline iterator begin() { return mpStart; }\
672 inline const_iterator begin() const { return mpStart; }\
674 inline iterator end() { return mpEnd; }\
676 inline const_iterator end() const { return mpEnd; }\
678 inline size_type size() const { return (size_type)difference_type(mpEnd-mpStart); }\
680 inline size_type max_size() const { return UINT_MAX/sizeof(value_type); }\
682 inline size_type capacity() const \
683 { return difference_type(mpEndOfBuf-mpStart)/sizeof(value_type); }\
685 inline int empty() const { return mpStart == mpEnd; }\
687 inline reference operator[](size_type n) { return *(mpStart+n); }\
689 inline const_reference operator[](size_type n) const { return *(mpStart+n); }\
691 inline reference front() { return (*mpStart); }\
693 inline const_reference front() const { return (*mpStart); }\
695 inline reference back() { return (*(mpEnd-1)); }\
697 inline const_reference back() const { return (*(mpEnd-1)); }\
699 inline void reserve(size_type n) {}\
701 inline void push_back(const value_type& x)\
703 if ( mpEnd != mpEndOfBuf ) \
705 new (mpEnd) value_type(x);\
709 DoInsert( mpEnd, x );\
712 inline iterator insert(iterator position, const value_type& x = value_type())\
714 if ( position == mpEnd && mpEnd != mpEndOfBuf )\
716 new (mpEnd) value_type(x);\
720 else return DoInsert( position, x );\
723 inline void pop_back()\
725 DestructRange( mpEnd-1, mpEnd );\
730 inline void erase(iterator first, iterator last)\
732 if ( last == mpEnd )\
734 DestructRange( first, last );\
739 CopyObjects( last, last + difference_type( mpEnd - last ), first );\
741 iterator newEnd = mpEnd - difference_type( last - first );\
742 DestructRange( newEnd, mpEnd );\
747 inline void erase( iterator position )\
749 erase( position, position + 1 );\
754 if ( size() < 2 ) return;\
755 quick_sort( 0, size()-1 );\
761 // redefine below symbol to change the default allocation unit of vector content buffer
762 #define WXSTL_VECTOR_MIN_BUF_SIZE 64
764 // defines vector class, where objects are copied
765 // using "deep-copy" sematics (i.e. by calling their copy constructors)
767 #define WXSTL_VECTOR(ELEMENT_CLASS) \
768 __DEFINE_STL_VECTOR_DEEP(_WXSTL_VECTOR_##ELEMENT_CLASS, ELEMENT_CLASS)
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)
775 #define WXSTL_VECTOR_SHALLOW_COPY(ELEMENT_CLASS) __DEFINE_STL_VECTOR_SHALLOW(_WXSTL_VECTORSC_##ELEMENT_CLASS, ELEMENT_CLASS)