1 // hashtable.h header -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009, 2010 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 bits/hashtable.h
26 * This is an internal header file, included by other library headers.
27 * You should not attempt to use it directly.
31 #define _HASHTABLE_H 1
33 #pragma GCC system_header
35 #include <bits/hashtable_policy.h>
39 // Class template _Hashtable, class definition.
41 // Meaning of class template _Hashtable's template parameters
43 // _Key and _Value: arbitrary CopyConstructible types.
45 // _Allocator: an allocator type ([lib.allocator.requirements]) whose
46 // value type is Value. As a conforming extension, we allow for
47 // value type != Value.
49 // _ExtractKey: function object that takes a object of type Value
50 // and returns a value of type _Key.
52 // _Equal: function object that takes two objects of type k and returns
53 // a bool-like value that is true if the two objects are considered equal.
55 // _H1: the hash function. A unary function object with argument type
56 // Key and result type size_t. Return values should be distributed
57 // over the entire range [0, numeric_limits<size_t>:::max()].
59 // _H2: the range-hashing function (in the terminology of Tavori and
60 // Dreizin). A binary function object whose argument types and result
61 // type are all size_t. Given arguments r and N, the return value is
62 // in the range [0, N).
64 // _Hash: the ranged hash function (Tavori and Dreizin). A binary function
65 // whose argument types are _Key and size_t and whose result type is
66 // size_t. Given arguments k and N, the return value is in the range
67 // [0, N). Default: hash(k, N) = h2(h1(k), N). If _Hash is anything other
68 // than the default, _H1 and _H2 are ignored.
70 // _RehashPolicy: Policy class with three members, all of which govern
71 // the bucket count. _M_next_bkt(n) returns a bucket count no smaller
72 // than n. _M_bkt_for_elements(n) returns a bucket count appropriate
73 // for an element count of n. _M_need_rehash(n_bkt, n_elt, n_ins)
74 // determines whether, if the current bucket count is n_bkt and the
75 // current element count is n_elt, we need to increase the bucket
76 // count. If so, returns make_pair(true, n), where n is the new
77 // bucket count. If not, returns make_pair(false, <anything>).
79 // ??? Right now it is hard-wired that the number of buckets never
80 // shrinks. Should we allow _RehashPolicy to change that?
82 // __cache_hash_code: bool. true if we store the value of the hash
83 // function along with the value. This is a time-space tradeoff.
84 // Storing it may improve lookup speed by reducing the number of times
85 // we need to call the Equal function.
87 // __constant_iterators: bool. true if iterator and const_iterator are
88 // both constant iterator types. This is true for unordered_set and
89 // unordered_multiset, false for unordered_map and unordered_multimap.
91 // __unique_keys: bool. true if the return value of _Hashtable::count(k)
92 // is always at most one, false if it may be an arbitrary number. This
93 // true for unordered_set and unordered_map, false for unordered_multiset
94 // and unordered_multimap.
96 template<typename _Key
, typename _Value
, typename _Allocator
,
97 typename _ExtractKey
, typename _Equal
,
98 typename _H1
, typename _H2
, typename _Hash
,
99 typename _RehashPolicy
,
100 bool __cache_hash_code
,
101 bool __constant_iterators
,
104 : public __detail::_Rehash_base
<_RehashPolicy
,
105 _Hashtable
<_Key
, _Value
, _Allocator
,
107 _Equal
, _H1
, _H2
, _Hash
,
110 __constant_iterators
,
112 public __detail::_Hash_code_base
<_Key
, _Value
, _ExtractKey
, _Equal
,
113 _H1
, _H2
, _Hash
, __cache_hash_code
>,
114 public __detail::_Map_base
<_Key
, _Value
, _ExtractKey
, __unique_keys
,
115 _Hashtable
<_Key
, _Value
, _Allocator
,
117 _Equal
, _H1
, _H2
, _Hash
,
120 __constant_iterators
,
124 typedef _Allocator allocator_type
;
125 typedef _Value value_type
;
126 typedef _Key key_type
;
127 typedef _Equal key_equal
;
128 // mapped_type, if present, comes from _Map_base.
129 // hasher, if present, comes from _Hash_code_base.
130 typedef typename
_Allocator::difference_type difference_type
;
131 typedef typename
_Allocator::size_type size_type
;
132 typedef typename
_Allocator::pointer pointer
;
133 typedef typename
_Allocator::const_pointer const_pointer
;
134 typedef typename
_Allocator::reference reference
;
135 typedef typename
_Allocator::const_reference const_reference
;
137 typedef __detail::_Node_iterator
<value_type
, __constant_iterators
,
140 typedef __detail::_Node_const_iterator
<value_type
,
141 __constant_iterators
,
143 const_local_iterator
;
145 typedef __detail::_Hashtable_iterator
<value_type
, __constant_iterators
,
148 typedef __detail::_Hashtable_const_iterator
<value_type
,
149 __constant_iterators
,
153 template<typename _Key2
, typename _Value2
, typename _Ex2
, bool __unique2
,
154 typename _Hashtable2
>
155 friend struct __detail::_Map_base
;
158 typedef __detail::_Hash_node
<_Value
, __cache_hash_code
> _Node
;
159 typedef typename
_Allocator::template rebind
<_Node
>::other
160 _Node_allocator_type
;
161 typedef typename
_Allocator::template rebind
<_Node
*>::other
162 _Bucket_allocator_type
;
164 typedef typename
_Allocator::template rebind
<_Value
>::other
165 _Value_allocator_type
;
167 _Node_allocator_type _M_node_allocator
;
169 size_type _M_bucket_count
;
170 size_type _M_element_count
;
171 _RehashPolicy _M_rehash_policy
;
174 _M_allocate_node(const value_type
& __v
);
177 _M_deallocate_node(_Node
* __n
);
180 _M_deallocate_nodes(_Node
**, size_type
);
183 _M_allocate_buckets(size_type __n
);
186 _M_deallocate_buckets(_Node
**, size_type __n
);
189 // Constructor, destructor, assignment, swap
190 _Hashtable(size_type __bucket_hint
,
191 const _H1
&, const _H2
&, const _Hash
&,
192 const _Equal
&, const _ExtractKey
&,
193 const allocator_type
&);
195 template<typename _InputIterator
>
196 _Hashtable(_InputIterator __first
, _InputIterator __last
,
197 size_type __bucket_hint
,
198 const _H1
&, const _H2
&, const _Hash
&,
199 const _Equal
&, const _ExtractKey
&,
200 const allocator_type
&);
202 _Hashtable(const _Hashtable
&);
204 _Hashtable(_Hashtable
&&);
207 operator=(const _Hashtable
&);
211 void swap(_Hashtable
&);
213 // Basic container operations
217 iterator
__i(_M_buckets
);
218 if (!__i
._M_cur_node
)
219 __i
._M_incr_bucket();
226 const_iterator
__i(_M_buckets
);
227 if (!__i
._M_cur_node
)
228 __i
._M_incr_bucket();
234 { return iterator(_M_buckets
+ _M_bucket_count
); }
238 { return const_iterator(_M_buckets
+ _M_bucket_count
); }
243 const_iterator
__i(_M_buckets
);
244 if (!__i
._M_cur_node
)
245 __i
._M_incr_bucket();
251 { return const_iterator(_M_buckets
+ _M_bucket_count
); }
255 { return _M_element_count
; }
259 { return size() == 0; }
262 get_allocator() const
263 { return allocator_type(_M_node_allocator
); }
265 _Value_allocator_type
266 _M_get_Value_allocator() const
267 { return _Value_allocator_type(_M_node_allocator
); }
271 { return _M_node_allocator
.max_size(); }
276 { return this->_M_eq
; }
278 // hash_function, if present, comes from _Hash_code_base.
283 { return _M_bucket_count
; }
286 max_bucket_count() const
287 { return max_size(); }
290 bucket_size(size_type __n
) const
291 { return std::distance(begin(__n
), end(__n
)); }
294 bucket(const key_type
& __k
) const
296 return this->_M_bucket_index(__k
, this->_M_hash_code(__k
),
302 { return local_iterator(_M_buckets
[__n
]); }
306 { return local_iterator(0); }
309 begin(size_type __n
) const
310 { return const_local_iterator(_M_buckets
[__n
]); }
314 { return const_local_iterator(0); }
318 cbegin(size_type __n
) const
319 { return const_local_iterator(_M_buckets
[__n
]); }
322 cend(size_type
) const
323 { return const_local_iterator(0); }
328 return static_cast<float>(size()) / static_cast<float>(bucket_count());
331 // max_load_factor, if present, comes from _Rehash_base.
333 // Generalization of max_load_factor. Extension, not found in TR1. Only
334 // useful if _RehashPolicy is something other than the default.
336 __rehash_policy() const
337 { return _M_rehash_policy
; }
340 __rehash_policy(const _RehashPolicy
&);
344 find(const key_type
& __k
);
347 find(const key_type
& __k
) const;
350 count(const key_type
& __k
) const;
352 std::pair
<iterator
, iterator
>
353 equal_range(const key_type
& __k
);
355 std::pair
<const_iterator
, const_iterator
>
356 equal_range(const key_type
& __k
) const;
358 private: // Find, insert and erase helper functions
359 // ??? This dispatching is a workaround for the fact that we don't
360 // have partial specialization of member templates; it would be
361 // better to just specialize insert on __unique_keys. There may be a
362 // cleaner workaround.
363 typedef typename
std::conditional
<__unique_keys
,
364 std::pair
<iterator
, bool>,
368 typedef typename
std::conditional
<__unique_keys
,
369 std::_Select1st
<_Insert_Return_Type
>,
370 std::_Identity
<_Insert_Return_Type
>
375 _M_find_node(_Node
*, const key_type
&,
376 typename
_Hashtable::_Hash_code_type
) const;
379 _M_insert_bucket(const value_type
&, size_type
,
380 typename
_Hashtable::_Hash_code_type
);
382 std::pair
<iterator
, bool>
383 _M_insert(const value_type
&, std::true_type
);
386 _M_insert(const value_type
&, std::false_type
);
391 insert(const value_type
& __v
)
392 { return _M_insert(__v
, std::integral_constant
<bool,
396 insert(const_iterator
, const value_type
& __v
)
397 { return iterator(_Insert_Conv_Type()(this->insert(__v
))); }
399 template<typename _InputIterator
>
401 insert(_InputIterator __first
, _InputIterator __last
);
404 insert(initializer_list
<value_type
> __l
)
405 { this->insert(__l
.begin(), __l
.end()); }
408 erase(const_iterator
);
411 erase(const key_type
&);
414 erase(const_iterator
, const_iterator
);
419 // Set number of buckets to be appropriate for container of n element.
420 void rehash(size_type __n
);
423 // Unconditionally change size of bucket array to n.
424 void _M_rehash(size_type __n
);
428 // Definitions of class template _Hashtable's out-of-line member functions.
429 template<typename _Key
, typename _Value
,
430 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
431 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
432 bool __chc
, bool __cit
, bool __uk
>
433 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
434 _H1
, _H2
, _Hash
, _RehashPolicy
,
435 __chc
, __cit
, __uk
>::_Node
*
436 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
437 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
438 _M_allocate_node(const value_type
& __v
)
440 _Node
* __n
= _M_node_allocator
.allocate(1);
443 _M_node_allocator
.construct(__n
, __v
);
449 _M_node_allocator
.deallocate(__n
, 1);
450 __throw_exception_again
;
454 template<typename _Key
, typename _Value
,
455 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
456 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
457 bool __chc
, bool __cit
, bool __uk
>
459 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
460 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
461 _M_deallocate_node(_Node
* __n
)
463 _M_node_allocator
.destroy(__n
);
464 _M_node_allocator
.deallocate(__n
, 1);
467 template<typename _Key
, typename _Value
,
468 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
469 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
470 bool __chc
, bool __cit
, bool __uk
>
472 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
473 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
474 _M_deallocate_nodes(_Node
** __array
, size_type __n
)
476 for (size_type __i
= 0; __i
< __n
; ++__i
)
478 _Node
* __p
= __array
[__i
];
483 _M_deallocate_node(__tmp
);
489 template<typename _Key
, typename _Value
,
490 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
491 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
492 bool __chc
, bool __cit
, bool __uk
>
493 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
494 _H1
, _H2
, _Hash
, _RehashPolicy
,
495 __chc
, __cit
, __uk
>::_Node
**
496 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
497 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
498 _M_allocate_buckets(size_type __n
)
500 _Bucket_allocator_type
__alloc(_M_node_allocator
);
502 // We allocate one extra bucket to hold a sentinel, an arbitrary
503 // non-null pointer. Iterator increment relies on this.
504 _Node
** __p
= __alloc
.allocate(__n
+ 1);
505 std::fill(__p
, __p
+ __n
, (_Node
*) 0);
506 __p
[__n
] = reinterpret_cast<_Node
*>(0x1000);
510 template<typename _Key
, typename _Value
,
511 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
512 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
513 bool __chc
, bool __cit
, bool __uk
>
515 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
516 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
517 _M_deallocate_buckets(_Node
** __p
, size_type __n
)
519 _Bucket_allocator_type
__alloc(_M_node_allocator
);
520 __alloc
.deallocate(__p
, __n
+ 1);
523 template<typename _Key
, typename _Value
,
524 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
525 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
526 bool __chc
, bool __cit
, bool __uk
>
527 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
528 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
529 _Hashtable(size_type __bucket_hint
,
530 const _H1
& __h1
, const _H2
& __h2
, const _Hash
& __h
,
531 const _Equal
& __eq
, const _ExtractKey
& __exk
,
532 const allocator_type
& __a
)
533 : __detail::_Rehash_base
<_RehashPolicy
, _Hashtable
>(),
534 __detail::_Hash_code_base
<_Key
, _Value
, _ExtractKey
, _Equal
,
535 _H1
, _H2
, _Hash
, __chc
>(__exk
, __eq
,
537 __detail::_Map_base
<_Key
, _Value
, _ExtractKey
, __uk
, _Hashtable
>(),
538 _M_node_allocator(__a
),
543 _M_bucket_count
= _M_rehash_policy
._M_next_bkt(__bucket_hint
);
544 _M_buckets
= _M_allocate_buckets(_M_bucket_count
);
547 template<typename _Key
, typename _Value
,
548 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
549 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
550 bool __chc
, bool __cit
, bool __uk
>
551 template<typename _InputIterator
>
552 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
553 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
554 _Hashtable(_InputIterator __f
, _InputIterator __l
,
555 size_type __bucket_hint
,
556 const _H1
& __h1
, const _H2
& __h2
, const _Hash
& __h
,
557 const _Equal
& __eq
, const _ExtractKey
& __exk
,
558 const allocator_type
& __a
)
559 : __detail::_Rehash_base
<_RehashPolicy
, _Hashtable
>(),
560 __detail::_Hash_code_base
<_Key
, _Value
, _ExtractKey
, _Equal
,
561 _H1
, _H2
, _Hash
, __chc
>(__exk
, __eq
,
563 __detail::_Map_base
<_Key
, _Value
, _ExtractKey
, __uk
, _Hashtable
>(),
564 _M_node_allocator(__a
),
569 _M_bucket_count
= std::max(_M_rehash_policy
._M_next_bkt(__bucket_hint
),
571 _M_bkt_for_elements(__detail::
574 _M_buckets
= _M_allocate_buckets(_M_bucket_count
);
577 for (; __f
!= __l
; ++__f
)
583 _M_deallocate_buckets(_M_buckets
, _M_bucket_count
);
584 __throw_exception_again
;
588 template<typename _Key
, typename _Value
,
589 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
590 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
591 bool __chc
, bool __cit
, bool __uk
>
592 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
593 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
594 _Hashtable(const _Hashtable
& __ht
)
595 : __detail::_Rehash_base
<_RehashPolicy
, _Hashtable
>(__ht
),
596 __detail::_Hash_code_base
<_Key
, _Value
, _ExtractKey
, _Equal
,
597 _H1
, _H2
, _Hash
, __chc
>(__ht
),
598 __detail::_Map_base
<_Key
, _Value
, _ExtractKey
, __uk
, _Hashtable
>(__ht
),
599 _M_node_allocator(__ht
._M_node_allocator
),
600 _M_bucket_count(__ht
._M_bucket_count
),
601 _M_element_count(__ht
._M_element_count
),
602 _M_rehash_policy(__ht
._M_rehash_policy
)
604 _M_buckets
= _M_allocate_buckets(_M_bucket_count
);
607 for (size_type __i
= 0; __i
< __ht
._M_bucket_count
; ++__i
)
609 _Node
* __n
= __ht
._M_buckets
[__i
];
610 _Node
** __tail
= _M_buckets
+ __i
;
613 *__tail
= _M_allocate_node(__n
->_M_v
);
614 this->_M_copy_code(*__tail
, __n
);
615 __tail
= &((*__tail
)->_M_next
);
623 _M_deallocate_buckets(_M_buckets
, _M_bucket_count
);
624 __throw_exception_again
;
628 template<typename _Key
, typename _Value
,
629 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
630 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
631 bool __chc
, bool __cit
, bool __uk
>
632 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
633 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
634 _Hashtable(_Hashtable
&& __ht
)
635 : __detail::_Rehash_base
<_RehashPolicy
, _Hashtable
>(__ht
),
636 __detail::_Hash_code_base
<_Key
, _Value
, _ExtractKey
, _Equal
,
637 _H1
, _H2
, _Hash
, __chc
>(__ht
),
638 __detail::_Map_base
<_Key
, _Value
, _ExtractKey
, __uk
, _Hashtable
>(__ht
),
639 _M_node_allocator(__ht
._M_node_allocator
),
640 _M_bucket_count(__ht
._M_bucket_count
),
641 _M_element_count(__ht
._M_element_count
),
642 _M_rehash_policy(__ht
._M_rehash_policy
),
643 _M_buckets(__ht
._M_buckets
)
645 size_type __n_bkt
= __ht
._M_rehash_policy
._M_next_bkt(0);
646 __ht
._M_buckets
= __ht
._M_allocate_buckets(__n_bkt
);
647 __ht
._M_bucket_count
= __n_bkt
;
648 __ht
._M_element_count
= 0;
649 __ht
._M_rehash_policy
= _RehashPolicy();
652 template<typename _Key
, typename _Value
,
653 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
654 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
655 bool __chc
, bool __cit
, bool __uk
>
656 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
657 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>&
658 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
659 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
660 operator=(const _Hashtable
& __ht
)
662 _Hashtable
__tmp(__ht
);
667 template<typename _Key
, typename _Value
,
668 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
669 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
670 bool __chc
, bool __cit
, bool __uk
>
671 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
672 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
676 _M_deallocate_buckets(_M_buckets
, _M_bucket_count
);
679 template<typename _Key
, typename _Value
,
680 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
681 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
682 bool __chc
, bool __cit
, bool __uk
>
684 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
685 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
686 swap(_Hashtable
& __x
)
688 // The only base class with member variables is hash_code_base. We
689 // define _Hash_code_base::_M_swap because different specializations
690 // have different members.
691 __detail::_Hash_code_base
<_Key
, _Value
, _ExtractKey
, _Equal
,
692 _H1
, _H2
, _Hash
, __chc
>::_M_swap(__x
);
694 // _GLIBCXX_RESOLVE_LIB_DEFECTS
695 // 431. Swapping containers with unequal allocators.
696 std::__alloc_swap
<_Node_allocator_type
>::_S_do_it(_M_node_allocator
,
697 __x
._M_node_allocator
);
699 std::swap(_M_rehash_policy
, __x
._M_rehash_policy
);
700 std::swap(_M_buckets
, __x
._M_buckets
);
701 std::swap(_M_bucket_count
, __x
._M_bucket_count
);
702 std::swap(_M_element_count
, __x
._M_element_count
);
705 template<typename _Key
, typename _Value
,
706 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
707 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
708 bool __chc
, bool __cit
, bool __uk
>
710 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
711 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
712 __rehash_policy(const _RehashPolicy
& __pol
)
714 _M_rehash_policy
= __pol
;
715 size_type __n_bkt
= __pol
._M_bkt_for_elements(_M_element_count
);
716 if (__n_bkt
> _M_bucket_count
)
720 template<typename _Key
, typename _Value
,
721 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
722 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
723 bool __chc
, bool __cit
, bool __uk
>
724 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
725 _H1
, _H2
, _Hash
, _RehashPolicy
,
726 __chc
, __cit
, __uk
>::iterator
727 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
728 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
729 find(const key_type
& __k
)
731 typename
_Hashtable::_Hash_code_type __code
= this->_M_hash_code(__k
);
732 std::size_t __n
= this->_M_bucket_index(__k
, __code
, _M_bucket_count
);
733 _Node
* __p
= _M_find_node(_M_buckets
[__n
], __k
, __code
);
734 return __p
? iterator(__p
, _M_buckets
+ __n
) : this->end();
737 template<typename _Key
, typename _Value
,
738 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
739 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
740 bool __chc
, bool __cit
, bool __uk
>
741 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
742 _H1
, _H2
, _Hash
, _RehashPolicy
,
743 __chc
, __cit
, __uk
>::const_iterator
744 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
745 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
746 find(const key_type
& __k
) const
748 typename
_Hashtable::_Hash_code_type __code
= this->_M_hash_code(__k
);
749 std::size_t __n
= this->_M_bucket_index(__k
, __code
, _M_bucket_count
);
750 _Node
* __p
= _M_find_node(_M_buckets
[__n
], __k
, __code
);
751 return __p
? const_iterator(__p
, _M_buckets
+ __n
) : this->end();
754 template<typename _Key
, typename _Value
,
755 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
756 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
757 bool __chc
, bool __cit
, bool __uk
>
758 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
759 _H1
, _H2
, _Hash
, _RehashPolicy
,
760 __chc
, __cit
, __uk
>::size_type
761 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
762 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
763 count(const key_type
& __k
) const
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 std::size_t __result
= 0;
768 for (_Node
* __p
= _M_buckets
[__n
]; __p
; __p
= __p
->_M_next
)
769 if (this->_M_compare(__k
, __code
, __p
))
774 template<typename _Key
, typename _Value
,
775 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
776 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
777 bool __chc
, bool __cit
, bool __uk
>
778 std::pair
<typename _Hashtable
<_Key
, _Value
, _Allocator
,
779 _ExtractKey
, _Equal
, _H1
,
780 _H2
, _Hash
, _RehashPolicy
,
781 __chc
, __cit
, __uk
>::iterator
,
782 typename _Hashtable
<_Key
, _Value
, _Allocator
,
783 _ExtractKey
, _Equal
, _H1
,
784 _H2
, _Hash
, _RehashPolicy
,
785 __chc
, __cit
, __uk
>::iterator
>
786 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
787 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
788 equal_range(const key_type
& __k
)
790 typename
_Hashtable::_Hash_code_type __code
= this->_M_hash_code(__k
);
791 std::size_t __n
= this->_M_bucket_index(__k
, __code
, _M_bucket_count
);
792 _Node
** __head
= _M_buckets
+ __n
;
793 _Node
* __p
= _M_find_node(*__head
, __k
, __code
);
797 _Node
* __p1
= __p
->_M_next
;
798 for (; __p1
; __p1
= __p1
->_M_next
)
799 if (!this->_M_compare(__k
, __code
, __p1
))
802 iterator
__first(__p
, __head
);
803 iterator
__last(__p1
, __head
);
805 __last
._M_incr_bucket();
806 return std::make_pair(__first
, __last
);
809 return std::make_pair(this->end(), this->end());
812 template<typename _Key
, typename _Value
,
813 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
814 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
815 bool __chc
, bool __cit
, bool __uk
>
816 std::pair
<typename _Hashtable
<_Key
, _Value
, _Allocator
,
817 _ExtractKey
, _Equal
, _H1
,
818 _H2
, _Hash
, _RehashPolicy
,
819 __chc
, __cit
, __uk
>::const_iterator
,
820 typename _Hashtable
<_Key
, _Value
, _Allocator
,
821 _ExtractKey
, _Equal
, _H1
,
822 _H2
, _Hash
, _RehashPolicy
,
823 __chc
, __cit
, __uk
>::const_iterator
>
824 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
825 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
826 equal_range(const key_type
& __k
) const
828 typename
_Hashtable::_Hash_code_type __code
= this->_M_hash_code(__k
);
829 std::size_t __n
= this->_M_bucket_index(__k
, __code
, _M_bucket_count
);
830 _Node
** __head
= _M_buckets
+ __n
;
831 _Node
* __p
= _M_find_node(*__head
, __k
, __code
);
835 _Node
* __p1
= __p
->_M_next
;
836 for (; __p1
; __p1
= __p1
->_M_next
)
837 if (!this->_M_compare(__k
, __code
, __p1
))
840 const_iterator
__first(__p
, __head
);
841 const_iterator
__last(__p1
, __head
);
843 __last
._M_incr_bucket();
844 return std::make_pair(__first
, __last
);
847 return std::make_pair(this->end(), this->end());
850 // Find the node whose key compares equal to k, beginning the search
851 // at p (usually the head of a bucket). Return nil if no node is found.
852 template<typename _Key
, typename _Value
,
853 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
854 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
855 bool __chc
, bool __cit
, bool __uk
>
856 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
,
857 _Equal
, _H1
, _H2
, _Hash
, _RehashPolicy
,
858 __chc
, __cit
, __uk
>::_Node
*
859 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
860 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
861 _M_find_node(_Node
* __p
, const key_type
& __k
,
862 typename
_Hashtable::_Hash_code_type __code
) const
864 for (; __p
; __p
= __p
->_M_next
)
865 if (this->_M_compare(__k
, __code
, __p
))
870 // Insert v in bucket n (assumes no element with its key already present).
871 template<typename _Key
, typename _Value
,
872 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
873 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
874 bool __chc
, bool __cit
, bool __uk
>
875 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
876 _H1
, _H2
, _Hash
, _RehashPolicy
,
877 __chc
, __cit
, __uk
>::iterator
878 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
879 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
880 _M_insert_bucket(const value_type
& __v
, size_type __n
,
881 typename
_Hashtable::_Hash_code_type __code
)
883 std::pair
<bool, std::size_t> __do_rehash
884 = _M_rehash_policy
._M_need_rehash(_M_bucket_count
,
885 _M_element_count
, 1);
887 // Allocate the new node before doing the rehash so that we don't
888 // do a rehash if the allocation throws.
889 _Node
* __new_node
= _M_allocate_node(__v
);
893 if (__do_rehash
.first
)
895 const key_type
& __k
= this->_M_extract(__v
);
896 __n
= this->_M_bucket_index(__k
, __code
, __do_rehash
.second
);
897 _M_rehash(__do_rehash
.second
);
900 __new_node
->_M_next
= _M_buckets
[__n
];
901 this->_M_store_code(__new_node
, __code
);
902 _M_buckets
[__n
] = __new_node
;
904 return iterator(__new_node
, _M_buckets
+ __n
);
908 _M_deallocate_node(__new_node
);
909 __throw_exception_again
;
913 // Insert v if no element with its key is already present.
914 template<typename _Key
, typename _Value
,
915 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
916 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
917 bool __chc
, bool __cit
, bool __uk
>
918 std::pair
<typename _Hashtable
<_Key
, _Value
, _Allocator
,
919 _ExtractKey
, _Equal
, _H1
,
920 _H2
, _Hash
, _RehashPolicy
,
921 __chc
, __cit
, __uk
>::iterator
, bool>
922 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
923 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
924 _M_insert(const value_type
& __v
, std::true_type
)
926 const key_type
& __k
= this->_M_extract(__v
);
927 typename
_Hashtable::_Hash_code_type __code
= this->_M_hash_code(__k
);
928 size_type __n
= this->_M_bucket_index(__k
, __code
, _M_bucket_count
);
930 if (_Node
* __p
= _M_find_node(_M_buckets
[__n
], __k
, __code
))
931 return std::make_pair(iterator(__p
, _M_buckets
+ __n
), false);
932 return std::make_pair(_M_insert_bucket(__v
, __n
, __code
), true);
935 // Insert v unconditionally.
936 template<typename _Key
, typename _Value
,
937 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
938 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
939 bool __chc
, bool __cit
, bool __uk
>
940 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
941 _H1
, _H2
, _Hash
, _RehashPolicy
,
942 __chc
, __cit
, __uk
>::iterator
943 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
944 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
945 _M_insert(const value_type
& __v
, std::false_type
)
947 std::pair
<bool, std::size_t> __do_rehash
948 = _M_rehash_policy
._M_need_rehash(_M_bucket_count
,
949 _M_element_count
, 1);
950 if (__do_rehash
.first
)
951 _M_rehash(__do_rehash
.second
);
953 const key_type
& __k
= this->_M_extract(__v
);
954 typename
_Hashtable::_Hash_code_type __code
= this->_M_hash_code(__k
);
955 size_type __n
= this->_M_bucket_index(__k
, __code
, _M_bucket_count
);
957 // First find the node, avoid leaking new_node if compare throws.
958 _Node
* __prev
= _M_find_node(_M_buckets
[__n
], __k
, __code
);
959 _Node
* __new_node
= _M_allocate_node(__v
);
963 __new_node
->_M_next
= __prev
->_M_next
;
964 __prev
->_M_next
= __new_node
;
968 __new_node
->_M_next
= _M_buckets
[__n
];
969 _M_buckets
[__n
] = __new_node
;
971 this->_M_store_code(__new_node
, __code
);
974 return iterator(__new_node
, _M_buckets
+ __n
);
977 template<typename _Key
, typename _Value
,
978 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
979 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
980 bool __chc
, bool __cit
, bool __uk
>
981 template<typename _InputIterator
>
983 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
984 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
985 insert(_InputIterator __first
, _InputIterator __last
)
987 size_type __n_elt
= __detail::__distance_fw(__first
, __last
);
988 std::pair
<bool, std::size_t> __do_rehash
989 = _M_rehash_policy
._M_need_rehash(_M_bucket_count
,
990 _M_element_count
, __n_elt
);
991 if (__do_rehash
.first
)
992 _M_rehash(__do_rehash
.second
);
994 for (; __first
!= __last
; ++__first
)
995 this->insert(*__first
);
998 template<typename _Key
, typename _Value
,
999 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
1000 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1001 bool __chc
, bool __cit
, bool __uk
>
1003 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1004 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
1005 erase(const_iterator __it
)
1007 _Node
* __p
= __it
._M_cur_node
;
1008 _Node
** __b
= __it
._M_cur_bucket
;
1010 _Node
* __cur
= *__b
;
1012 *__b
= __cur
->_M_next
;
1015 _Node
* __next
= __cur
->_M_next
;
1016 while (__next
!= __p
)
1019 __next
= __cur
->_M_next
;
1021 __cur
->_M_next
= __next
->_M_next
;
1024 _M_deallocate_node(__p
);
1028 template<typename _Key
, typename _Value
,
1029 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
1030 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1031 bool __chc
, bool __cit
, bool __uk
>
1032 typename _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1033 _H1
, _H2
, _Hash
, _RehashPolicy
,
1034 __chc
, __cit
, __uk
>::size_type
1035 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1036 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
1037 erase(const key_type
& __k
)
1039 typename
_Hashtable::_Hash_code_type __code
= this->_M_hash_code(__k
);
1040 std::size_t __n
= this->_M_bucket_index(__k
, __code
, _M_bucket_count
);
1041 size_type __result
= 0;
1043 _Node
** __slot
= _M_buckets
+ __n
;
1044 while (*__slot
&& !this->_M_compare(__k
, __code
, *__slot
))
1045 __slot
= &((*__slot
)->_M_next
);
1047 _Node
** __saved_slot
= 0;
1048 while (*__slot
&& this->_M_compare(__k
, __code
, *__slot
))
1050 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1051 // 526. Is it undefined if a function in the standard changes
1053 if (&this->_M_extract((*__slot
)->_M_v
) != &__k
)
1055 _Node
* __p
= *__slot
;
1056 *__slot
= __p
->_M_next
;
1057 _M_deallocate_node(__p
);
1063 __saved_slot
= __slot
;
1064 __slot
= &((*__slot
)->_M_next
);
1070 _Node
* __p
= *__saved_slot
;
1071 *__saved_slot
= __p
->_M_next
;
1072 _M_deallocate_node(__p
);
1080 // ??? This could be optimized by taking advantage of the bucket
1081 // structure, but it's not clear that it's worth doing. It probably
1082 // wouldn't even be an optimization unless the load factor is large.
1083 template<typename _Key
, typename _Value
,
1084 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
1085 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1086 bool __chc
, bool __cit
, bool __uk
>
1088 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1089 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
1090 erase(const_iterator __first
, const_iterator __last
)
1092 if (__first
== begin() && __last
== end())
1095 while (__first
!= __last
)
1099 template<typename _Key
, typename _Value
,
1100 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
1101 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1102 bool __chc
, bool __cit
, bool __uk
>
1104 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1105 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
1108 _M_deallocate_nodes(_M_buckets
, _M_bucket_count
);
1109 _M_element_count
= 0;
1112 template<typename _Key
, typename _Value
,
1113 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
1114 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1115 bool __chc
, bool __cit
, bool __uk
>
1117 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1118 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
1119 rehash(size_type __n
)
1121 _M_rehash(std::max(_M_rehash_policy
._M_next_bkt(__n
),
1122 _M_rehash_policy
._M_bkt_for_elements(_M_element_count
1126 template<typename _Key
, typename _Value
,
1127 typename _Allocator
, typename _ExtractKey
, typename _Equal
,
1128 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1129 bool __chc
, bool __cit
, bool __uk
>
1131 _Hashtable
<_Key
, _Value
, _Allocator
, _ExtractKey
, _Equal
,
1132 _H1
, _H2
, _Hash
, _RehashPolicy
, __chc
, __cit
, __uk
>::
1133 _M_rehash(size_type __n
)
1135 _Node
** __new_array
= _M_allocate_buckets(__n
);
1138 for (size_type __i
= 0; __i
< _M_bucket_count
; ++__i
)
1139 while (_Node
* __p
= _M_buckets
[__i
])
1141 std::size_t __new_index
= this->_M_bucket_index(__p
, __n
);
1142 _M_buckets
[__i
] = __p
->_M_next
;
1143 __p
->_M_next
= __new_array
[__new_index
];
1144 __new_array
[__new_index
] = __p
;
1146 _M_deallocate_buckets(_M_buckets
, _M_bucket_count
);
1147 _M_bucket_count
= __n
;
1148 _M_buckets
= __new_array
;
1152 // A failure here means that a hash function threw an exception.
1153 // We can't restore the previous state without calling the hash
1154 // function again, so the only sensible recovery is to delete
1156 _M_deallocate_nodes(__new_array
, __n
);
1157 _M_deallocate_buckets(__new_array
, __n
);
1158 _M_deallocate_nodes(_M_buckets
, _M_bucket_count
);
1159 _M_element_count
= 0;
1160 __throw_exception_again
;
1165 #endif // _HASHTABLE_H