_unordered_map.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * Copyright (c) 2004
  3. * Francois Dumont
  4. *
  5. * This material is provided "as is", with absolutely no warranty expressed
  6. * or implied. Any use is at your own risk.
  7. *
  8. * Permission to use or copy this software for any purpose is hereby granted
  9. * without fee, provided the above notices are retained on all copies.
  10. * Permission to modify the code and to distribute modified code is granted,
  11. * provided the above notices are retained, and a notice that the code was
  12. * modified is included with the above copyright notice.
  13. *
  14. */
  15. /* NOTE: This is an internal header file, included by other STL headers.
  16. * You should not attempt to use it directly.
  17. */
  18. #ifndef _STLP_INTERNAL_UNORDERED_MAP_H
  19. #define _STLP_INTERNAL_UNORDERED_MAP_H
  20. #ifndef _STLP_INTERNAL_HASHTABLE_H
  21. # include <stl/_hashtable.h>
  22. #endif
  23. _STLP_BEGIN_NAMESPACE
  24. //Specific iterator traits creation
  25. _STLP_CREATE_HASH_ITERATOR_TRAITS(UnorderedMapTraitsT, traits)
  26. _STLP_BEGIN_TR1_NAMESPACE
  27. template <class _Key, class _Tp, _STLP_DFL_TMPL_PARAM(_HashFcn,hash<_Key>),
  28. _STLP_DFL_TMPL_PARAM(_EqualKey, equal_to<_Key>),
  29. _STLP_DEFAULT_PAIR_ALLOCATOR_SELECT(_STLP_CONST _Key, _Tp) >
  30. class unordered_map
  31. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
  32. : public __stlport_class<unordered_map<_Key, _Tp, _HashFcn, _EqualKey, _Alloc> >
  33. #endif
  34. {
  35. private:
  36. typedef unordered_map<_Key, _Tp, _HashFcn, _EqualKey, _Alloc> _Self;
  37. public:
  38. typedef _Key key_type;
  39. typedef _Tp data_type;
  40. typedef _Tp mapped_type;
  41. typedef pair<_STLP_CONST key_type, data_type> value_type;
  42. private:
  43. //Specific iterator traits creation
  44. typedef _STLP_PRIV _UnorderedMapTraitsT<value_type> _UnorderedMapTraits;
  45. public:
  46. typedef hashtable<value_type, key_type, _HashFcn, _UnorderedMapTraits,
  47. _STLP_SELECT1ST(value_type, _Key), _EqualKey, _Alloc > _Ht;
  48. typedef typename _Ht::hasher hasher;
  49. typedef typename _Ht::key_equal key_equal;
  50. typedef typename _Ht::size_type size_type;
  51. typedef typename _Ht::difference_type difference_type;
  52. typedef typename _Ht::pointer pointer;
  53. typedef typename _Ht::const_pointer const_pointer;
  54. typedef typename _Ht::reference reference;
  55. typedef typename _Ht::const_reference const_reference;
  56. typedef typename _Ht::iterator iterator;
  57. typedef typename _Ht::const_iterator const_iterator;
  58. typedef typename _Ht::local_iterator local_iterator;
  59. typedef typename _Ht::const_local_iterator const_local_iterator;
  60. typedef typename _Ht::allocator_type allocator_type;
  61. hasher hash_function() const { return _M_ht.hash_funct(); }
  62. key_equal key_eq() const { return _M_ht.key_eq(); }
  63. allocator_type get_allocator() const { return _M_ht.get_allocator(); }
  64. private:
  65. _Ht _M_ht;
  66. _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
  67. public:
  68. explicit unordered_map(size_type __n = 0, const hasher& __hf = hasher(),
  69. const key_equal& __eql = key_equal(),
  70. const allocator_type& __a = allocator_type())
  71. : _M_ht(__n, __hf, __eql, __a) {}
  72. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  73. unordered_map(__move_source<_Self> src)
  74. : _M_ht(__move_source<_Ht>(src.get()._M_ht)) {}
  75. #endif
  76. #if defined (_STLP_MEMBER_TEMPLATES)
  77. template <class _InputIterator>
  78. unordered_map(_InputIterator __f, _InputIterator __l,
  79. size_type __n = 0, const hasher& __hf = hasher(),
  80. const key_equal& __eql = key_equal(),
  81. const allocator_type& __a = allocator_type())
  82. : _M_ht(__n, __hf, __eql, __a)
  83. { _M_ht.insert_unique(__f, __l); }
  84. #else
  85. unordered_map(const value_type* __f, const value_type* __l,
  86. size_type __n = 0, const hasher& __hf = hasher(),
  87. const key_equal& __eql = key_equal(),
  88. const allocator_type& __a = allocator_type())
  89. : _M_ht(__n, __hf, __eql, __a)
  90. { _M_ht.insert_unique(__f, __l); }
  91. unordered_map(const_iterator __f, const_iterator __l,
  92. size_type __n = 0, const hasher& __hf = hasher(),
  93. const key_equal& __eql = key_equal(),
  94. const allocator_type& __a = allocator_type())
  95. : _M_ht(__n, __hf, __eql, __a)
  96. { _M_ht.insert_unique(__f, __l); }
  97. #endif /*_STLP_MEMBER_TEMPLATES */
  98. _Self& operator = (const _Self& __other)
  99. { _M_ht = __other._M_ht; return *this; }
  100. size_type size() const { return _M_ht.size(); }
  101. size_type max_size() const { return _M_ht.max_size(); }
  102. bool empty() const { return _M_ht.empty(); }
  103. void swap(_Self& __hs) { _M_ht.swap(__hs._M_ht); }
  104. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  105. void _M_swap_workaround(_Self& __x) { swap(__x); }
  106. #endif
  107. iterator begin() { return _M_ht.begin(); }
  108. iterator end() { return _M_ht.end(); }
  109. const_iterator begin() const { return _M_ht.begin(); }
  110. const_iterator end() const { return _M_ht.end(); }
  111. pair<iterator,bool> insert(const value_type& __obj)
  112. { return _M_ht.insert_unique(__obj); }
  113. iterator insert(const_iterator /*__hint*/, const value_type& __obj)
  114. { return _M_ht.insert_unique(__obj); }
  115. #if defined (_STLP_MEMBER_TEMPLATES)
  116. template <class _InputIterator>
  117. void insert(_InputIterator __f, _InputIterator __l)
  118. #else
  119. void insert(const value_type* __f, const value_type* __l)
  120. { _M_ht.insert_unique(__f,__l); }
  121. void insert(const_iterator __f, const_iterator __l)
  122. #endif /*_STLP_MEMBER_TEMPLATES */
  123. { _M_ht.insert_unique(__f, __l); }
  124. _STLP_TEMPLATE_FOR_CONT_EXT
  125. iterator find(const _KT& __key) { return _M_ht.find(__key); }
  126. _STLP_TEMPLATE_FOR_CONT_EXT
  127. const_iterator find(const _KT& __key) const { return _M_ht.find(__key); }
  128. _STLP_TEMPLATE_FOR_CONT_EXT
  129. _Tp& operator[](const _KT& __key) {
  130. iterator __it = _M_ht.find(__key);
  131. return (__it == _M_ht.end() ?
  132. _M_ht._M_insert(value_type(__key, _STLP_DEFAULT_CONSTRUCTED(_Tp))).second :
  133. (*__it).second );
  134. }
  135. _STLP_TEMPLATE_FOR_CONT_EXT
  136. size_type count(const _KT& __key) const { return _M_ht.count(__key); }
  137. _STLP_TEMPLATE_FOR_CONT_EXT
  138. pair<iterator, iterator> equal_range(const _KT& __key)
  139. { return _M_ht.equal_range(__key); }
  140. _STLP_TEMPLATE_FOR_CONT_EXT
  141. pair<const_iterator, const_iterator> equal_range(const _KT& __key) const
  142. { return _M_ht.equal_range(__key); }
  143. size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
  144. void erase(const_iterator __it) { _M_ht.erase(__it); }
  145. void erase(const_iterator __f, const_iterator __l) { _M_ht.erase(__f, __l); }
  146. void clear() { _M_ht.clear(); }
  147. size_type bucket_count() const { return _M_ht.bucket_count(); }
  148. size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
  149. size_type bucket_size(size_type __n) const { return _M_ht.elems_in_bucket(__n); }
  150. _STLP_TEMPLATE_FOR_CONT_EXT
  151. size_type bucket(const _KT& __k) const { return _M_ht.bucket(__k); }
  152. local_iterator begin(size_type __n) { return _M_ht.begin(__n); }
  153. local_iterator end(size_type __n) { return _M_ht.end(__n); }
  154. const_local_iterator begin(size_type __n) const { return _M_ht.begin(__n); }
  155. const_local_iterator end(size_type __n) const { return _M_ht.end(__n); }
  156. float load_factor() const { return _M_ht.load_factor(); }
  157. float max_load_factor() const { return _M_ht.max_load_factor(); }
  158. void max_load_factor(float __val) { _M_ht.max_load_factor(__val); }
  159. void rehash(size_type __hint) { _M_ht.rehash(__hint); }
  160. #if defined (__DMC__) // disable operator==(pair<x,unordered_map>, pair<x,unordered_map>)
  161. bool operator==(const _Self&) const;
  162. #endif
  163. };
  164. _STLP_END_NAMESPACE
  165. //Specific iterator traits creation
  166. _STLP_CREATE_HASH_ITERATOR_TRAITS(UnorderedMultimapTraitsT, traits)
  167. _STLP_BEGIN_TR1_NAMESPACE
  168. template <class _Key, class _Tp, _STLP_DFL_TMPL_PARAM(_HashFcn,hash<_Key>),
  169. _STLP_DFL_TMPL_PARAM(_EqualKey, equal_to<_Key>),
  170. _STLP_DEFAULT_PAIR_ALLOCATOR_SELECT(_STLP_CONST _Key, _Tp) >
  171. class unordered_multimap
  172. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
  173. : public __stlport_class<unordered_multimap<_Key, _Tp, _HashFcn, _EqualKey, _Alloc> >
  174. #endif
  175. {
  176. private:
  177. typedef unordered_multimap<_Key, _Tp, _HashFcn, _EqualKey, _Alloc> _Self;
  178. public:
  179. typedef _Key key_type;
  180. typedef _Tp data_type;
  181. typedef _Tp mapped_type;
  182. typedef pair<_STLP_CONST key_type, data_type> value_type;
  183. private:
  184. //Specific iterator traits creation
  185. typedef _STLP_PRIV _UnorderedMultimapTraitsT<value_type> _UnorderedMultimapTraits;
  186. public:
  187. typedef hashtable<value_type, key_type, _HashFcn, _UnorderedMultimapTraits,
  188. _STLP_SELECT1ST(value_type, _Key), _EqualKey, _Alloc > _Ht;
  189. typedef typename _Ht::hasher hasher;
  190. typedef typename _Ht::key_equal key_equal;
  191. typedef typename _Ht::size_type size_type;
  192. typedef typename _Ht::difference_type difference_type;
  193. typedef typename _Ht::pointer pointer;
  194. typedef typename _Ht::const_pointer const_pointer;
  195. typedef typename _Ht::reference reference;
  196. typedef typename _Ht::const_reference const_reference;
  197. typedef typename _Ht::iterator iterator;
  198. typedef typename _Ht::const_iterator const_iterator;
  199. typedef typename _Ht::local_iterator local_iterator;
  200. typedef typename _Ht::const_local_iterator const_local_iterator;
  201. typedef typename _Ht::allocator_type allocator_type;
  202. hasher hash_function() const { return _M_ht.hash_funct(); }
  203. key_equal key_eq() const { return _M_ht.key_eq(); }
  204. allocator_type get_allocator() const { return _M_ht.get_allocator(); }
  205. private:
  206. _Ht _M_ht;
  207. _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
  208. public:
  209. explicit unordered_multimap(size_type __n = 0, const hasher& __hf = hasher(),
  210. const key_equal& __eql = key_equal(),
  211. const allocator_type& __a = allocator_type())
  212. : _M_ht(__n, __hf, __eql, __a) {}
  213. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  214. unordered_multimap(__move_source<_Self> src)
  215. : _M_ht(__move_source<_Ht>(src.get()._M_ht)) {}
  216. #endif
  217. #if defined (_STLP_MEMBER_TEMPLATES)
  218. template <class _InputIterator>
  219. unordered_multimap(_InputIterator __f, _InputIterator __l,
  220. size_type __n = 0, const hasher& __hf = hasher(),
  221. const key_equal& __eql = key_equal(),
  222. const allocator_type& __a = allocator_type())
  223. : _M_ht(__n, __hf, __eql, __a)
  224. { _M_ht.insert_equal(__f, __l); }
  225. #else
  226. unordered_multimap(const value_type* __f, const value_type* __l,
  227. size_type __n = 0, const hasher& __hf = hasher(),
  228. const key_equal& __eql = key_equal(),
  229. const allocator_type& __a = allocator_type())
  230. : _M_ht(__n, __hf, __eql, __a)
  231. { _M_ht.insert_equal(__f, __l); }
  232. unordered_multimap(const_iterator __f, const_iterator __l,
  233. size_type __n = 0, const hasher& __hf = hasher(),
  234. const key_equal& __eql = key_equal(),
  235. const allocator_type& __a = allocator_type())
  236. : _M_ht(__n, __hf, __eql, __a)
  237. { _M_ht.insert_equal(__f, __l); }
  238. #endif /*_STLP_MEMBER_TEMPLATES */
  239. _Self& operator = (const _Self& __other)
  240. { _M_ht = __other._M_ht; return *this; }
  241. size_type size() const { return _M_ht.size(); }
  242. size_type max_size() const { return _M_ht.max_size(); }
  243. bool empty() const { return _M_ht.empty(); }
  244. void swap(_Self& __hs) { _M_ht.swap(__hs._M_ht); }
  245. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  246. void _M_swap_workaround(_Self& __x) { swap(__x); }
  247. #endif
  248. iterator begin() { return _M_ht.begin(); }
  249. iterator end() { return _M_ht.end(); }
  250. const_iterator begin() const { return _M_ht.begin(); }
  251. const_iterator end() const { return _M_ht.end(); }
  252. iterator insert(const value_type& __obj)
  253. { return _M_ht.insert_equal(__obj); }
  254. iterator insert(const_iterator /*__hint*/, const value_type& __obj)
  255. { return _M_ht.insert_equal(__obj); }
  256. #if defined (_STLP_MEMBER_TEMPLATES)
  257. template <class _InputIterator>
  258. void insert(_InputIterator __f, _InputIterator __l)
  259. #else
  260. void insert(const value_type* __f, const value_type* __l)
  261. { _M_ht.insert_equal(__f,__l); }
  262. void insert(const_iterator __f, const_iterator __l)
  263. #endif /*_STLP_MEMBER_TEMPLATES */
  264. { _M_ht.insert_equal(__f, __l); }
  265. _STLP_TEMPLATE_FOR_CONT_EXT
  266. iterator find(const _KT& __key) { return _M_ht.find(__key); }
  267. _STLP_TEMPLATE_FOR_CONT_EXT
  268. const_iterator find(const _KT& __key) const { return _M_ht.find(__key); }
  269. _STLP_TEMPLATE_FOR_CONT_EXT
  270. size_type count(const _KT& __key) const { return _M_ht.count(__key); }
  271. _STLP_TEMPLATE_FOR_CONT_EXT
  272. pair<iterator, iterator> equal_range(const _KT& __key)
  273. { return _M_ht.equal_range(__key); }
  274. _STLP_TEMPLATE_FOR_CONT_EXT
  275. pair<const_iterator, const_iterator> equal_range(const _KT& __key) const
  276. { return _M_ht.equal_range(__key); }
  277. size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
  278. void erase(const_iterator __it) { _M_ht.erase(__it); }
  279. void erase(const_iterator __f, const_iterator __l) { _M_ht.erase(__f, __l); }
  280. void clear() { _M_ht.clear(); }
  281. size_type bucket_count() const { return _M_ht.bucket_count(); }
  282. size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
  283. size_type bucket_size(size_type __n) const { return _M_ht.elems_in_bucket(__n); }
  284. _STLP_TEMPLATE_FOR_CONT_EXT
  285. size_type bucket(const _KT& __k) const { return _M_ht.bucket(__k); }
  286. local_iterator begin(size_type __n) { return _M_ht.begin(__n); }
  287. local_iterator end(size_type __n) { return _M_ht.end(__n); }
  288. const_local_iterator begin(size_type __n) const { return _M_ht.begin(__n); }
  289. const_local_iterator end(size_type __n) const { return _M_ht.end(__n); }
  290. float load_factor() const { return _M_ht.load_factor(); }
  291. float max_load_factor() const { return _M_ht.max_load_factor(); }
  292. void max_load_factor(float __val) { _M_ht.max_load_factor(__val); }
  293. void rehash(size_type __hint) { _M_ht.rehash(__hint); }
  294. };
  295. #define _STLP_TEMPLATE_HEADER template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc>
  296. #define _STLP_TEMPLATE_CONTAINER unordered_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>
  297. #include <stl/_relops_hash_cont.h>
  298. #undef _STLP_TEMPLATE_CONTAINER
  299. #define _STLP_TEMPLATE_CONTAINER unordered_multimap<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>
  300. #include <stl/_relops_hash_cont.h>
  301. #undef _STLP_TEMPLATE_CONTAINER
  302. #undef _STLP_TEMPLATE_HEADER
  303. _STLP_END_NAMESPACE
  304. // Specialization of insert_iterator so that it will work for unordered_map
  305. // and unordered_multimap.
  306. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
  307. # if !defined (_STLP_NO_MOVE_SEMANTIC)
  308. template <class _Key, class _Tp, class _HashFn, class _EqKey, class _Alloc>
  309. struct __move_traits<_STLP_TR1 unordered_map<_Key, _Tp, _HashFn, _EqKey, _Alloc> > :
  310. _STLP_PRIV __move_traits_help<typename _STLP_TR1 unordered_map<_Key, _Tp, _HashFn, _EqKey, _Alloc>::_Ht>
  311. {};
  312. template <class _Key, class _Tp, class _HashFn, class _EqKey, class _Alloc>
  313. struct __move_traits<_STLP_TR1 unordered_multimap<_Key, _Tp, _HashFn, _EqKey, _Alloc> > :
  314. _STLP_PRIV __move_traits_help<typename _STLP_TR1 unordered_map<_Key, _Tp, _HashFn, _EqKey, _Alloc>::_Ht>
  315. {};
  316. # endif
  317. template <class _Key, class _Tp, class _HashFn, class _EqKey, class _Alloc>
  318. class insert_iterator<_STLP_TR1 unordered_map<_Key, _Tp, _HashFn, _EqKey, _Alloc> > {
  319. protected:
  320. typedef _STLP_TR1 unordered_map<_Key, _Tp, _HashFn, _EqKey, _Alloc> _Container;
  321. _Container* container;
  322. public:
  323. typedef _Container container_type;
  324. typedef output_iterator_tag iterator_category;
  325. typedef void value_type;
  326. typedef void difference_type;
  327. typedef void pointer;
  328. typedef void reference;
  329. insert_iterator(_Container& __x) : container(&__x) {}
  330. insert_iterator(_Container& __x, typename _Container::iterator)
  331. : container(&__x) {}
  332. insert_iterator<_Container>&
  333. operator=(const typename _Container::value_type& __val) {
  334. container->insert(__val);
  335. return *this;
  336. }
  337. insert_iterator<_Container>& operator*() { return *this; }
  338. insert_iterator<_Container>& operator++() { return *this; }
  339. insert_iterator<_Container>& operator++(int) { return *this; }
  340. };
  341. template <class _Key, class _Tp, class _HashFn, class _EqKey, class _Alloc>
  342. class insert_iterator<_STLP_TR1 unordered_multimap<_Key, _Tp, _HashFn, _EqKey, _Alloc> > {
  343. protected:
  344. typedef _STLP_TR1 unordered_multimap<_Key, _Tp, _HashFn, _EqKey, _Alloc> _Container;
  345. _Container* container;
  346. typename _Container::iterator iter;
  347. public:
  348. typedef _Container container_type;
  349. typedef output_iterator_tag iterator_category;
  350. typedef void value_type;
  351. typedef void difference_type;
  352. typedef void pointer;
  353. typedef void reference;
  354. insert_iterator(_Container& __x) : container(&__x) {}
  355. insert_iterator(_Container& __x, typename _Container::iterator)
  356. : container(&__x) {}
  357. insert_iterator<_Container>&
  358. operator=(const typename _Container::value_type& __val) {
  359. container->insert(__val);
  360. return *this;
  361. }
  362. insert_iterator<_Container>& operator*() { return *this; }
  363. insert_iterator<_Container>& operator++() { return *this; }
  364. insert_iterator<_Container>& operator++(int) { return *this; }
  365. };
  366. #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
  367. _STLP_END_NAMESPACE
  368. #endif /* _STLP_INTERNAL_UNORDERED_MAP_H */
  369. // Local Variables:
  370. // mode:C++
  371. // End: