]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/modules/lseditor/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__
16 #include <sys/types.h>
21 // VERSION:: 0.2 (copy-constructor/adign-op added)
23 // FOR NOW:: class-member operators "new" and "delete"
24 // are ignored by list class, memory allocated
25 // and freed using global operators
30 // the below macro used internally (see actual interface after this macro)
32 #define __DEFINE_STL_LIST(listClass,Type) class \
37 typedef Type value_type;\
38 typedef value_type* pointer;\
39 typedef const value_type* const_pointer;\
40 typedef value_type& reference;\
41 typedef const value_type& const_reference;\
42 typedef size_t size_type;\
43 typedef ptrdiff_t difference_type;\
53 typedef list_node* node_ref_type;\
55 node_ref_type mpFreeListHead;\
56 node_ref_type mpTerminator;\
59 inline node_ref_type AllocNode() \
61 if ( mpFreeListHead ) \
63 node_ref_type pFreeNode = mpFreeListHead;\
64 mpFreeListHead = mpFreeListHead->mpPrev;\
70 char* pHeapBlock = new char[sizeof(list_node)];\
72 return (node_ref_type)pHeapBlock;\
76 inline void DestroyFreeList()\
78 while ( mpFreeListHead )\
80 node_ref_type tmp = mpFreeListHead;\
81 mpFreeListHead = mpFreeListHead->mpPrev;\
87 inline void RecycleNode( node_ref_type pNode ) \
89 pNode->mpPrev = mpFreeListHead;\
90 mpFreeListHead = pNode;\
98 node_ref_type mpNode;\
99 friend class listClass;\
100 friend class const_iterator;\
101 friend class const_reverse_iterator;\
104 iterator( node_ref_type pNode )\
111 int operator==( const iterator& rhs ) const { return (mpNode == rhs.mpNode); }\
112 int operator!=( const iterator& rhs ) const { return (mpNode != rhs.mpNode); }\
114 inline iterator( const iterator& other )\
116 mpNode = other.mpNode;\
119 inline const iterator& operator--() \
121 mpNode = mpNode->mpPrev;\
125 inline iterator operator--(int)\
127 iterator tmp = *this;\
128 mpNode = mpNode->mpPrev;\
132 inline const iterator& operator++() \
134 mpNode = mpNode->mpNext;\
138 inline iterator operator++(int)\
140 iterator tmp = *this;\
141 mpNode = mpNode->mpNext;\
145 inline reference operator*() const { return mpNode->mData; }\
149 class const_iterator \
152 node_ref_type mpNode;\
153 friend class listClass;\
156 const_iterator( node_ref_type pNode )\
164 int operator==( const const_iterator& rhs ) const { return (mpNode == rhs.mpNode); }\
165 int operator!=( const const_iterator& rhs ) const { return (mpNode != rhs.mpNode); }\
168 inline const_iterator( const iterator& other )\
170 mpNode = other.mpNode;\
173 inline const const_iterator& operator--() \
175 mpNode = mpNode->mpPrev;\
179 inline const_iterator operator--(int)\
181 const_iterator tmp = *this;\
182 mpNode = mpNode->mpPrev;\
186 inline const const_iterator& operator++() \
188 mpNode = mpNode->mpNext;\
192 inline const_iterator operator++(int)\
194 const_iterator tmp = *this;\
195 mpNode = mpNode->mpNext;\
199 inline const_reference operator*() const { return mpNode->mData; }\
202 typedef iterator OutputIterator;\
203 typedef const_iterator InputIterator;\
205 class reverse_iterator \
208 node_ref_type mpNode;\
209 friend class listClass;\
210 friend class const_reverse_iterator;\
213 reverse_iterator ( node_ref_type pNode )\
220 reverse_iterator() {}\
221 int operator==( const reverse_iterator& rhs ) const { return (mpNode == rhs.mpNode); }\
222 int operator!=( const reverse_iterator& rhs ) const { return (mpNode != rhs.mpNode); }\
224 inline reverse_iterator( const reverse_iterator& other )\
226 mpNode = other.mpNode;\
229 inline const reverse_iterator& operator--() \
231 mpNode = mpNode->mpNext;\
235 inline reverse_iterator operator--(int)\
237 reverse_iterator tmp = *this;\
238 mpNode = mpNode->mpPrev;\
242 inline const reverse_iterator & operator++() \
244 mpNode = mpNode->mpNext;\
248 inline reverse_iterator operator++(int)\
250 reverse_iterator tmp = *this;\
251 mpNode = mpNode->mpPrev;\
255 inline const_reference operator*() const { return mpNode->mData; }\
259 class const_reverse_iterator \
262 node_ref_type mpNode;\
263 friend class listClass;\
266 const_reverse_iterator( node_ref_type pNode )\
273 const_reverse_iterator() {}\
274 int operator==( const const_reverse_iterator& rhs ) const { return (mpNode == rhs.mpNode); }\
275 int operator!=( const const_reverse_iterator& rhs ) const { return (mpNode != rhs.mpNode); }\
277 inline const_reverse_iterator( const reverse_iterator& other )\
279 mpNode = other.mpNode;\
282 inline const const_reverse_iterator& operator--() \
284 mpNode = mpNode->mpNext;\
288 inline const_reverse_iterator operator--(int)\
290 const_reverse_iterator tmp = *this;\
291 mpNode = mpNode->mpNext;\
295 inline const const_reverse_iterator& operator++() \
297 mpNode = mpNode->mpPrev;\
301 inline const_reverse_iterator operator++(int)\
303 const_reverse_iterator tmp = *this;\
304 mpNode = mpNode->mpPrev;\
308 inline const_reference operator*() const { return mpNode->mData; }\
314 : mpFreeListHead( 0 ),\
317 mpTerminator = AllocNode();\
318 mpTerminator->mpPrev = mpTerminator->mpNext = mpTerminator;\
321 listClass( const listClass& other )\
323 mpTerminator = AllocNode();\
324 mpTerminator->mpPrev = mpTerminator->mpNext = mpTerminator;\
326 for( listClass::const_iterator i = other.begin(); i != other.end(); ++i )\
331 inline const listClass& operator=( const listClass& rhs ) \
333 erase( begin(), end() );\
335 for( listClass::const_iterator i = rhs.begin(); i != rhs.end(); ++i )\
342 inline listClass(const_iterator first, const_iterator last)\
343 : mpFreeListHead( 0 ),\
346 { while( first != last ) push_back( *first++ ); }\
348 inline listClass( size_type n, const value_type& value = value_type() )\
350 { for( size_t i = 0; i != n; ++n ) push_back( value ); }\
352 inline ~listClass() \
354 erase( begin(), end() ); \
356 RecycleNode( mpTerminator );\
360 inline iterator begin() { return iterator(mpTerminator->mpNext); }\
362 inline const_iterator begin() const \
363 { return const_iterator(mpTerminator->mpNext); }\
365 inline iterator end() { return iterator(mpTerminator); }\
367 inline const_iterator end() const { return const_iterator(mpTerminator); }\
369 inline reverse_iterator rbegin() \
370 { return reverse_iterator(mpTerminator->mpPrev); }\
372 inline reverse_iterator rend() \
373 { return reverse_iterator(mpTerminator); }\
375 inline const_reverse_iterator rbegin() const\
376 { return const_reverse_iterator(mpTerminator->mpPrev); }\
378 inline const_reverse_iterator rend() const\
379 { return const_reverse_iterator(mpTerminator); }\
381 inline int empty() const { return (mSize == 0); }\
383 inline size_type size() const { return mSize; }\
385 inline size_type max_size() const { return UINT_MAX/sizeof(list_node); }\
387 inline reference front() { return mpTerminator->mData; }\
389 inline const_reference front() const { return mpTerminator->mData; }\
391 inline reference back() { return mpTerminator->mpPrev->mData; }\
393 inline const_reference back() const { return mpTerminator->mpPrev->mData; }\
395 inline void push_front(const value_type& x) { insert( begin(), x ); }\
397 inline void push_back(const value_type& x) { insert( end(), x ); }\
399 iterator insert(iterator position, const value_type& x = value_type())\
401 node_ref_type pNew = AllocNode();\
403 node_ref_type pos = *((node_ref_type*)&position);\
406 pNew->mpPrev = pos->mpPrev;\
407 pos->mpPrev->mpNext = pNew;\
410 new (&pNew->mData) value_type(x);\
414 return iterator(pNew);\
417 inline void insert(iterator position, const_iterator first, const_iterator last )\
419 while( first != last ) insert( position, *first++ );\
422 inline void splice( iterator position, listClass& other )\
424 if ( other.begin() == other.end() ) return;\
426 node_ref_type pTill = other.mpTerminator->mpPrev;\
427 node_ref_type pFrom = other.begin().mpNode;\
429 mpTerminator->mpPrev->mpNext = pFrom;\
430 pFrom->mpPrev = mpTerminator->mpPrev->mpNext;\
432 pTill->mpNext = mpTerminator;\
433 mpTerminator->mpPrev = pTill;\
435 other.mpTerminator->mpNext = \
436 other.mpTerminator->mpPrev = other.mpTerminator;\
438 mSize += other.mSize;\
442 inline void splice( iterator position, listClass& other, iterator first, iterator last )\
444 if ( first == last ) return;\
447 iterator tmp = first;\
448 while( tmp != last ) \
457 node_ref_type pPos = position.mpNode;\
458 node_ref_type pFirst = first.mpNode;\
459 node_ref_type pLast = last.mpNode;\
460 node_ref_type pTill = last.mpNode->mpPrev;\
462 pPos->mpPrev->mpNext = pFirst;\
463 pPos->mpPrev = pTill;\
465 pFirst->mpPrev->mpNext = last.mpNode;\
466 pLast->mpPrev = pTill;\
468 pFirst->mpPrev = pPos->mpPrev;\
469 pTill->mpNext = pPos;\
472 inline void pop_front() { erase( begin() ); }\
473 inline void pop_back() { erase( --end() ); }\
475 inline void erase(iterator position)\
477 erase( position, ++position );\
480 inline void erase(iterator first, iterator last)\
482 node_ref_type firstNode = *((node_ref_type*)&first);\
483 node_ref_type lastNode = *((node_ref_type*)&last);\
485 firstNode->mpPrev->mpNext = lastNode;\
486 lastNode->mpPrev = firstNode->mpPrev;\
488 while( firstNode != lastNode )\
490 node_ref_type next = firstNode->mpNext;\
492 typedef value_type value_type_local;\
493 firstNode->mData.value_type_local::~value_type_local();\
495 RecycleNode( firstNode );\
503 inline void remove(const value_type& value)\
505 for( iterator i = begin(); i != end(); ++i )\
507 if ( (*i) == value ) \
515 if ( mSize < 2 ) return;\
517 iterator from = begin();\
518 iterator other_end = end();\
521 for( size_type i = 0; i != mSize; ++i )\
523 size_type nSwaps = 0;\
525 iterator next = begin();\
528 for( iterator j = begin(); j != other_end; ++j )\
531 if ( (*next) < (*j) )\
533 value_type tmp = (*j);\
543 if ( !nSwaps) break;\
550 // defines list class with the given element type
551 #define WXSTL_LIST(ELEMENT_CLASS) __DEFINE_STL_LIST(\
553 _WXSTL_LIST_##ELEMENT_CLASS, ELEMENT_CLASS )