00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef _terimber_list_hpp_
00029 #define _terimber_list_hpp_
00030
00031 #include "base/list.h"
00032
00033 BEGIN_TERIMBER_NAMESPACE
00034 #pragma pack(4)
00035
00036
00037 template < class T >
00038 inline
00039 base_list< T >::base_list() :
00040 _head()
00041 {
00042 clear();
00043 }
00044
00045
00046 template < class T >
00047 inline
00048 base_list< T >::~base_list()
00049 {
00050 }
00051
00052
00053 template < class T >
00054 inline
00055 bool
00056 base_list< T >::empty() const
00057 {
00058 return _head._prev == head();
00059 }
00060
00061
00062 template < class T >
00063 inline
00064 const T&
00065 base_list< T >::front() const
00066 {
00067 return _head._next->_value;
00068 }
00069
00070
00071 template < class T >
00072 inline
00073 T&
00074 base_list< T >::front()
00075 {
00076 return _head._next->_value;
00077 }
00078
00079
00080 template < class T >
00081 inline
00082 const T&
00083 base_list< T >::back() const
00084 {
00085 return _head._prev->_value;
00086 }
00087
00088
00089 template < class T >
00090 inline
00091 T&
00092 base_list< T >::back()
00093 {
00094 return _head._prev->_value;
00095 }
00096
00097 template < class T >
00098 inline
00099 TYPENAME base_list< T >::const_iterator
00100 base_list< T >::begin() const
00101 {
00102 return const_iterator(_head._next);
00103 }
00104
00105 template < class T >
00106 inline
00107 TYPENAME base_list< T >::iterator
00108 base_list< T >::begin()
00109 {
00110 return iterator(_head._next);
00111 }
00112
00113 template < class T >
00114 inline
00115 TYPENAME base_list< T >::const_iterator
00116 base_list< T >::end() const
00117 {
00118 return const_iterator(head());
00119 }
00120
00121 template < class T >
00122 inline
00123 TYPENAME base_list< T >::iterator
00124 base_list< T >::end()
00125 {
00126 return iterator(head());
00127 }
00128
00129 template < class T >
00130 inline
00131 void
00132 base_list< T >::clear()
00133 {
00134 _head._prev = _head._next = head();
00135 }
00136
00137 template < class T >
00138 inline
00139 TYPENAME base_list< T >::_node*
00140 base_list< T >::head()
00141 {
00142 return static_cast< _node* >(&_head);
00143 }
00144
00145 template < class T >
00146 inline
00147 TYPENAME base_list< T >::_node*
00148 base_list< T >::head() const
00149 {
00150 return const_cast< TYPENAME base_list< T >::_node* >(static_cast< const TYPENAME base_list< T >::_node* >(&_head));
00151 }
00152
00154
00155
00156
00157
00158
00159 template < class T, class A >
00160 inline
00161 _list< T, A >::_list() :
00162 base_list< T >()
00163 {
00164 }
00165
00166 template < class T, class A >
00167 inline
00168 _list< T, A >::~_list()
00169 {
00170 this->clear();
00171 }
00172
00173 template < class T, class A >
00174 inline
00175 _list< T, A >::_list(const _list< T, A >& x) :
00176 base_list< T >()
00177 {
00178 *this = x;
00179 }
00180
00181
00182
00183 template < class T, class A >
00184 inline
00185 _list< T, A >&
00186 _list< T, A >::operator=(const _list< T, A >& x)
00187 {
00188 if (this != &x)
00189 {
00190 base_list< T >::clear();
00191 if (!x.empty())
00192 {
00193 this->_head._next = x._head._next;
00194 this->_head._prev = x._head._prev;
00195
00196 this->_head._prev->_next = this->_head._next->_prev = this->head();
00197 }
00198 }
00199 return *this;
00200 }
00201
00202 template < class T, class A >
00203 inline
00204 void
00205 _list< T, A >::assign(A& allocator_, const _list< T, A >& x)
00206 {
00207 assign(allocator_, x.begin(), x.end());
00208 }
00209
00210 template < class T, class A >
00211 inline
00212 void
00213 _list< T, A >::assign(A& allocator_, TYPENAME _list< T, A >::const_iterator first, TYPENAME _list< T, A >::const_iterator last)
00214 {
00215 this->clear();
00216 for (;first != last; ++first)
00217 push_back(allocator_, *first);
00218 }
00219
00220 template < class T, class A >
00221 inline
00222 void
00223 _list< T, A >::assign(A& allocator_, size_t n, const T& x)
00224 {
00225 this->clear();
00226 for (size_t I = 0; I < n; ++I)
00227 push_back(allocator_, x);
00228 }
00229
00230
00231 template < class T, class A >
00232 inline
00233 size_t
00234 _list< T, A >::size() const
00235 {
00236 size_t count = 0;
00237 for (TYPENAME _list< T, A >::const_iterator I = this->begin(); I != this->end(); ++I, ++count);
00238 return count;
00239 }
00240
00241 template < class T, class A >
00242 inline
00243 TYPENAME _list< T, A >::iterator
00244 _list< T, A >::push_back(A& allocator_, const T& x)
00245 {
00246
00247 TYPENAME _list< T, A >::_node* n = _buynode(allocator_, x);
00248 if (!n)
00249 return this->end();
00250
00251 n->_prev = this->_head._prev;
00252 n->_next = this->head();
00253 this->_head._prev->_next = n;
00254 this->_head._prev = n;
00255 return TYPENAME _list< T, A >::iterator(n);
00256 }
00257
00258 template < class T, class A >
00259 inline
00260 TYPENAME _list< T, A >::iterator
00261 _list< T, A >::push_front(A& allocator_, const T& x)
00262 {
00263
00264 TYPENAME _list< T, A >::_node* n = _buynode(allocator_, x);
00265 if (!n)
00266 return this->end();
00267 n->_prev = this->head();
00268 n->_next = this->_head._next;
00269 this->_head._next->_prev = n;
00270 this->_head._next = n;
00271 return TYPENAME _list< T, A >::iterator(n);
00272 }
00273
00274
00275
00276 template < class T, class A >
00277 inline
00278 bool
00279 _list< T, A >::pop_front()
00280 {
00281 if (this->empty())
00282 return false;
00283 this->_head._next->_next->_prev = this->head();
00284 this->_head._next = this->_head._next->_next;
00285 return true;
00286 }
00287
00288
00289 template < class T, class A >
00290 inline
00291 bool
00292 _list< T, A >::pop_front(node_allocator< TYPENAME base_list< T >::_node >& allocator_)
00293 {
00294 if (this->empty())
00295 return false;
00296
00297 TYPENAME _list< T, A >::_node* f = this->_head._next;
00298 this->_head._next->_next->_prev = this->head();
00299 this->_head._next = this->_head._next->_next;
00300 _freenode(allocator_, f);
00301 return true;
00302 }
00303
00304
00305
00306 template < class T, class A >
00307 inline
00308 bool
00309 _list< T, A >::pop_back()
00310 {
00311 if (this->empty())
00312 return false;
00313 this->_head._prev->_prev->_next = this->head();
00314 this->_head._prev = this->_head._prev->_prev;
00315 return true;
00316 }
00317
00318 template < class T, class A >
00319 inline
00320 bool
00321 _list< T, A >::pop_back(node_allocator< TYPENAME base_list< T >::_node >& allocator_)
00322 {
00323 if (this->empty())
00324 return false;
00325
00326 TYPENAME _list< T, A >::_node* f = this->_head._prev;
00327 this->_head._prev->_prev->_next = this->head();
00328 this->_head._prev = this->_head._prev->_prev;
00329 _freenode(allocator_, f);
00330
00331 return true;
00332 }
00333
00334 template < class T, class A >
00335 inline
00336 TYPENAME _list< T, A >::iterator
00337 _list< T, A >::insert(A& allocator_, TYPENAME _list< T, A >::iterator it, const T& x)
00338 {
00339 TYPENAME _list< T, A >::_node* n = _buynode(allocator_, x);
00340 if (!n)
00341 return this->end();
00342 TYPENAME _list< T, A >::_node* i = it.node();
00343
00344 n->_next = i;
00345 n->_prev = i->_prev;
00346 i->_prev = n;
00347 i = i->_prev;
00348 i->_prev->_next = i;
00349
00350 return TYPENAME _list< T, A >::iterator(n);
00351 }
00352
00353 template < class T, class A >
00354 inline
00355 void
00356 _list< T, A >::insert(A& allocator_, TYPENAME _list< T, A >::iterator it, size_t n, const T& x)
00357 {
00358 for (size_t I = 0; I < n; ++I)
00359 it = insert(allocator_, it, x);
00360 }
00361
00362 template < class T, class A >
00363 inline
00364 void
00365 _list< T, A >::insert(A& allocator_, TYPENAME _list< T, A >::iterator it, TYPENAME _list< T, A >::const_iterator first, TYPENAME _list< T, A >::const_iterator last)
00366 {
00367 for (first; first != last; ++first)
00368 it = insert(allocator_, it, *first);
00369 }
00370
00371
00372
00373 template < class T, class A >
00374 inline
00375 void
00376 _list< T, A >::remove(const T& x)
00377 {
00378 for (TYPENAME _list< T, A >::iterator I = this->begin(); I != this->end();)
00379 if (*I == x)
00380 I = remove(I);
00381 else
00382 ++I;
00383 }
00384
00385 template < class T, class A >
00386 inline
00387 void
00388 _list< T, A >::remove(node_allocator< TYPENAME base_list< T >::_node >& allocator_, const T& x)
00389 {
00390 for (TYPENAME _list< T, A >::iterator I = this->begin(); I != this->end();)
00391 if (*I == x)
00392 I = remove(allocator_, I);
00393 else
00394 ++I;
00395 }
00396
00397
00398
00399 template < class T, class A >
00400 inline
00401 TYPENAME _list< T, A >::iterator
00402 _list< T, A >::erase(TYPENAME _list< T, A >::iterator iter)
00403 {
00404 return remove(iter);
00405 }
00406
00407 template < class T, class A >
00408 inline
00409 TYPENAME _list< T, A >::iterator
00410 _list< T, A >::erase(node_allocator< TYPENAME base_list< T >::_node >& allocator_, TYPENAME _list< T, A >::iterator iter)
00411 {
00412 return remove(allocator_, iter);
00413 }
00414
00415 template < class T, class A >
00416 inline
00417 TYPENAME _list< T, A >::iterator
00418 _list< T, A >::erase(TYPENAME _list< T, A >::iterator first, TYPENAME _list< T, A >::iterator last)
00419 {
00420 for (;first != last;)
00421 first = remove(first);
00422 return first;
00423 }
00424
00425 template < class T, class A >
00426 inline
00427 TYPENAME _list< T, A >::iterator
00428 _list< T, A >::erase(node_allocator< TYPENAME base_list< T >::_node >& allocator_, TYPENAME _list< T, A >::iterator first, TYPENAME _list< T, A >::iterator last)
00429 {
00430 for (;first != last;)
00431 first = remove(allocator_, first);
00432 return first;
00433 }
00434
00435 template < class T, class A >
00436 inline
00437 TYPENAME _list< T, A >::iterator
00438 _list< T, A >::remove(TYPENAME _list< T, A >::iterator iter)
00439 {
00440 TYPENAME _list< T, A >::iterator ret = iter;
00441 ++ret;
00442 iter.prev()->_next = iter.next();
00443 iter.next()->_prev = iter.prev();
00444 return ret;
00445 }
00446
00447 template < class T, class A >
00448 inline
00449 TYPENAME _list< T, A >::iterator
00450 _list< T, A >::remove(node_allocator< TYPENAME base_list< T >::_node >& allocator_, TYPENAME _list< T, A >::iterator iter)
00451 {
00452 TYPENAME _list< T >::iterator ret = iter;
00453 ++ret;
00454 iter.prev()->_next = iter.next();
00455 iter.next()->_prev = iter.prev();
00456 _freenode(allocator_, iter.node());
00457 return ret;
00458 }
00459
00460 template < class T, class A >
00461 inline
00462 TYPENAME _list< T, A >::_node*
00463 _list< T, A >::_buynode(byte_allocator& allocator_, const T& x)
00464 {
00465 void* ptr = allocator_.allocate(sizeof(TYPENAME _list< T, A >::_node));
00466 if (!ptr)
00467 return 0;
00468 return new (ptr) TYPENAME _list< T, A >::_node(x);
00469 }
00470
00471 template < class T, class A >
00472 inline
00473 TYPENAME _list< T, A >::_node*
00474 _list< T, A >::_buynode(node_allocator< TYPENAME base_list< T >::_node >& allocator_, const T& x)
00475 {
00476 void* ptr = allocator_.allocate();
00477 if (!ptr)
00478 return 0;
00479 return new (ptr) TYPENAME _list< T, A >::_node(x);
00480 }
00481
00482 template < class T, class A >
00483 inline
00484 void
00485 _list< T, A >::_freenode(node_allocator< TYPENAME base_list< T >::_node >& allocator_, TYPENAME _list< T, A >::_node* p)
00486 {
00487 allocator_.deallocate(p);
00488 }
00489
00491
00492
00493
00494 template < class T >
00495 inline
00496 list< T >::list(size_t size) :
00497 base_list< T >(), _length(0), _allocator(size)
00498 {
00499 }
00500
00501
00502 template < class T >
00503 inline
00504 list< T >::~list()
00505 {
00506 clear();
00507 }
00508
00509
00510 template < class T >
00511 inline
00512 list< T >::list(const list< T >& x) :
00513 base_list< T >(), _length(0), _allocator(x._allocator.capacity())
00514 {
00515 *this = x;
00516 }
00517
00518
00519 template < class T >
00520 inline
00521 list< T >&
00522 list< T >::operator=(const list< T >& x)
00523 {
00524 if (this != &x)
00525 {
00526 clear();
00527 for (TYPENAME list< T >::const_iterator iter = x.begin(); iter != x.end(); ++iter)
00528 push_back(*iter);
00529 }
00530
00531 return *this;
00532 }
00533
00534
00535
00536 template < class T >
00537 inline
00538 void
00539 list< T >::clear()
00540 {
00541 for (TYPENAME list< T >::iterator I = this->begin(); I != this->end();)
00542 {
00543 TYPENAME list< T >::_node* f = I.node();
00544 ++I;
00545 _freenode(f);
00546 }
00547
00548 base_list< T >::clear();
00549 _allocator.clear_all();
00550 _length = 0;
00551 }
00552
00553 template < class T >
00554 inline
00555 void
00556 list< T >::assign(const list< T >& x)
00557 {
00558 assign(x.begin(), x.end());
00559 }
00560
00561 template < class T >
00562 inline
00563 void
00564 list< T >::assign(TYPENAME list< T >::const_iterator first, TYPENAME list< T >::const_iterator last)
00565 {
00566 clear();
00567 for (;first != last; ++first)
00568 push_back(*first);
00569 }
00570
00571 template < class T >
00572 inline
00573 void
00574 list< T >::assign(size_t n, const T& x)
00575 {
00576 clear();
00577 for (size_t I = 0; I < n; ++I)
00578 push_back(x);
00579 }
00580
00581 template < class T >
00582 inline
00583 size_t
00584 list< T >::size() const
00585 {
00586 return _length;
00587 }
00588
00589
00590 template < class T >
00591 inline
00592 TYPENAME
00593 list< T >::iterator
00594 list< T >::push_back(const T& x)
00595 {
00596
00597 TYPENAME list< T >::_node* n = _buynode(x);
00598 if (!n)
00599 return this->end();
00600
00601 n->_prev = this->_head._prev;
00602 n->_next = this->head();
00603 this->_head._prev->_next = n;
00604 this->_head._prev = n;
00605
00606 ++_length;
00607
00608 return TYPENAME list< T >::iterator(n);
00609 }
00610
00611
00612 template < class T >
00613 inline
00614 TYPENAME
00615 list< T >::iterator
00616 list< T >::push_front(const T& x)
00617 {
00618
00619 TYPENAME list< T >::_node* n = _buynode(x);
00620 if (!n)
00621 return this->end();
00622 n->_prev = this->head();
00623 n->_next = this->_head._next;
00624 this->_head._next->_prev = n;
00625 this->_head._next = n;
00626
00627 ++_length;
00628
00629 return TYPENAME list< T >::iterator(n);
00630 }
00631
00632
00633
00634 template < class T >
00635 inline
00636 bool
00637 list< T >::pop_front()
00638 {
00639 if (!_length)
00640 return false;
00641 TYPENAME list< T >::_node* f = this->_head._next;
00642 this->_head._next->_next->_prev = this->head();
00643 this->_head._next = this->_head._next->_next;
00644
00645 --_length;
00646 _freenode(f);
00647 return true;
00648 }
00649
00650
00651
00652 template < class T >
00653 inline
00654 bool
00655 list< T >::pop_back()
00656 {
00657 if (!_length)
00658 return false;
00659 TYPENAME list< T >::_node* f = this->_head._prev;
00660 this->_head._prev->_prev->_next = this->head();
00661 this->_head._prev = this->_head._prev->_prev;
00662 --_length;
00663 _freenode(f);
00664 return true;
00665 }
00666
00667 template < class T >
00668 inline
00669 TYPENAME list< T >::iterator
00670 list< T >::insert(TYPENAME list< T >::iterator it, const T& x)
00671 {
00672 TYPENAME list< T >::_node* n = _buynode(x);
00673 if (!n)
00674 return this->end();
00675 TYPENAME list< T >::_node* i = it.node();
00676
00677 n->_next = i;
00678 n->_prev = i->_prev;
00679 i->_prev = n;
00680 i = i->_prev;
00681 i->_prev->_next = i;
00682
00683
00684 ++_length;
00685 return TYPENAME list< T >::iterator(n);
00686 }
00687
00688 template < class T >
00689 inline
00690 void
00691 list< T >::insert(TYPENAME list< T >::iterator it, size_t n, const T& x)
00692 {
00693 for (size_t I = 0; I < n; ++I)
00694 it = insert(it, x);
00695 }
00696
00697 template < class T >
00698 inline
00699 void
00700 list< T >::insert(TYPENAME list< T >::iterator it, TYPENAME list< T >::const_iterator first, TYPENAME list< T >::const_iterator last)
00701 {
00702 for (first; first != last; ++first)
00703 it = insert(it, *first);
00704 }
00705
00706
00707
00708 template < class T >
00709 inline
00710 void
00711 list< T >::remove(const T& x)
00712 {
00713 for (TYPENAME list< T >::iterator I = this->begin(); I != this->end();)
00714 if (*I == x)
00715 I = remove(I);
00716 else
00717 ++I;
00718 }
00719
00720
00721
00722 template < class T >
00723 inline
00724 TYPENAME list< T >::iterator
00725 list< T >::erase(TYPENAME list< T >::iterator iter)
00726 {
00727 return remove(iter);
00728 }
00729
00730 template < class T >
00731 inline
00732 TYPENAME list< T >::iterator
00733 list< T >::erase(TYPENAME list< T >::iterator first, TYPENAME list< T >::iterator last)
00734 {
00735 for (;first != last;)
00736 first = remove(first);
00737
00738 return first;
00739 }
00740
00741 template < class T >
00742 inline
00743 TYPENAME list< T >::iterator
00744 list< T >::remove(TYPENAME list< T >::iterator iter)
00745 {
00746 iter.prev()->_next = iter.next();
00747 iter.next()->_prev = iter.prev();
00748 TYPENAME list< T >::iterator ret = iter;
00749 ++ret;
00750 _freenode(iter.node());
00751 --_length;
00752 return ret;
00753 }
00754
00755 template < class T >
00756 inline
00757 TYPENAME list< T >::_node*
00758 list< T >::_buynode(const T& x)
00759 {
00760 void* ptr = _allocator.allocate();
00761 if (!ptr)
00762 return 0;
00763 return new (ptr) TYPENAME list< T >::_node(x);
00764 }
00765
00766 template < class T >
00767 inline
00768 void
00769 list< T >::_freenode(TYPENAME list< T >::_node* p)
00770 {
00771 p->_value.~T();
00772 _allocator.deallocate(p);
00773 }
00774
00775 #pragma pack()
00776 END_TERIMBER_NAMESPACE
00777
00778 #endif // _terimber_list_hpp_
00779