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