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_map_h_
00029 #define _terimber_map_h_
00030
00031 #include "base/memory.h"
00032
00033 BEGIN_TERIMBER_NAMESPACE
00034 #pragma pack(4)
00035
00037 template< class T >
00038 class less
00039 {
00040 public:
00042 inline
00043 bool
00044 operator()( const T& first,
00045 const T& second
00046 ) const;
00047 };
00048
00052 template < class T1, class T2 >
00053 class pair
00054 {
00055 public:
00057 pair< T1, T2 >();
00059 pair< T1, T2 >( const T1& v1,
00060 const T2& v2
00061 );
00063 pair< T1, T2 >(const pair< T1, T2 >& x);
00065 inline bool operator==(const pair< T1, T2 >& x) const;
00067 inline bool operator<(const pair< T1, T2 >& x) const;
00069 inline bool operator!=(const pair< T1, T2 >& x) const;
00070
00071 T1 first;
00072 T2 second;
00073 };
00074
00077 template < class K, class T, class Pr = less< K >, bool M = false >
00078 class base_map
00079 {
00080 public:
00082 class iterator;
00083 class const_iterator;
00084
00086 friend class const_iterator;
00087 friend class iterator;
00088
00091 typedef bool redblack;
00092
00094 #define c_red true
00096 #define c_black false
00097
00100 typedef bool nodetype;
00101
00103 #define t_head true
00105 #define t_leaf false
00106
00108 class _node;
00111 class _node_
00112 {
00113 public:
00115 _node_() :
00116 _left(0),
00117 _parent(0),
00118 _right(0),
00119 _color(c_red),
00120 _type(t_leaf)
00121 {
00122 }
00123
00124 _node* _left;
00125 _node* _parent;
00126 _node* _right;
00127 redblack _color;
00128 nodetype _type;
00129 };
00132 class _node : public _node_
00133 {
00134 public:
00136 _node() :
00137 _node_()
00138 {
00139 }
00140
00141 K _key;
00142 T _value;
00143
00144 inline
00145 static
00146 _node*
00147 _max( _node* p
00148 )
00149 {
00150 while (p->_right->_type != t_head)
00151 p = p->_right;
00152 return p;
00153 }
00155 inline
00156 static
00157 _node*
00158 _min(_node* p)
00159 {
00160 while (p->_left->_type != t_head)
00161 p = p->_left;
00162 return p;
00163 }
00164 };
00165
00166 protected:
00168 explicit
00169 base_map< K, T, Pr, M >(const Pr& pr = Pr()
00170 );
00172 base_map< K, T, Pr, M >(const base_map< K, T, Pr, M >& x);
00174 ~base_map< K, T, Pr, M >();
00175
00176 public:
00180 class const_iterator
00181 {
00182 public:
00185 typedef std::bidirectional_iterator_tag iterator_category;
00187 typedef size_t size_type;
00189 typedef T* pointer;
00191 typedef const T* const_pointer;
00193 typedef T& reference;
00195 typedef const T& const_reference;
00197 typedef T value_type;
00199 typedef size_t difference_type;
00200
00202 inline
00203 const_iterator() :
00204 _ptr(0)
00205 {
00206 }
00208 inline
00209 const_iterator(_node* p
00210 ) :
00211 _ptr(p)
00212 {
00213 }
00215 inline
00216 const_iterator(const iterator& x) :
00217 _ptr(x._ptr)
00218 {
00219 }
00221 inline
00222 const T&
00223 operator*() const
00224 {
00225 return _ptr->_value;
00226 }
00228 inline
00229 const T*
00230 operator->() const
00231 {
00232 return &_ptr->_value;
00233 }
00235 inline
00236 const_iterator&
00237 operator++()
00238 {
00239 _inc();
00240 return *this;
00241 }
00243 inline
00244 const_iterator
00245 operator++(int)
00246 {
00247 const_iterator tmp = *this;
00248 _inc();
00249 return tmp;
00250 }
00252 inline
00253 const_iterator& operator--()
00254 {
00255 _dec();
00256 return *this;
00257 }
00259 inline
00260 const_iterator
00261 operator--(int)
00262 {
00263 const_iterator tmp = *this;
00264 _dec();
00265 return tmp;
00266 }
00268 inline
00269 bool
00270 operator==(const const_iterator& x) const
00271 {
00272 return _ptr == x._ptr;
00273 }
00275 inline
00276 bool
00277 operator!=(const const_iterator& x) const
00278 {
00279 return _ptr != x._ptr;
00280 }
00282 inline
00283 const
00284 K& key() const
00285 {
00286 return _ptr->_key;
00287 }
00289 inline void _dec()
00290 {
00291 if (_ptr->_type == t_head)
00292 _ptr = _ptr->_right;
00293 else if (_ptr->_left->_type != t_head)
00294 _ptr = _node::_max(_ptr->_left);
00295 else
00296 {
00297 _node* p;
00298 while ((p = _ptr->_parent)->_type != t_head && _ptr == p->_left)
00299 _ptr = p;
00300 if (p->_type != t_head)
00301 _ptr = p;
00302 }
00303 }
00305 inline void _inc()
00306 {
00307 if (_ptr->_type == t_head)
00308
00309 _ptr = _ptr->_left;
00310 else if (_ptr->_right->_type != t_head)
00311 _ptr = _node::_min(_ptr->_right);
00312 else
00313 {
00314 _node* p;
00315 while ((p = _ptr->_parent)->_type != t_head && _ptr == p->_right)
00316 _ptr = p;
00317
00318 _ptr = p;
00319 }
00320 }
00322 inline
00323 _node*
00324 left() const
00325 {
00326 return _ptr->_left;
00327 }
00329 inline
00330 _node*
00331 right() const
00332 {
00333 return _ptr->_right;
00334 }
00336 inline
00337 _node*
00338 parent() const
00339 {
00340 return _ptr->_parent;
00341 }
00343 inline
00344 _node* node() const
00345 {
00346 return _ptr;
00347 }
00348 protected:
00349 _node* _ptr;
00350 };
00351
00354 class iterator : public const_iterator
00355 {
00356 public:
00358 inline
00359 iterator()
00360 {}
00362 inline
00363 iterator( _node* p
00364 ) :
00365 const_iterator(p)
00366 {
00367 }
00369 inline
00370 T&
00371 operator*() const
00372 {
00373 return this->_ptr->_value;
00374 }
00376 inline
00377 T*
00378 operator->() const
00379 {
00380 return &this->_ptr->_value;
00381 }
00383 inline
00384 iterator&
00385 operator++()
00386 {
00387 this->_inc();
00388 return *this;
00389 }
00391 inline
00392 iterator
00393 operator++(int)
00394 {
00395 iterator tmp = *this;
00396 this->_inc();
00397 return tmp;
00398 }
00400 inline
00401 iterator&
00402 operator--()
00403 {
00404 this->_dec();
00405 return *this;
00406 }
00408 inline
00409 iterator
00410 operator--(int)
00411 {
00412 iterator tmp = *this;
00413 this->_dec();
00414 return tmp;
00415 }
00417 inline
00418 bool
00419 operator==(const iterator& x) const
00420 {
00421 return this->_ptr == x._ptr;
00422 }
00424 inline
00425 bool
00426 operator!=(const iterator& x) const
00427 {
00428 return this->_ptr != x._ptr;
00429 }
00430 };
00431
00433 inline
00434 const Pr&
00435 comp() const;
00437 inline
00438 TYPENAME
00439 base_map< K, T, Pr, M >::iterator
00440 begin();
00442 inline
00443 TYPENAME
00444 base_map< K, T, Pr, M >::const_iterator
00445 begin() const;
00447 inline
00448 TYPENAME
00449 base_map< K, T, Pr, M >::iterator
00450 end();
00452 inline
00453 TYPENAME
00454 base_map< K, T, Pr, M >::const_iterator
00455 end() const;
00457 inline
00458 size_t
00459 size() const;
00461 inline
00462 bool
00463 empty() const;
00465 inline
00466 TYPENAME
00467 base_map< K, T, Pr, M >::iterator
00468 find( const K& k
00469 );
00471 inline
00472 TYPENAME
00473 base_map< K, T, Pr, M >::const_iterator
00474 find( const K& k
00475 ) const;
00478 typedef pair< iterator, iterator > pairii_t;
00481 typedef pair< const_iterator, const_iterator > paircc_t;
00483 inline
00484 TYPENAME
00485 base_map< K, T, Pr, M >::iterator
00486 lower_bound( const K& k
00487 );
00489 inline
00490 TYPENAME
00491 base_map< K, T, Pr, M >::const_iterator
00492 lower_bound( const K& k
00493 ) const;
00495 inline
00496 TYPENAME
00497 base_map< K, T, Pr, M >::iterator
00498 upper_bound( const K& k
00499 );
00501 inline
00502 TYPENAME
00503 base_map< K, T, Pr, M >::const_iterator
00504 upper_bound( const K& k
00505 ) const;
00507 inline
00508 pairii_t
00509 equal_range( const K& k
00510 );
00512 inline
00513 paircc_t
00514 equal_range( const K& k
00515 ) const;
00517 inline
00518 void
00519 clear();
00520
00523 typedef pair< iterator, bool > pairib_t;
00524
00525 protected:
00527 inline
00528 TYPENAME
00529 base_map< K, T, Pr, M >::iterator
00530 _insert( bool left,
00531 TYPENAME base_map< K, T, Pr, M >::_node* w,
00532 TYPENAME base_map< K, T, Pr, M >::_node* n,
00533 const K& k,
00534 const T& v
00535 );
00537 inline
00538 TYPENAME
00539 base_map< K, T, Pr, M >::iterator
00540 _erase( iterator w,
00541 TYPENAME base_map< K, T, Pr, M >::_node*& e
00542 );
00544 inline
00545 TYPENAME base_map< K, T, Pr, M >::_node*
00546 _lbound( const K& k
00547 ) const;
00549 inline
00550 TYPENAME base_map< K, T, Pr, M >::_node*
00551 _ubound( const K& k
00552 ) const;
00554 inline
00555 TYPENAME
00556 base_map< K, T, Pr, M >::_node*&
00557 lmost();
00559 inline
00560 TYPENAME
00561 base_map< K, T, Pr, M >::_node*&
00562 lmost() const;
00564 inline
00565 TYPENAME
00566 base_map< K, T, Pr, M >::_node*&
00567 rmost();
00569 inline
00570 TYPENAME
00571 base_map< K, T, Pr, M >::_node*&
00572 rmost() const;
00574 inline
00575 TYPENAME
00576 base_map< K, T, Pr, M >::_node*&
00577 root();
00579 inline
00580 TYPENAME
00581 base_map< K, T, Pr, M >::_node*&
00582 root() const;
00583 inline
00584 TYPENAME
00585 base_map< K, T, Pr, M >::_node*
00586 head();
00588 inline
00589 TYPENAME
00590 base_map< K, T, Pr, M >::_node*
00591 head() const;
00593 inline
00594 void
00595 _lrotate( TYPENAME base_map< K, T, Pr, M >::_node* x
00596 );
00598 inline
00599 void
00600 _rrotate( TYPENAME base_map< K, T, Pr, M >::_node* x
00601 );
00603 inline
00604 void
00605 _consval( T* p,
00606 const T& v
00607 );
00609 inline
00610 void
00611 _conskey( K* p,
00612 const K& k
00613 );
00615 inline
00616 void
00617 _destval( T* p
00618 );
00620 inline
00621 void
00622 _destkey( K* p
00623 );
00624
00625
00626 Pr key_compare;
00627 TYPENAME base_map< K, T, Pr, M >::_node_ _head;
00628 size_t _size;
00629 };
00630
00634 template < class K, class T, class A = byte_allocator, class Pr = less< K >, bool M = false >
00635 class _map : public base_map< K, T, Pr, M >
00636 {
00638 _map< K, T, A, Pr, M >& operator=(const _map< K, T, A, Pr, M >& x);
00639 public:
00641 explicit
00642 _map< K, T, A, Pr, M >(const Pr& pr = Pr()
00643 );
00645 _map< K, T, A, Pr, M >(const _map< K, T, A, Pr, M >& x
00646 );
00648 ~_map< K, T, A, Pr, M >();
00651 inline
00652 void
00653 assign( A& allocator_,
00654 const _map< K, T, A, Pr, M >& x
00655 );
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667 inline
00668 TYPENAME
00669 _map< K, T, A, Pr, M >::pairib_t
00670 insert( A& allocator_,
00671 const K& k,
00672 const T& v
00673 );
00674
00677 inline
00678 TYPENAME _map< K, T, A, Pr, M >::iterator
00679 erase( TYPENAME _map< K, T, A, Pr, M >::iterator p
00680 );
00683 inline
00684 TYPENAME
00685 _map< K, T, A, Pr, M >::iterator
00686 erase( node_allocator< TYPENAME base_map< K, T, Pr, M >::_node >& allocator_,
00687 TYPENAME _map< K, T, A, Pr, M >::iterator p
00688 );
00689
00691 inline
00692 void
00693 erase( const K& k
00694 );
00696 inline
00697 void
00698 erase( node_allocator< TYPENAME base_map< K, T, Pr, M >::_node >& allocator_,
00699 const K& k
00700 );
00702 inline
00703 TYPENAME
00704 _map< K, T, A, Pr, M >::iterator
00705 erase( TYPENAME _map< K, T, A, Pr, M >::iterator first,
00706 TYPENAME _map< K, T, A, Pr, M >::iterator last
00707 );
00709 inline
00710 TYPENAME _map< K, T, A, Pr, M >::iterator
00711 erase( node_allocator< TYPENAME base_map< K, T, Pr, M >::_node >& allocator_,
00712 TYPENAME _map< K, T, A, Pr, M >::iterator first,
00713 TYPENAME _map< K, T, A, Pr, M >::iterator last
00714 );
00715 protected:
00717 inline
00718 void
00719 _erase( TYPENAME _map< K, T, A, Pr, M >::_node* x
00720 );
00722 inline
00723 void
00724 _erase( node_allocator< TYPENAME base_map< K, T, Pr, M >::_node >& allocator_,
00725 TYPENAME _map< K, T, A, Pr, M >::_node* x
00726 );
00728 inline
00729 TYPENAME
00730 _map< K, T, A, Pr, M >::iterator
00731 _insert( A& _allocator,
00732 bool left,
00733 TYPENAME _map< K, T, A, Pr, M >::_node* w,
00734 const K& k,
00735 const T& v
00736 );
00738 inline
00739 TYPENAME
00740 _map< K, T, A, Pr, M >::_node*
00741 _buynode( byte_allocator& _allocator,
00742 TYPENAME _map< K, T, A, Pr, M >::_node* p,
00743 TYPENAME _map< K, T, A, Pr, M >::redblack c,
00744 TYPENAME _map< K, T, A, Pr, M >::nodetype t
00745 );
00747 inline
00748 TYPENAME
00749 _map< K, T, A, Pr, M >::_node*
00750 _buynode( node_allocator< TYPENAME base_map< K, T, Pr, M >::_node >& _allocator,
00751 TYPENAME _map< K, T, A, Pr, M >::_node* p,
00752 TYPENAME _map< K, T, A, Pr, M >::redblack c,
00753 TYPENAME _map< K, T, A, Pr, M >::nodetype t
00754 );
00756 inline
00757 void
00758 _freenode( node_allocator< TYPENAME base_map< K, T, Pr, M >::_node >& _allocator,
00759 TYPENAME _map< K, T, A, Pr, M >::_node* s
00760 );
00761 };
00762
00766 template < class K, class T, class Pr = less< K >, bool M = false >
00767 class map : public base_map< K, T, Pr, M >
00768 {
00769 public:
00771 explicit
00772 map< K, T, Pr, M >( const Pr& pr = Pr(),
00773 size_t size = os_def_size
00774 );
00776 map< K, T, Pr, M >(const map< K, T, Pr, M >& x);
00778 ~map< K, T, Pr, M >();
00780 map< K, T, Pr, M >& operator=(const map< K, T, Pr, M >& x);
00782
00783
00784
00785
00786
00787
00788
00789 inline
00790 TYPENAME map< K, T, Pr, M >::pairib_t
00791 insert( const K& k,
00792 const T& v
00793 );
00795 inline
00796 TYPENAME map< K, T, Pr, M >::iterator
00797 erase( TYPENAME map< K, T, Pr, M >::iterator p
00798 );
00800 inline
00801 void
00802 erase( const K& k
00803 );
00805 inline
00806 void
00807 clear();
00808
00809 protected:
00811 inline
00812 TYPENAME map< K, T, Pr, M >::iterator
00813 erase( TYPENAME map< K, T, Pr, M >::iterator first,
00814 TYPENAME map< K, T, Pr, M >::iterator last
00815 );
00817 inline
00818 void
00819 _erase( TYPENAME map< K, T, Pr, M >::_node* x
00820 );
00822 inline
00823 TYPENAME
00824 map< K, T, Pr, M >::iterator
00825 _insert( bool left,
00826 TYPENAME map< K, T, Pr, M >::_node* w,
00827 const K& k,
00828 const T& v
00829 );
00831 inline
00832 TYPENAME
00833 map< K, T, Pr, M >::_node*
00834 _buynode( TYPENAME map< K, T, Pr, M >::_node* p,
00835 TYPENAME map< K, T, Pr, M >::redblack c,
00836 TYPENAME map< K, T, Pr, M >::nodetype t
00837 );
00839 inline
00840 void
00841 _freenode( TYPENAME map< K, T, Pr, M >::_node* s
00842 );
00843
00844 node_allocator< TYPENAME base_map< K, T, Pr, M >::_node > _allocator;
00845 };
00846
00847 #pragma pack()
00848 END_TERIMBER_NAMESPACE
00849
00850 #endif // _terimber_map_h_
00851