]>
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 #include <sys/types.h>
25 // VERSION:: 0.2 (copy-constructor/adign-op added)
27 // FOR NOW:: class-member operators "new" and "delete"
28 // are ignored by list class, memory allocated
29 // and freed using global operators
34 // the below macro used internally (see actual interface after this macro)
36 #define __DEFINE_STL_LIST(listClass,Type) class \
41 typedef Type value_type;\
42 typedef value_type* pointer;\
43 typedef const value_type* const_pointer;\
44 typedef value_type& reference;\
45 typedef const value_type& const_reference;\
46 typedef size_t size_type;\
47 typedef ptrdiff_t difference_type;\
57 typedef list_node* node_ref_type;\
59 node_ref_type mpFreeListHead;\
60 node_ref_type mpTerminator;\
63 inline node_ref_type AllocNode() \
65 if ( mpFreeListHead ) \
67 node_ref_type pFreeNode = mpFreeListHead;\
68 mpFreeListHead = mpFreeListHead->mpPrev;\
74 char* pHeapBlock = new char[sizeof(list_node)];\
76 return (node_ref_type)pHeapBlock;\
80 inline void DestroyFreeList()\
82 while ( mpFreeListHead )\
84 node_ref_type tmp = mpFreeListHead;\
85 mpFreeListHead = mpFreeListHead->mpPrev;\
91 inline void RecycleNode( node_ref_type pNode ) \
93 pNode->mpPrev = mpFreeListHead;\
94 mpFreeListHead = pNode;\
102 node_ref_type mpNode;\
103 friend class listClass;\
104 friend class const_iterator;\
105 friend class const_reverse_iterator;\
108 iterator( node_ref_type pNode )\
115 int operator==( const iterator& rhs ) const { return (mpNode == rhs.mpNode); }\
116 int operator!=( const iterator& rhs ) const { return (mpNode != rhs.mpNode); }\
118 inline iterator( const iterator& other )\
120 mpNode = other.mpNode;\
123 inline const iterator& operator--() \
125 mpNode = mpNode->mpPrev;\
129 inline iterator operator--(int)\
131 iterator tmp = *this;\
132 mpNode = mpNode->mpPrev;\
136 inline const iterator& operator++() \
138 mpNode = mpNode->mpNext;\
142 inline iterator operator++(int)\
144 iterator tmp = *this;\
145 mpNode = mpNode->mpNext;\
149 inline reference operator*() const { return mpNode->mData; }\
153 class const_iterator \
156 node_ref_type mpNode;\
157 friend class listClass;\
160 const_iterator( node_ref_type pNode )\
168 int operator==( const const_iterator& rhs ) const { return (mpNode == rhs.mpNode); }\
169 int operator!=( const const_iterator& rhs ) const { return (mpNode != rhs.mpNode); }\
172 inline const_iterator( const iterator& other )\
174 mpNode = other.mpNode;\
177 inline const const_iterator& operator--() \
179 mpNode = mpNode->mpPrev;\
183 inline const_iterator operator--(int)\
185 const_iterator tmp = *this;\
186 mpNode = mpNode->mpPrev;\
190 inline const const_iterator& operator++() \
192 mpNode = mpNode->mpNext;\
196 inline const_iterator operator++(int)\
198 const_iterator tmp = *this;\
199 mpNode = mpNode->mpNext;\
203 inline const_reference operator*() const { return mpNode->mData; }\
206 typedef iterator OutputIterator;\
207 typedef const_iterator InputIterator;\
209 class reverse_iterator \
212 node_ref_type mpNode;\
213 friend class listClass;\
214 friend class const_reverse_iterator;\
217 reverse_iterator ( node_ref_type pNode )\
224 reverse_iterator() {}\
225 int operator==( const reverse_iterator& rhs ) const { return (mpNode == rhs.mpNode); }\
226 int operator!=( const reverse_iterator& rhs ) const { return (mpNode != rhs.mpNode); }\
228 inline reverse_iterator( const reverse_iterator& other )\
230 mpNode = other.mpNode;\
233 inline const reverse_iterator& operator--() \
235 mpNode = mpNode->mpNext;\
239 inline reverse_iterator operator--(int)\
241 reverse_iterator tmp = *this;\
242 mpNode = mpNode->mpPrev;\
246 inline const reverse_iterator & operator++() \
248 mpNode = mpNode->mpNext;\
252 inline reverse_iterator operator++(int)\
254 reverse_iterator tmp = *this;\
255 mpNode = mpNode->mpPrev;\
259 inline const_reference operator*() const { return mpNode->mData; }\
263 class const_reverse_iterator \
266 node_ref_type mpNode;\
267 friend class listClass;\
270 const_reverse_iterator( node_ref_type pNode )\
277 const_reverse_iterator() {}\
278 int operator==( const const_reverse_iterator& rhs ) const { return (mpNode == rhs.mpNode); }\
279 int operator!=( const const_reverse_iterator& rhs ) const { return (mpNode != rhs.mpNode); }\
281 inline const_reverse_iterator( const reverse_iterator& other )\
283 mpNode = other.mpNode;\
286 inline const const_reverse_iterator& operator--() \
288 mpNode = mpNode->mpNext;\
292 inline const_reverse_iterator operator--(int)\
294 const_reverse_iterator tmp = *this;\
295 mpNode = mpNode->mpNext;\
299 inline const const_reverse_iterator& operator++() \
301 mpNode = mpNode->mpPrev;\
305 inline const_reverse_iterator operator++(int)\
307 const_reverse_iterator tmp = *this;\
308 mpNode = mpNode->mpPrev;\
312 inline const_reference operator*() const { return mpNode->mData; }\
318 : mpFreeListHead( 0 ),\
321 mpTerminator = AllocNode();\
322 mpTerminator->mpPrev = mpTerminator->mpNext = mpTerminator;\
325 listClass( const listClass& other )\
327 mpTerminator = AllocNode();\
328 mpTerminator->mpPrev = mpTerminator->mpNext = mpTerminator;\
330 for( listClass::const_iterator i = other.begin(); i != other.end(); ++i )\
335 inline const listClass& operator=( const listClass& rhs ) \
337 erase( begin(), end() );\
339 for( listClass::const_iterator i = rhs.begin(); i != rhs.end(); ++i )\
346 inline listClass(const_iterator first, const_iterator last)\
347 : mpFreeListHead( 0 ),\
350 { while( first != last ) push_back( *first++ ); }\
352 inline listClass( size_type n, const value_type& value = value_type() )\
354 { for( size_t i = 0; i != n; ++n ) push_back( value ); }\
356 inline ~listClass() \
358 erase( begin(), end() ); \
360 RecycleNode( mpTerminator );\
364 inline iterator begin() { return iterator(mpTerminator->mpNext); }\
366 inline const_iterator begin() const \
367 { return const_iterator(mpTerminator->mpNext); }\
369 inline iterator end() { return iterator(mpTerminator); }\
371 inline const_iterator end() const { return const_iterator(mpTerminator); }\
373 inline reverse_iterator rbegin() \
374 { return reverse_iterator(mpTerminator->mpPrev); }\
376 inline reverse_iterator rend() \
377 { return reverse_iterator(mpTerminator); }\
379 inline const_reverse_iterator rbegin() const\
380 { return const_reverse_iterator(mpTerminator->mpPrev); }\
382 inline const_reverse_iterator rend() const\
383 { return const_reverse_iterator(mpTerminator); }\
385 inline int empty() const { return (mSize == 0); }\
387 inline size_type size() const { return mSize; }\
389 inline size_type max_size() const { return UINT_MAX/sizeof(list_node); }\
391 inline reference front() { return mpTerminator->mData; }\
393 inline const_reference front() const { return mpTerminator->mData; }\
395 inline reference back() { return mpTerminator->mpPrev->mData; }\
397 inline const_reference back() const { return mpTerminator->mpPrev->mData; }\
399 inline void push_front(const value_type& x) { insert( begin(), x ); }\
401 inline void push_back(const value_type& x) { insert( end(), x ); }\
403 iterator insert(iterator position, const value_type& x = value_type())\
405 node_ref_type pNew = AllocNode();\
407 node_ref_type pos = *((node_ref_type*)&position);\
410 pNew->mpPrev = pos->mpPrev;\
411 pos->mpPrev->mpNext = pNew;\
414 new (&pNew->mData) value_type(x);\
418 return iterator(pNew);\
421 inline void insert(iterator position, const_iterator first, const_iterator last )\
423 while( first != last ) insert( position, *first++ );\
426 inline void splice( iterator position, listClass& other )\
428 if ( other.begin() == other.end() ) return;\
430 node_ref_type pTill = other.mpTerminator->mpPrev;\
431 node_ref_type pFrom = other.begin().mpNode;\
433 mpTerminator->mpPrev->mpNext = pFrom;\
434 pFrom->mpPrev = mpTerminator->mpPrev->mpNext;\
436 pTill->mpNext = mpTerminator;\
437 mpTerminator->mpPrev = pTill;\
439 other.mpTerminator->mpNext = \
440 other.mpTerminator->mpPrev = other.mpTerminator;\
442 mSize += other.mSize;\
446 inline void splice( iterator position, listClass& other, iterator first, iterator last )\
448 if ( first == last ) return;\
451 iterator tmp = first;\
452 while( tmp != last ) \
461 node_ref_type pPos = position.mpNode;\
462 node_ref_type pFirst = first.mpNode;\
463 node_ref_type pLast = last.mpNode;\
464 node_ref_type pTill = last.mpNode->mpPrev;\
466 pPos->mpPrev->mpNext = pFirst;\
467 pPos->mpPrev = pTill;\
469 pFirst->mpPrev->mpNext = last.mpNode;\
470 pLast->mpPrev = pTill;\
472 pFirst->mpPrev = pPos->mpPrev;\
473 pTill->mpNext = pPos;\
476 inline void pop_front() { erase( begin() ); }\
477 inline void pop_back() { erase( --end() ); }\
479 inline void erase(iterator position)\
481 erase( position, ++position );\
484 inline void erase(iterator first, iterator last)\
486 node_ref_type firstNode = *((node_ref_type*)&first);\
487 node_ref_type lastNode = *((node_ref_type*)&last);\
489 firstNode->mpPrev->mpNext = lastNode;\
490 lastNode->mpPrev = firstNode->mpPrev;\
492 while( firstNode != lastNode )\
494 node_ref_type next = firstNode->mpNext;\
496 typedef value_type value_type_local;\
497 firstNode->mData.value_type_local::~value_type_local();\
499 RecycleNode( firstNode );\
507 inline void remove(const value_type& value)\
509 for( iterator i = begin(); i != end(); ++i )\
511 if ( (*i) == value ) \
519 if ( mSize < 2 ) return;\
521 iterator from = begin();\
522 iterator other_end = end();\
525 for( size_type i = 0; i != mSize; ++i )\
527 size_type nSwaps = 0;\
529 iterator next = begin();\
532 for( iterator j = begin(); j != other_end; ++j )\
535 if ( (*next) < (*j) )\
537 value_type tmp = (*j);\
547 if ( !nSwaps) break;\
554 // defines list class with the given element type
555 #define WXSTL_LIST(ELEMENT_CLASS) __DEFINE_STL_LIST(\
557 _WXSTL_LIST_##ELEMENT_CLASS, ELEMENT_CLASS )