hashtable.hxx.in 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. /*
  11. * Copyright (c) 1996
  12. * Silicon Graphics Computer Systems, Inc.
  13. *
  14. * Permission to use, copy, modify, distribute and sell this software
  15. * and its documentation for any purpose is hereby granted without fee,
  16. * provided that the above copyright notice appear in all copies and
  17. * that both that copyright notice and this permission notice appear
  18. * in supporting documentation. Silicon Graphics makes no
  19. * representations about the suitability of this software for any
  20. * purpose. It is provided "as is" without express or implied warranty.
  21. *
  22. *
  23. * Copyright (c) 1994
  24. * Hewlett-Packard Company
  25. *
  26. * Permission to use, copy, modify, distribute and sell this software
  27. * and its documentation for any purpose is hereby granted without fee,
  28. * provided that the above copyright notice appear in all copies and
  29. * that both that copyright notice and this permission notice appear
  30. * in supporting documentation. Hewlett-Packard Company makes no
  31. * representations about the suitability of this software for any
  32. * purpose. It is provided "as is" without express or implied warranty.
  33. *
  34. */
  35. #ifdef __BORLANDC__
  36. # pragma warn -8027 /* 'for' not inlined. */
  37. # pragma warn -8026 /* 'exception' not inlined. */
  38. #endif
  39. #ifndef @KWSYS_NAMESPACE@_hashtable_hxx
  40. #define @KWSYS_NAMESPACE@_hashtable_hxx
  41. #include <@KWSYS_NAMESPACE@/Configure.hxx>
  42. #include <@KWSYS_NAMESPACE@/cstddef> // size_t
  43. #include <@KWSYS_NAMESPACE@/stl/algorithm> // lower_bound
  44. #include <@KWSYS_NAMESPACE@/stl/functional> // unary_function
  45. #include <@KWSYS_NAMESPACE@/stl/iterator> // iterator_traits
  46. #include <@KWSYS_NAMESPACE@/stl/memory> // allocator
  47. #include <@KWSYS_NAMESPACE@/stl/utility> // pair
  48. #include <@KWSYS_NAMESPACE@/stl/vector> // vector
  49. #if defined(_MSC_VER)
  50. # pragma warning (push)
  51. # pragma warning (disable:4284)
  52. # pragma warning (disable:4786)
  53. # pragma warning (disable:4512) /* no assignment operator for class */
  54. #endif
  55. #if defined(__sgi) && !defined(__GNUC__)
  56. # pragma set woff 3970 /* pointer to int conversion */ 3321 3968
  57. #endif
  58. #if @KWSYS_NAMESPACE@_STL_HAS_ALLOCATOR_TEMPLATE
  59. # define @KWSYS_NAMESPACE@_HASH_DEFAULT_ALLOCATOR(T) @KWSYS_NAMESPACE@_stl::allocator< T >
  60. #elif @KWSYS_NAMESPACE@_STL_HAS_ALLOCATOR_NONTEMPLATE
  61. # define @KWSYS_NAMESPACE@_HASH_DEFAULT_ALLOCATOR(T) @KWSYS_NAMESPACE@_stl::allocator
  62. #else
  63. # define @KWSYS_NAMESPACE@_HASH_DEFAULT_ALLOCATOR(T) @KWSYS_NAMESPACE@_stl::alloc
  64. #endif
  65. #if @KWSYS_NAMESPACE@_STL_HAS_ALLOCATOR_OBJECTS
  66. # define @KWSYS_NAMESPACE@_HASH_BUCKETS_INIT(__a) _M_buckets(__a)
  67. # define @KWSYS_NAMESPACE@_HASH_BUCKETS_GET_ALLOCATOR(__b) , __b.get_allocator()
  68. #else
  69. # define @KWSYS_NAMESPACE@_HASH_BUCKETS_INIT(__a) _M_buckets()
  70. # define @KWSYS_NAMESPACE@_HASH_BUCKETS_GET_ALLOCATOR(__b)
  71. #endif
  72. namespace @KWSYS_NAMESPACE@
  73. {
  74. //----------------------------------------------------------------------------
  75. // Define an allocator adaptor for platforms that do not provide an
  76. // allocator with the rebind member.
  77. #if !@KWSYS_NAMESPACE@_STL_HAS_ALLOCATOR_REBIND
  78. // Utility functions to convert item counts.
  79. inline size_t hash_sizeof(void*) { return sizeof(char); }
  80. inline size_t hash_sizeof(const void*) { return sizeof(char); }
  81. template <class TPtr> inline size_t hash_sizeof(TPtr p)
  82. {
  83. static_cast<void>(p);
  84. return sizeof(*p);
  85. }
  86. template <class POut, class PIn, class TSize>
  87. inline TSize hash_allocator_n(POut out, PIn in, TSize n)
  88. {
  89. return n*(hash_sizeof(out)/hash_sizeof(in) +
  90. (hash_sizeof(out)%hash_sizeof(in)>0));
  91. }
  92. // Define an allocation method to use the native allocator with
  93. // the proper signature. The following signatures of the allocate
  94. // method are used on various STL implementations:
  95. // pointer allocate(size_type, const void* hint)
  96. // pointer allocate(size_type)
  97. // static pointer allocate(size_type, const void* hint)
  98. // static pointer allocate(size_type)
  99. // Where pointer might be a real type or void*.
  100. // This set of overloads decodes the signature for a particular STL.
  101. // The extra three int/long arguments will favor certain signatures
  102. // over others in the case that multiple are present to avoid
  103. // ambiguity errors.
  104. template <class TAlloc, class PIn, class TSize, class THint, class POut>
  105. inline void hash_allocate(TAlloc* a, PIn (TAlloc::*allocate)(TSize, THint),
  106. TSize n_out, const void* hint, POut& out,
  107. int, int, int)
  108. {
  109. TSize n_in = hash_allocator_n(POut(), PIn(), n_out);
  110. void* vout = (a->*allocate)(n_in, const_cast<THint>(hint));
  111. out = static_cast<POut>(vout);
  112. }
  113. template <class TAlloc, class PIn, class TSize, class POut>
  114. inline void hash_allocate(TAlloc* a, PIn (TAlloc::*allocate)(TSize),
  115. TSize n_out, const void*, POut& out,
  116. int, int, long)
  117. {
  118. TSize n_in = hash_allocator_n(POut(), PIn(), n_out);
  119. void* vout = (a->*allocate)(n_in);
  120. out = static_cast<POut>(vout);
  121. }
  122. template <class PIn, class TSize, class THint, class POut>
  123. inline void hash_allocate(void*, PIn (*allocate)(TSize, THint),
  124. TSize n_out, const void* hint, POut& out,
  125. int, long, long)
  126. {
  127. TSize n_in = hash_allocator_n(POut(), PIn(), n_out);
  128. void* vout = allocate(n_in, const_cast<THint>(hint));
  129. out = static_cast<POut>(vout);
  130. }
  131. template <class PIn, class TSize, class POut>
  132. inline void hash_allocate(void*, PIn (*allocate)(TSize),
  133. TSize n_out, const void*, POut& out,
  134. long, long, long)
  135. {
  136. TSize n_in = hash_allocator_n(POut(), PIn(), n_out);
  137. void* vout = allocate(n_in);
  138. out = static_cast<POut>(vout);
  139. }
  140. // Define a deallocation method to use the native allocator with
  141. // the proper signature. The following signatures of the deallocate
  142. // method are used on various STL implementations:
  143. // void deallocate(pointer, size_type)
  144. // void deallocate(pointer)
  145. // static void deallocate(pointer, size_type)
  146. // static void deallocate(pointer)
  147. // Where pointer might be a real type or void*.
  148. // This set of overloads decodes the signature for a particular STL.
  149. // The extra three int/long arguments will favor certain signatures
  150. // over others in the case that multiple are present to avoid
  151. // ambiguity errors.
  152. template <class TAlloc, class PIn, class TSize, class PInReal, class POut>
  153. inline void hash_deallocate(TAlloc* a, void (TAlloc::*deallocate)(PIn, TSize),
  154. PInReal, POut p, TSize n_out, int, int, int)
  155. {
  156. TSize n_in = hash_allocator_n(POut(), PInReal(), n_out);
  157. void* vout = p;
  158. (a->*deallocate)(static_cast<PIn>(vout), n_in);
  159. }
  160. template <class TAlloc, class PIn, class TSize, class PInReal, class POut>
  161. inline void hash_deallocate(TAlloc* a, void (TAlloc::*deallocate)(PIn),
  162. PInReal, POut p, TSize, int, int, long)
  163. {
  164. void* vout = p;
  165. (a->*deallocate)(static_cast<PIn>(vout));
  166. }
  167. template <class PIn, class TSize, class PInReal, class POut>
  168. inline void hash_deallocate(void*, void (*deallocate)(PIn, TSize),
  169. PInReal, POut p, TSize n_out, int, long, long)
  170. {
  171. TSize n_in = hash_allocator_n(POut(), PInReal(), n_out);
  172. void* vout = p;
  173. deallocate(static_cast<PIn>(vout), n_in);
  174. }
  175. template <class PIn, class TSize, class PInReal, class POut>
  176. inline void hash_deallocate(void*, void (*deallocate)(PIn),
  177. PInReal, POut p, TSize, long, long, long)
  178. {
  179. void* vout = p;
  180. deallocate(static_cast<PIn>(vout));
  181. }
  182. // Use the same four overloads as hash_allocate to decode the type
  183. // really used for allocation. This is passed as PInReal to the
  184. // deallocate functions so that hash_allocator_n has the proper size.
  185. template <class TAlloc, class PIn, class TSize, class THint>
  186. inline PIn hash_allocate_type(PIn (TAlloc::*)(TSize, THint),
  187. int, int, int) { return 0; }
  188. template <class TAlloc, class PIn, class TSize>
  189. inline PIn hash_allocate_type(PIn (TAlloc::*)(TSize),
  190. int, int, long) { return 0; }
  191. template <class PIn, class TSize, class THint>
  192. inline PIn hash_allocate_type(PIn (*)(TSize, THint),
  193. int, long, long) { return 0; }
  194. template <class PIn, class TSize>
  195. inline PIn hash_allocate_type(PIn (*)(TSize),
  196. long, long, long) { return 0; }
  197. // Define the comparison operators in terms of a base type to avoid
  198. // needing templated versions.
  199. class hash_allocator_base {};
  200. inline bool operator==(const hash_allocator_base&,
  201. const hash_allocator_base&) throw() { return true; }
  202. inline bool operator!=(const hash_allocator_base&,
  203. const hash_allocator_base&) throw() { return false; }
  204. // Define the allocator template.
  205. template <class T, class Alloc>
  206. class hash_allocator: public hash_allocator_base
  207. {
  208. private:
  209. // Store the real allocator privately.
  210. typedef Alloc alloc_type;
  211. alloc_type alloc_;
  212. public:
  213. // Standard allocator interface.
  214. typedef size_t size_type;
  215. typedef ptrdiff_t difference_type;
  216. typedef T* pointer;
  217. typedef const T* const_pointer;
  218. typedef T& reference;
  219. typedef const T& const_reference;
  220. typedef T value_type;
  221. hash_allocator() throw(): alloc_() {}
  222. hash_allocator(const hash_allocator_base&) throw() : alloc_() {}
  223. hash_allocator(const hash_allocator& a) throw() : alloc_(a.alloc_) {}
  224. hash_allocator(const alloc_type& a) throw() : alloc_(a) {}
  225. ~hash_allocator() throw() {}
  226. # if @KWSYS_NAMESPACE@_CXX_HAS_MEMBER_TEMPLATES
  227. template <class U>
  228. struct rebind { typedef hash_allocator<U, alloc_type> other; };
  229. # endif
  230. pointer address(reference x) const { return &x; }
  231. const_pointer address(const_reference x) const { return &x; }
  232. typedef void* void_pointer;
  233. typedef const void* const_void_pointer;
  234. pointer allocate(size_type n=1, const_void_pointer hint = 0)
  235. {
  236. if(n)
  237. {
  238. pointer p;
  239. hash_allocate(&alloc_, &alloc_type::allocate, n, hint, p, 1, 1, 1);
  240. return p;
  241. }
  242. else
  243. {
  244. return 0;
  245. }
  246. }
  247. void deallocate(pointer p, size_type n=1)
  248. {
  249. if(n)
  250. {
  251. hash_deallocate(&alloc_, &alloc_type::deallocate,
  252. hash_allocate_type(&alloc_type::allocate, 1, 1, 1),
  253. p, n, 1, 1, 1);
  254. }
  255. }
  256. #if @KWSYS_NAMESPACE@_STL_HAS_ALLOCATOR_MAX_SIZE_ARGUMENT
  257. size_type max_size(size_type s) const throw()
  258. {
  259. return alloc_.max_size(s);
  260. }
  261. #else
  262. size_type max_size() const throw()
  263. {
  264. size_type n = alloc_.max_size() / sizeof(value_type);
  265. return n>0? n:1;
  266. }
  267. #endif
  268. void construct(pointer p, const value_type& val) { new (p) value_type(val); }
  269. void destroy(pointer p) { (void)p; p->~value_type(); }
  270. };
  271. #endif
  272. template <class _Val>
  273. struct _Hashtable_node
  274. {
  275. _Hashtable_node* _M_next;
  276. _Val _M_val;
  277. };
  278. template <class _Val, class _Key, class _HashFcn,
  279. class _ExtractKey, class _EqualKey,
  280. class _Alloc = @KWSYS_NAMESPACE@_HASH_DEFAULT_ALLOCATOR(char) >
  281. class hashtable;
  282. template <class _Val, class _Key, class _HashFcn,
  283. class _ExtractKey, class _EqualKey, class _Alloc>
  284. struct _Hashtable_iterator;
  285. template <class _Val, class _Key, class _HashFcn,
  286. class _ExtractKey, class _EqualKey, class _Alloc>
  287. struct _Hashtable_const_iterator;
  288. template <class _Val, class _Key, class _HashFcn,
  289. class _ExtractKey, class _EqualKey, class _Alloc>
  290. struct _Hashtable_iterator {
  291. typedef hashtable<_Val,_Key,_HashFcn,_ExtractKey,_EqualKey,_Alloc>
  292. _Hashtable;
  293. typedef _Hashtable_iterator<_Val, _Key, _HashFcn,
  294. _ExtractKey, _EqualKey, _Alloc>
  295. iterator;
  296. typedef _Hashtable_const_iterator<_Val, _Key, _HashFcn,
  297. _ExtractKey, _EqualKey, _Alloc>
  298. const_iterator;
  299. typedef _Hashtable_node<_Val> _Node;
  300. typedef @KWSYS_NAMESPACE@_stl::forward_iterator_tag iterator_category;
  301. typedef _Val value_type;
  302. typedef ptrdiff_t difference_type;
  303. typedef size_t size_type;
  304. typedef _Val& reference;
  305. typedef _Val* pointer;
  306. _Node* _M_cur;
  307. _Hashtable* _M_ht;
  308. _Hashtable_iterator(_Node* __n, _Hashtable* __tab)
  309. : _M_cur(__n), _M_ht(__tab) {}
  310. _Hashtable_iterator() {}
  311. reference operator*() const { return _M_cur->_M_val; }
  312. pointer operator->() const { return &(operator*()); }
  313. iterator& operator++();
  314. iterator operator++(int);
  315. bool operator==(const iterator& __it) const
  316. { return _M_cur == __it._M_cur; }
  317. bool operator!=(const iterator& __it) const
  318. { return _M_cur != __it._M_cur; }
  319. };
  320. template <class _Val, class _Key, class _HashFcn,
  321. class _ExtractKey, class _EqualKey, class _Alloc>
  322. struct _Hashtable_const_iterator {
  323. typedef hashtable<_Val,_Key,_HashFcn,_ExtractKey,_EqualKey,_Alloc>
  324. _Hashtable;
  325. typedef _Hashtable_iterator<_Val,_Key,_HashFcn,
  326. _ExtractKey,_EqualKey,_Alloc>
  327. iterator;
  328. typedef _Hashtable_const_iterator<_Val, _Key, _HashFcn,
  329. _ExtractKey, _EqualKey, _Alloc>
  330. const_iterator;
  331. typedef _Hashtable_node<_Val> _Node;
  332. typedef @KWSYS_NAMESPACE@_stl::forward_iterator_tag iterator_category;
  333. typedef _Val value_type;
  334. typedef ptrdiff_t difference_type;
  335. typedef size_t size_type;
  336. typedef const _Val& reference;
  337. typedef const _Val* pointer;
  338. const _Node* _M_cur;
  339. const _Hashtable* _M_ht;
  340. _Hashtable_const_iterator(const _Node* __n, const _Hashtable* __tab)
  341. : _M_cur(__n), _M_ht(__tab) {}
  342. _Hashtable_const_iterator() {}
  343. _Hashtable_const_iterator(const iterator& __it)
  344. : _M_cur(__it._M_cur), _M_ht(__it._M_ht) {}
  345. reference operator*() const { return _M_cur->_M_val; }
  346. pointer operator->() const { return &(operator*()); }
  347. const_iterator& operator++();
  348. const_iterator operator++(int);
  349. bool operator==(const const_iterator& __it) const
  350. { return _M_cur == __it._M_cur; }
  351. bool operator!=(const const_iterator& __it) const
  352. { return _M_cur != __it._M_cur; }
  353. };
  354. // Note: assumes long is at least 32 bits.
  355. enum { _stl_num_primes = 31 };
  356. // create a function with a static local to that function that returns
  357. // the static
  358. static inline const unsigned long* get_stl_prime_list() {
  359. static const unsigned long _stl_prime_list[_stl_num_primes] =
  360. {
  361. 5ul, 11ul, 23ul,
  362. 53ul, 97ul, 193ul, 389ul, 769ul,
  363. 1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
  364. 49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
  365. 1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
  366. 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
  367. 1610612741ul, 3221225473ul, 4294967291ul
  368. };
  369. return &_stl_prime_list[0]; }
  370. inline size_t _stl_next_prime(size_t __n)
  371. {
  372. const unsigned long* __first = get_stl_prime_list();
  373. const unsigned long* __last = get_stl_prime_list() + (int)_stl_num_primes;
  374. const unsigned long* pos = @KWSYS_NAMESPACE@_stl::lower_bound(__first, __last, __n);
  375. return pos == __last ? *(__last - 1) : *pos;
  376. }
  377. // Forward declaration of operator==.
  378. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  379. class hashtable;
  380. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  381. bool operator==(const hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>& __ht1,
  382. const hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>& __ht2);
  383. // Hashtables handle allocators a bit differently than other containers
  384. // do. If we're using standard-conforming allocators, then a hashtable
  385. // unconditionally has a member variable to hold its allocator, even if
  386. // it so happens that all instances of the allocator type are identical.
  387. // This is because, for hashtables, this extra storage is negligible.
  388. // Additionally, a base class wouldn't serve any other purposes; it
  389. // wouldn't, for example, simplify the exception-handling code.
  390. template <class _Val, class _Key, class _HashFcn,
  391. class _ExtractKey, class _EqualKey, class _Alloc>
  392. class hashtable {
  393. public:
  394. typedef _Key key_type;
  395. typedef _Val value_type;
  396. typedef _HashFcn hasher;
  397. typedef _EqualKey key_equal;
  398. typedef size_t size_type;
  399. typedef ptrdiff_t difference_type;
  400. typedef value_type* pointer;
  401. typedef const value_type* const_pointer;
  402. typedef value_type& reference;
  403. typedef const value_type& const_reference;
  404. hasher hash_funct() const { return _M_hash; }
  405. key_equal key_eq() const { return _M_equals; }
  406. private:
  407. typedef _Hashtable_node<_Val> _Node;
  408. #if @KWSYS_NAMESPACE@_STL_HAS_ALLOCATOR_REBIND
  409. public:
  410. typedef typename _Alloc::template rebind<_Val>::other allocator_type;
  411. allocator_type get_allocator() const { return _M_node_allocator; }
  412. private:
  413. typedef typename _Alloc::template rebind<_Node>::other _M_node_allocator_type;
  414. typedef typename _Alloc::template rebind<_Node*>::other _M_node_ptr_allocator_type;
  415. typedef @KWSYS_NAMESPACE@_stl::vector<_Node*,_M_node_ptr_allocator_type> _M_buckets_type;
  416. #else
  417. public:
  418. typedef hash_allocator<_Val, _Alloc> allocator_type;
  419. allocator_type get_allocator() const { return allocator_type(); }
  420. private:
  421. typedef hash_allocator<_Node, _Alloc> _M_node_allocator_type;
  422. # if @KWSYS_NAMESPACE@_STL_HAS_ALLOCATOR_OBJECTS
  423. typedef hash_allocator<_Node*, _Alloc> _M_node_ptr_allocator_type;
  424. # else
  425. typedef _Alloc _M_node_ptr_allocator_type;
  426. # endif
  427. typedef @KWSYS_NAMESPACE@_stl::vector<_Node*,_M_node_ptr_allocator_type> _M_buckets_type;
  428. #endif
  429. private:
  430. _M_node_allocator_type _M_node_allocator;
  431. hasher _M_hash;
  432. key_equal _M_equals;
  433. _ExtractKey _M_get_key;
  434. _M_buckets_type _M_buckets;
  435. size_type _M_num_elements;
  436. _Node* _M_get_node() { return _M_node_allocator.allocate(1); }
  437. void _M_put_node(_Node* __p) { _M_node_allocator.deallocate(__p, 1); }
  438. public:
  439. typedef _Hashtable_iterator<_Val,_Key,_HashFcn,_ExtractKey,_EqualKey,_Alloc>
  440. iterator;
  441. typedef _Hashtable_const_iterator<_Val,_Key,_HashFcn,_ExtractKey,_EqualKey,
  442. _Alloc>
  443. const_iterator;
  444. friend struct
  445. _Hashtable_iterator<_Val,_Key,_HashFcn,_ExtractKey,_EqualKey,_Alloc>;
  446. friend struct
  447. _Hashtable_const_iterator<_Val,_Key,_HashFcn,_ExtractKey,_EqualKey,_Alloc>;
  448. public:
  449. hashtable(size_type __n,
  450. const _HashFcn& __hf,
  451. const _EqualKey& __eql,
  452. const _ExtractKey& __ext,
  453. const allocator_type& __a = allocator_type())
  454. : _M_node_allocator(__a),
  455. _M_hash(__hf),
  456. _M_equals(__eql),
  457. _M_get_key(__ext),
  458. @KWSYS_NAMESPACE@_HASH_BUCKETS_INIT(__a),
  459. _M_num_elements(0)
  460. {
  461. _M_initialize_buckets(__n);
  462. }
  463. hashtable(size_type __n,
  464. const _HashFcn& __hf,
  465. const _EqualKey& __eql,
  466. const allocator_type& __a = allocator_type())
  467. : _M_node_allocator(__a),
  468. _M_hash(__hf),
  469. _M_equals(__eql),
  470. _M_get_key(_ExtractKey()),
  471. @KWSYS_NAMESPACE@_HASH_BUCKETS_INIT(__a),
  472. _M_num_elements(0)
  473. {
  474. _M_initialize_buckets(__n);
  475. }
  476. hashtable(const hashtable& __ht)
  477. : _M_node_allocator(__ht.get_allocator()),
  478. _M_hash(__ht._M_hash),
  479. _M_equals(__ht._M_equals),
  480. _M_get_key(__ht._M_get_key),
  481. @KWSYS_NAMESPACE@_HASH_BUCKETS_INIT(__ht.get_allocator()),
  482. _M_num_elements(0)
  483. {
  484. _M_copy_from(__ht);
  485. }
  486. hashtable& operator= (const hashtable& __ht)
  487. {
  488. if (&__ht != this) {
  489. clear();
  490. _M_hash = __ht._M_hash;
  491. _M_equals = __ht._M_equals;
  492. _M_get_key = __ht._M_get_key;
  493. _M_copy_from(__ht);
  494. }
  495. return *this;
  496. }
  497. ~hashtable() { clear(); }
  498. size_type size() const { return _M_num_elements; }
  499. size_type max_size() const { return size_type(-1); }
  500. bool empty() const { return size() == 0; }
  501. void swap(hashtable& __ht)
  502. {
  503. @KWSYS_NAMESPACE@_stl::swap(_M_hash, __ht._M_hash);
  504. @KWSYS_NAMESPACE@_stl::swap(_M_equals, __ht._M_equals);
  505. @KWSYS_NAMESPACE@_stl::swap(_M_get_key, __ht._M_get_key);
  506. _M_buckets.swap(__ht._M_buckets);
  507. @KWSYS_NAMESPACE@_stl::swap(_M_num_elements, __ht._M_num_elements);
  508. }
  509. iterator begin()
  510. {
  511. for (size_type __n = 0; __n < _M_buckets.size(); ++__n)
  512. if (_M_buckets[__n])
  513. return iterator(_M_buckets[__n], this);
  514. return end();
  515. }
  516. iterator end() { return iterator(0, this); }
  517. const_iterator begin() const
  518. {
  519. for (size_type __n = 0; __n < _M_buckets.size(); ++__n)
  520. if (_M_buckets[__n])
  521. return const_iterator(_M_buckets[__n], this);
  522. return end();
  523. }
  524. const_iterator end() const { return const_iterator(0, this); }
  525. friend bool operator==@KWSYS_NAMESPACE@_CXX_NULL_TEMPLATE_ARGS(const hashtable&,
  526. const hashtable&);
  527. public:
  528. size_type bucket_count() const { return _M_buckets.size(); }
  529. size_type max_bucket_count() const
  530. { return get_stl_prime_list()[(int)_stl_num_primes - 1]; }
  531. size_type elems_in_bucket(size_type __bucket) const
  532. {
  533. size_type __result = 0;
  534. for (_Node* __cur = _M_buckets[__bucket]; __cur; __cur = __cur->_M_next)
  535. __result += 1;
  536. return __result;
  537. }
  538. @KWSYS_NAMESPACE@_stl::pair<iterator, bool> insert_unique(const value_type& __obj)
  539. {
  540. resize(_M_num_elements + 1);
  541. return insert_unique_noresize(__obj);
  542. }
  543. iterator insert_equal(const value_type& __obj)
  544. {
  545. resize(_M_num_elements + 1);
  546. return insert_equal_noresize(__obj);
  547. }
  548. @KWSYS_NAMESPACE@_stl::pair<iterator, bool> insert_unique_noresize(const value_type& __obj);
  549. iterator insert_equal_noresize(const value_type& __obj);
  550. #if @KWSYS_NAMESPACE@_STL_HAS_ITERATOR_TRAITS
  551. # define @KWSYS_NAMESPACE@_HASH_ITERATOR_CATEGORY(T,I) \
  552. typename @KWSYS_NAMESPACE@_stl::iterator_traits< T >::iterator_category()
  553. #elif @KWSYS_NAMESPACE@_STL_HAS_ITERATOR_CATEGORY
  554. # define @KWSYS_NAMESPACE@_HASH_ITERATOR_CATEGORY(T,I) \
  555. @KWSYS_NAMESPACE@_stl::iterator_category( I )
  556. #elif @KWSYS_NAMESPACE@_STL_HAS___ITERATOR_CATEGORY
  557. # define @KWSYS_NAMESPACE@_HASH_ITERATOR_CATEGORY(T,I) \
  558. @KWSYS_NAMESPACE@_stl::__iterator_category( I )
  559. #endif
  560. #if @KWSYS_NAMESPACE@_CXX_HAS_MEMBER_TEMPLATES && defined(@KWSYS_NAMESPACE@_HASH_ITERATOR_CATEGORY)
  561. template <class _InputIterator>
  562. void insert_unique(_InputIterator __f, _InputIterator __l)
  563. {
  564. insert_unique(__f, __l,
  565. @KWSYS_NAMESPACE@_HASH_ITERATOR_CATEGORY(_InputIterator, __f));
  566. }
  567. template <class _InputIterator>
  568. void insert_equal(_InputIterator __f, _InputIterator __l)
  569. {
  570. insert_equal(__f, __l,
  571. @KWSYS_NAMESPACE@_HASH_ITERATOR_CATEGORY(_InputIterator, __f));
  572. }
  573. template <class _InputIterator>
  574. void insert_unique(_InputIterator __f, _InputIterator __l,
  575. @KWSYS_NAMESPACE@_stl::input_iterator_tag)
  576. {
  577. for ( ; __f != __l; ++__f)
  578. insert_unique(*__f);
  579. }
  580. template <class _InputIterator>
  581. void insert_equal(_InputIterator __f, _InputIterator __l,
  582. @KWSYS_NAMESPACE@_stl::input_iterator_tag)
  583. {
  584. for ( ; __f != __l; ++__f)
  585. insert_equal(*__f);
  586. }
  587. template <class _ForwardIterator>
  588. void insert_unique(_ForwardIterator __f, _ForwardIterator __l,
  589. @KWSYS_NAMESPACE@_stl::forward_iterator_tag)
  590. {
  591. size_type __n = 0;
  592. @KWSYS_NAMESPACE@_stl::distance(__f, __l, __n);
  593. resize(_M_num_elements + __n);
  594. for ( ; __n > 0; --__n, ++__f)
  595. insert_unique_noresize(*__f);
  596. }
  597. template <class _ForwardIterator>
  598. void insert_equal(_ForwardIterator __f, _ForwardIterator __l,
  599. @KWSYS_NAMESPACE@_stl::forward_iterator_tag)
  600. {
  601. size_type __n = 0;
  602. @KWSYS_NAMESPACE@_stl::distance(__f, __l, __n);
  603. resize(_M_num_elements + __n);
  604. for ( ; __n > 0; --__n, ++__f)
  605. insert_equal_noresize(*__f);
  606. }
  607. #else
  608. void insert_unique(const value_type* __f, const value_type* __l)
  609. {
  610. size_type __n = __l - __f;
  611. resize(_M_num_elements + __n);
  612. for ( ; __n > 0; --__n, ++__f)
  613. insert_unique_noresize(*__f);
  614. }
  615. void insert_equal(const value_type* __f, const value_type* __l)
  616. {
  617. size_type __n = __l - __f;
  618. resize(_M_num_elements + __n);
  619. for ( ; __n > 0; --__n, ++__f)
  620. insert_equal_noresize(*__f);
  621. }
  622. void insert_unique(const_iterator __f, const_iterator __l)
  623. {
  624. size_type __n = 0;
  625. @KWSYS_NAMESPACE@_stl::distance(__f, __l, __n);
  626. resize(_M_num_elements + __n);
  627. for ( ; __n > 0; --__n, ++__f)
  628. insert_unique_noresize(*__f);
  629. }
  630. void insert_equal(const_iterator __f, const_iterator __l)
  631. {
  632. size_type __n = 0;
  633. @KWSYS_NAMESPACE@_stl::distance(__f, __l, __n);
  634. resize(_M_num_elements + __n);
  635. for ( ; __n > 0; --__n, ++__f)
  636. insert_equal_noresize(*__f);
  637. }
  638. #endif
  639. reference find_or_insert(const value_type& __obj);
  640. iterator find(const key_type& __key)
  641. {
  642. size_type __n = _M_bkt_num_key(__key);
  643. _Node* __first;
  644. for ( __first = _M_buckets[__n];
  645. __first && !_M_equals(_M_get_key(__first->_M_val), __key);
  646. __first = __first->_M_next)
  647. {}
  648. return iterator(__first, this);
  649. }
  650. const_iterator find(const key_type& __key) const
  651. {
  652. size_type __n = _M_bkt_num_key(__key);
  653. const _Node* __first;
  654. for ( __first = _M_buckets[__n];
  655. __first && !_M_equals(_M_get_key(__first->_M_val), __key);
  656. __first = __first->_M_next)
  657. {}
  658. return const_iterator(__first, this);
  659. }
  660. size_type count(const key_type& __key) const
  661. {
  662. const size_type __n = _M_bkt_num_key(__key);
  663. size_type __result = 0;
  664. for (const _Node* __cur = _M_buckets[__n]; __cur; __cur = __cur->_M_next)
  665. if (_M_equals(_M_get_key(__cur->_M_val), __key))
  666. ++__result;
  667. return __result;
  668. }
  669. @KWSYS_NAMESPACE@_stl::pair<iterator, iterator>
  670. equal_range(const key_type& __key);
  671. @KWSYS_NAMESPACE@_stl::pair<const_iterator, const_iterator>
  672. equal_range(const key_type& __key) const;
  673. size_type erase(const key_type& __key);
  674. void erase(const iterator& __it);
  675. void erase(iterator __first, iterator __last);
  676. void erase(const const_iterator& __it);
  677. void erase(const_iterator __first, const_iterator __last);
  678. void resize(size_type __num_elements_hint);
  679. void clear();
  680. private:
  681. size_type _M_next_size(size_type __n) const
  682. { return _stl_next_prime(__n); }
  683. void _M_initialize_buckets(size_type __n)
  684. {
  685. const size_type __n_buckets = _M_next_size(__n);
  686. _M_buckets.reserve(__n_buckets);
  687. _M_buckets.insert(_M_buckets.end(), __n_buckets, (_Node*) 0);
  688. _M_num_elements = 0;
  689. }
  690. size_type _M_bkt_num_key(const key_type& __key) const
  691. {
  692. return _M_bkt_num_key(__key, _M_buckets.size());
  693. }
  694. size_type _M_bkt_num(const value_type& __obj) const
  695. {
  696. return _M_bkt_num_key(_M_get_key(__obj));
  697. }
  698. size_type _M_bkt_num_key(const key_type& __key, size_t __n) const
  699. {
  700. return _M_hash(__key) % __n;
  701. }
  702. size_type _M_bkt_num(const value_type& __obj, size_t __n) const
  703. {
  704. return _M_bkt_num_key(_M_get_key(__obj), __n);
  705. }
  706. void construct(_Val* p, const _Val& v)
  707. {
  708. new (p) _Val(v);
  709. }
  710. void destroy(_Val* p)
  711. {
  712. (void)p;
  713. p->~_Val();
  714. }
  715. _Node* _M_new_node(const value_type& __obj)
  716. {
  717. _Node* __n = _M_get_node();
  718. __n->_M_next = 0;
  719. try {
  720. construct(&__n->_M_val, __obj);
  721. return __n;
  722. }
  723. catch(...) {_M_put_node(__n); throw;}
  724. }
  725. void _M_delete_node(_Node* __n)
  726. {
  727. destroy(&__n->_M_val);
  728. _M_put_node(__n);
  729. }
  730. void _M_erase_bucket(const size_type __n, _Node* __first, _Node* __last);
  731. void _M_erase_bucket(const size_type __n, _Node* __last);
  732. void _M_copy_from(const hashtable& __ht);
  733. };
  734. template <class _Val, class _Key, class _HF, class _ExK, class _EqK,
  735. class _All>
  736. _Hashtable_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>&
  737. _Hashtable_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>::operator++()
  738. {
  739. const _Node* __old = _M_cur;
  740. _M_cur = _M_cur->_M_next;
  741. if (!_M_cur) {
  742. size_type __bucket = _M_ht->_M_bkt_num(__old->_M_val);
  743. while (!_M_cur && ++__bucket < _M_ht->_M_buckets.size())
  744. _M_cur = _M_ht->_M_buckets[__bucket];
  745. }
  746. return *this;
  747. }
  748. template <class _Val, class _Key, class _HF, class _ExK, class _EqK,
  749. class _All>
  750. inline _Hashtable_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>
  751. _Hashtable_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>::operator++(int)
  752. {
  753. iterator __tmp = *this;
  754. ++*this;
  755. return __tmp;
  756. }
  757. template <class _Val, class _Key, class _HF, class _ExK, class _EqK,
  758. class _All>
  759. _Hashtable_const_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>&
  760. _Hashtable_const_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>::operator++()
  761. {
  762. const _Node* __old = _M_cur;
  763. _M_cur = _M_cur->_M_next;
  764. if (!_M_cur) {
  765. size_type __bucket = _M_ht->_M_bkt_num(__old->_M_val);
  766. while (!_M_cur && ++__bucket < _M_ht->_M_buckets.size())
  767. _M_cur = _M_ht->_M_buckets[__bucket];
  768. }
  769. return *this;
  770. }
  771. template <class _Val, class _Key, class _HF, class _ExK, class _EqK,
  772. class _All>
  773. inline _Hashtable_const_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>
  774. _Hashtable_const_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>::operator++(int)
  775. {
  776. const_iterator __tmp = *this;
  777. ++*this;
  778. return __tmp;
  779. }
  780. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  781. bool operator==(const hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>& __ht1,
  782. const hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>& __ht2)
  783. {
  784. typedef typename hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::_Node _Node;
  785. if (__ht1._M_buckets.size() != __ht2._M_buckets.size())
  786. return false;
  787. for (int __n = 0; __n < __ht1._M_buckets.size(); ++__n) {
  788. _Node* __cur1 = __ht1._M_buckets[__n];
  789. _Node* __cur2 = __ht2._M_buckets[__n];
  790. for ( ; __cur1 && __cur2 && __cur1->_M_val == __cur2->_M_val;
  791. __cur1 = __cur1->_M_next, __cur2 = __cur2->_M_next)
  792. {}
  793. if (__cur1 || __cur2)
  794. return false;
  795. }
  796. return true;
  797. }
  798. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  799. inline bool operator!=(const hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>& __ht1,
  800. const hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>& __ht2) {
  801. return !(__ht1 == __ht2);
  802. }
  803. template <class _Val, class _Key, class _HF, class _Extract, class _EqKey,
  804. class _All>
  805. inline void swap(hashtable<_Val, _Key, _HF, _Extract, _EqKey, _All>& __ht1,
  806. hashtable<_Val, _Key, _HF, _Extract, _EqKey, _All>& __ht2) {
  807. __ht1.swap(__ht2);
  808. }
  809. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  810. @KWSYS_NAMESPACE@_stl::pair<@KWSYS_NAMESPACE@_CXX_DECL_TYPENAME hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::iterator, bool>
  811. hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>
  812. ::insert_unique_noresize(const value_type& __obj)
  813. {
  814. const size_type __n = _M_bkt_num(__obj);
  815. _Node* __first = _M_buckets[__n];
  816. for (_Node* __cur = __first; __cur; __cur = __cur->_M_next)
  817. if (_M_equals(_M_get_key(__cur->_M_val), _M_get_key(__obj)))
  818. return @KWSYS_NAMESPACE@_stl::pair<iterator, bool>(iterator(__cur, this), false);
  819. _Node* __tmp = _M_new_node(__obj);
  820. __tmp->_M_next = __first;
  821. _M_buckets[__n] = __tmp;
  822. ++_M_num_elements;
  823. return @KWSYS_NAMESPACE@_stl::pair<iterator, bool>(iterator(__tmp, this), true);
  824. }
  825. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  826. typename hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::iterator
  827. hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>
  828. ::insert_equal_noresize(const value_type& __obj)
  829. {
  830. const size_type __n = _M_bkt_num(__obj);
  831. _Node* __first = _M_buckets[__n];
  832. for (_Node* __cur = __first; __cur; __cur = __cur->_M_next)
  833. if (_M_equals(_M_get_key(__cur->_M_val), _M_get_key(__obj))) {
  834. _Node* __tmp = _M_new_node(__obj);
  835. __tmp->_M_next = __cur->_M_next;
  836. __cur->_M_next = __tmp;
  837. ++_M_num_elements;
  838. return iterator(__tmp, this);
  839. }
  840. _Node* __tmp = _M_new_node(__obj);
  841. __tmp->_M_next = __first;
  842. _M_buckets[__n] = __tmp;
  843. ++_M_num_elements;
  844. return iterator(__tmp, this);
  845. }
  846. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  847. typename hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::reference
  848. hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::find_or_insert(const value_type& __obj)
  849. {
  850. resize(_M_num_elements + 1);
  851. size_type __n = _M_bkt_num(__obj);
  852. _Node* __first = _M_buckets[__n];
  853. for (_Node* __cur = __first; __cur; __cur = __cur->_M_next)
  854. if (_M_equals(_M_get_key(__cur->_M_val), _M_get_key(__obj)))
  855. return __cur->_M_val;
  856. _Node* __tmp = _M_new_node(__obj);
  857. __tmp->_M_next = __first;
  858. _M_buckets[__n] = __tmp;
  859. ++_M_num_elements;
  860. return __tmp->_M_val;
  861. }
  862. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  863. @KWSYS_NAMESPACE@_stl::pair<@KWSYS_NAMESPACE@_CXX_DECL_TYPENAME hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::iterator,
  864. @KWSYS_NAMESPACE@_CXX_DECL_TYPENAME hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::iterator>
  865. hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::equal_range(const key_type& __key)
  866. {
  867. typedef @KWSYS_NAMESPACE@_stl::pair<iterator, iterator> _Pii;
  868. const size_type __n = _M_bkt_num_key(__key);
  869. for (_Node* __first = _M_buckets[__n]; __first; __first = __first->_M_next)
  870. if (_M_equals(_M_get_key(__first->_M_val), __key)) {
  871. for (_Node* __cur = __first->_M_next; __cur; __cur = __cur->_M_next)
  872. if (!_M_equals(_M_get_key(__cur->_M_val), __key))
  873. return _Pii(iterator(__first, this), iterator(__cur, this));
  874. for (size_type __m = __n + 1; __m < _M_buckets.size(); ++__m)
  875. if (_M_buckets[__m])
  876. return _Pii(iterator(__first, this),
  877. iterator(_M_buckets[__m], this));
  878. return _Pii(iterator(__first, this), end());
  879. }
  880. return _Pii(end(), end());
  881. }
  882. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  883. @KWSYS_NAMESPACE@_stl::pair<@KWSYS_NAMESPACE@_CXX_DECL_TYPENAME hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::const_iterator,
  884. @KWSYS_NAMESPACE@_CXX_DECL_TYPENAME hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::const_iterator>
  885. hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>
  886. ::equal_range(const key_type& __key) const
  887. {
  888. typedef @KWSYS_NAMESPACE@_stl::pair<const_iterator, const_iterator> _Pii;
  889. const size_type __n = _M_bkt_num_key(__key);
  890. for (const _Node* __first = _M_buckets[__n] ;
  891. __first;
  892. __first = __first->_M_next) {
  893. if (_M_equals(_M_get_key(__first->_M_val), __key)) {
  894. for (const _Node* __cur = __first->_M_next;
  895. __cur;
  896. __cur = __cur->_M_next)
  897. if (!_M_equals(_M_get_key(__cur->_M_val), __key))
  898. return _Pii(const_iterator(__first, this),
  899. const_iterator(__cur, this));
  900. for (size_type __m = __n + 1; __m < _M_buckets.size(); ++__m)
  901. if (_M_buckets[__m])
  902. return _Pii(const_iterator(__first, this),
  903. const_iterator(_M_buckets[__m], this));
  904. return _Pii(const_iterator(__first, this), end());
  905. }
  906. }
  907. return _Pii(end(), end());
  908. }
  909. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  910. typename hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::size_type
  911. hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::erase(const key_type& __key)
  912. {
  913. const size_type __n = _M_bkt_num_key(__key);
  914. _Node* __first = _M_buckets[__n];
  915. size_type __erased = 0;
  916. if (__first) {
  917. _Node* __cur = __first;
  918. _Node* __next = __cur->_M_next;
  919. while (__next) {
  920. if (_M_equals(_M_get_key(__next->_M_val), __key)) {
  921. __cur->_M_next = __next->_M_next;
  922. _M_delete_node(__next);
  923. __next = __cur->_M_next;
  924. ++__erased;
  925. --_M_num_elements;
  926. }
  927. else {
  928. __cur = __next;
  929. __next = __cur->_M_next;
  930. }
  931. }
  932. if (_M_equals(_M_get_key(__first->_M_val), __key)) {
  933. _M_buckets[__n] = __first->_M_next;
  934. _M_delete_node(__first);
  935. ++__erased;
  936. --_M_num_elements;
  937. }
  938. }
  939. return __erased;
  940. }
  941. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  942. void hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::erase(const iterator& __it)
  943. {
  944. _Node* __p = __it._M_cur;
  945. if (__p) {
  946. const size_type __n = _M_bkt_num(__p->_M_val);
  947. _Node* __cur = _M_buckets[__n];
  948. if (__cur == __p) {
  949. _M_buckets[__n] = __cur->_M_next;
  950. _M_delete_node(__cur);
  951. --_M_num_elements;
  952. }
  953. else {
  954. _Node* __next = __cur->_M_next;
  955. while (__next) {
  956. if (__next == __p) {
  957. __cur->_M_next = __next->_M_next;
  958. _M_delete_node(__next);
  959. --_M_num_elements;
  960. break;
  961. }
  962. else {
  963. __cur = __next;
  964. __next = __cur->_M_next;
  965. }
  966. }
  967. }
  968. }
  969. }
  970. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  971. void hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>
  972. ::erase(iterator __first, iterator __last)
  973. {
  974. size_type __f_bucket = __first._M_cur ?
  975. _M_bkt_num(__first._M_cur->_M_val) : _M_buckets.size();
  976. size_type __l_bucket = __last._M_cur ?
  977. _M_bkt_num(__last._M_cur->_M_val) : _M_buckets.size();
  978. if (__first._M_cur == __last._M_cur)
  979. return;
  980. else if (__f_bucket == __l_bucket)
  981. _M_erase_bucket(__f_bucket, __first._M_cur, __last._M_cur);
  982. else {
  983. _M_erase_bucket(__f_bucket, __first._M_cur, 0);
  984. for (size_type __n = __f_bucket + 1; __n < __l_bucket; ++__n)
  985. _M_erase_bucket(__n, 0);
  986. if (__l_bucket != _M_buckets.size())
  987. _M_erase_bucket(__l_bucket, __last._M_cur);
  988. }
  989. }
  990. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  991. inline void
  992. hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::erase(const_iterator __first,
  993. const_iterator __last)
  994. {
  995. erase(iterator(const_cast<_Node*>(__first._M_cur),
  996. const_cast<hashtable*>(__first._M_ht)),
  997. iterator(const_cast<_Node*>(__last._M_cur),
  998. const_cast<hashtable*>(__last._M_ht)));
  999. }
  1000. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  1001. inline void
  1002. hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::erase(const const_iterator& __it)
  1003. {
  1004. erase(iterator(const_cast<_Node*>(__it._M_cur),
  1005. const_cast<hashtable*>(__it._M_ht)));
  1006. }
  1007. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  1008. void hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>
  1009. ::resize(size_type __num_elements_hint)
  1010. {
  1011. const size_type __old_n = _M_buckets.size();
  1012. if (__num_elements_hint > __old_n) {
  1013. const size_type __n = _M_next_size(__num_elements_hint);
  1014. if (__n > __old_n) {
  1015. _M_buckets_type __tmp(
  1016. __n, (_Node*)(0)
  1017. @KWSYS_NAMESPACE@_HASH_BUCKETS_GET_ALLOCATOR(_M_buckets));
  1018. try {
  1019. for (size_type __bucket = 0; __bucket < __old_n; ++__bucket) {
  1020. _Node* __first = _M_buckets[__bucket];
  1021. while (__first) {
  1022. size_type __new_bucket = _M_bkt_num(__first->_M_val, __n);
  1023. _M_buckets[__bucket] = __first->_M_next;
  1024. __first->_M_next = __tmp[__new_bucket];
  1025. __tmp[__new_bucket] = __first;
  1026. __first = _M_buckets[__bucket];
  1027. }
  1028. }
  1029. _M_buckets.swap(__tmp);
  1030. }
  1031. catch(...) {
  1032. for (size_type __bucket = 0; __bucket < __tmp.size(); ++__bucket) {
  1033. while (__tmp[__bucket]) {
  1034. _Node* __next = __tmp[__bucket]->_M_next;
  1035. _M_delete_node(__tmp[__bucket]);
  1036. __tmp[__bucket] = __next;
  1037. }
  1038. }
  1039. throw;
  1040. }
  1041. }
  1042. }
  1043. }
  1044. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  1045. void hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>
  1046. ::_M_erase_bucket(const size_type __n, _Node* __first, _Node* __last)
  1047. {
  1048. _Node* __cur = _M_buckets[__n];
  1049. if (__cur == __first)
  1050. _M_erase_bucket(__n, __last);
  1051. else {
  1052. _Node* __next;
  1053. for (__next = __cur->_M_next;
  1054. __next != __first;
  1055. __cur = __next, __next = __cur->_M_next)
  1056. ;
  1057. while (__next != __last) {
  1058. __cur->_M_next = __next->_M_next;
  1059. _M_delete_node(__next);
  1060. __next = __cur->_M_next;
  1061. --_M_num_elements;
  1062. }
  1063. }
  1064. }
  1065. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  1066. void hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>
  1067. ::_M_erase_bucket(const size_type __n, _Node* __last)
  1068. {
  1069. _Node* __cur = _M_buckets[__n];
  1070. while (__cur != __last) {
  1071. _Node* __next = __cur->_M_next;
  1072. _M_delete_node(__cur);
  1073. __cur = __next;
  1074. _M_buckets[__n] = __cur;
  1075. --_M_num_elements;
  1076. }
  1077. }
  1078. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  1079. void hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::clear()
  1080. {
  1081. for (size_type __i = 0; __i < _M_buckets.size(); ++__i) {
  1082. _Node* __cur = _M_buckets[__i];
  1083. while (__cur != 0) {
  1084. _Node* __next = __cur->_M_next;
  1085. _M_delete_node(__cur);
  1086. __cur = __next;
  1087. }
  1088. _M_buckets[__i] = 0;
  1089. }
  1090. _M_num_elements = 0;
  1091. }
  1092. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  1093. void hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>
  1094. ::_M_copy_from(const hashtable& __ht)
  1095. {
  1096. _M_buckets.clear();
  1097. _M_buckets.reserve(__ht._M_buckets.size());
  1098. _M_buckets.insert(_M_buckets.end(), __ht._M_buckets.size(), (_Node*) 0);
  1099. try {
  1100. for (size_type __i = 0; __i < __ht._M_buckets.size(); ++__i) {
  1101. const _Node* __cur = __ht._M_buckets[__i];
  1102. if (__cur) {
  1103. _Node* __copy = _M_new_node(__cur->_M_val);
  1104. _M_buckets[__i] = __copy;
  1105. for (_Node* __next = __cur->_M_next;
  1106. __next;
  1107. __cur = __next, __next = __cur->_M_next) {
  1108. __copy->_M_next = _M_new_node(__next->_M_val);
  1109. __copy = __copy->_M_next;
  1110. }
  1111. }
  1112. }
  1113. _M_num_elements = __ht._M_num_elements;
  1114. }
  1115. catch(...) {clear(); throw;}
  1116. }
  1117. } // namespace @KWSYS_NAMESPACE@
  1118. // Normally the comparison operators should be found in the @KWSYS_NAMESPACE@
  1119. // namespace by argument dependent lookup. For compilers that do not
  1120. // support it we must bring them into the global namespace now.
  1121. #if !@KWSYS_NAMESPACE@_CXX_HAS_ARGUMENT_DEPENDENT_LOOKUP
  1122. using @KWSYS_NAMESPACE@::operator==;
  1123. using @KWSYS_NAMESPACE@::operator!=;
  1124. #endif
  1125. #if defined(_MSC_VER)
  1126. # pragma warning (pop)
  1127. #endif
  1128. #endif