37 template<
typename T,
typename _Alloc>
40 template<
typename T,
typename _Alloc>
41 class CWConstListIterator;
56 mutable unsigned short count;
57 mutable unsigned short dead;
59 CWNode() : count(0), dead(0) { }
60 explicit CWNode(T
const& __val) : mElement(__val), count(0), dead(0) { }
64 bool operator==(CWNode
const& __node)
const { LIBCWD_ASSERT(!__node.dead);
return mElement == __node.mElement && !dead; }
65 bool operator!=(CWNode
const& __node)
const { LIBCWD_ASSERT(!__node.dead);
return mElement != __node.mElement || dead; }
68 bool operator<(CWNode
const& __node)
const {
return mElement < __node.mElement; }
76 template<
typename T,
typename _Alloc>
77 class CWListIterator {
79 typedef CWListIterator<T, _Alloc> _Self;
80 typedef std::list<CWNode<T>, _Alloc> _Container;
81 typedef typename _Container::iterator _Iterator;
83 _Container* mContainer;
95 if (mContainer && mContainer->end() != mIterator)
97 backtrace(&buffer[index], 32);
103 LIBCWD_ASSERT(!mIterator->dead && mIterator->count < 100);
104 ++(mIterator->count);
112 if (mContainer && mContainer->end() != mIterator)
114 backtrace(&buffer[index], 32);
117 LIBCWD_ASSERT(mIterator->count > 0);
118 if (--(mIterator->count) == 0 && mIterator->dead)
120 mContainer->erase(mIterator);
127 typedef typename _Iterator::difference_type difference_type;
128 typedef typename _Iterator::iterator_category iterator_category;
129 typedef T value_type;
131 typedef T& reference;
134 CWListIterator() : mContainer(NULL), index(0) { std::memset(buffer, 0,
sizeof(buffer)); }
137 CWListIterator(_Container* __c, _Iterator
const& __i) : mContainer(__c), mIterator(__i), index(0)
139 std::memset(buffer, 0,
sizeof(buffer));
140 LIBCWD_ASSERT(mContainer);
145 CWListIterator(CWListIterator
const& __i) : mContainer(__i.mContainer), mIterator(__i.mIterator), index(0)
147 std::memset(buffer, 0,
sizeof(buffer));
158 _Self& operator=(_Self
const& __x)
161 mContainer = __x.mContainer;
162 mIterator = __x.mIterator;
163 LIBCWD_ASSERT(mContainer);
169 reference operator*()
const 172 LIBCWD_ASSERT(mContainer && !mIterator->dead);
173 return mIterator->mElement;
177 pointer operator->()
const 180 LIBCWD_ASSERT(mContainer && !mIterator->dead);
181 return &mIterator->mElement;
187 LIBCWD_ASSERT(mContainer && mIterator != mContainer->end());
188 LIBCWD_ASSERT(mIterator->count > 0);
189 _Iterator cur = mIterator;
192 while(cur != mContainer->end() && cur->dead)
202 _Self operator++(
int)
212 LIBCWD_ASSERT(mContainer && mIterator != mContainer->begin());
213 LIBCWD_ASSERT(mIterator->count > 0);
214 _Iterator cur = mIterator;
227 _Self operator--(
int)
237 bool operator==(_Self
const& __x)
const {
return mIterator == __x.mIterator; }
238 bool operator!=(_Self
const& __x)
const {
return mIterator != __x.mIterator; }
240 friend class CWList<T, _Alloc>;
241 friend class CWConstListIterator<T, _Alloc>;
242 template<
typename T2,
typename A>
friend bool operator==(CWListIterator<T2, A>
const& __x, CWConstListIterator<T2, A>
const& __y);
243 template<
typename T2,
typename A>
friend bool operator!=(CWListIterator<T2, A>
const& __x, CWConstListIterator<T2, A>
const& __y);
250 if (mContainer && mIterator != mContainer->end())
252 return mIterator->count;
262 #ifdef _GLIBCXX_DEBUG 264 memset(&mIterator, 0,
sizeof(mIterator));
276 template<
typename T,
typename _Alloc = std::allocator<T> >
277 class CWConstListIterator {
279 typedef CWConstListIterator<T, _Alloc> _Self;
280 typedef std::list<CWNode<T>, _Alloc> _Container;
281 typedef typename _Container::iterator _Iterator;
282 typedef typename _Container::const_iterator _ConstIterator;
283 typedef CWListIterator<T, _Alloc> iterator;
285 _Container
const* mContainer;
286 _Iterator mConstIterator;
290 if (mContainer && mContainer->end() != mConstIterator)
292 LIBCWD_ASSERT(mConstIterator->count < 100);
293 mConstIterator->count++;
299 if (mContainer && mContainer->end() != mConstIterator)
301 LIBCWD_ASSERT(mConstIterator->count > 0);
302 mConstIterator->count--;
303 if (mConstIterator->count == 0 && mConstIterator->dead)
305 const_cast<_Container*
>(mContainer)->erase(mConstIterator);
311 typedef typename _ConstIterator::difference_type difference_type;
312 typedef typename _ConstIterator::iterator_category iterator_category;
313 typedef T value_type;
314 typedef T
const* pointer;
315 typedef T
const& reference;
317 CWConstListIterator() : mContainer(NULL) { }
318 CWConstListIterator(_Container
const* __c, _Iterator
const& __i) : mContainer(__c), mConstIterator(__i)
320 LIBCWD_ASSERT(mContainer);
324 CWConstListIterator(iterator
const& __x) : mContainer(__x.mContainer), mConstIterator(__x.mIterator)
328 CWConstListIterator(CWConstListIterator
const& __i) : mContainer(__i.mContainer), mConstIterator(__i.mConstIterator)
332 ~CWConstListIterator()
337 _Self& operator=(_Self
const& __x)
340 mContainer = __x.mContainer;
341 mConstIterator = __x.mConstIterator;
342 LIBCWD_ASSERT(mContainer);
348 _Self& operator=(iterator
const& __x)
351 mContainer = __x.mContainer;
352 mConstIterator = __x.mIterator;
353 LIBCWD_ASSERT(mContainer);
358 reference operator*()
const 360 LIBCWD_ASSERT(mContainer && !mConstIterator->dead);
361 return mConstIterator->mElement;
364 pointer operator->()
const 366 LIBCWD_ASSERT(mContainer && !mConstIterator->dead);
367 return &mConstIterator->mElement;
372 _Iterator cur = mConstIterator;
375 while(cur != mContainer->end() && cur->dead)
379 mConstIterator = cur;
384 _Self operator++(
int)
393 _Iterator cur = mConstIterator;
400 mConstIterator = cur;
405 _Self operator--(
int)
412 bool operator==(_Self
const& __x)
const {
return mConstIterator == __x.mConstIterator; }
413 bool operator!=(_Self
const& __x)
const {
return mConstIterator != __x.mConstIterator; }
414 bool operator==(iterator
const& __x)
const {
return mConstIterator == __x.mIterator; }
415 bool operator!=(iterator
const& __x)
const {
return mConstIterator != __x.mIterator; }
417 template<
typename T2,
typename A>
friend bool operator==(CWListIterator<T2, A>
const& __x, CWConstListIterator<T2, A>
const& __y);
418 template<
typename T2,
typename A>
friend bool operator!=(CWListIterator<T2, A>
const& __x, CWConstListIterator<T2, A>
const& __y);
422 if (mContainer && mConstIterator != mContainer->end())
424 return mConstIterator->count;
430 template<
typename T,
typename _Alloc>
431 inline bool operator==(CWListIterator<T, _Alloc>
const& __x, CWConstListIterator<T, _Alloc>
const& __y)
433 return __x.mIterator == __y.mConstIterator;
436 template<
typename T,
typename _Alloc>
437 inline bool operator!=(CWListIterator<T, _Alloc>
const& __x, CWConstListIterator<T, _Alloc>
const& __y)
439 return __x.mIterator != __y.mConstIterator;
458 template<
typename T,
typename _Alloc>
461 typedef std::list<CWNode<T>, _Alloc> _Container;
462 typedef typename _Container::iterator _Iterator;
463 typedef typename _Container::const_iterator _ConstIterator;
465 _Container mContainer;
469 typedef T value_type;
471 typedef T
const* const_pointer;
472 typedef T& reference;
473 typedef T
const& const_reference;
474 typedef CWListIterator<T, _Alloc> iterator;
475 typedef CWConstListIterator<T, _Alloc> const_iterator;
476 typedef std::reverse_iterator<iterator> reverse_iterator;
477 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
478 typedef size_t size_type;
479 typedef ptrdiff_t difference_type;
482 CWList() : mSize(0) { }
483 #ifdef CWDEBUG_DEBUGM 485 ~CWList() { clear(); }
489 explicit CWList(size_type __n, value_type
const& __val = value_type()) : mContainer(__n, CWNode<T>(__val)), mSize(__n) { }
492 CWList(CWList
const& __list) : mSize(0)
494 for (_ConstIterator __i = __list.mContainer.begin(); __i != __list.mContainer.end(); ++__i)
498 mContainer.push_back(CWNode<T>(__i->mElement));
505 template<
typename _InputIterator>
506 CWList(_InputIterator __first, _InputIterator __last) : mSize(0)
508 for (_InputIterator __i = __first; __i != __last; ++__i)
510 mContainer.push_back(CWNode<T>(*__i));
516 CWList& operator=(CWList
const& __list)
519 for (_ConstIterator __i = __list.mContainer.begin(); __i != __list.mContainer.end(); ++__i)
523 mContainer.push_back(CWNode<T>(__i->mElement));
532 _Iterator __i = mContainer.begin();
533 while(__i != mContainer.end() && __i->dead)
537 return iterator(&mContainer, __i);
540 const_iterator begin()
const 542 _Iterator __i =
const_cast<_Container&
>(mContainer).begin();
543 while(__i != mContainer.end() && __i->dead)
547 return const_iterator(&mContainer, __i);
550 iterator end() {
return iterator(&mContainer, mContainer.end()); }
551 const_iterator end()
const {
return const_iterator(&mContainer, const_cast<_Container&>(mContainer).end()); }
552 reverse_iterator rbegin() {
return reverse_iterator(end()); }
553 const_reverse_iterator rbegin()
const {
return const_reverse_iterator(end()); }
554 reverse_iterator rend() {
return reverse_iterator(begin()); }
555 const_reverse_iterator rend()
const {
return const_reverse_iterator(begin()); }
557 bool empty()
const {
return mSize == 0; }
558 size_type size()
const {
return mSize; }
559 size_type max_size()
const {
return mContainer.max_size(); }
561 reference front() { iterator __i = begin();
return *__i; }
562 const_reference front()
const { const_iterator __i = begin();
return *__i; }
563 reference back() { iterator __i = end(); --__i;
return *__i; }
564 const_reference back()
const { const_iterator __i = end(); --__i;
return *__i; }
566 void push_front(value_type
const& __x)
568 mContainer.push_front(CWNode<T>(__x));
573 iterator __i = begin();
576 void push_back(value_type
const& __x)
578 mContainer.push_back(CWNode<T>(__x));
583 iterator __i = end();
588 iterator insert(iterator __position, value_type
const& __x)
590 assert(__position.mContainer == &mContainer && (__position.mIterator == mContainer.end() || !__position.mIterator->dead));
592 return iterator(&mContainer, mContainer.insert(__position.mIterator, CWNode<T>(__x)));
597 #ifdef CWDEBUG_DEBUGM 599 for (_Iterator __i = mContainer.begin(); __i != mContainer.end(); ++__i)
601 LIBCWD_ASSERT(__i->count == 0);
608 void erase(iterator __position)
613 LIBCWD_ASSERT(__position.mContainer == &mContainer && __position.mIterator != mContainer.end() && __position.mIterator->count > 0 && !__position.mIterator->dead);
614 __position.mIterator->dead = 1;
619 void remove(value_type
const& __val)
621 _Iterator
const __e = mContainer.end();
622 for (_Iterator __i = mContainer.begin(); __i != __e;)
624 if (!__i->dead && __i->mElement == __val)
629 mContainer.erase(__i++);
641 #ifdef CWDEBUG_DEBUGM 643 for (_Iterator __i = mContainer.begin(); __i != mContainer.end(); ++__i)
645 LIBCWD_ASSERT(__i->count == 0);
650 template<
typename _StrictWeakOrdering>
653 _StrictWeakOrdering mPred;
654 PredWrapper(_StrictWeakOrdering
const& pred) : mPred(pred) { }
655 bool operator()(CWNode<T>
const& __x, CWNode<T>
const& __y)
const {
return mPred(__x.mElement, __y.mElement); }
657 template<
typename _StrictWeakOrdering>
658 void sort(_StrictWeakOrdering
const& pred)
660 #ifdef CWDEBUG_DEBUGM 662 for (_Iterator __i = mContainer.begin(); __i != mContainer.end(); ++__i)
664 LIBCWD_ASSERT(__i->count == 0);
667 mContainer.sort(PredWrapper<_StrictWeakOrdering>(pred));