/project/jni/stlport/stlport/stl/_unordered_map.h

https://github.com/aichunyu/FFPlayer · C Header · 427 lines · 334 code · 67 blank · 26 comment · 1 complexity · 6757058ec720dfc908097215edc72682 MD5 · raw file

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