hashtable.hxx.in 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
  3. /*
  4. * Copyright (c) 1996
  5. * Silicon Graphics Computer Systems, Inc.
  6. *
  7. * Permission to use, copy, modify, distribute and sell this software
  8. * and its documentation for any purpose is hereby granted without fee,
  9. * provided that the above copyright notice appear in all copies and
  10. * that both that copyright notice and this permission notice appear
  11. * in supporting documentation. Silicon Graphics makes no
  12. * representations about the suitability of this software for any
  13. * purpose. It is provided "as is" without express or implied warranty.
  14. *
  15. *
  16. * Copyright (c) 1994
  17. * Hewlett-Packard Company
  18. *
  19. * Permission to use, copy, modify, distribute and sell this software
  20. * and its documentation for any purpose is hereby granted without fee,
  21. * provided that the above copyright notice appear in all copies and
  22. * that both that copyright notice and this permission notice appear
  23. * in supporting documentation. Hewlett-Packard Company makes no
  24. * representations about the suitability of this software for any
  25. * purpose. It is provided "as is" without express or implied warranty.
  26. *
  27. */
  28. #ifdef __BORLANDC__
  29. #pragma warn - 8027 /* 'for' not inlined. */
  30. #pragma warn - 8026 /* 'exception' not inlined. */
  31. #endif
  32. #ifndef @KWSYS_NAMESPACE@_hashtable_hxx
  33. #define @KWSYS_NAMESPACE@_hashtable_hxx
  34. #include <@KWSYS_NAMESPACE@/Configure.hxx>
  35. #include <algorithm> // lower_bound
  36. #include <iterator> // iterator_traits
  37. #include <memory> // allocator
  38. #include <stddef.h> // size_t
  39. #include <utility> // pair
  40. #include <vector> // vector
  41. #if defined(_MSC_VER)
  42. #pragma warning(push)
  43. #pragma warning(disable : 4284)
  44. #pragma warning(disable : 4786)
  45. #pragma warning(disable : 4512) /* no assignment operator for class */
  46. #endif
  47. #if defined(__sgi) && !defined(__GNUC__)
  48. #pragma set woff 3970 /* pointer to int conversion */ 3321 3968
  49. #endif
  50. // In C++11, clang will warn about using dynamic exception specifications
  51. // as they are deprecated. But as this class is trying to faithfully
  52. // mimic unordered_set and unordered_map, we want to keep the 'throw()'
  53. // decorations below. So we suppress the warning.
  54. #if defined(__clang__) && defined(__has_warning)
  55. #if __has_warning("-Wdeprecated")
  56. #pragma clang diagnostic push
  57. #pragma clang diagnostic ignored "-Wdeprecated"
  58. #endif
  59. #endif
  60. namespace @KWSYS_NAMESPACE@ {
  61. template <class _Val>
  62. struct _Hashtable_node
  63. {
  64. _Hashtable_node* _M_next;
  65. _Val _M_val;
  66. void public_method_to_quiet_warning_about_all_methods_private();
  67. private:
  68. void operator=(_Hashtable_node<_Val> const&); // poison node assignment
  69. };
  70. template <class _Val, class _Key, class _HashFcn, class _ExtractKey,
  71. class _EqualKey, class _Alloc = std::allocator<char> >
  72. class hashtable;
  73. template <class _Val, class _Key, class _HashFcn, class _ExtractKey,
  74. class _EqualKey, class _Alloc>
  75. struct _Hashtable_iterator;
  76. template <class _Val, class _Key, class _HashFcn, class _ExtractKey,
  77. class _EqualKey, class _Alloc>
  78. struct _Hashtable_const_iterator;
  79. template <class _Val, class _Key, class _HashFcn, class _ExtractKey,
  80. class _EqualKey, class _Alloc>
  81. struct _Hashtable_iterator
  82. {
  83. typedef hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>
  84. _Hashtable;
  85. typedef _Hashtable_iterator<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey,
  86. _Alloc>
  87. iterator;
  88. typedef _Hashtable_const_iterator<_Val, _Key, _HashFcn, _ExtractKey,
  89. _EqualKey, _Alloc>
  90. const_iterator;
  91. typedef _Hashtable_node<_Val> _Node;
  92. typedef std::forward_iterator_tag iterator_category;
  93. typedef _Val value_type;
  94. typedef ptrdiff_t difference_type;
  95. typedef size_t size_type;
  96. typedef _Val& reference;
  97. typedef _Val* pointer;
  98. _Node* _M_cur;
  99. _Hashtable* _M_ht;
  100. _Hashtable_iterator(_Node* __n, _Hashtable* __tab)
  101. : _M_cur(__n)
  102. , _M_ht(__tab)
  103. {
  104. }
  105. _Hashtable_iterator() {}
  106. reference operator*() const { return _M_cur->_M_val; }
  107. pointer operator->() const { return &(operator*()); }
  108. iterator& operator++();
  109. iterator operator++(int);
  110. bool operator==(const iterator& __it) const { return _M_cur == __it._M_cur; }
  111. bool operator!=(const iterator& __it) const { return _M_cur != __it._M_cur; }
  112. };
  113. template <class _Val, class _Key, class _HashFcn, class _ExtractKey,
  114. class _EqualKey, class _Alloc>
  115. struct _Hashtable_const_iterator
  116. {
  117. typedef hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>
  118. _Hashtable;
  119. typedef _Hashtable_iterator<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey,
  120. _Alloc>
  121. iterator;
  122. typedef _Hashtable_const_iterator<_Val, _Key, _HashFcn, _ExtractKey,
  123. _EqualKey, _Alloc>
  124. const_iterator;
  125. typedef _Hashtable_node<_Val> _Node;
  126. typedef std::forward_iterator_tag iterator_category;
  127. typedef _Val value_type;
  128. typedef ptrdiff_t difference_type;
  129. typedef size_t size_type;
  130. typedef const _Val& reference;
  131. typedef const _Val* pointer;
  132. const _Node* _M_cur;
  133. const _Hashtable* _M_ht;
  134. _Hashtable_const_iterator(const _Node* __n, const _Hashtable* __tab)
  135. : _M_cur(__n)
  136. , _M_ht(__tab)
  137. {
  138. }
  139. _Hashtable_const_iterator() {}
  140. _Hashtable_const_iterator(const iterator& __it)
  141. : _M_cur(__it._M_cur)
  142. , _M_ht(__it._M_ht)
  143. {
  144. }
  145. reference operator*() const { return _M_cur->_M_val; }
  146. pointer operator->() const { return &(operator*()); }
  147. const_iterator& operator++();
  148. const_iterator operator++(int);
  149. bool operator==(const const_iterator& __it) const
  150. {
  151. return _M_cur == __it._M_cur;
  152. }
  153. bool operator!=(const const_iterator& __it) const
  154. {
  155. return _M_cur != __it._M_cur;
  156. }
  157. };
  158. // Note: assumes long is at least 32 bits.
  159. enum
  160. {
  161. _stl_num_primes = 31
  162. };
  163. // create a function with a static local to that function that returns
  164. // the static
  165. static inline const unsigned long* get_stl_prime_list()
  166. {
  167. static const unsigned long _stl_prime_list[_stl_num_primes] = {
  168. 5ul, 11ul, 23ul, 53ul, 97ul,
  169. 193ul, 389ul, 769ul, 1543ul, 3079ul,
  170. 6151ul, 12289ul, 24593ul, 49157ul, 98317ul,
  171. 196613ul, 393241ul, 786433ul, 1572869ul, 3145739ul,
  172. 6291469ul, 12582917ul, 25165843ul, 50331653ul, 100663319ul,
  173. 201326611ul, 402653189ul, 805306457ul, 1610612741ul, 3221225473ul,
  174. 4294967291ul
  175. };
  176. return &_stl_prime_list[0];
  177. }
  178. static inline size_t _stl_next_prime(size_t __n)
  179. {
  180. const unsigned long* __first = get_stl_prime_list();
  181. const unsigned long* __last = get_stl_prime_list() + (int)_stl_num_primes;
  182. const unsigned long* pos = std::lower_bound(__first, __last, __n);
  183. return pos == __last ? *(__last - 1) : *pos;
  184. }
  185. // Forward declaration of operator==.
  186. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  187. class hashtable;
  188. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  189. bool operator==(const hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>& __ht1,
  190. const hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>& __ht2);
  191. // Hashtables handle allocators a bit differently than other containers
  192. // do. If we're using standard-conforming allocators, then a hashtable
  193. // unconditionally has a member variable to hold its allocator, even if
  194. // it so happens that all instances of the allocator type are identical.
  195. // This is because, for hashtables, this extra storage is negligible.
  196. // Additionally, a base class wouldn't serve any other purposes; it
  197. // wouldn't, for example, simplify the exception-handling code.
  198. template <class _Val, class _Key, class _HashFcn, class _ExtractKey,
  199. class _EqualKey, class _Alloc>
  200. class hashtable
  201. {
  202. public:
  203. typedef _Key key_type;
  204. typedef _Val value_type;
  205. typedef _HashFcn hasher;
  206. typedef _EqualKey key_equal;
  207. typedef size_t size_type;
  208. typedef ptrdiff_t difference_type;
  209. typedef value_type* pointer;
  210. typedef const value_type* const_pointer;
  211. typedef value_type& reference;
  212. typedef const value_type& const_reference;
  213. hasher hash_funct() const { return _M_hash; }
  214. key_equal key_eq() const { return _M_equals; }
  215. private:
  216. typedef _Hashtable_node<_Val> _Node;
  217. public:
  218. typedef typename _Alloc::template rebind<_Val>::other allocator_type;
  219. allocator_type get_allocator() const { return _M_node_allocator; }
  220. private:
  221. typedef
  222. typename _Alloc::template rebind<_Node>::other _M_node_allocator_type;
  223. typedef
  224. typename _Alloc::template rebind<_Node*>::other _M_node_ptr_allocator_type;
  225. typedef std::vector<_Node*, _M_node_ptr_allocator_type> _M_buckets_type;
  226. private:
  227. _M_node_allocator_type _M_node_allocator;
  228. hasher _M_hash;
  229. key_equal _M_equals;
  230. _ExtractKey _M_get_key;
  231. _M_buckets_type _M_buckets;
  232. size_type _M_num_elements;
  233. _Node* _M_get_node() { return _M_node_allocator.allocate(1); }
  234. void _M_put_node(_Node* __p) { _M_node_allocator.deallocate(__p, 1); }
  235. public:
  236. typedef _Hashtable_iterator<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey,
  237. _Alloc>
  238. iterator;
  239. typedef _Hashtable_const_iterator<_Val, _Key, _HashFcn, _ExtractKey,
  240. _EqualKey, _Alloc>
  241. const_iterator;
  242. friend struct _Hashtable_iterator<_Val, _Key, _HashFcn, _ExtractKey,
  243. _EqualKey, _Alloc>;
  244. friend struct _Hashtable_const_iterator<_Val, _Key, _HashFcn, _ExtractKey,
  245. _EqualKey, _Alloc>;
  246. public:
  247. hashtable(size_type __n, const _HashFcn& __hf, const _EqualKey& __eql,
  248. const _ExtractKey& __ext,
  249. const allocator_type& __a = allocator_type())
  250. : _M_node_allocator(__a)
  251. , _M_hash(__hf)
  252. , _M_equals(__eql)
  253. , _M_get_key(__ext)
  254. , _M_buckets(__a)
  255. , _M_num_elements(0)
  256. {
  257. _M_initialize_buckets(__n);
  258. }
  259. hashtable(size_type __n, const _HashFcn& __hf, const _EqualKey& __eql,
  260. const allocator_type& __a = allocator_type())
  261. : _M_node_allocator(__a)
  262. , _M_hash(__hf)
  263. , _M_equals(__eql)
  264. , _M_get_key(_ExtractKey())
  265. , _M_buckets(__a)
  266. , _M_num_elements(0)
  267. {
  268. _M_initialize_buckets(__n);
  269. }
  270. hashtable(const hashtable& __ht)
  271. : _M_node_allocator(__ht.get_allocator())
  272. , _M_hash(__ht._M_hash)
  273. , _M_equals(__ht._M_equals)
  274. , _M_get_key(__ht._M_get_key)
  275. , _M_buckets(__ht.get_allocator())
  276. , _M_num_elements(0)
  277. {
  278. _M_copy_from(__ht);
  279. }
  280. hashtable& operator=(const hashtable& __ht)
  281. {
  282. if (&__ht != this) {
  283. clear();
  284. _M_hash = __ht._M_hash;
  285. _M_equals = __ht._M_equals;
  286. _M_get_key = __ht._M_get_key;
  287. _M_copy_from(__ht);
  288. }
  289. return *this;
  290. }
  291. ~hashtable() { clear(); }
  292. size_type size() const { return _M_num_elements; }
  293. size_type max_size() const { return size_type(-1); }
  294. bool empty() const { return size() == 0; }
  295. void swap(hashtable& __ht)
  296. {
  297. std::swap(_M_hash, __ht._M_hash);
  298. std::swap(_M_equals, __ht._M_equals);
  299. std::swap(_M_get_key, __ht._M_get_key);
  300. _M_buckets.swap(__ht._M_buckets);
  301. std::swap(_M_num_elements, __ht._M_num_elements);
  302. }
  303. iterator begin()
  304. {
  305. for (size_type __n = 0; __n < _M_buckets.size(); ++__n)
  306. if (_M_buckets[__n])
  307. return iterator(_M_buckets[__n], this);
  308. return end();
  309. }
  310. iterator end() { return iterator(0, this); }
  311. const_iterator begin() const
  312. {
  313. for (size_type __n = 0; __n < _M_buckets.size(); ++__n)
  314. if (_M_buckets[__n])
  315. return const_iterator(_M_buckets[__n], this);
  316. return end();
  317. }
  318. const_iterator end() const { return const_iterator(0, this); }
  319. friend bool operator==<>(const hashtable&, const hashtable&);
  320. public:
  321. size_type bucket_count() const { return _M_buckets.size(); }
  322. size_type max_bucket_count() const
  323. {
  324. return get_stl_prime_list()[(int)_stl_num_primes - 1];
  325. }
  326. size_type elems_in_bucket(size_type __bucket) const
  327. {
  328. size_type __result = 0;
  329. for (_Node* __cur = _M_buckets[__bucket]; __cur; __cur = __cur->_M_next)
  330. __result += 1;
  331. return __result;
  332. }
  333. std::pair<iterator, bool> insert_unique(const value_type& __obj)
  334. {
  335. resize(_M_num_elements + 1);
  336. return insert_unique_noresize(__obj);
  337. }
  338. iterator insert_equal(const value_type& __obj)
  339. {
  340. resize(_M_num_elements + 1);
  341. return insert_equal_noresize(__obj);
  342. }
  343. std::pair<iterator, bool> insert_unique_noresize(const value_type& __obj);
  344. iterator insert_equal_noresize(const value_type& __obj);
  345. template <class _InputIterator>
  346. void insert_unique(_InputIterator __f, _InputIterator __l)
  347. {
  348. insert_unique(
  349. __f, __l,
  350. typename std::iterator_traits<_InputIterator>::iterator_category());
  351. }
  352. template <class _InputIterator>
  353. void insert_equal(_InputIterator __f, _InputIterator __l)
  354. {
  355. insert_equal(
  356. __f, __l,
  357. typename std::iterator_traits<_InputIterator>::iterator_category());
  358. }
  359. template <class _InputIterator>
  360. void insert_unique(_InputIterator __f, _InputIterator __l,
  361. std::input_iterator_tag)
  362. {
  363. for (; __f != __l; ++__f)
  364. insert_unique(*__f);
  365. }
  366. template <class _InputIterator>
  367. void insert_equal(_InputIterator __f, _InputIterator __l,
  368. std::input_iterator_tag)
  369. {
  370. for (; __f != __l; ++__f)
  371. insert_equal(*__f);
  372. }
  373. template <class _ForwardIterator>
  374. void insert_unique(_ForwardIterator __f, _ForwardIterator __l,
  375. std::forward_iterator_tag)
  376. {
  377. size_type __n = 0;
  378. std::distance(__f, __l, __n);
  379. resize(_M_num_elements + __n);
  380. for (; __n > 0; --__n, ++__f)
  381. insert_unique_noresize(*__f);
  382. }
  383. template <class _ForwardIterator>
  384. void insert_equal(_ForwardIterator __f, _ForwardIterator __l,
  385. std::forward_iterator_tag)
  386. {
  387. size_type __n = 0;
  388. std::distance(__f, __l, __n);
  389. resize(_M_num_elements + __n);
  390. for (; __n > 0; --__n, ++__f)
  391. insert_equal_noresize(*__f);
  392. }
  393. reference find_or_insert(const value_type& __obj);
  394. iterator find(const key_type& __key)
  395. {
  396. size_type __n = _M_bkt_num_key(__key);
  397. _Node* __first;
  398. for (__first = _M_buckets[__n];
  399. __first && !_M_equals(_M_get_key(__first->_M_val), __key);
  400. __first = __first->_M_next) {
  401. }
  402. return iterator(__first, this);
  403. }
  404. const_iterator find(const key_type& __key) const
  405. {
  406. size_type __n = _M_bkt_num_key(__key);
  407. const _Node* __first;
  408. for (__first = _M_buckets[__n];
  409. __first && !_M_equals(_M_get_key(__first->_M_val), __key);
  410. __first = __first->_M_next) {
  411. }
  412. return const_iterator(__first, this);
  413. }
  414. size_type count(const key_type& __key) const
  415. {
  416. const size_type __n = _M_bkt_num_key(__key);
  417. size_type __result = 0;
  418. for (const _Node* __cur = _M_buckets[__n]; __cur; __cur = __cur->_M_next)
  419. if (_M_equals(_M_get_key(__cur->_M_val), __key))
  420. ++__result;
  421. return __result;
  422. }
  423. std::pair<iterator, iterator> equal_range(const key_type& __key);
  424. std::pair<const_iterator, const_iterator> equal_range(
  425. const key_type& __key) const;
  426. size_type erase(const key_type& __key);
  427. void erase(const iterator& __it);
  428. void erase(iterator __first, iterator __last);
  429. void erase(const const_iterator& __it);
  430. void erase(const_iterator __first, const_iterator __last);
  431. void resize(size_type __num_elements_hint);
  432. void clear();
  433. private:
  434. size_type _M_next_size(size_type __n) const { return _stl_next_prime(__n); }
  435. void _M_initialize_buckets(size_type __n)
  436. {
  437. const size_type __n_buckets = _M_next_size(__n);
  438. _M_buckets.reserve(__n_buckets);
  439. _M_buckets.insert(_M_buckets.end(), __n_buckets, (_Node*)0);
  440. _M_num_elements = 0;
  441. }
  442. size_type _M_bkt_num_key(const key_type& __key) const
  443. {
  444. return _M_bkt_num_key(__key, _M_buckets.size());
  445. }
  446. size_type _M_bkt_num(const value_type& __obj) const
  447. {
  448. return _M_bkt_num_key(_M_get_key(__obj));
  449. }
  450. size_type _M_bkt_num_key(const key_type& __key, size_t __n) const
  451. {
  452. return _M_hash(__key) % __n;
  453. }
  454. size_type _M_bkt_num(const value_type& __obj, size_t __n) const
  455. {
  456. return _M_bkt_num_key(_M_get_key(__obj), __n);
  457. }
  458. void construct(_Val* p, const _Val& v) { new (p) _Val(v); }
  459. void destroy(_Val* p)
  460. {
  461. (void)p;
  462. p->~_Val();
  463. }
  464. _Node* _M_new_node(const value_type& __obj)
  465. {
  466. _Node* __n = _M_get_node();
  467. __n->_M_next = 0;
  468. try {
  469. construct(&__n->_M_val, __obj);
  470. return __n;
  471. } catch (...) {
  472. _M_put_node(__n);
  473. throw;
  474. }
  475. }
  476. void _M_delete_node(_Node* __n)
  477. {
  478. destroy(&__n->_M_val);
  479. _M_put_node(__n);
  480. }
  481. void _M_erase_bucket(const size_type __n, _Node* __first, _Node* __last);
  482. void _M_erase_bucket(const size_type __n, _Node* __last);
  483. void _M_copy_from(const hashtable& __ht);
  484. };
  485. template <class _Val, class _Key, class _HF, class _ExK, class _EqK,
  486. class _All>
  487. _Hashtable_iterator<_Val, _Key, _HF, _ExK, _EqK, _All>&
  488. _Hashtable_iterator<_Val, _Key, _HF, _ExK, _EqK, _All>::operator++()
  489. {
  490. const _Node* __old = _M_cur;
  491. _M_cur = _M_cur->_M_next;
  492. if (!_M_cur) {
  493. size_type __bucket = _M_ht->_M_bkt_num(__old->_M_val);
  494. while (!_M_cur && ++__bucket < _M_ht->_M_buckets.size())
  495. _M_cur = _M_ht->_M_buckets[__bucket];
  496. }
  497. return *this;
  498. }
  499. template <class _Val, class _Key, class _HF, class _ExK, class _EqK,
  500. class _All>
  501. inline _Hashtable_iterator<_Val, _Key, _HF, _ExK, _EqK, _All>
  502. _Hashtable_iterator<_Val, _Key, _HF, _ExK, _EqK, _All>::operator++(int)
  503. {
  504. iterator __tmp = *this;
  505. ++*this;
  506. return __tmp;
  507. }
  508. template <class _Val, class _Key, class _HF, class _ExK, class _EqK,
  509. class _All>
  510. _Hashtable_const_iterator<_Val, _Key, _HF, _ExK, _EqK, _All>&
  511. _Hashtable_const_iterator<_Val, _Key, _HF, _ExK, _EqK, _All>::operator++()
  512. {
  513. const _Node* __old = _M_cur;
  514. _M_cur = _M_cur->_M_next;
  515. if (!_M_cur) {
  516. size_type __bucket = _M_ht->_M_bkt_num(__old->_M_val);
  517. while (!_M_cur && ++__bucket < _M_ht->_M_buckets.size())
  518. _M_cur = _M_ht->_M_buckets[__bucket];
  519. }
  520. return *this;
  521. }
  522. template <class _Val, class _Key, class _HF, class _ExK, class _EqK,
  523. class _All>
  524. inline _Hashtable_const_iterator<_Val, _Key, _HF, _ExK, _EqK, _All>
  525. _Hashtable_const_iterator<_Val, _Key, _HF, _ExK, _EqK, _All>::operator++(int)
  526. {
  527. const_iterator __tmp = *this;
  528. ++*this;
  529. return __tmp;
  530. }
  531. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  532. bool operator==(const hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>& __ht1,
  533. const hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>& __ht2)
  534. {
  535. typedef typename hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::_Node _Node;
  536. if (__ht1._M_buckets.size() != __ht2._M_buckets.size())
  537. return false;
  538. for (int __n = 0; __n < __ht1._M_buckets.size(); ++__n) {
  539. _Node* __cur1 = __ht1._M_buckets[__n];
  540. _Node* __cur2 = __ht2._M_buckets[__n];
  541. for (; __cur1 && __cur2 && __cur1->_M_val == __cur2->_M_val;
  542. __cur1 = __cur1->_M_next, __cur2 = __cur2->_M_next) {
  543. }
  544. if (__cur1 || __cur2)
  545. return false;
  546. }
  547. return true;
  548. }
  549. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  550. inline bool operator!=(const hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>& __ht1,
  551. const hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>& __ht2)
  552. {
  553. return !(__ht1 == __ht2);
  554. }
  555. template <class _Val, class _Key, class _HF, class _Extract, class _EqKey,
  556. class _All>
  557. inline void swap(hashtable<_Val, _Key, _HF, _Extract, _EqKey, _All>& __ht1,
  558. hashtable<_Val, _Key, _HF, _Extract, _EqKey, _All>& __ht2)
  559. {
  560. __ht1.swap(__ht2);
  561. }
  562. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  563. std::pair<typename hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::iterator, bool>
  564. hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::insert_unique_noresize(
  565. const value_type& __obj)
  566. {
  567. const size_type __n = _M_bkt_num(__obj);
  568. _Node* __first = _M_buckets[__n];
  569. for (_Node* __cur = __first; __cur; __cur = __cur->_M_next)
  570. if (_M_equals(_M_get_key(__cur->_M_val), _M_get_key(__obj)))
  571. return std::pair<iterator, bool>(iterator(__cur, this), false);
  572. _Node* __tmp = _M_new_node(__obj);
  573. __tmp->_M_next = __first;
  574. _M_buckets[__n] = __tmp;
  575. ++_M_num_elements;
  576. return std::pair<iterator, bool>(iterator(__tmp, this), true);
  577. }
  578. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  579. typename hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::iterator
  580. hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::insert_equal_noresize(
  581. const value_type& __obj)
  582. {
  583. const size_type __n = _M_bkt_num(__obj);
  584. _Node* __first = _M_buckets[__n];
  585. for (_Node* __cur = __first; __cur; __cur = __cur->_M_next)
  586. if (_M_equals(_M_get_key(__cur->_M_val), _M_get_key(__obj))) {
  587. _Node* __tmp = _M_new_node(__obj);
  588. __tmp->_M_next = __cur->_M_next;
  589. __cur->_M_next = __tmp;
  590. ++_M_num_elements;
  591. return iterator(__tmp, this);
  592. }
  593. _Node* __tmp = _M_new_node(__obj);
  594. __tmp->_M_next = __first;
  595. _M_buckets[__n] = __tmp;
  596. ++_M_num_elements;
  597. return iterator(__tmp, this);
  598. }
  599. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  600. typename hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::reference hashtable<
  601. _Val, _Key, _HF, _Ex, _Eq, _All>::find_or_insert(const value_type& __obj)
  602. {
  603. resize(_M_num_elements + 1);
  604. size_type __n = _M_bkt_num(__obj);
  605. _Node* __first = _M_buckets[__n];
  606. for (_Node* __cur = __first; __cur; __cur = __cur->_M_next)
  607. if (_M_equals(_M_get_key(__cur->_M_val), _M_get_key(__obj)))
  608. return __cur->_M_val;
  609. _Node* __tmp = _M_new_node(__obj);
  610. __tmp->_M_next = __first;
  611. _M_buckets[__n] = __tmp;
  612. ++_M_num_elements;
  613. return __tmp->_M_val;
  614. }
  615. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  616. std::pair<typename hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::iterator,
  617. typename hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::iterator>
  618. hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::equal_range(const key_type& __key)
  619. {
  620. typedef std::pair<iterator, iterator> _Pii;
  621. const size_type __n = _M_bkt_num_key(__key);
  622. for (_Node* __first = _M_buckets[__n]; __first; __first = __first->_M_next)
  623. if (_M_equals(_M_get_key(__first->_M_val), __key)) {
  624. for (_Node* __cur = __first->_M_next; __cur; __cur = __cur->_M_next)
  625. if (!_M_equals(_M_get_key(__cur->_M_val), __key))
  626. return _Pii(iterator(__first, this), iterator(__cur, this));
  627. for (size_type __m = __n + 1; __m < _M_buckets.size(); ++__m)
  628. if (_M_buckets[__m])
  629. return _Pii(iterator(__first, this),
  630. iterator(_M_buckets[__m], this));
  631. return _Pii(iterator(__first, this), end());
  632. }
  633. return _Pii(end(), end());
  634. }
  635. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  636. std::pair<typename hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::const_iterator,
  637. typename hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::const_iterator>
  638. hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::equal_range(
  639. const key_type& __key) const
  640. {
  641. typedef std::pair<const_iterator, const_iterator> _Pii;
  642. const size_type __n = _M_bkt_num_key(__key);
  643. for (const _Node* __first = _M_buckets[__n]; __first;
  644. __first = __first->_M_next) {
  645. if (_M_equals(_M_get_key(__first->_M_val), __key)) {
  646. for (const _Node* __cur = __first->_M_next; __cur;
  647. __cur = __cur->_M_next)
  648. if (!_M_equals(_M_get_key(__cur->_M_val), __key))
  649. return _Pii(const_iterator(__first, this),
  650. const_iterator(__cur, this));
  651. for (size_type __m = __n + 1; __m < _M_buckets.size(); ++__m)
  652. if (_M_buckets[__m])
  653. return _Pii(const_iterator(__first, this),
  654. const_iterator(_M_buckets[__m], this));
  655. return _Pii(const_iterator(__first, this), end());
  656. }
  657. }
  658. return _Pii(end(), end());
  659. }
  660. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  661. typename hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::size_type
  662. hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::erase(const key_type& __key)
  663. {
  664. const size_type __n = _M_bkt_num_key(__key);
  665. _Node* __first = _M_buckets[__n];
  666. size_type __erased = 0;
  667. if (__first) {
  668. _Node* __cur = __first;
  669. _Node* __next = __cur->_M_next;
  670. while (__next) {
  671. if (_M_equals(_M_get_key(__next->_M_val), __key)) {
  672. __cur->_M_next = __next->_M_next;
  673. _M_delete_node(__next);
  674. __next = __cur->_M_next;
  675. ++__erased;
  676. --_M_num_elements;
  677. } else {
  678. __cur = __next;
  679. __next = __cur->_M_next;
  680. }
  681. }
  682. if (_M_equals(_M_get_key(__first->_M_val), __key)) {
  683. _M_buckets[__n] = __first->_M_next;
  684. _M_delete_node(__first);
  685. ++__erased;
  686. --_M_num_elements;
  687. }
  688. }
  689. return __erased;
  690. }
  691. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  692. void hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::erase(const iterator& __it)
  693. {
  694. _Node* __p = __it._M_cur;
  695. if (__p) {
  696. const size_type __n = _M_bkt_num(__p->_M_val);
  697. _Node* __cur = _M_buckets[__n];
  698. if (__cur == __p) {
  699. _M_buckets[__n] = __cur->_M_next;
  700. _M_delete_node(__cur);
  701. --_M_num_elements;
  702. } else {
  703. _Node* __next = __cur->_M_next;
  704. while (__next) {
  705. if (__next == __p) {
  706. __cur->_M_next = __next->_M_next;
  707. _M_delete_node(__next);
  708. --_M_num_elements;
  709. break;
  710. } else {
  711. __cur = __next;
  712. __next = __cur->_M_next;
  713. }
  714. }
  715. }
  716. }
  717. }
  718. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  719. void hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::erase(iterator __first,
  720. iterator __last)
  721. {
  722. size_type __f_bucket =
  723. __first._M_cur ? _M_bkt_num(__first._M_cur->_M_val) : _M_buckets.size();
  724. size_type __l_bucket =
  725. __last._M_cur ? _M_bkt_num(__last._M_cur->_M_val) : _M_buckets.size();
  726. if (__first._M_cur == __last._M_cur)
  727. return;
  728. else if (__f_bucket == __l_bucket)
  729. _M_erase_bucket(__f_bucket, __first._M_cur, __last._M_cur);
  730. else {
  731. _M_erase_bucket(__f_bucket, __first._M_cur, 0);
  732. for (size_type __n = __f_bucket + 1; __n < __l_bucket; ++__n)
  733. _M_erase_bucket(__n, 0);
  734. if (__l_bucket != _M_buckets.size())
  735. _M_erase_bucket(__l_bucket, __last._M_cur);
  736. }
  737. }
  738. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  739. inline void hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::erase(
  740. const_iterator __first, const_iterator __last)
  741. {
  742. erase(iterator(const_cast<_Node*>(__first._M_cur),
  743. const_cast<hashtable*>(__first._M_ht)),
  744. iterator(const_cast<_Node*>(__last._M_cur),
  745. const_cast<hashtable*>(__last._M_ht)));
  746. }
  747. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  748. inline void hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::erase(
  749. const const_iterator& __it)
  750. {
  751. erase(iterator(const_cast<_Node*>(__it._M_cur),
  752. const_cast<hashtable*>(__it._M_ht)));
  753. }
  754. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  755. void hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::resize(
  756. size_type __num_elements_hint)
  757. {
  758. const size_type __old_n = _M_buckets.size();
  759. if (__num_elements_hint > __old_n) {
  760. const size_type __n = _M_next_size(__num_elements_hint);
  761. if (__n > __old_n) {
  762. _M_buckets_type __tmp(__n, (_Node*)(0), _M_buckets.get_allocator());
  763. try {
  764. for (size_type __bucket = 0; __bucket < __old_n; ++__bucket) {
  765. _Node* __first = _M_buckets[__bucket];
  766. while (__first) {
  767. size_type __new_bucket = _M_bkt_num(__first->_M_val, __n);
  768. _M_buckets[__bucket] = __first->_M_next;
  769. __first->_M_next = __tmp[__new_bucket];
  770. __tmp[__new_bucket] = __first;
  771. __first = _M_buckets[__bucket];
  772. }
  773. }
  774. _M_buckets.swap(__tmp);
  775. } catch (...) {
  776. for (size_type __bucket = 0; __bucket < __tmp.size(); ++__bucket) {
  777. while (__tmp[__bucket]) {
  778. _Node* __next = __tmp[__bucket]->_M_next;
  779. _M_delete_node(__tmp[__bucket]);
  780. __tmp[__bucket] = __next;
  781. }
  782. }
  783. throw;
  784. }
  785. }
  786. }
  787. }
  788. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  789. void hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::_M_erase_bucket(
  790. const size_type __n, _Node* __first, _Node* __last)
  791. {
  792. _Node* __cur = _M_buckets[__n];
  793. if (__cur == __first)
  794. _M_erase_bucket(__n, __last);
  795. else {
  796. _Node* __next;
  797. for (__next = __cur->_M_next; __next != __first;
  798. __cur = __next, __next = __cur->_M_next)
  799. ;
  800. while (__next != __last) {
  801. __cur->_M_next = __next->_M_next;
  802. _M_delete_node(__next);
  803. __next = __cur->_M_next;
  804. --_M_num_elements;
  805. }
  806. }
  807. }
  808. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  809. void hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::_M_erase_bucket(
  810. const size_type __n, _Node* __last)
  811. {
  812. _Node* __cur = _M_buckets[__n];
  813. while (__cur != __last) {
  814. _Node* __next = __cur->_M_next;
  815. _M_delete_node(__cur);
  816. __cur = __next;
  817. _M_buckets[__n] = __cur;
  818. --_M_num_elements;
  819. }
  820. }
  821. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  822. void hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::clear()
  823. {
  824. for (size_type __i = 0; __i < _M_buckets.size(); ++__i) {
  825. _Node* __cur = _M_buckets[__i];
  826. while (__cur != 0) {
  827. _Node* __next = __cur->_M_next;
  828. _M_delete_node(__cur);
  829. __cur = __next;
  830. }
  831. _M_buckets[__i] = 0;
  832. }
  833. _M_num_elements = 0;
  834. }
  835. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  836. void hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>::_M_copy_from(
  837. const hashtable& __ht)
  838. {
  839. _M_buckets.clear();
  840. _M_buckets.reserve(__ht._M_buckets.size());
  841. _M_buckets.insert(_M_buckets.end(), __ht._M_buckets.size(), (_Node*)0);
  842. try {
  843. for (size_type __i = 0; __i < __ht._M_buckets.size(); ++__i) {
  844. const _Node* __cur = __ht._M_buckets[__i];
  845. if (__cur) {
  846. _Node* __copy = _M_new_node(__cur->_M_val);
  847. _M_buckets[__i] = __copy;
  848. for (_Node *__next = __cur->_M_next; __next;
  849. __cur = __next, __next = __cur->_M_next) {
  850. __copy->_M_next = _M_new_node(__next->_M_val);
  851. __copy = __copy->_M_next;
  852. }
  853. }
  854. }
  855. _M_num_elements = __ht._M_num_elements;
  856. } catch (...) {
  857. clear();
  858. throw;
  859. }
  860. }
  861. } // namespace @KWSYS_NAMESPACE@
  862. // Undo warning suppression.
  863. #if defined(__clang__) && defined(__has_warning)
  864. #if __has_warning("-Wdeprecated")
  865. #pragma clang diagnostic pop
  866. #endif
  867. #endif
  868. #if defined(_MSC_VER)
  869. #pragma warning(pop)
  870. #endif
  871. #endif