hashtable.hxx.in 41 KB

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