111
|
1 // Internal policy header for unordered_set and unordered_map -*- C++ -*-
|
|
2
|
|
3 // Copyright (C) 2010-2017 Free Software Foundation, Inc.
|
|
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
|
|
8 // Free Software Foundation; either version 3, or (at your option)
|
|
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
|
|
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/>.
|
|
24
|
|
25 /** @file bits/hashtable_policy.h
|
|
26 * This is an internal header file, included by other library headers.
|
|
27 * Do not attempt to use it directly.
|
|
28 * @headername{unordered_map,unordered_set}
|
|
29 */
|
|
30
|
|
31 #ifndef _HASHTABLE_POLICY_H
|
|
32 #define _HASHTABLE_POLICY_H 1
|
|
33
|
|
34 #include <bits/stl_algobase.h> // for std::min.
|
|
35
|
|
36 namespace std _GLIBCXX_VISIBILITY(default)
|
|
37 {
|
|
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|
39
|
|
40 template<typename _Key, typename _Value, typename _Alloc,
|
|
41 typename _ExtractKey, typename _Equal,
|
|
42 typename _H1, typename _H2, typename _Hash,
|
|
43 typename _RehashPolicy, typename _Traits>
|
|
44 class _Hashtable;
|
|
45
|
|
46 namespace __detail
|
|
47 {
|
|
48 /**
|
|
49 * @defgroup hashtable-detail Base and Implementation Classes
|
|
50 * @ingroup unordered_associative_containers
|
|
51 * @{
|
|
52 */
|
|
53 template<typename _Key, typename _Value,
|
|
54 typename _ExtractKey, typename _Equal,
|
|
55 typename _H1, typename _H2, typename _Hash, typename _Traits>
|
|
56 struct _Hashtable_base;
|
|
57
|
|
58 // Helper function: return distance(first, last) for forward
|
|
59 // iterators, or 0 for input iterators.
|
|
60 template<class _Iterator>
|
|
61 inline typename std::iterator_traits<_Iterator>::difference_type
|
|
62 __distance_fw(_Iterator __first, _Iterator __last,
|
|
63 std::input_iterator_tag)
|
|
64 { return 0; }
|
|
65
|
|
66 template<class _Iterator>
|
|
67 inline typename std::iterator_traits<_Iterator>::difference_type
|
|
68 __distance_fw(_Iterator __first, _Iterator __last,
|
|
69 std::forward_iterator_tag)
|
|
70 { return std::distance(__first, __last); }
|
|
71
|
|
72 template<class _Iterator>
|
|
73 inline typename std::iterator_traits<_Iterator>::difference_type
|
|
74 __distance_fw(_Iterator __first, _Iterator __last)
|
|
75 {
|
|
76 typedef typename std::iterator_traits<_Iterator>::iterator_category _Tag;
|
|
77 return __distance_fw(__first, __last, _Tag());
|
|
78 }
|
|
79
|
|
80 // Helper type used to detect whether the hash functor is noexcept.
|
|
81 template <typename _Key, typename _Hash>
|
|
82 struct __is_noexcept_hash : std::__bool_constant<
|
|
83 noexcept(declval<const _Hash&>()(declval<const _Key&>()))>
|
|
84 { };
|
|
85
|
|
86 struct _Identity
|
|
87 {
|
|
88 template<typename _Tp>
|
|
89 _Tp&&
|
|
90 operator()(_Tp&& __x) const
|
|
91 { return std::forward<_Tp>(__x); }
|
|
92 };
|
|
93
|
|
94 struct _Select1st
|
|
95 {
|
|
96 template<typename _Tp>
|
|
97 auto
|
|
98 operator()(_Tp&& __x) const
|
|
99 -> decltype(std::get<0>(std::forward<_Tp>(__x)))
|
|
100 { return std::get<0>(std::forward<_Tp>(__x)); }
|
|
101 };
|
|
102
|
|
103 template<typename _NodeAlloc>
|
|
104 struct _Hashtable_alloc;
|
|
105
|
|
106 // Functor recycling a pool of nodes and using allocation once the pool is
|
|
107 // empty.
|
|
108 template<typename _NodeAlloc>
|
|
109 struct _ReuseOrAllocNode
|
|
110 {
|
|
111 private:
|
|
112 using __node_alloc_type = _NodeAlloc;
|
|
113 using __hashtable_alloc = _Hashtable_alloc<__node_alloc_type>;
|
|
114 using __node_alloc_traits =
|
|
115 typename __hashtable_alloc::__node_alloc_traits;
|
|
116 using __node_type = typename __hashtable_alloc::__node_type;
|
|
117
|
|
118 public:
|
|
119 _ReuseOrAllocNode(__node_type* __nodes, __hashtable_alloc& __h)
|
|
120 : _M_nodes(__nodes), _M_h(__h) { }
|
|
121 _ReuseOrAllocNode(const _ReuseOrAllocNode&) = delete;
|
|
122
|
|
123 ~_ReuseOrAllocNode()
|
|
124 { _M_h._M_deallocate_nodes(_M_nodes); }
|
|
125
|
|
126 template<typename _Arg>
|
|
127 __node_type*
|
|
128 operator()(_Arg&& __arg) const
|
|
129 {
|
|
130 if (_M_nodes)
|
|
131 {
|
|
132 __node_type* __node = _M_nodes;
|
|
133 _M_nodes = _M_nodes->_M_next();
|
|
134 __node->_M_nxt = nullptr;
|
|
135 auto& __a = _M_h._M_node_allocator();
|
|
136 __node_alloc_traits::destroy(__a, __node->_M_valptr());
|
|
137 __try
|
|
138 {
|
|
139 __node_alloc_traits::construct(__a, __node->_M_valptr(),
|
|
140 std::forward<_Arg>(__arg));
|
|
141 }
|
|
142 __catch(...)
|
|
143 {
|
|
144 __node->~__node_type();
|
|
145 __node_alloc_traits::deallocate(__a, __node, 1);
|
|
146 __throw_exception_again;
|
|
147 }
|
|
148 return __node;
|
|
149 }
|
|
150 return _M_h._M_allocate_node(std::forward<_Arg>(__arg));
|
|
151 }
|
|
152
|
|
153 private:
|
|
154 mutable __node_type* _M_nodes;
|
|
155 __hashtable_alloc& _M_h;
|
|
156 };
|
|
157
|
|
158 // Functor similar to the previous one but without any pool of nodes to
|
|
159 // recycle.
|
|
160 template<typename _NodeAlloc>
|
|
161 struct _AllocNode
|
|
162 {
|
|
163 private:
|
|
164 using __hashtable_alloc = _Hashtable_alloc<_NodeAlloc>;
|
|
165 using __node_type = typename __hashtable_alloc::__node_type;
|
|
166
|
|
167 public:
|
|
168 _AllocNode(__hashtable_alloc& __h)
|
|
169 : _M_h(__h) { }
|
|
170
|
|
171 template<typename _Arg>
|
|
172 __node_type*
|
|
173 operator()(_Arg&& __arg) const
|
|
174 { return _M_h._M_allocate_node(std::forward<_Arg>(__arg)); }
|
|
175
|
|
176 private:
|
|
177 __hashtable_alloc& _M_h;
|
|
178 };
|
|
179
|
|
180 // Auxiliary types used for all instantiations of _Hashtable nodes
|
|
181 // and iterators.
|
|
182
|
|
183 /**
|
|
184 * struct _Hashtable_traits
|
|
185 *
|
|
186 * Important traits for hash tables.
|
|
187 *
|
|
188 * @tparam _Cache_hash_code Boolean value. True if the value of
|
|
189 * the hash function is stored along with the value. This is a
|
|
190 * time-space tradeoff. Storing it may improve lookup speed by
|
|
191 * reducing the number of times we need to call the _Equal
|
|
192 * function.
|
|
193 *
|
|
194 * @tparam _Constant_iterators Boolean value. True if iterator and
|
|
195 * const_iterator are both constant iterator types. This is true
|
|
196 * for unordered_set and unordered_multiset, false for
|
|
197 * unordered_map and unordered_multimap.
|
|
198 *
|
|
199 * @tparam _Unique_keys Boolean value. True if the return value
|
|
200 * of _Hashtable::count(k) is always at most one, false if it may
|
|
201 * be an arbitrary number. This is true for unordered_set and
|
|
202 * unordered_map, false for unordered_multiset and
|
|
203 * unordered_multimap.
|
|
204 */
|
|
205 template<bool _Cache_hash_code, bool _Constant_iterators, bool _Unique_keys>
|
|
206 struct _Hashtable_traits
|
|
207 {
|
|
208 using __hash_cached = __bool_constant<_Cache_hash_code>;
|
|
209 using __constant_iterators = __bool_constant<_Constant_iterators>;
|
|
210 using __unique_keys = __bool_constant<_Unique_keys>;
|
|
211 };
|
|
212
|
|
213 /**
|
|
214 * struct _Hash_node_base
|
|
215 *
|
|
216 * Nodes, used to wrap elements stored in the hash table. A policy
|
|
217 * template parameter of class template _Hashtable controls whether
|
|
218 * nodes also store a hash code. In some cases (e.g. strings) this
|
|
219 * may be a performance win.
|
|
220 */
|
|
221 struct _Hash_node_base
|
|
222 {
|
|
223 _Hash_node_base* _M_nxt;
|
|
224
|
|
225 _Hash_node_base() noexcept : _M_nxt() { }
|
|
226
|
|
227 _Hash_node_base(_Hash_node_base* __next) noexcept : _M_nxt(__next) { }
|
|
228 };
|
|
229
|
|
230 /**
|
|
231 * struct _Hash_node_value_base
|
|
232 *
|
|
233 * Node type with the value to store.
|
|
234 */
|
|
235 template<typename _Value>
|
|
236 struct _Hash_node_value_base : _Hash_node_base
|
|
237 {
|
|
238 typedef _Value value_type;
|
|
239
|
|
240 __gnu_cxx::__aligned_buffer<_Value> _M_storage;
|
|
241
|
|
242 _Value*
|
|
243 _M_valptr() noexcept
|
|
244 { return _M_storage._M_ptr(); }
|
|
245
|
|
246 const _Value*
|
|
247 _M_valptr() const noexcept
|
|
248 { return _M_storage._M_ptr(); }
|
|
249
|
|
250 _Value&
|
|
251 _M_v() noexcept
|
|
252 { return *_M_valptr(); }
|
|
253
|
|
254 const _Value&
|
|
255 _M_v() const noexcept
|
|
256 { return *_M_valptr(); }
|
|
257 };
|
|
258
|
|
259 /**
|
|
260 * Primary template struct _Hash_node.
|
|
261 */
|
|
262 template<typename _Value, bool _Cache_hash_code>
|
|
263 struct _Hash_node;
|
|
264
|
|
265 /**
|
|
266 * Specialization for nodes with caches, struct _Hash_node.
|
|
267 *
|
|
268 * Base class is __detail::_Hash_node_value_base.
|
|
269 */
|
|
270 template<typename _Value>
|
|
271 struct _Hash_node<_Value, true> : _Hash_node_value_base<_Value>
|
|
272 {
|
|
273 std::size_t _M_hash_code;
|
|
274
|
|
275 _Hash_node*
|
|
276 _M_next() const noexcept
|
|
277 { return static_cast<_Hash_node*>(this->_M_nxt); }
|
|
278 };
|
|
279
|
|
280 /**
|
|
281 * Specialization for nodes without caches, struct _Hash_node.
|
|
282 *
|
|
283 * Base class is __detail::_Hash_node_value_base.
|
|
284 */
|
|
285 template<typename _Value>
|
|
286 struct _Hash_node<_Value, false> : _Hash_node_value_base<_Value>
|
|
287 {
|
|
288 _Hash_node*
|
|
289 _M_next() const noexcept
|
|
290 { return static_cast<_Hash_node*>(this->_M_nxt); }
|
|
291 };
|
|
292
|
|
293 /// Base class for node iterators.
|
|
294 template<typename _Value, bool _Cache_hash_code>
|
|
295 struct _Node_iterator_base
|
|
296 {
|
|
297 using __node_type = _Hash_node<_Value, _Cache_hash_code>;
|
|
298
|
|
299 __node_type* _M_cur;
|
|
300
|
|
301 _Node_iterator_base(__node_type* __p) noexcept
|
|
302 : _M_cur(__p) { }
|
|
303
|
|
304 void
|
|
305 _M_incr() noexcept
|
|
306 { _M_cur = _M_cur->_M_next(); }
|
|
307 };
|
|
308
|
|
309 template<typename _Value, bool _Cache_hash_code>
|
|
310 inline bool
|
|
311 operator==(const _Node_iterator_base<_Value, _Cache_hash_code>& __x,
|
|
312 const _Node_iterator_base<_Value, _Cache_hash_code >& __y)
|
|
313 noexcept
|
|
314 { return __x._M_cur == __y._M_cur; }
|
|
315
|
|
316 template<typename _Value, bool _Cache_hash_code>
|
|
317 inline bool
|
|
318 operator!=(const _Node_iterator_base<_Value, _Cache_hash_code>& __x,
|
|
319 const _Node_iterator_base<_Value, _Cache_hash_code>& __y)
|
|
320 noexcept
|
|
321 { return __x._M_cur != __y._M_cur; }
|
|
322
|
|
323 /// Node iterators, used to iterate through all the hashtable.
|
|
324 template<typename _Value, bool __constant_iterators, bool __cache>
|
|
325 struct _Node_iterator
|
|
326 : public _Node_iterator_base<_Value, __cache>
|
|
327 {
|
|
328 private:
|
|
329 using __base_type = _Node_iterator_base<_Value, __cache>;
|
|
330 using __node_type = typename __base_type::__node_type;
|
|
331
|
|
332 public:
|
|
333 typedef _Value value_type;
|
|
334 typedef std::ptrdiff_t difference_type;
|
|
335 typedef std::forward_iterator_tag iterator_category;
|
|
336
|
|
337 using pointer = typename std::conditional<__constant_iterators,
|
|
338 const _Value*, _Value*>::type;
|
|
339
|
|
340 using reference = typename std::conditional<__constant_iterators,
|
|
341 const _Value&, _Value&>::type;
|
|
342
|
|
343 _Node_iterator() noexcept
|
|
344 : __base_type(0) { }
|
|
345
|
|
346 explicit
|
|
347 _Node_iterator(__node_type* __p) noexcept
|
|
348 : __base_type(__p) { }
|
|
349
|
|
350 reference
|
|
351 operator*() const noexcept
|
|
352 { return this->_M_cur->_M_v(); }
|
|
353
|
|
354 pointer
|
|
355 operator->() const noexcept
|
|
356 { return this->_M_cur->_M_valptr(); }
|
|
357
|
|
358 _Node_iterator&
|
|
359 operator++() noexcept
|
|
360 {
|
|
361 this->_M_incr();
|
|
362 return *this;
|
|
363 }
|
|
364
|
|
365 _Node_iterator
|
|
366 operator++(int) noexcept
|
|
367 {
|
|
368 _Node_iterator __tmp(*this);
|
|
369 this->_M_incr();
|
|
370 return __tmp;
|
|
371 }
|
|
372 };
|
|
373
|
|
374 /// Node const_iterators, used to iterate through all the hashtable.
|
|
375 template<typename _Value, bool __constant_iterators, bool __cache>
|
|
376 struct _Node_const_iterator
|
|
377 : public _Node_iterator_base<_Value, __cache>
|
|
378 {
|
|
379 private:
|
|
380 using __base_type = _Node_iterator_base<_Value, __cache>;
|
|
381 using __node_type = typename __base_type::__node_type;
|
|
382
|
|
383 public:
|
|
384 typedef _Value value_type;
|
|
385 typedef std::ptrdiff_t difference_type;
|
|
386 typedef std::forward_iterator_tag iterator_category;
|
|
387
|
|
388 typedef const _Value* pointer;
|
|
389 typedef const _Value& reference;
|
|
390
|
|
391 _Node_const_iterator() noexcept
|
|
392 : __base_type(0) { }
|
|
393
|
|
394 explicit
|
|
395 _Node_const_iterator(__node_type* __p) noexcept
|
|
396 : __base_type(__p) { }
|
|
397
|
|
398 _Node_const_iterator(const _Node_iterator<_Value, __constant_iterators,
|
|
399 __cache>& __x) noexcept
|
|
400 : __base_type(__x._M_cur) { }
|
|
401
|
|
402 reference
|
|
403 operator*() const noexcept
|
|
404 { return this->_M_cur->_M_v(); }
|
|
405
|
|
406 pointer
|
|
407 operator->() const noexcept
|
|
408 { return this->_M_cur->_M_valptr(); }
|
|
409
|
|
410 _Node_const_iterator&
|
|
411 operator++() noexcept
|
|
412 {
|
|
413 this->_M_incr();
|
|
414 return *this;
|
|
415 }
|
|
416
|
|
417 _Node_const_iterator
|
|
418 operator++(int) noexcept
|
|
419 {
|
|
420 _Node_const_iterator __tmp(*this);
|
|
421 this->_M_incr();
|
|
422 return __tmp;
|
|
423 }
|
|
424 };
|
|
425
|
|
426 // Many of class template _Hashtable's template parameters are policy
|
|
427 // classes. These are defaults for the policies.
|
|
428
|
|
429 /// Default range hashing function: use division to fold a large number
|
|
430 /// into the range [0, N).
|
|
431 struct _Mod_range_hashing
|
|
432 {
|
|
433 typedef std::size_t first_argument_type;
|
|
434 typedef std::size_t second_argument_type;
|
|
435 typedef std::size_t result_type;
|
|
436
|
|
437 result_type
|
|
438 operator()(first_argument_type __num,
|
|
439 second_argument_type __den) const noexcept
|
|
440 { return __num % __den; }
|
|
441 };
|
|
442
|
|
443 /// Default ranged hash function H. In principle it should be a
|
|
444 /// function object composed from objects of type H1 and H2 such that
|
|
445 /// h(k, N) = h2(h1(k), N), but that would mean making extra copies of
|
|
446 /// h1 and h2. So instead we'll just use a tag to tell class template
|
|
447 /// hashtable to do that composition.
|
|
448 struct _Default_ranged_hash { };
|
|
449
|
|
450 /// Default value for rehash policy. Bucket size is (usually) the
|
|
451 /// smallest prime that keeps the load factor small enough.
|
|
452 struct _Prime_rehash_policy
|
|
453 {
|
|
454 using __has_load_factor = std::true_type;
|
|
455
|
|
456 _Prime_rehash_policy(float __z = 1.0) noexcept
|
|
457 : _M_max_load_factor(__z), _M_next_resize(0) { }
|
|
458
|
|
459 float
|
|
460 max_load_factor() const noexcept
|
|
461 { return _M_max_load_factor; }
|
|
462
|
|
463 // Return a bucket size no smaller than n.
|
|
464 std::size_t
|
|
465 _M_next_bkt(std::size_t __n) const;
|
|
466
|
|
467 // Return a bucket count appropriate for n elements
|
|
468 std::size_t
|
|
469 _M_bkt_for_elements(std::size_t __n) const
|
|
470 { return __builtin_ceil(__n / (long double)_M_max_load_factor); }
|
|
471
|
|
472 // __n_bkt is current bucket count, __n_elt is current element count,
|
|
473 // and __n_ins is number of elements to be inserted. Do we need to
|
|
474 // increase bucket count? If so, return make_pair(true, n), where n
|
|
475 // is the new bucket count. If not, return make_pair(false, 0).
|
|
476 std::pair<bool, std::size_t>
|
|
477 _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
|
|
478 std::size_t __n_ins) const;
|
|
479
|
|
480 typedef std::size_t _State;
|
|
481
|
|
482 _State
|
|
483 _M_state() const
|
|
484 { return _M_next_resize; }
|
|
485
|
|
486 void
|
|
487 _M_reset() noexcept
|
|
488 { _M_next_resize = 0; }
|
|
489
|
|
490 void
|
|
491 _M_reset(_State __state)
|
|
492 { _M_next_resize = __state; }
|
|
493
|
|
494 static const std::size_t _S_growth_factor = 2;
|
|
495
|
|
496 float _M_max_load_factor;
|
|
497 mutable std::size_t _M_next_resize;
|
|
498 };
|
|
499
|
|
500 /// Range hashing function assuming that second arg is a power of 2.
|
|
501 struct _Mask_range_hashing
|
|
502 {
|
|
503 typedef std::size_t first_argument_type;
|
|
504 typedef std::size_t second_argument_type;
|
|
505 typedef std::size_t result_type;
|
|
506
|
|
507 result_type
|
|
508 operator()(first_argument_type __num,
|
|
509 second_argument_type __den) const noexcept
|
|
510 { return __num & (__den - 1); }
|
|
511 };
|
|
512
|
|
513 /// Compute closest power of 2.
|
|
514 _GLIBCXX14_CONSTEXPR
|
|
515 inline std::size_t
|
|
516 __clp2(std::size_t __n) noexcept
|
|
517 {
|
|
518 #if __SIZEOF_SIZE_T__ >= 8
|
|
519 std::uint_fast64_t __x = __n;
|
|
520 #else
|
|
521 std::uint_fast32_t __x = __n;
|
|
522 #endif
|
|
523 // Algorithm from Hacker's Delight, Figure 3-3.
|
|
524 __x = __x - 1;
|
|
525 __x = __x | (__x >> 1);
|
|
526 __x = __x | (__x >> 2);
|
|
527 __x = __x | (__x >> 4);
|
|
528 __x = __x | (__x >> 8);
|
|
529 __x = __x | (__x >>16);
|
|
530 #if __SIZEOF_SIZE_T__ >= 8
|
|
531 __x = __x | (__x >>32);
|
|
532 #endif
|
|
533 return __x + 1;
|
|
534 }
|
|
535
|
|
536 /// Rehash policy providing power of 2 bucket numbers. Avoids modulo
|
|
537 /// operations.
|
|
538 struct _Power2_rehash_policy
|
|
539 {
|
|
540 using __has_load_factor = std::true_type;
|
|
541
|
|
542 _Power2_rehash_policy(float __z = 1.0) noexcept
|
|
543 : _M_max_load_factor(__z), _M_next_resize(0) { }
|
|
544
|
|
545 float
|
|
546 max_load_factor() const noexcept
|
|
547 { return _M_max_load_factor; }
|
|
548
|
|
549 // Return a bucket size no smaller than n (as long as n is not above the
|
|
550 // highest power of 2).
|
|
551 std::size_t
|
|
552 _M_next_bkt(std::size_t __n) noexcept
|
|
553 {
|
|
554 const auto __max_width = std::min<size_t>(sizeof(size_t), 8);
|
|
555 const auto __max_bkt = size_t(1) << (__max_width * __CHAR_BIT__ - 1);
|
|
556 std::size_t __res = __clp2(__n);
|
|
557
|
|
558 if (__res == __n)
|
|
559 __res <<= 1;
|
|
560
|
|
561 if (__res == 0)
|
|
562 __res = __max_bkt;
|
|
563
|
|
564 if (__res == __max_bkt)
|
|
565 // Set next resize to the max value so that we never try to rehash again
|
|
566 // as we already reach the biggest possible bucket number.
|
|
567 // Note that it might result in max_load_factor not being respected.
|
|
568 _M_next_resize = std::size_t(-1);
|
|
569 else
|
|
570 _M_next_resize
|
|
571 = __builtin_ceil(__res * (long double)_M_max_load_factor);
|
|
572
|
|
573 return __res;
|
|
574 }
|
|
575
|
|
576 // Return a bucket count appropriate for n elements
|
|
577 std::size_t
|
|
578 _M_bkt_for_elements(std::size_t __n) const noexcept
|
|
579 { return __builtin_ceil(__n / (long double)_M_max_load_factor); }
|
|
580
|
|
581 // __n_bkt is current bucket count, __n_elt is current element count,
|
|
582 // and __n_ins is number of elements to be inserted. Do we need to
|
|
583 // increase bucket count? If so, return make_pair(true, n), where n
|
|
584 // is the new bucket count. If not, return make_pair(false, 0).
|
|
585 std::pair<bool, std::size_t>
|
|
586 _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
|
|
587 std::size_t __n_ins) noexcept
|
|
588 {
|
|
589 if (__n_elt + __n_ins >= _M_next_resize)
|
|
590 {
|
|
591 long double __min_bkts = (__n_elt + __n_ins)
|
|
592 / (long double)_M_max_load_factor;
|
|
593 if (__min_bkts >= __n_bkt)
|
|
594 return std::make_pair(true,
|
|
595 _M_next_bkt(std::max<std::size_t>(__builtin_floor(__min_bkts) + 1,
|
|
596 __n_bkt * _S_growth_factor)));
|
|
597
|
|
598 _M_next_resize
|
|
599 = __builtin_floor(__n_bkt * (long double)_M_max_load_factor);
|
|
600 return std::make_pair(false, 0);
|
|
601 }
|
|
602 else
|
|
603 return std::make_pair(false, 0);
|
|
604 }
|
|
605
|
|
606 typedef std::size_t _State;
|
|
607
|
|
608 _State
|
|
609 _M_state() const noexcept
|
|
610 { return _M_next_resize; }
|
|
611
|
|
612 void
|
|
613 _M_reset() noexcept
|
|
614 { _M_next_resize = 0; }
|
|
615
|
|
616 void
|
|
617 _M_reset(_State __state) noexcept
|
|
618 { _M_next_resize = __state; }
|
|
619
|
|
620 static const std::size_t _S_growth_factor = 2;
|
|
621
|
|
622 float _M_max_load_factor;
|
|
623 std::size_t _M_next_resize;
|
|
624 };
|
|
625
|
|
626 // Base classes for std::_Hashtable. We define these base classes
|
|
627 // because in some cases we want to do different things depending on
|
|
628 // the value of a policy class. In some cases the policy class
|
|
629 // affects which member functions and nested typedefs are defined;
|
|
630 // we handle that by specializing base class templates. Several of
|
|
631 // the base class templates need to access other members of class
|
|
632 // template _Hashtable, so we use a variant of the "Curiously
|
|
633 // Recurring Template Pattern" (CRTP) technique.
|
|
634
|
|
635 /**
|
|
636 * Primary class template _Map_base.
|
|
637 *
|
|
638 * If the hashtable has a value type of the form pair<T1, T2> and a
|
|
639 * key extraction policy (_ExtractKey) that returns the first part
|
|
640 * of the pair, the hashtable gets a mapped_type typedef. If it
|
|
641 * satisfies those criteria and also has unique keys, then it also
|
|
642 * gets an operator[].
|
|
643 */
|
|
644 template<typename _Key, typename _Value, typename _Alloc,
|
|
645 typename _ExtractKey, typename _Equal,
|
|
646 typename _H1, typename _H2, typename _Hash,
|
|
647 typename _RehashPolicy, typename _Traits,
|
|
648 bool _Unique_keys = _Traits::__unique_keys::value>
|
|
649 struct _Map_base { };
|
|
650
|
|
651 /// Partial specialization, __unique_keys set to false.
|
|
652 template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
|
|
653 typename _H1, typename _H2, typename _Hash,
|
|
654 typename _RehashPolicy, typename _Traits>
|
|
655 struct _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
|
|
656 _H1, _H2, _Hash, _RehashPolicy, _Traits, false>
|
|
657 {
|
|
658 using mapped_type = typename std::tuple_element<1, _Pair>::type;
|
|
659 };
|
|
660
|
|
661 /// Partial specialization, __unique_keys set to true.
|
|
662 template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
|
|
663 typename _H1, typename _H2, typename _Hash,
|
|
664 typename _RehashPolicy, typename _Traits>
|
|
665 struct _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
|
|
666 _H1, _H2, _Hash, _RehashPolicy, _Traits, true>
|
|
667 {
|
|
668 private:
|
|
669 using __hashtable_base = __detail::_Hashtable_base<_Key, _Pair,
|
|
670 _Select1st,
|
|
671 _Equal, _H1, _H2, _Hash,
|
|
672 _Traits>;
|
|
673
|
|
674 using __hashtable = _Hashtable<_Key, _Pair, _Alloc,
|
|
675 _Select1st, _Equal,
|
|
676 _H1, _H2, _Hash, _RehashPolicy, _Traits>;
|
|
677
|
|
678 using __hash_code = typename __hashtable_base::__hash_code;
|
|
679 using __node_type = typename __hashtable_base::__node_type;
|
|
680
|
|
681 public:
|
|
682 using key_type = typename __hashtable_base::key_type;
|
|
683 using iterator = typename __hashtable_base::iterator;
|
|
684 using mapped_type = typename std::tuple_element<1, _Pair>::type;
|
|
685
|
|
686 mapped_type&
|
|
687 operator[](const key_type& __k);
|
|
688
|
|
689 mapped_type&
|
|
690 operator[](key_type&& __k);
|
|
691
|
|
692 // _GLIBCXX_RESOLVE_LIB_DEFECTS
|
|
693 // DR 761. unordered_map needs an at() member function.
|
|
694 mapped_type&
|
|
695 at(const key_type& __k);
|
|
696
|
|
697 const mapped_type&
|
|
698 at(const key_type& __k) const;
|
|
699 };
|
|
700
|
|
701 template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
|
|
702 typename _H1, typename _H2, typename _Hash,
|
|
703 typename _RehashPolicy, typename _Traits>
|
|
704 auto
|
|
705 _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
|
|
706 _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
|
|
707 operator[](const key_type& __k)
|
|
708 -> mapped_type&
|
|
709 {
|
|
710 __hashtable* __h = static_cast<__hashtable*>(this);
|
|
711 __hash_code __code = __h->_M_hash_code(__k);
|
|
712 std::size_t __n = __h->_M_bucket_index(__k, __code);
|
|
713 __node_type* __p = __h->_M_find_node(__n, __k, __code);
|
|
714
|
|
715 if (!__p)
|
|
716 {
|
|
717 __p = __h->_M_allocate_node(std::piecewise_construct,
|
|
718 std::tuple<const key_type&>(__k),
|
|
719 std::tuple<>());
|
|
720 return __h->_M_insert_unique_node(__n, __code, __p)->second;
|
|
721 }
|
|
722
|
|
723 return __p->_M_v().second;
|
|
724 }
|
|
725
|
|
726 template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
|
|
727 typename _H1, typename _H2, typename _Hash,
|
|
728 typename _RehashPolicy, typename _Traits>
|
|
729 auto
|
|
730 _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
|
|
731 _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
|
|
732 operator[](key_type&& __k)
|
|
733 -> mapped_type&
|
|
734 {
|
|
735 __hashtable* __h = static_cast<__hashtable*>(this);
|
|
736 __hash_code __code = __h->_M_hash_code(__k);
|
|
737 std::size_t __n = __h->_M_bucket_index(__k, __code);
|
|
738 __node_type* __p = __h->_M_find_node(__n, __k, __code);
|
|
739
|
|
740 if (!__p)
|
|
741 {
|
|
742 __p = __h->_M_allocate_node(std::piecewise_construct,
|
|
743 std::forward_as_tuple(std::move(__k)),
|
|
744 std::tuple<>());
|
|
745 return __h->_M_insert_unique_node(__n, __code, __p)->second;
|
|
746 }
|
|
747
|
|
748 return __p->_M_v().second;
|
|
749 }
|
|
750
|
|
751 template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
|
|
752 typename _H1, typename _H2, typename _Hash,
|
|
753 typename _RehashPolicy, typename _Traits>
|
|
754 auto
|
|
755 _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
|
|
756 _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
|
|
757 at(const key_type& __k)
|
|
758 -> mapped_type&
|
|
759 {
|
|
760 __hashtable* __h = static_cast<__hashtable*>(this);
|
|
761 __hash_code __code = __h->_M_hash_code(__k);
|
|
762 std::size_t __n = __h->_M_bucket_index(__k, __code);
|
|
763 __node_type* __p = __h->_M_find_node(__n, __k, __code);
|
|
764
|
|
765 if (!__p)
|
|
766 __throw_out_of_range(__N("_Map_base::at"));
|
|
767 return __p->_M_v().second;
|
|
768 }
|
|
769
|
|
770 template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
|
|
771 typename _H1, typename _H2, typename _Hash,
|
|
772 typename _RehashPolicy, typename _Traits>
|
|
773 auto
|
|
774 _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
|
|
775 _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
|
|
776 at(const key_type& __k) const
|
|
777 -> const mapped_type&
|
|
778 {
|
|
779 const __hashtable* __h = static_cast<const __hashtable*>(this);
|
|
780 __hash_code __code = __h->_M_hash_code(__k);
|
|
781 std::size_t __n = __h->_M_bucket_index(__k, __code);
|
|
782 __node_type* __p = __h->_M_find_node(__n, __k, __code);
|
|
783
|
|
784 if (!__p)
|
|
785 __throw_out_of_range(__N("_Map_base::at"));
|
|
786 return __p->_M_v().second;
|
|
787 }
|
|
788
|
|
789 /**
|
|
790 * Primary class template _Insert_base.
|
|
791 *
|
|
792 * Defines @c insert member functions appropriate to all _Hashtables.
|
|
793 */
|
|
794 template<typename _Key, typename _Value, typename _Alloc,
|
|
795 typename _ExtractKey, typename _Equal,
|
|
796 typename _H1, typename _H2, typename _Hash,
|
|
797 typename _RehashPolicy, typename _Traits>
|
|
798 struct _Insert_base
|
|
799 {
|
|
800 protected:
|
|
801 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
|
|
802 _Equal, _H1, _H2, _Hash,
|
|
803 _RehashPolicy, _Traits>;
|
|
804
|
|
805 using __hashtable_base = _Hashtable_base<_Key, _Value, _ExtractKey,
|
|
806 _Equal, _H1, _H2, _Hash,
|
|
807 _Traits>;
|
|
808
|
|
809 using value_type = typename __hashtable_base::value_type;
|
|
810 using iterator = typename __hashtable_base::iterator;
|
|
811 using const_iterator = typename __hashtable_base::const_iterator;
|
|
812 using size_type = typename __hashtable_base::size_type;
|
|
813
|
|
814 using __unique_keys = typename __hashtable_base::__unique_keys;
|
|
815 using __ireturn_type = typename __hashtable_base::__ireturn_type;
|
|
816 using __node_type = _Hash_node<_Value, _Traits::__hash_cached::value>;
|
|
817 using __node_alloc_type = __alloc_rebind<_Alloc, __node_type>;
|
|
818 using __node_gen_type = _AllocNode<__node_alloc_type>;
|
|
819
|
|
820 __hashtable&
|
|
821 _M_conjure_hashtable()
|
|
822 { return *(static_cast<__hashtable*>(this)); }
|
|
823
|
|
824 template<typename _InputIterator, typename _NodeGetter>
|
|
825 void
|
|
826 _M_insert_range(_InputIterator __first, _InputIterator __last,
|
|
827 const _NodeGetter&);
|
|
828
|
|
829 public:
|
|
830 __ireturn_type
|
|
831 insert(const value_type& __v)
|
|
832 {
|
|
833 __hashtable& __h = _M_conjure_hashtable();
|
|
834 __node_gen_type __node_gen(__h);
|
|
835 return __h._M_insert(__v, __node_gen, __unique_keys());
|
|
836 }
|
|
837
|
|
838 iterator
|
|
839 insert(const_iterator __hint, const value_type& __v)
|
|
840 {
|
|
841 __hashtable& __h = _M_conjure_hashtable();
|
|
842 __node_gen_type __node_gen(__h);
|
|
843 return __h._M_insert(__hint, __v, __node_gen, __unique_keys());
|
|
844 }
|
|
845
|
|
846 void
|
|
847 insert(initializer_list<value_type> __l)
|
|
848 { this->insert(__l.begin(), __l.end()); }
|
|
849
|
|
850 template<typename _InputIterator>
|
|
851 void
|
|
852 insert(_InputIterator __first, _InputIterator __last)
|
|
853 {
|
|
854 __hashtable& __h = _M_conjure_hashtable();
|
|
855 __node_gen_type __node_gen(__h);
|
|
856 return _M_insert_range(__first, __last, __node_gen);
|
|
857 }
|
|
858 };
|
|
859
|
|
860 template<typename _Key, typename _Value, typename _Alloc,
|
|
861 typename _ExtractKey, typename _Equal,
|
|
862 typename _H1, typename _H2, typename _Hash,
|
|
863 typename _RehashPolicy, typename _Traits>
|
|
864 template<typename _InputIterator, typename _NodeGetter>
|
|
865 void
|
|
866 _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
|
|
867 _RehashPolicy, _Traits>::
|
|
868 _M_insert_range(_InputIterator __first, _InputIterator __last,
|
|
869 const _NodeGetter& __node_gen)
|
|
870 {
|
|
871 using __rehash_type = typename __hashtable::__rehash_type;
|
|
872 using __rehash_state = typename __hashtable::__rehash_state;
|
|
873 using pair_type = std::pair<bool, std::size_t>;
|
|
874
|
|
875 size_type __n_elt = __detail::__distance_fw(__first, __last);
|
|
876
|
|
877 __hashtable& __h = _M_conjure_hashtable();
|
|
878 __rehash_type& __rehash = __h._M_rehash_policy;
|
|
879 const __rehash_state& __saved_state = __rehash._M_state();
|
|
880 pair_type __do_rehash = __rehash._M_need_rehash(__h._M_bucket_count,
|
|
881 __h._M_element_count,
|
|
882 __n_elt);
|
|
883
|
|
884 if (__do_rehash.first)
|
|
885 __h._M_rehash(__do_rehash.second, __saved_state);
|
|
886
|
|
887 for (; __first != __last; ++__first)
|
|
888 __h._M_insert(*__first, __node_gen, __unique_keys());
|
|
889 }
|
|
890
|
|
891 /**
|
|
892 * Primary class template _Insert.
|
|
893 *
|
|
894 * Defines @c insert member functions that depend on _Hashtable policies,
|
|
895 * via partial specializations.
|
|
896 */
|
|
897 template<typename _Key, typename _Value, typename _Alloc,
|
|
898 typename _ExtractKey, typename _Equal,
|
|
899 typename _H1, typename _H2, typename _Hash,
|
|
900 typename _RehashPolicy, typename _Traits,
|
|
901 bool _Constant_iterators = _Traits::__constant_iterators::value>
|
|
902 struct _Insert;
|
|
903
|
|
904 /// Specialization.
|
|
905 template<typename _Key, typename _Value, typename _Alloc,
|
|
906 typename _ExtractKey, typename _Equal,
|
|
907 typename _H1, typename _H2, typename _Hash,
|
|
908 typename _RehashPolicy, typename _Traits>
|
|
909 struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
|
|
910 _RehashPolicy, _Traits, true>
|
|
911 : public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
|
912 _H1, _H2, _Hash, _RehashPolicy, _Traits>
|
|
913 {
|
|
914 using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey,
|
|
915 _Equal, _H1, _H2, _Hash,
|
|
916 _RehashPolicy, _Traits>;
|
|
917
|
|
918 using __hashtable_base = _Hashtable_base<_Key, _Value, _ExtractKey,
|
|
919 _Equal, _H1, _H2, _Hash,
|
|
920 _Traits>;
|
|
921
|
|
922 using value_type = typename __base_type::value_type;
|
|
923 using iterator = typename __base_type::iterator;
|
|
924 using const_iterator = typename __base_type::const_iterator;
|
|
925
|
|
926 using __unique_keys = typename __base_type::__unique_keys;
|
|
927 using __ireturn_type = typename __hashtable_base::__ireturn_type;
|
|
928 using __hashtable = typename __base_type::__hashtable;
|
|
929 using __node_gen_type = typename __base_type::__node_gen_type;
|
|
930
|
|
931 using __base_type::insert;
|
|
932
|
|
933 __ireturn_type
|
|
934 insert(value_type&& __v)
|
|
935 {
|
|
936 __hashtable& __h = this->_M_conjure_hashtable();
|
|
937 __node_gen_type __node_gen(__h);
|
|
938 return __h._M_insert(std::move(__v), __node_gen, __unique_keys());
|
|
939 }
|
|
940
|
|
941 iterator
|
|
942 insert(const_iterator __hint, value_type&& __v)
|
|
943 {
|
|
944 __hashtable& __h = this->_M_conjure_hashtable();
|
|
945 __node_gen_type __node_gen(__h);
|
|
946 return __h._M_insert(__hint, std::move(__v), __node_gen,
|
|
947 __unique_keys());
|
|
948 }
|
|
949 };
|
|
950
|
|
951 /// Specialization.
|
|
952 template<typename _Key, typename _Value, typename _Alloc,
|
|
953 typename _ExtractKey, typename _Equal,
|
|
954 typename _H1, typename _H2, typename _Hash,
|
|
955 typename _RehashPolicy, typename _Traits>
|
|
956 struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
|
|
957 _RehashPolicy, _Traits, false>
|
|
958 : public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
|
959 _H1, _H2, _Hash, _RehashPolicy, _Traits>
|
|
960 {
|
|
961 using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey,
|
|
962 _Equal, _H1, _H2, _Hash,
|
|
963 _RehashPolicy, _Traits>;
|
|
964 using value_type = typename __base_type::value_type;
|
|
965 using iterator = typename __base_type::iterator;
|
|
966 using const_iterator = typename __base_type::const_iterator;
|
|
967
|
|
968 using __unique_keys = typename __base_type::__unique_keys;
|
|
969 using __hashtable = typename __base_type::__hashtable;
|
|
970 using __ireturn_type = typename __base_type::__ireturn_type;
|
|
971
|
|
972 using __base_type::insert;
|
|
973
|
|
974 template<typename _Pair>
|
|
975 using __is_cons = std::is_constructible<value_type, _Pair&&>;
|
|
976
|
|
977 template<typename _Pair>
|
|
978 using _IFcons = std::enable_if<__is_cons<_Pair>::value>;
|
|
979
|
|
980 template<typename _Pair>
|
|
981 using _IFconsp = typename _IFcons<_Pair>::type;
|
|
982
|
|
983 template<typename _Pair, typename = _IFconsp<_Pair>>
|
|
984 __ireturn_type
|
|
985 insert(_Pair&& __v)
|
|
986 {
|
|
987 __hashtable& __h = this->_M_conjure_hashtable();
|
|
988 return __h._M_emplace(__unique_keys(), std::forward<_Pair>(__v));
|
|
989 }
|
|
990
|
|
991 template<typename _Pair, typename = _IFconsp<_Pair>>
|
|
992 iterator
|
|
993 insert(const_iterator __hint, _Pair&& __v)
|
|
994 {
|
|
995 __hashtable& __h = this->_M_conjure_hashtable();
|
|
996 return __h._M_emplace(__hint, __unique_keys(),
|
|
997 std::forward<_Pair>(__v));
|
|
998 }
|
|
999 };
|
|
1000
|
|
1001 template<typename _Policy>
|
|
1002 using __has_load_factor = typename _Policy::__has_load_factor;
|
|
1003
|
|
1004 /**
|
|
1005 * Primary class template _Rehash_base.
|
|
1006 *
|
|
1007 * Give hashtable the max_load_factor functions and reserve iff the
|
|
1008 * rehash policy supports it.
|
|
1009 */
|
|
1010 template<typename _Key, typename _Value, typename _Alloc,
|
|
1011 typename _ExtractKey, typename _Equal,
|
|
1012 typename _H1, typename _H2, typename _Hash,
|
|
1013 typename _RehashPolicy, typename _Traits,
|
|
1014 typename =
|
|
1015 __detected_or_t<std::false_type, __has_load_factor, _RehashPolicy>>
|
|
1016 struct _Rehash_base;
|
|
1017
|
|
1018 /// Specialization when rehash policy doesn't provide load factor management.
|
|
1019 template<typename _Key, typename _Value, typename _Alloc,
|
|
1020 typename _ExtractKey, typename _Equal,
|
|
1021 typename _H1, typename _H2, typename _Hash,
|
|
1022 typename _RehashPolicy, typename _Traits>
|
|
1023 struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
|
1024 _H1, _H2, _Hash, _RehashPolicy, _Traits,
|
|
1025 std::false_type>
|
|
1026 {
|
|
1027 };
|
|
1028
|
|
1029 /// Specialization when rehash policy provide load factor management.
|
|
1030 template<typename _Key, typename _Value, typename _Alloc,
|
|
1031 typename _ExtractKey, typename _Equal,
|
|
1032 typename _H1, typename _H2, typename _Hash,
|
|
1033 typename _RehashPolicy, typename _Traits>
|
|
1034 struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
|
1035 _H1, _H2, _Hash, _RehashPolicy, _Traits,
|
|
1036 std::true_type>
|
|
1037 {
|
|
1038 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
|
|
1039 _Equal, _H1, _H2, _Hash,
|
|
1040 _RehashPolicy, _Traits>;
|
|
1041
|
|
1042 float
|
|
1043 max_load_factor() const noexcept
|
|
1044 {
|
|
1045 const __hashtable* __this = static_cast<const __hashtable*>(this);
|
|
1046 return __this->__rehash_policy().max_load_factor();
|
|
1047 }
|
|
1048
|
|
1049 void
|
|
1050 max_load_factor(float __z)
|
|
1051 {
|
|
1052 __hashtable* __this = static_cast<__hashtable*>(this);
|
|
1053 __this->__rehash_policy(_RehashPolicy(__z));
|
|
1054 }
|
|
1055
|
|
1056 void
|
|
1057 reserve(std::size_t __n)
|
|
1058 {
|
|
1059 __hashtable* __this = static_cast<__hashtable*>(this);
|
|
1060 __this->rehash(__builtin_ceil(__n / max_load_factor()));
|
|
1061 }
|
|
1062 };
|
|
1063
|
|
1064 /**
|
|
1065 * Primary class template _Hashtable_ebo_helper.
|
|
1066 *
|
|
1067 * Helper class using EBO when it is not forbidden (the type is not
|
|
1068 * final) and when it is worth it (the type is empty.)
|
|
1069 */
|
|
1070 template<int _Nm, typename _Tp,
|
|
1071 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
|
|
1072 struct _Hashtable_ebo_helper;
|
|
1073
|
|
1074 /// Specialization using EBO.
|
|
1075 template<int _Nm, typename _Tp>
|
|
1076 struct _Hashtable_ebo_helper<_Nm, _Tp, true>
|
|
1077 : private _Tp
|
|
1078 {
|
|
1079 _Hashtable_ebo_helper() = default;
|
|
1080
|
|
1081 template<typename _OtherTp>
|
|
1082 _Hashtable_ebo_helper(_OtherTp&& __tp)
|
|
1083 : _Tp(std::forward<_OtherTp>(__tp))
|
|
1084 { }
|
|
1085
|
|
1086 static const _Tp&
|
|
1087 _S_cget(const _Hashtable_ebo_helper& __eboh)
|
|
1088 { return static_cast<const _Tp&>(__eboh); }
|
|
1089
|
|
1090 static _Tp&
|
|
1091 _S_get(_Hashtable_ebo_helper& __eboh)
|
|
1092 { return static_cast<_Tp&>(__eboh); }
|
|
1093 };
|
|
1094
|
|
1095 /// Specialization not using EBO.
|
|
1096 template<int _Nm, typename _Tp>
|
|
1097 struct _Hashtable_ebo_helper<_Nm, _Tp, false>
|
|
1098 {
|
|
1099 _Hashtable_ebo_helper() = default;
|
|
1100
|
|
1101 template<typename _OtherTp>
|
|
1102 _Hashtable_ebo_helper(_OtherTp&& __tp)
|
|
1103 : _M_tp(std::forward<_OtherTp>(__tp))
|
|
1104 { }
|
|
1105
|
|
1106 static const _Tp&
|
|
1107 _S_cget(const _Hashtable_ebo_helper& __eboh)
|
|
1108 { return __eboh._M_tp; }
|
|
1109
|
|
1110 static _Tp&
|
|
1111 _S_get(_Hashtable_ebo_helper& __eboh)
|
|
1112 { return __eboh._M_tp; }
|
|
1113
|
|
1114 private:
|
|
1115 _Tp _M_tp;
|
|
1116 };
|
|
1117
|
|
1118 /**
|
|
1119 * Primary class template _Local_iterator_base.
|
|
1120 *
|
|
1121 * Base class for local iterators, used to iterate within a bucket
|
|
1122 * but not between buckets.
|
|
1123 */
|
|
1124 template<typename _Key, typename _Value, typename _ExtractKey,
|
|
1125 typename _H1, typename _H2, typename _Hash,
|
|
1126 bool __cache_hash_code>
|
|
1127 struct _Local_iterator_base;
|
|
1128
|
|
1129 /**
|
|
1130 * Primary class template _Hash_code_base.
|
|
1131 *
|
|
1132 * Encapsulates two policy issues that aren't quite orthogonal.
|
|
1133 * (1) the difference between using a ranged hash function and using
|
|
1134 * the combination of a hash function and a range-hashing function.
|
|
1135 * In the former case we don't have such things as hash codes, so
|
|
1136 * we have a dummy type as placeholder.
|
|
1137 * (2) Whether or not we cache hash codes. Caching hash codes is
|
|
1138 * meaningless if we have a ranged hash function.
|
|
1139 *
|
|
1140 * We also put the key extraction objects here, for convenience.
|
|
1141 * Each specialization derives from one or more of the template
|
|
1142 * parameters to benefit from Ebo. This is important as this type
|
|
1143 * is inherited in some cases by the _Local_iterator_base type used
|
|
1144 * to implement local_iterator and const_local_iterator. As with
|
|
1145 * any iterator type we prefer to make it as small as possible.
|
|
1146 *
|
|
1147 * Primary template is unused except as a hook for specializations.
|
|
1148 */
|
|
1149 template<typename _Key, typename _Value, typename _ExtractKey,
|
|
1150 typename _H1, typename _H2, typename _Hash,
|
|
1151 bool __cache_hash_code>
|
|
1152 struct _Hash_code_base;
|
|
1153
|
|
1154 /// Specialization: ranged hash function, no caching hash codes. H1
|
|
1155 /// and H2 are provided but ignored. We define a dummy hash code type.
|
|
1156 template<typename _Key, typename _Value, typename _ExtractKey,
|
|
1157 typename _H1, typename _H2, typename _Hash>
|
|
1158 struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash, false>
|
|
1159 : private _Hashtable_ebo_helper<0, _ExtractKey>,
|
|
1160 private _Hashtable_ebo_helper<1, _Hash>
|
|
1161 {
|
|
1162 private:
|
|
1163 using __ebo_extract_key = _Hashtable_ebo_helper<0, _ExtractKey>;
|
|
1164 using __ebo_hash = _Hashtable_ebo_helper<1, _Hash>;
|
|
1165
|
|
1166 protected:
|
|
1167 typedef void* __hash_code;
|
|
1168 typedef _Hash_node<_Value, false> __node_type;
|
|
1169
|
|
1170 // We need the default constructor for the local iterators and _Hashtable
|
|
1171 // default constructor.
|
|
1172 _Hash_code_base() = default;
|
|
1173
|
|
1174 _Hash_code_base(const _ExtractKey& __ex, const _H1&, const _H2&,
|
|
1175 const _Hash& __h)
|
|
1176 : __ebo_extract_key(__ex), __ebo_hash(__h) { }
|
|
1177
|
|
1178 __hash_code
|
|
1179 _M_hash_code(const _Key& __key) const
|
|
1180 { return 0; }
|
|
1181
|
|
1182 std::size_t
|
|
1183 _M_bucket_index(const _Key& __k, __hash_code, std::size_t __n) const
|
|
1184 { return _M_ranged_hash()(__k, __n); }
|
|
1185
|
|
1186 std::size_t
|
|
1187 _M_bucket_index(const __node_type* __p, std::size_t __n) const
|
|
1188 noexcept( noexcept(declval<const _Hash&>()(declval<const _Key&>(),
|
|
1189 (std::size_t)0)) )
|
|
1190 { return _M_ranged_hash()(_M_extract()(__p->_M_v()), __n); }
|
|
1191
|
|
1192 void
|
|
1193 _M_store_code(__node_type*, __hash_code) const
|
|
1194 { }
|
|
1195
|
|
1196 void
|
|
1197 _M_copy_code(__node_type*, const __node_type*) const
|
|
1198 { }
|
|
1199
|
|
1200 void
|
|
1201 _M_swap(_Hash_code_base& __x)
|
|
1202 {
|
|
1203 std::swap(_M_extract(), __x._M_extract());
|
|
1204 std::swap(_M_ranged_hash(), __x._M_ranged_hash());
|
|
1205 }
|
|
1206
|
|
1207 const _ExtractKey&
|
|
1208 _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
|
|
1209
|
|
1210 _ExtractKey&
|
|
1211 _M_extract() { return __ebo_extract_key::_S_get(*this); }
|
|
1212
|
|
1213 const _Hash&
|
|
1214 _M_ranged_hash() const { return __ebo_hash::_S_cget(*this); }
|
|
1215
|
|
1216 _Hash&
|
|
1217 _M_ranged_hash() { return __ebo_hash::_S_get(*this); }
|
|
1218 };
|
|
1219
|
|
1220 // No specialization for ranged hash function while caching hash codes.
|
|
1221 // That combination is meaningless, and trying to do it is an error.
|
|
1222
|
|
1223 /// Specialization: ranged hash function, cache hash codes. This
|
|
1224 /// combination is meaningless, so we provide only a declaration
|
|
1225 /// and no definition.
|
|
1226 template<typename _Key, typename _Value, typename _ExtractKey,
|
|
1227 typename _H1, typename _H2, typename _Hash>
|
|
1228 struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash, true>;
|
|
1229
|
|
1230 /// Specialization: hash function and range-hashing function, no
|
|
1231 /// caching of hash codes.
|
|
1232 /// Provides typedef and accessor required by C++ 11.
|
|
1233 template<typename _Key, typename _Value, typename _ExtractKey,
|
|
1234 typename _H1, typename _H2>
|
|
1235 struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
|
|
1236 _Default_ranged_hash, false>
|
|
1237 : private _Hashtable_ebo_helper<0, _ExtractKey>,
|
|
1238 private _Hashtable_ebo_helper<1, _H1>,
|
|
1239 private _Hashtable_ebo_helper<2, _H2>
|
|
1240 {
|
|
1241 private:
|
|
1242 using __ebo_extract_key = _Hashtable_ebo_helper<0, _ExtractKey>;
|
|
1243 using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>;
|
|
1244 using __ebo_h2 = _Hashtable_ebo_helper<2, _H2>;
|
|
1245
|
|
1246 // Gives the local iterator implementation access to _M_bucket_index().
|
|
1247 friend struct _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2,
|
|
1248 _Default_ranged_hash, false>;
|
|
1249
|
|
1250 public:
|
|
1251 typedef _H1 hasher;
|
|
1252
|
|
1253 hasher
|
|
1254 hash_function() const
|
|
1255 { return _M_h1(); }
|
|
1256
|
|
1257 protected:
|
|
1258 typedef std::size_t __hash_code;
|
|
1259 typedef _Hash_node<_Value, false> __node_type;
|
|
1260
|
|
1261 // We need the default constructor for the local iterators and _Hashtable
|
|
1262 // default constructor.
|
|
1263 _Hash_code_base() = default;
|
|
1264
|
|
1265 _Hash_code_base(const _ExtractKey& __ex,
|
|
1266 const _H1& __h1, const _H2& __h2,
|
|
1267 const _Default_ranged_hash&)
|
|
1268 : __ebo_extract_key(__ex), __ebo_h1(__h1), __ebo_h2(__h2) { }
|
|
1269
|
|
1270 __hash_code
|
|
1271 _M_hash_code(const _Key& __k) const
|
|
1272 { return _M_h1()(__k); }
|
|
1273
|
|
1274 std::size_t
|
|
1275 _M_bucket_index(const _Key&, __hash_code __c, std::size_t __n) const
|
|
1276 { return _M_h2()(__c, __n); }
|
|
1277
|
|
1278 std::size_t
|
|
1279 _M_bucket_index(const __node_type* __p, std::size_t __n) const
|
|
1280 noexcept( noexcept(declval<const _H1&>()(declval<const _Key&>()))
|
|
1281 && noexcept(declval<const _H2&>()((__hash_code)0,
|
|
1282 (std::size_t)0)) )
|
|
1283 { return _M_h2()(_M_h1()(_M_extract()(__p->_M_v())), __n); }
|
|
1284
|
|
1285 void
|
|
1286 _M_store_code(__node_type*, __hash_code) const
|
|
1287 { }
|
|
1288
|
|
1289 void
|
|
1290 _M_copy_code(__node_type*, const __node_type*) const
|
|
1291 { }
|
|
1292
|
|
1293 void
|
|
1294 _M_swap(_Hash_code_base& __x)
|
|
1295 {
|
|
1296 std::swap(_M_extract(), __x._M_extract());
|
|
1297 std::swap(_M_h1(), __x._M_h1());
|
|
1298 std::swap(_M_h2(), __x._M_h2());
|
|
1299 }
|
|
1300
|
|
1301 const _ExtractKey&
|
|
1302 _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
|
|
1303
|
|
1304 _ExtractKey&
|
|
1305 _M_extract() { return __ebo_extract_key::_S_get(*this); }
|
|
1306
|
|
1307 const _H1&
|
|
1308 _M_h1() const { return __ebo_h1::_S_cget(*this); }
|
|
1309
|
|
1310 _H1&
|
|
1311 _M_h1() { return __ebo_h1::_S_get(*this); }
|
|
1312
|
|
1313 const _H2&
|
|
1314 _M_h2() const { return __ebo_h2::_S_cget(*this); }
|
|
1315
|
|
1316 _H2&
|
|
1317 _M_h2() { return __ebo_h2::_S_get(*this); }
|
|
1318 };
|
|
1319
|
|
1320 /// Specialization: hash function and range-hashing function,
|
|
1321 /// caching hash codes. H is provided but ignored. Provides
|
|
1322 /// typedef and accessor required by C++ 11.
|
|
1323 template<typename _Key, typename _Value, typename _ExtractKey,
|
|
1324 typename _H1, typename _H2>
|
|
1325 struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
|
|
1326 _Default_ranged_hash, true>
|
|
1327 : private _Hashtable_ebo_helper<0, _ExtractKey>,
|
|
1328 private _Hashtable_ebo_helper<1, _H1>,
|
|
1329 private _Hashtable_ebo_helper<2, _H2>
|
|
1330 {
|
|
1331 private:
|
|
1332 // Gives the local iterator implementation access to _M_h2().
|
|
1333 friend struct _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2,
|
|
1334 _Default_ranged_hash, true>;
|
|
1335
|
|
1336 using __ebo_extract_key = _Hashtable_ebo_helper<0, _ExtractKey>;
|
|
1337 using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>;
|
|
1338 using __ebo_h2 = _Hashtable_ebo_helper<2, _H2>;
|
|
1339
|
|
1340 public:
|
|
1341 typedef _H1 hasher;
|
|
1342
|
|
1343 hasher
|
|
1344 hash_function() const
|
|
1345 { return _M_h1(); }
|
|
1346
|
|
1347 protected:
|
|
1348 typedef std::size_t __hash_code;
|
|
1349 typedef _Hash_node<_Value, true> __node_type;
|
|
1350
|
|
1351 // We need the default constructor for _Hashtable default constructor.
|
|
1352 _Hash_code_base() = default;
|
|
1353 _Hash_code_base(const _ExtractKey& __ex,
|
|
1354 const _H1& __h1, const _H2& __h2,
|
|
1355 const _Default_ranged_hash&)
|
|
1356 : __ebo_extract_key(__ex), __ebo_h1(__h1), __ebo_h2(__h2) { }
|
|
1357
|
|
1358 __hash_code
|
|
1359 _M_hash_code(const _Key& __k) const
|
|
1360 { return _M_h1()(__k); }
|
|
1361
|
|
1362 std::size_t
|
|
1363 _M_bucket_index(const _Key&, __hash_code __c,
|
|
1364 std::size_t __n) const
|
|
1365 { return _M_h2()(__c, __n); }
|
|
1366
|
|
1367 std::size_t
|
|
1368 _M_bucket_index(const __node_type* __p, std::size_t __n) const
|
|
1369 noexcept( noexcept(declval<const _H2&>()((__hash_code)0,
|
|
1370 (std::size_t)0)) )
|
|
1371 { return _M_h2()(__p->_M_hash_code, __n); }
|
|
1372
|
|
1373 void
|
|
1374 _M_store_code(__node_type* __n, __hash_code __c) const
|
|
1375 { __n->_M_hash_code = __c; }
|
|
1376
|
|
1377 void
|
|
1378 _M_copy_code(__node_type* __to, const __node_type* __from) const
|
|
1379 { __to->_M_hash_code = __from->_M_hash_code; }
|
|
1380
|
|
1381 void
|
|
1382 _M_swap(_Hash_code_base& __x)
|
|
1383 {
|
|
1384 std::swap(_M_extract(), __x._M_extract());
|
|
1385 std::swap(_M_h1(), __x._M_h1());
|
|
1386 std::swap(_M_h2(), __x._M_h2());
|
|
1387 }
|
|
1388
|
|
1389 const _ExtractKey&
|
|
1390 _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
|
|
1391
|
|
1392 _ExtractKey&
|
|
1393 _M_extract() { return __ebo_extract_key::_S_get(*this); }
|
|
1394
|
|
1395 const _H1&
|
|
1396 _M_h1() const { return __ebo_h1::_S_cget(*this); }
|
|
1397
|
|
1398 _H1&
|
|
1399 _M_h1() { return __ebo_h1::_S_get(*this); }
|
|
1400
|
|
1401 const _H2&
|
|
1402 _M_h2() const { return __ebo_h2::_S_cget(*this); }
|
|
1403
|
|
1404 _H2&
|
|
1405 _M_h2() { return __ebo_h2::_S_get(*this); }
|
|
1406 };
|
|
1407
|
|
1408 /**
|
|
1409 * Primary class template _Equal_helper.
|
|
1410 *
|
|
1411 */
|
|
1412 template <typename _Key, typename _Value, typename _ExtractKey,
|
|
1413 typename _Equal, typename _HashCodeType,
|
|
1414 bool __cache_hash_code>
|
|
1415 struct _Equal_helper;
|
|
1416
|
|
1417 /// Specialization.
|
|
1418 template<typename _Key, typename _Value, typename _ExtractKey,
|
|
1419 typename _Equal, typename _HashCodeType>
|
|
1420 struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, true>
|
|
1421 {
|
|
1422 static bool
|
|
1423 _S_equals(const _Equal& __eq, const _ExtractKey& __extract,
|
|
1424 const _Key& __k, _HashCodeType __c, _Hash_node<_Value, true>* __n)
|
|
1425 { return __c == __n->_M_hash_code && __eq(__k, __extract(__n->_M_v())); }
|
|
1426 };
|
|
1427
|
|
1428 /// Specialization.
|
|
1429 template<typename _Key, typename _Value, typename _ExtractKey,
|
|
1430 typename _Equal, typename _HashCodeType>
|
|
1431 struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, false>
|
|
1432 {
|
|
1433 static bool
|
|
1434 _S_equals(const _Equal& __eq, const _ExtractKey& __extract,
|
|
1435 const _Key& __k, _HashCodeType, _Hash_node<_Value, false>* __n)
|
|
1436 { return __eq(__k, __extract(__n->_M_v())); }
|
|
1437 };
|
|
1438
|
|
1439
|
|
1440 /// Partial specialization used when nodes contain a cached hash code.
|
|
1441 template<typename _Key, typename _Value, typename _ExtractKey,
|
|
1442 typename _H1, typename _H2, typename _Hash>
|
|
1443 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
|
|
1444 _H1, _H2, _Hash, true>
|
|
1445 : private _Hashtable_ebo_helper<0, _H2>
|
|
1446 {
|
|
1447 protected:
|
|
1448 using __base_type = _Hashtable_ebo_helper<0, _H2>;
|
|
1449 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
|
|
1450 _H1, _H2, _Hash, true>;
|
|
1451
|
|
1452 _Local_iterator_base() = default;
|
|
1453 _Local_iterator_base(const __hash_code_base& __base,
|
|
1454 _Hash_node<_Value, true>* __p,
|
|
1455 std::size_t __bkt, std::size_t __bkt_count)
|
|
1456 : __base_type(__base._M_h2()),
|
|
1457 _M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count) { }
|
|
1458
|
|
1459 void
|
|
1460 _M_incr()
|
|
1461 {
|
|
1462 _M_cur = _M_cur->_M_next();
|
|
1463 if (_M_cur)
|
|
1464 {
|
|
1465 std::size_t __bkt
|
|
1466 = __base_type::_S_get(*this)(_M_cur->_M_hash_code,
|
|
1467 _M_bucket_count);
|
|
1468 if (__bkt != _M_bucket)
|
|
1469 _M_cur = nullptr;
|
|
1470 }
|
|
1471 }
|
|
1472
|
|
1473 _Hash_node<_Value, true>* _M_cur;
|
|
1474 std::size_t _M_bucket;
|
|
1475 std::size_t _M_bucket_count;
|
|
1476
|
|
1477 public:
|
|
1478 const void*
|
|
1479 _M_curr() const { return _M_cur; } // for equality ops
|
|
1480
|
|
1481 std::size_t
|
|
1482 _M_get_bucket() const { return _M_bucket; } // for debug mode
|
|
1483 };
|
|
1484
|
|
1485 // Uninitialized storage for a _Hash_code_base.
|
|
1486 // This type is DefaultConstructible and Assignable even if the
|
|
1487 // _Hash_code_base type isn't, so that _Local_iterator_base<..., false>
|
|
1488 // can be DefaultConstructible and Assignable.
|
|
1489 template<typename _Tp, bool _IsEmpty = std::is_empty<_Tp>::value>
|
|
1490 struct _Hash_code_storage
|
|
1491 {
|
|
1492 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
|
|
1493
|
|
1494 _Tp*
|
|
1495 _M_h() { return _M_storage._M_ptr(); }
|
|
1496
|
|
1497 const _Tp*
|
|
1498 _M_h() const { return _M_storage._M_ptr(); }
|
|
1499 };
|
|
1500
|
|
1501 // Empty partial specialization for empty _Hash_code_base types.
|
|
1502 template<typename _Tp>
|
|
1503 struct _Hash_code_storage<_Tp, true>
|
|
1504 {
|
|
1505 static_assert( std::is_empty<_Tp>::value, "Type must be empty" );
|
|
1506
|
|
1507 // As _Tp is an empty type there will be no bytes written/read through
|
|
1508 // the cast pointer, so no strict-aliasing violation.
|
|
1509 _Tp*
|
|
1510 _M_h() { return reinterpret_cast<_Tp*>(this); }
|
|
1511
|
|
1512 const _Tp*
|
|
1513 _M_h() const { return reinterpret_cast<const _Tp*>(this); }
|
|
1514 };
|
|
1515
|
|
1516 template<typename _Key, typename _Value, typename _ExtractKey,
|
|
1517 typename _H1, typename _H2, typename _Hash>
|
|
1518 using __hash_code_for_local_iter
|
|
1519 = _Hash_code_storage<_Hash_code_base<_Key, _Value, _ExtractKey,
|
|
1520 _H1, _H2, _Hash, false>>;
|
|
1521
|
|
1522 // Partial specialization used when hash codes are not cached
|
|
1523 template<typename _Key, typename _Value, typename _ExtractKey,
|
|
1524 typename _H1, typename _H2, typename _Hash>
|
|
1525 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
|
|
1526 _H1, _H2, _Hash, false>
|
|
1527 : __hash_code_for_local_iter<_Key, _Value, _ExtractKey, _H1, _H2, _Hash>
|
|
1528 {
|
|
1529 protected:
|
|
1530 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
|
|
1531 _H1, _H2, _Hash, false>;
|
|
1532
|
|
1533 _Local_iterator_base() : _M_bucket_count(-1) { }
|
|
1534
|
|
1535 _Local_iterator_base(const __hash_code_base& __base,
|
|
1536 _Hash_node<_Value, false>* __p,
|
|
1537 std::size_t __bkt, std::size_t __bkt_count)
|
|
1538 : _M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count)
|
|
1539 { _M_init(__base); }
|
|
1540
|
|
1541 ~_Local_iterator_base()
|
|
1542 {
|
|
1543 if (_M_bucket_count != -1)
|
|
1544 _M_destroy();
|
|
1545 }
|
|
1546
|
|
1547 _Local_iterator_base(const _Local_iterator_base& __iter)
|
|
1548 : _M_cur(__iter._M_cur), _M_bucket(__iter._M_bucket),
|
|
1549 _M_bucket_count(__iter._M_bucket_count)
|
|
1550 {
|
|
1551 if (_M_bucket_count != -1)
|
|
1552 _M_init(*__iter._M_h());
|
|
1553 }
|
|
1554
|
|
1555 _Local_iterator_base&
|
|
1556 operator=(const _Local_iterator_base& __iter)
|
|
1557 {
|
|
1558 if (_M_bucket_count != -1)
|
|
1559 _M_destroy();
|
|
1560 _M_cur = __iter._M_cur;
|
|
1561 _M_bucket = __iter._M_bucket;
|
|
1562 _M_bucket_count = __iter._M_bucket_count;
|
|
1563 if (_M_bucket_count != -1)
|
|
1564 _M_init(*__iter._M_h());
|
|
1565 return *this;
|
|
1566 }
|
|
1567
|
|
1568 void
|
|
1569 _M_incr()
|
|
1570 {
|
|
1571 _M_cur = _M_cur->_M_next();
|
|
1572 if (_M_cur)
|
|
1573 {
|
|
1574 std::size_t __bkt = this->_M_h()->_M_bucket_index(_M_cur,
|
|
1575 _M_bucket_count);
|
|
1576 if (__bkt != _M_bucket)
|
|
1577 _M_cur = nullptr;
|
|
1578 }
|
|
1579 }
|
|
1580
|
|
1581 _Hash_node<_Value, false>* _M_cur;
|
|
1582 std::size_t _M_bucket;
|
|
1583 std::size_t _M_bucket_count;
|
|
1584
|
|
1585 void
|
|
1586 _M_init(const __hash_code_base& __base)
|
|
1587 { ::new(this->_M_h()) __hash_code_base(__base); }
|
|
1588
|
|
1589 void
|
|
1590 _M_destroy() { this->_M_h()->~__hash_code_base(); }
|
|
1591
|
|
1592 public:
|
|
1593 const void*
|
|
1594 _M_curr() const { return _M_cur; } // for equality ops and debug mode
|
|
1595
|
|
1596 std::size_t
|
|
1597 _M_get_bucket() const { return _M_bucket; } // for debug mode
|
|
1598 };
|
|
1599
|
|
1600 template<typename _Key, typename _Value, typename _ExtractKey,
|
|
1601 typename _H1, typename _H2, typename _Hash, bool __cache>
|
|
1602 inline bool
|
|
1603 operator==(const _Local_iterator_base<_Key, _Value, _ExtractKey,
|
|
1604 _H1, _H2, _Hash, __cache>& __x,
|
|
1605 const _Local_iterator_base<_Key, _Value, _ExtractKey,
|
|
1606 _H1, _H2, _Hash, __cache>& __y)
|
|
1607 { return __x._M_curr() == __y._M_curr(); }
|
|
1608
|
|
1609 template<typename _Key, typename _Value, typename _ExtractKey,
|
|
1610 typename _H1, typename _H2, typename _Hash, bool __cache>
|
|
1611 inline bool
|
|
1612 operator!=(const _Local_iterator_base<_Key, _Value, _ExtractKey,
|
|
1613 _H1, _H2, _Hash, __cache>& __x,
|
|
1614 const _Local_iterator_base<_Key, _Value, _ExtractKey,
|
|
1615 _H1, _H2, _Hash, __cache>& __y)
|
|
1616 { return __x._M_curr() != __y._M_curr(); }
|
|
1617
|
|
1618 /// local iterators
|
|
1619 template<typename _Key, typename _Value, typename _ExtractKey,
|
|
1620 typename _H1, typename _H2, typename _Hash,
|
|
1621 bool __constant_iterators, bool __cache>
|
|
1622 struct _Local_iterator
|
|
1623 : public _Local_iterator_base<_Key, _Value, _ExtractKey,
|
|
1624 _H1, _H2, _Hash, __cache>
|
|
1625 {
|
|
1626 private:
|
|
1627 using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey,
|
|
1628 _H1, _H2, _Hash, __cache>;
|
|
1629 using __hash_code_base = typename __base_type::__hash_code_base;
|
|
1630 public:
|
|
1631 typedef _Value value_type;
|
|
1632 typedef typename std::conditional<__constant_iterators,
|
|
1633 const _Value*, _Value*>::type
|
|
1634 pointer;
|
|
1635 typedef typename std::conditional<__constant_iterators,
|
|
1636 const _Value&, _Value&>::type
|
|
1637 reference;
|
|
1638 typedef std::ptrdiff_t difference_type;
|
|
1639 typedef std::forward_iterator_tag iterator_category;
|
|
1640
|
|
1641 _Local_iterator() = default;
|
|
1642
|
|
1643 _Local_iterator(const __hash_code_base& __base,
|
|
1644 _Hash_node<_Value, __cache>* __p,
|
|
1645 std::size_t __bkt, std::size_t __bkt_count)
|
|
1646 : __base_type(__base, __p, __bkt, __bkt_count)
|
|
1647 { }
|
|
1648
|
|
1649 reference
|
|
1650 operator*() const
|
|
1651 { return this->_M_cur->_M_v(); }
|
|
1652
|
|
1653 pointer
|
|
1654 operator->() const
|
|
1655 { return this->_M_cur->_M_valptr(); }
|
|
1656
|
|
1657 _Local_iterator&
|
|
1658 operator++()
|
|
1659 {
|
|
1660 this->_M_incr();
|
|
1661 return *this;
|
|
1662 }
|
|
1663
|
|
1664 _Local_iterator
|
|
1665 operator++(int)
|
|
1666 {
|
|
1667 _Local_iterator __tmp(*this);
|
|
1668 this->_M_incr();
|
|
1669 return __tmp;
|
|
1670 }
|
|
1671 };
|
|
1672
|
|
1673 /// local const_iterators
|
|
1674 template<typename _Key, typename _Value, typename _ExtractKey,
|
|
1675 typename _H1, typename _H2, typename _Hash,
|
|
1676 bool __constant_iterators, bool __cache>
|
|
1677 struct _Local_const_iterator
|
|
1678 : public _Local_iterator_base<_Key, _Value, _ExtractKey,
|
|
1679 _H1, _H2, _Hash, __cache>
|
|
1680 {
|
|
1681 private:
|
|
1682 using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey,
|
|
1683 _H1, _H2, _Hash, __cache>;
|
|
1684 using __hash_code_base = typename __base_type::__hash_code_base;
|
|
1685
|
|
1686 public:
|
|
1687 typedef _Value value_type;
|
|
1688 typedef const _Value* pointer;
|
|
1689 typedef const _Value& reference;
|
|
1690 typedef std::ptrdiff_t difference_type;
|
|
1691 typedef std::forward_iterator_tag iterator_category;
|
|
1692
|
|
1693 _Local_const_iterator() = default;
|
|
1694
|
|
1695 _Local_const_iterator(const __hash_code_base& __base,
|
|
1696 _Hash_node<_Value, __cache>* __p,
|
|
1697 std::size_t __bkt, std::size_t __bkt_count)
|
|
1698 : __base_type(__base, __p, __bkt, __bkt_count)
|
|
1699 { }
|
|
1700
|
|
1701 _Local_const_iterator(const _Local_iterator<_Key, _Value, _ExtractKey,
|
|
1702 _H1, _H2, _Hash,
|
|
1703 __constant_iterators,
|
|
1704 __cache>& __x)
|
|
1705 : __base_type(__x)
|
|
1706 { }
|
|
1707
|
|
1708 reference
|
|
1709 operator*() const
|
|
1710 { return this->_M_cur->_M_v(); }
|
|
1711
|
|
1712 pointer
|
|
1713 operator->() const
|
|
1714 { return this->_M_cur->_M_valptr(); }
|
|
1715
|
|
1716 _Local_const_iterator&
|
|
1717 operator++()
|
|
1718 {
|
|
1719 this->_M_incr();
|
|
1720 return *this;
|
|
1721 }
|
|
1722
|
|
1723 _Local_const_iterator
|
|
1724 operator++(int)
|
|
1725 {
|
|
1726 _Local_const_iterator __tmp(*this);
|
|
1727 this->_M_incr();
|
|
1728 return __tmp;
|
|
1729 }
|
|
1730 };
|
|
1731
|
|
1732 /**
|
|
1733 * Primary class template _Hashtable_base.
|
|
1734 *
|
|
1735 * Helper class adding management of _Equal functor to
|
|
1736 * _Hash_code_base type.
|
|
1737 *
|
|
1738 * Base class templates are:
|
|
1739 * - __detail::_Hash_code_base
|
|
1740 * - __detail::_Hashtable_ebo_helper
|
|
1741 */
|
|
1742 template<typename _Key, typename _Value,
|
|
1743 typename _ExtractKey, typename _Equal,
|
|
1744 typename _H1, typename _H2, typename _Hash, typename _Traits>
|
|
1745 struct _Hashtable_base
|
|
1746 : public _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
|
|
1747 _Traits::__hash_cached::value>,
|
|
1748 private _Hashtable_ebo_helper<0, _Equal>
|
|
1749 {
|
|
1750 public:
|
|
1751 typedef _Key key_type;
|
|
1752 typedef _Value value_type;
|
|
1753 typedef _Equal key_equal;
|
|
1754 typedef std::size_t size_type;
|
|
1755 typedef std::ptrdiff_t difference_type;
|
|
1756
|
|
1757 using __traits_type = _Traits;
|
|
1758 using __hash_cached = typename __traits_type::__hash_cached;
|
|
1759 using __constant_iterators = typename __traits_type::__constant_iterators;
|
|
1760 using __unique_keys = typename __traits_type::__unique_keys;
|
|
1761
|
|
1762 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
|
|
1763 _H1, _H2, _Hash,
|
|
1764 __hash_cached::value>;
|
|
1765
|
|
1766 using __hash_code = typename __hash_code_base::__hash_code;
|
|
1767 using __node_type = typename __hash_code_base::__node_type;
|
|
1768
|
|
1769 using iterator = __detail::_Node_iterator<value_type,
|
|
1770 __constant_iterators::value,
|
|
1771 __hash_cached::value>;
|
|
1772
|
|
1773 using const_iterator = __detail::_Node_const_iterator<value_type,
|
|
1774 __constant_iterators::value,
|
|
1775 __hash_cached::value>;
|
|
1776
|
|
1777 using local_iterator = __detail::_Local_iterator<key_type, value_type,
|
|
1778 _ExtractKey, _H1, _H2, _Hash,
|
|
1779 __constant_iterators::value,
|
|
1780 __hash_cached::value>;
|
|
1781
|
|
1782 using const_local_iterator = __detail::_Local_const_iterator<key_type,
|
|
1783 value_type,
|
|
1784 _ExtractKey, _H1, _H2, _Hash,
|
|
1785 __constant_iterators::value,
|
|
1786 __hash_cached::value>;
|
|
1787
|
|
1788 using __ireturn_type = typename std::conditional<__unique_keys::value,
|
|
1789 std::pair<iterator, bool>,
|
|
1790 iterator>::type;
|
|
1791 private:
|
|
1792 using _EqualEBO = _Hashtable_ebo_helper<0, _Equal>;
|
|
1793 using _EqualHelper = _Equal_helper<_Key, _Value, _ExtractKey, _Equal,
|
|
1794 __hash_code, __hash_cached::value>;
|
|
1795
|
|
1796 protected:
|
|
1797 _Hashtable_base() = default;
|
|
1798 _Hashtable_base(const _ExtractKey& __ex, const _H1& __h1, const _H2& __h2,
|
|
1799 const _Hash& __hash, const _Equal& __eq)
|
|
1800 : __hash_code_base(__ex, __h1, __h2, __hash), _EqualEBO(__eq)
|
|
1801 { }
|
|
1802
|
|
1803 bool
|
|
1804 _M_equals(const _Key& __k, __hash_code __c, __node_type* __n) const
|
|
1805 {
|
|
1806 return _EqualHelper::_S_equals(_M_eq(), this->_M_extract(),
|
|
1807 __k, __c, __n);
|
|
1808 }
|
|
1809
|
|
1810 void
|
|
1811 _M_swap(_Hashtable_base& __x)
|
|
1812 {
|
|
1813 __hash_code_base::_M_swap(__x);
|
|
1814 std::swap(_M_eq(), __x._M_eq());
|
|
1815 }
|
|
1816
|
|
1817 const _Equal&
|
|
1818 _M_eq() const { return _EqualEBO::_S_cget(*this); }
|
|
1819
|
|
1820 _Equal&
|
|
1821 _M_eq() { return _EqualEBO::_S_get(*this); }
|
|
1822 };
|
|
1823
|
|
1824 /**
|
|
1825 * struct _Equality_base.
|
|
1826 *
|
|
1827 * Common types and functions for class _Equality.
|
|
1828 */
|
|
1829 struct _Equality_base
|
|
1830 {
|
|
1831 protected:
|
|
1832 template<typename _Uiterator>
|
|
1833 static bool
|
|
1834 _S_is_permutation(_Uiterator, _Uiterator, _Uiterator);
|
|
1835 };
|
|
1836
|
|
1837 // See std::is_permutation in N3068.
|
|
1838 template<typename _Uiterator>
|
|
1839 bool
|
|
1840 _Equality_base::
|
|
1841 _S_is_permutation(_Uiterator __first1, _Uiterator __last1,
|
|
1842 _Uiterator __first2)
|
|
1843 {
|
|
1844 for (; __first1 != __last1; ++__first1, ++__first2)
|
|
1845 if (!(*__first1 == *__first2))
|
|
1846 break;
|
|
1847
|
|
1848 if (__first1 == __last1)
|
|
1849 return true;
|
|
1850
|
|
1851 _Uiterator __last2 = __first2;
|
|
1852 std::advance(__last2, std::distance(__first1, __last1));
|
|
1853
|
|
1854 for (_Uiterator __it1 = __first1; __it1 != __last1; ++__it1)
|
|
1855 {
|
|
1856 _Uiterator __tmp = __first1;
|
|
1857 while (__tmp != __it1 && !bool(*__tmp == *__it1))
|
|
1858 ++__tmp;
|
|
1859
|
|
1860 // We've seen this one before.
|
|
1861 if (__tmp != __it1)
|
|
1862 continue;
|
|
1863
|
|
1864 std::ptrdiff_t __n2 = 0;
|
|
1865 for (__tmp = __first2; __tmp != __last2; ++__tmp)
|
|
1866 if (*__tmp == *__it1)
|
|
1867 ++__n2;
|
|
1868
|
|
1869 if (!__n2)
|
|
1870 return false;
|
|
1871
|
|
1872 std::ptrdiff_t __n1 = 0;
|
|
1873 for (__tmp = __it1; __tmp != __last1; ++__tmp)
|
|
1874 if (*__tmp == *__it1)
|
|
1875 ++__n1;
|
|
1876
|
|
1877 if (__n1 != __n2)
|
|
1878 return false;
|
|
1879 }
|
|
1880 return true;
|
|
1881 }
|
|
1882
|
|
1883 /**
|
|
1884 * Primary class template _Equality.
|
|
1885 *
|
|
1886 * This is for implementing equality comparison for unordered
|
|
1887 * containers, per N3068, by John Lakos and Pablo Halpern.
|
|
1888 * Algorithmically, we follow closely the reference implementations
|
|
1889 * therein.
|
|
1890 */
|
|
1891 template<typename _Key, typename _Value, typename _Alloc,
|
|
1892 typename _ExtractKey, typename _Equal,
|
|
1893 typename _H1, typename _H2, typename _Hash,
|
|
1894 typename _RehashPolicy, typename _Traits,
|
|
1895 bool _Unique_keys = _Traits::__unique_keys::value>
|
|
1896 struct _Equality;
|
|
1897
|
|
1898 /// Specialization.
|
|
1899 template<typename _Key, typename _Value, typename _Alloc,
|
|
1900 typename _ExtractKey, typename _Equal,
|
|
1901 typename _H1, typename _H2, typename _Hash,
|
|
1902 typename _RehashPolicy, typename _Traits>
|
|
1903 struct _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
|
1904 _H1, _H2, _Hash, _RehashPolicy, _Traits, true>
|
|
1905 {
|
|
1906 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
|
1907 _H1, _H2, _Hash, _RehashPolicy, _Traits>;
|
|
1908
|
|
1909 bool
|
|
1910 _M_equal(const __hashtable&) const;
|
|
1911 };
|
|
1912
|
|
1913 template<typename _Key, typename _Value, typename _Alloc,
|
|
1914 typename _ExtractKey, typename _Equal,
|
|
1915 typename _H1, typename _H2, typename _Hash,
|
|
1916 typename _RehashPolicy, typename _Traits>
|
|
1917 bool
|
|
1918 _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
|
1919 _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
|
|
1920 _M_equal(const __hashtable& __other) const
|
|
1921 {
|
|
1922 const __hashtable* __this = static_cast<const __hashtable*>(this);
|
|
1923
|
|
1924 if (__this->size() != __other.size())
|
|
1925 return false;
|
|
1926
|
|
1927 for (auto __itx = __this->begin(); __itx != __this->end(); ++__itx)
|
|
1928 {
|
|
1929 const auto __ity = __other.find(_ExtractKey()(*__itx));
|
|
1930 if (__ity == __other.end() || !bool(*__ity == *__itx))
|
|
1931 return false;
|
|
1932 }
|
|
1933 return true;
|
|
1934 }
|
|
1935
|
|
1936 /// Specialization.
|
|
1937 template<typename _Key, typename _Value, typename _Alloc,
|
|
1938 typename _ExtractKey, typename _Equal,
|
|
1939 typename _H1, typename _H2, typename _Hash,
|
|
1940 typename _RehashPolicy, typename _Traits>
|
|
1941 struct _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
|
1942 _H1, _H2, _Hash, _RehashPolicy, _Traits, false>
|
|
1943 : public _Equality_base
|
|
1944 {
|
|
1945 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
|
1946 _H1, _H2, _Hash, _RehashPolicy, _Traits>;
|
|
1947
|
|
1948 bool
|
|
1949 _M_equal(const __hashtable&) const;
|
|
1950 };
|
|
1951
|
|
1952 template<typename _Key, typename _Value, typename _Alloc,
|
|
1953 typename _ExtractKey, typename _Equal,
|
|
1954 typename _H1, typename _H2, typename _Hash,
|
|
1955 typename _RehashPolicy, typename _Traits>
|
|
1956 bool
|
|
1957 _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
|
1958 _H1, _H2, _Hash, _RehashPolicy, _Traits, false>::
|
|
1959 _M_equal(const __hashtable& __other) const
|
|
1960 {
|
|
1961 const __hashtable* __this = static_cast<const __hashtable*>(this);
|
|
1962
|
|
1963 if (__this->size() != __other.size())
|
|
1964 return false;
|
|
1965
|
|
1966 for (auto __itx = __this->begin(); __itx != __this->end();)
|
|
1967 {
|
|
1968 const auto __xrange = __this->equal_range(_ExtractKey()(*__itx));
|
|
1969 const auto __yrange = __other.equal_range(_ExtractKey()(*__itx));
|
|
1970
|
|
1971 if (std::distance(__xrange.first, __xrange.second)
|
|
1972 != std::distance(__yrange.first, __yrange.second))
|
|
1973 return false;
|
|
1974
|
|
1975 if (!_S_is_permutation(__xrange.first, __xrange.second,
|
|
1976 __yrange.first))
|
|
1977 return false;
|
|
1978
|
|
1979 __itx = __xrange.second;
|
|
1980 }
|
|
1981 return true;
|
|
1982 }
|
|
1983
|
|
1984 /**
|
|
1985 * This type deals with all allocation and keeps an allocator instance through
|
|
1986 * inheritance to benefit from EBO when possible.
|
|
1987 */
|
|
1988 template<typename _NodeAlloc>
|
|
1989 struct _Hashtable_alloc : private _Hashtable_ebo_helper<0, _NodeAlloc>
|
|
1990 {
|
|
1991 private:
|
|
1992 using __ebo_node_alloc = _Hashtable_ebo_helper<0, _NodeAlloc>;
|
|
1993 public:
|
|
1994 using __node_type = typename _NodeAlloc::value_type;
|
|
1995 using __node_alloc_type = _NodeAlloc;
|
|
1996 // Use __gnu_cxx to benefit from _S_always_equal and al.
|
|
1997 using __node_alloc_traits = __gnu_cxx::__alloc_traits<__node_alloc_type>;
|
|
1998
|
|
1999 using __value_alloc_traits = typename __node_alloc_traits::template
|
|
2000 rebind_traits<typename __node_type::value_type>;
|
|
2001
|
|
2002 using __node_base = __detail::_Hash_node_base;
|
|
2003 using __bucket_type = __node_base*;
|
|
2004 using __bucket_alloc_type =
|
|
2005 __alloc_rebind<__node_alloc_type, __bucket_type>;
|
|
2006 using __bucket_alloc_traits = std::allocator_traits<__bucket_alloc_type>;
|
|
2007
|
|
2008 _Hashtable_alloc() = default;
|
|
2009 _Hashtable_alloc(const _Hashtable_alloc&) = default;
|
|
2010 _Hashtable_alloc(_Hashtable_alloc&&) = default;
|
|
2011
|
|
2012 template<typename _Alloc>
|
|
2013 _Hashtable_alloc(_Alloc&& __a)
|
|
2014 : __ebo_node_alloc(std::forward<_Alloc>(__a))
|
|
2015 { }
|
|
2016
|
|
2017 __node_alloc_type&
|
|
2018 _M_node_allocator()
|
|
2019 { return __ebo_node_alloc::_S_get(*this); }
|
|
2020
|
|
2021 const __node_alloc_type&
|
|
2022 _M_node_allocator() const
|
|
2023 { return __ebo_node_alloc::_S_cget(*this); }
|
|
2024
|
|
2025 template<typename... _Args>
|
|
2026 __node_type*
|
|
2027 _M_allocate_node(_Args&&... __args);
|
|
2028
|
|
2029 void
|
|
2030 _M_deallocate_node(__node_type* __n);
|
|
2031
|
|
2032 // Deallocate the linked list of nodes pointed to by __n
|
|
2033 void
|
|
2034 _M_deallocate_nodes(__node_type* __n);
|
|
2035
|
|
2036 __bucket_type*
|
|
2037 _M_allocate_buckets(std::size_t __n);
|
|
2038
|
|
2039 void
|
|
2040 _M_deallocate_buckets(__bucket_type*, std::size_t __n);
|
|
2041 };
|
|
2042
|
|
2043 // Definitions of class template _Hashtable_alloc's out-of-line member
|
|
2044 // functions.
|
|
2045 template<typename _NodeAlloc>
|
|
2046 template<typename... _Args>
|
|
2047 typename _Hashtable_alloc<_NodeAlloc>::__node_type*
|
|
2048 _Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&&... __args)
|
|
2049 {
|
|
2050 auto __nptr = __node_alloc_traits::allocate(_M_node_allocator(), 1);
|
|
2051 __node_type* __n = std::__to_address(__nptr);
|
|
2052 __try
|
|
2053 {
|
|
2054 ::new ((void*)__n) __node_type;
|
|
2055 __node_alloc_traits::construct(_M_node_allocator(),
|
|
2056 __n->_M_valptr(),
|
|
2057 std::forward<_Args>(__args)...);
|
|
2058 return __n;
|
|
2059 }
|
|
2060 __catch(...)
|
|
2061 {
|
|
2062 __node_alloc_traits::deallocate(_M_node_allocator(), __nptr, 1);
|
|
2063 __throw_exception_again;
|
|
2064 }
|
|
2065 }
|
|
2066
|
|
2067 template<typename _NodeAlloc>
|
|
2068 void
|
|
2069 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node(__node_type* __n)
|
|
2070 {
|
|
2071 typedef typename __node_alloc_traits::pointer _Ptr;
|
|
2072 auto __ptr = std::pointer_traits<_Ptr>::pointer_to(*__n);
|
|
2073 __node_alloc_traits::destroy(_M_node_allocator(), __n->_M_valptr());
|
|
2074 __n->~__node_type();
|
|
2075 __node_alloc_traits::deallocate(_M_node_allocator(), __ptr, 1);
|
|
2076 }
|
|
2077
|
|
2078 template<typename _NodeAlloc>
|
|
2079 void
|
|
2080 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_nodes(__node_type* __n)
|
|
2081 {
|
|
2082 while (__n)
|
|
2083 {
|
|
2084 __node_type* __tmp = __n;
|
|
2085 __n = __n->_M_next();
|
|
2086 _M_deallocate_node(__tmp);
|
|
2087 }
|
|
2088 }
|
|
2089
|
|
2090 template<typename _NodeAlloc>
|
|
2091 typename _Hashtable_alloc<_NodeAlloc>::__bucket_type*
|
|
2092 _Hashtable_alloc<_NodeAlloc>::_M_allocate_buckets(std::size_t __n)
|
|
2093 {
|
|
2094 __bucket_alloc_type __alloc(_M_node_allocator());
|
|
2095
|
|
2096 auto __ptr = __bucket_alloc_traits::allocate(__alloc, __n);
|
|
2097 __bucket_type* __p = std::__to_address(__ptr);
|
|
2098 __builtin_memset(__p, 0, __n * sizeof(__bucket_type));
|
|
2099 return __p;
|
|
2100 }
|
|
2101
|
|
2102 template<typename _NodeAlloc>
|
|
2103 void
|
|
2104 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_buckets(__bucket_type* __bkts,
|
|
2105 std::size_t __n)
|
|
2106 {
|
|
2107 typedef typename __bucket_alloc_traits::pointer _Ptr;
|
|
2108 auto __ptr = std::pointer_traits<_Ptr>::pointer_to(*__bkts);
|
|
2109 __bucket_alloc_type __alloc(_M_node_allocator());
|
|
2110 __bucket_alloc_traits::deallocate(__alloc, __ptr, __n);
|
|
2111 }
|
|
2112
|
|
2113 //@} hashtable-detail
|
|
2114 } // namespace __detail
|
|
2115 _GLIBCXX_END_NAMESPACE_VERSION
|
|
2116 } // namespace std
|
|
2117
|
|
2118 #endif // _HASHTABLE_POLICY_H
|