Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

964 lines
31 KiB

  1. #if _MSC_VER > 1000
  2. #pragma once
  3. #endif // _MSC_VER > 1000
  4. template< class Key, class T, class ExtractKey, class CompareKey, class Allocator>
  5. class RBTree
  6. {
  7. public: // Types
  8. typedef RBTree< Key, T, ExtractKey, CompareKey, Allocator> tree_type;
  9. typedef Allocator allocator_type;
  10. typedef Key key_type;
  11. typedef typename allocator_type::value_type value_type;
  12. typedef CompareKey key_compare;
  13. typedef ExtractKey key_extract;
  14. typedef typename allocator_type::pointer pointer;
  15. typedef typename allocator_type::const_pointer const_pointer;
  16. typedef typename allocator_type::reference reference;
  17. typedef typename allocator_type::const_reference const_reference;
  18. typedef typename allocator_type::size_type size_type;
  19. typedef typename allocator_type::difference_type difference_type;
  20. protected: // Types
  21. struct tree_node;
  22. typedef typename Allocator::rebind< tree_node>::other
  23. tree_node_allocator_type;
  24. typedef typename tree_node_allocator_type::pointer tree_node_pointer;
  25. typedef typename tree_node_allocator_type::const_pointer
  26. tree_node_const_pointer;
  27. struct tree_node
  28. {
  29. tree_node_pointer m_pLeft;
  30. tree_node_pointer m_pParent;
  31. tree_node_pointer m_pRight;
  32. value_type m_Value;
  33. bool m_bRed;
  34. tree_node( const tree_node_pointer& pP, const value_type& v, bool bRed)
  35. :m_pLeft( NULL), m_pParent( pP), m_pRight( NULL), m_Value( v),
  36. m_bRed( bRed)
  37. { }
  38. ~tree_node()
  39. { }
  40. };
  41. template< class TPointer>
  42. struct NextInOrderNode:
  43. public unary_function< TPointer, TPointer>
  44. {
  45. result_type operator()( argument_type Arg, const bool bCouldBeEnd) const
  46. {
  47. if( bCouldBeEnd&& Arg->m_bRed&& Arg->m_pParent->m_pParent== Arg)
  48. Arg= Arg->m_pLeft;
  49. else if( Arg->m_pRight!= NULL)
  50. {
  51. Arg= Arg->m_pRight;
  52. while( Arg->m_pLeft!= NULL)
  53. Arg= Arg->m_pLeft;
  54. }
  55. else
  56. {
  57. TPointer pP;
  58. while( Arg== (pP= Arg->m_pParent)->m_pRight)
  59. Arg= pP;
  60. if( bCouldBeEnd|| Arg->m_pRight!= pP)
  61. Arg= pP;
  62. }
  63. return Arg;
  64. }
  65. };
  66. template< class TPointer>
  67. struct PrevInOrderNode:
  68. public unary_function< TPointer, TPointer>
  69. {
  70. result_type operator()( argument_type Arg, const bool bCouldBeEnd) const
  71. {
  72. if( bCouldBeEnd&& Arg->m_bRed&& Arg->m_pParent->m_pParent== Arg)
  73. Arg= Arg->m_pRight;
  74. else if( Arg->m_pLeft!= NULL)
  75. {
  76. Arg= Arg->m_pLeft;
  77. while( Arg->m_pRight!= NULL)
  78. Arg= Arg->m_pRight;
  79. }
  80. else
  81. {
  82. TPointer pP;
  83. while( Arg== (pP= Arg->m_pParent)->m_pLeft)
  84. Arg= pP;
  85. if( bCouldBeEnd|| Arg->m_pLeft!= pP)
  86. Arg= pP;
  87. }
  88. return Arg;
  89. }
  90. };
  91. public: // Types
  92. class iterator;
  93. class const_iterator;
  94. class reverse_iterator;
  95. class const_reverse_iterator;
  96. friend class iterator;
  97. class iterator
  98. {
  99. public: // Types
  100. typedef bidirectional_iterator_tag iterator_category;
  101. typedef value_type value_type;
  102. typedef difference_type difference_type;
  103. typedef pointer pointer;
  104. typedef reference reference;
  105. friend class const_iterator;
  106. friend class reverse_iterator;
  107. friend class const_reverse_iterator;
  108. friend class RBTree< Key, T, ExtractKey, CompareKey, Allocator>;
  109. protected:
  110. tree_node_pointer m_pNode;
  111. NextInOrderNode< tree_node_pointer> m_fnNext;
  112. PrevInOrderNode< tree_node_pointer> m_fnPrev;
  113. public:
  114. iterator()
  115. { }
  116. explicit iterator( const tree_node_pointer& pN)
  117. :m_pNode( pN)
  118. { }
  119. iterator( const iterator& Other)
  120. :m_pNode( Other.m_pNode)
  121. { }
  122. iterator( const reverse_iterator& Other)
  123. :m_pNode( Other.m_pNode)
  124. { }
  125. reference operator*() const
  126. { return m_pNode->m_Value; }
  127. pointer operator->() const
  128. { return &**this; }
  129. iterator& operator++()
  130. {
  131. // end()++ is not valid.
  132. m_pNode= m_fnNext( m_pNode, false);
  133. return (*this);
  134. }
  135. iterator operator++( int)
  136. {
  137. iterator Temp( *this);
  138. ++(*this);
  139. return Temp;
  140. }
  141. iterator& operator--()
  142. {
  143. // end()-- is valid.
  144. m_pNode= m_fnPrev( m_pNode, true);
  145. return (*this);
  146. }
  147. iterator operator--( int)
  148. {
  149. iterator Temp( *this);
  150. --(*this);
  151. return (Temp);
  152. }
  153. bool operator==( const iterator& Other) const
  154. { return m_pNode== Other.m_pNode; }
  155. bool operator!=( const iterator& Other) const
  156. { return m_pNode!= Other.m_pNode; }
  157. };
  158. friend class const_iterator;
  159. class const_iterator
  160. {
  161. public: // Types
  162. typedef bidirectional_iterator_tag iterator_category;
  163. typedef value_type value_type;
  164. typedef difference_type difference_type;
  165. typedef const_pointer pointer;
  166. typedef const_reference reference;
  167. friend class const_reverse_iterator;
  168. friend class RBTree< Key, T, ExtractKey, CompareKey, Allocator>;
  169. protected:
  170. tree_node_const_pointer m_pNode;
  171. NextInOrderNode< tree_node_const_pointer> m_fnNext;
  172. PrevInOrderNode< tree_node_const_pointer> m_fnPrev;
  173. public:
  174. const_iterator()
  175. { }
  176. explicit const_iterator( const tree_node_const_pointer& pN)
  177. :m_pNode( pN)
  178. { }
  179. const_iterator( const iterator& Other)
  180. :m_pNode( Other.m_pNode)
  181. { }
  182. const_iterator( const const_iterator& Other)
  183. :m_pNode( Other.m_pNode)
  184. { }
  185. const_iterator( const reverse_iterator& Other)
  186. :m_pNode( Other.m_pNode)
  187. { }
  188. const_iterator( const const_reverse_iterator& Other)
  189. :m_pNode( Other.m_pNode)
  190. { }
  191. reference operator*() const
  192. { return m_pNode->m_Value; }
  193. pointer operator->() const
  194. { return &**this; }
  195. const_iterator& operator++()
  196. {
  197. // end()++ is not valid.
  198. m_pNode= m_fnNext( m_pNode, false);
  199. return (*this);
  200. }
  201. const_iterator operator++( int)
  202. {
  203. iterator Temp( *this);
  204. ++(*this);
  205. return Temp;
  206. }
  207. const_iterator& operator--()
  208. {
  209. // end()-- is valid.
  210. m_pNode= m_fnPrev( m_pNode, true);
  211. return (*this);
  212. }
  213. const_iterator operator--( int)
  214. {
  215. iterator Temp( *this);
  216. --(*this);
  217. return (Temp);
  218. }
  219. bool operator==( const const_iterator& Other) const
  220. { return m_pNode== Other.m_pNode; }
  221. bool operator!=( const const_iterator& Other) const
  222. { return m_pNode!= Other.m_pNode; }
  223. };
  224. friend class reverse_iterator;
  225. class reverse_iterator
  226. {
  227. public: // Types
  228. typedef bidirectional_iterator_tag iterator_category;
  229. typedef value_type value_type;
  230. typedef difference_type difference_type;
  231. typedef pointer pointer;
  232. typedef reference reference;
  233. friend class iterator;
  234. friend class const_iterator;
  235. friend class const_reverse_iterator;
  236. friend class RBTree< Key, T, ExtractKey, CompareKey, Allocator>;
  237. protected:
  238. tree_node_pointer m_pNode;
  239. PrevInOrderNode< tree_node_pointer> m_fnNext;
  240. NextInOrderNode< tree_node_pointer> m_fnPrev;
  241. public:
  242. reverse_iterator()
  243. { }
  244. explicit reverse_iterator( const tree_node_pointer& pN)
  245. :m_pNode( pN)
  246. { }
  247. reverse_iterator( const iterator& Other)
  248. :m_pNode( Other.m_pNode)
  249. { }
  250. reverse_iterator( const reverse_iterator& Other)
  251. :m_pNode( Other.m_pNode)
  252. { }
  253. reference operator*() const
  254. { return m_pNode->m_Value; }
  255. pointer operator->() const
  256. { return &**this; }
  257. reverse_iterator& operator++()
  258. {
  259. // rend()++ is not valid.
  260. m_pNode= m_fnNext( m_pNode, false);
  261. return (*this);
  262. }
  263. reverse_iterator operator++( int)
  264. {
  265. iterator Temp( *this);
  266. ++(*this);
  267. return Temp;
  268. }
  269. reverse_iterator& operator--()
  270. {
  271. // rend()-- is valid.
  272. m_pNode= m_fnPrev( m_pNode, true);
  273. return (*this);
  274. }
  275. reverse_iterator operator--( int)
  276. {
  277. iterator Temp( *this);
  278. --(*this);
  279. return (Temp);
  280. }
  281. bool operator==( const reverse_iterator& Other) const
  282. { return m_pNode== Other.m_pNode; }
  283. bool operator!=( const reverse_iterator& Other) const
  284. { return m_pNode!= Other.m_pNode; }
  285. };
  286. friend class const_reverse_iterator;
  287. class const_reverse_iterator
  288. {
  289. public: // Types
  290. typedef bidirectional_iterator_tag iterator_category;
  291. typedef value_type value_type;
  292. typedef difference_type difference_type;
  293. typedef const_pointer pointer;
  294. typedef const_reference reference;
  295. friend class const_iterator;
  296. friend class RBTree< Key, T, ExtractKey, CompareKey, Allocator>;
  297. protected:
  298. tree_node_const_pointer m_pNode;
  299. PrevInOrderNode< tree_node_const_pointer> m_fnNext;
  300. NextInOrderNode< tree_node_const_pointer> m_fnPrev;
  301. public:
  302. const_reverse_iterator()
  303. { }
  304. explicit const_reverse_iterator( const tree_node_const_pointer& pN)
  305. :m_pNode( pN)
  306. { }
  307. const_reverse_iterator( const iterator& Other)
  308. :m_pNode( Other.m_pNode)
  309. { }
  310. const_reverse_iterator( const const_iterator& Other)
  311. :m_pNode( Other.m_pNode)
  312. { }
  313. const_reverse_iterator( const reverse_iterator& Other)
  314. :m_pNode( Other.m_pNode)
  315. { }
  316. const_reverse_iterator( const const_reverse_iterator& Other)
  317. :m_pNode( Other.m_pNode)
  318. { }
  319. reference operator*() const
  320. { return m_pNode->m_Value; }
  321. pointer operator->() const
  322. { return &**this; }
  323. const_reverse_iterator& operator++()
  324. {
  325. // rend()++ is not valid.
  326. m_pNode= m_fnNext( m_pNode, false);
  327. return (*this);
  328. }
  329. const_reverse_iterator operator++( int)
  330. {
  331. iterator Temp( *this);
  332. ++(*this);
  333. return Temp;
  334. }
  335. const_reverse_iterator& operator--()
  336. {
  337. // rend()-- is valid.
  338. m_pNode= m_fnPrev( m_pNode, true);
  339. return (*this);
  340. }
  341. const_reverse_iterator operator--( int)
  342. {
  343. iterator Temp( *this);
  344. --(*this);
  345. return (Temp);
  346. }
  347. bool operator==( const const_reverse_iterator& Other) const
  348. { return m_pNode== Other.m_pNode; }
  349. bool operator!=( const const_reverse_iterator& Other) const
  350. { return m_pNode!= Other.m_pNode; }
  351. };
  352. protected: // Variables
  353. tree_node_pointer m_pHead;
  354. size_type m_uiNodes;
  355. key_compare m_key_compare;
  356. key_extract m_key_extract;
  357. tree_node_allocator_type m_allocator;
  358. protected: // Functions
  359. void BuildHeadNode()
  360. {
  361. m_pHead= m_allocator.allocate( 1);
  362. new (&m_pHead->m_pLeft) tree_node_pointer( m_pHead);
  363. new (&m_pHead->m_pParent) tree_node_pointer( NULL);
  364. new (&m_pHead->m_pRight) tree_node_pointer( m_pHead);
  365. new (&m_pHead->m_bRed) bool( true);
  366. }
  367. void DestroyHeadNode()
  368. {
  369. m_pHead->m_pLeft.~list_node_pointer();
  370. m_pHead->m_pParent.~list_node_pointer();
  371. m_pHead->m_pRight.~list_node_pointer();
  372. m_allocator.deallocate( m_pHead, 1);
  373. }
  374. void RotateLeft( tree_node_pointer pX)
  375. {
  376. tree_node_pointer pY( pX->m_pRight);
  377. pX->m_pRight= pY->m_pLeft;
  378. if( pY->m_pLeft!= NULL)
  379. pY->m_pLeft->m_pParent= pX;
  380. pY->m_pParent= pX->m_pParent;
  381. if( pX== m_pHead->m_pParent)
  382. m_pHead->m_pParent= pY;
  383. else if( pX== pX->m_pParent->m_pLeft)
  384. pX->m_pParent->m_pLeft= pY;
  385. else
  386. pX->m_pParent->m_pRight= pY;
  387. pY->m_pLeft= pX;
  388. pX->m_pParent= pY;
  389. }
  390. void RotateRight( tree_node_pointer pX)
  391. {
  392. tree_node_pointer pY( pX->m_pLeft);
  393. pX->m_pLeft= pY->m_pRight;
  394. if( pY->m_pRight!= NULL)
  395. pY->m_pRight->m_pParent= pX;
  396. pY->m_pParent= pX->m_pParent;
  397. if( pX== m_pHead->m_pParent)
  398. m_pHead->m_pParent= pY;
  399. else if( pX== pX->m_pParent->m_pRight)
  400. pX->m_pParent->m_pRight= pY;
  401. else
  402. pX->m_pParent->m_pLeft= pY;
  403. pY->m_pRight= pX;
  404. pX->m_pParent= pY;
  405. }
  406. void RecDelete( tree_node_pointer pX)
  407. {
  408. for( tree_node_pointer pY( pX); pY!= NULL; pX= pY)
  409. {
  410. RecDelete( pY->m_pRight);
  411. pY= pY->m_pLeft;
  412. m_allocator.destroy( pX);
  413. m_allocator.deallocate( pX, 1);
  414. }
  415. }
  416. tree_node_pointer lowerbound( const key_type& k) const
  417. {
  418. tree_node_pointer pX( m_pHead->m_pParent);
  419. tree_node_pointer pY( m_pHead);
  420. while( pX!= NULL)
  421. {
  422. if( m_key_compare( m_key_extract( pX->m_Value), k))
  423. pX= pX->m_pRight;
  424. else
  425. {
  426. pY= pX;
  427. pX= pX->m_pLeft;
  428. }
  429. }
  430. return pY;
  431. }
  432. tree_node_pointer upperbound( const key_type& k) const
  433. {
  434. tree_node_pointer pX( m_pHead->m_pParent);
  435. tree_node_pointer pY( m_pHead);
  436. while( pX!= NULL)
  437. {
  438. if( m_key_compare( k, m_key_extract( pX->m_Value)))
  439. {
  440. pY= pX;
  441. pX= pX->m_pLeft;
  442. }
  443. else
  444. pX= pX->m_pRight;
  445. }
  446. return pY;
  447. }
  448. iterator Insert( tree_node_pointer pX,
  449. tree_node_pointer pY, const value_type& V)
  450. {
  451. tree_node_pointer pZ( m_allocator.allocate( 1));
  452. m_allocator.construct( pZ, tree_node( pY, V, true));
  453. ++m_uiNodes;
  454. if( pY== m_pHead|| pX!= NULL||
  455. m_key_compare( m_key_extract( V), m_key_extract( pY->m_Value)))
  456. {
  457. pY->m_pLeft= pZ;
  458. if( pY== m_pHead)
  459. {
  460. m_pHead->m_pParent= pZ;
  461. m_pHead->m_pRight= pZ;
  462. }
  463. else if( pY== m_pHead->m_pLeft)
  464. m_pHead->m_pLeft= pZ;
  465. }
  466. else
  467. {
  468. pY->m_pRight= pZ;
  469. if( pY== m_pHead->m_pRight)
  470. m_pHead->m_pRight= pZ;
  471. }
  472. for( pX= pZ; pX!= m_pHead->m_pParent&& pX->m_pParent->m_bRed; )
  473. {
  474. if( pX->m_pParent== pX->m_pParent->m_pParent->m_pLeft)
  475. {
  476. pY= pX->m_pParent->m_pParent->m_pRight;
  477. if( pY!= NULL&& pY->m_bRed)
  478. {
  479. pX->m_pParent->m_bRed= false;
  480. pY->m_bRed= false;
  481. pX->m_pParent->m_pParent->m_bRed= true;
  482. pX= pX->m_pParent->m_pParent;
  483. }
  484. else
  485. {
  486. if( pX== pX->m_pParent->m_pRight)
  487. {
  488. pX= pX->m_pParent;
  489. RotateLeft( pX);
  490. }
  491. pX->m_pParent->m_bRed= false;
  492. pX->m_pParent->m_pParent->m_bRed= true;
  493. RotateRight( pX->m_pParent->m_pParent);
  494. }
  495. }
  496. else
  497. {
  498. pY= pX->m_pParent->m_pParent->m_pLeft;
  499. if( pY!= NULL&& pY->m_bRed)
  500. {
  501. pX->m_pParent->m_bRed= false;
  502. pY->m_bRed= false;
  503. pX->m_pParent->m_pParent->m_bRed= true;
  504. pX= pX->m_pParent->m_pParent;
  505. }
  506. else
  507. {
  508. if( pX== pX->m_pParent->m_pLeft)
  509. {
  510. pX= pX->m_pParent;
  511. RotateRight( pX);
  512. }
  513. pX->m_pParent->m_bRed= false;
  514. pX->m_pParent->m_pParent->m_bRed= true;
  515. RotateLeft( pX->m_pParent->m_pParent);
  516. }
  517. }
  518. }
  519. m_pHead->m_pParent->m_bRed= false;
  520. return iterator( pZ);
  521. }
  522. public: // Functions
  523. iterator begin()
  524. { return iterator( m_pHead->m_pLeft); }
  525. const_iterator begin() const
  526. { return const_iterator( m_pHead->m_pLeft); }
  527. iterator end()
  528. { return iterator( m_pHead); }
  529. const_iterator end() const
  530. { return const_iterator( m_pHead); }
  531. reverse_iterator rbegin()
  532. { return reverse_iterator( m_pHead->m_pRight); }
  533. const_reverse_iterator rbegin() const
  534. { return const_reverse_iterator( m_pHead->m_pRight); }
  535. reverse_iterator rend()
  536. { return reverse_iterator( m_pHead); }
  537. const_reverse_iterator rend() const
  538. { return const_reverse_iterator( m_pHead); }
  539. size_type size() const
  540. { return m_uiNodes; }
  541. size_type max_size() const
  542. { return m_allocator.max_size(); }
  543. bool empty() const
  544. { return 0== size(); }
  545. key_compare key_comp() const
  546. { return m_key_compare; }
  547. explicit RBTree( const CompareKey& CmpKey= CompareKey(), const ExtractKey&
  548. ExKey= ExtractKey(), const Allocator& A= Allocator()): m_pHead( NULL),
  549. m_uiNodes( 0), m_key_compare( CmpKey), m_key_extract( ExKey),
  550. m_allocator( A)
  551. {
  552. BuildHeadNode();
  553. }
  554. RBTree( const tree_type& Other): m_pHead( NULL), m_uiNodes( 0),
  555. m_key_compare( Other.m_key_compare), m_key_extract( Other.m_key_extract),
  556. m_allocator( Other.m_allocator)
  557. {
  558. BuildHeadNode();
  559. try {
  560. *this= Other;
  561. } catch( ... ) {
  562. clear();
  563. DestroyHeadNode();
  564. throw;
  565. }
  566. }
  567. ~RBTree()
  568. {
  569. clear();
  570. DestroyHeadNode();
  571. }
  572. tree_type& operator=( const tree_type& x)
  573. {
  574. if( this!= &x)
  575. {
  576. erase( begin(), end());
  577. m_key_compare= x.m_key_compare;
  578. m_key_extract= x.m_key_extract;
  579. insert_equal( x.begin(), x.end());
  580. }
  581. return (*this);
  582. }
  583. allocator_type get_allocator() const
  584. { return m_allocator; }
  585. void swap( const tree_type& x)
  586. {
  587. swap( m_key_compare, x.m_key_compare);
  588. swap( m_key_extract, x.m_key_extract);
  589. if( m_allocator== x.m_allocator)
  590. {
  591. swap( m_pHead, x.m_pHead);
  592. swap( m_uiNodes, x.m_uiNodes);
  593. }
  594. else
  595. {
  596. tree_type Temp( *this);
  597. *this= x;
  598. x= Temp;
  599. }
  600. }
  601. //
  602. // Global ::swap was always called instead of this method. Now that we always instantiate it,
  603. // as required by the Standard, it confilcts with the global definition
  604. //
  605. // An overload (see the end of this file) is now called instead of this friend function
  606. //
  607. // friend void swap( tree_type& x, tree_type& y)
  608. // { x.swap( y); }
  609. pair< iterator, bool> insert_unique( const value_type& x)
  610. {
  611. tree_node_pointer pA( m_pHead->m_pParent);
  612. tree_node_pointer pB( m_pHead);
  613. const key_type XKey( m_key_extract( x));
  614. bool bCmp( true);
  615. while( pA!= NULL)
  616. {
  617. pB= pA;
  618. bCmp= m_key_compare( XKey, m_key_extract( pA->m_Value));
  619. pA= (bCmp? pA->m_pLeft: pA->m_pRight);
  620. }
  621. iterator itP( pB);
  622. if( bCmp)
  623. {
  624. if( itP== begin())
  625. return pair< iterator, bool>( Insert( pA, pB, x), true);
  626. else
  627. --itP;
  628. }
  629. if( m_key_compare( m_key_extract( itP.m_pNode->m_Value), XKey))
  630. return pair< iterator, bool>( Insert( pA, pB, x), true);
  631. return pair< iterator, bool>( itP, false);
  632. }
  633. iterator insert_unique( iterator pP, const value_type& x)
  634. {
  635. return insert_unique( x).first;
  636. }
  637. template< class ForwardIterator>
  638. void insert_unique( ForwardIterator f, ForwardIterator l)
  639. {
  640. while( f!= l)
  641. {
  642. insert_unique( *f);
  643. ++f;
  644. }
  645. }
  646. iterator insert_equal( const value_type& x)
  647. {
  648. tree_node_pointer pA( m_pHead->m_pParent);
  649. tree_node_pointer pB( m_pHead);
  650. const key_type XKey( m_key_extract( x));
  651. bool bCmp( true);
  652. while( pA!= NULL)
  653. {
  654. pB= pA;
  655. bCmp= m_key_compare( XKey, m_key_extract( pA->m_Value));
  656. pA= (bCmp? pA->m_pLeft: pA->m_pRight);
  657. }
  658. return iterator( Insert( pA, pB, x));
  659. }
  660. iterator insert_equal( iterator pP, const value_type& x)
  661. {
  662. return insert_equal( x);
  663. }
  664. template< class ForwardIterator>
  665. void insert_equal( ForwardIterator f, ForwardIterator l)
  666. {
  667. while( f!= l)
  668. {
  669. insert_equal( *f);
  670. ++f;
  671. }
  672. }
  673. iterator erase( iterator itDel)
  674. {
  675. tree_node_pointer pW;
  676. tree_node_pointer pX;
  677. tree_node_pointer pY( itDel.m_pNode);
  678. tree_node_pointer pZ( pY);
  679. ++itDel;
  680. if( pY->m_pLeft== NULL)
  681. pX= pY->m_pRight;
  682. else if( pY->m_pRight== NULL)
  683. pX= pY->m_pLeft;
  684. else
  685. {
  686. pY= pY->m_pRight;
  687. while( pY->m_pLeft!= NULL)
  688. pY= pY->m_pLeft;
  689. pX= pY->m_pRight;
  690. }
  691. tree_node_pointer pXParent( pY);
  692. if( pY!= pZ)
  693. {
  694. pZ->m_pLeft->m_pParent= pY;
  695. pY->m_pLeft= pZ->m_pLeft;
  696. if( pY== pZ->m_pRight)
  697. (pX!= NULL? pX->m_pParent= pXParent= pY: pXParent= pY);
  698. else
  699. {
  700. (pX!= NULL? pX->m_pParent= pXParent= pY->m_pParent: pXParent= pY->m_pParent);
  701. pY->m_pParent->m_pLeft= pX;
  702. pY->m_pRight= pZ->m_pRight;
  703. pZ->m_pRight->m_pParent= pY;
  704. }
  705. if( m_pHead->m_pParent== pZ)
  706. m_pHead->m_pParent= pY;
  707. else if( pZ->m_pParent->m_pLeft== pZ)
  708. pZ->m_pParent->m_pLeft= pY;
  709. else
  710. pZ->m_pParent->m_pRight= pY;
  711. pY->m_pParent= pZ->m_pParent;
  712. const bool bTmp( pY->m_bRed);
  713. pY->m_bRed= pZ->m_bRed;
  714. pZ->m_bRed= bTmp;
  715. pY= pZ;
  716. }
  717. else
  718. {
  719. (pX!= NULL? pX->m_pParent= pXParent= pY->m_pParent: pXParent= pY->m_pParent);
  720. if( m_pHead->m_pParent== pZ)
  721. m_pHead->m_pParent= pX;
  722. else if( pZ->m_pParent->m_pLeft== pZ)
  723. pZ->m_pParent->m_pLeft= pX;
  724. else
  725. pZ->m_pParent->m_pRight= pX;
  726. if( m_pHead->m_pLeft== pZ)
  727. {
  728. if( pZ->m_pRight== NULL)
  729. m_pHead->m_pLeft= pZ->m_pParent;
  730. else
  731. {
  732. m_pHead->m_pLeft= pX;
  733. while( m_pHead->m_pLeft->m_pLeft!= NULL)
  734. m_pHead->m_pLeft= m_pHead->m_pLeft->m_pLeft;
  735. }
  736. }
  737. if( m_pHead->m_pRight== pZ)
  738. {
  739. if( pZ->m_pLeft== NULL)
  740. m_pHead->m_pRight= pZ->m_pParent;
  741. else
  742. {
  743. m_pHead->m_pRight= pX;
  744. while( m_pHead->m_pRight->m_pRight!= NULL)
  745. m_pHead->m_pRight= m_pHead->m_pRight->m_pRight;
  746. }
  747. }
  748. }
  749. if( !pY->m_bRed)
  750. {
  751. while( pX!= m_pHead->m_pParent&& (pX== NULL|| !pX->m_bRed))
  752. {
  753. if( pX== pXParent->m_pLeft)
  754. {
  755. pW= pXParent->m_pRight;
  756. if( pW->m_bRed)
  757. {
  758. pW->m_bRed= false;
  759. pXParent->m_bRed= true;
  760. RotateLeft( pXParent);
  761. pW= pXParent->m_pRight;
  762. }
  763. if( (pW->m_pLeft== NULL|| !pW->m_pLeft->m_bRed)&&
  764. (pW->m_pRight== NULL|| !pW->m_pRight->m_bRed) )
  765. {
  766. pW->m_bRed= true;
  767. pX= pXParent;
  768. pXParent= pXParent->m_pParent;
  769. }
  770. else
  771. {
  772. if( pW->m_pRight== NULL|| !pW->m_pRight->m_bRed)
  773. {
  774. pW->m_pLeft->m_bRed= false;
  775. pW->m_bRed= true;
  776. RotateRight( pW);
  777. pW= pXParent->m_pRight;
  778. }
  779. pW->m_bRed= pXParent->m_bRed;
  780. pXParent->m_bRed= false;
  781. pW->m_pRight->m_bRed= false;
  782. RotateLeft( pXParent);
  783. break;
  784. }
  785. }
  786. else
  787. {
  788. pW= pXParent->m_pLeft;
  789. if( pW->m_bRed)
  790. {
  791. pW->m_bRed= false;
  792. pXParent->m_bRed= true;
  793. RotateRight( pXParent);
  794. pW= pXParent->m_pLeft;
  795. }
  796. if( (pW->m_pLeft== NULL|| !pW->m_pLeft->m_bRed)&&
  797. (pW->m_pRight== NULL|| !pW->m_pRight->m_bRed) )
  798. {
  799. pW->m_bRed= true;
  800. pX= pXParent;
  801. pXParent= pXParent->m_pParent;
  802. }
  803. else
  804. {
  805. if( pW->m_pLeft== NULL|| !pW->m_pLeft->m_bRed)
  806. {
  807. pW->m_pRight->m_bRed= false;
  808. pW->m_bRed= true;
  809. RotateLeft( pW);
  810. pW= pXParent->m_pLeft;
  811. }
  812. pW->m_bRed= pXParent->m_bRed;
  813. pXParent->m_bRed= false;
  814. pW->m_pLeft->m_bRed= false;
  815. RotateRight( pXParent);
  816. break;
  817. }
  818. }
  819. }
  820. if( pX!= NULL)
  821. pX->m_bRed= false;
  822. }
  823. m_allocator.destroy( pY);
  824. m_allocator.deallocate( pY, 1);
  825. --m_uiNodes;
  826. return itDel;
  827. }
  828. iterator erase( iterator f, iterator l)
  829. {
  830. if( 0== size()|| f!= begin() || l!= end())
  831. {
  832. while( f!= l)
  833. f= erase( f);
  834. return f;
  835. }
  836. clear();
  837. return begin();
  838. }
  839. size_type erase( const key_type& k)
  840. {
  841. const pair< iterator, iterator> EqRng( equal_range( k));
  842. size_type n( 0);
  843. iterator itCur( EqRng.first);
  844. while( itCur!= EqRng.second)
  845. {
  846. ++itCur;
  847. ++n;
  848. }
  849. erase( EqRng.first, EqRng.second);
  850. return n;
  851. }
  852. void clear()
  853. {
  854. RecDelete( m_pHead->m_pParent);
  855. m_uiNodes= 0;
  856. m_pHead->m_pParent= NULL;
  857. m_pHead->m_pRight= m_pHead->m_pLeft= m_pHead;
  858. }
  859. iterator find( const key_type& k)
  860. {
  861. iterator itLB( lower_bound( k));
  862. return( itLB== end()|| m_key_compare( k, m_key_extract(
  863. itLB.m_pNode->m_Value))? end(): itLB);
  864. }
  865. const_iterator find( const key_type& k) const
  866. {
  867. const_iterator itLB( lower_bound( k));
  868. return( itLB== end()|| m_key_compare( k, m_key_extract(
  869. itLB.m_pNode->m_Value))? end(): itLB);
  870. }
  871. size_type count( const key_type& k) const
  872. {
  873. const pair< const_iterator, const_iterator> EqRng( equal_range( k));
  874. size_type n( 0);
  875. const_iterator itCur( EqRng.first);
  876. while( itCur!= EqRng.second)
  877. {
  878. ++itCur;
  879. ++n;
  880. }
  881. return n;
  882. }
  883. iterator lower_bound( const key_type& k)
  884. { return iterator( lowerbound( k)); }
  885. const_iterator lower_bound( const key_type& k) const
  886. { return const_iterator( lowerbound( k)); }
  887. iterator upper_bound( const key_type& k)
  888. { return iterator( upperbound( k)); }
  889. const_iterator upper_bound( const key_type& k) const
  890. { return const_iterator( upperbound( k)); }
  891. pair< iterator, iterator> equal_range( const key_type& k)
  892. { return pair< iterator, iterator>( lower_bound( k), upper_bound( k)); }
  893. pair< const_iterator, const_iterator> equal_range( const key_type& k) const
  894. { return pair< const_iterator, const_iterator>( lower_bound( k), upper_bound( k)); }
  895. #if 0
  896. pair< bool, int> InValid( tree_node_pointer pNode, tree_node_pointer pParent, bool bParentRed)
  897. {
  898. pair< bool, int> RightRet( false, 1);
  899. if( pNode->m_pRight!= NULL)
  900. RightRet= InValid( pNode->m_pRight, pNode, pNode->m_bRed);
  901. pair< bool, int> LeftRet( false, 1);
  902. if( pNode->m_pLeft!= NULL)
  903. LeftRet= InValid( pNode->m_pLeft, pNode, pNode->m_bRed);
  904. return pair< bool, int>( pNode->m_pParent!= pParent|| RightRet.first|| LeftRet.first||
  905. RightRet.second!= LeftRet.second|| (bParentRed&& pNode->m_bRed),
  906. RightRet.second+ (pNode->m_bRed? 0: 1));
  907. }
  908. bool valid()
  909. {
  910. if( m_pHead->m_pParent!= NULL)
  911. {
  912. bool bInvalid( InValid( m_pHead->m_pParent, m_pHead, m_pHead->m_bRed).first);
  913. tree_node_pointer pChk= m_pHead->m_pParent;
  914. while( pChk->m_pLeft!= NULL)
  915. pChk= pChk->m_pLeft;
  916. bInvalid|= (pChk!= m_pHead->m_pLeft);
  917. pChk= m_pHead->m_pParent;
  918. while( pChk->m_pRight!= NULL)
  919. pChk= pChk->m_pRight;
  920. bInvalid|= (pChk!= m_pHead->m_pRight);
  921. return !bInvalid;
  922. }
  923. else
  924. return true;
  925. }
  926. #endif
  927. };
  928. template< class Key, class T, class ExtractKey, class CompareKey, class Allocator>
  929. void swap(RBTree<Key,T,ExtractKey,CompareKey,Allocator>& x, RBTree<Key,T,ExtractKey,CompareKey, Allocator>& y)
  930. {
  931. x.swap(y);
  932. }