]>
git.saurik.com Git - wxWidgets.git/blob - utils/HelpGen/src/wxstllst.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Contrib. demo
4 // Author: Aleksandras Gluchovas
8 // Copyright: (c) Aleskandars Gluchovas
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef __WXSTLLST_G__
13 #define __WXSTLLST_G__
20 #if !defined(__WXMAC__) || defined(__DARWIN__)
21 # include <sys/types.h>
27 // VERSION:: 0.2 (copy-constructor/assign-op added)
29 // FOR NOW:: class-member operators "new" and "delete"
30 // are ignored by list class, memory allocated
31 // and freed using global operators
36 // the below macro used internally (see actual interface after this macro)
38 #define __DEFINE_STL_LIST(listClass,Type) class \
43 typedef Type value_type;\
44 typedef value_type* pointer;\
45 typedef const value_type* const_pointer;\
46 typedef value_type& reference;\
47 typedef const value_type& const_reference;\
48 typedef size_t size_type;\
49 typedef ptrdiff_t difference_type;\
59 typedef list_node* node_ref_type;\
61 node_ref_type mpFreeListHead;\
62 node_ref_type mpTerminator;\
65 inline node_ref_type AllocNode() \
67 if ( mpFreeListHead ) \
69 node_ref_type pFreeNode = mpFreeListHead;\
70 mpFreeListHead = mpFreeListHead->mpPrev;\
76 char* pHeapBlock = new char[sizeof(list_node)];\
78 return (node_ref_type)pHeapBlock;\
82 inline void DestroyFreeList()\
84 while ( mpFreeListHead )\
86 node_ref_type tmp = mpFreeListHead;\
87 mpFreeListHead = mpFreeListHead->mpPrev;\
93 inline void RecycleNode( node_ref_type pNode ) \
95 pNode->mpPrev = mpFreeListHead;\
96 mpFreeListHead = pNode;\
104 node_ref_type mpNode;\
105 friend class listClass;\
106 friend class const_iterator;\
107 friend class const_reverse_iterator;\
110 iterator( node_ref_type pNode )\
117 int operator==( const iterator& rhs ) const { return (mpNode == rhs.mpNode); }\
118 int operator!=( const iterator& rhs ) const { return (mpNode != rhs.mpNode); }\
120 inline iterator( const iterator& other )\
122 mpNode = other.mpNode;\
125 inline const iterator& operator--() \
127 mpNode = mpNode->mpPrev;\
131 inline iterator operator--(int)\
133 iterator tmp = *this;\
134 mpNode = mpNode->mpPrev;\
138 inline const iterator& operator++() \
140 mpNode = mpNode->mpNext;\
144 inline iterator operator++(int)\
146 iterator tmp = *this;\
147 mpNode = mpNode->mpNext;\
151 inline reference operator*() const { return mpNode->mData; }\
155 class const_iterator \
158 node_ref_type mpNode;\
159 friend class listClass;\
162 const_iterator( node_ref_type pNode )\
170 int operator==( const const_iterator& rhs ) const { return (mpNode == rhs.mpNode); }\
171 int operator!=( const const_iterator& rhs ) const { return (mpNode != rhs.mpNode); }\
174 inline const_iterator( const iterator& other )\
176 mpNode = other.mpNode;\
179 inline const const_iterator& operator--() \
181 mpNode = mpNode->mpPrev;\
185 inline const_iterator operator--(int)\
187 const_iterator tmp = *this;\
188 mpNode = mpNode->mpPrev;\
192 inline const const_iterator& operator++() \
194 mpNode = mpNode->mpNext;\
198 inline const_iterator operator++(int)\
200 const_iterator tmp = *this;\
201 mpNode = mpNode->mpNext;\
205 inline const_reference operator*() const { return mpNode->mData; }\
208 typedef iterator OutputIterator;\
209 typedef const_iterator InputIterator;\
211 class reverse_iterator \
214 node_ref_type mpNode;\
215 friend class listClass;\
216 friend class const_reverse_iterator;\
219 reverse_iterator ( node_ref_type pNode )\
226 reverse_iterator() {}\
227 int operator==( const reverse_iterator& rhs ) const { return (mpNode == rhs.mpNode); }\
228 int operator!=( const reverse_iterator& rhs ) const { return (mpNode != rhs.mpNode); }\
230 inline reverse_iterator( const reverse_iterator& other )\
232 mpNode = other.mpNode;\
235 inline const reverse_iterator& operator--() \
237 mpNode = mpNode->mpNext;\
241 inline reverse_iterator operator--(int)\
243 reverse_iterator tmp = *this;\
244 mpNode = mpNode->mpPrev;\
248 inline const reverse_iterator & operator++() \
250 mpNode = mpNode->mpNext;\
254 inline reverse_iterator operator++(int)\
256 reverse_iterator tmp = *this;\
257 mpNode = mpNode->mpPrev;\
261 inline const_reference operator*() const { return mpNode->mData; }\
265 class const_reverse_iterator \
268 node_ref_type mpNode;\
269 friend class listClass;\
272 const_reverse_iterator( node_ref_type pNode )\
279 const_reverse_iterator() {}\
280 int operator==( const const_reverse_iterator& rhs ) const { return (mpNode == rhs.mpNode); }\
281 int operator!=( const const_reverse_iterator& rhs ) const { return (mpNode != rhs.mpNode); }\
283 inline const_reverse_iterator( const reverse_iterator& other )\
285 mpNode = other.mpNode;\
288 inline const const_reverse_iterator& operator--() \
290 mpNode = mpNode->mpNext;\
294 inline const_reverse_iterator operator--(int)\
296 const_reverse_iterator tmp = *this;\
297 mpNode = mpNode->mpNext;\
301 inline const const_reverse_iterator& operator++() \
303 mpNode = mpNode->mpPrev;\
307 inline const_reverse_iterator operator++(int)\
309 const_reverse_iterator tmp = *this;\
310 mpNode = mpNode->mpPrev;\
314 inline const_reference operator*() const { return mpNode->mData; }\
320 : mpFreeListHead( 0 ),\
323 mpTerminator = AllocNode();\
324 mpTerminator->mpPrev = mpTerminator->mpNext = mpTerminator;\
327 listClass( const listClass& other )\
329 mpTerminator = AllocNode();\
330 mpTerminator->mpPrev = mpTerminator->mpNext = mpTerminator;\
332 for( listClass::const_iterator i = other.begin(); i != other.end(); ++i )\
337 inline const listClass& operator=( const listClass& rhs ) \
339 erase( begin(), end() );\
341 for( listClass::const_iterator i = rhs.begin(); i != rhs.end(); ++i )\
348 inline listClass(const_iterator first, const_iterator last)\
349 : mpFreeListHead( 0 ),\
352 { while( first != last ) push_back( *first++ ); }\
354 inline listClass( size_type n, const value_type& value = value_type() )\
356 { for( size_t i = 0; i != n; ++n ) push_back( value ); }\
358 inline ~listClass() \
360 erase( begin(), end() ); \
362 RecycleNode( mpTerminator );\
366 inline iterator begin() { return iterator(mpTerminator->mpNext); }\
368 inline const_iterator begin() const \
369 { return const_iterator(mpTerminator->mpNext); }\
371 inline iterator end() { return iterator(mpTerminator); }\
373 inline const_iterator end() const { return const_iterator(mpTerminator); }\
375 inline reverse_iterator rbegin() \
376 { return reverse_iterator(mpTerminator->mpPrev); }\
378 inline reverse_iterator rend() \
379 { return reverse_iterator(mpTerminator); }\
381 inline const_reverse_iterator rbegin() const\
382 { return const_reverse_iterator(mpTerminator->mpPrev); }\
384 inline const_reverse_iterator rend() const\
385 { return const_reverse_iterator(mpTerminator); }\
387 inline int empty() const { return (m_Size == 0); }\
389 inline size_type size() const { return m_Size; }\
391 inline size_type max_size() const { return UINT_MAX/sizeof(list_node); }\
393 inline reference front() { return mpTerminator->mData; }\
395 inline const_reference front() const { return mpTerminator->mData; }\
397 inline reference back() { return mpTerminator->mpPrev->mData; }\
399 inline const_reference back() const { return mpTerminator->mpPrev->mData; }\
401 inline void push_front(const value_type& x) { insert( begin(), x ); }\
403 inline void push_back(const value_type& x) { insert( end(), x ); }\
405 iterator insert(iterator position, const value_type& x = value_type())\
407 node_ref_type pNew = AllocNode();\
409 node_ref_type pos = *((node_ref_type*)&position);\
412 pNew->mpPrev = pos->mpPrev;\
413 pos->mpPrev->mpNext = pNew;\
416 new (&pNew->mData) value_type(x);\
420 return iterator(pNew);\
423 inline void insert(iterator position, const_iterator first, const_iterator last )\
425 while( first != last ) insert( position, *first++ );\
428 inline void splice( iterator position, listClass& other )\
430 if ( other.begin() == other.end() ) return;\
432 node_ref_type pTill = other.mpTerminator->mpPrev;\
433 node_ref_type pFrom = other.begin().mpNode;\
435 mpTerminator->mpPrev->mpNext = pFrom;\
436 pFrom->mpPrev = mpTerminator->mpPrev->mpNext;\
438 pTill->mpNext = mpTerminator;\
439 mpTerminator->mpPrev = pTill;\
441 other.mpTerminator->mpNext = \
442 other.mpTerminator->mpPrev = other.mpTerminator;\
444 m_Size += other.m_Size;\
448 inline void splice( iterator position, listClass& other, iterator first, iterator last )\
450 if ( first == last ) return;\
453 iterator tmp = first;\
454 while( tmp != last ) \
463 node_ref_type pPos = position.mpNode;\
464 node_ref_type pFirst = first.mpNode;\
465 node_ref_type pLast = last.mpNode;\
466 node_ref_type pTill = last.mpNode->mpPrev;\
468 pPos->mpPrev->mpNext = pFirst;\
469 pPos->mpPrev = pTill;\
471 pFirst->mpPrev->mpNext = last.mpNode;\
472 pLast->mpPrev = pTill;\
474 pFirst->mpPrev = pPos->mpPrev;\
475 pTill->mpNext = pPos;\
478 inline void pop_front() { erase( begin() ); }\
479 inline void pop_back() { erase( --end() ); }\
481 inline void erase(iterator position)\
483 erase( position, ++position );\
486 inline void erase(iterator first, iterator last)\
488 node_ref_type firstNode = *((node_ref_type*)&first);\
489 node_ref_type lastNode = *((node_ref_type*)&last);\
491 firstNode->mpPrev->mpNext = lastNode;\
492 lastNode->mpPrev = firstNode->mpPrev;\
494 while( firstNode != lastNode )\
496 node_ref_type next = firstNode->mpNext;\
498 typedef value_type value_type_local;\
499 firstNode->mData.value_type_local::~value_type_local();\
501 RecycleNode( firstNode );\
509 inline void remove(const value_type& value)\
511 for( iterator i = begin(); i != end(); ++i )\
513 if ( (*i) == value ) \
521 if ( m_Size < 2 ) return;\
523 iterator from = begin();\
524 iterator other_end = end();\
527 for( size_type i = 0; i != m_Size; ++i )\
529 size_type nSwaps = 0;\
531 iterator next = begin();\
534 for( iterator j = begin(); j != other_end; ++j )\
537 if ( (*next) < (*j) )\
539 value_type tmp = (*j);\
549 if ( !nSwaps) break;\
556 // defines list class with the given element type
557 #define WXSTL_LIST(ELEMENT_CLASS) __DEFINE_STL_LIST(\
559 _WXSTL_LIST_##ELEMENT_CLASS, ELEMENT_CLASS )