Source code of Windows XP (NT5)
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.

953 lines
30 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_pHead->m_bRed.~bool();
  373. m_allocator.deallocate( m_pHead, 1);
  374. }
  375. void RotateLeft( tree_node_pointer pX)
  376. {
  377. tree_node_pointer pY( pX->m_pRight);
  378. pX->m_pRight= pY->m_pLeft;
  379. if( pY->m_pLeft!= NULL)
  380. pY->m_pLeft->m_pParent= pX;
  381. pY->m_pParent= pX->m_pParent;
  382. if( pX== m_pHead->m_pParent)
  383. m_pHead->m_pParent= pY;
  384. else if( pX== pX->m_pParent->m_pLeft)
  385. pX->m_pParent->m_pLeft= pY;
  386. else
  387. pX->m_pParent->m_pRight= pY;
  388. pY->m_pLeft= pX;
  389. pX->m_pParent= pY;
  390. }
  391. void RotateRight( tree_node_pointer pX)
  392. {
  393. tree_node_pointer pY( pX->m_pLeft);
  394. pX->m_pLeft= pY->m_pRight;
  395. if( pY->m_pRight!= NULL)
  396. pY->m_pRight->m_pParent= pX;
  397. pY->m_pParent= pX->m_pParent;
  398. if( pX== m_pHead->m_pParent)
  399. m_pHead->m_pParent= pY;
  400. else if( pX== pX->m_pParent->m_pRight)
  401. pX->m_pParent->m_pRight= pY;
  402. else
  403. pX->m_pParent->m_pLeft= pY;
  404. pY->m_pRight= pX;
  405. pX->m_pParent= pY;
  406. }
  407. void RecDelete( tree_node_pointer pX)
  408. {
  409. for( tree_node_pointer pY( pX); pY!= NULL; pX= pY)
  410. {
  411. RecDelete( pY->m_pRight);
  412. pY= pY->m_pLeft;
  413. m_allocator.destroy( pX);
  414. m_allocator.deallocate( pX, 1);
  415. }
  416. }
  417. tree_node_pointer lowerbound( const key_type& k) const
  418. {
  419. tree_node_pointer pX( m_pHead->m_pParent);
  420. tree_node_pointer pY( m_pHead);
  421. while( pX!= NULL)
  422. {
  423. if( m_key_compare( m_key_extract( pX->m_Value), k))
  424. pX= pX->m_pRight;
  425. else
  426. {
  427. pY= pX;
  428. pX= pX->m_pLeft;
  429. }
  430. }
  431. return pY;
  432. }
  433. tree_node_pointer upperbound( const key_type& k) const
  434. {
  435. tree_node_pointer pX( m_pHead->m_pParent);
  436. tree_node_pointer pY( m_pHead);
  437. while( pX!= NULL)
  438. {
  439. if( m_key_compare( k, m_key_extract( pX->m_Value)))
  440. {
  441. pY= pX;
  442. pX= pX->m_pLeft;
  443. }
  444. else
  445. pX= pX->m_pRight;
  446. }
  447. return pY;
  448. }
  449. iterator Insert( tree_node_pointer pX,
  450. tree_node_pointer pY, const value_type& V)
  451. {
  452. tree_node_pointer pZ( m_allocator.allocate( 1));
  453. m_allocator.construct( pZ, tree_node( pY, V, true));
  454. ++m_uiNodes;
  455. if( pY== m_pHead|| pX!= NULL||
  456. m_key_compare( m_key_extract( V), m_key_extract( pY->m_Value)))
  457. {
  458. pY->m_pLeft= pZ;
  459. if( pY== m_pHead)
  460. {
  461. m_pHead->m_pParent= pZ;
  462. m_pHead->m_pRight= pZ;
  463. }
  464. else if( pY== m_pHead->m_pLeft)
  465. m_pHead->m_pLeft= pZ;
  466. }
  467. else
  468. {
  469. pY->m_pRight= pZ;
  470. if( pY== m_pHead->m_pRight)
  471. m_pHead->m_pRight= pZ;
  472. }
  473. for( pX= pZ; pX!= m_pHead->m_pParent&& pX->m_pParent->m_bRed; )
  474. {
  475. if( pX->m_pParent== pX->m_pParent->m_pParent->m_pLeft)
  476. {
  477. pY= pX->m_pParent->m_pParent->m_pRight;
  478. if( pY!= NULL&& pY->m_bRed)
  479. {
  480. pX->m_pParent->m_bRed= false;
  481. pY->m_bRed= false;
  482. pX->m_pParent->m_pParent->m_bRed= true;
  483. pX= pX->m_pParent->m_pParent;
  484. }
  485. else
  486. {
  487. if( pX== pX->m_pParent->m_pRight)
  488. {
  489. pX= pX->m_pParent;
  490. RotateLeft( pX);
  491. }
  492. pX->m_pParent->m_bRed= false;
  493. pX->m_pParent->m_pParent->m_bRed= true;
  494. RotateRight( pX->m_pParent->m_pParent);
  495. }
  496. }
  497. else
  498. {
  499. pY= pX->m_pParent->m_pParent->m_pLeft;
  500. if( pY!= NULL&& pY->m_bRed)
  501. {
  502. pX->m_pParent->m_bRed= false;
  503. pY->m_bRed= false;
  504. pX->m_pParent->m_pParent->m_bRed= true;
  505. pX= pX->m_pParent->m_pParent;
  506. }
  507. else
  508. {
  509. if( pX== pX->m_pParent->m_pLeft)
  510. {
  511. pX= pX->m_pParent;
  512. RotateRight( pX);
  513. }
  514. pX->m_pParent->m_bRed= false;
  515. pX->m_pParent->m_pParent->m_bRed= true;
  516. RotateLeft( pX->m_pParent->m_pParent);
  517. }
  518. }
  519. }
  520. m_pHead->m_pParent->m_bRed= false;
  521. return iterator( pZ);
  522. }
  523. public: // Functions
  524. iterator begin()
  525. { return iterator( m_pHead->m_pLeft); }
  526. const_iterator begin() const
  527. { return const_iterator( m_pHead->m_pLeft); }
  528. iterator end()
  529. { return iterator( m_pHead); }
  530. const_iterator end() const
  531. { return const_iterator( m_pHead); }
  532. reverse_iterator rbegin()
  533. { return reverse_iterator( m_pHead->m_pRight); }
  534. const_reverse_iterator rbegin() const
  535. { return const_reverse_iterator( m_pHead->m_pRight); }
  536. reverse_iterator rend()
  537. { return reverse_iterator( m_pHead); }
  538. const_reverse_iterator rend() const
  539. { return const_reverse_iterator( m_pHead); }
  540. size_type size() const
  541. { return m_uiNodes; }
  542. size_type max_size() const
  543. { return m_allocator.max_size(); }
  544. bool empty() const
  545. { return 0== size(); }
  546. key_compare key_comp() const
  547. { return m_key_compare; }
  548. explicit RBTree( const CompareKey& CmpKey= CompareKey(), const ExtractKey&
  549. ExKey= ExtractKey(), const Allocator& A= Allocator()): m_pHead( NULL),
  550. m_uiNodes( 0), m_key_compare( CmpKey), m_key_extract( ExKey),
  551. m_allocator( A)
  552. {
  553. BuildHeadNode();
  554. }
  555. RBTree( const tree_type& Other): m_pHead( NULL), m_uiNodes( 0),
  556. m_key_compare( Other.m_key_compare), m_key_extract( Other.m_key_extract),
  557. m_allocator( Other.m_allocator)
  558. {
  559. BuildHeadNode();
  560. try {
  561. *this= Other;
  562. } catch( ... ) {
  563. clear();
  564. DestroyHeadNode();
  565. throw;
  566. }
  567. }
  568. ~RBTree()
  569. {
  570. clear();
  571. DestroyHeadNode();
  572. }
  573. tree_type& operator=( const tree_type& x)
  574. {
  575. if( this!= &x)
  576. {
  577. erase( begin(), end());
  578. m_key_compare= x.m_key_compare;
  579. m_key_extract= x.m_key_extract;
  580. insert_equal( x.begin(), x.end());
  581. }
  582. return (*this);
  583. }
  584. allocator_type get_allocator() const
  585. { return m_allocator; }
  586. void swap( const tree_type& x)
  587. {
  588. swap( m_key_compare, x.m_key_compare);
  589. swap( m_key_extract, x.m_key_extract);
  590. if( m_allocator== x.m_allocator)
  591. {
  592. swap( m_pHead, x.m_pHead);
  593. swap( m_uiNodes, x.m_uiNodes);
  594. }
  595. else
  596. {
  597. tree_type Temp( *this);
  598. *this= x;
  599. x= Temp;
  600. }
  601. }
  602. friend void swap( tree_type& x, tree_type& y)
  603. { x.swap( y); }
  604. pair< iterator, bool> insert_unique( const value_type& x)
  605. {
  606. tree_node_pointer pA( m_pHead->m_pParent);
  607. tree_node_pointer pB( m_pHead);
  608. const key_type XKey( m_key_extract( x));
  609. bool bCmp( true);
  610. while( pA!= NULL)
  611. {
  612. pB= pA;
  613. bCmp= m_key_compare( XKey, m_key_extract( pA->m_Value));
  614. pA= (bCmp? pA->m_pLeft: pA->m_pRight);
  615. }
  616. iterator itP( pB);
  617. if( bCmp)
  618. {
  619. if( itP== begin())
  620. return pair< iterator, bool>( Insert( pA, pB, x), true);
  621. else
  622. --itP;
  623. }
  624. if( m_key_compare( m_key_extract( itP.m_pNode->m_Value), XKey))
  625. return pair< iterator, bool>( Insert( pA, pB, x), true);
  626. return pair< iterator, bool>( itP, false);
  627. }
  628. iterator insert_unique( iterator pP, const value_type& x)
  629. {
  630. return insert_unique( x).first;
  631. }
  632. template< class ForwardIterator>
  633. void insert_unique( ForwardIterator f, ForwardIterator l)
  634. {
  635. while( f!= l)
  636. {
  637. insert_unique( *f);
  638. ++f;
  639. }
  640. }
  641. iterator insert_equal( const value_type& x)
  642. {
  643. tree_node_pointer pA( m_pHead->m_pParent);
  644. tree_node_pointer pB( m_pHead);
  645. const key_type XKey( m_key_extract( x));
  646. bool bCmp( true);
  647. while( pA!= NULL)
  648. {
  649. pB= pA;
  650. bCmp= m_key_compare( XKey, m_key_extract( pA->m_Value));
  651. pA= (bCmp? pA->m_pLeft: pA->m_pRight);
  652. }
  653. return iterator( Insert( pA, pB, x));
  654. }
  655. iterator insert_equal( iterator pP, const value_type& x)
  656. {
  657. return insert_equal( x);
  658. }
  659. template< class ForwardIterator>
  660. void insert_equal( ForwardIterator f, ForwardIterator l)
  661. {
  662. while( f!= l)
  663. {
  664. insert_equal( *f);
  665. ++f;
  666. }
  667. }
  668. iterator erase( iterator itDel)
  669. {
  670. tree_node_pointer pW;
  671. tree_node_pointer pX;
  672. tree_node_pointer pY( itDel.m_pNode);
  673. tree_node_pointer pZ( pY);
  674. ++itDel;
  675. if( pY->m_pLeft== NULL)
  676. pX= pY->m_pRight;
  677. else if( pY->m_pRight== NULL)
  678. pX= pY->m_pLeft;
  679. else
  680. {
  681. pY= pY->m_pRight;
  682. while( pY->m_pLeft!= NULL)
  683. pY= pY->m_pLeft;
  684. pX= pY->m_pRight;
  685. }
  686. tree_node_pointer pXParent( pY);
  687. if( pY!= pZ)
  688. {
  689. pZ->m_pLeft->m_pParent= pY;
  690. pY->m_pLeft= pZ->m_pLeft;
  691. if( pY== pZ->m_pRight)
  692. (pX!= NULL? pX->m_pParent= pXParent= pY: pXParent= pY);
  693. else
  694. {
  695. (pX!= NULL? pX->m_pParent= pXParent= pY->m_pParent: pXParent= pY->m_pParent);
  696. pY->m_pParent->m_pLeft= pX;
  697. pY->m_pRight= pZ->m_pRight;
  698. pZ->m_pRight->m_pParent= pY;
  699. }
  700. if( m_pHead->m_pParent== pZ)
  701. m_pHead->m_pParent= pY;
  702. else if( pZ->m_pParent->m_pLeft== pZ)
  703. pZ->m_pParent->m_pLeft= pY;
  704. else
  705. pZ->m_pParent->m_pRight= pY;
  706. pY->m_pParent= pZ->m_pParent;
  707. const bool bTmp( pY->m_bRed);
  708. pY->m_bRed= pZ->m_bRed;
  709. pZ->m_bRed= bTmp;
  710. pY= pZ;
  711. }
  712. else
  713. {
  714. (pX!= NULL? pX->m_pParent= pXParent= pY->m_pParent: pXParent= pY->m_pParent);
  715. if( m_pHead->m_pParent== pZ)
  716. m_pHead->m_pParent= pX;
  717. else if( pZ->m_pParent->m_pLeft== pZ)
  718. pZ->m_pParent->m_pLeft= pX;
  719. else
  720. pZ->m_pParent->m_pRight= pX;
  721. if( m_pHead->m_pLeft== pZ)
  722. {
  723. if( pZ->m_pRight== NULL)
  724. m_pHead->m_pLeft= pZ->m_pParent;
  725. else
  726. {
  727. m_pHead->m_pLeft= pX;
  728. while( m_pHead->m_pLeft->m_pLeft!= NULL)
  729. m_pHead->m_pLeft= m_pHead->m_pLeft->m_pLeft;
  730. }
  731. }
  732. if( m_pHead->m_pRight== pZ)
  733. {
  734. if( pZ->m_pLeft== NULL)
  735. m_pHead->m_pRight= pZ->m_pParent;
  736. else
  737. {
  738. m_pHead->m_pRight= pX;
  739. while( m_pHead->m_pRight->m_pRight!= NULL)
  740. m_pHead->m_pRight= m_pHead->m_pRight->m_pRight;
  741. }
  742. }
  743. }
  744. if( !pY->m_bRed)
  745. {
  746. while( pX!= m_pHead->m_pParent&& (pX== NULL|| !pX->m_bRed))
  747. {
  748. if( pX== pXParent->m_pLeft)
  749. {
  750. pW= pXParent->m_pRight;
  751. if( pW->m_bRed)
  752. {
  753. pW->m_bRed= false;
  754. pXParent->m_bRed= true;
  755. RotateLeft( pXParent);
  756. pW= pXParent->m_pRight;
  757. }
  758. if( (pW->m_pLeft== NULL|| !pW->m_pLeft->m_bRed)&&
  759. (pW->m_pRight== NULL|| !pW->m_pRight->m_bRed) )
  760. {
  761. pW->m_bRed= true;
  762. pX= pXParent;
  763. pXParent= pXParent->m_pParent;
  764. }
  765. else
  766. {
  767. if( pW->m_pRight== NULL|| !pW->m_pRight->m_bRed)
  768. {
  769. pW->m_pLeft->m_bRed= false;
  770. pW->m_bRed= true;
  771. RotateRight( pW);
  772. pW= pXParent->m_pRight;
  773. }
  774. pW->m_bRed= pXParent->m_bRed;
  775. pXParent->m_bRed= false;
  776. pW->m_pRight->m_bRed= false;
  777. RotateLeft( pXParent);
  778. break;
  779. }
  780. }
  781. else
  782. {
  783. pW= pXParent->m_pLeft;
  784. if( pW->m_bRed)
  785. {
  786. pW->m_bRed= false;
  787. pXParent->m_bRed= true;
  788. RotateRight( pXParent);
  789. pW= pXParent->m_pLeft;
  790. }
  791. if( (pW->m_pLeft== NULL|| !pW->m_pLeft->m_bRed)&&
  792. (pW->m_pRight== NULL|| !pW->m_pRight->m_bRed) )
  793. {
  794. pW->m_bRed= true;
  795. pX= pXParent;
  796. pXParent= pXParent->m_pParent;
  797. }
  798. else
  799. {
  800. if( pW->m_pLeft== NULL|| !pW->m_pLeft->m_bRed)
  801. {
  802. pW->m_pRight->m_bRed= false;
  803. pW->m_bRed= true;
  804. RotateLeft( pW);
  805. pW= pXParent->m_pLeft;
  806. }
  807. pW->m_bRed= pXParent->m_bRed;
  808. pXParent->m_bRed= false;
  809. pW->m_pLeft->m_bRed= false;
  810. RotateRight( pXParent);
  811. break;
  812. }
  813. }
  814. }
  815. if( pX!= NULL)
  816. pX->m_bRed= false;
  817. }
  818. m_allocator.destroy( pY);
  819. m_allocator.deallocate( pY, 1);
  820. --m_uiNodes;
  821. return itDel;
  822. }
  823. iterator erase( iterator f, iterator l)
  824. {
  825. if( 0== size()|| f!= begin() || l!= end())
  826. {
  827. while( f!= l)
  828. f= erase( f);
  829. return f;
  830. }
  831. clear();
  832. return begin();
  833. }
  834. size_type erase( const key_type& k)
  835. {
  836. const pair< iterator, iterator> EqRng( equal_range( k));
  837. size_type n( 0);
  838. iterator itCur( EqRng.first);
  839. while( itCur!= EqRng.second)
  840. {
  841. ++itCur;
  842. ++n;
  843. }
  844. erase( EqRng.first, EqRng.second);
  845. return n;
  846. }
  847. void clear()
  848. {
  849. RecDelete( m_pHead->m_pParent);
  850. m_uiNodes= 0;
  851. m_pHead->m_pParent= NULL;
  852. m_pHead->m_pRight= m_pHead->m_pLeft= m_pHead;
  853. }
  854. iterator find( const key_type& k)
  855. {
  856. iterator itLB( lower_bound( k));
  857. return( itLB== end()|| m_key_compare( k, m_key_extract(
  858. itLB.m_pNode->m_Value))? end(): itLB);
  859. }
  860. const_iterator find( const key_type& k) const
  861. {
  862. const_iterator itLB( lower_bound( k));
  863. return( itLB== end()|| m_key_compare( k, m_key_extract(
  864. itLB.m_pNode->m_Value))? end(): itLB);
  865. }
  866. size_type count( const key_type& k) const
  867. {
  868. const pair< const_iterator, const_iterator> EqRng( equal_range( k));
  869. size_type n( 0);
  870. const_iterator itCur( EqRng.first);
  871. while( itCur!= EqRng.second)
  872. {
  873. ++itCur;
  874. ++n;
  875. }
  876. return n;
  877. }
  878. iterator lower_bound( const key_type& k)
  879. { return iterator( lowerbound( k)); }
  880. const_iterator lower_bound( const key_type& k) const
  881. { return const_iterator( lowerbound( k)); }
  882. iterator upper_bound( const key_type& k)
  883. { return iterator( upperbound( k)); }
  884. const_iterator upper_bound( const key_type& k) const
  885. { return const_iterator( upperbound( k)); }
  886. pair< iterator, iterator> equal_range( const key_type& k)
  887. { return pair< iterator, iterator>( lower_bound( k), upper_bound( k)); }
  888. pair< const_iterator, const_iterator> equal_range( const key_type& k) const
  889. { return pair< const_iterator, const_iterator>( lower_bound( k), upper_bound( k)); }
  890. #if 0
  891. pair< bool, int> InValid( tree_node_pointer pNode, tree_node_pointer pParent, bool bParentRed)
  892. {
  893. pair< bool, int> RightRet( false, 1);
  894. if( pNode->m_pRight!= NULL)
  895. RightRet= InValid( pNode->m_pRight, pNode, pNode->m_bRed);
  896. pair< bool, int> LeftRet( false, 1);
  897. if( pNode->m_pLeft!= NULL)
  898. LeftRet= InValid( pNode->m_pLeft, pNode, pNode->m_bRed);
  899. return pair< bool, int>( pNode->m_pParent!= pParent|| RightRet.first|| LeftRet.first||
  900. RightRet.second!= LeftRet.second|| (bParentRed&& pNode->m_bRed),
  901. RightRet.second+ (pNode->m_bRed? 0: 1));
  902. }
  903. bool valid()
  904. {
  905. if( m_pHead->m_pParent!= NULL)
  906. {
  907. bool bInvalid( InValid( m_pHead->m_pParent, m_pHead, m_pHead->m_bRed).first);
  908. tree_node_pointer pChk= m_pHead->m_pParent;
  909. while( pChk->m_pLeft!= NULL)
  910. pChk= pChk->m_pLeft;
  911. bInvalid|= (pChk!= m_pHead->m_pLeft);
  912. pChk= m_pHead->m_pParent;
  913. while( pChk->m_pRight!= NULL)
  914. pChk= pChk->m_pRight;
  915. bInvalid|= (pChk!= m_pHead->m_pRight);
  916. return !bInvalid;
  917. }
  918. else
  919. return true;
  920. }
  921. #endif
  922. };