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_h_
00029 #define _terimber_list_h_
00030
00031 #include "base/memory.h"
00032
00033 BEGIN_TERIMBER_NAMESPACE
00034 #pragma pack(4)
00035
00038 template < class T >
00039 class base_list
00040 {
00042 base_list< T >(const base_list< T >& x);
00044 base_list< T >& operator=(const base_list< T >& x);
00045
00046 public:
00048 class _node;
00051 class _node_
00052 {
00053 public:
00055 _node_() :
00056 _next(0),
00057 _prev(0)
00058 {
00059 }
00060
00061 _node* _next;
00062 _node* _prev;
00063 };
00064
00067 class _node : public _node_
00068 {
00069 public:
00071 _node(const T& x) :
00072 _node_(),
00073 _value(x)
00074 {
00075 }
00076
00077 T _value;
00078 };
00079
00080 protected:
00081
00083 inline
00084 base_list< T >();
00086 inline
00087 ~base_list< T >();
00088
00089 public:
00091 inline
00092 bool
00093 empty() const;
00096 inline
00097 const T&
00098 front() const;
00101 inline
00102 T&
00103 front();
00106 inline
00107 const T&
00108 back() const;
00111 inline
00112 T&
00113 back();
00114
00118 class iterator;
00121 class const_iterator
00122 {
00123 public:
00125 typedef std::bidirectional_iterator_tag iterator_category;
00127 typedef size_t size_type;
00129 typedef T* pointer;
00131 typedef const T* const_pointer;
00133 typedef T& reference;
00135 typedef const T& const_reference;
00137 typedef T value_type;
00139 typedef size_t difference_type;
00140
00142 inline
00143 const_iterator(_node* p
00144 ) : _ptr(p) {}
00146 inline
00147 const_iterator(const iterator& x
00148 ) : _ptr(x._ptr) {}
00150 inline
00151 const T&
00152 operator*() const
00153 {
00154 return _ptr->_value;
00155 }
00157 inline
00158 const T*
00159 operator->() const
00160 {
00161 return &(_ptr->_value);
00162 }
00164 inline
00165 const_iterator
00166 operator++()
00167 {
00168 _ptr = _ptr->_next;
00169 return *this;
00170 }
00172 inline
00173 const_iterator
00174 operator++(int)
00175 {
00176 const_iterator tmp = _ptr;
00177 _ptr = _ptr->_next;
00178 return tmp;
00179 }
00181 inline
00182 const_iterator
00183 operator--()
00184 {
00185 _ptr = _ptr->_prev;
00186 return *this;
00187 }
00189 inline
00190 const_iterator
00191 operator--(int)
00192 {
00193 const_iterator tmp = _ptr;
00194 _ptr = _ptr->_prev;
00195 return tmp;
00196 }
00198 inline
00199 bool
00200 operator==(const const_iterator& x) const
00201 {
00202 return _ptr == x._ptr;
00203 }
00205 inline
00206 bool
00207 operator!=(const const_iterator& x) const
00208 {
00209 return _ptr != x._ptr;
00210 }
00212 inline
00213 _node*
00214 next() const
00215 {
00216 return _ptr->_next;
00217 }
00219 inline
00220 _node*
00221 prev() const
00222 {
00223 return _ptr->_prev;
00224 }
00226 inline
00227 _node*
00228 node() const
00229 {
00230 return _ptr;
00231 }
00232 protected:
00233 _node* _ptr;
00234 };
00235
00238 class iterator : public const_iterator
00239 {
00240 public:
00242 inline
00243 iterator( _node* p
00244 ) : const_iterator(p)
00245 {
00246 }
00248 inline
00249 T&
00250 operator*() const
00251 {
00252 return this->_ptr->_value;
00253 }
00255 inline
00256 T*
00257 operator->() const
00258 {
00259 return &(this->_ptr->_value);
00260 }
00262 inline
00263 iterator
00264 operator++()
00265 {
00266 this->_ptr = this->_ptr->_next;
00267 return *this;
00268 }
00270 inline
00271 iterator
00272 operator++(int)
00273 {
00274 iterator tmp = this->_ptr;
00275 this->_ptr = this->_ptr->_next;
00276 return tmp;
00277 }
00279 inline
00280 iterator
00281 operator--()
00282 {
00283 this->_ptr = this->_ptr->_prev;
00284 return *this;
00285 }
00287 inline
00288 iterator
00289 operator--(int)
00290 {
00291 iterator tmp = this->_ptr;
00292 this->_ptr = this->_ptr->_prev;
00293 return tmp;
00294 }
00296 inline
00297 bool
00298 operator==(const iterator& x) const
00299 {
00300 return this->_ptr == x._ptr;
00301 }
00303 inline
00304 bool
00305 operator!=(const iterator& x) const
00306 {
00307 return this->_ptr != x._ptr;
00308 }
00309 };
00310
00312 inline TYPENAME base_list< T >::const_iterator begin() const;
00314 inline TYPENAME base_list< T >::iterator begin();
00316 inline TYPENAME base_list< T >::const_iterator end() const;
00318 inline TYPENAME base_list< T >::iterator end();
00320 inline
00321 void
00322 clear();
00323 protected:
00325 inline
00326 TYPENAME base_list< T >::_node*
00327 head();
00329 inline
00330 TYPENAME base_list< T >::_node*
00331 head() const;
00332
00333 TYPENAME base_list< T >::_node_ _head;
00334 };
00335
00342 template < class T, class A = byte_allocator >
00343 class _list : public base_list< T >
00344 {
00345 public:
00347 inline
00348 _list< T, A >();
00350 inline
00351 ~_list< T, A >();
00355 inline
00356 _list< T, A >(const _list< T, A >& x);
00360 inline
00361 _list< T, A >& operator=(const _list< T, A >& x);
00363 inline
00364 void
00365 assign( A& allocator_,
00366 const _list< T, A >& x
00367 );
00369 inline
00370 void
00371 assign( A& allocator_,
00372 TYPENAME _list< T, A >::const_iterator first,
00373 TYPENAME _list< T, A >::const_iterator last
00374 );
00376 inline
00377 void
00378 assign( A& allocator_,
00379 size_t n,
00380 const T& x = T()
00381 );
00384 inline
00385 size_t
00386 size() const;
00388 inline
00389 TYPENAME _list< T, A >::iterator
00390 push_back( A& allocator_,
00391 const T& x
00392 );
00394 inline
00395 TYPENAME _list< T, A >::iterator
00396 push_front( A& allocator_,
00397 const T& x
00398 );
00401 inline
00402 bool
00403 pop_front();
00405 inline
00406 bool
00407 pop_front( node_allocator< TYPENAME base_list< T >::_node >& allocator_
00408 );
00410 inline
00411 bool
00412 pop_back();
00414 inline
00415 bool
00416 pop_back( node_allocator< TYPENAME base_list< T >::_node >& allocator_
00417 );
00419 inline
00420 TYPENAME _list< T, A >::iterator
00421 insert( A& allocator_,
00422 TYPENAME _list< T, A >::iterator it,
00423 const T& x = T()
00424 );
00426 inline
00427 void
00428 insert( A& allocator_,
00429 TYPENAME _list< T, A >::iterator it,
00430 size_t n,
00431 const T& x
00432 );
00434 inline
00435 void
00436 insert( A& allocator_,
00437 TYPENAME _list< T, A >::iterator it,
00438 TYPENAME _list< T, A >::const_iterator first,
00439 TYPENAME _list< T, A >::const_iterator last
00440 );
00443 inline
00444 void
00445 remove( const T& x
00446 );
00449 inline
00450 void
00451 remove( node_allocator< TYPENAME base_list< T >::_node >& allocator_,
00452 const T& x
00453 );
00456 inline
00457 TYPENAME _list< T, A >::iterator
00458 erase( TYPENAME _list< T, A >::iterator iter
00459 );
00462 inline
00463 TYPENAME _list< T, A >::iterator
00464 erase( node_allocator< TYPENAME base_list< T >::_node >& allocator_,
00465 TYPENAME _list< T, A >::iterator iter
00466 );
00469 inline
00470 TYPENAME _list< T, A >::iterator
00471 erase( TYPENAME _list< T, A >::iterator first,
00472 TYPENAME _list< T, A >::iterator last
00473 );
00476 inline
00477 TYPENAME _list< T, A >::iterator
00478 erase( node_allocator< TYPENAME base_list< T >::_node >& allocator_,
00479 TYPENAME _list< T, A >::iterator first,
00480 TYPENAME _list< T, A >::iterator last
00481 );
00482 private:
00485 inline
00486 TYPENAME _list< T, A >::iterator
00487 remove( TYPENAME _list< T, A >::iterator iter
00488 );
00491 inline
00492 TYPENAME _list< T, A >::iterator
00493 remove( node_allocator< TYPENAME base_list< T >::_node >& allocator_,
00494 TYPENAME _list< T, A >::iterator iter
00495 );
00497 inline
00498 TYPENAME _list< T, A >::_node*
00499 _buynode( byte_allocator& allocator_,
00500 const T& x
00501 );
00503 inline
00504 TYPENAME _list< T, A >::_node*
00505 _buynode( node_allocator< TYPENAME base_list< T >::_node >& allocator_,
00506 const T& x
00507 );
00509 inline
00510 void
00511 _freenode( node_allocator< TYPENAME base_list< T >::_node >& allocator_,
00512 TYPENAME _list< T, A >::_node* p
00513 );
00514 };
00515
00521 template < class T >
00522 class list : public base_list< T >
00523 {
00524 public:
00526 list< T >( size_t size = os_def_size
00527 );
00529 ~list< T >();
00531 list< T >(const list< T >& x);
00533 list< T >& operator=(const list< T >& x);
00536 inline
00537 void
00538 clear();
00540 inline
00541 void
00542 assign( const list< T >& x
00543 );
00545 inline
00546 void
00547 assign( TYPENAME list< T >::const_iterator first,
00548 TYPENAME list< T >::const_iterator last
00549 );
00551 inline
00552 void
00553 assign( size_t n,
00554 const T& x = T()
00555 );
00557 inline
00558 size_t
00559 size() const;
00561 inline
00562 TYPENAME list< T >::iterator
00563 push_back( const T& x
00564 );
00566 inline
00567 TYPENAME list< T >::iterator
00568 push_front( const T& x
00569 );
00572 inline
00573 bool
00574 pop_front();
00577 inline
00578 bool
00579 pop_back();
00582 TYPENAME list< T >::iterator
00583 insert( TYPENAME list< T >::iterator it,
00584 const T& x = T()
00585 );
00587
00588 void
00589 insert( TYPENAME list< T >::iterator it,
00590 size_t n,
00591 const T& x
00592 );
00595 void
00596 insert( TYPENAME list< T >::iterator it,
00597 TYPENAME list< T >::const_iterator first,
00598 TYPENAME list< T >::const_iterator last
00599 );
00602 inline
00603 void
00604 remove( const T& x
00605 );
00609 inline
00610 TYPENAME list< T >::iterator
00611 erase( TYPENAME list< T >::iterator iter
00612 );
00616 inline
00617 TYPENAME list< T >::iterator
00618 erase( TYPENAME list< T >::iterator first,
00619 TYPENAME list< T >::iterator last
00620 );
00621 private:
00623 inline
00624 TYPENAME list< T >::iterator
00625 remove( TYPENAME list< T >::iterator iter
00626 );
00627 protected:
00629 inline
00630 TYPENAME list< T >::_node*
00631 _buynode( const T& x
00632 );
00634 inline
00635 void
00636 _freenode( TYPENAME list< T >::_node* p
00637 );
00638 protected:
00639 size_t _length;
00640 node_allocator< TYPENAME base_list< T >::_node > _allocator;
00641 };
00642
00643 #pragma pack()
00644 END_TERIMBER_NAMESPACE
00645
00646 #endif // _terimber_list_h_
00647