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.

243 lines
7.4 KiB

  1. // map standard header
  2. #pragma once
  3. #ifndef _MAP_
  4. #define _MAP_
  5. #include <xtree>
  6. #pragma pack(push,8)
  7. #pragma warning(push,3)
  8. _STD_BEGIN
  9. // TEMPLATE CLASS _Tmap_traits
  10. template<class _Kty, // key type
  11. class _Ty, // mapped type
  12. class _Pr, // comparator predicate type
  13. class _Alloc, // actual allocator type (should be value allocator)
  14. bool _Mfl> // true if multiple equivalent keys are permitted
  15. class _Tmap_traits
  16. { // traits required to make _Tree behave like a map
  17. public:
  18. typedef _Kty key_type;
  19. typedef pair<const _Kty, _Ty> value_type;
  20. typedef _Pr key_compare;
  21. typedef typename _Alloc::_TEMPLATE_MEMBER rebind<value_type>::other
  22. allocator_type;
  23. typedef _POINTER_X(value_type, allocator_type) _ITptr;
  24. typedef _REFERENCE_X(value_type, allocator_type) _IReft;
  25. enum
  26. { // make multi parameter visible as an enum constant
  27. _Multi = _Mfl};
  28. _Tmap_traits()
  29. : comp()
  30. { // construct with default comparator
  31. }
  32. _Tmap_traits(_Pr _Parg)
  33. : comp(_Parg)
  34. { // construct with specified comparator
  35. }
  36. class value_compare
  37. : public binary_function<value_type, value_type, bool>
  38. { // functor for comparing two element values
  39. friend class _Tmap_traits<_Kty, _Ty, _Pr, _Alloc, _Mfl>;
  40. public:
  41. bool operator()(const value_type& _Left,
  42. const value_type& _Right) const
  43. { // test if _Left precedes _Right by comparing just keys
  44. return (comp(_Left.first, _Right.first));
  45. }
  46. value_compare(key_compare _Pred)
  47. : comp(_Pred)
  48. { // construct with specified predicate
  49. }
  50. protected:
  51. key_compare comp; // the comparator predicate for keys
  52. };
  53. static const _Kty& _Kfn(const value_type& _Val)
  54. { // extract key from element value
  55. return (_Val.first);
  56. }
  57. _Pr comp; // the comparator predicate for keys
  58. };
  59. // TEMPLATE CLASS map
  60. template<class _Kty,
  61. class _Ty,
  62. class _Pr = less<_Kty>,
  63. class _Alloc = allocator<pair<const _Kty, _Ty> > >
  64. class map
  65. : public _Tree<_Tmap_traits<_Kty, _Ty, _Pr, _Alloc, false> >
  66. { // ordered red-black tree of {key, mapped} values, unique keys
  67. public:
  68. typedef map<_Kty, _Ty, _Pr, _Alloc> _Myt;
  69. typedef _Tree<_Tmap_traits<_Kty, _Ty, _Pr, _Alloc, false> > _Mybase;
  70. typedef _Kty key_type;
  71. typedef _Ty mapped_type;
  72. typedef _Ty referent_type; // retained
  73. typedef _Pr key_compare;
  74. typedef typename _Mybase::value_compare value_compare;
  75. typedef typename _Mybase::allocator_type allocator_type;
  76. typedef typename _Mybase::size_type size_type;
  77. typedef typename _Mybase::difference_type difference_type;
  78. typedef typename _Mybase::pointer pointer;
  79. typedef typename _Mybase::const_pointer const_pointer;
  80. typedef typename _Mybase::reference reference;
  81. typedef typename _Mybase::const_reference const_reference;
  82. typedef typename _Mybase::iterator iterator;
  83. typedef typename _Mybase::const_iterator const_iterator;
  84. typedef typename _Mybase::reverse_iterator reverse_iterator;
  85. typedef typename _Mybase::const_reverse_iterator
  86. const_reverse_iterator;
  87. typedef typename _Mybase::value_type value_type;
  88. map()
  89. : _Mybase(key_compare(), allocator_type())
  90. { // construct empty map from defaults
  91. }
  92. explicit map(const key_compare& _Pred)
  93. : _Mybase(_Pred, allocator_type())
  94. { // construct empty map from comparator
  95. }
  96. map(const key_compare& _Pred, const allocator_type& _Al)
  97. : _Mybase(_Pred, _Al)
  98. { // construct empty map from comparator and allocator
  99. }
  100. template<class _Iter>
  101. map(_Iter _First, _Iter _Last)
  102. : _Mybase(key_compare(), allocator_type())
  103. { // construct map from [_First, _Last), defaults
  104. for (; _First != _Last; ++_First)
  105. this->insert(*_First);
  106. }
  107. template<class _Iter>
  108. map(_Iter _First, _Iter _Last, const key_compare& _Pred)
  109. : _Mybase(_Pred, allocator_type())
  110. { // construct map from [_First, _Last), comparator
  111. for (; _First != _Last; ++_First)
  112. this->insert(*_First);
  113. }
  114. template<class _Iter>
  115. map(_Iter _First, _Iter _Last, const key_compare& _Pred,
  116. const allocator_type& _Al)
  117. : _Mybase(_Pred, _Al)
  118. { // construct map from [_First, _Last), comparator, and allocator
  119. for (; _First != _Last; ++_First)
  120. this->insert(*_First);
  121. }
  122. mapped_type& operator[](const key_type& _Keyval)
  123. { // find element matching _Keyval or insert with default mapped
  124. iterator _Where =
  125. insert(value_type(_Keyval, mapped_type())).first;
  126. return ((*_Where).second);
  127. }
  128. };
  129. // TEMPLATE CLASS multimap
  130. template<class _Kty,
  131. class _Ty,
  132. class _Pr = less<_Kty>,
  133. class _Alloc = allocator<pair<const _Kty, _Ty> > >
  134. class multimap
  135. : public _Tree<_Tmap_traits<_Kty, _Ty, _Pr, _Alloc, true> >
  136. { // ordered red-black tree of {key, mapped} values, non-unique keys
  137. public:
  138. typedef multimap<_Kty, _Ty, _Pr, _Alloc> _Myt;
  139. typedef _Tree<_Tmap_traits<_Kty, _Ty, _Pr, _Alloc, true> > _Mybase;
  140. typedef _Kty key_type;
  141. typedef _Ty mapped_type;
  142. typedef _Ty referent_type; // retained
  143. typedef _Pr key_compare;
  144. typedef typename _Mybase::value_compare value_compare;
  145. typedef typename _Mybase::allocator_type allocator_type;
  146. typedef typename _Mybase::size_type size_type;
  147. typedef typename _Mybase::difference_type difference_type;
  148. typedef typename _Mybase::pointer pointer;
  149. typedef typename _Mybase::const_pointer const_pointer;
  150. typedef typename _Mybase::reference reference;
  151. typedef typename _Mybase::const_reference const_reference;
  152. typedef typename _Mybase::iterator iterator;
  153. typedef typename _Mybase::const_iterator const_iterator;
  154. typedef typename _Mybase::reverse_iterator reverse_iterator;
  155. typedef typename _Mybase::const_reverse_iterator
  156. const_reverse_iterator;
  157. typedef typename _Mybase::value_type value_type;
  158. multimap()
  159. : _Mybase(key_compare(), allocator_type())
  160. { // construct empty map from defaults
  161. }
  162. explicit multimap(const key_compare& _Pred)
  163. : _Mybase(_Pred, allocator_type())
  164. { // construct empty map from comparator
  165. }
  166. multimap(const key_compare& _Pred, const allocator_type& _Al)
  167. : _Mybase(_Pred, _Al)
  168. { // construct empty map from comparator and allocator
  169. }
  170. template<class _Iter>
  171. multimap(_Iter _First, _Iter _Last)
  172. : _Mybase(key_compare(), allocator_type())
  173. { // construct map from [_First, _Last), defaults
  174. for (; _First != _Last; ++_First)
  175. insert(*_First);
  176. }
  177. template<class _Iter>
  178. multimap(_Iter _First, _Iter _Last, const key_compare& _Pred)
  179. : _Mybase(_Pred, allocator_type())
  180. { // construct map from [_First, _Last), comparator
  181. for (; _First != _Last; ++_First)
  182. insert(*_First);
  183. }
  184. template<class _Iter>
  185. multimap(_Iter _First, _Iter _Last, const key_compare& _Pred,
  186. const allocator_type& _Al)
  187. : _Mybase(_Pred, _Al)
  188. { // construct map from [_First, _Last), comparator, and allocator
  189. for (; _First != _Last; ++_First)
  190. insert(*_First);
  191. }
  192. iterator insert(const value_type& _Val)
  193. { // insert a {key, mapped} value
  194. return (_Mybase::insert(_Val).first);
  195. }
  196. iterator insert(iterator _Where, const value_type& _Val)
  197. { // insert a {key, mapped} value, with hint
  198. return (_Mybase::insert(_Where, _Val));
  199. }
  200. template<class _Iter>
  201. void insert(_Iter _First, _Iter _Last)
  202. { // insert [_First, _Last), arbitrary iterators
  203. for (; _First != _Last; ++_First)
  204. insert(*_First);
  205. }
  206. };
  207. _STD_END
  208. #pragma warning(pop)
  209. #pragma pack(pop)
  210. #endif /* _MAP_ */
  211. /*
  212. * Copyright (c) 1992-2001 by P.J. Plauger. ALL RIGHTS RESERVED.
  213. * Consult your license regarding permissions and restrictions.
  214. V3.10:0009 */