]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/modules/lseditor/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) \
188 pivot_index = ( !(mpStart[low] < mpStart[low+1])) ? low : (low+1);\
189 value_type pivot_value = mpStart[pivot_index];\
191 left = low; right = hi;\
194 while ((left <= hi) && (mpStart[left] < pivot_value)) left++;\
196 while ((right >= low) && (pivot_value < mpStart[right])) right--;\
200 value_type tmp = mpStart[left];\
201 mpStart[left] = mpStart[right];\
202 mpStart[right] = tmp;\
208 } while (left <= right);\
209 if (low < right) quick_sort(low, right);\
210 if (left < hi) quick_sort(left, hi);\
213 inline void DestructRange( iterator first, iterator last )\
215 typedef value_type value_type_local;\
217 while ( first != last ) \
219 first->value_type_local::~value_type_local();\
224 inline iterator DoInsert(iterator position, const value_type& x)\
226 if ( mpEnd < mpEndOfBuf )\
228 new (mpEnd) value_type(*(mpEnd-1) );\
230 CopyObjectsBack( position, mpEnd, position + 1 );\
239 size_type minBufLen = WXSTL_VECTOR_MIN_BUF_SIZE/sizeof(value_type);\
241 size_type doubledSize = size()*2;\
243 size_type newLen = ( doubledSize < minBufLen ) ? minBufLen : doubledSize;\
245 iterator pNewStart = (iterator)( new char[newLen*sizeof(value_type)] );\
247 PlacementCopy( mpStart, position, pNewStart );\
249 iterator atPosition = pNewStart + difference_type( position - mpStart );\
251 new (atPosition) value_type(x);\
253 iterator newPos = atPosition;\
259 PlacementCopy( position, mpEnd, atPosition );\
260 DestructRange( mpStart, mpEnd );\
261 delete [](char*)mpStart;\
264 mpEnd = atPosition + difference_type( mpEnd - position );\
266 mpStart = pNewStart;\
267 mpEndOfBuf = pNewStart + newLen;\
274 inline vectorClass() : mpStart(0), \
279 inline vectorClass( const_iterator first, const_iterator last )\
284 { while( first != last ) push_back( *first++ ); }\
286 inline vectorClass( size_type n, const value_type& value = value_type() )\
291 { for( size_type i = 0; i != n; ++i ) push_back( value ); }\
293 inline int operator==( const vectorClass& other )\
295 size_type sz = size();\
297 if ( sz != other.size() ) return 0;\
299 for( size_type i = 0; i != sz; ++i )\
301 if ( !( (*this)[i] == other[i] ) ) return 0;\
307 inline const vectorClass& operator=( const vectorClass& other )\
311 DestructRange( begin(), end() );\
312 delete [](char*)mpStart; \
315 size_t newLen = difference_type( other.mpEndOfBuf - other.mpStart );\
317 mpStart = (iterator)( new char[newLen*sizeof(value_type)] );\
319 PlacementCopy( other.begin(), other.end(), mpStart );\
321 mpEnd = mpStart + other.size();\
323 mpEndOfBuf = mpStart + newLen;\
328 inline vectorClass( const vectorClass& other )\
333 this->operator=( other );\
336 inline ~vectorClass() \
340 DestructRange( begin(), end() );\
341 delete [](char*)mpStart; \
345 inline iterator begin() { return mpStart; }\
347 inline const_iterator begin() const { return mpStart; }\
349 inline iterator end() { return mpEnd; }\
351 inline const_iterator end() const { return mpEnd; }\
353 inline size_type size() const { return (size_type)difference_type(mpEnd-mpStart); }\
355 inline size_type max_size() const { return UINT_MAX/sizeof(value_type); }\
357 inline size_type capacity() const \
358 { return difference_type(mpEndOfBuf-mpStart)/sizeof(value_type); }\
360 inline int empty() const { return mpStart == mpEnd; }\
362 inline reference operator[](size_type n) { return *(mpStart+n); }\
364 inline const_reference operator[](size_type n) const { return *(mpStart+n); }\
366 inline reference front() { return (*mpStart); }\
368 inline const_reference front() const { return (*mpStart); }\
370 inline reference back() { return (*(mpEnd-1)); }\
372 inline const_reference back() const { return (*(mpEnd-1)); }\
374 inline void reserve(size_type n) {}\
376 inline void push_back(const value_type& x)\
378 if ( mpEnd != mpEndOfBuf ) \
380 new (mpEnd) value_type(x);\
384 DoInsert( mpEnd, x );\
387 inline iterator insert(iterator position, const value_type& x = value_type())\
389 if ( position == mpEnd && mpEnd != mpEndOfBuf )\
391 new (mpEnd) value_type(x);\
395 else return DoInsert( position, x );\
398 inline void pop_back()\
400 DestructRange( mpEnd-1, mpEnd );\
405 inline void erase(iterator first, iterator last)\
407 if ( last == mpEnd )\
409 DestructRange( first, last );\
414 CopyObjects( last, last + difference_type( mpEnd - last ), first );\
416 iterator newEnd = mpEnd - difference_type( last - first );\
417 DestructRange( newEnd, mpEnd );\
422 inline void erase( iterator position )\
424 erase( position, position + 1 );\
429 if ( size() < 2 ) return;\
430 quick_sort( 0, size()-1 );\
434 /////////////////////////////// shallow-copy container ///////////////////////
436 #define __DEFINE_STL_VECTOR_SHALLOW( vectorClass, Type ) class vectorClass {\
439 typedef Type value_type;\
440 typedef value_type* iterator;\
441 typedef const value_type* const_iterator;\
442 typedef iterator pointer;\
443 typedef const iterator const_pointer;\
444 typedef value_type& reference;\
445 typedef const value_type& const_reference;\
446 typedef size_t size_type;\
447 typedef ptrdiff_t difference_type;\
449 typedef iterator OutputIterator;\
450 typedef const_iterator InputIterator;\
454 inline void PlacementCopy( const_iterator first, const_iterator last, iterator result )\
456 memcpy(result, first, int(difference_type(last-first)*sizeof(value_type)) );\
459 inline void ConstructObjects( iterator first, iterator last, const value_type& pattern )\
461 if ( sizeof(pattern) == 1 )\
463 memset( first, int(difference_type(last-first)/sizeof(value_type)), \
464 int(*((char*)&pattern)) );\
466 while( first != last ) \
470 inline void CopyObjects( iterator first, iterator last, iterator result )\
472 memcpy(result, first, int(difference_type(last-first)*sizeof(value_type)) );\
475 inline void CopyObjectsBack( iterator first, iterator last, iterator result )\
477 memmove(result, first, int(difference_type(last-first)*sizeof(value_type)) );\
482 class reverse_iterator \
484 friend class vectorClass;\
485 friend class const_reverse_iterator;\
492 reverse_iterator() {}\
494 reverse_iterator ( iterator pPos )\
499 int operator==( const reverse_iterator& rhs ) const { return (mpPos == rhs.mpPos); }\
500 int operator!=( const reverse_iterator& rhs ) const { return (mpPos != rhs.mpPos); }\
502 inline reverse_iterator( const reverse_iterator& other )\
504 mpPos = other.mpPos;\
507 inline const reverse_iterator& operator--() \
513 inline reverse_iterator operator--(int)\
515 reverse_iterator tmp = *this;\
520 inline const reverse_iterator & operator++() \
526 inline reverse_iterator operator++(int)\
528 reverse_iterator tmp = *this;\
533 inline const_reference operator*() const { return *mpPos; }\
537 class const_reverse_iterator \
543 const_reverse_iterator() {}\
545 const_reverse_iterator( const iterator pPos )\
550 int operator==( const const_reverse_iterator& rhs ) const { return (mpPos == rhs.mpPos); }\
551 int operator!=( const const_reverse_iterator& rhs ) const { return (mpPos != rhs.mpPos); }\
553 inline const_reverse_iterator( const reverse_iterator& other )\
555 mpPos = other.mpPos;\
558 inline const const_reverse_iterator& operator--() \
564 inline const_reverse_iterator operator--(int)\
566 const_reverse_iterator tmp = *this;\
571 inline const const_reverse_iterator & operator++() \
577 inline const_reverse_iterator operator++(int)\
579 const_reverse_iterator tmp = *this;\
584 inline const_reference operator*() const { return *mpPos; }\
595 inline void quick_sort(int low, int hi) \
600 pivot_index = ( !(mpStart[low] < mpStart[low+1])) ? low : (low+1);\
601 value_type pivot_value = mpStart[pivot_index];\
603 left = low; right = hi;\
606 while ((left <= hi) && (mpStart[left] < pivot_value)) left++;\
608 while ((right >= low) && (pivot_value < mpStart[right])) right--;\
612 value_type tmp = mpStart[left];\
613 mpStart[left] = mpStart[right];\
614 mpStart[right] = tmp;\
620 } while (left <= right);\
621 if (low < right) quick_sort(low, right);\
622 if (left < hi) quick_sort(left, hi);\
625 inline void DestructRange( iterator first, iterator last )\
629 inline iterator DoInsert(iterator position, const value_type& x)\
631 if ( mpEnd < mpEndOfBuf )\
633 new (mpEnd) value_type(*(mpEnd-1) );\
635 CopyObjectsBack( position, mpEnd, position + 1 );\
644 size_type minBufLen = WXSTL_VECTOR_MIN_BUF_SIZE/sizeof(value_type);\
646 size_type doubledSize = size()*2;\
648 size_type newLen = ( doubledSize < minBufLen ) ? minBufLen : doubledSize;\
650 iterator pNewStart = (iterator)( new char[newLen*sizeof(value_type)] );\
652 PlacementCopy( mpStart, position, pNewStart );\
654 iterator atPosition = pNewStart + difference_type( position - mpStart );\
656 new (atPosition) value_type(x);\
658 iterator newPos = atPosition;\
664 PlacementCopy( position, mpEnd, atPosition );\
665 DestructRange( mpStart, mpEnd );\
666 delete [](char*)mpStart;\
669 mpEnd = atPosition + difference_type( mpEnd - position );\
671 mpStart = pNewStart;\
672 mpEndOfBuf = pNewStart + newLen;\
679 inline vectorClass() : mpStart(0), \
684 inline vectorClass( const_iterator first, const_iterator last )\
689 { while( first != last ) push_back( *first++ ); }\
691 inline vectorClass( size_type n, const value_type& value = value_type() )\
696 { for( size_type i = 0; i != n; ++i ) push_back( value ); }\
698 inline int operator==( const vectorClass& other )\
700 size_type sz = size();\
702 if ( sz != other.size() ) return 0;\
704 for( size_type i = 0; i != sz; ++i )\
706 if ( !( (*this)[i] == other[i] ) ) return 0;\
712 inline const vectorClass& operator=( const vectorClass& other )\
716 DestructRange( begin(), end() );\
717 delete [](char*)mpStart; \
720 size_t newLen = difference_type( other.mpEndOfBuf - other.mpStart );\
722 mpStart = (iterator)( new char[newLen*sizeof(value_type)] );\
724 PlacementCopy( other.begin(), other.end(), mpStart );\
726 mpEnd = mpStart + other.size();\
728 mpEndOfBuf = mpStart + newLen;\
733 inline vectorClass( const vectorClass& other )\
738 this->operator=( other );\
741 inline ~vectorClass() \
745 DestructRange( begin(), end() );\
746 delete [](char*)mpStart; \
750 inline iterator begin() { return mpStart; }\
752 inline const_iterator begin() const { return mpStart; }\
754 inline iterator end() { return mpEnd; }\
756 inline const_iterator end() const { return mpEnd; }\
758 inline size_type size() const { return (size_type)difference_type(mpEnd-mpStart); }\
760 inline size_type max_size() const { return UINT_MAX/sizeof(value_type); }\
762 inline size_type capacity() const \
763 { return difference_type(mpEndOfBuf-mpStart)/sizeof(value_type); }\
765 inline int empty() const { return mpStart == mpEnd; }\
767 inline reference operator[](size_type n) { return *(mpStart+n); }\
769 inline const_reference operator[](size_type n) const { return *(mpStart+n); }\
771 inline reference front() { return (*mpStart); }\
773 inline const_reference front() const { return (*mpStart); }\
775 inline reference back() { return (*(mpEnd-1)); }\
777 inline const_reference back() const { return (*(mpEnd-1)); }\
779 inline void reserve(size_type n) {}\
781 inline void push_back(const value_type& x)\
783 if ( mpEnd != mpEndOfBuf ) \
785 new (mpEnd) value_type(x);\
789 DoInsert( mpEnd, x );\
792 inline iterator insert(iterator position, const value_type& x = value_type())\
794 if ( position == mpEnd && mpEnd != mpEndOfBuf )\
796 new (mpEnd) value_type(x);\
800 else return DoInsert( position, x );\
803 inline void pop_back()\
805 DestructRange( mpEnd-1, mpEnd );\
810 inline void erase(iterator first, iterator last)\
812 if ( last == mpEnd )\
814 DestructRange( first, last );\
819 CopyObjects( last, last + difference_type( mpEnd - last ), first );\
821 iterator newEnd = mpEnd - difference_type( last - first );\
822 DestructRange( newEnd, mpEnd );\
827 inline void erase( iterator position )\
829 erase( position, position + 1 );\
834 if ( size() < 2 ) return;\
835 quick_sort( 0, size()-1 );\
841 // redefine below symbol to change the default allocation unit of vector content buffer
842 #define WXSTL_VECTOR_MIN_BUF_SIZE 64
844 // defines vector class, where objects are copied
845 // using "deep-copy" sematics (i.e. by calling their copy constructors)
847 #define WXSTL_VECTOR(ELEMENT_CLASS) \
848 __DEFINE_STL_VECTOR_DEEP(_WXSTL_VECTOR_##ELEMENT_CLASS, ELEMENT_CLASS)
850 // defines vector class, where objects are copied
851 // using "shallow-copy" sematics (i.e. instead of calling
852 // their constructors, memcpy() and memmove() are used to copy their raw data)
855 #define WXSTL_VECTOR_SHALLOW_COPY(ELEMENT_CLASS) __DEFINE_STL_VECTOR_SHALLOW(_WXSTL_VECTORSC_##ELEMENT_CLASS, ELEMENT_CLASS)