1 // Internal policy header for TR1 unordered_set and unordered_map -*- 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 tr1_impl/hashtable_policy.h
26 * This is an internal header file, included by other library headers.
27 * You should not attempt to use it directly.
36 // Helper function: return distance(first, last) for forward
37 // iterators, or 0 for input iterators.
38 template<class _Iterator>
39 inline typename std::iterator_traits<_Iterator>::difference_type
40 __distance_fw(_Iterator __first, _Iterator __last,
41 std::input_iterator_tag)
44 template<class _Iterator>
45 inline typename std::iterator_traits<_Iterator>::difference_type
46 __distance_fw(_Iterator __first, _Iterator __last,
47 std::forward_iterator_tag)
48 { return std::distance(__first, __last); }
50 template<class _Iterator>
51 inline typename std::iterator_traits<_Iterator>::difference_type
52 __distance_fw(_Iterator __first, _Iterator __last)
54 typedef typename std::iterator_traits<_Iterator>::iterator_category _Tag;
55 return __distance_fw(__first, __last, _Tag());
58 template<typename _RAIter, typename _Tp>
60 __lower_bound(_RAIter __first, _RAIter __last, const _Tp& __val)
62 typedef typename std::iterator_traits<_RAIter>::difference_type _DType;
64 _DType __len = __last - __first;
67 _DType __half = __len >> 1;
68 _RAIter __middle = __first + __half;
69 if (*__middle < __val)
73 __len = __len - __half - 1;
81 // Auxiliary types used for all instantiations of _Hashtable: nodes
84 // Nodes, used to wrap elements stored in the hash table. A policy
85 // template parameter of class template _Hashtable controls whether
86 // nodes also store a hash code. In some cases (e.g. strings) this
87 // may be a performance win.
88 template<typename _Value, bool __cache_hash_code>
91 template<typename _Value>
92 struct _Hash_node<_Value, true>
95 std::size_t _M_hash_code;
99 template<typename _Value>
100 struct _Hash_node<_Value, false>
106 // Local iterators, used to iterate within a bucket but not between
108 template<typename _Value, bool __cache>
109 struct _Node_iterator_base
111 _Node_iterator_base(_Hash_node<_Value, __cache>* __p)
116 { _M_cur = _M_cur->_M_next; }
118 _Hash_node<_Value, __cache>* _M_cur;
121 template<typename _Value, bool __cache>
123 operator==(const _Node_iterator_base<_Value, __cache>& __x,
124 const _Node_iterator_base<_Value, __cache>& __y)
125 { return __x._M_cur == __y._M_cur; }
127 template<typename _Value, bool __cache>
129 operator!=(const _Node_iterator_base<_Value, __cache>& __x,
130 const _Node_iterator_base<_Value, __cache>& __y)
131 { return __x._M_cur != __y._M_cur; }
133 template<typename _Value, bool __constant_iterators, bool __cache>
134 struct _Node_iterator
135 : public _Node_iterator_base<_Value, __cache>
137 typedef _Value value_type;
139 __gnu_cxx::__conditional_type<__constant_iterators,
140 const _Value*, _Value*>::__type
143 __gnu_cxx::__conditional_type<__constant_iterators,
144 const _Value&, _Value&>::__type
146 typedef std::ptrdiff_t difference_type;
147 typedef std::forward_iterator_tag iterator_category;
150 : _Node_iterator_base<_Value, __cache>(0) { }
153 _Node_iterator(_Hash_node<_Value, __cache>* __p)
154 : _Node_iterator_base<_Value, __cache>(__p) { }
158 { return this->_M_cur->_M_v; }
162 { return &this->_M_cur->_M_v; }
174 _Node_iterator __tmp(*this);
180 template<typename _Value, bool __constant_iterators, bool __cache>
181 struct _Node_const_iterator
182 : public _Node_iterator_base<_Value, __cache>
184 typedef _Value value_type;
185 typedef const _Value* pointer;
186 typedef const _Value& reference;
187 typedef std::ptrdiff_t difference_type;
188 typedef std::forward_iterator_tag iterator_category;
190 _Node_const_iterator()
191 : _Node_iterator_base<_Value, __cache>(0) { }
194 _Node_const_iterator(_Hash_node<_Value, __cache>* __p)
195 : _Node_iterator_base<_Value, __cache>(__p) { }
197 _Node_const_iterator(const _Node_iterator<_Value, __constant_iterators,
199 : _Node_iterator_base<_Value, __cache>(__x._M_cur) { }
203 { return this->_M_cur->_M_v; }
207 { return &this->_M_cur->_M_v; }
209 _Node_const_iterator&
219 _Node_const_iterator __tmp(*this);
225 template<typename _Value, bool __cache>
226 struct _Hashtable_iterator_base
228 _Hashtable_iterator_base(_Hash_node<_Value, __cache>* __node,
229 _Hash_node<_Value, __cache>** __bucket)
230 : _M_cur_node(__node), _M_cur_bucket(__bucket) { }
235 _M_cur_node = _M_cur_node->_M_next;
243 _Hash_node<_Value, __cache>* _M_cur_node;
244 _Hash_node<_Value, __cache>** _M_cur_bucket;
247 // Global iterators, used for arbitrary iteration within a hash
248 // table. Larger and more expensive than local iterators.
249 template<typename _Value, bool __cache>
251 _Hashtable_iterator_base<_Value, __cache>::
256 // This loop requires the bucket array to have a non-null sentinel.
257 while (!*_M_cur_bucket)
259 _M_cur_node = *_M_cur_bucket;
262 template<typename _Value, bool __cache>
264 operator==(const _Hashtable_iterator_base<_Value, __cache>& __x,
265 const _Hashtable_iterator_base<_Value, __cache>& __y)
266 { return __x._M_cur_node == __y._M_cur_node; }
268 template<typename _Value, bool __cache>
270 operator!=(const _Hashtable_iterator_base<_Value, __cache>& __x,
271 const _Hashtable_iterator_base<_Value, __cache>& __y)
272 { return __x._M_cur_node != __y._M_cur_node; }
274 template<typename _Value, bool __constant_iterators, bool __cache>
275 struct _Hashtable_iterator
276 : public _Hashtable_iterator_base<_Value, __cache>
278 typedef _Value value_type;
280 __gnu_cxx::__conditional_type<__constant_iterators,
281 const _Value*, _Value*>::__type
284 __gnu_cxx::__conditional_type<__constant_iterators,
285 const _Value&, _Value&>::__type
287 typedef std::ptrdiff_t difference_type;
288 typedef std::forward_iterator_tag iterator_category;
290 _Hashtable_iterator()
291 : _Hashtable_iterator_base<_Value, __cache>(0, 0) { }
293 _Hashtable_iterator(_Hash_node<_Value, __cache>* __p,
294 _Hash_node<_Value, __cache>** __b)
295 : _Hashtable_iterator_base<_Value, __cache>(__p, __b) { }
298 _Hashtable_iterator(_Hash_node<_Value, __cache>** __b)
299 : _Hashtable_iterator_base<_Value, __cache>(*__b, __b) { }
303 { return this->_M_cur_node->_M_v; }
307 { return &this->_M_cur_node->_M_v; }
319 _Hashtable_iterator __tmp(*this);
325 template<typename _Value, bool __constant_iterators, bool __cache>
326 struct _Hashtable_const_iterator
327 : public _Hashtable_iterator_base<_Value, __cache>
329 typedef _Value value_type;
330 typedef const _Value* pointer;
331 typedef const _Value& reference;
332 typedef std::ptrdiff_t difference_type;
333 typedef std::forward_iterator_tag iterator_category;
335 _Hashtable_const_iterator()
336 : _Hashtable_iterator_base<_Value, __cache>(0, 0) { }
338 _Hashtable_const_iterator(_Hash_node<_Value, __cache>* __p,
339 _Hash_node<_Value, __cache>** __b)
340 : _Hashtable_iterator_base<_Value, __cache>(__p, __b) { }
343 _Hashtable_const_iterator(_Hash_node<_Value, __cache>** __b)
344 : _Hashtable_iterator_base<_Value, __cache>(*__b, __b) { }
346 _Hashtable_const_iterator(const _Hashtable_iterator<_Value,
347 __constant_iterators, __cache>& __x)
348 : _Hashtable_iterator_base<_Value, __cache>(__x._M_cur_node,
349 __x._M_cur_bucket) { }
353 { return this->_M_cur_node->_M_v; }
357 { return &this->_M_cur_node->_M_v; }
359 _Hashtable_const_iterator&
366 _Hashtable_const_iterator
369 _Hashtable_const_iterator __tmp(*this);
376 // Many of class template _Hashtable's template parameters are policy
377 // classes. These are defaults for the policies.
379 // Default range hashing function: use division to fold a large number
380 // into the range [0, N).
381 struct _Mod_range_hashing
383 typedef std::size_t first_argument_type;
384 typedef std::size_t second_argument_type;
385 typedef std::size_t result_type;
388 operator()(first_argument_type __num, second_argument_type __den) const
389 { return __num % __den; }
392 // Default ranged hash function H. In principle it should be a
393 // function object composed from objects of type H1 and H2 such that
394 // h(k, N) = h2(h1(k), N), but that would mean making extra copies of
395 // h1 and h2. So instead we'll just use a tag to tell class template
396 // hashtable to do that composition.
397 struct _Default_ranged_hash { };
399 // Default value for rehash policy. Bucket size is (usually) the
400 // smallest prime that keeps the load factor small enough.
401 struct _Prime_rehash_policy
403 _Prime_rehash_policy(float __z = 1.0)
404 : _M_max_load_factor(__z), _M_growth_factor(2.f), _M_next_resize(0) { }
407 max_load_factor() const
408 { return _M_max_load_factor; }
410 // Return a bucket size no smaller than n.
412 _M_next_bkt(std::size_t __n) const;
414 // Return a bucket count appropriate for n elements
416 _M_bkt_for_elements(std::size_t __n) const;
418 // __n_bkt is current bucket count, __n_elt is current element count,
419 // and __n_ins is number of elements to be inserted. Do we need to
420 // increase bucket count? If so, return make_pair(true, n), where n
421 // is the new bucket count. If not, return make_pair(false, 0).
422 std::pair<bool, std::size_t>
423 _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
424 std::size_t __n_ins) const;
426 enum { _S_n_primes = sizeof(unsigned long) != 8 ? 256 : 256 + 48 };
428 float _M_max_load_factor;
429 float _M_growth_factor;
430 mutable std::size_t _M_next_resize;
433 extern const unsigned long __prime_list[];
435 // XXX This is a hack. There's no good reason for any of
436 // _Prime_rehash_policy's member functions to be inline.
438 // Return a prime no smaller than n.
440 _Prime_rehash_policy::
441 _M_next_bkt(std::size_t __n) const
443 const unsigned long* __p = __lower_bound(__prime_list, __prime_list
446 static_cast<std::size_t>(__builtin_ceil(*__p * _M_max_load_factor));
450 // Return the smallest prime p such that alpha p >= n, where alpha
451 // is the load factor.
453 _Prime_rehash_policy::
454 _M_bkt_for_elements(std::size_t __n) const
456 const float __min_bkts = __n / _M_max_load_factor;
457 const unsigned long* __p = __lower_bound(__prime_list, __prime_list
458 + _S_n_primes, __min_bkts);
460 static_cast<std::size_t>(__builtin_ceil(*__p * _M_max_load_factor));
464 // Finds the smallest prime p such that alpha p > __n_elt + __n_ins.
465 // If p > __n_bkt, return make_pair(true, p); otherwise return
466 // make_pair(false, 0). In principle this isn't very different from
467 // _M_bkt_for_elements.
469 // The only tricky part is that we're caching the element count at
470 // which we need to rehash, so we don't have to do a floating-point
471 // multiply for every insertion.
473 inline std::pair<bool, std::size_t>
474 _Prime_rehash_policy::
475 _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
476 std::size_t __n_ins) const
478 if (__n_elt + __n_ins > _M_next_resize)
480 float __min_bkts = ((float(__n_ins) + float(__n_elt))
481 / _M_max_load_factor);
482 if (__min_bkts > __n_bkt)
484 __min_bkts = std::max(__min_bkts, _M_growth_factor * __n_bkt);
485 const unsigned long* __p =
486 __lower_bound(__prime_list, __prime_list + _S_n_primes,
488 _M_next_resize = static_cast<std::size_t>
489 (__builtin_ceil(*__p * _M_max_load_factor));
490 return std::make_pair(true, *__p);
494 _M_next_resize = static_cast<std::size_t>
495 (__builtin_ceil(__n_bkt * _M_max_load_factor));
496 return std::make_pair(false, 0);
500 return std::make_pair(false, 0);
503 // Base classes for std::tr1::_Hashtable. We define these base
504 // classes because in some cases we want to do different things
505 // depending on the value of a policy class. In some cases the
506 // policy class affects which member functions and nested typedefs
507 // are defined; we handle that by specializing base class templates.
508 // Several of the base class templates need to access other members
509 // of class template _Hashtable, so we use the "curiously recurring
510 // template pattern" for them.
512 // class template _Map_base. If the hashtable has a value type of the
513 // form pair<T1, T2> and a key extraction policy that returns the
514 // first part of the pair, the hashtable gets a mapped_type typedef.
515 // If it satisfies those criteria and also has unique keys, then it
516 // also gets an operator[].
517 template<typename _Key, typename _Value, typename _Ex, bool __unique,
519 struct _Map_base { };
521 template<typename _Key, typename _Pair, typename _Hashtable>
522 struct _Map_base<_Key, _Pair, std::_Select1st<_Pair>, false, _Hashtable>
524 typedef typename _Pair::second_type mapped_type;
527 template<typename _Key, typename _Pair, typename _Hashtable>
528 struct _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>
530 typedef typename _Pair::second_type mapped_type;
533 operator[](const _Key& __k);
536 template<typename _Key, typename _Pair, typename _Hashtable>
537 typename _Map_base<_Key, _Pair, std::_Select1st<_Pair>,
538 true, _Hashtable>::mapped_type&
539 _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::
540 operator[](const _Key& __k)
542 _Hashtable* __h = static_cast<_Hashtable*>(this);
543 typename _Hashtable::_Hash_code_type __code = __h->_M_hash_code(__k);
544 std::size_t __n = __h->_M_bucket_index(__k, __code,
545 __h->_M_bucket_count);
547 typename _Hashtable::_Node* __p =
548 __h->_M_find_node(__h->_M_buckets[__n], __k, __code);
550 return __h->_M_insert_bucket(std::make_pair(__k, mapped_type()),
551 __n, __code)->second;
552 return (__p->_M_v).second;
555 // class template _Rehash_base. Give hashtable the max_load_factor
556 // functions iff the rehash policy is _Prime_rehash_policy.
557 template<typename _RehashPolicy, typename _Hashtable>
558 struct _Rehash_base { };
560 template<typename _Hashtable>
561 struct _Rehash_base<_Prime_rehash_policy, _Hashtable>
564 max_load_factor() const
566 const _Hashtable* __this = static_cast<const _Hashtable*>(this);
567 return __this->__rehash_policy().max_load_factor();
571 max_load_factor(float __z)
573 _Hashtable* __this = static_cast<_Hashtable*>(this);
574 __this->__rehash_policy(_Prime_rehash_policy(__z));
578 // Class template _Hash_code_base. Encapsulates two policy issues that
579 // aren't quite orthogonal.
580 // (1) the difference between using a ranged hash function and using
581 // the combination of a hash function and a range-hashing function.
582 // In the former case we don't have such things as hash codes, so
583 // we have a dummy type as placeholder.
584 // (2) Whether or not we cache hash codes. Caching hash codes is
585 // meaningless if we have a ranged hash function.
586 // We also put the key extraction and equality comparison function
587 // objects here, for convenience.
589 // Primary template: unused except as a hook for specializations.
590 template<typename _Key, typename _Value,
591 typename _ExtractKey, typename _Equal,
592 typename _H1, typename _H2, typename _Hash,
593 bool __cache_hash_code>
594 struct _Hash_code_base;
596 // Specialization: ranged hash function, no caching hash codes. H1
597 // and H2 are provided but ignored. We define a dummy hash code type.
598 template<typename _Key, typename _Value,
599 typename _ExtractKey, typename _Equal,
600 typename _H1, typename _H2, typename _Hash>
601 struct _Hash_code_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2,
605 _Hash_code_base(const _ExtractKey& __ex, const _Equal& __eq,
606 const _H1&, const _H2&, const _Hash& __h)
607 : _M_extract(__ex), _M_eq(__eq), _M_ranged_hash(__h) { }
609 typedef void* _Hash_code_type;
612 _M_hash_code(const _Key& __key) const
616 _M_bucket_index(const _Key& __k, _Hash_code_type,
617 std::size_t __n) const
618 { return _M_ranged_hash(__k, __n); }
621 _M_bucket_index(const _Hash_node<_Value, false>* __p,
622 std::size_t __n) const
623 { return _M_ranged_hash(_M_extract(__p->_M_v), __n); }
626 _M_compare(const _Key& __k, _Hash_code_type,
627 _Hash_node<_Value, false>* __n) const
628 { return _M_eq(__k, _M_extract(__n->_M_v)); }
631 _M_store_code(_Hash_node<_Value, false>*, _Hash_code_type) const
635 _M_copy_code(_Hash_node<_Value, false>*,
636 const _Hash_node<_Value, false>*) const
640 _M_swap(_Hash_code_base& __x)
642 std::swap(_M_extract, __x._M_extract);
643 std::swap(_M_eq, __x._M_eq);
644 std::swap(_M_ranged_hash, __x._M_ranged_hash);
648 _ExtractKey _M_extract;
650 _Hash _M_ranged_hash;
654 // No specialization for ranged hash function while caching hash codes.
655 // That combination is meaningless, and trying to do it is an error.
658 // Specialization: ranged hash function, cache hash codes. This
659 // combination is meaningless, so we provide only a declaration
660 // and no definition.
661 template<typename _Key, typename _Value,
662 typename _ExtractKey, typename _Equal,
663 typename _H1, typename _H2, typename _Hash>
664 struct _Hash_code_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2,
667 // Specialization: hash function and range-hashing function, no
668 // caching of hash codes. H is provided but ignored. Provides
669 // typedef and accessor required by TR1.
670 template<typename _Key, typename _Value,
671 typename _ExtractKey, typename _Equal,
672 typename _H1, typename _H2>
673 struct _Hash_code_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2,
674 _Default_ranged_hash, false>
679 hash_function() const
683 _Hash_code_base(const _ExtractKey& __ex, const _Equal& __eq,
684 const _H1& __h1, const _H2& __h2,
685 const _Default_ranged_hash&)
686 : _M_extract(__ex), _M_eq(__eq), _M_h1(__h1), _M_h2(__h2) { }
688 typedef std::size_t _Hash_code_type;
691 _M_hash_code(const _Key& __k) const
692 { return _M_h1(__k); }
695 _M_bucket_index(const _Key&, _Hash_code_type __c,
696 std::size_t __n) const
697 { return _M_h2(__c, __n); }
700 _M_bucket_index(const _Hash_node<_Value, false>* __p,
701 std::size_t __n) const
702 { return _M_h2(_M_h1(_M_extract(__p->_M_v)), __n); }
705 _M_compare(const _Key& __k, _Hash_code_type,
706 _Hash_node<_Value, false>* __n) const
707 { return _M_eq(__k, _M_extract(__n->_M_v)); }
710 _M_store_code(_Hash_node<_Value, false>*, _Hash_code_type) const
714 _M_copy_code(_Hash_node<_Value, false>*,
715 const _Hash_node<_Value, false>*) const
719 _M_swap(_Hash_code_base& __x)
721 std::swap(_M_extract, __x._M_extract);
722 std::swap(_M_eq, __x._M_eq);
723 std::swap(_M_h1, __x._M_h1);
724 std::swap(_M_h2, __x._M_h2);
728 _ExtractKey _M_extract;
734 // Specialization: hash function and range-hashing function,
735 // caching hash codes. H is provided but ignored. Provides
736 // typedef and accessor required by TR1.
737 template<typename _Key, typename _Value,
738 typename _ExtractKey, typename _Equal,
739 typename _H1, typename _H2>
740 struct _Hash_code_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2,
741 _Default_ranged_hash, true>
746 hash_function() const
750 _Hash_code_base(const _ExtractKey& __ex, const _Equal& __eq,
751 const _H1& __h1, const _H2& __h2,
752 const _Default_ranged_hash&)
753 : _M_extract(__ex), _M_eq(__eq), _M_h1(__h1), _M_h2(__h2) { }
755 typedef std::size_t _Hash_code_type;
758 _M_hash_code(const _Key& __k) const
759 { return _M_h1(__k); }
762 _M_bucket_index(const _Key&, _Hash_code_type __c,
763 std::size_t __n) const
764 { return _M_h2(__c, __n); }
767 _M_bucket_index(const _Hash_node<_Value, true>* __p,
768 std::size_t __n) const
769 { return _M_h2(__p->_M_hash_code, __n); }
772 _M_compare(const _Key& __k, _Hash_code_type __c,
773 _Hash_node<_Value, true>* __n) const
774 { return __c == __n->_M_hash_code && _M_eq(__k, _M_extract(__n->_M_v)); }
777 _M_store_code(_Hash_node<_Value, true>* __n, _Hash_code_type __c) const
778 { __n->_M_hash_code = __c; }
781 _M_copy_code(_Hash_node<_Value, true>* __to,
782 const _Hash_node<_Value, true>* __from) const
783 { __to->_M_hash_code = __from->_M_hash_code; }
786 _M_swap(_Hash_code_base& __x)
788 std::swap(_M_extract, __x._M_extract);
789 std::swap(_M_eq, __x._M_eq);
790 std::swap(_M_h1, __x._M_h1);
791 std::swap(_M_h2, __x._M_h2);
795 _ExtractKey _M_extract;
800 } // namespace __detail