1 // Internal header for TR1 unordered_set and unordered_map -*- C++ -*-
3 // Copyright (C) 2005, 2006, 2007 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file tr1/hashtable
31 * This is a TR1 C++ Library header.
34 // This header file defines std::tr1::hashtable, which is used to
35 // implement std::tr1::unordered_set, std::tr1::unordered_map,
36 // std::tr1::unordered_multiset, and std::tr1::unordered_multimap.
37 // hashtable has many template parameters, partly to accommodate
38 // the differences between those four classes and partly to
39 // accommodate policy choices that go beyond what TR1 calls for.
41 // Class template hashtable attempts to encapsulate all reasonable
42 // variation among hash tables that use chaining. It does not handle
46 // M. Austern, "A Proposal to Add Hash Tables to the Standard
47 // Library (revision 4)," WG21 Document N1456=03-0039, 2003.
48 // D. E. Knuth, The Art of Computer Programming, v. 3, Sorting and Searching.
49 // A. Tavori and V. Dreizin, "Policy-Based Data Structures", 2004.
50 // http://gcc.gnu.org/onlinedocs/libstdc++/ext/pb_ds/index.html
52 #ifndef _TR1_HASHTABLE
53 #define _TR1_HASHTABLE 1
55 #include <utility> // For std::pair
59 #include <bits/allocator.h>
60 #include <bits/functexcept.h>
61 #include <tr1/type_traits> // For true_type and false_type
62 #include <tr1/hashtable_policy.h>
66 _GLIBCXX_BEGIN_NAMESPACE(_GLIBCXX_TR1)
68 // Class template _Hashtable, class definition.
70 // Meaning of class template _Hashtable's template parameters
72 // _Key and _Value: arbitrary CopyConstructible types.
74 // _Allocator: an allocator type ([lib.allocator.requirements]) whose
75 // value type is Value. As a conforming extension, we allow for
76 // value type != Value.
78 // _ExtractKey: function object that takes a object of type Value
79 // and returns a value of type _Key.
81 // _Equal: function object that takes two objects of type k and returns
82 // a bool-like value that is true if the two objects are considered equal.
84 // _H1: the hash function. A unary function object with argument type
85 // Key and result type size_t. Return values should be distributed
86 // over the entire range [0, numeric_limits<size_t>:::max()].
88 // _H2: the range-hashing function (in the terminology of Tavori and
89 // Dreizin). A binary function object whose argument types and result
90 // type are all size_t. Given arguments r and N, the return value is
91 // in the range [0, N).
93 // _Hash: the ranged hash function (Tavori and Dreizin). A binary function
94 // whose argument types are _Key and size_t and whose result type is
95 // size_t. Given arguments k and N, the return value is in the range
96 // [0, N). Default: hash(k, N) = h2(h1(k), N). If _Hash is anything other
97 // than the default, _H1 and _H2 are ignored.
99 // _RehashPolicy: Policy class with three members, all of which govern
100 // the bucket count. _M_next_bkt(n) returns a bucket count no smaller
101 // than n. _M_bkt_for_elements(n) returns a bucket count appropriate
102 // for an element count of n. _M_need_rehash(n_bkt, n_elt, n_ins)
103 // determines whether, if the current bucket count is n_bkt and the
104 // current element count is n_elt, we need to increase the bucket
105 // count. If so, returns make_pair(true, n), where n is the new
106 // bucket count. If not, returns make_pair(false, <anything>).
108 // ??? Right now it is hard-wired that the number of buckets never
109 // shrinks. Should we allow _RehashPolicy to change that?
111 // __cache_hash_code: bool. true if we store the value of the hash
112 // function along with the value. This is a time-space tradeoff.
113 // Storing it may improve lookup speed by reducing the number of times
114 // we need to call the Equal function.
116 // __constant_iterators: bool. true if iterator and const_iterator are
117 // both constant iterator types. This is true for unordered_set and
118 // unordered_multiset, false for unordered_map and unordered_multimap.
120 // __unique_keys: bool. true if the return value of _Hashtable::count(k)
121 // is always at most one, false if it may be an arbitrary number. This
122 // true for unordered_set and unordered_map, false for unordered_multiset
123 // and unordered_multimap.
125 template<typename _Key, typename _Value, typename _Allocator,
126 typename _ExtractKey, typename _Equal,
127 typename _H1, typename _H2, typename _Hash,
128 typename _RehashPolicy,
129 bool __cache_hash_code,
130 bool __constant_iterators,
133 : public __detail::_Rehash_base<_RehashPolicy,
134 _Hashtable<_Key, _Value, _Allocator,
136 _Equal, _H1, _H2, _Hash,
139 __constant_iterators,
141 public __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
142 _H1, _H2, _Hash, __cache_hash_code>,
143 public __detail::_Map_base<_Key, _Value, _ExtractKey, __unique_keys,
144 _Hashtable<_Key, _Value, _Allocator,
146 _Equal, _H1, _H2, _Hash,
149 __constant_iterators,
153 typedef _Allocator allocator_type;
154 typedef _Value value_type;
155 typedef _Key key_type;
156 typedef _Equal key_equal;
157 // mapped_type, if present, comes from _Map_base.
158 // hasher, if present, comes from _Hash_code_base.
159 typedef typename _Allocator::difference_type difference_type;
160 typedef typename _Allocator::size_type size_type;
161 typedef typename _Allocator::reference reference;
162 typedef typename _Allocator::const_reference const_reference;
164 typedef __detail::_Node_iterator<value_type, __constant_iterators,
167 typedef __detail::_Node_const_iterator<value_type,
168 __constant_iterators,
170 const_local_iterator;
172 typedef __detail::_Hashtable_iterator<value_type, __constant_iterators,
175 typedef __detail::_Hashtable_const_iterator<value_type,
176 __constant_iterators,
180 template<typename _Key2, typename _Pair, typename _Hashtable>
181 friend struct __detail::_Map_base;
184 typedef __detail::_Hash_node<_Value, __cache_hash_code> _Node;
185 typedef typename _Allocator::template rebind<_Node>::other
186 _Node_allocator_type;
187 typedef typename _Allocator::template rebind<_Node*>::other
188 _Bucket_allocator_type;
190 typedef typename _Allocator::template rebind<_Value>::other
191 _Value_allocator_type;
193 _Node_allocator_type _M_node_allocator;
195 size_type _M_bucket_count;
196 size_type _M_element_count;
197 _RehashPolicy _M_rehash_policy;
200 _M_allocate_node(const value_type& __v);
203 _M_deallocate_node(_Node* __n);
206 _M_deallocate_nodes(_Node**, size_type);
209 _M_allocate_buckets(size_type __n);
212 _M_deallocate_buckets(_Node**, size_type __n);
215 // Constructor, destructor, assignment, swap
216 _Hashtable(size_type __bucket_hint,
217 const _H1&, const _H2&, const _Hash&,
218 const _Equal&, const _ExtractKey&,
219 const allocator_type&);
221 template<typename _InputIterator>
222 _Hashtable(_InputIterator __first, _InputIterator __last,
223 size_type __bucket_hint,
224 const _H1&, const _H2&, const _Hash&,
225 const _Equal&, const _ExtractKey&,
226 const allocator_type&);
228 _Hashtable(const _Hashtable&);
231 operator=(const _Hashtable&);
235 void swap(_Hashtable&);
237 // Basic container operations
241 iterator __i(_M_buckets);
242 if (!__i._M_cur_node)
243 __i._M_incr_bucket();
250 const_iterator __i(_M_buckets);
251 if (!__i._M_cur_node)
252 __i._M_incr_bucket();
258 { return iterator(_M_buckets + _M_bucket_count); }
262 { return const_iterator(_M_buckets + _M_bucket_count); }
266 { return _M_element_count; }
270 { return size() == 0; }
273 get_allocator() const
274 { return allocator_type(_M_node_allocator); }
276 _Value_allocator_type
277 _M_get_Value_allocator() const
278 { return _Value_allocator_type(_M_node_allocator); }
282 { return _M_get_Value_allocator().max_size(); }
287 { return this->_M_eq; }
289 // hash_function, if present, comes from _Hash_code_base.
294 { return _M_bucket_count; }
297 max_bucket_count() const
298 { return max_size(); }
301 bucket_size(size_type __n) const
302 { return std::distance(begin(__n), end(__n)); }
305 bucket(const key_type& __k) const
307 return this->_M_bucket_index(__k, this->_M_hash_code(__k),
313 { return local_iterator(_M_buckets[__n]); }
317 { return local_iterator(0); }
320 begin(size_type __n) const
321 { return const_local_iterator(_M_buckets[__n]); }
325 { return const_local_iterator(0); }
330 return static_cast<float>(size()) / static_cast<float>(bucket_count());
333 // max_load_factor, if present, comes from _Rehash_base.
335 // Generalization of max_load_factor. Extension, not found in TR1. Only
336 // useful if _RehashPolicy is something other than the default.
338 __rehash_policy() const
339 { return _M_rehash_policy; }
342 __rehash_policy(const _RehashPolicy&);
346 find(const key_type& __k);
349 find(const key_type& __k) const;
352 count(const key_type& __k) const;
354 std::pair<iterator, iterator>
355 equal_range(const key_type& __k);
357 std::pair<const_iterator, const_iterator>
358 equal_range(const key_type& __k) const;
360 private: // Find, insert and erase helper functions
361 // ??? This dispatching is a workaround for the fact that we don't
362 // have partial specialization of member templates; it would be
363 // better to just specialize insert on __unique_keys. There may be a
364 // cleaner workaround.
365 typedef typename __gnu_cxx::__conditional_type<__unique_keys,
366 std::pair<iterator, bool>, iterator>::__type
369 typedef typename __gnu_cxx::__conditional_type<__unique_keys,
370 std::_Select1st<_Insert_Return_Type>,
371 std::_Identity<_Insert_Return_Type>
376 _M_find_node(_Node*, const key_type&,
377 typename _Hashtable::_Hash_code_type) const;
380 _M_insert_bucket(const value_type&, size_type,
381 typename _Hashtable::_Hash_code_type);
383 std::pair<iterator, bool>
384 _M_insert(const value_type&, std::_GLIBCXX_TR1::true_type);
387 _M_insert(const value_type&, std::_GLIBCXX_TR1::false_type);
390 _M_erase_node(_Node*, _Node**);
395 insert(const value_type& __v)
396 { return _M_insert(__v, std::_GLIBCXX_TR1::integral_constant<bool,
400 insert(iterator, const value_type& __v)
401 { return iterator(_Insert_Conv_Type()(this->insert(__v))); }
404 insert(const_iterator, const value_type& __v)
405 { return const_iterator(_Insert_Conv_Type()(this->insert(__v))); }
407 template<typename _InputIterator>
409 insert(_InputIterator __first, _InputIterator __last);
415 erase(const_iterator);
418 erase(const key_type&);
421 erase(iterator, iterator);
424 erase(const_iterator, const_iterator);
429 // Set number of buckets to be appropriate for container of n element.
430 void rehash(size_type __n);
433 // Unconditionally change size of bucket array to n.
434 void _M_rehash(size_type __n);
438 // Definitions of class template _Hashtable's out-of-line member functions.
439 template<typename _Key, typename _Value,
440 typename _Allocator, typename _ExtractKey, typename _Equal,
441 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
442 bool __chc, bool __cit, bool __uk>
443 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
444 _H1, _H2, _Hash, _RehashPolicy,
445 __chc, __cit, __uk>::_Node*
446 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
447 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
448 _M_allocate_node(const value_type& __v)
450 _Node* __n = _M_node_allocator.allocate(1);
453 _M_get_Value_allocator().construct(&__n->_M_v, __v);
459 _M_node_allocator.deallocate(__n, 1);
460 __throw_exception_again;
464 template<typename _Key, typename _Value,
465 typename _Allocator, typename _ExtractKey, typename _Equal,
466 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
467 bool __chc, bool __cit, bool __uk>
469 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
470 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
471 _M_deallocate_node(_Node* __n)
473 _M_get_Value_allocator().destroy(&__n->_M_v);
474 _M_node_allocator.deallocate(__n, 1);
477 template<typename _Key, typename _Value,
478 typename _Allocator, typename _ExtractKey, typename _Equal,
479 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
480 bool __chc, bool __cit, bool __uk>
482 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
483 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
484 _M_deallocate_nodes(_Node** __array, size_type __n)
486 for (size_type __i = 0; __i < __n; ++__i)
488 _Node* __p = __array[__i];
493 _M_deallocate_node(__tmp);
499 template<typename _Key, typename _Value,
500 typename _Allocator, typename _ExtractKey, typename _Equal,
501 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
502 bool __chc, bool __cit, bool __uk>
503 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
504 _H1, _H2, _Hash, _RehashPolicy,
505 __chc, __cit, __uk>::_Node**
506 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
507 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
508 _M_allocate_buckets(size_type __n)
510 _Bucket_allocator_type __alloc(_M_node_allocator);
512 // We allocate one extra bucket to hold a sentinel, an arbitrary
513 // non-null pointer. Iterator increment relies on this.
514 _Node** __p = __alloc.allocate(__n + 1);
515 std::fill(__p, __p + __n, (_Node*) 0);
516 __p[__n] = reinterpret_cast<_Node*>(0x1000);
520 template<typename _Key, typename _Value,
521 typename _Allocator, typename _ExtractKey, typename _Equal,
522 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
523 bool __chc, bool __cit, bool __uk>
525 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
526 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
527 _M_deallocate_buckets(_Node** __p, size_type __n)
529 _Bucket_allocator_type __alloc(_M_node_allocator);
530 __alloc.deallocate(__p, __n + 1);
533 template<typename _Key, typename _Value,
534 typename _Allocator, typename _ExtractKey, typename _Equal,
535 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
536 bool __chc, bool __cit, bool __uk>
537 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
538 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
539 _Hashtable(size_type __bucket_hint,
540 const _H1& __h1, const _H2& __h2, const _Hash& __h,
541 const _Equal& __eq, const _ExtractKey& __exk,
542 const allocator_type& __a)
543 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(),
544 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
545 _H1, _H2, _Hash, __chc>(__exk, __eq,
547 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(),
548 _M_node_allocator(__a),
553 _M_bucket_count = _M_rehash_policy._M_next_bkt(__bucket_hint);
554 _M_buckets = _M_allocate_buckets(_M_bucket_count);
557 template<typename _Key, typename _Value,
558 typename _Allocator, typename _ExtractKey, typename _Equal,
559 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
560 bool __chc, bool __cit, bool __uk>
561 template<typename _InputIterator>
562 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
563 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
564 _Hashtable(_InputIterator __f, _InputIterator __l,
565 size_type __bucket_hint,
566 const _H1& __h1, const _H2& __h2, const _Hash& __h,
567 const _Equal& __eq, const _ExtractKey& __exk,
568 const allocator_type& __a)
569 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(),
570 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
571 _H1, _H2, _Hash, __chc>(__exk, __eq,
573 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(),
574 _M_node_allocator(__a),
579 _M_bucket_count = std::max(_M_rehash_policy._M_next_bkt(__bucket_hint),
581 _M_bkt_for_elements(__detail::
584 _M_buckets = _M_allocate_buckets(_M_bucket_count);
587 for (; __f != __l; ++__f)
593 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
594 __throw_exception_again;
598 template<typename _Key, typename _Value,
599 typename _Allocator, typename _ExtractKey, typename _Equal,
600 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
601 bool __chc, bool __cit, bool __uk>
602 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
603 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
604 _Hashtable(const _Hashtable& __ht)
605 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(__ht),
606 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
607 _H1, _H2, _Hash, __chc>(__ht),
608 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(__ht),
609 _M_node_allocator(__ht._M_node_allocator),
610 _M_bucket_count(__ht._M_bucket_count),
611 _M_element_count(__ht._M_element_count),
612 _M_rehash_policy(__ht._M_rehash_policy)
614 _M_buckets = _M_allocate_buckets(_M_bucket_count);
617 for (size_type __i = 0; __i < __ht._M_bucket_count; ++__i)
619 _Node* __n = __ht._M_buckets[__i];
620 _Node** __tail = _M_buckets + __i;
623 *__tail = _M_allocate_node(__n->_M_v);
624 this->_M_copy_code(*__tail, __n);
625 __tail = &((*__tail)->_M_next);
633 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
634 __throw_exception_again;
638 template<typename _Key, typename _Value,
639 typename _Allocator, typename _ExtractKey, typename _Equal,
640 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
641 bool __chc, bool __cit, bool __uk>
642 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
643 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>&
644 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
645 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
646 operator=(const _Hashtable& __ht)
648 _Hashtable __tmp(__ht);
653 template<typename _Key, typename _Value,
654 typename _Allocator, typename _ExtractKey, typename _Equal,
655 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
656 bool __chc, bool __cit, bool __uk>
657 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
658 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
662 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
665 template<typename _Key, typename _Value,
666 typename _Allocator, typename _ExtractKey, typename _Equal,
667 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
668 bool __chc, bool __cit, bool __uk>
670 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
671 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
672 swap(_Hashtable& __x)
674 // The only base class with member variables is hash_code_base. We
675 // define _Hash_code_base::_M_swap because different specializations
676 // have different members.
677 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
678 _H1, _H2, _Hash, __chc>::_M_swap(__x);
680 // _GLIBCXX_RESOLVE_LIB_DEFECTS
681 // 431. Swapping containers with unequal allocators.
682 std::__alloc_swap<_Node_allocator_type>::_S_do_it(_M_node_allocator,
683 __x._M_node_allocator);
685 std::swap(_M_rehash_policy, __x._M_rehash_policy);
686 std::swap(_M_buckets, __x._M_buckets);
687 std::swap(_M_bucket_count, __x._M_bucket_count);
688 std::swap(_M_element_count, __x._M_element_count);
691 template<typename _Key, typename _Value,
692 typename _Allocator, typename _ExtractKey, typename _Equal,
693 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
694 bool __chc, bool __cit, bool __uk>
696 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
697 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
698 __rehash_policy(const _RehashPolicy& __pol)
700 _M_rehash_policy = __pol;
701 size_type __n_bkt = __pol._M_bkt_for_elements(_M_element_count);
702 if (__n_bkt > _M_bucket_count)
706 template<typename _Key, typename _Value,
707 typename _Allocator, typename _ExtractKey, typename _Equal,
708 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
709 bool __chc, bool __cit, bool __uk>
710 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
711 _H1, _H2, _Hash, _RehashPolicy,
712 __chc, __cit, __uk>::iterator
713 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
714 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
715 find(const key_type& __k)
717 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
718 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
719 _Node* __p = _M_find_node(_M_buckets[__n], __k, __code);
720 return __p ? iterator(__p, _M_buckets + __n) : this->end();
723 template<typename _Key, typename _Value,
724 typename _Allocator, typename _ExtractKey, typename _Equal,
725 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
726 bool __chc, bool __cit, bool __uk>
727 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
728 _H1, _H2, _Hash, _RehashPolicy,
729 __chc, __cit, __uk>::const_iterator
730 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
731 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
732 find(const key_type& __k) const
734 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
735 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
736 _Node* __p = _M_find_node(_M_buckets[__n], __k, __code);
737 return __p ? const_iterator(__p, _M_buckets + __n) : this->end();
740 template<typename _Key, typename _Value,
741 typename _Allocator, typename _ExtractKey, typename _Equal,
742 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
743 bool __chc, bool __cit, bool __uk>
744 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
745 _H1, _H2, _Hash, _RehashPolicy,
746 __chc, __cit, __uk>::size_type
747 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
748 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
749 count(const key_type& __k) const
751 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
752 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
753 std::size_t __result = 0;
754 for (_Node* __p = _M_buckets[__n]; __p; __p = __p->_M_next)
755 if (this->_M_compare(__k, __code, __p))
760 template<typename _Key, typename _Value,
761 typename _Allocator, typename _ExtractKey, typename _Equal,
762 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
763 bool __chc, bool __cit, bool __uk>
764 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
765 _ExtractKey, _Equal, _H1,
766 _H2, _Hash, _RehashPolicy,
767 __chc, __cit, __uk>::iterator,
768 typename _Hashtable<_Key, _Value, _Allocator,
769 _ExtractKey, _Equal, _H1,
770 _H2, _Hash, _RehashPolicy,
771 __chc, __cit, __uk>::iterator>
772 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
773 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
774 equal_range(const key_type& __k)
776 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
777 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
778 _Node** __head = _M_buckets + __n;
779 _Node* __p = _M_find_node(*__head, __k, __code);
783 _Node* __p1 = __p->_M_next;
784 for (; __p1; __p1 = __p1->_M_next)
785 if (!this->_M_compare(__k, __code, __p1))
788 iterator __first(__p, __head);
789 iterator __last(__p1, __head);
791 __last._M_incr_bucket();
792 return std::make_pair(__first, __last);
795 return std::make_pair(this->end(), this->end());
798 template<typename _Key, typename _Value,
799 typename _Allocator, typename _ExtractKey, typename _Equal,
800 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
801 bool __chc, bool __cit, bool __uk>
802 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
803 _ExtractKey, _Equal, _H1,
804 _H2, _Hash, _RehashPolicy,
805 __chc, __cit, __uk>::const_iterator,
806 typename _Hashtable<_Key, _Value, _Allocator,
807 _ExtractKey, _Equal, _H1,
808 _H2, _Hash, _RehashPolicy,
809 __chc, __cit, __uk>::const_iterator>
810 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
811 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
812 equal_range(const key_type& __k) const
814 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
815 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
816 _Node** __head = _M_buckets + __n;
817 _Node* __p = _M_find_node(*__head, __k, __code);
821 _Node* __p1 = __p->_M_next;
822 for (; __p1; __p1 = __p1->_M_next)
823 if (!this->_M_compare(__k, __code, __p1))
826 const_iterator __first(__p, __head);
827 const_iterator __last(__p1, __head);
829 __last._M_incr_bucket();
830 return std::make_pair(__first, __last);
833 return std::make_pair(this->end(), this->end());
836 // Find the node whose key compares equal to k, beginning the search
837 // at p (usually the head of a bucket). Return nil if no node is found.
838 template<typename _Key, typename _Value,
839 typename _Allocator, typename _ExtractKey, typename _Equal,
840 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
841 bool __chc, bool __cit, bool __uk>
842 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey,
843 _Equal, _H1, _H2, _Hash, _RehashPolicy,
844 __chc, __cit, __uk>::_Node*
845 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
846 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
847 _M_find_node(_Node* __p, const key_type& __k,
848 typename _Hashtable::_Hash_code_type __code) const
850 for (; __p; __p = __p->_M_next)
851 if (this->_M_compare(__k, __code, __p))
856 // Insert v in bucket n (assumes no element with its key already present).
857 template<typename _Key, typename _Value,
858 typename _Allocator, typename _ExtractKey, typename _Equal,
859 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
860 bool __chc, bool __cit, bool __uk>
861 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
862 _H1, _H2, _Hash, _RehashPolicy,
863 __chc, __cit, __uk>::iterator
864 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
865 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
866 _M_insert_bucket(const value_type& __v, size_type __n,
867 typename _Hashtable::_Hash_code_type __code)
869 std::pair<bool, std::size_t> __do_rehash
870 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
871 _M_element_count, 1);
873 // Allocate the new node before doing the rehash so that we don't
874 // do a rehash if the allocation throws.
875 _Node* __new_node = _M_allocate_node(__v);
879 if (__do_rehash.first)
881 const key_type& __k = this->_M_extract(__v);
882 __n = this->_M_bucket_index(__k, __code, __do_rehash.second);
883 _M_rehash(__do_rehash.second);
886 __new_node->_M_next = _M_buckets[__n];
887 this->_M_store_code(__new_node, __code);
888 _M_buckets[__n] = __new_node;
890 return iterator(__new_node, _M_buckets + __n);
894 _M_deallocate_node(__new_node);
895 __throw_exception_again;
899 // Insert v if no element with its key is already present.
900 template<typename _Key, typename _Value,
901 typename _Allocator, typename _ExtractKey, typename _Equal,
902 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
903 bool __chc, bool __cit, bool __uk>
904 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
905 _ExtractKey, _Equal, _H1,
906 _H2, _Hash, _RehashPolicy,
907 __chc, __cit, __uk>::iterator, bool>
908 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
909 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
910 _M_insert(const value_type& __v, std::_GLIBCXX_TR1::true_type)
912 const key_type& __k = this->_M_extract(__v);
913 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
914 size_type __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
916 if (_Node* __p = _M_find_node(_M_buckets[__n], __k, __code))
917 return std::make_pair(iterator(__p, _M_buckets + __n), false);
918 return std::make_pair(_M_insert_bucket(__v, __n, __code), true);
921 // Insert v unconditionally.
922 template<typename _Key, typename _Value,
923 typename _Allocator, typename _ExtractKey, typename _Equal,
924 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
925 bool __chc, bool __cit, bool __uk>
926 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
927 _H1, _H2, _Hash, _RehashPolicy,
928 __chc, __cit, __uk>::iterator
929 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
930 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
931 _M_insert(const value_type& __v, std::_GLIBCXX_TR1::false_type)
933 std::pair<bool, std::size_t> __do_rehash
934 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
935 _M_element_count, 1);
936 if (__do_rehash.first)
937 _M_rehash(__do_rehash.second);
939 const key_type& __k = this->_M_extract(__v);
940 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
941 size_type __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
943 // First find the node, avoid leaking new_node if compare throws.
944 _Node* __prev = _M_find_node(_M_buckets[__n], __k, __code);
945 _Node* __new_node = _M_allocate_node(__v);
949 __new_node->_M_next = __prev->_M_next;
950 __prev->_M_next = __new_node;
954 __new_node->_M_next = _M_buckets[__n];
955 _M_buckets[__n] = __new_node;
957 this->_M_store_code(__new_node, __code);
960 return iterator(__new_node, _M_buckets + __n);
963 // For erase(iterator) and erase(const_iterator).
964 template<typename _Key, typename _Value,
965 typename _Allocator, typename _ExtractKey, typename _Equal,
966 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
967 bool __chc, bool __cit, bool __uk>
969 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
970 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
971 _M_erase_node(_Node* __p, _Node** __b)
975 *__b = __cur->_M_next;
978 _Node* __next = __cur->_M_next;
979 while (__next != __p)
982 __next = __cur->_M_next;
984 __cur->_M_next = __next->_M_next;
987 _M_deallocate_node(__p);
991 template<typename _Key, typename _Value,
992 typename _Allocator, typename _ExtractKey, typename _Equal,
993 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
994 bool __chc, bool __cit, bool __uk>
995 template<typename _InputIterator>
997 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
998 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
999 insert(_InputIterator __first, _InputIterator __last)
1001 size_type __n_elt = __detail::__distance_fw(__first, __last);
1002 std::pair<bool, std::size_t> __do_rehash
1003 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
1004 _M_element_count, __n_elt);
1005 if (__do_rehash.first)
1006 _M_rehash(__do_rehash.second);
1008 for (; __first != __last; ++__first)
1009 this->insert(*__first);
1012 template<typename _Key, typename _Value,
1013 typename _Allocator, typename _ExtractKey, typename _Equal,
1014 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1015 bool __chc, bool __cit, bool __uk>
1016 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1017 _H1, _H2, _Hash, _RehashPolicy,
1018 __chc, __cit, __uk>::iterator
1019 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1020 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1021 erase(iterator __it)
1023 iterator __result = __it;
1025 _M_erase_node(__it._M_cur_node, __it._M_cur_bucket);
1029 template<typename _Key, typename _Value,
1030 typename _Allocator, typename _ExtractKey, typename _Equal,
1031 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1032 bool __chc, bool __cit, bool __uk>
1033 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1034 _H1, _H2, _Hash, _RehashPolicy,
1035 __chc, __cit, __uk>::const_iterator
1036 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1037 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1038 erase(const_iterator __it)
1040 const_iterator __result = __it;
1042 _M_erase_node(__it._M_cur_node, __it._M_cur_bucket);
1046 template<typename _Key, typename _Value,
1047 typename _Allocator, typename _ExtractKey, typename _Equal,
1048 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1049 bool __chc, bool __cit, bool __uk>
1050 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1051 _H1, _H2, _Hash, _RehashPolicy,
1052 __chc, __cit, __uk>::size_type
1053 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1054 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1055 erase(const key_type& __k)
1057 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
1058 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
1059 size_type __result = 0;
1061 _Node** __slot = _M_buckets + __n;
1062 while (*__slot && !this->_M_compare(__k, __code, *__slot))
1063 __slot = &((*__slot)->_M_next);
1065 _Node** __saved_slot = 0;
1066 while (*__slot && this->_M_compare(__k, __code, *__slot))
1068 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1069 // 526. Is it undefined if a function in the standard changes
1071 if (&this->_M_extract((*__slot)->_M_v) != &__k)
1073 _Node* __p = *__slot;
1074 *__slot = __p->_M_next;
1075 _M_deallocate_node(__p);
1081 __saved_slot = __slot;
1082 __slot = &((*__slot)->_M_next);
1088 _Node* __p = *__saved_slot;
1089 *__saved_slot = __p->_M_next;
1090 _M_deallocate_node(__p);
1098 // ??? This could be optimized by taking advantage of the bucket
1099 // structure, but it's not clear that it's worth doing. It probably
1100 // wouldn't even be an optimization unless the load factor is large.
1101 template<typename _Key, typename _Value,
1102 typename _Allocator, typename _ExtractKey, typename _Equal,
1103 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1104 bool __chc, bool __cit, bool __uk>
1105 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1106 _H1, _H2, _Hash, _RehashPolicy,
1107 __chc, __cit, __uk>::iterator
1108 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1109 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1110 erase(iterator __first, iterator __last)
1112 while (__first != __last)
1113 __first = this->erase(__first);
1117 template<typename _Key, typename _Value,
1118 typename _Allocator, typename _ExtractKey, typename _Equal,
1119 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1120 bool __chc, bool __cit, bool __uk>
1121 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1122 _H1, _H2, _Hash, _RehashPolicy,
1123 __chc, __cit, __uk>::const_iterator
1124 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1125 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1126 erase(const_iterator __first, const_iterator __last)
1128 while (__first != __last)
1129 __first = this->erase(__first);
1133 template<typename _Key, typename _Value,
1134 typename _Allocator, typename _ExtractKey, typename _Equal,
1135 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1136 bool __chc, bool __cit, bool __uk>
1138 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1139 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1142 _M_deallocate_nodes(_M_buckets, _M_bucket_count);
1143 _M_element_count = 0;
1146 template<typename _Key, typename _Value,
1147 typename _Allocator, typename _ExtractKey, typename _Equal,
1148 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1149 bool __chc, bool __cit, bool __uk>
1151 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1152 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1153 rehash(size_type __n)
1155 _M_rehash(std::max(_M_rehash_policy._M_next_bkt(__n),
1156 _M_rehash_policy._M_bkt_for_elements(_M_element_count
1160 template<typename _Key, typename _Value,
1161 typename _Allocator, typename _ExtractKey, typename _Equal,
1162 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1163 bool __chc, bool __cit, bool __uk>
1165 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1166 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1167 _M_rehash(size_type __n)
1169 _Node** __new_array = _M_allocate_buckets(__n);
1172 for (size_type __i = 0; __i < _M_bucket_count; ++__i)
1173 while (_Node* __p = _M_buckets[__i])
1175 std::size_t __new_index = this->_M_bucket_index(__p, __n);
1176 _M_buckets[__i] = __p->_M_next;
1177 __p->_M_next = __new_array[__new_index];
1178 __new_array[__new_index] = __p;
1180 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
1181 _M_bucket_count = __n;
1182 _M_buckets = __new_array;
1186 // A failure here means that a hash function threw an exception.
1187 // We can't restore the previous state without calling the hash
1188 // function again, so the only sensible recovery is to delete
1190 _M_deallocate_nodes(__new_array, __n);
1191 _M_deallocate_buckets(__new_array, __n);
1192 _M_deallocate_nodes(_M_buckets, _M_bucket_count);
1193 _M_element_count = 0;
1194 __throw_exception_again;
1198 _GLIBCXX_END_NAMESPACE
1199 } // namespace std::tr1
1201 #endif // _TR1_HASHTABLE