1 // TR1 hashtable.h header -*- C++ -*-
3 // Copyright (C) 2007-2023 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25 /** @file tr1/hashtable.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly.
28 * @headername{tr1/unordered_set, tr1/unordered_map}
31 #ifndef _GLIBCXX_TR1_HASHTABLE_H
32 #define _GLIBCXX_TR1_HASHTABLE_H 1
34 #pragma GCC system_header
36 #include <tr1/hashtable_policy.h>
37 #include <ext/alloc_traits.h>
39 namespace std
_GLIBCXX_VISIBILITY(default)
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 // Class template _Hashtable, class definition.
47 // Meaning of class template _Hashtable's template parameters
49 // _Key and _Value: arbitrary CopyConstructible types.
51 // _Allocator: an allocator type ([lib.allocator.requirements]) whose
52 // value type is Value. As a conforming extension, we allow for
53 // value type != Value.
55 // _ExtractKey: function object that takes a object of type Value
56 // and returns a value of type _Key.
58 // _Equal: function object that takes two objects of type k and returns
59 // a bool-like value that is true if the two objects are considered equal.
61 // _H1: the hash function. A unary function object with argument type
62 // Key and result type size_t. Return values should be distributed
63 // over the entire range [0, numeric_limits<size_t>:::max()].
65 // _H2: the range-hashing function (in the terminology of Tavori and
66 // Dreizin). A binary function object whose argument types and result
67 // type are all size_t. Given arguments r and N, the return value is
68 // in the range [0, N).
70 // _Hash: the ranged hash function (Tavori and Dreizin). A binary function
71 // whose argument types are _Key and size_t and whose result type is
72 // size_t. Given arguments k and N, the return value is in the range
73 // [0, N). Default: hash(k, N) = h2(h1(k), N). If _Hash is anything other
74 // than the default, _H1 and _H2 are ignored.
76 // _RehashPolicy: Policy class with three members, all of which govern
77 // the bucket count. _M_next_bkt(n) returns a bucket count no smaller
78 // than n. _M_bkt_for_elements(n) returns a bucket count appropriate
79 // for an element count of n. _M_need_rehash(n_bkt, n_elt, n_ins)
80 // determines whether, if the current bucket count is n_bkt and the
81 // current element count is n_elt, we need to increase the bucket
82 // count. If so, returns make_pair(true, n), where n is the new
83 // bucket count. If not, returns make_pair(false, <anything>).
85 // ??? Right now it is hard-wired that the number of buckets never
86 // shrinks. Should we allow _RehashPolicy to change that?
88 // __cache_hash_code: bool. true if we store the value of the hash
89 // function along with the value. This is a time-space tradeoff.
90 // Storing it may improve lookup speed by reducing the number of times
91 // we need to call the Equal function.
93 // __constant_iterators: bool. true if iterator and const_iterator are
94 // both constant iterator types. This is true for unordered_set and
95 // unordered_multiset, false for unordered_map and unordered_multimap.
97 // __unique_keys: bool. true if the return value of _Hashtable::count(k)
98 // is always at most one, false if it may be an arbitrary number. This
99 // true for unordered_set and unordered_map, false for unordered_multiset
100 // and unordered_multimap.
102 template<typename _Key
, typename _Value
, typename _Allocator
,
103 typename _ExtractKey
, typename _Equal
,
104 typename _H1
, typename _H2
, typename _Hash
,
105 typename _RehashPolicy
,
106 bool __cache_hash_code
,
107 bool __constant_iterators
,
110 : public __detail::_Rehash_base
<_RehashPolicy
,
111 _Hashtable
<_Key
, _Value
, _Allocator
,
113 _Equal
, _H1
, _H2
, _Hash
,
116 __constant_iterators
,
118 public __detail::_Hash_code_base
<_Key
, _Value
, _ExtractKey
, _Equal
,
119 _H1
, _H2
, _Hash
, __cache_hash_code
>,
120 public __detail::_Map_base
<_Key
, _Value
, _ExtractKey
, __unique_keys
,
121 _Hashtable
<_Key
, _Value
, _Allocator
,
123 _Equal
, _H1
, _H2
, _Hash
,
126 __constant_iterators
,
129 typedef __gnu_cxx::__alloc_traits
<_Allocator
> _Alloc_traits
;
132 typedef _Allocator allocator_type
;
133 typedef _Value value_type
;
134 typedef _Key key_type
;
135 typedef _Equal key_equal
;
136 // mapped_type, if present, comes from _Map_base.
137 // hasher, if present, comes from _Hash_code_base.
138 typedef typename
_Allocator::difference_type difference_type
;
139 typedef typename
_Allocator::size_type size_type
;
140 typedef typename
_Alloc_traits::pointer pointer
;
141 typedef typename
_Alloc_traits::const_pointer const_pointer
;
142 typedef typename
_Alloc_traits::reference reference
;
143 typedef typename
_Alloc_traits::const_reference const_reference
;
145 typedef __detail::_Node_iterator
<value_type
, __constant_iterators
,
148 typedef __detail::_Node_const_iterator
<value_type
,
149 __constant_iterators
,
151 const_local_iterator
;
153 typedef __detail::_Hashtable_iterator
<value_type
, __constant_iterators
,
156 typedef __detail::_Hashtable_const_iterator
<value_type
,
157 __constant_iterators
,
161 template<typename _Key2
, typename _Value2
, typename _Ex2
, bool __unique2
,
162 typename _Hashtable2
>
163 friend struct __detail::_Map_base
;
166 typedef __detail::_Hash_node
<_Value
, __cache_hash_code
> _Node
;
167 typedef typename
_Alloc_traits::template rebind
<_Node
>::other
168 _Node_allocator_type
;
169 typedef typename
_Alloc_traits::template rebind
<_Node
*>::other
170 _Bucket_allocator_type
;
172 typedef typename
_Alloc_traits::template rebind
<_Value
>::other
173 _Value_allocator_type
;
175 _Node_allocator_type _M_node_allocator
;
177 size_type _M_bucket_count
;
178 size_type _M_element_count
;
179 _RehashPolicy _M_rehash_policy
;
182 _M_allocate_node(const value_type
& __v
);
185 _M_deallocate_node(_Node
* __n
);
188 _M_deallocate_nodes(_Node
**, size_type
);
191 _M_allocate_buckets(size_type __n
);
194 _M_deallocate_buckets(_Node
**, size_type __n
);
197 // Constructor, destructor, assignment, swap
198 _Hashtable(size_type __bucket_hint
,
199 const _H1
&, const _H2
&, const _Hash
&,
200 const _Equal
&, const _ExtractKey
&,
201 const allocator_type
&);
203 template<typename _InputIterator
>
204 _Hashtable(_InputIterator __first
, _InputIterator __last
,
205 size_type __bucket_hint
,
206 const _H1
&, const _H2
&, const _Hash
&,
207 const _Equal
&, const _ExtractKey
&,
208 const allocator_type
&);
210 _Hashtable(const _Hashtable
&);
213 operator=(const _Hashtable
&);
217 void swap(_Hashtable
&);
219 // Basic container operations
223 iterator
__i(_M_buckets
);
224 if (!__i
._M_cur_node
)
225 __i
._M_incr_bucket();
232 const_iterator
__i(_M_buckets
);
233 if (!__i
._M_cur_node
)
234 __i
._M_incr_bucket();
240 { return iterator(_M_buckets
+ _M_bucket_count
); }
244 { return const_iterator(_M_buckets
+ _M_bucket_count
); }
248 { return _M_element_count
; }
250 _GLIBCXX_NODISCARD
bool
252 { return size() == 0; }
255 get_allocator() const
256 { return allocator_type(_M_node_allocator
); }
258 _Value_allocator_type
259 _M_get_Value_allocator() const
260 { return _Value_allocator_type(_M_node_allocator
); }
265 typedef __gnu_cxx::__alloc_traits
<_Node_allocator_type
> _Traits
;
266 return _Traits::max_size(_M_node_allocator
);
272 { return this->_M_eq
; }
274 // hash_function, if present, comes from _Hash_code_base.
279 { return _M_bucket_count
; }
282 max_bucket_count() const
283 { return max_size(); }
286 bucket_size(size_type __n
) const
287 { return std::distance(begin(__n
), end(__n
)); }
290 bucket(const key_type
& __k
) const
292 return this->_M_bucket_index(__k
, this->_M_hash_code(__k
),
298 { return local_iterator(_M_buckets
[__n
]); }
302 { return local_iterator(0); }
305 begin(size_type __n
) const
306 { return const_local_iterator(_M_buckets
[__n
]); }
310 { return const_local_iterator(0); }
315 return static_cast<float>(size()) / static_cast<float>(bucket_count());
318 // max_load_factor, if present, comes from _Rehash_base.
320 // Generalization of max_load_factor. Extension, not found in TR1. Only
321 // useful if _RehashPolicy is something other than the default.
323 __rehash_policy() const
324 { return _M_rehash_policy
; }
327 __rehash_policy(const _RehashPolicy
&);
331 find(const key_type
& __k
);
334 find(const key_type
& __k
) const;
337 count(const key_type
& __k
) const;
339 std::pair
<iterator
, iterator
>
340 equal_range(const key_type
& __k
);
342 std::pair
<const_iterator
, const_iterator
>
343 equal_range(const key_type
& __k
) const;
345 private: // Find, insert and erase helper functions
346 // ??? This dispatching is a workaround for the fact that we don't
347 // have partial specialization of member templates; it would be
348 // better to just specialize insert on __unique_keys. There may be a
349 // cleaner workaround.
350 typedef typename
__gnu_cxx::__conditional_type
<__unique_keys
,
351 std::pair
<iterator
, bool>, iterator
>::__type
354 typedef typename
__gnu_cxx::__conditional_type
<__unique_keys
,
355 std::_Select1st
<_Insert_Return_Type
>,
356 std::_Identity
<_Insert_Return_Type
>
361 _M_find_node(_Node
*, const key_type
&,
362 typename
_Hashtable::_Hash_code_type
) const;
365 _M_insert_bucket(const value_type
&, size_type
,
366 typename
_Hashtable::_Hash_code_type
);
368 std::pair
<iterator
, bool>
369 _M_insert(const value_type
&, std::tr1::true_type
);
372 _M_insert(const value_type
&, std::tr1::false_type
);
375 _M_erase_node(_Node
*, _Node
**);
380 insert(const value_type
& __v
)
381 { return _M_insert(__v
, std::tr1::integral_constant
<bool,
385 insert(iterator
, const value_type
& __v
)
386 { return iterator(_Insert_Conv_Type()(this->insert(__v
))); }
389 insert(const_iterator
, const value_type
& __v
)
390 { return const_iterator(_Insert_Conv_Type()(this->insert(__v
))); }
392 template<typename _InputIterator
>
394 insert(_InputIterator __first
, _InputIterator __last
);
400 erase(const_iterator
);
403 erase(const key_type
&);
406 erase(iterator
, iterator
);
409 erase(const_iterator
, const_iterator
);
414 // Set number of buckets to be appropriate for container of n element.
415 void rehash(size_type __n
);
418 // Unconditionally change size of bucket array to n.
419 void _M_rehash(size_type __n
);
423 // Definitions of class template _Hashtable's out-of-line member functions.
424 template<typename _Key
, typename _Value
,
425 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
426 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
427 bool __chc
, bool __cit
, bool __uk
>
428 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
429 _H1
, _H2
, _Hash
, _RehashPolicy
,
430 __chc
, __cit
, __uk
>::_Node
*
431 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
432 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
433 _M_allocate_node(const value_type
& __v
)
435 _Node
* __n
= _M_node_allocator
.allocate(1);
438 _Value_allocator_type __a
= _M_get_Value_allocator();
439 typedef __gnu_cxx::__alloc_traits
<_Value_allocator_type
> _Traits
;
440 _Traits::construct(__a
, &__n
->_M_v
, __v
);
446 _M_node_allocator
.deallocate(__n
, 1);
447 __throw_exception_again
;
451 template<typename _Key
, typename _Value
,
452 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
453 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
454 bool __chc
, bool __cit
, bool __uk
>
456 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
457 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
458 _M_deallocate_node(_Node
* __n
)
460 _Value_allocator_type __a
= _M_get_Value_allocator();
461 typedef __gnu_cxx::__alloc_traits
<_Value_allocator_type
> _Traits
;
462 _Traits::destroy(__a
, &__n
->_M_v
);
463 _M_node_allocator
.deallocate(__n
, 1);
466 template<typename _Key
, typename _Value
,
467 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
468 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
469 bool __chc
, bool __cit
, bool __uk
>
471 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
472 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
473 _M_deallocate_nodes(_Node
** __array
, size_type __n
)
475 for (size_type __i
= 0; __i
< __n
; ++__i
)
477 _Node
* __p
= __array
[__i
];
482 _M_deallocate_node(__tmp
);
488 template<typename _Key
, typename _Value
,
489 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
490 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
491 bool __chc
, bool __cit
, bool __uk
>
492 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
493 _H1
, _H2
, _Hash
, _RehashPolicy
,
494 __chc
, __cit
, __uk
>::_Node
**
495 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
496 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
497 _M_allocate_buckets(size_type __n
)
499 _Bucket_allocator_type
__alloc(_M_node_allocator
);
501 // We allocate one extra bucket to hold a sentinel, an arbitrary
502 // non-null pointer. Iterator increment relies on this.
503 _Node
** __p
= __alloc
.allocate(__n
+ 1);
504 std::fill(__p
, __p
+ __n
, (_Node
*) 0);
505 __p
[__n
] = reinterpret_cast<_Node
*>(0x1000);
509 template<typename _Key
, typename _Value
,
510 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
511 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
512 bool __chc
, bool __cit
, bool __uk
>
514 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
515 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
516 _M_deallocate_buckets(_Node
** __p
, size_type __n
)
518 _Bucket_allocator_type
__alloc(_M_node_allocator
);
519 __alloc
.deallocate(__p
, __n
+ 1);
522 template<typename _Key
, typename _Value
,
523 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
524 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
525 bool __chc
, bool __cit
, bool __uk
>
526 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
527 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
528 _Hashtable(size_type __bucket_hint
,
529 const _H1
& __h1
, const _H2
& __h2
, const _Hash
& __h
,
530 const _Equal
& __eq
, const _ExtractKey
& __exk
,
531 const allocator_type
& __a
)
532 : __detail::_Rehash_base
<_RehashPolicy
, _Hashtable
>(),
533 __detail::_Hash_code_base
<_Key
, _Value
, _ExtractKey
, _Equal
,
534 _H1
, _H2
, _Hash
, __chc
>(__exk
, __eq
,
536 __detail::_Map_base
<_Key
, _Value
, _ExtractKey
, __uk
, _Hashtable
>(),
537 _M_node_allocator(__a
),
542 _M_bucket_count
= _M_rehash_policy
._M_next_bkt(__bucket_hint
);
543 _M_buckets
= _M_allocate_buckets(_M_bucket_count
);
546 template<typename _Key
, typename _Value
,
547 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
548 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
549 bool __chc
, bool __cit
, bool __uk
>
550 template<typename _InputIterator
>
551 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
552 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
553 _Hashtable(_InputIterator __f
, _InputIterator __l
,
554 size_type __bucket_hint
,
555 const _H1
& __h1
, const _H2
& __h2
, const _Hash
& __h
,
556 const _Equal
& __eq
, const _ExtractKey
& __exk
,
557 const allocator_type
& __a
)
558 : __detail::_Rehash_base
<_RehashPolicy
, _Hashtable
>(),
559 __detail::_Hash_code_base
<_Key
, _Value
, _ExtractKey
, _Equal
,
560 _H1
, _H2
, _Hash
, __chc
>(__exk
, __eq
,
562 __detail::_Map_base
<_Key
, _Value
, _ExtractKey
, __uk
, _Hashtable
>(),
563 _M_node_allocator(__a
),
568 _M_bucket_count
= std::max(_M_rehash_policy
._M_next_bkt(__bucket_hint
),
570 _M_bkt_for_elements(__detail::
573 _M_buckets
= _M_allocate_buckets(_M_bucket_count
);
576 for (; __f
!= __l
; ++__f
)
582 _M_deallocate_buckets(_M_buckets
, _M_bucket_count
);
583 __throw_exception_again
;
587 template<typename _Key
, typename _Value
,
588 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
589 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
590 bool __chc
, bool __cit
, bool __uk
>
591 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
592 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
593 _Hashtable(const _Hashtable
& __ht
)
594 : __detail::_Rehash_base
<_RehashPolicy
, _Hashtable
>(__ht
),
595 __detail::_Hash_code_base
<_Key
, _Value
, _ExtractKey
, _Equal
,
596 _H1
, _H2
, _Hash
, __chc
>(__ht
),
597 __detail::_Map_base
<_Key
, _Value
, _ExtractKey
, __uk
, _Hashtable
>(__ht
),
598 _M_node_allocator(__ht
._M_node_allocator
),
599 _M_bucket_count(__ht
._M_bucket_count
),
600 _M_element_count(__ht
._M_element_count
),
601 _M_rehash_policy(__ht
._M_rehash_policy
)
603 _M_buckets
= _M_allocate_buckets(_M_bucket_count
);
606 for (size_type __i
= 0; __i
< __ht
._M_bucket_count
; ++__i
)
608 _Node
* __n
= __ht
._M_buckets
[__i
];
609 _Node
** __tail
= _M_buckets
+ __i
;
612 *__tail
= _M_allocate_node(__n
->_M_v
);
613 this->_M_copy_code(*__tail
, __n
);
614 __tail
= &((*__tail
)->_M_next
);
622 _M_deallocate_buckets(_M_buckets
, _M_bucket_count
);
623 __throw_exception_again
;
627 template<typename _Key
, typename _Value
,
628 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
629 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
630 bool __chc
, bool __cit
, bool __uk
>
631 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
632 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>&
633 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
634 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
635 operator=(const _Hashtable
& __ht
)
637 _Hashtable
__tmp(__ht
);
642 template<typename _Key
, typename _Value
,
643 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
644 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
645 bool __chc
, bool __cit
, bool __uk
>
646 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
647 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
651 _M_deallocate_buckets(_M_buckets
, _M_bucket_count
);
654 template<typename _Key
, typename _Value
,
655 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
656 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
657 bool __chc
, bool __cit
, bool __uk
>
659 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
660 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
661 swap(_Hashtable
& __x
)
663 // The only base class with member variables is hash_code_base. We
664 // define _Hash_code_base::_M_swap because different specializations
665 // have different members.
666 __detail::_Hash_code_base
<_Key
, _Value
, _ExtractKey
, _Equal
,
667 _H1
, _H2
, _Hash
, __chc
>::_M_swap(__x
);
669 // _GLIBCXX_RESOLVE_LIB_DEFECTS
670 // 431. Swapping containers with unequal allocators.
671 std::__alloc_swap
<_Node_allocator_type
>::_S_do_it(_M_node_allocator
,
672 __x
._M_node_allocator
);
674 std::swap(_M_rehash_policy
, __x
._M_rehash_policy
);
675 std::swap(_M_buckets
, __x
._M_buckets
);
676 std::swap(_M_bucket_count
, __x
._M_bucket_count
);
677 std::swap(_M_element_count
, __x
._M_element_count
);
680 template<typename _Key
, typename _Value
,
681 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
682 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
683 bool __chc
, bool __cit
, bool __uk
>
685 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
686 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
687 __rehash_policy(const _RehashPolicy
& __pol
)
689 _M_rehash_policy
= __pol
;
690 size_type __n_bkt
= __pol
._M_bkt_for_elements(_M_element_count
);
691 if (__n_bkt
> _M_bucket_count
)
695 template<typename _Key
, typename _Value
,
696 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
697 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
698 bool __chc
, bool __cit
, bool __uk
>
699 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
700 _H1
, _H2
, _Hash
, _RehashPolicy
,
701 __chc
, __cit
, __uk
>::iterator
702 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
703 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
704 find(const key_type
& __k
)
706 typename
_Hashtable::_Hash_code_type __code
= this->_M_hash_code(__k
);
707 std::size_t __n
= this->_M_bucket_index(__k
, __code
, _M_bucket_count
);
708 _Node
* __p
= _M_find_node(_M_buckets
[__n
], __k
, __code
);
709 return __p
? iterator(__p
, _M_buckets
+ __n
) : this->end();
712 template<typename _Key
, typename _Value
,
713 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
714 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
715 bool __chc
, bool __cit
, bool __uk
>
716 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
717 _H1
, _H2
, _Hash
, _RehashPolicy
,
718 __chc
, __cit
, __uk
>::const_iterator
719 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
720 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
721 find(const key_type
& __k
) const
723 typename
_Hashtable::_Hash_code_type __code
= this->_M_hash_code(__k
);
724 std::size_t __n
= this->_M_bucket_index(__k
, __code
, _M_bucket_count
);
725 _Node
* __p
= _M_find_node(_M_buckets
[__n
], __k
, __code
);
726 return __p
? const_iterator(__p
, _M_buckets
+ __n
) : this->end();
729 template<typename _Key
, typename _Value
,
730 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
731 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
732 bool __chc
, bool __cit
, bool __uk
>
733 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
734 _H1
, _H2
, _Hash
, _RehashPolicy
,
735 __chc
, __cit
, __uk
>::size_type
736 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
737 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
738 count(const key_type
& __k
) const
740 typename
_Hashtable::_Hash_code_type __code
= this->_M_hash_code(__k
);
741 std::size_t __n
= this->_M_bucket_index(__k
, __code
, _M_bucket_count
);
742 std::size_t __result
= 0;
743 for (_Node
* __p
= _M_buckets
[__n
]; __p
; __p
= __p
->_M_next
)
744 if (this->_M_compare(__k
, __code
, __p
))
749 template<typename _Key
, typename _Value
,
750 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
751 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
752 bool __chc
, bool __cit
, bool __uk
>
753 std::pair
<typename _Hashtable
<_Key
, _Value
, _Allocator
,
754 _ExtractKey
, _Equal
, _H1
,
755 _H2
, _Hash
, _RehashPolicy
,
756 __chc
, __cit
, __uk
>::iterator
,
757 typename _Hashtable
<_Key
, _Value
, _Allocator
,
758 _ExtractKey
, _Equal
, _H1
,
759 _H2
, _Hash
, _RehashPolicy
,
760 __chc
, __cit
, __uk
>::iterator
>
761 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
762 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
763 equal_range(const key_type
& __k
)
765 typename
_Hashtable::_Hash_code_type __code
= this->_M_hash_code(__k
);
766 std::size_t __n
= this->_M_bucket_index(__k
, __code
, _M_bucket_count
);
767 _Node
** __head
= _M_buckets
+ __n
;
768 _Node
* __p
= _M_find_node(*__head
, __k
, __code
);
772 _Node
* __p1
= __p
->_M_next
;
773 for (; __p1
; __p1
= __p1
->_M_next
)
774 if (!this->_M_compare(__k
, __code
, __p1
))
777 iterator
__first(__p
, __head
);
778 iterator
__last(__p1
, __head
);
780 __last
._M_incr_bucket();
781 return std::make_pair(__first
, __last
);
784 return std::make_pair(this->end(), this->end());
787 template<typename _Key
, typename _Value
,
788 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
789 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
790 bool __chc
, bool __cit
, bool __uk
>
791 std::pair
<typename _Hashtable
<_Key
, _Value
, _Allocator
,
792 _ExtractKey
, _Equal
, _H1
,
793 _H2
, _Hash
, _RehashPolicy
,
794 __chc
, __cit
, __uk
>::const_iterator
,
795 typename _Hashtable
<_Key
, _Value
, _Allocator
,
796 _ExtractKey
, _Equal
, _H1
,
797 _H2
, _Hash
, _RehashPolicy
,
798 __chc
, __cit
, __uk
>::const_iterator
>
799 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
800 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
801 equal_range(const key_type
& __k
) const
803 typename
_Hashtable::_Hash_code_type __code
= this->_M_hash_code(__k
);
804 std::size_t __n
= this->_M_bucket_index(__k
, __code
, _M_bucket_count
);
805 _Node
** __head
= _M_buckets
+ __n
;
806 _Node
* __p
= _M_find_node(*__head
, __k
, __code
);
810 _Node
* __p1
= __p
->_M_next
;
811 for (; __p1
; __p1
= __p1
->_M_next
)
812 if (!this->_M_compare(__k
, __code
, __p1
))
815 const_iterator
__first(__p
, __head
);
816 const_iterator
__last(__p1
, __head
);
818 __last
._M_incr_bucket();
819 return std::make_pair(__first
, __last
);
822 return std::make_pair(this->end(), this->end());
825 // Find the node whose key compares equal to k, beginning the search
826 // at p (usually the head of a bucket). Return zero if no node is found.
827 template<typename _Key
, typename _Value
,
828 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
829 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
830 bool __chc
, bool __cit
, bool __uk
>
831 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
,
832 _Equal
, _H1
, _H2
, _Hash
, _RehashPolicy
,
833 __chc
, __cit
, __uk
>::_Node
*
834 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
835 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
836 _M_find_node(_Node
* __p
, const key_type
& __k
,
837 typename
_Hashtable::_Hash_code_type __code
) const
839 for (; __p
; __p
= __p
->_M_next
)
840 if (this->_M_compare(__k
, __code
, __p
))
845 // Insert v in bucket n (assumes no element with its key already present).
846 template<typename _Key
, typename _Value
,
847 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
848 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
849 bool __chc
, bool __cit
, bool __uk
>
850 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
851 _H1
, _H2
, _Hash
, _RehashPolicy
,
852 __chc
, __cit
, __uk
>::iterator
853 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
854 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
855 _M_insert_bucket(const value_type
& __v
, size_type __n
,
856 typename
_Hashtable::_Hash_code_type __code
)
858 std::pair
<bool, std::size_t> __do_rehash
859 = _M_rehash_policy
._M_need_rehash(_M_bucket_count
,
860 _M_element_count
, 1);
862 // Allocate the new node before doing the rehash so that we don't
863 // do a rehash if the allocation throws.
864 _Node
* __new_node
= _M_allocate_node(__v
);
868 if (__do_rehash
.first
)
870 const key_type
& __k
= this->_M_extract(__v
);
871 __n
= this->_M_bucket_index(__k
, __code
, __do_rehash
.second
);
872 _M_rehash(__do_rehash
.second
);
875 __new_node
->_M_next
= _M_buckets
[__n
];
876 this->_M_store_code(__new_node
, __code
);
877 _M_buckets
[__n
] = __new_node
;
879 return iterator(__new_node
, _M_buckets
+ __n
);
883 _M_deallocate_node(__new_node
);
884 __throw_exception_again
;
888 // Insert v if no element with its key is already present.
889 template<typename _Key
, typename _Value
,
890 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
891 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
892 bool __chc
, bool __cit
, bool __uk
>
893 std::pair
<typename _Hashtable
<_Key
, _Value
, _Allocator
,
894 _ExtractKey
, _Equal
, _H1
,
895 _H2
, _Hash
, _RehashPolicy
,
896 __chc
, __cit
, __uk
>::iterator
, bool>
897 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
898 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
899 _M_insert(const value_type
& __v
, std::tr1::true_type
)
901 const key_type
& __k
= this->_M_extract(__v
);
902 typename
_Hashtable::_Hash_code_type __code
= this->_M_hash_code(__k
);
903 size_type __n
= this->_M_bucket_index(__k
, __code
, _M_bucket_count
);
905 if (_Node
* __p
= _M_find_node(_M_buckets
[__n
], __k
, __code
))
906 return std::make_pair(iterator(__p
, _M_buckets
+ __n
), false);
907 return std::make_pair(_M_insert_bucket(__v
, __n
, __code
), true);
910 // Insert v unconditionally.
911 template<typename _Key
, typename _Value
,
912 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
913 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
914 bool __chc
, bool __cit
, bool __uk
>
915 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
916 _H1
, _H2
, _Hash
, _RehashPolicy
,
917 __chc
, __cit
, __uk
>::iterator
918 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
919 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
920 _M_insert(const value_type
& __v
, std::tr1::false_type
)
922 std::pair
<bool, std::size_t> __do_rehash
923 = _M_rehash_policy
._M_need_rehash(_M_bucket_count
,
924 _M_element_count
, 1);
925 if (__do_rehash
.first
)
926 _M_rehash(__do_rehash
.second
);
928 const key_type
& __k
= this->_M_extract(__v
);
929 typename
_Hashtable::_Hash_code_type __code
= this->_M_hash_code(__k
);
930 size_type __n
= this->_M_bucket_index(__k
, __code
, _M_bucket_count
);
932 // First find the node, avoid leaking new_node if compare throws.
933 _Node
* __prev
= _M_find_node(_M_buckets
[__n
], __k
, __code
);
934 _Node
* __new_node
= _M_allocate_node(__v
);
938 __new_node
->_M_next
= __prev
->_M_next
;
939 __prev
->_M_next
= __new_node
;
943 __new_node
->_M_next
= _M_buckets
[__n
];
944 _M_buckets
[__n
] = __new_node
;
946 this->_M_store_code(__new_node
, __code
);
949 return iterator(__new_node
, _M_buckets
+ __n
);
952 // For erase(iterator) and erase(const_iterator).
953 template<typename _Key
, typename _Value
,
954 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
955 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
956 bool __chc
, bool __cit
, bool __uk
>
958 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
959 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
960 _M_erase_node(_Node
* __p
, _Node
** __b
)
964 *__b
= __cur
->_M_next
;
967 _Node
* __next
= __cur
->_M_next
;
968 while (__next
!= __p
)
971 __next
= __cur
->_M_next
;
973 __cur
->_M_next
= __next
->_M_next
;
976 _M_deallocate_node(__p
);
980 template<typename _Key
, typename _Value
,
981 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
982 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
983 bool __chc
, bool __cit
, bool __uk
>
984 template<typename _InputIterator
>
986 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
987 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
988 insert(_InputIterator __first
, _InputIterator __last
)
990 size_type __n_elt
= __detail::__distance_fw(__first
, __last
);
991 std::pair
<bool, std::size_t> __do_rehash
992 = _M_rehash_policy
._M_need_rehash(_M_bucket_count
,
993 _M_element_count
, __n_elt
);
994 if (__do_rehash
.first
)
995 _M_rehash(__do_rehash
.second
);
997 for (; __first
!= __last
; ++__first
)
998 this->insert(*__first
);
1001 template<typename _Key
, typename _Value
,
1002 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
1003 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1004 bool __chc
, bool __cit
, bool __uk
>
1005 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1006 _H1
, _H2
, _Hash
, _RehashPolicy
,
1007 __chc
, __cit
, __uk
>::iterator
1008 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1009 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
1010 erase(iterator __it
)
1012 iterator __result
= __it
;
1014 _M_erase_node(__it
._M_cur_node
, __it
._M_cur_bucket
);
1018 template<typename _Key
, typename _Value
,
1019 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
1020 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1021 bool __chc
, bool __cit
, bool __uk
>
1022 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1023 _H1
, _H2
, _Hash
, _RehashPolicy
,
1024 __chc
, __cit
, __uk
>::const_iterator
1025 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1026 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
1027 erase(const_iterator __it
)
1029 const_iterator __result
= __it
;
1031 _M_erase_node(__it
._M_cur_node
, __it
._M_cur_bucket
);
1035 template<typename _Key
, typename _Value
,
1036 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
1037 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1038 bool __chc
, bool __cit
, bool __uk
>
1039 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1040 _H1
, _H2
, _Hash
, _RehashPolicy
,
1041 __chc
, __cit
, __uk
>::size_type
1042 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1043 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
1044 erase(const key_type
& __k
)
1046 typename
_Hashtable::_Hash_code_type __code
= this->_M_hash_code(__k
);
1047 std::size_t __n
= this->_M_bucket_index(__k
, __code
, _M_bucket_count
);
1048 size_type __result
= 0;
1050 _Node
** __slot
= _M_buckets
+ __n
;
1051 while (*__slot
&& !this->_M_compare(__k
, __code
, *__slot
))
1052 __slot
= &((*__slot
)->_M_next
);
1054 _Node
** __saved_slot
= 0;
1055 while (*__slot
&& this->_M_compare(__k
, __code
, *__slot
))
1057 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1058 // 526. Is it undefined if a function in the standard changes
1060 if (&this->_M_extract((*__slot
)->_M_v
) != &__k
)
1062 _Node
* __p
= *__slot
;
1063 *__slot
= __p
->_M_next
;
1064 _M_deallocate_node(__p
);
1070 __saved_slot
= __slot
;
1071 __slot
= &((*__slot
)->_M_next
);
1077 _Node
* __p
= *__saved_slot
;
1078 *__saved_slot
= __p
->_M_next
;
1079 _M_deallocate_node(__p
);
1087 // ??? This could be optimized by taking advantage of the bucket
1088 // structure, but it's not clear that it's worth doing. It probably
1089 // wouldn't even be an optimization unless the load factor is large.
1090 template<typename _Key
, typename _Value
,
1091 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
1092 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1093 bool __chc
, bool __cit
, bool __uk
>
1094 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1095 _H1
, _H2
, _Hash
, _RehashPolicy
,
1096 __chc
, __cit
, __uk
>::iterator
1097 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1098 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
1099 erase(iterator __first
, iterator __last
)
1101 while (__first
!= __last
)
1102 __first
= this->erase(__first
);
1106 template<typename _Key
, typename _Value
,
1107 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
1108 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1109 bool __chc
, bool __cit
, bool __uk
>
1110 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1111 _H1
, _H2
, _Hash
, _RehashPolicy
,
1112 __chc
, __cit
, __uk
>::const_iterator
1113 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1114 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
1115 erase(const_iterator __first
, const_iterator __last
)
1117 while (__first
!= __last
)
1118 __first
= this->erase(__first
);
1122 template<typename _Key
, typename _Value
,
1123 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
1124 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1125 bool __chc
, bool __cit
, bool __uk
>
1127 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1128 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
1131 _M_deallocate_nodes(_M_buckets
, _M_bucket_count
);
1132 _M_element_count
= 0;
1135 template<typename _Key
, typename _Value
,
1136 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
1137 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1138 bool __chc
, bool __cit
, bool __uk
>
1140 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1141 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
1142 rehash(size_type __n
)
1144 _M_rehash(std::max(_M_rehash_policy
._M_next_bkt(__n
),
1145 _M_rehash_policy
._M_bkt_for_elements(_M_element_count
1149 template<typename _Key
, typename _Value
,
1150 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
1151 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1152 bool __chc
, bool __cit
, bool __uk
>
1154 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1155 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
1156 _M_rehash(size_type __n
)
1158 _Node
** __new_array
= _M_allocate_buckets(__n
);
1161 for (size_type __i
= 0; __i
< _M_bucket_count
; ++__i
)
1162 while (_Node
* __p
= _M_buckets
[__i
])
1164 std::size_t __new_index
= this->_M_bucket_index(__p
, __n
);
1165 _M_buckets
[__i
] = __p
->_M_next
;
1166 __p
->_M_next
= __new_array
[__new_index
];
1167 __new_array
[__new_index
] = __p
;
1169 _M_deallocate_buckets(_M_buckets
, _M_bucket_count
);
1170 _M_bucket_count
= __n
;
1171 _M_buckets
= __new_array
;
1175 // A failure here means that a hash function threw an exception.
1176 // We can't restore the previous state without calling the hash
1177 // function again, so the only sensible recovery is to delete
1179 _M_deallocate_nodes(__new_array
, __n
);
1180 _M_deallocate_buckets(__new_array
, __n
);
1181 _M_deallocate_nodes(_M_buckets
, _M_bucket_count
);
1182 _M_element_count
= 0;
1183 __throw_exception_again
;
1188 _GLIBCXX_END_NAMESPACE_VERSION
1191 #endif // _GLIBCXX_TR1_HASHTABLE_H