]>
Commit | Line | Data |
---|---|---|
180ecd6a MA |
1 | // Internal header for TR1 unordered_set and unordered_map -*- C++ -*- |
2 | ||
5004040e | 3 | // Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
180ecd6a MA |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
180ecd6a MA |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
19 | ||
20 | // You should have received a copy of the GNU General Public License and | |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
909a9d44 | 24 | |
e133ace8 PC |
25 | /** @file tr1_impl/hashtable |
26 | * This is an internal header file, included by other library headers. | |
27 | * You should not attempt to use it directly. | |
180ecd6a MA |
28 | */ |
29 | ||
30 | // This header file defines std::tr1::hashtable, which is used to | |
31 | // implement std::tr1::unordered_set, std::tr1::unordered_map, | |
32 | // std::tr1::unordered_multiset, and std::tr1::unordered_multimap. | |
33 | // hashtable has many template parameters, partly to accommodate | |
34 | // the differences between those four classes and partly to | |
ece84738 | 35 | // accommodate policy choices that go beyond TR1 specifications. |
180ecd6a | 36 | |
180ecd6a MA |
37 | // Class template hashtable attempts to encapsulate all reasonable |
38 | // variation among hash tables that use chaining. It does not handle | |
39 | // open addressing. | |
40 | ||
41 | // References: | |
42 | // M. Austern, "A Proposal to Add Hash Tables to the Standard | |
43 | // Library (revision 4)," WG21 Document N1456=03-0039, 2003. | |
44 | // D. E. Knuth, The Art of Computer Programming, v. 3, Sorting and Searching. | |
b82f782b BK |
45 | // A. Tavori and V. Dreizin, "Policy-Based Data Structures", 2004. |
46 | // http://gcc.gnu.org/onlinedocs/libstdc++/ext/pb_ds/index.html | |
180ecd6a | 47 | |
e133ace8 | 48 | #include <tr1_impl/hashtable_policy.h> |
180ecd6a | 49 | |
7ffd2d94 PC |
50 | namespace std |
51 | { | |
3b2524b1 PC |
52 | namespace tr1 |
53 | { | |
95cefe5f | 54 | // Class template _Hashtable, class definition. |
7ffd2d94 | 55 | |
95cefe5f | 56 | // Meaning of class template _Hashtable's template parameters |
7ffd2d94 | 57 | |
95cefe5f | 58 | // _Key and _Value: arbitrary CopyConstructible types. |
7ffd2d94 | 59 | |
95cefe5f | 60 | // _Allocator: an allocator type ([lib.allocator.requirements]) whose |
1f9c69a9 PC |
61 | // value type is Value. As a conforming extension, we allow for |
62 | // value type != Value. | |
63 | ||
95cefe5f PC |
64 | // _ExtractKey: function object that takes a object of type Value |
65 | // and returns a value of type _Key. | |
7ffd2d94 | 66 | |
95cefe5f | 67 | // _Equal: function object that takes two objects of type k and returns |
7ffd2d94 PC |
68 | // a bool-like value that is true if the two objects are considered equal. |
69 | ||
95cefe5f | 70 | // _H1: the hash function. A unary function object with argument type |
7ffd2d94 PC |
71 | // Key and result type size_t. Return values should be distributed |
72 | // over the entire range [0, numeric_limits<size_t>:::max()]. | |
73 | ||
95cefe5f | 74 | // _H2: the range-hashing function (in the terminology of Tavori and |
7ffd2d94 PC |
75 | // Dreizin). A binary function object whose argument types and result |
76 | // type are all size_t. Given arguments r and N, the return value is | |
77 | // in the range [0, N). | |
78 | ||
95cefe5f PC |
79 | // _Hash: the ranged hash function (Tavori and Dreizin). A binary function |
80 | // whose argument types are _Key and size_t and whose result type is | |
7ffd2d94 | 81 | // size_t. Given arguments k and N, the return value is in the range |
95cefe5f PC |
82 | // [0, N). Default: hash(k, N) = h2(h1(k), N). If _Hash is anything other |
83 | // than the default, _H1 and _H2 are ignored. | |
7ffd2d94 | 84 | |
95cefe5f PC |
85 | // _RehashPolicy: Policy class with three members, all of which govern |
86 | // the bucket count. _M_next_bkt(n) returns a bucket count no smaller | |
87 | // than n. _M_bkt_for_elements(n) returns a bucket count appropriate | |
88 | // for an element count of n. _M_need_rehash(n_bkt, n_elt, n_ins) | |
7ffd2d94 PC |
89 | // determines whether, if the current bucket count is n_bkt and the |
90 | // current element count is n_elt, we need to increase the bucket | |
91 | // count. If so, returns make_pair(true, n), where n is the new | |
92 | // bucket count. If not, returns make_pair(false, <anything>). | |
93 | ||
94 | // ??? Right now it is hard-wired that the number of buckets never | |
1f9c69a9 | 95 | // shrinks. Should we allow _RehashPolicy to change that? |
7ffd2d94 | 96 | |
95cefe5f | 97 | // __cache_hash_code: bool. true if we store the value of the hash |
7ffd2d94 PC |
98 | // function along with the value. This is a time-space tradeoff. |
99 | // Storing it may improve lookup speed by reducing the number of times | |
100 | // we need to call the Equal function. | |
101 | ||
95cefe5f | 102 | // __constant_iterators: bool. true if iterator and const_iterator are |
3c9b5053 PC |
103 | // both constant iterator types. This is true for unordered_set and |
104 | // unordered_multiset, false for unordered_map and unordered_multimap. | |
7ffd2d94 | 105 | |
95cefe5f | 106 | // __unique_keys: bool. true if the return value of _Hashtable::count(k) |
7ffd2d94 PC |
107 | // is always at most one, false if it may be an arbitrary number. This |
108 | // true for unordered_set and unordered_map, false for unordered_multiset | |
109 | // and unordered_multimap. | |
110 | ||
95cefe5f PC |
111 | template<typename _Key, typename _Value, typename _Allocator, |
112 | typename _ExtractKey, typename _Equal, | |
113 | typename _H1, typename _H2, typename _Hash, | |
114 | typename _RehashPolicy, | |
115 | bool __cache_hash_code, | |
116 | bool __constant_iterators, | |
117 | bool __unique_keys> | |
118 | class _Hashtable | |
119 | : public __detail::_Rehash_base<_RehashPolicy, | |
120 | _Hashtable<_Key, _Value, _Allocator, | |
121 | _ExtractKey, | |
122 | _Equal, _H1, _H2, _Hash, | |
123 | _RehashPolicy, | |
124 | __cache_hash_code, | |
125 | __constant_iterators, | |
126 | __unique_keys> >, | |
127 | public __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal, | |
128 | _H1, _H2, _Hash, __cache_hash_code>, | |
129 | public __detail::_Map_base<_Key, _Value, _ExtractKey, __unique_keys, | |
130 | _Hashtable<_Key, _Value, _Allocator, | |
131 | _ExtractKey, | |
132 | _Equal, _H1, _H2, _Hash, | |
133 | _RehashPolicy, | |
134 | __cache_hash_code, | |
135 | __constant_iterators, | |
136 | __unique_keys> > | |
7ffd2d94 PC |
137 | { |
138 | public: | |
95cefe5f PC |
139 | typedef _Allocator allocator_type; |
140 | typedef _Value value_type; | |
141 | typedef _Key key_type; | |
142 | typedef _Equal key_equal; | |
1f9c69a9 PC |
143 | // mapped_type, if present, comes from _Map_base. |
144 | // hasher, if present, comes from _Hash_code_base. | |
95cefe5f PC |
145 | typedef typename _Allocator::difference_type difference_type; |
146 | typedef typename _Allocator::size_type size_type; | |
eafb2f2d PC |
147 | typedef typename _Allocator::pointer pointer; |
148 | typedef typename _Allocator::const_pointer const_pointer; | |
95cefe5f PC |
149 | typedef typename _Allocator::reference reference; |
150 | typedef typename _Allocator::const_reference const_reference; | |
7ffd2d94 | 151 | |
95cefe5f PC |
152 | typedef __detail::_Node_iterator<value_type, __constant_iterators, |
153 | __cache_hash_code> | |
03ba64bb | 154 | local_iterator; |
95cefe5f PC |
155 | typedef __detail::_Node_const_iterator<value_type, |
156 | __constant_iterators, | |
157 | __cache_hash_code> | |
03ba64bb | 158 | const_local_iterator; |
7ffd2d94 | 159 | |
95cefe5f PC |
160 | typedef __detail::_Hashtable_iterator<value_type, __constant_iterators, |
161 | __cache_hash_code> | |
03ba64bb | 162 | iterator; |
95cefe5f PC |
163 | typedef __detail::_Hashtable_const_iterator<value_type, |
164 | __constant_iterators, | |
165 | __cache_hash_code> | |
03ba64bb | 166 | const_iterator; |
7ffd2d94 | 167 | |
51631813 FB |
168 | template<typename _Key2, typename _Value2, typename _Ex2, bool __unique2, |
169 | typename _Hashtable2> | |
95cefe5f | 170 | friend struct __detail::_Map_base; |
f99b2be1 | 171 | |
7ffd2d94 | 172 | private: |
95cefe5f PC |
173 | typedef __detail::_Hash_node<_Value, __cache_hash_code> _Node; |
174 | typedef typename _Allocator::template rebind<_Node>::other | |
175 | _Node_allocator_type; | |
176 | typedef typename _Allocator::template rebind<_Node*>::other | |
177 | _Bucket_allocator_type; | |
178 | ||
1f9c69a9 PC |
179 | typedef typename _Allocator::template rebind<_Value>::other |
180 | _Value_allocator_type; | |
181 | ||
95cefe5f PC |
182 | _Node_allocator_type _M_node_allocator; |
183 | _Node** _M_buckets; | |
184 | size_type _M_bucket_count; | |
185 | size_type _M_element_count; | |
186 | _RehashPolicy _M_rehash_policy; | |
7ffd2d94 | 187 | |
95cefe5f PC |
188 | _Node* |
189 | _M_allocate_node(const value_type& __v); | |
7ffd2d94 PC |
190 | |
191 | void | |
95cefe5f | 192 | _M_deallocate_node(_Node* __n); |
7ffd2d94 PC |
193 | |
194 | void | |
95cefe5f | 195 | _M_deallocate_nodes(_Node**, size_type); |
180ecd6a | 196 | |
95cefe5f PC |
197 | _Node** |
198 | _M_allocate_buckets(size_type __n); | |
7ffd2d94 PC |
199 | |
200 | void | |
95cefe5f | 201 | _M_deallocate_buckets(_Node**, size_type __n); |
7ffd2d94 | 202 | |
b82f782b BK |
203 | public: |
204 | // Constructor, destructor, assignment, swap | |
95cefe5f PC |
205 | _Hashtable(size_type __bucket_hint, |
206 | const _H1&, const _H2&, const _Hash&, | |
207 | const _Equal&, const _ExtractKey&, | |
208 | const allocator_type&); | |
7ffd2d94 | 209 | |
95cefe5f PC |
210 | template<typename _InputIterator> |
211 | _Hashtable(_InputIterator __first, _InputIterator __last, | |
212 | size_type __bucket_hint, | |
213 | const _H1&, const _H2&, const _Hash&, | |
214 | const _Equal&, const _ExtractKey&, | |
215 | const allocator_type&); | |
7ffd2d94 | 216 | |
95cefe5f | 217 | _Hashtable(const _Hashtable&); |
3b2524b1 | 218 | |
95cefe5f PC |
219 | _Hashtable& |
220 | operator=(const _Hashtable&); | |
2839c151 | 221 | |
95cefe5f | 222 | ~_Hashtable(); |
7ffd2d94 | 223 | |
95cefe5f | 224 | void swap(_Hashtable&); |
7ffd2d94 | 225 | |
b82f782b | 226 | // Basic container operations |
7ffd2d94 PC |
227 | iterator |
228 | begin() | |
229 | { | |
95cefe5f PC |
230 | iterator __i(_M_buckets); |
231 | if (!__i._M_cur_node) | |
232 | __i._M_incr_bucket(); | |
233 | return __i; | |
7ffd2d94 | 234 | } |
180ecd6a | 235 | |
7ffd2d94 PC |
236 | const_iterator |
237 | begin() const | |
238 | { | |
95cefe5f PC |
239 | const_iterator __i(_M_buckets); |
240 | if (!__i._M_cur_node) | |
241 | __i._M_incr_bucket(); | |
242 | return __i; | |
7ffd2d94 | 243 | } |
180ecd6a | 244 | |
7ffd2d94 PC |
245 | iterator |
246 | end() | |
95cefe5f | 247 | { return iterator(_M_buckets + _M_bucket_count); } |
180ecd6a | 248 | |
7ffd2d94 PC |
249 | const_iterator |
250 | end() const | |
95cefe5f | 251 | { return const_iterator(_M_buckets + _M_bucket_count); } |
180ecd6a | 252 | |
7ffd2d94 PC |
253 | size_type |
254 | size() const | |
95cefe5f | 255 | { return _M_element_count; } |
7ffd2d94 PC |
256 | |
257 | bool | |
258 | empty() const | |
259 | { return size() == 0; } | |
180ecd6a | 260 | |
7ffd2d94 PC |
261 | allocator_type |
262 | get_allocator() const | |
1f9c69a9 PC |
263 | { return allocator_type(_M_node_allocator); } |
264 | ||
265 | _Value_allocator_type | |
266 | _M_get_Value_allocator() const | |
267 | { return _Value_allocator_type(_M_node_allocator); } | |
268 | ||
7ffd2d94 PC |
269 | size_type |
270 | max_size() const | |
d090f47a | 271 | { return _M_node_allocator.max_size(); } |
7ffd2d94 | 272 | |
b82f782b | 273 | // Observers |
774b9d21 PC |
274 | key_equal |
275 | key_eq() const | |
95cefe5f | 276 | { return this->_M_eq; } |
774b9d21 | 277 | |
95cefe5f | 278 | // hash_function, if present, comes from _Hash_code_base. |
774b9d21 | 279 | |
b82f782b | 280 | // Bucket operations |
7ffd2d94 PC |
281 | size_type |
282 | bucket_count() const | |
95cefe5f | 283 | { return _M_bucket_count; } |
180ecd6a | 284 | |
7ffd2d94 PC |
285 | size_type |
286 | max_bucket_count() const | |
287 | { return max_size(); } | |
288 | ||
289 | size_type | |
95cefe5f PC |
290 | bucket_size(size_type __n) const |
291 | { return std::distance(begin(__n), end(__n)); } | |
7ffd2d94 | 292 | |
774b9d21 | 293 | size_type |
95cefe5f | 294 | bucket(const key_type& __k) const |
7ffd2d94 | 295 | { |
95cefe5f PC |
296 | return this->_M_bucket_index(__k, this->_M_hash_code(__k), |
297 | bucket_count()); | |
7ffd2d94 | 298 | } |
180ecd6a | 299 | |
7ffd2d94 | 300 | local_iterator |
95cefe5f PC |
301 | begin(size_type __n) |
302 | { return local_iterator(_M_buckets[__n]); } | |
64bd554c | 303 | |
7ffd2d94 | 304 | local_iterator |
8b5bc374 | 305 | end(size_type) |
7ffd2d94 | 306 | { return local_iterator(0); } |
64bd554c | 307 | |
7ffd2d94 | 308 | const_local_iterator |
95cefe5f PC |
309 | begin(size_type __n) const |
310 | { return const_local_iterator(_M_buckets[__n]); } | |
64bd554c | 311 | |
7ffd2d94 | 312 | const_local_iterator |
8b5bc374 | 313 | end(size_type) const |
7ffd2d94 PC |
314 | { return const_local_iterator(0); } |
315 | ||
316 | float | |
317 | load_factor() const | |
318 | { | |
319 | return static_cast<float>(size()) / static_cast<float>(bucket_count()); | |
320 | } | |
f8190ffc | 321 | |
95cefe5f | 322 | // max_load_factor, if present, comes from _Rehash_base. |
7ffd2d94 PC |
323 | |
324 | // Generalization of max_load_factor. Extension, not found in TR1. Only | |
95cefe5f PC |
325 | // useful if _RehashPolicy is something other than the default. |
326 | const _RehashPolicy& | |
327 | __rehash_policy() const | |
328 | { return _M_rehash_policy; } | |
7ffd2d94 PC |
329 | |
330 | void | |
95cefe5f | 331 | __rehash_policy(const _RehashPolicy&); |
7ffd2d94 | 332 | |
b82f782b | 333 | // Lookup. |
7ffd2d94 | 334 | iterator |
95cefe5f | 335 | find(const key_type& __k); |
7ffd2d94 PC |
336 | |
337 | const_iterator | |
95cefe5f | 338 | find(const key_type& __k) const; |
7ffd2d94 PC |
339 | |
340 | size_type | |
95cefe5f | 341 | count(const key_type& __k) const; |
7ffd2d94 PC |
342 | |
343 | std::pair<iterator, iterator> | |
95cefe5f | 344 | equal_range(const key_type& __k); |
7ffd2d94 PC |
345 | |
346 | std::pair<const_iterator, const_iterator> | |
95cefe5f | 347 | equal_range(const key_type& __k) const; |
7ffd2d94 | 348 | |
f99b2be1 | 349 | private: // Find, insert and erase helper functions |
7ffd2d94 PC |
350 | // ??? This dispatching is a workaround for the fact that we don't |
351 | // have partial specialization of member templates; it would be | |
95cefe5f | 352 | // better to just specialize insert on __unique_keys. There may be a |
7ffd2d94 | 353 | // cleaner workaround. |
95cefe5f | 354 | typedef typename __gnu_cxx::__conditional_type<__unique_keys, |
105c6331 | 355 | std::pair<iterator, bool>, iterator>::__type |
95cefe5f | 356 | _Insert_Return_Type; |
7ffd2d94 | 357 | |
95cefe5f PC |
358 | typedef typename __gnu_cxx::__conditional_type<__unique_keys, |
359 | std::_Select1st<_Insert_Return_Type>, | |
360 | std::_Identity<_Insert_Return_Type> | |
105c6331 | 361 | >::__type |
95cefe5f | 362 | _Insert_Conv_Type; |
3c9b5053 | 363 | |
95cefe5f PC |
364 | _Node* |
365 | _M_find_node(_Node*, const key_type&, | |
366 | typename _Hashtable::_Hash_code_type) const; | |
f99b2be1 PC |
367 | |
368 | iterator | |
95cefe5f PC |
369 | _M_insert_bucket(const value_type&, size_type, |
370 | typename _Hashtable::_Hash_code_type); | |
7ffd2d94 PC |
371 | |
372 | std::pair<iterator, bool> | |
3b2524b1 | 373 | _M_insert(const value_type&, std::tr1::true_type); |
f99b2be1 | 374 | |
7ffd2d94 | 375 | iterator |
3b2524b1 | 376 | _M_insert(const value_type&, std::tr1::false_type); |
7ffd2d94 | 377 | |
76f5f441 | 378 | void |
95cefe5f | 379 | _M_erase_node(_Node*, _Node**); |
76f5f441 | 380 | |
b82f782b BK |
381 | public: |
382 | // Insert and erase | |
95cefe5f PC |
383 | _Insert_Return_Type |
384 | insert(const value_type& __v) | |
3b2524b1 | 385 | { return _M_insert(__v, std::tr1::integral_constant<bool, |
95cefe5f | 386 | __unique_keys>()); } |
3c9b5053 PC |
387 | |
388 | iterator | |
95cefe5f PC |
389 | insert(iterator, const value_type& __v) |
390 | { return iterator(_Insert_Conv_Type()(this->insert(__v))); } | |
f99b2be1 | 391 | |
3c9b5053 | 392 | const_iterator |
95cefe5f PC |
393 | insert(const_iterator, const value_type& __v) |
394 | { return const_iterator(_Insert_Conv_Type()(this->insert(__v))); } | |
180ecd6a | 395 | |
95cefe5f | 396 | template<typename _InputIterator> |
7ffd2d94 | 397 | void |
95cefe5f | 398 | insert(_InputIterator __first, _InputIterator __last); |
180ecd6a | 399 | |
3c9b5053 PC |
400 | iterator |
401 | erase(iterator); | |
76f5f441 | 402 | |
3c9b5053 PC |
403 | const_iterator |
404 | erase(const_iterator); | |
76f5f441 | 405 | |
7ffd2d94 PC |
406 | size_type |
407 | erase(const key_type&); | |
3c9b5053 PC |
408 | |
409 | iterator | |
410 | erase(iterator, iterator); | |
76f5f441 | 411 | |
3c9b5053 | 412 | const_iterator |
7ffd2d94 | 413 | erase(const_iterator, const_iterator); |
3c9b5053 | 414 | |
7ffd2d94 PC |
415 | void |
416 | clear(); | |
417 | ||
f8190ffc | 418 | // Set number of buckets to be appropriate for container of n element. |
95cefe5f | 419 | void rehash(size_type __n); |
7ffd2d94 PC |
420 | |
421 | private: | |
422 | // Unconditionally change size of bucket array to n. | |
95cefe5f | 423 | void _M_rehash(size_type __n); |
7ffd2d94 PC |
424 | }; |
425 | ||
03ba64bb | 426 | |
95cefe5f PC |
427 | // Definitions of class template _Hashtable's out-of-line member functions. |
428 | template<typename _Key, typename _Value, | |
429 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
430 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
431 | bool __chc, bool __cit, bool __uk> | |
432 | typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
433 | _H1, _H2, _Hash, _RehashPolicy, | |
434 | __chc, __cit, __uk>::_Node* | |
435 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
436 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
437 | _M_allocate_node(const value_type& __v) | |
7ffd2d94 | 438 | { |
95cefe5f | 439 | _Node* __n = _M_node_allocator.allocate(1); |
bc2631e0 | 440 | __try |
7ffd2d94 | 441 | { |
1f9c69a9 | 442 | _M_get_Value_allocator().construct(&__n->_M_v, __v); |
95cefe5f PC |
443 | __n->_M_next = 0; |
444 | return __n; | |
7ffd2d94 | 445 | } |
bc2631e0 | 446 | __catch(...) |
7ffd2d94 | 447 | { |
95cefe5f | 448 | _M_node_allocator.deallocate(__n, 1); |
40f15089 | 449 | __throw_exception_again; |
7ffd2d94 PC |
450 | } |
451 | } | |
180ecd6a | 452 | |
95cefe5f PC |
453 | template<typename _Key, typename _Value, |
454 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
455 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
456 | bool __chc, bool __cit, bool __uk> | |
7ffd2d94 | 457 | void |
95cefe5f PC |
458 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, |
459 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
460 | _M_deallocate_node(_Node* __n) | |
7ffd2d94 | 461 | { |
1f9c69a9 | 462 | _M_get_Value_allocator().destroy(&__n->_M_v); |
95cefe5f | 463 | _M_node_allocator.deallocate(__n, 1); |
180ecd6a | 464 | } |
180ecd6a | 465 | |
95cefe5f PC |
466 | template<typename _Key, typename _Value, |
467 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
468 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
469 | bool __chc, bool __cit, bool __uk> | |
7ffd2d94 | 470 | void |
95cefe5f PC |
471 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, |
472 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
473 | _M_deallocate_nodes(_Node** __array, size_type __n) | |
7ffd2d94 | 474 | { |
95cefe5f | 475 | for (size_type __i = 0; __i < __n; ++__i) |
7ffd2d94 | 476 | { |
95cefe5f PC |
477 | _Node* __p = __array[__i]; |
478 | while (__p) | |
7ffd2d94 | 479 | { |
95cefe5f PC |
480 | _Node* __tmp = __p; |
481 | __p = __p->_M_next; | |
482 | _M_deallocate_node(__tmp); | |
7ffd2d94 | 483 | } |
95cefe5f | 484 | __array[__i] = 0; |
7ffd2d94 PC |
485 | } |
486 | } | |
180ecd6a | 487 | |
95cefe5f PC |
488 | template<typename _Key, typename _Value, |
489 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
490 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
491 | bool __chc, bool __cit, bool __uk> | |
492 | typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
493 | _H1, _H2, _Hash, _RehashPolicy, | |
494 | __chc, __cit, __uk>::_Node** | |
495 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
496 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
497 | _M_allocate_buckets(size_type __n) | |
7ffd2d94 | 498 | { |
95cefe5f | 499 | _Bucket_allocator_type __alloc(_M_node_allocator); |
7ffd2d94 PC |
500 | |
501 | // We allocate one extra bucket to hold a sentinel, an arbitrary | |
502 | // non-null pointer. Iterator increment relies on this. | |
95cefe5f PC |
503 | _Node** __p = __alloc.allocate(__n + 1); |
504 | std::fill(__p, __p + __n, (_Node*) 0); | |
505 | __p[__n] = reinterpret_cast<_Node*>(0x1000); | |
506 | return __p; | |
7ffd2d94 | 507 | } |
180ecd6a | 508 | |
95cefe5f PC |
509 | template<typename _Key, typename _Value, |
510 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
511 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
512 | bool __chc, bool __cit, bool __uk> | |
7ffd2d94 | 513 | void |
95cefe5f PC |
514 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, |
515 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
516 | _M_deallocate_buckets(_Node** __p, size_type __n) | |
7ffd2d94 | 517 | { |
95cefe5f PC |
518 | _Bucket_allocator_type __alloc(_M_node_allocator); |
519 | __alloc.deallocate(__p, __n + 1); | |
7ffd2d94 | 520 | } |
180ecd6a | 521 | |
95cefe5f PC |
522 | template<typename _Key, typename _Value, |
523 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
524 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
525 | bool __chc, bool __cit, bool __uk> | |
526 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
527 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
528 | _Hashtable(size_type __bucket_hint, | |
529 | const _H1& __h1, const _H2& __h2, const _Hash& __h, | |
530 | const _Equal& __eq, const _ExtractKey& __exk, | |
531 | const allocator_type& __a) | |
532 | : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(), | |
533 | __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal, | |
534 | _H1, _H2, _Hash, __chc>(__exk, __eq, | |
535 | __h1, __h2, __h), | |
536 | __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(), | |
537 | _M_node_allocator(__a), | |
538 | _M_bucket_count(0), | |
539 | _M_element_count(0), | |
540 | _M_rehash_policy() | |
7ffd2d94 | 541 | { |
95cefe5f PC |
542 | _M_bucket_count = _M_rehash_policy._M_next_bkt(__bucket_hint); |
543 | _M_buckets = _M_allocate_buckets(_M_bucket_count); | |
7ffd2d94 | 544 | } |
180ecd6a | 545 | |
95cefe5f PC |
546 | template<typename _Key, typename _Value, |
547 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
548 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
549 | bool __chc, bool __cit, bool __uk> | |
550 | template<typename _InputIterator> | |
551 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
552 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
553 | _Hashtable(_InputIterator __f, _InputIterator __l, | |
1f9c69a9 PC |
554 | size_type __bucket_hint, |
555 | const _H1& __h1, const _H2& __h2, const _Hash& __h, | |
556 | const _Equal& __eq, const _ExtractKey& __exk, | |
557 | const allocator_type& __a) | |
95cefe5f PC |
558 | : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(), |
559 | __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal, | |
560 | _H1, _H2, _Hash, __chc>(__exk, __eq, | |
561 | __h1, __h2, __h), | |
562 | __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(), | |
563 | _M_node_allocator(__a), | |
564 | _M_bucket_count(0), | |
565 | _M_element_count(0), | |
566 | _M_rehash_policy() | |
7ffd2d94 | 567 | { |
95cefe5f PC |
568 | _M_bucket_count = std::max(_M_rehash_policy._M_next_bkt(__bucket_hint), |
569 | _M_rehash_policy. | |
570 | _M_bkt_for_elements(__detail:: | |
571 | __distance_fw(__f, | |
572 | __l))); | |
573 | _M_buckets = _M_allocate_buckets(_M_bucket_count); | |
bc2631e0 | 574 | __try |
7ffd2d94 | 575 | { |
95cefe5f PC |
576 | for (; __f != __l; ++__f) |
577 | this->insert(*__f); | |
7ffd2d94 | 578 | } |
bc2631e0 | 579 | __catch(...) |
7ffd2d94 PC |
580 | { |
581 | clear(); | |
95cefe5f | 582 | _M_deallocate_buckets(_M_buckets, _M_bucket_count); |
40f15089 | 583 | __throw_exception_again; |
7ffd2d94 | 584 | } |
180ecd6a | 585 | } |
7ffd2d94 | 586 | |
95cefe5f PC |
587 | template<typename _Key, typename _Value, |
588 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
589 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
590 | bool __chc, bool __cit, bool __uk> | |
591 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
592 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
593 | _Hashtable(const _Hashtable& __ht) | |
594 | : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(__ht), | |
595 | __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal, | |
596 | _H1, _H2, _Hash, __chc>(__ht), | |
597 | __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(__ht), | |
1f9c69a9 | 598 | _M_node_allocator(__ht._M_node_allocator), |
95cefe5f PC |
599 | _M_bucket_count(__ht._M_bucket_count), |
600 | _M_element_count(__ht._M_element_count), | |
601 | _M_rehash_policy(__ht._M_rehash_policy) | |
7ffd2d94 | 602 | { |
95cefe5f | 603 | _M_buckets = _M_allocate_buckets(_M_bucket_count); |
bc2631e0 | 604 | __try |
7ffd2d94 | 605 | { |
95cefe5f | 606 | for (size_type __i = 0; __i < __ht._M_bucket_count; ++__i) |
7ffd2d94 | 607 | { |
95cefe5f PC |
608 | _Node* __n = __ht._M_buckets[__i]; |
609 | _Node** __tail = _M_buckets + __i; | |
610 | while (__n) | |
7ffd2d94 | 611 | { |
95cefe5f PC |
612 | *__tail = _M_allocate_node(__n->_M_v); |
613 | this->_M_copy_code(*__tail, __n); | |
614 | __tail = &((*__tail)->_M_next); | |
615 | __n = __n->_M_next; | |
7ffd2d94 PC |
616 | } |
617 | } | |
618 | } | |
bc2631e0 | 619 | __catch(...) |
7ffd2d94 PC |
620 | { |
621 | clear(); | |
95cefe5f | 622 | _M_deallocate_buckets(_M_buckets, _M_bucket_count); |
40f15089 | 623 | __throw_exception_again; |
7ffd2d94 | 624 | } |
180ecd6a | 625 | } |
180ecd6a | 626 | |
95cefe5f PC |
627 | template<typename _Key, typename _Value, |
628 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
629 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
630 | bool __chc, bool __cit, bool __uk> | |
631 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
632 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>& | |
633 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
634 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
635 | operator=(const _Hashtable& __ht) | |
7ffd2d94 | 636 | { |
95cefe5f PC |
637 | _Hashtable __tmp(__ht); |
638 | this->swap(__tmp); | |
7ffd2d94 PC |
639 | return *this; |
640 | } | |
180ecd6a | 641 | |
95cefe5f PC |
642 | template<typename _Key, typename _Value, |
643 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
644 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
645 | bool __chc, bool __cit, bool __uk> | |
646 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
647 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
648 | ~_Hashtable() | |
7ffd2d94 PC |
649 | { |
650 | clear(); | |
95cefe5f | 651 | _M_deallocate_buckets(_M_buckets, _M_bucket_count); |
7ffd2d94 | 652 | } |
180ecd6a | 653 | |
95cefe5f PC |
654 | template<typename _Key, typename _Value, |
655 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
656 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
657 | bool __chc, bool __cit, bool __uk> | |
7ffd2d94 | 658 | void |
95cefe5f PC |
659 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, |
660 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
661 | swap(_Hashtable& __x) | |
7ffd2d94 PC |
662 | { |
663 | // The only base class with member variables is hash_code_base. We | |
95cefe5f | 664 | // define _Hash_code_base::_M_swap because different specializations |
7ffd2d94 | 665 | // have different members. |
95cefe5f PC |
666 | __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal, |
667 | _H1, _H2, _Hash, __chc>::_M_swap(__x); | |
7ffd2d94 | 668 | |
f7ace77f PC |
669 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
670 | // 431. Swapping containers with unequal allocators. | |
95cefe5f PC |
671 | std::__alloc_swap<_Node_allocator_type>::_S_do_it(_M_node_allocator, |
672 | __x._M_node_allocator); | |
f7ace77f | 673 | |
95cefe5f PC |
674 | std::swap(_M_rehash_policy, __x._M_rehash_policy); |
675 | std::swap(_M_buckets, __x._M_buckets); | |
676 | std::swap(_M_bucket_count, __x._M_bucket_count); | |
677 | std::swap(_M_element_count, __x._M_element_count); | |
7ffd2d94 | 678 | } |
180ecd6a | 679 | |
95cefe5f PC |
680 | template<typename _Key, typename _Value, |
681 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
682 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
683 | bool __chc, bool __cit, bool __uk> | |
7ffd2d94 | 684 | void |
95cefe5f PC |
685 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, |
686 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
687 | __rehash_policy(const _RehashPolicy& __pol) | |
7ffd2d94 | 688 | { |
95cefe5f PC |
689 | _M_rehash_policy = __pol; |
690 | size_type __n_bkt = __pol._M_bkt_for_elements(_M_element_count); | |
691 | if (__n_bkt > _M_bucket_count) | |
692 | _M_rehash(__n_bkt); | |
7ffd2d94 | 693 | } |
180ecd6a | 694 | |
95cefe5f PC |
695 | template<typename _Key, typename _Value, |
696 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
697 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
698 | bool __chc, bool __cit, bool __uk> | |
699 | typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
700 | _H1, _H2, _Hash, _RehashPolicy, | |
701 | __chc, __cit, __uk>::iterator | |
702 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
703 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
704 | find(const key_type& __k) | |
f99b2be1 | 705 | { |
95cefe5f PC |
706 | typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k); |
707 | std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count); | |
708 | _Node* __p = _M_find_node(_M_buckets[__n], __k, __code); | |
709 | return __p ? iterator(__p, _M_buckets + __n) : this->end(); | |
f99b2be1 PC |
710 | } |
711 | ||
95cefe5f PC |
712 | template<typename _Key, typename _Value, |
713 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
714 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
715 | bool __chc, bool __cit, bool __uk> | |
716 | typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
717 | _H1, _H2, _Hash, _RehashPolicy, | |
718 | __chc, __cit, __uk>::const_iterator | |
719 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
720 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
721 | find(const key_type& __k) const | |
7ffd2d94 | 722 | { |
95cefe5f PC |
723 | typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k); |
724 | std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count); | |
725 | _Node* __p = _M_find_node(_M_buckets[__n], __k, __code); | |
726 | return __p ? const_iterator(__p, _M_buckets + __n) : this->end(); | |
7ffd2d94 | 727 | } |
f99b2be1 | 728 | |
95cefe5f PC |
729 | template<typename _Key, typename _Value, |
730 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
731 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
732 | bool __chc, bool __cit, bool __uk> | |
733 | typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
734 | _H1, _H2, _Hash, _RehashPolicy, | |
735 | __chc, __cit, __uk>::size_type | |
736 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
737 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
738 | count(const key_type& __k) const | |
7ffd2d94 | 739 | { |
95cefe5f PC |
740 | typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k); |
741 | std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count); | |
742 | std::size_t __result = 0; | |
743 | for (_Node* __p = _M_buckets[__n]; __p; __p = __p->_M_next) | |
744 | if (this->_M_compare(__k, __code, __p)) | |
745 | ++__result; | |
746 | return __result; | |
7ffd2d94 | 747 | } |
180ecd6a | 748 | |
95cefe5f PC |
749 | template<typename _Key, typename _Value, |
750 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
751 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
752 | bool __chc, bool __cit, bool __uk> | |
753 | std::pair<typename _Hashtable<_Key, _Value, _Allocator, | |
754 | _ExtractKey, _Equal, _H1, | |
755 | _H2, _Hash, _RehashPolicy, | |
756 | __chc, __cit, __uk>::iterator, | |
757 | typename _Hashtable<_Key, _Value, _Allocator, | |
758 | _ExtractKey, _Equal, _H1, | |
759 | _H2, _Hash, _RehashPolicy, | |
760 | __chc, __cit, __uk>::iterator> | |
761 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
762 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
763 | equal_range(const key_type& __k) | |
7ffd2d94 | 764 | { |
95cefe5f PC |
765 | typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k); |
766 | std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count); | |
767 | _Node** __head = _M_buckets + __n; | |
768 | _Node* __p = _M_find_node(*__head, __k, __code); | |
7ffd2d94 | 769 | |
95cefe5f | 770 | if (__p) |
7ffd2d94 | 771 | { |
95cefe5f PC |
772 | _Node* __p1 = __p->_M_next; |
773 | for (; __p1; __p1 = __p1->_M_next) | |
774 | if (!this->_M_compare(__k, __code, __p1)) | |
7ffd2d94 PC |
775 | break; |
776 | ||
95cefe5f PC |
777 | iterator __first(__p, __head); |
778 | iterator __last(__p1, __head); | |
779 | if (!__p1) | |
780 | __last._M_incr_bucket(); | |
781 | return std::make_pair(__first, __last); | |
7ffd2d94 PC |
782 | } |
783 | else | |
784 | return std::make_pair(this->end(), this->end()); | |
180ecd6a MA |
785 | } |
786 | ||
95cefe5f PC |
787 | template<typename _Key, typename _Value, |
788 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
789 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
790 | bool __chc, bool __cit, bool __uk> | |
791 | std::pair<typename _Hashtable<_Key, _Value, _Allocator, | |
792 | _ExtractKey, _Equal, _H1, | |
793 | _H2, _Hash, _RehashPolicy, | |
794 | __chc, __cit, __uk>::const_iterator, | |
795 | typename _Hashtable<_Key, _Value, _Allocator, | |
796 | _ExtractKey, _Equal, _H1, | |
797 | _H2, _Hash, _RehashPolicy, | |
798 | __chc, __cit, __uk>::const_iterator> | |
799 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
800 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
801 | equal_range(const key_type& __k) const | |
7ffd2d94 | 802 | { |
95cefe5f PC |
803 | typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k); |
804 | std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count); | |
805 | _Node** __head = _M_buckets + __n; | |
806 | _Node* __p = _M_find_node(*__head, __k, __code); | |
7ffd2d94 | 807 | |
95cefe5f | 808 | if (__p) |
7ffd2d94 | 809 | { |
95cefe5f PC |
810 | _Node* __p1 = __p->_M_next; |
811 | for (; __p1; __p1 = __p1->_M_next) | |
812 | if (!this->_M_compare(__k, __code, __p1)) | |
7ffd2d94 PC |
813 | break; |
814 | ||
95cefe5f PC |
815 | const_iterator __first(__p, __head); |
816 | const_iterator __last(__p1, __head); | |
817 | if (!__p1) | |
818 | __last._M_incr_bucket(); | |
819 | return std::make_pair(__first, __last); | |
7ffd2d94 PC |
820 | } |
821 | else | |
822 | return std::make_pair(this->end(), this->end()); | |
823 | } | |
180ecd6a | 824 | |
7ffd2d94 PC |
825 | // Find the node whose key compares equal to k, beginning the search |
826 | // at p (usually the head of a bucket). Return nil if no node is found. | |
95cefe5f PC |
827 | template<typename _Key, typename _Value, |
828 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
829 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
830 | bool __chc, bool __cit, bool __uk> | |
831 | typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, | |
832 | _Equal, _H1, _H2, _Hash, _RehashPolicy, | |
833 | __chc, __cit, __uk>::_Node* | |
834 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
835 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
836 | _M_find_node(_Node* __p, const key_type& __k, | |
837 | typename _Hashtable::_Hash_code_type __code) const | |
7ffd2d94 | 838 | { |
95cefe5f PC |
839 | for (; __p; __p = __p->_M_next) |
840 | if (this->_M_compare(__k, __code, __p)) | |
841 | return __p; | |
7ffd2d94 PC |
842 | return false; |
843 | } | |
180ecd6a | 844 | |
f99b2be1 | 845 | // Insert v in bucket n (assumes no element with its key already present). |
95cefe5f PC |
846 | template<typename _Key, typename _Value, |
847 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
848 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
849 | bool __chc, bool __cit, bool __uk> | |
850 | typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
851 | _H1, _H2, _Hash, _RehashPolicy, | |
852 | __chc, __cit, __uk>::iterator | |
853 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
854 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
855 | _M_insert_bucket(const value_type& __v, size_type __n, | |
856 | typename _Hashtable::_Hash_code_type __code) | |
7ffd2d94 | 857 | { |
95cefe5f PC |
858 | std::pair<bool, std::size_t> __do_rehash |
859 | = _M_rehash_policy._M_need_rehash(_M_bucket_count, | |
860 | _M_element_count, 1); | |
7ffd2d94 PC |
861 | |
862 | // Allocate the new node before doing the rehash so that we don't | |
863 | // do a rehash if the allocation throws. | |
95cefe5f | 864 | _Node* __new_node = _M_allocate_node(__v); |
f99b2be1 | 865 | |
bc2631e0 | 866 | __try |
7ffd2d94 | 867 | { |
95cefe5f | 868 | if (__do_rehash.first) |
7ffd2d94 | 869 | { |
95cefe5f PC |
870 | const key_type& __k = this->_M_extract(__v); |
871 | __n = this->_M_bucket_index(__k, __code, __do_rehash.second); | |
872 | _M_rehash(__do_rehash.second); | |
7ffd2d94 PC |
873 | } |
874 | ||
95cefe5f PC |
875 | __new_node->_M_next = _M_buckets[__n]; |
876 | this->_M_store_code(__new_node, __code); | |
877 | _M_buckets[__n] = __new_node; | |
878 | ++_M_element_count; | |
879 | return iterator(__new_node, _M_buckets + __n); | |
7ffd2d94 | 880 | } |
bc2631e0 | 881 | __catch(...) |
7ffd2d94 | 882 | { |
95cefe5f | 883 | _M_deallocate_node(__new_node); |
40f15089 | 884 | __throw_exception_again; |
7ffd2d94 PC |
885 | } |
886 | } | |
f99b2be1 PC |
887 | |
888 | // Insert v if no element with its key is already present. | |
95cefe5f PC |
889 | template<typename _Key, typename _Value, |
890 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
891 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
892 | bool __chc, bool __cit, bool __uk> | |
893 | std::pair<typename _Hashtable<_Key, _Value, _Allocator, | |
894 | _ExtractKey, _Equal, _H1, | |
895 | _H2, _Hash, _RehashPolicy, | |
896 | __chc, __cit, __uk>::iterator, bool> | |
897 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
898 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
3b2524b1 | 899 | _M_insert(const value_type& __v, std::tr1::true_type) |
f99b2be1 | 900 | { |
95cefe5f PC |
901 | const key_type& __k = this->_M_extract(__v); |
902 | typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k); | |
903 | size_type __n = this->_M_bucket_index(__k, __code, _M_bucket_count); | |
f99b2be1 | 904 | |
95cefe5f PC |
905 | if (_Node* __p = _M_find_node(_M_buckets[__n], __k, __code)) |
906 | return std::make_pair(iterator(__p, _M_buckets + __n), false); | |
907 | return std::make_pair(_M_insert_bucket(__v, __n, __code), true); | |
f99b2be1 | 908 | } |
7ffd2d94 PC |
909 | |
910 | // Insert v unconditionally. | |
95cefe5f PC |
911 | template<typename _Key, typename _Value, |
912 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
913 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
914 | bool __chc, bool __cit, bool __uk> | |
915 | typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
916 | _H1, _H2, _Hash, _RehashPolicy, | |
917 | __chc, __cit, __uk>::iterator | |
918 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
919 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
3b2524b1 | 920 | _M_insert(const value_type& __v, std::tr1::false_type) |
7ffd2d94 | 921 | { |
95cefe5f PC |
922 | std::pair<bool, std::size_t> __do_rehash |
923 | = _M_rehash_policy._M_need_rehash(_M_bucket_count, | |
924 | _M_element_count, 1); | |
925 | if (__do_rehash.first) | |
926 | _M_rehash(__do_rehash.second); | |
f99b2be1 | 927 | |
95cefe5f PC |
928 | const key_type& __k = this->_M_extract(__v); |
929 | typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k); | |
930 | size_type __n = this->_M_bucket_index(__k, __code, _M_bucket_count); | |
f99b2be1 | 931 | |
b9cebd50 | 932 | // First find the node, avoid leaking new_node if compare throws. |
95cefe5f PC |
933 | _Node* __prev = _M_find_node(_M_buckets[__n], __k, __code); |
934 | _Node* __new_node = _M_allocate_node(__v); | |
b9cebd50 | 935 | |
95cefe5f | 936 | if (__prev) |
7ffd2d94 | 937 | { |
95cefe5f PC |
938 | __new_node->_M_next = __prev->_M_next; |
939 | __prev->_M_next = __new_node; | |
7ffd2d94 PC |
940 | } |
941 | else | |
942 | { | |
95cefe5f PC |
943 | __new_node->_M_next = _M_buckets[__n]; |
944 | _M_buckets[__n] = __new_node; | |
7ffd2d94 | 945 | } |
95cefe5f | 946 | this->_M_store_code(__new_node, __code); |
7ffd2d94 | 947 | |
95cefe5f PC |
948 | ++_M_element_count; |
949 | return iterator(__new_node, _M_buckets + __n); | |
7ffd2d94 | 950 | } |
180ecd6a | 951 | |
76f5f441 | 952 | // For erase(iterator) and erase(const_iterator). |
95cefe5f PC |
953 | template<typename _Key, typename _Value, |
954 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
955 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
956 | bool __chc, bool __cit, bool __uk> | |
76f5f441 | 957 | void |
95cefe5f PC |
958 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, |
959 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
960 | _M_erase_node(_Node* __p, _Node** __b) | |
76f5f441 | 961 | { |
95cefe5f PC |
962 | _Node* __cur = *__b; |
963 | if (__cur == __p) | |
964 | *__b = __cur->_M_next; | |
76f5f441 PC |
965 | else |
966 | { | |
95cefe5f PC |
967 | _Node* __next = __cur->_M_next; |
968 | while (__next != __p) | |
76f5f441 | 969 | { |
95cefe5f PC |
970 | __cur = __next; |
971 | __next = __cur->_M_next; | |
76f5f441 | 972 | } |
95cefe5f | 973 | __cur->_M_next = __next->_M_next; |
76f5f441 PC |
974 | } |
975 | ||
95cefe5f PC |
976 | _M_deallocate_node(__p); |
977 | --_M_element_count; | |
76f5f441 PC |
978 | } |
979 | ||
95cefe5f PC |
980 | template<typename _Key, typename _Value, |
981 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
982 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
983 | bool __chc, bool __cit, bool __uk> | |
984 | template<typename _InputIterator> | |
7ffd2d94 | 985 | void |
95cefe5f PC |
986 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, |
987 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
988 | insert(_InputIterator __first, _InputIterator __last) | |
7ffd2d94 | 989 | { |
95cefe5f PC |
990 | size_type __n_elt = __detail::__distance_fw(__first, __last); |
991 | std::pair<bool, std::size_t> __do_rehash | |
992 | = _M_rehash_policy._M_need_rehash(_M_bucket_count, | |
993 | _M_element_count, __n_elt); | |
994 | if (__do_rehash.first) | |
995 | _M_rehash(__do_rehash.second); | |
996 | ||
997 | for (; __first != __last; ++__first) | |
998 | this->insert(*__first); | |
7ffd2d94 | 999 | } |
180ecd6a | 1000 | |
95cefe5f PC |
1001 | template<typename _Key, typename _Value, |
1002 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
1003 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
1004 | bool __chc, bool __cit, bool __uk> | |
1005 | typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
1006 | _H1, _H2, _Hash, _RehashPolicy, | |
1007 | __chc, __cit, __uk>::iterator | |
1008 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
1009 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
1010 | erase(iterator __it) | |
3c9b5053 | 1011 | { |
95cefe5f PC |
1012 | iterator __result = __it; |
1013 | ++__result; | |
1014 | _M_erase_node(__it._M_cur_node, __it._M_cur_bucket); | |
1015 | return __result; | |
3c9b5053 | 1016 | } |
f99b2be1 | 1017 | |
95cefe5f PC |
1018 | template<typename _Key, typename _Value, |
1019 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
1020 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
1021 | bool __chc, bool __cit, bool __uk> | |
1022 | typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
1023 | _H1, _H2, _Hash, _RehashPolicy, | |
1024 | __chc, __cit, __uk>::const_iterator | |
1025 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
1026 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
1027 | erase(const_iterator __it) | |
7ffd2d94 | 1028 | { |
95cefe5f PC |
1029 | const_iterator __result = __it; |
1030 | ++__result; | |
1031 | _M_erase_node(__it._M_cur_node, __it._M_cur_bucket); | |
1032 | return __result; | |
180ecd6a | 1033 | } |
180ecd6a | 1034 | |
95cefe5f PC |
1035 | template<typename _Key, typename _Value, |
1036 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
1037 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
1038 | bool __chc, bool __cit, bool __uk> | |
1039 | typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
1040 | _H1, _H2, _Hash, _RehashPolicy, | |
1041 | __chc, __cit, __uk>::size_type | |
1042 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
1043 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
1044 | erase(const key_type& __k) | |
7ffd2d94 | 1045 | { |
95cefe5f PC |
1046 | typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k); |
1047 | std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count); | |
1048 | size_type __result = 0; | |
7ffd2d94 | 1049 | |
95cefe5f PC |
1050 | _Node** __slot = _M_buckets + __n; |
1051 | while (*__slot && !this->_M_compare(__k, __code, *__slot)) | |
1052 | __slot = &((*__slot)->_M_next); | |
7ffd2d94 | 1053 | |
c0c424e4 | 1054 | _Node** __saved_slot = 0; |
95cefe5f | 1055 | while (*__slot && this->_M_compare(__k, __code, *__slot)) |
7ffd2d94 | 1056 | { |
c0c424e4 PC |
1057 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
1058 | // 526. Is it undefined if a function in the standard changes | |
1059 | // in parameters? | |
1060 | if (&this->_M_extract((*__slot)->_M_v) != &__k) | |
1061 | { | |
1062 | _Node* __p = *__slot; | |
1063 | *__slot = __p->_M_next; | |
1064 | _M_deallocate_node(__p); | |
1065 | --_M_element_count; | |
1066 | ++__result; | |
1067 | } | |
1068 | else | |
1069 | { | |
1070 | __saved_slot = __slot; | |
1071 | __slot = &((*__slot)->_M_next); | |
1072 | } | |
1073 | } | |
1074 | ||
1075 | if (__saved_slot) | |
1076 | { | |
1077 | _Node* __p = *__saved_slot; | |
1078 | *__saved_slot = __p->_M_next; | |
95cefe5f PC |
1079 | _M_deallocate_node(__p); |
1080 | --_M_element_count; | |
1081 | ++__result; | |
7ffd2d94 | 1082 | } |
e7dbb3ee | 1083 | |
95cefe5f | 1084 | return __result; |
7ffd2d94 | 1085 | } |
180ecd6a | 1086 | |
7ffd2d94 PC |
1087 | // ??? This could be optimized by taking advantage of the bucket |
1088 | // structure, but it's not clear that it's worth doing. It probably | |
1089 | // wouldn't even be an optimization unless the load factor is large. | |
95cefe5f PC |
1090 | template<typename _Key, typename _Value, |
1091 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
1092 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
1093 | bool __chc, bool __cit, bool __uk> | |
1094 | typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
1095 | _H1, _H2, _Hash, _RehashPolicy, | |
1096 | __chc, __cit, __uk>::iterator | |
1097 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
1098 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
1099 | erase(iterator __first, iterator __last) | |
3c9b5053 | 1100 | { |
95cefe5f PC |
1101 | while (__first != __last) |
1102 | __first = this->erase(__first); | |
1103 | return __last; | |
3c9b5053 PC |
1104 | } |
1105 | ||
95cefe5f PC |
1106 | template<typename _Key, typename _Value, |
1107 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
1108 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
1109 | bool __chc, bool __cit, bool __uk> | |
1110 | typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
1111 | _H1, _H2, _Hash, _RehashPolicy, | |
1112 | __chc, __cit, __uk>::const_iterator | |
1113 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, | |
1114 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
1115 | erase(const_iterator __first, const_iterator __last) | |
7ffd2d94 | 1116 | { |
95cefe5f PC |
1117 | while (__first != __last) |
1118 | __first = this->erase(__first); | |
1119 | return __last; | |
7ffd2d94 | 1120 | } |
180ecd6a | 1121 | |
95cefe5f PC |
1122 | template<typename _Key, typename _Value, |
1123 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
1124 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
1125 | bool __chc, bool __cit, bool __uk> | |
7ffd2d94 | 1126 | void |
95cefe5f PC |
1127 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, |
1128 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
7ffd2d94 PC |
1129 | clear() |
1130 | { | |
95cefe5f PC |
1131 | _M_deallocate_nodes(_M_buckets, _M_bucket_count); |
1132 | _M_element_count = 0; | |
7ffd2d94 | 1133 | } |
f8190ffc | 1134 | |
95cefe5f PC |
1135 | template<typename _Key, typename _Value, |
1136 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
1137 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
1138 | bool __chc, bool __cit, bool __uk> | |
f8190ffc | 1139 | void |
95cefe5f PC |
1140 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, |
1141 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
1142 | rehash(size_type __n) | |
f8190ffc | 1143 | { |
95cefe5f PC |
1144 | _M_rehash(std::max(_M_rehash_policy._M_next_bkt(__n), |
1145 | _M_rehash_policy._M_bkt_for_elements(_M_element_count | |
1146 | + 1))); | |
f8190ffc | 1147 | } |
180ecd6a | 1148 | |
95cefe5f PC |
1149 | template<typename _Key, typename _Value, |
1150 | typename _Allocator, typename _ExtractKey, typename _Equal, | |
1151 | typename _H1, typename _H2, typename _Hash, typename _RehashPolicy, | |
1152 | bool __chc, bool __cit, bool __uk> | |
7ffd2d94 | 1153 | void |
95cefe5f PC |
1154 | _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, |
1155 | _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>:: | |
1156 | _M_rehash(size_type __n) | |
7ffd2d94 | 1157 | { |
95cefe5f | 1158 | _Node** __new_array = _M_allocate_buckets(__n); |
bc2631e0 | 1159 | __try |
7ffd2d94 | 1160 | { |
95cefe5f PC |
1161 | for (size_type __i = 0; __i < _M_bucket_count; ++__i) |
1162 | while (_Node* __p = _M_buckets[__i]) | |
7ffd2d94 | 1163 | { |
95cefe5f PC |
1164 | std::size_t __new_index = this->_M_bucket_index(__p, __n); |
1165 | _M_buckets[__i] = __p->_M_next; | |
1166 | __p->_M_next = __new_array[__new_index]; | |
1167 | __new_array[__new_index] = __p; | |
7ffd2d94 | 1168 | } |
95cefe5f PC |
1169 | _M_deallocate_buckets(_M_buckets, _M_bucket_count); |
1170 | _M_bucket_count = __n; | |
1171 | _M_buckets = __new_array; | |
7ffd2d94 | 1172 | } |
bc2631e0 | 1173 | __catch(...) |
7ffd2d94 PC |
1174 | { |
1175 | // A failure here means that a hash function threw an exception. | |
1176 | // We can't restore the previous state without calling the hash | |
1177 | // function again, so the only sensible recovery is to delete | |
1178 | // everything. | |
95cefe5f PC |
1179 | _M_deallocate_nodes(__new_array, __n); |
1180 | _M_deallocate_buckets(__new_array, __n); | |
1181 | _M_deallocate_nodes(_M_buckets, _M_bucket_count); | |
1182 | _M_element_count = 0; | |
40f15089 | 1183 | __throw_exception_again; |
7ffd2d94 PC |
1184 | } |
1185 | } | |
3b2524b1 | 1186 | } |
e133ace8 | 1187 | } |