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.

520 lines
13 KiB

  1. #pragma once
  2. #ifndef _STLMAP_H_
  3. #define _STLMAP_H_
  4. //#include <functional>
  5. //#include <xtree>
  6. #include <stlfunc.h>
  7. #include <stlxtree.h>
  8. #include <stlxutil.h>
  9. #ifdef _MSC_VER
  10. #pragma pack(push,8)
  11. #endif /* _MSC_VER */
  12. _STD_BEGIN
  13. // TEMPLATE CLASS map
  14. template<class _K, class _Ty, class _Pr = less<_K>, class _A = allocator<_Ty> >
  15. class map
  16. {
  17. public:
  18. typedef map<_K, _Ty, _Pr, _A> _Myt;
  19. typedef pair<const _K, _Ty> value_type;
  20. struct _Kfn : public unary_function<value_type, _K>
  21. {
  22. const _K& operator()(const value_type& _X) const
  23. {
  24. return (_X.first);
  25. }
  26. };
  27. class value_compare
  28. : public binary_function<value_type, value_type, bool>
  29. {
  30. friend class map<_K, _Ty, _Pr, _A>;
  31. public:
  32. bool operator()(const value_type& _X,
  33. const value_type& _Y) const
  34. {
  35. return (comp(_X.first, _Y.first));
  36. }
  37. _PROTECTED:
  38. value_compare(_Pr _Pred)
  39. : comp(_Pred)
  40. {
  41. }
  42. _Pr comp;
  43. };
  44. typedef _K key_type;
  45. typedef _Ty referent_type;
  46. typedef _Pr key_compare;
  47. typedef _A allocator_type;
  48. typedef _A::reference _Tref;
  49. typedef _Tree<_K, value_type, _Kfn, _Pr, _A> _Imp;
  50. typedef _Imp::size_type size_type;
  51. typedef _Imp::difference_type difference_type;
  52. typedef _Imp::reference reference;
  53. typedef _Imp::const_reference const_reference;
  54. typedef _Imp::iterator iterator;
  55. typedef _Imp::const_iterator const_iterator;
  56. typedef _Imp::reverse_iterator reverse_iterator;
  57. typedef _Imp::const_reverse_iterator const_reverse_iterator;
  58. typedef pair<iterator, bool> _Pairib;
  59. typedef pair<iterator, iterator> _Pairii;
  60. typedef pair<const_iterator, const_iterator> _Paircc;
  61. explicit map(const _Pr& _Pred = _Pr(), const _A& _Al = _A())
  62. : _Tr(_Pred, false, _Al)
  63. {
  64. }
  65. typedef const value_type *_It;
  66. map(_It _F, _It _L, const _Pr& _Pred = _Pr(),
  67. const _A& _Al = _A())
  68. : _Tr(_Pred, false, _Al)
  69. {
  70. for (; _F != _L; ++_F)
  71. _Tr.insert(*_F);
  72. }
  73. iterator begin()
  74. {
  75. return (_Tr.begin());
  76. }
  77. const_iterator begin() const
  78. {
  79. return (_Tr.begin());
  80. }
  81. iterator end()
  82. {
  83. return (_Tr.end());
  84. }
  85. const_iterator end() const
  86. {
  87. return (_Tr.end());
  88. }
  89. reverse_iterator rbegin()
  90. {
  91. return (_Tr.rbegin());
  92. }
  93. const_reverse_iterator rbegin() const
  94. {
  95. return (_Tr.rbegin());
  96. }
  97. reverse_iterator rend()
  98. {
  99. return (_Tr.rend());
  100. }
  101. const_reverse_iterator rend() const
  102. {
  103. return (_Tr.rend());
  104. }
  105. size_type size() const
  106. {
  107. return (_Tr.size());
  108. }
  109. size_type max_size() const
  110. {
  111. return (_Tr.max_size());
  112. }
  113. bool empty() const
  114. {
  115. return (_Tr.empty());
  116. }
  117. _A get_allocator() const
  118. {
  119. return (_Tr.get_allocator());
  120. }
  121. _Tref operator[](const key_type& _Kv)
  122. {
  123. iterator _P = insert(value_type(_Kv, _Ty())).first;
  124. return ((*_P).second);
  125. }
  126. _Pairib insert(const value_type& _X)
  127. {
  128. _Imp::_Pairib _Ans = _Tr.insert(_X);
  129. return (_Pairib(_Ans.first, _Ans.second));
  130. }
  131. iterator insert(iterator _P, const value_type& _X)
  132. {
  133. return (_Tr.insert((_Imp::iterator&)_P, _X));
  134. }
  135. void insert(_It _F, _It _L)
  136. {
  137. for (; _F != _L; ++_F)
  138. _Tr.insert(*_F);
  139. }
  140. iterator erase(iterator _P)
  141. {
  142. return (_Tr.erase((_Imp::iterator&)_P));
  143. }
  144. iterator erase(iterator _F, iterator _L)
  145. {
  146. return (_Tr.erase((_Imp::iterator&)_F,
  147. (_Imp::iterator&)_L));
  148. }
  149. size_type erase(const _K& _Kv)
  150. {
  151. return (_Tr.erase(_Kv));
  152. }
  153. void clear()
  154. {
  155. _Tr.clear();
  156. }
  157. void swap(_Myt& _X)
  158. {
  159. std::swap(_Tr, _X._Tr);
  160. }
  161. friend void swap(_Myt& _X, _Myt& _Y)
  162. {
  163. _X.swap(_Y);
  164. }
  165. key_compare key_comp() const
  166. {
  167. return (_Tr.key_comp());
  168. }
  169. value_compare value_comp() const
  170. {
  171. return (value_compare(_Tr.key_comp()));
  172. }
  173. iterator find(const _K& _Kv)
  174. {
  175. return (_Tr.find(_Kv));
  176. }
  177. const_iterator find(const _K& _Kv) const
  178. {
  179. return (_Tr.find(_Kv));
  180. }
  181. size_type count(const _K& _Kv) const
  182. {
  183. return (_Tr.count(_Kv));
  184. }
  185. iterator lower_bound(const _K& _Kv)
  186. {
  187. return (_Tr.lower_bound(_Kv));
  188. }
  189. const_iterator lower_bound(const _K& _Kv) const
  190. {
  191. return (_Tr.lower_bound(_Kv));
  192. }
  193. iterator upper_bound(const _K& _Kv)
  194. {
  195. return (_Tr.upper_bound(_Kv));
  196. }
  197. const_iterator upper_bound(const _K& _Kv) const
  198. {
  199. return (_Tr.upper_bound(_Kv));
  200. }
  201. _Pairii equal_range(const _K& _Kv)
  202. {
  203. return (_Tr.equal_range(_Kv));
  204. }
  205. _Paircc equal_range(const _K& _Kv) const
  206. {
  207. return (_Tr.equal_range(_Kv));
  208. }
  209. protected:
  210. _Imp _Tr;
  211. };
  212. // map TEMPLATE OPERATORS
  213. template<class _K, class _Ty, class _Pr, class _A> inline
  214. bool operator==(const map<_K, _Ty, _Pr, _A>& _X,
  215. const map<_K, _Ty, _Pr, _A>& _Y)
  216. {
  217. return (_X.size() == _Y.size()
  218. && equal(_X.begin(), _X.end(), _Y.begin()));
  219. }
  220. template<class _K, class _Ty, class _Pr, class _A> inline
  221. bool operator!=(const map<_K, _Ty, _Pr, _A>& _X,
  222. const map<_K, _Ty, _Pr, _A>& _Y)
  223. {
  224. return (!(_X == _Y));
  225. }
  226. template<class _K, class _Ty, class _Pr, class _A> inline
  227. bool operator<(const map<_K, _Ty, _Pr, _A>& _X,
  228. const map<_K, _Ty, _Pr, _A>& _Y)
  229. {
  230. return (lexicographical_compare(_X.begin(), _X.end(),
  231. _Y.begin(), _Y.end()));
  232. }
  233. template<class _K, class _Ty, class _Pr, class _A> inline
  234. bool operator>(const map<_K, _Ty, _Pr, _A>& _X,
  235. const map<_K, _Ty, _Pr, _A>& _Y)
  236. {
  237. return (_Y < _X);
  238. }
  239. template<class _K, class _Ty, class _Pr, class _A> inline
  240. bool operator<=(const map<_K, _Ty, _Pr, _A>& _X,
  241. const map<_K, _Ty, _Pr, _A>& _Y)
  242. {
  243. return (!(_Y < _X));
  244. }
  245. template<class _K, class _Ty, class _Pr, class _A> inline
  246. bool operator>=(const map<_K, _Ty, _Pr, _A>& _X,
  247. const map<_K, _Ty, _Pr, _A>& _Y)
  248. {
  249. return (!(_X < _Y));
  250. }
  251. // TEMPLATE CLASS multimap
  252. template<class _K, class _Ty, class _Pr = less<_K>,
  253. class _A = allocator<_Ty> >
  254. class multimap
  255. {
  256. public:
  257. typedef multimap<_K, _Ty, _Pr, _A> _Myt;
  258. typedef pair<const _K, _Ty> value_type;
  259. struct _Kfn : public unary_function<value_type, _K>
  260. {
  261. const _K& operator()(const value_type& _X) const
  262. {
  263. return (_X.first);
  264. }
  265. };
  266. class value_compare
  267. : public binary_function<value_type, value_type, bool>
  268. {
  269. friend class map<_K, _Ty, _Pr, _A>;
  270. public:
  271. bool operator()(const value_type& _X,
  272. const value_type& _Y) const
  273. {
  274. return (comp(_X.first, _Y.first));
  275. }
  276. _PROTECTED:
  277. value_compare(_Pr _Pred)
  278. : comp(_Pred)
  279. {
  280. }
  281. _Pr comp;
  282. };
  283. typedef _K key_type;
  284. typedef _Ty referent_type;
  285. typedef _Pr key_compare;
  286. typedef _A allocator_type;
  287. typedef _Tree<_K, value_type, _Kfn, _Pr, _A> _Imp;
  288. typedef _Imp::size_type size_type;
  289. typedef _Imp::difference_type difference_type;
  290. typedef _Imp::reference reference;
  291. typedef _Imp::const_reference const_reference;
  292. typedef _Imp::iterator iterator;
  293. typedef _Imp::const_iterator const_iterator;
  294. typedef _Imp::reverse_iterator reverse_iterator;
  295. typedef _Imp::const_reverse_iterator const_reverse_iterator;
  296. typedef pair<iterator, iterator> _Pairii;
  297. typedef pair<const_iterator, const_iterator> _Paircc;
  298. explicit multimap(const _Pr& _Pred = _Pr(),
  299. const _A& _Al = _A())
  300. : _Tr(_Pred, true, _Al)
  301. {
  302. }
  303. typedef const value_type *_It;
  304. multimap(_It _F, _It _L, const _Pr& _Pred = _Pr(),
  305. const _A& _Al = _A())
  306. : _Tr(_Pred, true, _Al)
  307. {
  308. for (; _F != _L; ++_F)
  309. _Tr.insert(*_F);
  310. }
  311. iterator begin()
  312. {
  313. return (_Tr.begin());
  314. }
  315. const_iterator begin() const
  316. {
  317. return (_Tr.begin());
  318. }
  319. iterator end()
  320. {
  321. return (_Tr.end());
  322. }
  323. const_iterator end() const
  324. {
  325. return (_Tr.end());
  326. }
  327. reverse_iterator rbegin()
  328. {
  329. return (_Tr.rbegin());
  330. }
  331. const_reverse_iterator rbegin() const
  332. {
  333. return (_Tr.rbegin());
  334. }
  335. reverse_iterator rend()
  336. {
  337. return (_Tr.rend());
  338. }
  339. const_reverse_iterator rend() const
  340. {
  341. return (_Tr.rend());
  342. }
  343. size_type size() const
  344. {
  345. return (_Tr.size());
  346. }
  347. size_type max_size() const
  348. {
  349. return (_Tr.max_size());
  350. }
  351. bool empty() const
  352. {
  353. return (_Tr.empty());
  354. }
  355. _A get_allocator() const
  356. {
  357. return (_Tr.get_allocator());
  358. }
  359. iterator insert(const value_type& _X)
  360. {
  361. return (_Tr.insert(_X).first);
  362. }
  363. iterator insert(iterator _P, const value_type& _X)
  364. {
  365. return (_Tr.insert((_Imp::iterator&)_P, _X));
  366. }
  367. void insert(_It _F, _It _L)
  368. {
  369. for (; _F != _L; ++_F)
  370. _Tr.insert(*_F);
  371. }
  372. iterator erase(iterator _P)
  373. {
  374. return (_Tr.erase((_Imp::iterator&)_P));
  375. }
  376. iterator erase(iterator _F, iterator _L)
  377. {
  378. return (_Tr.erase((_Imp::iterator&)_F,
  379. (_Imp::iterator&)_L));
  380. }
  381. size_type erase(const _K& _Kv = _K())
  382. {
  383. return (_Tr.erase(_Kv));
  384. }
  385. void clear()
  386. {
  387. _Tr.clear();
  388. }
  389. void swap(_Myt& _X)
  390. {
  391. std::swap(_Tr, _X._Tr);
  392. }
  393. friend void swap(_Myt& _X, _Myt& _Y)
  394. {
  395. _X.swap(_Y);
  396. }
  397. key_compare key_comp() const
  398. {
  399. return (_Tr.key_comp());
  400. }
  401. value_compare value_comp() const
  402. {
  403. return (value_compare(_Tr.key_comp()));
  404. }
  405. iterator find(const _K& _Kv)
  406. {
  407. return (_Tr.find(_Kv));
  408. }
  409. const_iterator find(const _K& _Kv) const
  410. {
  411. return (_Tr.find(_Kv));
  412. }
  413. size_type count(const _K& _Kv) const
  414. {
  415. return (_Tr.count(_Kv));
  416. }
  417. iterator lower_bound(const _K& _Kv)
  418. {
  419. return (_Tr.lower_bound(_Kv));
  420. }
  421. const_iterator lower_bound(const _K& _Kv) const
  422. {
  423. return (_Tr.lower_bound(_Kv));
  424. }
  425. iterator upper_bound(const _K& _Kv)
  426. {
  427. return (_Tr.upper_bound(_Kv));
  428. }
  429. const_iterator upper_bound(const _K& _Kv) const
  430. {
  431. return (_Tr.upper_bound(_Kv));
  432. }
  433. _Pairii equal_range(const _K& _Kv)
  434. {
  435. return (_Tr.equal_range(_Kv));
  436. }
  437. _Paircc equal_range(const _K& _Kv) const
  438. {
  439. return (_Tr.equal_range(_Kv));
  440. }
  441. protected:
  442. _Imp _Tr;
  443. };
  444. // multimap TEMPLATE OPERATORS
  445. template<class _K, class _Ty, class _Pr, class _A> inline
  446. bool operator==(const multimap<_K, _Ty, _Pr, _A>& _X,
  447. const multimap<_K, _Ty, _Pr, _A>& _Y)
  448. {
  449. return (_X.size() == _Y.size()
  450. && equal(_X.begin(), _X.end(), _Y.begin()));
  451. }
  452. template<class _K, class _Ty, class _Pr, class _A> inline
  453. bool operator!=(const multimap<_K, _Ty, _Pr, _A>& _X,
  454. const multimap<_K, _Ty, _Pr, _A>& _Y)
  455. {
  456. return (!(_X == _Y));
  457. }
  458. template<class _K, class _Ty, class _Pr, class _A> inline
  459. bool operator<(const multimap<_K, _Ty, _Pr, _A>& _X,
  460. const multimap<_K, _Ty, _Pr, _A>& _Y)
  461. {
  462. return (lexicographical_compare(_X.begin(), _X.end(),
  463. _Y.begin(), _Y.end()));
  464. }
  465. template<class _K, class _Ty, class _Pr, class _A> inline
  466. bool operator>(const multimap<_K, _Ty, _Pr, _A>& _X,
  467. const multimap<_K, _Ty, _Pr, _A>& _Y)
  468. {
  469. return (_Y < _X);
  470. }
  471. template<class _K, class _Ty, class _Pr, class _A> inline
  472. bool operator<=(const multimap<_K, _Ty, _Pr, _A>& _X,
  473. const multimap<_K, _Ty, _Pr, _A>& _Y)
  474. {
  475. return (!(_Y < _X));
  476. }
  477. template<class _K, class _Ty, class _Pr, class _A> inline
  478. bool operator>=(const multimap<_K, _Ty, _Pr, _A>& _X,
  479. const multimap<_K, _Ty, _Pr, _A>& _Y)
  480. {
  481. return (!(_X < _Y));
  482. }
  483. _STD_END
  484. #ifdef _MSC_VER
  485. #pragma pack(pop)
  486. #endif /* _MSC_VER */
  487. #endif /* _STLMAP_H_ */
  488. /*
  489. * Copyright (c) 1995 by P.J. Plauger. ALL RIGHTS RESERVED.
  490. * Consult your license regarding permissions and restrictions.
  491. */
  492. /*
  493. * This file is derived from software bearing the following
  494. * restrictions:
  495. *
  496. * Copyright (c) 1994
  497. * Hewlett-Packard Company
  498. *
  499. * Permission to use, copy, modify, distribute and sell this
  500. * software and its documentation for any purpose is hereby
  501. * granted without fee, provided that the above copyright notice
  502. * appear in all copies and that both that copyright notice and
  503. * this permission notice appear in supporting documentation.
  504. * Hewlett-Packard Company makes no representations about the
  505. * suitability of this software for any purpose. It is provided
  506. * "as is" without express or implied warranty.
  507. */