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.

244 lines
7.2 KiB

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