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.

1217 lines
34 KiB

  1. // xtree internal header
  2. #pragma once
  3. #ifndef _XTREE_
  4. #define _XTREE_
  5. #include <functional>
  6. #include <memory>
  7. #include <stdexcept>
  8. #pragma pack(push,8)
  9. #pragma warning(push,3)
  10. #pragma warning(disable:4127)
  11. _STD_BEGIN
  12. // TEMPLATE CLASS _Tree_nod
  13. template<class _Traits>
  14. class _Tree_nod
  15. : public _Traits // traits form ultimate base
  16. { // base class for _Tree_ptr to hold allocator _Alnod
  17. protected:
  18. typedef typename _Traits::allocator_type allocator_type;
  19. typedef typename _Traits::key_compare key_compare;
  20. typedef typename _Traits::value_type value_type;
  21. struct _Node;
  22. friend struct _Node;
  23. typedef typename allocator_type::_TEMPLATE_MEMBER
  24. rebind<_Node>::other::pointer _Genptr; // generic node pointer
  25. struct _Node
  26. { // tree node
  27. _Node(_Genptr _Larg, _Genptr _Parg, _Genptr _Rarg,
  28. const value_type& _Val, char _Carg)
  29. : _Left(_Larg), _Parent(_Parg), _Right(_Rarg),
  30. _Myval(_Val), _Color(_Carg), _Isnil(false)
  31. { // construct a node with value
  32. }
  33. _Genptr _Left; // left subtree, or smallest element if head
  34. _Genptr _Parent; // parent, or root of tree if head
  35. _Genptr _Right; // right subtree, or largest element if head
  36. value_type _Myval; // the stored value, unused if head
  37. char _Color; // _Red or _Black, _Black if head
  38. char _Isnil; // true only if head (also nil) node
  39. };
  40. _Tree_nod(const key_compare& _Parg,
  41. allocator_type _Al)
  42. : _Traits(_Parg), _Alnod(_Al)
  43. { // construct traits from _Parg and allocator from _Al
  44. }
  45. typename allocator_type::_TEMPLATE_MEMBER rebind<_Node>::other
  46. _Alnod; // allocator object for nodes
  47. };
  48. // TEMPLATE CLASS _Tree_ptr
  49. template<class _Traits>
  50. class _Tree_ptr
  51. : public _Tree_nod<_Traits>
  52. { // base class for _Tree_val to hold allocator _Alptr
  53. protected:
  54. typedef typename _Tree_nod<_Traits>::_Node _Node;
  55. typedef typename _Traits::allocator_type allocator_type;
  56. typedef typename _Traits::key_compare key_compare;
  57. typedef typename allocator_type::_TEMPLATE_MEMBER
  58. rebind<_Node>::other::pointer _Nodeptr;
  59. _Tree_ptr(const key_compare& _Parg,
  60. allocator_type _Al)
  61. : _Tree_nod<_Traits>(_Parg, _Al), _Alptr(_Al)
  62. { // construct base, and allocator from _Al
  63. }
  64. typename allocator_type::_TEMPLATE_MEMBER rebind<_Nodeptr>::other
  65. _Alptr; // allocator object for pointers to nodes
  66. };
  67. // TEMPLATE CLASS _Tree_val
  68. template<class _Traits>
  69. class _Tree_val
  70. : public _Tree_ptr<_Traits>
  71. { // base class for _Tree to hold allocator _Alval
  72. protected:
  73. typedef typename _Traits::allocator_type allocator_type;
  74. typedef typename _Traits::key_compare key_compare;
  75. _Tree_val(const key_compare& _Parg,
  76. allocator_type _Al)
  77. : _Tree_ptr<_Traits>(_Parg, _Al), _Alval(_Al)
  78. { // construct base, and allocator from _Al
  79. }
  80. allocator_type _Alval; // allocator object for values stored in nodes
  81. };
  82. // TEMPLATE CLASS _Tree
  83. template<class _Traits>
  84. class _Tree
  85. : public _Tree_val<_Traits>
  86. { // ordered red-black tree for [multi_]{map set}
  87. public:
  88. typedef _Tree<_Traits> _Myt;
  89. typedef _Tree_val<_Traits> _Mybase;
  90. typedef typename _Traits::key_type key_type;
  91. typedef typename _Traits::key_compare key_compare;
  92. typedef typename _Traits::value_compare value_compare;
  93. typedef typename _Traits::value_type value_type;
  94. typedef typename _Traits::allocator_type allocator_type;
  95. typedef typename _Traits::_ITptr _ITptr;
  96. typedef typename _Traits::_IReft _IReft;
  97. protected:
  98. typedef typename _Tree_nod<_Traits>::_Genptr _Genptr;
  99. typedef typename _Tree_nod<_Traits>::_Node _Node;
  100. enum _Redbl
  101. { // colors for link to parent
  102. _Red, _Black};
  103. typedef _POINTER_X(_Node, allocator_type) _Nodeptr;
  104. typedef _REFERENCE_X(_Nodeptr, allocator_type) _Nodepref;
  105. typedef _CREFERENCE_X(key_type, allocator_type) _Keyref;
  106. typedef _REFERENCE_X(char, allocator_type) _Charref;
  107. typedef _REFERENCE_X(value_type, allocator_type) _Vref;
  108. static _Charref _Color(_Nodeptr _Pnode)
  109. { // return reference to color in node
  110. return ((_Charref)(*_Pnode)._Color);
  111. }
  112. static _Charref _Isnil(_Nodeptr _Pnode)
  113. { // return reference to nil flag in node
  114. return ((_Charref)(*_Pnode)._Isnil);
  115. }
  116. static _Keyref _Key(_Nodeptr _Pnode)
  117. { // return reference to key in node
  118. return (_Mybase::_Kfn(_Myval(_Pnode)));
  119. }
  120. static _Nodepref _Left(_Nodeptr _Pnode)
  121. { // return reference to left pointer in node
  122. return ((_Nodepref)(*_Pnode)._Left);
  123. }
  124. static _Nodepref _Parent(_Nodeptr _Pnode)
  125. { // return reference to parent pointer in node
  126. return ((_Nodepref)(*_Pnode)._Parent);
  127. }
  128. static _Nodepref _Right(_Nodeptr _Pnode)
  129. { // return reference to right pointer in node
  130. return ((_Nodepref)(*_Pnode)._Right);
  131. }
  132. static _Vref _Myval(_Nodeptr _Pnode)
  133. { // return reference to value in node
  134. return ((_Vref)(*_Pnode)._Myval);
  135. }
  136. public:
  137. typedef typename allocator_type::size_type size_type;
  138. typedef typename allocator_type::difference_type _Dift;
  139. typedef _Dift difference_type;
  140. typedef _POINTER_X(value_type, allocator_type) _Tptr;
  141. typedef _CPOINTER_X(value_type, allocator_type) _Ctptr;
  142. typedef _REFERENCE_X(value_type, allocator_type) _Reft;
  143. typedef _Tptr pointer;
  144. typedef _Ctptr const_pointer;
  145. typedef _Reft reference;
  146. typedef _CREFERENCE_X(value_type, allocator_type)
  147. const_reference;
  148. // CLASS const_iterator
  149. class const_iterator;
  150. friend class const_iterator;
  151. class const_iterator
  152. : public _Bidit<value_type, _Dift, _Ctptr, const_reference>
  153. { // iterator for nonmutable _Tree
  154. public:
  155. typedef bidirectional_iterator_tag iterator_category;
  156. typedef _Dift difference_type;
  157. typedef _Ctptr pointer;
  158. typedef const_reference reference;
  159. const_iterator()
  160. : _Ptr(0)
  161. { // construct with null node pointer
  162. }
  163. const_iterator(_Nodeptr _Pnode)
  164. : _Ptr(_Pnode)
  165. { // construct with node pointer _Pnode
  166. }
  167. const_reference operator*() const
  168. { // return designated value
  169. return (_Myval(_Ptr));
  170. }
  171. _Ctptr operator->() const
  172. { // return pointer to class object
  173. return (&**this);
  174. }
  175. const_iterator& operator++()
  176. { // preincrement
  177. _Inc();
  178. return (*this);
  179. }
  180. const_iterator operator++(int)
  181. { // postincrement
  182. const_iterator _Tmp = *this;
  183. ++*this;
  184. return (_Tmp);
  185. }
  186. const_iterator& operator--()
  187. { // predecrement
  188. _Dec();
  189. return (*this);
  190. }
  191. const_iterator operator--(int)
  192. { // postdecrement
  193. const_iterator _Tmp = *this;
  194. --*this;
  195. return (_Tmp);
  196. }
  197. bool operator==(const const_iterator& _Right) const
  198. { // test for iterator equality
  199. return (_Ptr == _Right._Ptr);
  200. }
  201. bool operator!=(const const_iterator& _Right) const
  202. { // test for iterator inequality
  203. return (!(*this == _Right));
  204. }
  205. void _Dec()
  206. { // move to node with next smaller value
  207. if (_Isnil(_Ptr))
  208. _Ptr = _Right(_Ptr); // end() ==> rightmost
  209. else if (!_Isnil(_Left(_Ptr)))
  210. _Ptr = _Max(_Left(_Ptr)); // ==> largest of left subtree
  211. else
  212. { // climb looking for left subtree
  213. _Nodeptr _Pnode;
  214. while (!_Isnil(_Pnode = _Parent(_Ptr))
  215. && _Ptr == _Left(_Pnode))
  216. _Ptr = _Pnode; // ==> parent while left subtree
  217. if (!_Isnil(_Pnode))
  218. _Ptr = _Pnode; // ==> parent if not head
  219. }
  220. }
  221. void _Inc()
  222. { // move to node with next larger value
  223. if (_Isnil(_Ptr))
  224. ; // end() shouldn't be incremented, don't move
  225. else if (!_Isnil(_Right(_Ptr)))
  226. _Ptr = _Min(_Right(_Ptr)); // ==> smallest of right subtree
  227. else
  228. { // climb looking for right subtree
  229. _Nodeptr _Pnode;
  230. while (!_Isnil(_Pnode = _Parent(_Ptr))
  231. && _Ptr == _Right(_Pnode))
  232. _Ptr = _Pnode; // ==> parent while right subtree
  233. _Ptr = _Pnode; // ==> parent (head if end())
  234. }
  235. }
  236. _Nodeptr _Mynode() const
  237. { // return node pointer
  238. return (_Ptr);
  239. }
  240. protected:
  241. _Nodeptr _Ptr; // pointer to node
  242. };
  243. // CLASS iterator
  244. class iterator;
  245. friend class iterator;
  246. class iterator
  247. : public const_iterator
  248. { // iterator for mutable _Tree
  249. public:
  250. typedef bidirectional_iterator_tag iterator_category;
  251. typedef _Dift difference_type;
  252. typedef _ITptr pointer;
  253. typedef _IReft reference;
  254. iterator()
  255. : const_iterator(0)
  256. { // construct with null node pointer
  257. }
  258. iterator(_Nodeptr _Pnode)
  259. : const_iterator(_Pnode)
  260. { // construct with node pointer _Pnode
  261. }
  262. reference operator*() const
  263. { // return designated value
  264. return (_Myval(_Ptr));
  265. }
  266. pointer operator->() const
  267. { // return pointer to class object
  268. return (&**this);
  269. }
  270. iterator& operator++()
  271. { // preincrement
  272. _Inc();
  273. return (*this);
  274. }
  275. iterator operator++(int)
  276. { // postincrement
  277. iterator _Tmp = *this;
  278. ++*this;
  279. return (_Tmp);
  280. }
  281. iterator& operator--()
  282. { // predecrement
  283. _Dec();
  284. return (*this);
  285. }
  286. iterator operator--(int)
  287. { // postdecrement
  288. iterator _Tmp = *this;
  289. --*this;
  290. return (_Tmp);
  291. }
  292. };
  293. typedef std::reverse_iterator<iterator> reverse_iterator;
  294. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  295. typedef pair<iterator, bool> _Pairib;
  296. typedef pair<iterator, iterator> _Pairii;
  297. typedef pair<const_iterator, const_iterator> _Paircc;
  298. explicit _Tree(const key_compare& _Parg,
  299. const allocator_type& _Al)
  300. : _Mybase(_Parg, _Al)
  301. { // construct empty tree
  302. _Init();
  303. }
  304. _Tree(const value_type *_First, const value_type *_Last,
  305. const key_compare& _Parg, const allocator_type& _Al)
  306. : _Mybase(_Parg, _Al)
  307. { // construct tree from [_First, _Last) array
  308. _Init();
  309. _TRY_BEGIN
  310. insert(_First, _Last);
  311. _CATCH_ALL
  312. _Tidy();
  313. _RERAISE;
  314. _CATCH_END
  315. }
  316. _Tree(const _Myt& _Right)
  317. : _Mybase(_Right.key_comp(), _Right.get_allocator())
  318. { // construct tree by copying _Right
  319. _Init();
  320. _TRY_BEGIN
  321. _Copy(_Right);
  322. _CATCH_ALL
  323. _Tidy();
  324. _RERAISE;
  325. _CATCH_END
  326. }
  327. ~_Tree()
  328. { // destroy tree
  329. _Tidy();
  330. }
  331. _Myt& operator=(const _Myt& _Right)
  332. { // replace contents from _Right
  333. if (this != &_Right)
  334. { // worth doing
  335. erase(begin(), end());
  336. this->comp = _Right.comp;
  337. _Copy(_Right);
  338. }
  339. return (*this);
  340. }
  341. iterator begin()
  342. { // return iterator for beginning of mutable sequence
  343. return (iterator(_Lmost()));
  344. }
  345. const_iterator begin() const
  346. { // return iterator for beginning of nonmutable sequence
  347. return (const_iterator(_Lmost()));
  348. }
  349. iterator end()
  350. { // return iterator for end of mutable sequence
  351. return (iterator(_Myhead));
  352. }
  353. const_iterator end() const
  354. { // return iterator for end of nonmutable sequence
  355. return (const_iterator(_Myhead));
  356. }
  357. reverse_iterator rbegin()
  358. { // return iterator for beginning of reversed mutable sequence
  359. return (reverse_iterator(end()));
  360. }
  361. const_reverse_iterator rbegin() const
  362. { // return iterator for beginning of reversed nonmutable sequence
  363. return (const_reverse_iterator(end()));
  364. }
  365. reverse_iterator rend()
  366. { // return iterator for end of reversed mutable sequence
  367. return (reverse_iterator(begin()));
  368. }
  369. const_reverse_iterator rend() const
  370. { // return iterator for end of reversed nonmutable sequence
  371. return (const_reverse_iterator(begin()));
  372. }
  373. size_type size() const
  374. { // return length of sequence
  375. return (_Mysize);
  376. }
  377. size_type max_size() const
  378. { // return maximum possible length of sequence
  379. return (this->_Alval.max_size());
  380. }
  381. bool empty() const
  382. { // return true only if sequence is empty
  383. return (size() == 0);
  384. }
  385. allocator_type get_allocator() const
  386. { // return allocator object for values
  387. return (this->_Alval);
  388. }
  389. key_compare key_comp() const
  390. { // return object for comparing keys
  391. return (this->comp);
  392. }
  393. value_compare value_comp() const
  394. { // return object for comparing values
  395. return (value_compare(key_comp()));
  396. }
  397. _Pairib insert(const value_type& _Val)
  398. { // try to insert node with value _Val
  399. _Nodeptr _Trynode = _Root();
  400. _Nodeptr _Wherenode = _Myhead;
  401. bool _Addleft = true; // add to left of head if tree empty
  402. while (!_Isnil(_Trynode))
  403. { // look for leaf to insert before (_Addleft) or after
  404. _Wherenode = _Trynode;
  405. _Addleft = this->comp(this->_Kfn(_Val), _Key(_Trynode));
  406. _Trynode = _Addleft ? _Left(_Trynode) : _Right(_Trynode);
  407. }
  408. if (this->_Multi)
  409. return (_Pairib(_Insert(_Addleft, _Wherenode, _Val), true));
  410. else
  411. { // insert only if unique
  412. iterator _Where = iterator(_Wherenode);
  413. if (!_Addleft)
  414. ; // need to test if insert after is okay
  415. else if (_Where == begin())
  416. return (_Pairib(_Insert(true, _Wherenode, _Val), true));
  417. else
  418. --_Where; // need to test if insert before is okay
  419. if (this->comp(_Key(_Where._Mynode()), this->_Kfn(_Val)))
  420. return (_Pairib(_Insert(_Addleft, _Wherenode, _Val), true));
  421. else
  422. return (_Pairib(_Where, false));
  423. }
  424. }
  425. iterator insert(iterator _Where, const value_type& _Val)
  426. { // try to insert node with value _Val using _Where as a hint
  427. iterator _Next;
  428. if (size() == 0)
  429. return (_Insert(true, _Myhead, _Val)); // insert into empty tree
  430. else if (this->_Multi)
  431. { // insert even if duplicate
  432. if (_Where == begin())
  433. { // insert at beginning if before first element
  434. if (!this->comp(_Key(_Where._Mynode()), this->_Kfn(_Val)))
  435. return (_Insert(true, _Where._Mynode(), _Val));
  436. }
  437. else if (_Where == end())
  438. { // insert at end if after last element
  439. if (!this->comp(this->_Kfn(_Val), _Key(_Rmost())))
  440. return (_Insert(false, _Rmost(), _Val));
  441. }
  442. else if (!this->comp(_Key(_Where._Mynode()), this->_Kfn(_Val))
  443. && !this->comp(this->_Kfn(_Val),
  444. _Key((--(_Next = _Where))._Mynode())))
  445. { // insert before _Where
  446. if (_Isnil(_Right(_Next._Mynode())))
  447. return (_Insert(false, _Next._Mynode(), _Val));
  448. else
  449. return (_Insert(true, _Where._Mynode(), _Val));
  450. }
  451. else if (!this->comp(this->_Kfn(_Val), _Key(_Where._Mynode()))
  452. && (++(_Next = _Where) == end()
  453. || !this->comp(_Key(_Next._Mynode()),
  454. this->_Kfn(_Val))))
  455. { // insert after _Where
  456. if (_Isnil(_Right(_Where._Mynode())))
  457. return (_Insert(false, _Where._Mynode(), _Val));
  458. else
  459. return (_Insert(true, _Next._Mynode(), _Val));
  460. }
  461. }
  462. else
  463. { // insert only if unique
  464. if (_Where == begin())
  465. { // insert at beginning if before first element
  466. if (this->comp(this->_Kfn(_Val), _Key(_Where._Mynode())))
  467. return (_Insert(true, _Where._Mynode(), _Val));
  468. }
  469. else if (_Where == end())
  470. { // insert at end if after last element
  471. if (this->comp(_Key(_Rmost()), this->_Kfn(_Val)))
  472. return (_Insert(false, _Rmost(), _Val));
  473. }
  474. else if (this->comp(this->_Kfn(_Val), _Key(_Where._Mynode()))
  475. && this->comp(_Key((--(_Next = _Where))._Mynode()),
  476. this->_Kfn(_Val)))
  477. { // insert before _Where
  478. if (_Isnil(_Right(_Next._Mynode())))
  479. return (_Insert(false, _Next._Mynode(), _Val));
  480. else
  481. return (_Insert(true, _Where._Mynode(), _Val));
  482. }
  483. else if (this->comp(_Key(_Where._Mynode()), this->_Kfn(_Val))
  484. && (++(_Next = _Where) == end()
  485. || this->comp(this->_Kfn(_Val),
  486. _Key(_Next._Mynode()))))
  487. { // insert after _Where
  488. if (_Isnil(_Right(_Where._Mynode())))
  489. return (_Insert(false, _Where._Mynode(), _Val));
  490. else
  491. return (_Insert(true, _Next._Mynode(), _Val));
  492. }
  493. }
  494. return (insert(_Val).first); // try usual insert if all else fails
  495. }
  496. template<class _Iter>
  497. void insert(_Iter _First, _Iter _Last)
  498. { // insert [_First, _Last) one at a time
  499. for (; _First != _Last; ++_First)
  500. insert(*_First);
  501. }
  502. iterator erase(iterator _Where)
  503. { // erase element at _Where
  504. if (_Isnil(_Where._Mynode()))
  505. _THROW(out_of_range, "invalid map/set<T> iterator");
  506. _Nodeptr _Fixnode; // the node to recolor as needed
  507. _Nodeptr _Fixnodeparent; // parent of _Fixnode (which may be nil)
  508. _Nodeptr _Erasednode = _Where._Mynode(); // node to erase
  509. _Nodeptr _Pnode = _Erasednode;
  510. ++_Where; // save successor iterator for return
  511. if (_Isnil(_Left(_Pnode)))
  512. _Fixnode = _Right(_Pnode); // must stitch up right subtree
  513. else if (_Isnil(_Right(_Pnode)))
  514. _Fixnode = _Left(_Pnode); // must stitch up left subtree
  515. else
  516. { // two subtrees, must lift successor node to replace erased
  517. _Pnode = _Where._Mynode(); // _Pnode is successor node
  518. _Fixnode = _Right(_Pnode); // _Fixnode is its only subtree
  519. }
  520. if (_Pnode == _Erasednode)
  521. { // at most one subtree, relink it
  522. _Fixnodeparent = _Parent(_Erasednode);
  523. if (!_Isnil(_Fixnode))
  524. _Parent(_Fixnode) = _Fixnodeparent; // link up
  525. if (_Root() == _Erasednode)
  526. _Root() = _Fixnode; // link down from root
  527. else if (_Left(_Fixnodeparent) == _Erasednode)
  528. _Left(_Fixnodeparent) = _Fixnode; // link down to left
  529. else
  530. _Right(_Fixnodeparent) = _Fixnode; // link down to right
  531. if (_Lmost() == _Erasednode)
  532. _Lmost() = _Isnil(_Fixnode)
  533. ? _Fixnodeparent // smallest is parent of erased node
  534. : _Min(_Fixnode); // smallest in relinked subtree
  535. if (_Rmost() == _Erasednode)
  536. _Rmost() = _Isnil(_Fixnode)
  537. ? _Fixnodeparent // largest is parent of erased node
  538. : _Max(_Fixnode); // largest in relinked subtree
  539. }
  540. else
  541. { // erased has two subtrees, _Pnode is successor to erased
  542. _Parent(_Left(_Erasednode)) = _Pnode; // link left up
  543. _Left(_Pnode) = _Left(_Erasednode); // link successor down
  544. if (_Pnode == _Right(_Erasednode))
  545. _Fixnodeparent = _Pnode; // successor is next to erased
  546. else
  547. { // successor further down, link in place of erased
  548. _Fixnodeparent = _Parent(_Pnode); // parent is successor's
  549. if (!_Isnil(_Fixnode))
  550. _Parent(_Fixnode) = _Fixnodeparent; // link fix up
  551. _Left(_Fixnodeparent) = _Fixnode; // link fix down
  552. _Right(_Pnode) = _Right(_Erasednode); // link successor down
  553. _Parent(_Right(_Erasednode)) = _Pnode; // link right up
  554. }
  555. if (_Root() == _Erasednode)
  556. _Root() = _Pnode; // link down from root
  557. else if (_Left(_Parent(_Erasednode)) == _Erasednode)
  558. _Left(_Parent(_Erasednode)) = _Pnode; // link down to left
  559. else
  560. _Right(_Parent(_Erasednode)) = _Pnode; // link down to right
  561. _Parent(_Pnode) = _Parent(_Erasednode); // link successor up
  562. std::swap(_Color(_Pnode), _Color(_Erasednode)); // recolor it
  563. }
  564. if (_Color(_Erasednode) == _Black)
  565. { // erasing black link, must recolor/rebalance tree
  566. for (; _Fixnode != _Root() && _Color(_Fixnode) == _Black;
  567. _Fixnodeparent = _Parent(_Fixnode))
  568. if (_Fixnode == _Left(_Fixnodeparent))
  569. { // fixup left subtree
  570. _Pnode = _Right(_Fixnodeparent);
  571. if (_Color(_Pnode) == _Red)
  572. { // rotate red up from right subtree
  573. _Color(_Pnode) = _Black;
  574. _Color(_Fixnodeparent) = _Red;
  575. _Lrotate(_Fixnodeparent);
  576. _Pnode = _Right(_Fixnodeparent);
  577. }
  578. if (_Isnil(_Pnode))
  579. _Fixnode = _Fixnodeparent; // shouldn't happen
  580. else if (_Color(_Left(_Pnode)) == _Black
  581. && _Color(_Right(_Pnode)) == _Black)
  582. { // redden right subtree with black children
  583. _Color(_Pnode) = _Red;
  584. _Fixnode = _Fixnodeparent;
  585. }
  586. else
  587. { // must rearrange right subtree
  588. if (_Color(_Right(_Pnode)) == _Black)
  589. { // rotate red up from left sub-subtree
  590. _Color(_Left(_Pnode)) = _Black;
  591. _Color(_Pnode) = _Red;
  592. _Rrotate(_Pnode);
  593. _Pnode = _Right(_Fixnodeparent);
  594. }
  595. _Color(_Pnode) = _Color(_Fixnodeparent);
  596. _Color(_Fixnodeparent) = _Black;
  597. _Color(_Right(_Pnode)) = _Black;
  598. _Lrotate(_Fixnodeparent);
  599. break; // tree now recolored/rebalanced
  600. }
  601. }
  602. else
  603. { // fixup right subtree
  604. _Pnode = _Left(_Fixnodeparent);
  605. if (_Color(_Pnode) == _Red)
  606. { // rotate red up from left subtree
  607. _Color(_Pnode) = _Black;
  608. _Color(_Fixnodeparent) = _Red;
  609. _Rrotate(_Fixnodeparent);
  610. _Pnode = _Left(_Fixnodeparent);
  611. }
  612. if (_Isnil(_Pnode))
  613. _Fixnode = _Fixnodeparent; // shouldn't happen
  614. else if (_Color(_Right(_Pnode)) == _Black
  615. && _Color(_Left(_Pnode)) == _Black)
  616. { // redden left subtree with black children
  617. _Color(_Pnode) = _Red;
  618. _Fixnode = _Fixnodeparent;
  619. }
  620. else
  621. { // must rearrange left subtree
  622. if (_Color(_Left(_Pnode)) == _Black)
  623. { // rotate red up from right sub-subtree
  624. _Color(_Right(_Pnode)) = _Black;
  625. _Color(_Pnode) = _Red;
  626. _Lrotate(_Pnode);
  627. _Pnode = _Left(_Fixnodeparent);
  628. }
  629. _Color(_Pnode) = _Color(_Fixnodeparent);
  630. _Color(_Fixnodeparent) = _Black;
  631. _Color(_Left(_Pnode)) = _Black;
  632. _Rrotate(_Fixnodeparent);
  633. break; // tree now recolored/rebalanced
  634. }
  635. }
  636. _Color(_Fixnode) = _Black; // ensure stopping node is black
  637. }
  638. this->_Alnod.destroy(_Erasednode); // destroy, free erased node
  639. this->_Alnod.deallocate(_Erasednode, 1);
  640. if (0 < _Mysize)
  641. --_Mysize;
  642. return (_Where); // return successor iterator
  643. }
  644. iterator erase(iterator _First, iterator _Last)
  645. { // erase [_First, _Last)
  646. if (_First == begin() && _Last == end())
  647. { // erase all
  648. clear();
  649. return (begin());
  650. }
  651. else
  652. { // partial erase, one at a time
  653. while (_First != _Last)
  654. erase(_First++);
  655. return (_First);
  656. }
  657. }
  658. size_type erase(const key_type& _Keyval)
  659. { // erase and count all that match _Keyval
  660. _Pairii _Where = equal_range(_Keyval);
  661. size_type _Num = 0;
  662. _Distance(_Where.first, _Where.second, _Num);
  663. erase(_Where.first, _Where.second);
  664. return (_Num);
  665. }
  666. void erase(const key_type *_First, const key_type *_Last)
  667. { // erase all that match array of keys [_First, _Last)
  668. while (_First != _Last)
  669. erase(*_First++);
  670. }
  671. void clear()
  672. { // erase all
  673. _Erase(_Root());
  674. _Root() = _Myhead, _Mysize = 0;
  675. _Lmost() = _Myhead, _Rmost() = _Myhead;
  676. }
  677. iterator find(const key_type& _Keyval)
  678. { // find an element in mutable sequence that matches _Keyval
  679. iterator _Where = lower_bound(_Keyval);
  680. return (_Where == end() || this->comp(_Keyval, _Key(_Where._Mynode()))
  681. ? end() : _Where);
  682. }
  683. const_iterator find(const key_type& _Keyval) const
  684. { // find an element in nonmutable sequence that matches _Keyval
  685. const_iterator _Where = lower_bound(_Keyval);
  686. return (_Where == end() || this->comp(_Keyval, _Key(_Where._Mynode()))
  687. ? end() : _Where);
  688. }
  689. size_type count(const key_type& _Keyval) const
  690. { // count all elements that match _Keyval
  691. _Paircc _Ans = equal_range(_Keyval);
  692. size_type _Num = 0;
  693. _Distance(_Ans.first, _Ans.second, _Num);
  694. return (_Num);
  695. }
  696. iterator lower_bound(const key_type& _Keyval)
  697. { // find leftmost node not less than _Keyval in mutable tree
  698. return (iterator(_Lbound(_Keyval)));
  699. }
  700. const_iterator lower_bound(const key_type& _Keyval) const
  701. { // find leftmost node not less than _Keyval in nonmutable tree
  702. return (const_iterator(_Lbound(_Keyval)));
  703. }
  704. iterator upper_bound(const key_type& _Keyval)
  705. { // find leftmost node greater than _Keyval in mutable tree
  706. return (iterator(_Ubound(_Keyval)));
  707. }
  708. const_iterator upper_bound(const key_type& _Keyval) const
  709. { // find leftmost node greater than _Keyval in nonmutable tree
  710. return (const_iterator(_Ubound(_Keyval)));
  711. }
  712. _Pairii equal_range(const key_type& _Keyval)
  713. { // find range equivalent to _Keyval in mutable tree
  714. return (_Pairii(lower_bound(_Keyval), upper_bound(_Keyval)));
  715. }
  716. _Paircc equal_range(const key_type& _Keyval) const
  717. { // find range equivalent to _Keyval in nonmutable tree
  718. return (_Paircc(lower_bound(_Keyval), upper_bound(_Keyval)));
  719. }
  720. void swap(_Myt& _Right)
  721. { // exchange contents with _Right
  722. if (get_allocator() == _Right.get_allocator())
  723. { // same allocator, swap control information
  724. std::swap(this->comp, _Right.comp);
  725. std::swap(_Myhead, _Right._Myhead);
  726. std::swap(_Mysize, _Right._Mysize);
  727. }
  728. else
  729. { // different allocator, do multiple assigns
  730. _Myt _Tmp = *this; *this = _Right, _Right = _Tmp;
  731. }
  732. }
  733. friend void swap(_Myt& _Left, _Myt& _Right)
  734. { // swap _Left and _Right trees
  735. _Left.swap(_Right);
  736. }
  737. protected:
  738. void _Copy(const _Myt& _Right)
  739. { // copy entire tree from _Right
  740. _Root() = _Copy(_Right._Root(), _Myhead);
  741. _Mysize = _Right.size();
  742. if (!_Isnil(_Root()))
  743. { // nonempty tree, look for new smallest and largest
  744. _Lmost() = _Min(_Root());
  745. _Rmost() = _Max(_Root());
  746. }
  747. else
  748. _Lmost() = _Myhead, _Rmost() = _Myhead; // empty tree
  749. }
  750. _Nodeptr _Copy(_Nodeptr _Rootnode, _Nodeptr _Wherenode)
  751. { // copy entire subtree, recursively
  752. _Nodeptr _Newroot = _Myhead; // point at nil node
  753. if (!_Isnil(_Rootnode))
  754. { // copy a node, then any subtrees
  755. _Nodeptr _Pnode = _Buynode(_Myhead, _Wherenode, _Myhead,
  756. _Myval(_Rootnode), _Color(_Rootnode));
  757. if (_Isnil(_Newroot))
  758. _Newroot = _Pnode; // memorize new root
  759. _TRY_BEGIN
  760. _Left(_Pnode) = _Copy(_Left(_Rootnode), _Pnode);
  761. _Right(_Pnode) = _Copy(_Right(_Rootnode), _Pnode);
  762. _CATCH_ALL
  763. _Erase(_Newroot); // subtree copy failed, bail out
  764. _RERAISE;
  765. _CATCH_END
  766. }
  767. return (_Newroot); // return newly constructed tree
  768. }
  769. void _Erase(_Nodeptr _Rootnode)
  770. { // free entire subtree, recursively
  771. for (_Nodeptr _Pnode = _Rootnode; !_Isnil(_Pnode); _Rootnode = _Pnode)
  772. { // free subtrees, then node
  773. _Erase(_Right(_Pnode));
  774. _Pnode = _Left(_Pnode);
  775. this->_Alnod.destroy(_Rootnode); // destroy, free erased node
  776. this->_Alnod.deallocate(_Rootnode, 1);
  777. }
  778. }
  779. void _Init()
  780. { // create head/nil node and make tree empty
  781. _Myhead = _Buynode();
  782. _Isnil(_Myhead) = true;
  783. _Root() = _Myhead;
  784. _Lmost() = _Myhead, _Rmost() = _Myhead;
  785. _Mysize = 0;
  786. }
  787. iterator _Insert(bool _Addleft, _Nodeptr _Wherenode,
  788. const value_type& _Val)
  789. { // add node with value next to _Wherenode, to left if _Addnode
  790. if (max_size() - 1 <= _Mysize)
  791. _THROW(length_error, "map/set<T> too long");
  792. _Nodeptr _Newnode = _Buynode(_Myhead, _Wherenode, _Myhead,
  793. _Val, _Red);
  794. ++_Mysize;
  795. if (_Wherenode == _Myhead)
  796. { // first node in tree, just set head values
  797. _Root() = _Newnode;
  798. _Lmost() = _Newnode, _Rmost() = _Newnode;
  799. }
  800. else if (_Addleft)
  801. { // add to left of _Wherenode
  802. _Left(_Wherenode) = _Newnode;
  803. if (_Wherenode == _Lmost())
  804. _Lmost() = _Newnode;
  805. }
  806. else
  807. { // add to right of _Wherenode
  808. _Right(_Wherenode) = _Newnode;
  809. if (_Wherenode == _Rmost())
  810. _Rmost() = _Newnode;
  811. }
  812. for (_Nodeptr _Pnode = _Newnode; _Color(_Parent(_Pnode)) == _Red; )
  813. if (_Parent(_Pnode) == _Left(_Parent(_Parent(_Pnode))))
  814. { // fixup red-red in left subtree
  815. _Wherenode = _Right(_Parent(_Parent(_Pnode)));
  816. if (_Color(_Wherenode) == _Red)
  817. { // parent has two red children, blacken both
  818. _Color(_Parent(_Pnode)) = _Black;
  819. _Color(_Wherenode) = _Black;
  820. _Color(_Parent(_Parent(_Pnode))) = _Red;
  821. _Pnode = _Parent(_Parent(_Pnode));
  822. }
  823. else
  824. { // parent has red and black children
  825. if (_Pnode == _Right(_Parent(_Pnode)))
  826. { // rotate right child to left
  827. _Pnode = _Parent(_Pnode);
  828. _Lrotate(_Pnode);
  829. }
  830. _Color(_Parent(_Pnode)) = _Black; // propagate red up
  831. _Color(_Parent(_Parent(_Pnode))) = _Red;
  832. _Rrotate(_Parent(_Parent(_Pnode)));
  833. }
  834. }
  835. else
  836. { // fixup red-red in right subtree
  837. _Wherenode = _Left(_Parent(_Parent(_Pnode)));
  838. if (_Color(_Wherenode) == _Red)
  839. { // parent has two red children, blacken both
  840. _Color(_Parent(_Pnode)) = _Black;
  841. _Color(_Wherenode) = _Black;
  842. _Color(_Parent(_Parent(_Pnode))) = _Red;
  843. _Pnode = _Parent(_Parent(_Pnode));
  844. }
  845. else
  846. { // parent has red and black children
  847. if (_Pnode == _Left(_Parent(_Pnode)))
  848. { // rotate left child to right
  849. _Pnode = _Parent(_Pnode);
  850. _Rrotate(_Pnode);
  851. }
  852. _Color(_Parent(_Pnode)) = _Black; // propagate red up
  853. _Color(_Parent(_Parent(_Pnode))) = _Red;
  854. _Lrotate(_Parent(_Parent(_Pnode)));
  855. }
  856. }
  857. _Color(_Root()) = _Black; // root is always black
  858. return (iterator(_Newnode));
  859. }
  860. _Nodeptr _Lbound(const key_type& _Keyval) const
  861. { // find leftmost node not less than _Keyval
  862. _Nodeptr _Pnode = _Root();
  863. _Nodeptr _Wherenode = _Myhead; // end() if search fails
  864. while (!_Isnil(_Pnode))
  865. if (this->comp(_Key(_Pnode), _Keyval))
  866. _Pnode = _Right(_Pnode); // descend right subtree
  867. else
  868. { // _Pnode not less than _Keyval, remember it
  869. _Wherenode = _Pnode;
  870. _Pnode = _Left(_Pnode); // descend left subtree
  871. }
  872. return (_Wherenode); // return best remembered candidate
  873. }
  874. _Nodeptr& _Lmost()
  875. { // return leftmost node in mutable tree
  876. return (_Left(_Myhead));
  877. }
  878. _Nodeptr& _Lmost() const
  879. { // return leftmost node in nonmutable tree
  880. return (_Left(_Myhead));
  881. }
  882. void _Lrotate(_Nodeptr _Wherenode)
  883. { // promote right node to root of subtree
  884. _Nodeptr _Pnode = _Right(_Wherenode);
  885. _Right(_Wherenode) = _Left(_Pnode);
  886. if (!_Isnil(_Left(_Pnode)))
  887. _Parent(_Left(_Pnode)) = _Wherenode;
  888. _Parent(_Pnode) = _Parent(_Wherenode);
  889. if (_Wherenode == _Root())
  890. _Root() = _Pnode;
  891. else if (_Wherenode == _Left(_Parent(_Wherenode)))
  892. _Left(_Parent(_Wherenode)) = _Pnode;
  893. else
  894. _Right(_Parent(_Wherenode)) = _Pnode;
  895. _Left(_Pnode) = _Wherenode;
  896. _Parent(_Wherenode) = _Pnode;
  897. }
  898. static _Nodeptr _Max(_Nodeptr _Pnode)
  899. { // return rightmost node in subtree at _Pnode
  900. while (!_Isnil(_Right(_Pnode)))
  901. _Pnode = _Right(_Pnode);
  902. return (_Pnode);
  903. }
  904. static _Nodeptr _Min(_Nodeptr _Pnode)
  905. { // return leftmost node in subtree at _Pnode
  906. while (!_Isnil(_Left(_Pnode)))
  907. _Pnode = _Left(_Pnode);
  908. return (_Pnode);
  909. }
  910. _Nodeptr& _Rmost()
  911. { // return rightmost node in mutable tree
  912. return (_Right(_Myhead));
  913. }
  914. _Nodeptr& _Rmost() const
  915. { // return rightmost node in nonmutable tree
  916. return (_Right(_Myhead));
  917. }
  918. _Nodeptr& _Root()
  919. { // return root of mutable tree
  920. return (_Parent(_Myhead));
  921. }
  922. _Nodeptr& _Root() const
  923. { // return root of nonmutable tree
  924. return (_Parent(_Myhead));
  925. }
  926. void _Rrotate(_Nodeptr _Wherenode)
  927. { // promote left node to root of subtree
  928. _Nodeptr _Pnode = _Left(_Wherenode);
  929. _Left(_Wherenode) = _Right(_Pnode);
  930. if (!_Isnil(_Right(_Pnode)))
  931. _Parent(_Right(_Pnode)) = _Wherenode;
  932. _Parent(_Pnode) = _Parent(_Wherenode);
  933. if (_Wherenode == _Root())
  934. _Root() = _Pnode;
  935. else if (_Wherenode == _Right(_Parent(_Wherenode)))
  936. _Right(_Parent(_Wherenode)) = _Pnode;
  937. else
  938. _Left(_Parent(_Wherenode)) = _Pnode;
  939. _Right(_Pnode) = _Wherenode;
  940. _Parent(_Wherenode) = _Pnode;
  941. }
  942. _Nodeptr _Ubound(const key_type& _Keyval) const
  943. { // find leftmost node greater than _Keyval
  944. _Nodeptr _Pnode = _Root();
  945. _Nodeptr _Wherenode = _Myhead; // end() if search fails
  946. while (!_Isnil(_Pnode))
  947. if (this->comp(_Keyval, _Key(_Pnode)))
  948. { // _Pnode greater than _Keyval, remember it
  949. _Wherenode = _Pnode;
  950. _Pnode = _Left(_Pnode); // descend left subtree
  951. }
  952. else
  953. _Pnode = _Right(_Pnode); // descend right subtree
  954. return (_Wherenode); // return best remembered candidate
  955. }
  956. _Nodeptr _Buynode()
  957. { // allocate a head/nil node
  958. _Nodeptr _Wherenode = this->_Alnod.allocate(1, (void *)0);
  959. int _Linkcnt = 0;
  960. _TRY_BEGIN
  961. this->_Alptr.construct(&_Left(_Wherenode), 0);
  962. ++_Linkcnt;
  963. this->_Alptr.construct(&_Parent(_Wherenode), 0);
  964. ++_Linkcnt;
  965. this->_Alptr.construct(&_Right(_Wherenode), 0);
  966. _CATCH_ALL
  967. if (1 < _Linkcnt)
  968. this->_Alptr.destroy(&_Parent(_Wherenode));
  969. if (0 < _Linkcnt)
  970. this->_Alptr.destroy(&_Left(_Wherenode));
  971. this->_Alnod.deallocate(_Wherenode, 1);
  972. _RERAISE;
  973. _CATCH_END
  974. _Color(_Wherenode) = _Black;
  975. _Isnil(_Wherenode) = false;
  976. return (_Wherenode);
  977. }
  978. _Nodeptr _Buynode(_Nodeptr _Larg, _Nodeptr _Parg, _Nodeptr _Rarg,
  979. const value_type& _Val, char _Carg)
  980. { // allocate a node with pointers, value, and color
  981. _Nodeptr _Wherenode = this->_Alnod.allocate(1, (void *)0);
  982. _TRY_BEGIN
  983. new ((void *)_Wherenode) _Node(_Larg, _Parg, _Rarg, _Val, _Carg);
  984. _CATCH_ALL
  985. this->_Alnod.deallocate(_Wherenode, 1);
  986. _RERAISE;
  987. _CATCH_END
  988. return (_Wherenode);
  989. }
  990. void _Tidy()
  991. { // free all storage
  992. erase(begin(), end());
  993. this->_Alptr.destroy(&_Left(_Myhead));
  994. this->_Alptr.destroy(&_Parent(_Myhead));
  995. this->_Alptr.destroy(&_Right(_Myhead));
  996. this->_Alnod.deallocate(_Myhead, 1);
  997. _Myhead = 0, _Mysize = 0;
  998. }
  999. _Nodeptr _Myhead; // pointer to head node
  1000. size_type _Mysize; // number of elements
  1001. };
  1002. // _Tree TEMPLATE OPERATORS
  1003. template<class _Traits> inline
  1004. bool operator==(const _Tree<_Traits>& _Left, const _Tree<_Traits>& _Right)
  1005. { // test for _Tree equality
  1006. return (_Left.size() == _Right.size()
  1007. && equal(_Left.begin(), _Left.end(), _Right.begin()));
  1008. }
  1009. template<class _Traits> inline
  1010. bool operator!=(const _Tree<_Traits>& _Left, const _Tree<_Traits>& _Right)
  1011. { // test for _Tree inequality
  1012. return (!(_Left == _Right));
  1013. }
  1014. template<class _Traits> inline
  1015. bool operator<(const _Tree<_Traits>& _Left, const _Tree<_Traits>& _Right)
  1016. { // test if _Less < _Right for _Trees
  1017. return (lexicographical_compare(_Left.begin(), _Left.end(),
  1018. _Right.begin(), _Right.end()));
  1019. }
  1020. template<class _Traits> inline
  1021. bool operator>(const _Tree<_Traits>& _Left, const _Tree<_Traits>& _Right)
  1022. { // test if _Less > _Right for _Trees
  1023. return (_Right < _Left);
  1024. }
  1025. template<class _Traits> inline
  1026. bool operator<=(const _Tree<_Traits>& _Left, const _Tree<_Traits>& _Right)
  1027. { // test if _Less <= _Right for _Trees
  1028. return (!(_Right < _Left));
  1029. }
  1030. template<class _Traits> inline
  1031. bool operator>=(const _Tree<_Traits>& _Left, const _Tree<_Traits>& _Right)
  1032. { // test if _Less >= _Right for _Trees
  1033. return (!(_Left < _Right));
  1034. }
  1035. _STD_END
  1036. #pragma warning(default:4127)
  1037. #pragma warning(pop)
  1038. #pragma pack(pop)
  1039. #endif /* _XTREE_ */
  1040. /*
  1041. * Copyright (c) 1992-2001 by P.J. Plauger. ALL RIGHTS RESERVED.
  1042. * Consult your license regarding permissions and restrictions.
  1043. */
  1044. /*
  1045. * This file is derived from software bearing the following
  1046. * restrictions:
  1047. *
  1048. * Copyright (c) 1994
  1049. * Hewlett-Packard Company
  1050. *
  1051. * Permission to use, copy, modify, distribute and sell this
  1052. * software and its documentation for any purpose is hereby
  1053. * granted without fee, provided that the above copyright notice
  1054. * appear in all copies and that both that copyright notice and
  1055. * this permission notice appear in supporting documentation.
  1056. * Hewlett-Packard Company makes no representations about the
  1057. * suitability of this software for any purpose. It is provided
  1058. * "as is" without express or implied warranty.
  1059. V3.10:0009 */