1 // Internal header for TR1 unordered_set and unordered_map -*- C++ -*-
3 // Copyright (C) 2005, 2006 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
61 #include <bits/functexcept.h>
62 #include <tr1/type_traits> // For true_type and false_type
63 #include <tr1/hashtable_policy.h>
67 _GLIBCXX_BEGIN_NAMESPACE(tr1)
69 // Class template _Hashtable, class definition.
71 // Meaning of class template _Hashtable's template parameters
73 // _Key and _Value: arbitrary CopyConstructible types.
75 // _Allocator: an allocator type ([lib.allocator.requirements]) whose
76 // value type is Value. As a conforming extension, we allow for
77 // value type != Value.
79 // _ExtractKey: function object that takes a object of type Value
80 // and returns a value of type _Key.
82 // _Equal: function object that takes two objects of type k and returns
83 // a bool-like value that is true if the two objects are considered equal.
85 // _H1: the hash function. A unary function object with argument type
86 // Key and result type size_t. Return values should be distributed
87 // over the entire range [0, numeric_limits<size_t>:::max()].
89 // _H2: the range-hashing function (in the terminology of Tavori and
90 // Dreizin). A binary function object whose argument types and result
91 // type are all size_t. Given arguments r and N, the return value is
92 // in the range [0, N).
94 // _Hash: the ranged hash function (Tavori and Dreizin). A binary function
95 // whose argument types are _Key and size_t and whose result type is
96 // size_t. Given arguments k and N, the return value is in the range
97 // [0, N). Default: hash(k, N) = h2(h1(k), N). If _Hash is anything other
98 // than the default, _H1 and _H2 are ignored.
100 // _RehashPolicy: Policy class with three members, all of which govern
101 // the bucket count. _M_next_bkt(n) returns a bucket count no smaller
102 // than n. _M_bkt_for_elements(n) returns a bucket count appropriate
103 // for an element count of n. _M_need_rehash(n_bkt, n_elt, n_ins)
104 // determines whether, if the current bucket count is n_bkt and the
105 // current element count is n_elt, we need to increase the bucket
106 // count. If so, returns make_pair(true, n), where n is the new
107 // bucket count. If not, returns make_pair(false, <anything>).
109 // ??? Right now it is hard-wired that the number of buckets never
110 // shrinks. Should we allow _RehashPolicy to change that?
112 // __cache_hash_code: bool. true if we store the value of the hash
113 // function along with the value. This is a time-space tradeoff.
114 // Storing it may improve lookup speed by reducing the number of times
115 // we need to call the Equal function.
117 // __constant_iterators: bool. true if iterator and const_iterator are
118 // both constant iterator types. This is true for unordered_set and
119 // unordered_multiset, false for unordered_map and unordered_multimap.
121 // __unique_keys: bool. true if the return value of _Hashtable::count(k)
122 // is always at most one, false if it may be an arbitrary number. This
123 // true for unordered_set and unordered_map, false for unordered_multiset
124 // and unordered_multimap.
126 template<typename _Key, typename _Value, typename _Allocator,
127 typename _ExtractKey, typename _Equal,
128 typename _H1, typename _H2, typename _Hash,
129 typename _RehashPolicy,
130 bool __cache_hash_code,
131 bool __constant_iterators,
134 : public __detail::_Rehash_base<_RehashPolicy,
135 _Hashtable<_Key, _Value, _Allocator,
137 _Equal, _H1, _H2, _Hash,
140 __constant_iterators,
142 public __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
143 _H1, _H2, _Hash, __cache_hash_code>,
144 public __detail::_Map_base<_Key, _Value, _ExtractKey, __unique_keys,
145 _Hashtable<_Key, _Value, _Allocator,
147 _Equal, _H1, _H2, _Hash,
150 __constant_iterators,
154 typedef _Allocator allocator_type;
155 typedef _Value value_type;
156 typedef _Key key_type;
157 typedef _Equal key_equal;
158 // mapped_type, if present, comes from _Map_base.
159 // hasher, if present, comes from _Hash_code_base.
160 typedef typename _Allocator::difference_type difference_type;
161 typedef typename _Allocator::size_type size_type;
162 typedef typename _Allocator::reference reference;
163 typedef typename _Allocator::const_reference const_reference;
165 typedef __detail::_Node_iterator<value_type, __constant_iterators,
168 typedef __detail::_Node_const_iterator<value_type,
169 __constant_iterators,
171 const_local_iterator;
173 typedef __detail::_Hashtable_iterator<value_type, __constant_iterators,
176 typedef __detail::_Hashtable_const_iterator<value_type,
177 __constant_iterators,
181 template<typename _Key2, typename _Pair, typename _Hashtable>
182 friend struct __detail::_Map_base;
185 typedef __detail::_Hash_node<_Value, __cache_hash_code> _Node;
186 typedef typename _Allocator::template rebind<_Node>::other
187 _Node_allocator_type;
188 typedef typename _Allocator::template rebind<_Node*>::other
189 _Bucket_allocator_type;
191 typedef typename _Allocator::template rebind<_Value>::other
192 _Value_allocator_type;
194 _Node_allocator_type _M_node_allocator;
196 size_type _M_bucket_count;
197 size_type _M_element_count;
198 _RehashPolicy _M_rehash_policy;
201 _M_allocate_node(const value_type& __v);
204 _M_deallocate_node(_Node* __n);
207 _M_deallocate_nodes(_Node**, size_type);
210 _M_allocate_buckets(size_type __n);
213 _M_deallocate_buckets(_Node**, size_type __n);
216 // Constructor, destructor, assignment, swap
217 _Hashtable(size_type __bucket_hint,
218 const _H1&, const _H2&, const _Hash&,
219 const _Equal&, const _ExtractKey&,
220 const allocator_type&);
222 template<typename _InputIterator>
223 _Hashtable(_InputIterator __first, _InputIterator __last,
224 size_type __bucket_hint,
225 const _H1&, const _H2&, const _Hash&,
226 const _Equal&, const _ExtractKey&,
227 const allocator_type&);
229 _Hashtable(const _Hashtable&);
232 operator=(const _Hashtable&);
236 void swap(_Hashtable&);
238 // Basic container operations
242 iterator __i(_M_buckets);
243 if (!__i._M_cur_node)
244 __i._M_incr_bucket();
251 const_iterator __i(_M_buckets);
252 if (!__i._M_cur_node)
253 __i._M_incr_bucket();
259 { return iterator(_M_buckets + _M_bucket_count); }
263 { return const_iterator(_M_buckets + _M_bucket_count); }
267 { return _M_element_count; }
271 { return size() == 0; }
274 get_allocator() const
275 { return allocator_type(_M_node_allocator); }
277 _Value_allocator_type
278 _M_get_Value_allocator() const
279 { return _Value_allocator_type(_M_node_allocator); }
283 { return _M_get_Value_allocator().max_size(); }
288 { return this->_M_eq; }
290 // hash_function, if present, comes from _Hash_code_base.
295 { return _M_bucket_count; }
298 max_bucket_count() const
299 { return max_size(); }
302 bucket_size(size_type __n) const
303 { return std::distance(begin(__n), end(__n)); }
306 bucket(const key_type& __k) const
308 return this->_M_bucket_index(__k, this->_M_hash_code(__k),
314 { return local_iterator(_M_buckets[__n]); }
318 { return local_iterator(0); }
321 begin(size_type __n) const
322 { return const_local_iterator(_M_buckets[__n]); }
326 { return const_local_iterator(0); }
331 return static_cast<float>(size()) / static_cast<float>(bucket_count());
334 // max_load_factor, if present, comes from _Rehash_base.
336 // Generalization of max_load_factor. Extension, not found in TR1. Only
337 // useful if _RehashPolicy is something other than the default.
339 __rehash_policy() const
340 { return _M_rehash_policy; }
343 __rehash_policy(const _RehashPolicy&);
347 find(const key_type& __k);
350 find(const key_type& __k) const;
353 count(const key_type& __k) const;
355 std::pair<iterator, iterator>
356 equal_range(const key_type& __k);
358 std::pair<const_iterator, const_iterator>
359 equal_range(const key_type& __k) const;
361 private: // Find, insert and erase helper functions
362 // ??? This dispatching is a workaround for the fact that we don't
363 // have partial specialization of member templates; it would be
364 // better to just specialize insert on __unique_keys. There may be a
365 // cleaner workaround.
366 typedef typename __gnu_cxx::__conditional_type<__unique_keys,
367 std::pair<iterator, bool>, iterator>::__type
370 typedef typename __gnu_cxx::__conditional_type<__unique_keys,
371 std::_Select1st<_Insert_Return_Type>,
372 std::_Identity<_Insert_Return_Type>
377 _M_find_node(_Node*, const key_type&,
378 typename _Hashtable::_Hash_code_type) const;
381 _M_insert_bucket(const value_type&, size_type,
382 typename _Hashtable::_Hash_code_type);
384 std::pair<iterator, bool>
385 _M_insert(const value_type&, std::tr1::true_type);
388 _M_insert(const value_type&, std::tr1::false_type);
391 _M_erase_node(_Node*, _Node**);
396 insert(const value_type& __v)
397 { return _M_insert(__v, std::tr1::integral_constant<bool,
401 insert(iterator, const value_type& __v)
402 { return iterator(_Insert_Conv_Type()(this->insert(__v))); }
405 insert(const_iterator, const value_type& __v)
406 { return const_iterator(_Insert_Conv_Type()(this->insert(__v))); }
408 template<typename _InputIterator>
410 insert(_InputIterator __first, _InputIterator __last);
416 erase(const_iterator);
419 erase(const key_type&);
422 erase(iterator, iterator);
425 erase(const_iterator, const_iterator);
430 // Set number of buckets to be appropriate for container of n element.
431 void rehash(size_type __n);
434 // Unconditionally change size of bucket array to n.
435 void _M_rehash(size_type __n);
439 // Definitions of class template _Hashtable's out-of-line member functions.
440 template<typename _Key, typename _Value,
441 typename _Allocator, typename _ExtractKey, typename _Equal,
442 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
443 bool __chc, bool __cit, bool __uk>
444 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
445 _H1, _H2, _Hash, _RehashPolicy,
446 __chc, __cit, __uk>::_Node*
447 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
448 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
449 _M_allocate_node(const value_type& __v)
451 _Node* __n = _M_node_allocator.allocate(1);
454 _M_get_Value_allocator().construct(&__n->_M_v, __v);
460 _M_node_allocator.deallocate(__n, 1);
461 __throw_exception_again;
465 template<typename _Key, typename _Value,
466 typename _Allocator, typename _ExtractKey, typename _Equal,
467 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
468 bool __chc, bool __cit, bool __uk>
470 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
471 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
472 _M_deallocate_node(_Node* __n)
474 _M_get_Value_allocator().destroy(&__n->_M_v);
475 _M_node_allocator.deallocate(__n, 1);
478 template<typename _Key, typename _Value,
479 typename _Allocator, typename _ExtractKey, typename _Equal,
480 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
481 bool __chc, bool __cit, bool __uk>
483 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
484 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
485 _M_deallocate_nodes(_Node** __array, size_type __n)
487 for (size_type __i = 0; __i < __n; ++__i)
489 _Node* __p = __array[__i];
494 _M_deallocate_node(__tmp);
500 template<typename _Key, typename _Value,
501 typename _Allocator, typename _ExtractKey, typename _Equal,
502 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
503 bool __chc, bool __cit, bool __uk>
504 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
505 _H1, _H2, _Hash, _RehashPolicy,
506 __chc, __cit, __uk>::_Node**
507 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
508 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
509 _M_allocate_buckets(size_type __n)
511 _Bucket_allocator_type __alloc(_M_node_allocator);
513 // We allocate one extra bucket to hold a sentinel, an arbitrary
514 // non-null pointer. Iterator increment relies on this.
515 _Node** __p = __alloc.allocate(__n + 1);
516 std::fill(__p, __p + __n, (_Node*) 0);
517 __p[__n] = reinterpret_cast<_Node*>(0x1000);
521 template<typename _Key, typename _Value,
522 typename _Allocator, typename _ExtractKey, typename _Equal,
523 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
524 bool __chc, bool __cit, bool __uk>
526 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
527 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
528 _M_deallocate_buckets(_Node** __p, size_type __n)
530 _Bucket_allocator_type __alloc(_M_node_allocator);
531 __alloc.deallocate(__p, __n + 1);
534 template<typename _Key, typename _Value,
535 typename _Allocator, typename _ExtractKey, typename _Equal,
536 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
537 bool __chc, bool __cit, bool __uk>
538 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
539 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
540 _Hashtable(size_type __bucket_hint,
541 const _H1& __h1, const _H2& __h2, const _Hash& __h,
542 const _Equal& __eq, const _ExtractKey& __exk,
543 const allocator_type& __a)
544 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(),
545 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
546 _H1, _H2, _Hash, __chc>(__exk, __eq,
548 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(),
549 _M_node_allocator(__a),
554 _M_bucket_count = _M_rehash_policy._M_next_bkt(__bucket_hint);
555 _M_buckets = _M_allocate_buckets(_M_bucket_count);
558 template<typename _Key, typename _Value,
559 typename _Allocator, typename _ExtractKey, typename _Equal,
560 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
561 bool __chc, bool __cit, bool __uk>
562 template<typename _InputIterator>
563 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
564 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
565 _Hashtable(_InputIterator __f, _InputIterator __l,
566 size_type __bucket_hint,
567 const _H1& __h1, const _H2& __h2, const _Hash& __h,
568 const _Equal& __eq, const _ExtractKey& __exk,
569 const allocator_type& __a)
570 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(),
571 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
572 _H1, _H2, _Hash, __chc>(__exk, __eq,
574 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(),
575 _M_node_allocator(__a),
580 _M_bucket_count = std::max(_M_rehash_policy._M_next_bkt(__bucket_hint),
582 _M_bkt_for_elements(__detail::
585 _M_buckets = _M_allocate_buckets(_M_bucket_count);
588 for (; __f != __l; ++__f)
594 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
595 __throw_exception_again;
599 template<typename _Key, typename _Value,
600 typename _Allocator, typename _ExtractKey, typename _Equal,
601 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
602 bool __chc, bool __cit, bool __uk>
603 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
604 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
605 _Hashtable(const _Hashtable& __ht)
606 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(__ht),
607 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
608 _H1, _H2, _Hash, __chc>(__ht),
609 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(__ht),
610 _M_node_allocator(__ht._M_node_allocator),
611 _M_bucket_count(__ht._M_bucket_count),
612 _M_element_count(__ht._M_element_count),
613 _M_rehash_policy(__ht._M_rehash_policy)
615 _M_buckets = _M_allocate_buckets(_M_bucket_count);
618 for (size_type __i = 0; __i < __ht._M_bucket_count; ++__i)
620 _Node* __n = __ht._M_buckets[__i];
621 _Node** __tail = _M_buckets + __i;
624 *__tail = _M_allocate_node(__n->_M_v);
625 this->_M_copy_code(*__tail, __n);
626 __tail = &((*__tail)->_M_next);
634 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
635 __throw_exception_again;
639 template<typename _Key, typename _Value,
640 typename _Allocator, typename _ExtractKey, typename _Equal,
641 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
642 bool __chc, bool __cit, bool __uk>
643 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
644 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>&
645 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
646 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
647 operator=(const _Hashtable& __ht)
649 _Hashtable __tmp(__ht);
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>
658 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
659 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
663 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
666 template<typename _Key, typename _Value,
667 typename _Allocator, typename _ExtractKey, typename _Equal,
668 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
669 bool __chc, bool __cit, bool __uk>
671 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
672 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
673 swap(_Hashtable& __x)
675 // The only base class with member variables is hash_code_base. We
676 // define _Hash_code_base::_M_swap because different specializations
677 // have different members.
678 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
679 _H1, _H2, _Hash, __chc>::_M_swap(__x);
681 // _GLIBCXX_RESOLVE_LIB_DEFECTS
682 // 431. Swapping containers with unequal allocators.
683 std::__alloc_swap<_Node_allocator_type>::_S_do_it(_M_node_allocator,
684 __x._M_node_allocator);
686 std::swap(_M_rehash_policy, __x._M_rehash_policy);
687 std::swap(_M_buckets, __x._M_buckets);
688 std::swap(_M_bucket_count, __x._M_bucket_count);
689 std::swap(_M_element_count, __x._M_element_count);
692 template<typename _Key, typename _Value,
693 typename _Allocator, typename _ExtractKey, typename _Equal,
694 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
695 bool __chc, bool __cit, bool __uk>
697 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
698 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
699 __rehash_policy(const _RehashPolicy& __pol)
701 _M_rehash_policy = __pol;
702 size_type __n_bkt = __pol._M_bkt_for_elements(_M_element_count);
703 if (__n_bkt > _M_bucket_count)
707 template<typename _Key, typename _Value,
708 typename _Allocator, typename _ExtractKey, typename _Equal,
709 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
710 bool __chc, bool __cit, bool __uk>
711 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
712 _H1, _H2, _Hash, _RehashPolicy,
713 __chc, __cit, __uk>::iterator
714 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
715 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
716 find(const key_type& __k)
718 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
719 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
720 _Node* __p = _M_find_node(_M_buckets[__n], __k, __code);
721 return __p ? iterator(__p, _M_buckets + __n) : this->end();
724 template<typename _Key, typename _Value,
725 typename _Allocator, typename _ExtractKey, typename _Equal,
726 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
727 bool __chc, bool __cit, bool __uk>
728 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
729 _H1, _H2, _Hash, _RehashPolicy,
730 __chc, __cit, __uk>::const_iterator
731 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
732 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
733 find(const key_type& __k) const
735 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
736 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
737 _Node* __p = _M_find_node(_M_buckets[__n], __k, __code);
738 return __p ? const_iterator(__p, _M_buckets + __n) : this->end();
741 template<typename _Key, typename _Value,
742 typename _Allocator, typename _ExtractKey, typename _Equal,
743 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
744 bool __chc, bool __cit, bool __uk>
745 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
746 _H1, _H2, _Hash, _RehashPolicy,
747 __chc, __cit, __uk>::size_type
748 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
749 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
750 count(const key_type& __k) const
752 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
753 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
754 std::size_t __result = 0;
755 for (_Node* __p = _M_buckets[__n]; __p; __p = __p->_M_next)
756 if (this->_M_compare(__k, __code, __p))
761 template<typename _Key, typename _Value,
762 typename _Allocator, typename _ExtractKey, typename _Equal,
763 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
764 bool __chc, bool __cit, bool __uk>
765 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
766 _ExtractKey, _Equal, _H1,
767 _H2, _Hash, _RehashPolicy,
768 __chc, __cit, __uk>::iterator,
769 typename _Hashtable<_Key, _Value, _Allocator,
770 _ExtractKey, _Equal, _H1,
771 _H2, _Hash, _RehashPolicy,
772 __chc, __cit, __uk>::iterator>
773 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
774 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
775 equal_range(const key_type& __k)
777 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
778 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
779 _Node** __head = _M_buckets + __n;
780 _Node* __p = _M_find_node(*__head, __k, __code);
784 _Node* __p1 = __p->_M_next;
785 for (; __p1; __p1 = __p1->_M_next)
786 if (!this->_M_compare(__k, __code, __p1))
789 iterator __first(__p, __head);
790 iterator __last(__p1, __head);
792 __last._M_incr_bucket();
793 return std::make_pair(__first, __last);
796 return std::make_pair(this->end(), this->end());
799 template<typename _Key, typename _Value,
800 typename _Allocator, typename _ExtractKey, typename _Equal,
801 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
802 bool __chc, bool __cit, bool __uk>
803 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
804 _ExtractKey, _Equal, _H1,
805 _H2, _Hash, _RehashPolicy,
806 __chc, __cit, __uk>::const_iterator,
807 typename _Hashtable<_Key, _Value, _Allocator,
808 _ExtractKey, _Equal, _H1,
809 _H2, _Hash, _RehashPolicy,
810 __chc, __cit, __uk>::const_iterator>
811 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
812 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
813 equal_range(const key_type& __k) const
815 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
816 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
817 _Node** __head = _M_buckets + __n;
818 _Node* __p = _M_find_node(*__head, __k, __code);
822 _Node* __p1 = __p->_M_next;
823 for (; __p1; __p1 = __p1->_M_next)
824 if (!this->_M_compare(__k, __code, __p1))
827 const_iterator __first(__p, __head);
828 const_iterator __last(__p1, __head);
830 __last._M_incr_bucket();
831 return std::make_pair(__first, __last);
834 return std::make_pair(this->end(), this->end());
837 // Find the node whose key compares equal to k, beginning the search
838 // at p (usually the head of a bucket). Return nil if no node is found.
839 template<typename _Key, typename _Value,
840 typename _Allocator, typename _ExtractKey, typename _Equal,
841 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
842 bool __chc, bool __cit, bool __uk>
843 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey,
844 _Equal, _H1, _H2, _Hash, _RehashPolicy,
845 __chc, __cit, __uk>::_Node*
846 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
847 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
848 _M_find_node(_Node* __p, const key_type& __k,
849 typename _Hashtable::_Hash_code_type __code) const
851 for (; __p; __p = __p->_M_next)
852 if (this->_M_compare(__k, __code, __p))
857 // Insert v in bucket n (assumes no element with its key already present).
858 template<typename _Key, typename _Value,
859 typename _Allocator, typename _ExtractKey, typename _Equal,
860 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
861 bool __chc, bool __cit, bool __uk>
862 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
863 _H1, _H2, _Hash, _RehashPolicy,
864 __chc, __cit, __uk>::iterator
865 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
866 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
867 _M_insert_bucket(const value_type& __v, size_type __n,
868 typename _Hashtable::_Hash_code_type __code)
870 std::pair<bool, std::size_t> __do_rehash
871 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
872 _M_element_count, 1);
874 // Allocate the new node before doing the rehash so that we don't
875 // do a rehash if the allocation throws.
876 _Node* __new_node = _M_allocate_node(__v);
880 if (__do_rehash.first)
882 const key_type& __k = this->_M_extract(__v);
883 __n = this->_M_bucket_index(__k, __code, __do_rehash.second);
884 _M_rehash(__do_rehash.second);
887 __new_node->_M_next = _M_buckets[__n];
888 this->_M_store_code(__new_node, __code);
889 _M_buckets[__n] = __new_node;
891 return iterator(__new_node, _M_buckets + __n);
895 _M_deallocate_node(__new_node);
896 __throw_exception_again;
900 // Insert v if no element with its key is already present.
901 template<typename _Key, typename _Value,
902 typename _Allocator, typename _ExtractKey, typename _Equal,
903 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
904 bool __chc, bool __cit, bool __uk>
905 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
906 _ExtractKey, _Equal, _H1,
907 _H2, _Hash, _RehashPolicy,
908 __chc, __cit, __uk>::iterator, bool>
909 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
910 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
911 _M_insert(const value_type& __v, std::tr1::true_type)
913 const key_type& __k = this->_M_extract(__v);
914 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
915 size_type __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
917 if (_Node* __p = _M_find_node(_M_buckets[__n], __k, __code))
918 return std::make_pair(iterator(__p, _M_buckets + __n), false);
919 return std::make_pair(_M_insert_bucket(__v, __n, __code), true);
922 // Insert v unconditionally.
923 template<typename _Key, typename _Value,
924 typename _Allocator, typename _ExtractKey, typename _Equal,
925 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
926 bool __chc, bool __cit, bool __uk>
927 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
928 _H1, _H2, _Hash, _RehashPolicy,
929 __chc, __cit, __uk>::iterator
930 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
931 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
932 _M_insert(const value_type& __v, std::tr1::false_type)
934 std::pair<bool, std::size_t> __do_rehash
935 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
936 _M_element_count, 1);
937 if (__do_rehash.first)
938 _M_rehash(__do_rehash.second);
940 const key_type& __k = this->_M_extract(__v);
941 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
942 size_type __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
944 // First find the node, avoid leaking new_node if compare throws.
945 _Node* __prev = _M_find_node(_M_buckets[__n], __k, __code);
946 _Node* __new_node = _M_allocate_node(__v);
950 __new_node->_M_next = __prev->_M_next;
951 __prev->_M_next = __new_node;
955 __new_node->_M_next = _M_buckets[__n];
956 _M_buckets[__n] = __new_node;
958 this->_M_store_code(__new_node, __code);
961 return iterator(__new_node, _M_buckets + __n);
964 // For erase(iterator) and erase(const_iterator).
965 template<typename _Key, typename _Value,
966 typename _Allocator, typename _ExtractKey, typename _Equal,
967 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
968 bool __chc, bool __cit, bool __uk>
970 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
971 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
972 _M_erase_node(_Node* __p, _Node** __b)
976 *__b = __cur->_M_next;
979 _Node* __next = __cur->_M_next;
980 while (__next != __p)
983 __next = __cur->_M_next;
985 __cur->_M_next = __next->_M_next;
988 _M_deallocate_node(__p);
992 template<typename _Key, typename _Value,
993 typename _Allocator, typename _ExtractKey, typename _Equal,
994 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
995 bool __chc, bool __cit, bool __uk>
996 template<typename _InputIterator>
998 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
999 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1000 insert(_InputIterator __first, _InputIterator __last)
1002 size_type __n_elt = __detail::__distance_fw(__first, __last);
1003 std::pair<bool, std::size_t> __do_rehash
1004 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
1005 _M_element_count, __n_elt);
1006 if (__do_rehash.first)
1007 _M_rehash(__do_rehash.second);
1009 for (; __first != __last; ++__first)
1010 this->insert(*__first);
1013 template<typename _Key, typename _Value,
1014 typename _Allocator, typename _ExtractKey, typename _Equal,
1015 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1016 bool __chc, bool __cit, bool __uk>
1017 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1018 _H1, _H2, _Hash, _RehashPolicy,
1019 __chc, __cit, __uk>::iterator
1020 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1021 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1022 erase(iterator __it)
1024 iterator __result = __it;
1026 _M_erase_node(__it._M_cur_node, __it._M_cur_bucket);
1030 template<typename _Key, typename _Value,
1031 typename _Allocator, typename _ExtractKey, typename _Equal,
1032 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1033 bool __chc, bool __cit, bool __uk>
1034 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1035 _H1, _H2, _Hash, _RehashPolicy,
1036 __chc, __cit, __uk>::const_iterator
1037 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1038 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1039 erase(const_iterator __it)
1041 const_iterator __result = __it;
1043 _M_erase_node(__it._M_cur_node, __it._M_cur_bucket);
1047 template<typename _Key, typename _Value,
1048 typename _Allocator, typename _ExtractKey, typename _Equal,
1049 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1050 bool __chc, bool __cit, bool __uk>
1051 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1052 _H1, _H2, _Hash, _RehashPolicy,
1053 __chc, __cit, __uk>::size_type
1054 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1055 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1056 erase(const key_type& __k)
1058 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
1059 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
1060 size_type __result = 0;
1062 _Node** __slot = _M_buckets + __n;
1063 while (*__slot && !this->_M_compare(__k, __code, *__slot))
1064 __slot = &((*__slot)->_M_next);
1066 while (*__slot && this->_M_compare(__k, __code, *__slot))
1068 _Node* __p = *__slot;
1069 *__slot = __p->_M_next;
1070 _M_deallocate_node(__p);
1078 // ??? This could be optimized by taking advantage of the bucket
1079 // structure, but it's not clear that it's worth doing. It probably
1080 // wouldn't even be an optimization unless the load factor is large.
1081 template<typename _Key, typename _Value,
1082 typename _Allocator, typename _ExtractKey, typename _Equal,
1083 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1084 bool __chc, bool __cit, bool __uk>
1085 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1086 _H1, _H2, _Hash, _RehashPolicy,
1087 __chc, __cit, __uk>::iterator
1088 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1089 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1090 erase(iterator __first, iterator __last)
1092 while (__first != __last)
1093 __first = this->erase(__first);
1097 template<typename _Key, typename _Value,
1098 typename _Allocator, typename _ExtractKey, typename _Equal,
1099 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1100 bool __chc, bool __cit, bool __uk>
1101 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1102 _H1, _H2, _Hash, _RehashPolicy,
1103 __chc, __cit, __uk>::const_iterator
1104 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1105 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1106 erase(const_iterator __first, const_iterator __last)
1108 while (__first != __last)
1109 __first = this->erase(__first);
1113 template<typename _Key, typename _Value,
1114 typename _Allocator, typename _ExtractKey, typename _Equal,
1115 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1116 bool __chc, bool __cit, bool __uk>
1118 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1119 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1122 _M_deallocate_nodes(_M_buckets, _M_bucket_count);
1123 _M_element_count = 0;
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 rehash(size_type __n)
1135 _M_rehash(std::max(_M_rehash_policy._M_next_bkt(__n),
1136 _M_rehash_policy._M_bkt_for_elements(_M_element_count
1140 template<typename _Key, typename _Value,
1141 typename _Allocator, typename _ExtractKey, typename _Equal,
1142 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1143 bool __chc, bool __cit, bool __uk>
1145 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1146 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1147 _M_rehash(size_type __n)
1149 _Node** __new_array = _M_allocate_buckets(__n);
1152 for (size_type __i = 0; __i < _M_bucket_count; ++__i)
1153 while (_Node* __p = _M_buckets[__i])
1155 std::size_t __new_index = this->_M_bucket_index(__p, __n);
1156 _M_buckets[__i] = __p->_M_next;
1157 __p->_M_next = __new_array[__new_index];
1158 __new_array[__new_index] = __p;
1160 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
1161 _M_bucket_count = __n;
1162 _M_buckets = __new_array;
1166 // A failure here means that a hash function threw an exception.
1167 // We can't restore the previous state without calling the hash
1168 // function again, so the only sensible recovery is to delete
1170 _M_deallocate_nodes(__new_array, __n);
1171 _M_deallocate_buckets(__new_array, __n);
1172 _M_deallocate_nodes(_M_buckets, _M_bucket_count);
1173 _M_element_count = 0;
1174 __throw_exception_again;
1178 _GLIBCXX_END_NAMESPACE
1179 } // namespace std::tr1
1181 #endif // _TR1_HASHTABLE