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.

1218 lines
35 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. protected:
  734. void _Copy(const _Myt& _Right)
  735. { // copy entire tree from _Right
  736. _Root() = _Copy(_Right._Root(), _Myhead);
  737. _Mysize = _Right.size();
  738. if (!_Isnil(_Root()))
  739. { // nonempty tree, look for new smallest and largest
  740. _Lmost() = _Min(_Root());
  741. _Rmost() = _Max(_Root());
  742. }
  743. else
  744. _Lmost() = _Myhead, _Rmost() = _Myhead; // empty tree
  745. }
  746. _Nodeptr _Copy(_Nodeptr _Rootnode, _Nodeptr _Wherenode)
  747. { // copy entire subtree, recursively
  748. _Nodeptr _Newroot = _Myhead; // point at nil node
  749. if (!_Isnil(_Rootnode))
  750. { // copy a node, then any subtrees
  751. _Nodeptr _Pnode = _Buynode(_Myhead, _Wherenode, _Myhead,
  752. _Myval(_Rootnode), _Color(_Rootnode));
  753. if (_Isnil(_Newroot))
  754. _Newroot = _Pnode; // memorize new root
  755. _TRY_BEGIN
  756. _Left(_Pnode) = _Copy(_Left(_Rootnode), _Pnode);
  757. _Right(_Pnode) = _Copy(_Right(_Rootnode), _Pnode);
  758. _CATCH_ALL
  759. _Erase(_Newroot); // subtree copy failed, bail out
  760. _RERAISE;
  761. _CATCH_END
  762. }
  763. return (_Newroot); // return newly constructed tree
  764. }
  765. void _Erase(_Nodeptr _Rootnode)
  766. { // free entire subtree, recursively
  767. for (_Nodeptr _Pnode = _Rootnode; !_Isnil(_Pnode); _Rootnode = _Pnode)
  768. { // free subtrees, then node
  769. _Erase(_Right(_Pnode));
  770. _Pnode = _Left(_Pnode);
  771. this->_Alnod.destroy(_Rootnode); // destroy, free erased node
  772. this->_Alnod.deallocate(_Rootnode, 1);
  773. }
  774. }
  775. void _Init()
  776. { // create head/nil node and make tree empty
  777. _Myhead = _Buynode();
  778. _Isnil(_Myhead) = true;
  779. _Root() = _Myhead;
  780. _Lmost() = _Myhead, _Rmost() = _Myhead;
  781. _Mysize = 0;
  782. }
  783. iterator _Insert(bool _Addleft, _Nodeptr _Wherenode,
  784. const value_type& _Val)
  785. { // add node with value next to _Wherenode, to left if _Addnode
  786. if (max_size() - 1 <= _Mysize)
  787. _THROW(length_error, "map/set<T> too long");
  788. _Nodeptr _Newnode = _Buynode(_Myhead, _Wherenode, _Myhead,
  789. _Val, _Red);
  790. ++_Mysize;
  791. if (_Wherenode == _Myhead)
  792. { // first node in tree, just set head values
  793. _Root() = _Newnode;
  794. _Lmost() = _Newnode, _Rmost() = _Newnode;
  795. }
  796. else if (_Addleft)
  797. { // add to left of _Wherenode
  798. _Left(_Wherenode) = _Newnode;
  799. if (_Wherenode == _Lmost())
  800. _Lmost() = _Newnode;
  801. }
  802. else
  803. { // add to right of _Wherenode
  804. _Right(_Wherenode) = _Newnode;
  805. if (_Wherenode == _Rmost())
  806. _Rmost() = _Newnode;
  807. }
  808. for (_Nodeptr _Pnode = _Newnode; _Color(_Parent(_Pnode)) == _Red; )
  809. if (_Parent(_Pnode) == _Left(_Parent(_Parent(_Pnode))))
  810. { // fixup red-red in left subtree
  811. _Wherenode = _Right(_Parent(_Parent(_Pnode)));
  812. if (_Color(_Wherenode) == _Red)
  813. { // parent has two red children, blacken both
  814. _Color(_Parent(_Pnode)) = _Black;
  815. _Color(_Wherenode) = _Black;
  816. _Color(_Parent(_Parent(_Pnode))) = _Red;
  817. _Pnode = _Parent(_Parent(_Pnode));
  818. }
  819. else
  820. { // parent has red and black children
  821. if (_Pnode == _Right(_Parent(_Pnode)))
  822. { // rotate right child to left
  823. _Pnode = _Parent(_Pnode);
  824. _Lrotate(_Pnode);
  825. }
  826. _Color(_Parent(_Pnode)) = _Black; // propagate red up
  827. _Color(_Parent(_Parent(_Pnode))) = _Red;
  828. _Rrotate(_Parent(_Parent(_Pnode)));
  829. }
  830. }
  831. else
  832. { // fixup red-red in right subtree
  833. _Wherenode = _Left(_Parent(_Parent(_Pnode)));
  834. if (_Color(_Wherenode) == _Red)
  835. { // parent has two red children, blacken both
  836. _Color(_Parent(_Pnode)) = _Black;
  837. _Color(_Wherenode) = _Black;
  838. _Color(_Parent(_Parent(_Pnode))) = _Red;
  839. _Pnode = _Parent(_Parent(_Pnode));
  840. }
  841. else
  842. { // parent has red and black children
  843. if (_Pnode == _Left(_Parent(_Pnode)))
  844. { // rotate left child to right
  845. _Pnode = _Parent(_Pnode);
  846. _Rrotate(_Pnode);
  847. }
  848. _Color(_Parent(_Pnode)) = _Black; // propagate red up
  849. _Color(_Parent(_Parent(_Pnode))) = _Red;
  850. _Lrotate(_Parent(_Parent(_Pnode)));
  851. }
  852. }
  853. _Color(_Root()) = _Black; // root is always black
  854. return (iterator(_Newnode));
  855. }
  856. _Nodeptr _Lbound(const key_type& _Keyval) const
  857. { // find leftmost node not less than _Keyval
  858. _Nodeptr _Pnode = _Root();
  859. _Nodeptr _Wherenode = _Myhead; // end() if search fails
  860. while (!_Isnil(_Pnode))
  861. if (this->comp(_Key(_Pnode), _Keyval))
  862. _Pnode = _Right(_Pnode); // descend right subtree
  863. else
  864. { // _Pnode not less than _Keyval, remember it
  865. _Wherenode = _Pnode;
  866. _Pnode = _Left(_Pnode); // descend left subtree
  867. }
  868. return (_Wherenode); // return best remembered candidate
  869. }
  870. _Nodeptr& _Lmost()
  871. { // return leftmost node in mutable tree
  872. return (_Left(_Myhead));
  873. }
  874. _Nodeptr& _Lmost() const
  875. { // return leftmost node in nonmutable tree
  876. return (_Left(_Myhead));
  877. }
  878. void _Lrotate(_Nodeptr _Wherenode)
  879. { // promote right node to root of subtree
  880. _Nodeptr _Pnode = _Right(_Wherenode);
  881. _Right(_Wherenode) = _Left(_Pnode);
  882. if (!_Isnil(_Left(_Pnode)))
  883. _Parent(_Left(_Pnode)) = _Wherenode;
  884. _Parent(_Pnode) = _Parent(_Wherenode);
  885. if (_Wherenode == _Root())
  886. _Root() = _Pnode;
  887. else if (_Wherenode == _Left(_Parent(_Wherenode)))
  888. _Left(_Parent(_Wherenode)) = _Pnode;
  889. else
  890. _Right(_Parent(_Wherenode)) = _Pnode;
  891. _Left(_Pnode) = _Wherenode;
  892. _Parent(_Wherenode) = _Pnode;
  893. }
  894. static _Nodeptr _Max(_Nodeptr _Pnode)
  895. { // return rightmost node in subtree at _Pnode
  896. while (!_Isnil(_Right(_Pnode)))
  897. _Pnode = _Right(_Pnode);
  898. return (_Pnode);
  899. }
  900. static _Nodeptr _Min(_Nodeptr _Pnode)
  901. { // return leftmost node in subtree at _Pnode
  902. while (!_Isnil(_Left(_Pnode)))
  903. _Pnode = _Left(_Pnode);
  904. return (_Pnode);
  905. }
  906. _Nodeptr& _Rmost()
  907. { // return rightmost node in mutable tree
  908. return (_Right(_Myhead));
  909. }
  910. _Nodeptr& _Rmost() const
  911. { // return rightmost node in nonmutable tree
  912. return (_Right(_Myhead));
  913. }
  914. _Nodeptr& _Root()
  915. { // return root of mutable tree
  916. return (_Parent(_Myhead));
  917. }
  918. _Nodeptr& _Root() const
  919. { // return root of nonmutable tree
  920. return (_Parent(_Myhead));
  921. }
  922. void _Rrotate(_Nodeptr _Wherenode)
  923. { // promote left node to root of subtree
  924. _Nodeptr _Pnode = _Left(_Wherenode);
  925. _Left(_Wherenode) = _Right(_Pnode);
  926. if (!_Isnil(_Right(_Pnode)))
  927. _Parent(_Right(_Pnode)) = _Wherenode;
  928. _Parent(_Pnode) = _Parent(_Wherenode);
  929. if (_Wherenode == _Root())
  930. _Root() = _Pnode;
  931. else if (_Wherenode == _Right(_Parent(_Wherenode)))
  932. _Right(_Parent(_Wherenode)) = _Pnode;
  933. else
  934. _Left(_Parent(_Wherenode)) = _Pnode;
  935. _Right(_Pnode) = _Wherenode;
  936. _Parent(_Wherenode) = _Pnode;
  937. }
  938. _Nodeptr _Ubound(const key_type& _Keyval) const
  939. { // find leftmost node greater than _Keyval
  940. _Nodeptr _Pnode = _Root();
  941. _Nodeptr _Wherenode = _Myhead; // end() if search fails
  942. while (!_Isnil(_Pnode))
  943. if (this->comp(_Keyval, _Key(_Pnode)))
  944. { // _Pnode greater than _Keyval, remember it
  945. _Wherenode = _Pnode;
  946. _Pnode = _Left(_Pnode); // descend left subtree
  947. }
  948. else
  949. _Pnode = _Right(_Pnode); // descend right subtree
  950. return (_Wherenode); // return best remembered candidate
  951. }
  952. _Nodeptr _Buynode()
  953. { // allocate a head/nil node
  954. _Nodeptr _Wherenode = this->_Alnod.allocate(1, (void *)0);
  955. int _Linkcnt = 0;
  956. _TRY_BEGIN
  957. this->_Alptr.construct(&_Left(_Wherenode), 0);
  958. ++_Linkcnt;
  959. this->_Alptr.construct(&_Parent(_Wherenode), 0);
  960. ++_Linkcnt;
  961. this->_Alptr.construct(&_Right(_Wherenode), 0);
  962. _CATCH_ALL
  963. if (1 < _Linkcnt)
  964. this->_Alptr.destroy(&_Parent(_Wherenode));
  965. if (0 < _Linkcnt)
  966. this->_Alptr.destroy(&_Left(_Wherenode));
  967. this->_Alnod.deallocate(_Wherenode, 1);
  968. _RERAISE;
  969. _CATCH_END
  970. _Color(_Wherenode) = _Black;
  971. _Isnil(_Wherenode) = false;
  972. return (_Wherenode);
  973. }
  974. _Nodeptr _Buynode(_Nodeptr _Larg, _Nodeptr _Parg, _Nodeptr _Rarg,
  975. const value_type& _Val, char _Carg)
  976. { // allocate a node with pointers, value, and color
  977. _Nodeptr _Wherenode = this->_Alnod.allocate(1, (void *)0);
  978. _TRY_BEGIN
  979. new ((void *)_Wherenode) _Node(_Larg, _Parg, _Rarg, _Val, _Carg);
  980. _CATCH_ALL
  981. this->_Alnod.deallocate(_Wherenode, 1);
  982. _RERAISE;
  983. _CATCH_END
  984. return (_Wherenode);
  985. }
  986. void _Tidy()
  987. { // free all storage
  988. erase(begin(), end());
  989. this->_Alptr.destroy(&_Left(_Myhead));
  990. this->_Alptr.destroy(&_Parent(_Myhead));
  991. this->_Alptr.destroy(&_Right(_Myhead));
  992. this->_Alnod.deallocate(_Myhead, 1);
  993. _Myhead = 0, _Mysize = 0;
  994. }
  995. _Nodeptr _Myhead; // pointer to head node
  996. size_type _Mysize; // number of elements
  997. };
  998. // _Tree TEMPLATE FUNCTIONS
  999. template<class _Traits> inline
  1000. bool operator==(const _Tree<_Traits>& _Left, const _Tree<_Traits>& _Right)
  1001. { // test for _Tree equality
  1002. return (_Left.size() == _Right.size()
  1003. && equal(_Left.begin(), _Left.end(), _Right.begin()));
  1004. }
  1005. template<class _Traits> inline
  1006. bool operator!=(const _Tree<_Traits>& _Left, const _Tree<_Traits>& _Right)
  1007. { // test for _Tree inequality
  1008. return (!(_Left == _Right));
  1009. }
  1010. template<class _Traits> inline
  1011. bool operator<(const _Tree<_Traits>& _Left, const _Tree<_Traits>& _Right)
  1012. { // test if _Less < _Right for _Trees
  1013. return (lexicographical_compare(_Left.begin(), _Left.end(),
  1014. _Right.begin(), _Right.end()));
  1015. }
  1016. template<class _Traits> inline
  1017. bool operator>(const _Tree<_Traits>& _Left, const _Tree<_Traits>& _Right)
  1018. { // test if _Less > _Right for _Trees
  1019. return (_Right < _Left);
  1020. }
  1021. template<class _Traits> inline
  1022. bool operator<=(const _Tree<_Traits>& _Left, const _Tree<_Traits>& _Right)
  1023. { // test if _Less <= _Right for _Trees
  1024. return (!(_Right < _Left));
  1025. }
  1026. template<class _Traits> inline
  1027. bool operator>=(const _Tree<_Traits>& _Left, const _Tree<_Traits>& _Right)
  1028. { // test if _Less >= _Right for _Trees
  1029. return (!(_Left < _Right));
  1030. }
  1031. template<class _Traits> inline
  1032. void swap(_Tree<_Traits>& _Left, _Tree<_Traits>& _Right)
  1033. { // swap _Left and _Right trees
  1034. _Left.swap(_Right);
  1035. }
  1036. _STD_END
  1037. #pragma warning(default:4127)
  1038. #pragma warning(pop)
  1039. #pragma pack(pop)
  1040. #endif /* _XTREE_ */
  1041. /*
  1042. * Copyright (c) 1992-2001 by P.J. Plauger. ALL RIGHTS RESERVED.
  1043. * Consult your license regarding permissions and restrictions.
  1044. */
  1045. /*
  1046. * This file is derived from software bearing the following
  1047. * restrictions:
  1048. *
  1049. * Copyright (c) 1994
  1050. * Hewlett-Packard Company
  1051. *
  1052. * Permission to use, copy, modify, distribute and sell this
  1053. * software and its documentation for any purpose is hereby
  1054. * granted without fee, provided that the above copyright notice
  1055. * appear in all copies and that both that copyright notice and
  1056. * this permission notice appear in supporting documentation.
  1057. * Hewlett-Packard Company makes no representations about the
  1058. * suitability of this software for any purpose. It is provided
  1059. * "as is" without express or implied warranty.
  1060. V3.10:0009 */