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.

655 lines
30 KiB

  1. // tree internal header
  2. #ifndef _TREE_
  3. #define _TREE_
  4. #include <cstddef>
  5. #include <iterator>
  6. #include <memory>
  7. #include <xutility>
  8. #ifdef _MSC_VER
  9. #pragma pack(push,8)
  10. #endif /* _MSC_VER */
  11. _STD_BEGIN
  12. // TEMPLATE CLASS _Tree
  13. template<class _K, class _Ty, class _Kfn, class _Pr, class _A>
  14. class _Tree {
  15. protected:
  16. typedef _POINTER_X(void, _A) _Genptr;
  17. enum _Redbl {_Red, _Black};
  18. struct _Node;
  19. friend struct _Node;
  20. struct _Node {
  21. _Genptr _Left, _Parent, _Right;
  22. _Ty _Value;
  23. _Redbl _Color;
  24. };
  25. typedef _POINTER_X(_Node, _A) _Nodeptr;
  26. typedef _REFERENCE_X(_Nodeptr, _A) _Nodepref;
  27. typedef _REFERENCE_X(const _K, _A) _Keyref;
  28. typedef _REFERENCE_X(_Redbl, _A) _Rbref;
  29. typedef _REFERENCE_X(_Ty, _A) _Vref;
  30. static _Rbref _Color(_Nodeptr _P)
  31. {return ((_Rbref)(*_P)._Color); }
  32. static _Keyref _Key(_Nodeptr _P)
  33. {return (_Kfn()(_Value(_P))); }
  34. static _Nodepref _Left(_Nodeptr _P)
  35. {return ((_Nodepref)(*_P)._Left); }
  36. static _Nodepref _Parent(_Nodeptr _P)
  37. {return ((_Nodepref)(*_P)._Parent); }
  38. static _Nodepref _Right(_Nodeptr _P)
  39. {return ((_Nodepref)(*_P)._Right); }
  40. static _Vref _Value(_Nodeptr _P)
  41. {return ((_Vref)(*_P)._Value); }
  42. public:
  43. typedef _Tree<_K, _Ty, _Kfn, _Pr, _A> _Myt;
  44. typedef _K key_type;
  45. typedef _Ty value_type;
  46. typedef typename _A::size_type size_type;
  47. typedef typename _A::difference_type difference_type;
  48. typedef _POINTER_X(_Ty, _A) _Tptr;
  49. typedef _POINTER_X(const _Ty, _A) _Ctptr;
  50. typedef _REFERENCE_X(_Ty, _A) reference;
  51. typedef _REFERENCE_X(const _Ty, _A) const_reference;
  52. // CLASS iterator
  53. class iterator;
  54. friend class iterator;
  55. class iterator : public _Bidit<_Ty, difference_type> {
  56. public:
  57. iterator()
  58. {}
  59. iterator(_Nodeptr _P)
  60. : _Ptr(_P) {}
  61. reference operator*() const
  62. {return (_Value(_Ptr)); }
  63. _Tptr operator->() const
  64. {return (&**this); }
  65. iterator& operator++()
  66. {_Inc();
  67. return (*this); }
  68. iterator operator++(int)
  69. {iterator _Tmp = *this;
  70. ++*this;
  71. return (_Tmp); }
  72. iterator& operator--()
  73. {_Dec();
  74. return (*this); }
  75. iterator operator--(int)
  76. {iterator _Tmp = *this;
  77. --*this;
  78. return (_Tmp); }
  79. bool operator==(const iterator& _X) const
  80. {return (_Ptr == _X._Ptr); }
  81. bool operator!=(const iterator& _X) const
  82. {return (!(*this == _X)); }
  83. void _Dec()
  84. {if (_Color(_Ptr) == _Red
  85. && _Parent(_Parent(_Ptr)) == _Ptr)
  86. _Ptr = _Right(_Ptr);
  87. else if (_Left(_Ptr) != _Nil)
  88. _Ptr = _Max(_Left(_Ptr));
  89. else
  90. {_Nodeptr _P;
  91. while (_Ptr == _Left(_P = _Parent(_Ptr)))
  92. _Ptr = _P;
  93. _Ptr = _P; }}
  94. void _Inc()
  95. {if (_Right(_Ptr) != _Nil)
  96. _Ptr = _Min(_Right(_Ptr));
  97. else
  98. {_Nodeptr _P;
  99. while (_Ptr == _Right(_P = _Parent(_Ptr)))
  100. _Ptr = _P;
  101. if (_Right(_Ptr) != _P)
  102. _Ptr = _P; }}
  103. _Nodeptr _Mynode() const
  104. {return (_Ptr); }
  105. protected:
  106. _Nodeptr _Ptr;
  107. };
  108. // CLASS const_iterator
  109. class const_iterator;
  110. friend class const_iterator;
  111. class const_iterator : public iterator {
  112. public:
  113. const_iterator()
  114. {}
  115. const_iterator(_Nodeptr _P)
  116. : iterator(_P) {}
  117. const_iterator(const iterator& _X)
  118. : iterator(_X) {}
  119. const_reference operator*() const
  120. {return (_Value(_Ptr)); }
  121. _Ctptr operator->() const
  122. {return (&**this); }
  123. const_iterator& operator++()
  124. {_Inc();
  125. return (*this); }
  126. const_iterator operator++(int)
  127. {iterator _Tmp = *this;
  128. ++*this;
  129. return (_Tmp); }
  130. const_iterator& operator--()
  131. {_Dec();
  132. return (*this); }
  133. const_iterator operator--(int)
  134. {iterator _Tmp = *this;
  135. --*this;
  136. return (_Tmp); }
  137. bool operator==(const const_iterator& _X) const
  138. {return (_Ptr == _X._Ptr); }
  139. bool operator!=(const const_iterator& _X) const
  140. {return (!(*this == _X)); }
  141. };
  142. typedef reverse_bidirectional_iterator<iterator,
  143. value_type, reference, _Tptr, difference_type>
  144. reverse_iterator;
  145. typedef reverse_bidirectional_iterator<const_iterator,
  146. value_type, const_reference, _Ctptr, difference_type>
  147. const_reverse_iterator;
  148. typedef pair<iterator, bool> _Pairib;
  149. typedef pair<iterator, iterator> _Pairii;
  150. typedef pair<const_iterator, const_iterator> _Paircc;
  151. explicit _Tree(const _Pr& _Parg, bool _Marg = true,
  152. const _A& _Al = _A())
  153. : allocator(_Al),
  154. key_compare(_Parg), _Multi(_Marg)
  155. {_Init(); }
  156. _Tree(const _Ty *_F, const _Ty *_L,
  157. const _Pr& _Parg, bool _Marg = true,
  158. const _A& _Al = _A())
  159. : allocator(_Al),
  160. key_compare(_Parg), _Multi(_Marg)
  161. {_Init();
  162. insert(_F, _L); }
  163. _Tree(const _Myt& _X)
  164. : allocator(_X.allocator),
  165. key_compare(_X.key_compare), _Multi(_X._Multi)
  166. {_Init();
  167. _Copy(_X); }
  168. ~_Tree()
  169. {erase(begin(), end());
  170. _Freenode(_Head);
  171. _Head = 0, _Size = 0;
  172. _Nodeptr _Tmp = 0;
  173. {_Lockit _Lk;
  174. if (--_Nilrefs == 0)
  175. {_Tmp = _Nil;
  176. _Nil = 0; }}
  177. if (_Tmp != 0)
  178. _Freenode(_Tmp); }
  179. _Myt& operator=(const _Myt& _X)
  180. {if (this != &_X)
  181. {erase(begin(), end());
  182. key_compare = _X.key_compare;
  183. _Copy(_X); }
  184. return (*this); }
  185. iterator begin()
  186. {return (iterator(_Lmost())); }
  187. const_iterator begin() const
  188. {return (const_iterator(_Lmost())); }
  189. iterator end()
  190. {return (iterator(_Head)); }
  191. const_iterator end() const
  192. {return (const_iterator(_Head)); }
  193. reverse_iterator rbegin()
  194. {return (reverse_iterator(end())); }
  195. const_reverse_iterator rbegin() const
  196. {return (const_reverse_iterator(end())); }
  197. reverse_iterator rend()
  198. {return (reverse_iterator(begin())); }
  199. const_reverse_iterator rend() const
  200. {return (const_reverse_iterator(begin())); }
  201. size_type size() const
  202. {return (_Size); }
  203. size_type max_size() const
  204. {return (allocator.max_size()); }
  205. bool empty() const
  206. {return (size() == 0); }
  207. _A get_allocator() const
  208. {return (allocator); }
  209. _Pr key_comp() const
  210. {return (key_compare); }
  211. _Pairib insert(const value_type& _V)
  212. {_Nodeptr _X = _Root();
  213. _Nodeptr _Y = _Head;
  214. bool _Ans = true;
  215. while (_X != _Nil)
  216. {_Y = _X;
  217. _Ans = key_compare(_Kfn()(_V), _Key(_X));
  218. _X = _Ans ? _Left(_X) : _Right(_X); }
  219. if (_Multi)
  220. return (_Pairib(_Insert(_X, _Y, _V), true));
  221. iterator _P = iterator(_Y);
  222. if (!_Ans)
  223. ;
  224. else if (_P == begin())
  225. return (_Pairib(_Insert(_X, _Y, _V), true));
  226. else
  227. --_P;
  228. if (key_compare(_Key(_P._Mynode()), _Kfn()(_V)))
  229. return (_Pairib(_Insert(_X, _Y, _V), true));
  230. return (_Pairib(_P, false)); }
  231. iterator insert(iterator _P, const value_type& _V)
  232. {if (size() == 0)
  233. ;
  234. else if (_P == begin())
  235. {if (key_compare(_Kfn()(_V), _Key(_P._Mynode())))
  236. return (_Insert(_Head, _P._Mynode(), _V)); }
  237. else if (_P == end())
  238. {if (key_compare(_Key(_Rmost()), _Kfn()(_V)))
  239. return (_Insert(_Nil, _Rmost(), _V)); }
  240. else
  241. {iterator _Pb = _P;
  242. if (key_compare(_Key((--_Pb)._Mynode()), _Kfn()(_V))
  243. && key_compare(_Kfn()(_V), _Key(_P._Mynode())))
  244. {if (_Right(_Pb._Mynode()) == _Nil)
  245. return (_Insert(_Nil, _Pb._Mynode(), _V));
  246. else
  247. return (_Insert(_Head, _P._Mynode(), _V)); }}
  248. return (insert(_V).first); }
  249. void insert(iterator _F, iterator _L)
  250. {for (; _F != _L; ++_F)
  251. insert(*_F); }
  252. void insert(const value_type *_F, const value_type *_L)
  253. {for (; _F != _L; ++_F)
  254. insert(*_F); }
  255. iterator erase(iterator _P)
  256. {_Nodeptr _X;
  257. _Nodeptr _Y = (_P++)._Mynode();
  258. _Nodeptr _Z = _Y;
  259. if (_Left(_Y) == _Nil)
  260. _X = _Right(_Y);
  261. else if (_Right(_Y) == _Nil)
  262. _X = _Left(_Y);
  263. else
  264. _Y = _Min(_Right(_Y)), _X = _Right(_Y);
  265. { _Lockit _Lk;
  266. if (_Y != _Z)
  267. {_Parent(_Left(_Z)) = _Y;
  268. _Left(_Y) = _Left(_Z);
  269. if (_Y == _Right(_Z))
  270. _Parent(_X) = _Y;
  271. else
  272. {_Parent(_X) = _Parent(_Y);
  273. _Left(_Parent(_Y)) = _X;
  274. _Right(_Y) = _Right(_Z);
  275. _Parent(_Right(_Z)) = _Y; }
  276. if (_Root() == _Z)
  277. _Root() = _Y;
  278. else if (_Left(_Parent(_Z)) == _Z)
  279. _Left(_Parent(_Z)) = _Y;
  280. else
  281. _Right(_Parent(_Z)) = _Y;
  282. _Parent(_Y) = _Parent(_Z);
  283. std::swap(_Color(_Y), _Color(_Z));
  284. _Y = _Z; }
  285. else
  286. {_Parent(_X) = _Parent(_Y);
  287. if (_Root() == _Z)
  288. _Root() = _X;
  289. else if (_Left(_Parent(_Z)) == _Z)
  290. _Left(_Parent(_Z)) = _X;
  291. else
  292. _Right(_Parent(_Z)) = _X;
  293. if (_Lmost() != _Z)
  294. ;
  295. else if (_Right(_Z) == _Nil)
  296. _Lmost() = _Parent(_Z);
  297. else
  298. _Lmost() = _Min(_X);
  299. if (_Rmost() != _Z)
  300. ;
  301. else if (_Left(_Z) == _Nil)
  302. _Rmost() = _Parent(_Z);
  303. else
  304. _Rmost() = _Max(_X); }
  305. if (_Color(_Y) == _Black)
  306. {while (_X != _Root() && _Color(_X) == _Black)
  307. if (_X == _Left(_Parent(_X)))
  308. {_Nodeptr _W = _Right(_Parent(_X));
  309. if (_Color(_W) == _Red)
  310. {_Color(_W) = _Black;
  311. _Color(_Parent(_X)) = _Red;
  312. _Lrotate(_Parent(_X));
  313. _W = _Right(_Parent(_X)); }
  314. if (_Color(_Left(_W)) == _Black
  315. && _Color(_Right(_W)) == _Black)
  316. {_Color(_W) = _Red;
  317. _X = _Parent(_X); }
  318. else
  319. {if (_Color(_Right(_W)) == _Black)
  320. {_Color(_Left(_W)) = _Black;
  321. _Color(_W) = _Red;
  322. _Rrotate(_W);
  323. _W = _Right(_Parent(_X)); }
  324. _Color(_W) = _Color(_Parent(_X));
  325. _Color(_Parent(_X)) = _Black;
  326. _Color(_Right(_W)) = _Black;
  327. _Lrotate(_Parent(_X));
  328. break; }}
  329. else
  330. {_Nodeptr _W = _Left(_Parent(_X));
  331. if (_Color(_W) == _Red)
  332. {_Color(_W) = _Black;
  333. _Color(_Parent(_X)) = _Red;
  334. _Rrotate(_Parent(_X));
  335. _W = _Left(_Parent(_X)); }
  336. if (_Color(_Right(_W)) == _Black
  337. && _Color(_Left(_W)) == _Black)
  338. {_Color(_W) = _Red;
  339. _X = _Parent(_X); }
  340. else
  341. {if (_Color(_Left(_W)) == _Black)
  342. {_Color(_Right(_W)) = _Black;
  343. _Color(_W) = _Red;
  344. _Lrotate(_W);
  345. _W = _Left(_Parent(_X)); }
  346. _Color(_W) = _Color(_Parent(_X));
  347. _Color(_Parent(_X)) = _Black;
  348. _Color(_Left(_W)) = _Black;
  349. _Rrotate(_Parent(_X));
  350. break; }}
  351. _Color(_X) = _Black; }
  352. }
  353. _Destval(&_Value(_Y));
  354. _Freenode(_Y);
  355. --_Size;
  356. return (_P); }
  357. iterator erase(iterator _F, iterator _L)
  358. {if (size() == 0 || _F != begin() || _L != end())
  359. {while (_F != _L)
  360. erase(_F++);
  361. return (_F); }
  362. else
  363. {_Erase(_Root());
  364. _Root() = _Nil, _Size = 0;
  365. _Lmost() = _Head, _Rmost() = _Head;
  366. return (begin()); }}
  367. size_type erase(const _K& _X)
  368. {_Pairii _P = equal_range(_X);
  369. size_type _N = 0;
  370. _Distance(_P.first, _P.second, _N);
  371. erase(_P.first, _P.second);
  372. return (_N); }
  373. void erase(const _K *_F, const _K *_L)
  374. {for (; _F != _L; ++_F)
  375. erase(*_F); }
  376. void clear()
  377. {erase(begin(), end()); }
  378. iterator find(const _K& _Kv)
  379. {iterator _P = lower_bound(_Kv);
  380. return (_P == end()
  381. || key_compare(_Kv, _Key(_P._Mynode()))
  382. ? end() : _P); }
  383. const_iterator find(const _K& _Kv) const
  384. {const_iterator _P = lower_bound(_Kv);
  385. return (_P == end()
  386. || key_compare(_Kv, _Key(_P._Mynode()))
  387. ? end() : _P); }
  388. size_type count(const _K& _Kv) const
  389. {_Paircc _Ans = equal_range(_Kv);
  390. size_type _N = 0;
  391. _Distance(_Ans.first, _Ans.second, _N);
  392. return (_N); }
  393. iterator lower_bound(const _K& _Kv)
  394. {return (iterator(_Lbound(_Kv))); }
  395. const_iterator lower_bound(const _K& _Kv) const
  396. {return (const_iterator(_Lbound(_Kv))); }
  397. iterator upper_bound(const _K& _Kv)
  398. {return (iterator(_Ubound(_Kv))); }
  399. const_iterator upper_bound(const _K& _Kv) const
  400. {return (iterator(_Ubound(_Kv))); }
  401. _Pairii equal_range(const _K& _Kv)
  402. {return (_Pairii(lower_bound(_Kv), upper_bound(_Kv))); }
  403. _Paircc equal_range(const _K& _Kv) const
  404. {return (_Paircc(lower_bound(_Kv), upper_bound(_Kv))); }
  405. void swap(_Myt& _X)
  406. {std::swap(key_compare, _X.key_compare);
  407. if (allocator == _X.allocator)
  408. {std::swap(_Head, _X._Head);
  409. std::swap(_Multi, _X._Multi);
  410. std::swap(_Size, _X._Size); }
  411. else
  412. {_Myt _Ts = *this; *this = _X, _X = _Ts; }}
  413. friend void swap(_Myt& _X, _Myt& _Y)
  414. {_X.swap(_Y); }
  415. protected:
  416. static _Nodeptr _Nil;
  417. static size_t _Nilrefs;
  418. void _Copy(const _Myt& _X)
  419. {_Root() = _Copy(_X._Root(), _Head);
  420. _Size = _X.size();
  421. if (_Root() != _Nil)
  422. {_Lmost() = _Min(_Root());
  423. _Rmost() = _Max(_Root()); }
  424. else
  425. _Lmost() = _Head, _Rmost() = _Head; }
  426. _Nodeptr _Copy(_Nodeptr _X, _Nodeptr _P)
  427. {_Nodeptr _R = _X;
  428. for (; _X != _Nil; _X = _Left(_X))
  429. {_Nodeptr _Y = _Buynode(_P, _Color(_X));
  430. if (_R == _X)
  431. _R = _Y;
  432. _Right(_Y) = _Copy(_Right(_X), _Y);
  433. _Consval(&_Value(_Y), _Value(_X));
  434. _Left(_P) = _Y;
  435. _P = _Y; }
  436. _Left(_P) = _Nil;
  437. return (_R); }
  438. void _Erase(_Nodeptr _X)
  439. {for (_Nodeptr _Y = _X; _Y != _Nil; _X = _Y)
  440. {_Erase(_Right(_Y));
  441. _Y = _Left(_Y);
  442. _Destval(&_Value(_X));
  443. _Freenode(_X); }}
  444. void _Init()
  445. {_Nodeptr _Tmp = _Buynode(0, _Black);
  446. {_Lockit _Lk;
  447. if (_Nil == 0)
  448. {_Nil = _Tmp;
  449. _Tmp = 0;
  450. _Left(_Nil) = 0, _Right(_Nil) = 0; }
  451. ++_Nilrefs; }
  452. if (_Tmp != 0)
  453. _Freenode(_Tmp);
  454. _Head = _Buynode(_Nil, _Red), _Size = 0;
  455. _Lmost() = _Head, _Rmost() = _Head; }
  456. iterator _Insert(_Nodeptr _X, _Nodeptr _Y, const _Ty& _V)
  457. {_Nodeptr _Z = _Buynode(_Y, _Red);
  458. _Left(_Z) = _Nil, _Right(_Z) = _Nil;
  459. _Consval(&_Value(_Z), _V);
  460. ++_Size;
  461. if (_Y == _Head || _X != _Nil
  462. || key_compare(_Kfn()(_V), _Key(_Y)))
  463. {_Left(_Y) = _Z;
  464. if (_Y == _Head)
  465. {_Root() = _Z;
  466. _Rmost() = _Z; }
  467. else if (_Y == _Lmost())
  468. _Lmost() = _Z; }
  469. else
  470. {_Right(_Y) = _Z;
  471. if (_Y == _Rmost())
  472. _Rmost() = _Z; }
  473. for (_X = _Z; _X != _Root()
  474. && _Color(_Parent(_X)) == _Red; )
  475. if (_Parent(_X) == _Left(_Parent(_Parent(_X))))
  476. {_Y = _Right(_Parent(_Parent(_X)));
  477. if (_Color(_Y) == _Red)
  478. {_Color(_Parent(_X)) = _Black;
  479. _Color(_Y) = _Black;
  480. _Color(_Parent(_Parent(_X))) = _Red;
  481. _X = _Parent(_Parent(_X)); }
  482. else
  483. {if (_X == _Right(_Parent(_X)))
  484. {_X = _Parent(_X);
  485. _Lrotate(_X); }
  486. _Color(_Parent(_X)) = _Black;
  487. _Color(_Parent(_Parent(_X))) = _Red;
  488. _Rrotate(_Parent(_Parent(_X))); }}
  489. else
  490. {_Y = _Left(_Parent(_Parent(_X)));
  491. if (_Color(_Y) == _Red)
  492. {_Color(_Parent(_X)) = _Black;
  493. _Color(_Y) = _Black;
  494. _Color(_Parent(_Parent(_X))) = _Red;
  495. _X = _Parent(_Parent(_X)); }
  496. else
  497. {if (_X == _Left(_Parent(_X)))
  498. {_X = _Parent(_X);
  499. _Rrotate(_X); }
  500. _Color(_Parent(_X)) = _Black;
  501. _Color(_Parent(_Parent(_X))) = _Red;
  502. _Lrotate(_Parent(_Parent(_X))); }}
  503. _Color(_Root()) = _Black;
  504. return (iterator(_Z)); }
  505. _Nodeptr _Lbound(const _K& _Kv) const
  506. {_Nodeptr _X = _Root();
  507. _Nodeptr _Y = _Head;
  508. while (_X != _Nil)
  509. if (key_compare(_Key(_X), _Kv))
  510. _X = _Right(_X);
  511. else
  512. _Y = _X, _X = _Left(_X);
  513. return (_Y); }
  514. _Nodeptr& _Lmost()
  515. {return (_Left(_Head)); }
  516. _Nodeptr& _Lmost() const
  517. {return (_Left(_Head)); }
  518. void _Lrotate(_Nodeptr _X)
  519. {_Nodeptr _Y = _Right(_X);
  520. _Right(_X) = _Left(_Y);
  521. if (_Left(_Y) != _Nil)
  522. _Parent(_Left(_Y)) = _X;
  523. _Parent(_Y) = _Parent(_X);
  524. if (_X == _Root())
  525. _Root() = _Y;
  526. else if (_X == _Left(_Parent(_X)))
  527. _Left(_Parent(_X)) = _Y;
  528. else
  529. _Right(_Parent(_X)) = _Y;
  530. _Left(_Y) = _X;
  531. _Parent(_X) = _Y; }
  532. static _Nodeptr _Max(_Nodeptr _P)
  533. {while (_Right(_P) != _Nil)
  534. _P = _Right(_P);
  535. return (_P); }
  536. static _Nodeptr _Min(_Nodeptr _P)
  537. {while (_Left(_P) != _Nil)
  538. _P = _Left(_P);
  539. return (_P); }
  540. _Nodeptr& _Rmost()
  541. {return (_Right(_Head)); }
  542. _Nodeptr& _Rmost() const
  543. {return (_Right(_Head)); }
  544. _Nodeptr& _Root()
  545. {return (_Parent(_Head)); }
  546. _Nodeptr& _Root() const
  547. {return (_Parent(_Head)); }
  548. void _Rrotate(_Nodeptr _X)
  549. {_Nodeptr _Y = _Left(_X);
  550. _Left(_X) = _Right(_Y);
  551. if (_Right(_Y) != _Nil)
  552. _Parent(_Right(_Y)) = _X;
  553. _Parent(_Y) = _Parent(_X);
  554. if (_X == _Root())
  555. _Root() = _Y;
  556. else if (_X == _Right(_Parent(_X)))
  557. _Right(_Parent(_X)) = _Y;
  558. else
  559. _Left(_Parent(_X)) = _Y;
  560. _Right(_Y) = _X;
  561. _Parent(_X) = _Y; }
  562. _Nodeptr _Ubound(const _K& _Kv) const
  563. {_Nodeptr _X = _Root();
  564. _Nodeptr _Y = _Head;
  565. while (_X != _Nil)
  566. if (key_compare(_Kv, _Key(_X)))
  567. _Y = _X, _X = _Left(_X);
  568. else
  569. _X = _Right(_X);
  570. return (_Y); }
  571. _Nodeptr _Buynode(_Nodeptr _Parg, _Redbl _Carg)
  572. {_Nodeptr _S = (_Nodeptr)allocator._Charalloc(
  573. 1 * sizeof (_Node));
  574. _Parent(_S) = _Parg;
  575. _Color(_S) = _Carg;
  576. return (_S); }
  577. void _Consval(_Tptr _P, const _Ty& _V)
  578. {_Construct(&*_P, _V); }
  579. void _Destval(_Tptr _P)
  580. {_Destroy(&*_P); }
  581. void _Freenode(_Nodeptr _S)
  582. {allocator.deallocate(_S, 1); }
  583. _A allocator;
  584. _Pr key_compare;
  585. _Nodeptr _Head;
  586. bool _Multi;
  587. size_type _Size;
  588. };
  589. template<class _K, class _Ty, class _Kfn, class _Pr, class _A>
  590. typename _Tree<_K, _Ty, _Kfn, _Pr, _A>::_Nodeptr
  591. _Tree<_K, _Ty, _Kfn, _Pr, _A>::_Nil = 0;
  592. template<class _K, class _Ty, class _Kfn, class _Pr, class _A>
  593. size_t _Tree<_K, _Ty, _Kfn, _Pr, _A>::_Nilrefs = 0;
  594. // tree TEMPLATE OPERATORS
  595. template<class _K, class _Ty, class _Kfn,
  596. class _Pr, class _A> inline
  597. bool operator==(const _Tree<_K, _Ty, _Kfn, _Pr, _A>& _X,
  598. const _Tree<_K, _Ty, _Kfn, _Pr, _A>& _Y)
  599. {return (_X.size() == _Y.size()
  600. && equal(_X.begin(), _X.end(), _Y.begin())); }
  601. template<class _K, class _Ty, class _Kfn,
  602. class _Pr, class _A> inline
  603. bool operator!=(const _Tree<_K, _Ty, _Kfn, _Pr, _A>& _X,
  604. const _Tree<_K, _Ty, _Kfn, _Pr, _A>& _Y)
  605. {return (!(_X == _Y)); }
  606. template<class _K, class _Ty, class _Kfn,
  607. class _Pr, class _A> inline
  608. bool operator<(const _Tree<_K, _Ty, _Kfn, _Pr, _A>& _X,
  609. const _Tree<_K, _Ty, _Kfn, _Pr, _A>& _Y)
  610. {return (lexicographical_compare(_X.begin(), _X.end(),
  611. _Y.begin(), _Y.end())); }
  612. template<class _K, class _Ty, class _Kfn,
  613. class _Pr, class _A> inline
  614. bool operator>(const _Tree<_K, _Ty, _Kfn, _Pr, _A>& _X,
  615. const _Tree<_K, _Ty, _Kfn, _Pr, _A>& _Y)
  616. {return (_Y < _X); }
  617. template<class _K, class _Ty, class _Kfn,
  618. class _Pr, class _A> inline
  619. bool operator<=(const _Tree<_K, _Ty, _Kfn, _Pr, _A>& _X,
  620. const _Tree<_K, _Ty, _Kfn, _Pr, _A>& _Y)
  621. {return (!(_Y < _X)); }
  622. template<class _K, class _Ty, class _Kfn,
  623. class _Pr, class _A> inline
  624. bool operator>=(const _Tree<_K, _Ty, _Kfn, _Pr, _A>& _X,
  625. const _Tree<_K, _Ty, _Kfn, _Pr, _A>& _Y)
  626. {return (!(_X < _Y)); }
  627. _STD_END
  628. #ifdef _MSC_VER
  629. #pragma pack(pop)
  630. #endif /* _MSC_VER */
  631. #endif /* _TREE_ */
  632. /*
  633. * Copyright (c) 1995 by P.J. Plauger. ALL RIGHTS RESERVED.
  634. * Consult your license regarding permissions and restrictions.
  635. */
  636. /*
  637. * This file is derived from software bearing the following
  638. * restrictions:
  639. *
  640. * Copyright (c) 1994
  641. * Hewlett-Packard Company
  642. *
  643. * Permission to use, copy, modify, distribute and sell this
  644. * software and its documentation for any purpose is hereby
  645. * granted without fee, provided that the above copyright notice
  646. * appear in all copies and that both that copyright notice and
  647. * this permission notice appear in supporting documentation.
  648. * Hewlett-Packard Company makes no representations about the
  649. * suitability of this software for any purpose. It is provided
  650. * "as is" without express or implied warranty.
  651. */