1 |
35 |
ultra_embe |
// Internal policy header for unordered_set and unordered_map -*- C++ -*-
|
2 |
|
|
|
3 |
|
|
// Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc.
|
4 |
|
|
//
|
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)
|
9 |
|
|
// any later version.
|
10 |
|
|
|
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.
|
15 |
|
|
|
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.
|
19 |
|
|
|
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/>.
|
24 |
|
|
|
25 |
|
|
/** @file bits/hashtable_policy.h
|
26 |
|
|
* This is an internal header file, included by other library headers.
|
27 |
|
|
* Do not attempt to use it directly.
|
28 |
|
|
* @headername{unordered_map,unordered_set}
|
29 |
|
|
*/
|
30 |
|
|
|
31 |
|
|
#ifndef _HASHTABLE_POLICY_H
|
32 |
|
|
#define _HASHTABLE_POLICY_H 1
|
33 |
|
|
|
34 |
|
|
namespace std _GLIBCXX_VISIBILITY(default)
|
35 |
|
|
{
|
36 |
|
|
_GLIBCXX_BEGIN_NAMESPACE_VERSION
|
37 |
|
|
|
38 |
|
|
template<typename _Key, typename _Value, typename _Alloc,
|
39 |
|
|
typename _ExtractKey, typename _Equal,
|
40 |
|
|
typename _H1, typename _H2, typename _Hash,
|
41 |
|
|
typename _RehashPolicy, typename _Traits>
|
42 |
|
|
class _Hashtable;
|
43 |
|
|
|
44 |
|
|
_GLIBCXX_END_NAMESPACE_VERSION
|
45 |
|
|
|
46 |
|
|
namespace __detail
|
47 |
|
|
{
|
48 |
|
|
_GLIBCXX_BEGIN_NAMESPACE_VERSION
|
49 |
|
|
|
50 |
|
|
/**
|
51 |
|
|
* @defgroup hashtable-detail Base and Implementation Classes
|
52 |
|
|
* @ingroup unordered_associative_containers
|
53 |
|
|
* @{
|
54 |
|
|
*/
|
55 |
|
|
template<typename _Key, typename _Value,
|
56 |
|
|
typename _ExtractKey, typename _Equal,
|
57 |
|
|
typename _H1, typename _H2, typename _Hash, typename _Traits>
|
58 |
|
|
struct _Hashtable_base;
|
59 |
|
|
|
60 |
|
|
// Helper function: return distance(first, last) for forward
|
61 |
|
|
// iterators, or 0 for input iterators.
|
62 |
|
|
template<class _Iterator>
|
63 |
|
|
inline typename std::iterator_traits<_Iterator>::difference_type
|
64 |
|
|
__distance_fw(_Iterator __first, _Iterator __last,
|
65 |
|
|
std::input_iterator_tag)
|
66 |
|
|
{ return 0; }
|
67 |
|
|
|
68 |
|
|
template<class _Iterator>
|
69 |
|
|
inline typename std::iterator_traits<_Iterator>::difference_type
|
70 |
|
|
__distance_fw(_Iterator __first, _Iterator __last,
|
71 |
|
|
std::forward_iterator_tag)
|
72 |
|
|
{ return std::distance(__first, __last); }
|
73 |
|
|
|
74 |
|
|
template<class _Iterator>
|
75 |
|
|
inline typename std::iterator_traits<_Iterator>::difference_type
|
76 |
|
|
__distance_fw(_Iterator __first, _Iterator __last)
|
77 |
|
|
{
|
78 |
|
|
typedef typename std::iterator_traits<_Iterator>::iterator_category _Tag;
|
79 |
|
|
return __distance_fw(__first, __last, _Tag());
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
// Helper type used to detect whether the hash functor is noexcept.
|
83 |
|
|
template <typename _Key, typename _Hash>
|
84 |
|
|
struct __is_noexcept_hash : std::integral_constant<bool,
|
85 |
|
|
noexcept(declval<const _Hash&>()(declval<const _Key&>()))>
|
86 |
|
|
{ };
|
87 |
|
|
|
88 |
|
|
struct _Identity
|
89 |
|
|
{
|
90 |
|
|
template<typename _Tp>
|
91 |
|
|
_Tp&&
|
92 |
|
|
operator()(_Tp&& __x) const
|
93 |
|
|
{ return std::forward<_Tp>(__x); }
|
94 |
|
|
};
|
95 |
|
|
|
96 |
|
|
struct _Select1st
|
97 |
|
|
{
|
98 |
|
|
template<typename _Tp>
|
99 |
|
|
auto
|
100 |
|
|
operator()(_Tp&& __x) const
|
101 |
|
|
-> decltype(std::get<0>(std::forward<_Tp>(__x)))
|
102 |
|
|
{ return std::get<0>(std::forward<_Tp>(__x)); }
|
103 |
|
|
};
|
104 |
|
|
|
105 |
|
|
// Auxiliary types used for all instantiations of _Hashtable nodes
|
106 |
|
|
// and iterators.
|
107 |
|
|
|
108 |
|
|
/**
|
109 |
|
|
* struct _Hashtable_traits
|
110 |
|
|
*
|
111 |
|
|
* Important traits for hash tables.
|
112 |
|
|
*
|
113 |
|
|
* @tparam _Cache_hash_code Boolean value. True if the value of
|
114 |
|
|
* the hash function is stored along with the value. This is a
|
115 |
|
|
* time-space tradeoff. Storing it may improve lookup speed by
|
116 |
|
|
* reducing the number of times we need to call the _Equal
|
117 |
|
|
* function.
|
118 |
|
|
*
|
119 |
|
|
* @tparam _Constant_iterators Boolean value. True if iterator and
|
120 |
|
|
* const_iterator are both constant iterator types. This is true
|
121 |
|
|
* for unordered_set and unordered_multiset, false for
|
122 |
|
|
* unordered_map and unordered_multimap.
|
123 |
|
|
*
|
124 |
|
|
* @tparam _Unique_keys Boolean value. True if the return value
|
125 |
|
|
* of _Hashtable::count(k) is always at most one, false if it may
|
126 |
|
|
* be an arbitrary number. This is true for unordered_set and
|
127 |
|
|
* unordered_map, false for unordered_multiset and
|
128 |
|
|
* unordered_multimap.
|
129 |
|
|
*/
|
130 |
|
|
template<bool _Cache_hash_code, bool _Constant_iterators, bool _Unique_keys>
|
131 |
|
|
struct _Hashtable_traits
|
132 |
|
|
{
|
133 |
|
|
template<bool _Cond>
|
134 |
|
|
using __bool_constant = integral_constant<bool, _Cond>;
|
135 |
|
|
|
136 |
|
|
using __hash_cached = __bool_constant<_Cache_hash_code>;
|
137 |
|
|
using __constant_iterators = __bool_constant<_Constant_iterators>;
|
138 |
|
|
using __unique_keys = __bool_constant<_Unique_keys>;
|
139 |
|
|
};
|
140 |
|
|
|
141 |
|
|
/**
|
142 |
|
|
* struct _Hash_node_base
|
143 |
|
|
*
|
144 |
|
|
* Nodes, used to wrap elements stored in the hash table. A policy
|
145 |
|
|
* template parameter of class template _Hashtable controls whether
|
146 |
|
|
* nodes also store a hash code. In some cases (e.g. strings) this
|
147 |
|
|
* may be a performance win.
|
148 |
|
|
*/
|
149 |
|
|
struct _Hash_node_base
|
150 |
|
|
{
|
151 |
|
|
_Hash_node_base* _M_nxt;
|
152 |
|
|
|
153 |
|
|
_Hash_node_base() : _M_nxt() { }
|
154 |
|
|
|
155 |
|
|
_Hash_node_base(_Hash_node_base* __next) : _M_nxt(__next) { }
|
156 |
|
|
};
|
157 |
|
|
|
158 |
|
|
/**
|
159 |
|
|
* Primary template struct _Hash_node.
|
160 |
|
|
*/
|
161 |
|
|
template<typename _Value, bool _Cache_hash_code>
|
162 |
|
|
struct _Hash_node;
|
163 |
|
|
|
164 |
|
|
/**
|
165 |
|
|
* Specialization for nodes with caches, struct _Hash_node.
|
166 |
|
|
*
|
167 |
|
|
* Base class is __detail::_Hash_node_base.
|
168 |
|
|
*/
|
169 |
|
|
template<typename _Value>
|
170 |
|
|
struct _Hash_node<_Value, true> : _Hash_node_base
|
171 |
|
|
{
|
172 |
|
|
_Value _M_v;
|
173 |
|
|
std::size_t _M_hash_code;
|
174 |
|
|
|
175 |
|
|
template<typename... _Args>
|
176 |
|
|
_Hash_node(_Args&&... __args)
|
177 |
|
|
: _M_v(std::forward<_Args>(__args)...), _M_hash_code() { }
|
178 |
|
|
|
179 |
|
|
_Hash_node*
|
180 |
|
|
_M_next() const { return static_cast<_Hash_node*>(_M_nxt); }
|
181 |
|
|
};
|
182 |
|
|
|
183 |
|
|
/**
|
184 |
|
|
* Specialization for nodes without caches, struct _Hash_node.
|
185 |
|
|
*
|
186 |
|
|
* Base class is __detail::_Hash_node_base.
|
187 |
|
|
*/
|
188 |
|
|
template<typename _Value>
|
189 |
|
|
struct _Hash_node<_Value, false> : _Hash_node_base
|
190 |
|
|
{
|
191 |
|
|
_Value _M_v;
|
192 |
|
|
|
193 |
|
|
template<typename... _Args>
|
194 |
|
|
_Hash_node(_Args&&... __args)
|
195 |
|
|
: _M_v(std::forward<_Args>(__args)...) { }
|
196 |
|
|
|
197 |
|
|
_Hash_node*
|
198 |
|
|
_M_next() const { return static_cast<_Hash_node*>(_M_nxt); }
|
199 |
|
|
};
|
200 |
|
|
|
201 |
|
|
/// Base class for node iterators.
|
202 |
|
|
template<typename _Value, bool _Cache_hash_code>
|
203 |
|
|
struct _Node_iterator_base
|
204 |
|
|
{
|
205 |
|
|
typedef _Hash_node<_Value, _Cache_hash_code> __node_type;
|
206 |
|
|
|
207 |
|
|
__node_type* _M_cur;
|
208 |
|
|
|
209 |
|
|
_Node_iterator_base(__node_type* __p)
|
210 |
|
|
: _M_cur(__p) { }
|
211 |
|
|
|
212 |
|
|
void
|
213 |
|
|
_M_incr()
|
214 |
|
|
{ _M_cur = _M_cur->_M_next(); }
|
215 |
|
|
};
|
216 |
|
|
|
217 |
|
|
template<typename _Value, bool _Cache_hash_code>
|
218 |
|
|
inline bool
|
219 |
|
|
operator==(const _Node_iterator_base<_Value, _Cache_hash_code>& __x,
|
220 |
|
|
const _Node_iterator_base<_Value, _Cache_hash_code >& __y)
|
221 |
|
|
{ return __x._M_cur == __y._M_cur; }
|
222 |
|
|
|
223 |
|
|
template<typename _Value, bool _Cache_hash_code>
|
224 |
|
|
inline bool
|
225 |
|
|
operator!=(const _Node_iterator_base<_Value, _Cache_hash_code>& __x,
|
226 |
|
|
const _Node_iterator_base<_Value, _Cache_hash_code>& __y)
|
227 |
|
|
{ return __x._M_cur != __y._M_cur; }
|
228 |
|
|
|
229 |
|
|
/// Node iterators, used to iterate through all the hashtable.
|
230 |
|
|
template<typename _Value, bool __constant_iterators, bool __cache>
|
231 |
|
|
struct _Node_iterator
|
232 |
|
|
: public _Node_iterator_base<_Value, __cache>
|
233 |
|
|
{
|
234 |
|
|
private:
|
235 |
|
|
using __base_type = _Node_iterator_base<_Value, __cache>;
|
236 |
|
|
using __node_type = typename __base_type::__node_type;
|
237 |
|
|
|
238 |
|
|
public:
|
239 |
|
|
typedef _Value value_type;
|
240 |
|
|
typedef std::ptrdiff_t difference_type;
|
241 |
|
|
typedef std::forward_iterator_tag iterator_category;
|
242 |
|
|
|
243 |
|
|
using pointer = typename std::conditional<__constant_iterators,
|
244 |
|
|
const _Value*, _Value*>::type;
|
245 |
|
|
|
246 |
|
|
using reference = typename std::conditional<__constant_iterators,
|
247 |
|
|
const _Value&, _Value&>::type;
|
248 |
|
|
|
249 |
|
|
_Node_iterator()
|
250 |
|
|
: __base_type(0) { }
|
251 |
|
|
|
252 |
|
|
explicit
|
253 |
|
|
_Node_iterator(__node_type* __p)
|
254 |
|
|
: __base_type(__p) { }
|
255 |
|
|
|
256 |
|
|
reference
|
257 |
|
|
operator*() const
|
258 |
|
|
{ return this->_M_cur->_M_v; }
|
259 |
|
|
|
260 |
|
|
pointer
|
261 |
|
|
operator->() const
|
262 |
|
|
{ return std::__addressof(this->_M_cur->_M_v); }
|
263 |
|
|
|
264 |
|
|
_Node_iterator&
|
265 |
|
|
operator++()
|
266 |
|
|
{
|
267 |
|
|
this->_M_incr();
|
268 |
|
|
return *this;
|
269 |
|
|
}
|
270 |
|
|
|
271 |
|
|
_Node_iterator
|
272 |
|
|
operator++(int)
|
273 |
|
|
{
|
274 |
|
|
_Node_iterator __tmp(*this);
|
275 |
|
|
this->_M_incr();
|
276 |
|
|
return __tmp;
|
277 |
|
|
}
|
278 |
|
|
};
|
279 |
|
|
|
280 |
|
|
/// Node const_iterators, used to iterate through all the hashtable.
|
281 |
|
|
template<typename _Value, bool __constant_iterators, bool __cache>
|
282 |
|
|
struct _Node_const_iterator
|
283 |
|
|
: public _Node_iterator_base<_Value, __cache>
|
284 |
|
|
{
|
285 |
|
|
private:
|
286 |
|
|
using __base_type = _Node_iterator_base<_Value, __cache>;
|
287 |
|
|
using __node_type = typename __base_type::__node_type;
|
288 |
|
|
|
289 |
|
|
public:
|
290 |
|
|
typedef _Value value_type;
|
291 |
|
|
typedef std::ptrdiff_t difference_type;
|
292 |
|
|
typedef std::forward_iterator_tag iterator_category;
|
293 |
|
|
|
294 |
|
|
typedef const _Value* pointer;
|
295 |
|
|
typedef const _Value& reference;
|
296 |
|
|
|
297 |
|
|
_Node_const_iterator()
|
298 |
|
|
: __base_type(0) { }
|
299 |
|
|
|
300 |
|
|
explicit
|
301 |
|
|
_Node_const_iterator(__node_type* __p)
|
302 |
|
|
: __base_type(__p) { }
|
303 |
|
|
|
304 |
|
|
_Node_const_iterator(const _Node_iterator<_Value, __constant_iterators,
|
305 |
|
|
__cache>& __x)
|
306 |
|
|
: __base_type(__x._M_cur) { }
|
307 |
|
|
|
308 |
|
|
reference
|
309 |
|
|
operator*() const
|
310 |
|
|
{ return this->_M_cur->_M_v; }
|
311 |
|
|
|
312 |
|
|
pointer
|
313 |
|
|
operator->() const
|
314 |
|
|
{ return std::__addressof(this->_M_cur->_M_v); }
|
315 |
|
|
|
316 |
|
|
_Node_const_iterator&
|
317 |
|
|
operator++()
|
318 |
|
|
{
|
319 |
|
|
this->_M_incr();
|
320 |
|
|
return *this;
|
321 |
|
|
}
|
322 |
|
|
|
323 |
|
|
_Node_const_iterator
|
324 |
|
|
operator++(int)
|
325 |
|
|
{
|
326 |
|
|
_Node_const_iterator __tmp(*this);
|
327 |
|
|
this->_M_incr();
|
328 |
|
|
return __tmp;
|
329 |
|
|
}
|
330 |
|
|
};
|
331 |
|
|
|
332 |
|
|
// Many of class template _Hashtable's template parameters are policy
|
333 |
|
|
// classes. These are defaults for the policies.
|
334 |
|
|
|
335 |
|
|
/// Default range hashing function: use division to fold a large number
|
336 |
|
|
/// into the range [0, N).
|
337 |
|
|
struct _Mod_range_hashing
|
338 |
|
|
{
|
339 |
|
|
typedef std::size_t first_argument_type;
|
340 |
|
|
typedef std::size_t second_argument_type;
|
341 |
|
|
typedef std::size_t result_type;
|
342 |
|
|
|
343 |
|
|
result_type
|
344 |
|
|
operator()(first_argument_type __num, second_argument_type __den) const
|
345 |
|
|
{ return __num % __den; }
|
346 |
|
|
};
|
347 |
|
|
|
348 |
|
|
/// Default ranged hash function H. In principle it should be a
|
349 |
|
|
/// function object composed from objects of type H1 and H2 such that
|
350 |
|
|
/// h(k, N) = h2(h1(k), N), but that would mean making extra copies of
|
351 |
|
|
/// h1 and h2. So instead we'll just use a tag to tell class template
|
352 |
|
|
/// hashtable to do that composition.
|
353 |
|
|
struct _Default_ranged_hash { };
|
354 |
|
|
|
355 |
|
|
/// Default value for rehash policy. Bucket size is (usually) the
|
356 |
|
|
/// smallest prime that keeps the load factor small enough.
|
357 |
|
|
struct _Prime_rehash_policy
|
358 |
|
|
{
|
359 |
|
|
_Prime_rehash_policy(float __z = 1.0)
|
360 |
|
|
: _M_max_load_factor(__z), _M_next_resize(0) { }
|
361 |
|
|
|
362 |
|
|
float
|
363 |
|
|
max_load_factor() const noexcept
|
364 |
|
|
{ return _M_max_load_factor; }
|
365 |
|
|
|
366 |
|
|
// Return a bucket size no smaller than n.
|
367 |
|
|
std::size_t
|
368 |
|
|
_M_next_bkt(std::size_t __n) const;
|
369 |
|
|
|
370 |
|
|
// Return a bucket count appropriate for n elements
|
371 |
|
|
std::size_t
|
372 |
|
|
_M_bkt_for_elements(std::size_t __n) const;
|
373 |
|
|
|
374 |
|
|
// __n_bkt is current bucket count, __n_elt is current element count,
|
375 |
|
|
// and __n_ins is number of elements to be inserted. Do we need to
|
376 |
|
|
// increase bucket count? If so, return make_pair(true, n), where n
|
377 |
|
|
// is the new bucket count. If not, return make_pair(false, 0).
|
378 |
|
|
std::pair<bool, std::size_t>
|
379 |
|
|
_M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
|
380 |
|
|
std::size_t __n_ins) const;
|
381 |
|
|
|
382 |
|
|
typedef std::size_t _State;
|
383 |
|
|
|
384 |
|
|
_State
|
385 |
|
|
_M_state() const
|
386 |
|
|
{ return _M_next_resize; }
|
387 |
|
|
|
388 |
|
|
void
|
389 |
|
|
_M_reset(_State __state)
|
390 |
|
|
{ _M_next_resize = __state; }
|
391 |
|
|
|
392 |
|
|
enum { _S_n_primes = sizeof(unsigned long) != 8 ? 256 : 256 + 48 };
|
393 |
|
|
|
394 |
|
|
static const std::size_t _S_growth_factor = 2;
|
395 |
|
|
|
396 |
|
|
float _M_max_load_factor;
|
397 |
|
|
mutable std::size_t _M_next_resize;
|
398 |
|
|
};
|
399 |
|
|
|
400 |
|
|
extern const unsigned long __prime_list[];
|
401 |
|
|
|
402 |
|
|
// XXX This is a hack. There's no good reason for any of
|
403 |
|
|
// _Prime_rehash_policy's member functions to be inline.
|
404 |
|
|
|
405 |
|
|
// Return a prime no smaller than n.
|
406 |
|
|
inline std::size_t
|
407 |
|
|
_Prime_rehash_policy::
|
408 |
|
|
_M_next_bkt(std::size_t __n) const
|
409 |
|
|
{
|
410 |
|
|
// Optimize lookups involving the first elements of __prime_list.
|
411 |
|
|
// (useful to speed-up, eg, constructors)
|
412 |
|
|
static const unsigned char __fast_bkt[12]
|
413 |
|
|
= { 2, 2, 2, 3, 5, 5, 7, 7, 11, 11, 11, 11 };
|
414 |
|
|
|
415 |
|
|
if (__n <= 11)
|
416 |
|
|
{
|
417 |
|
|
_M_next_resize
|
418 |
|
|
= __builtin_ceil(__fast_bkt[__n]
|
419 |
|
|
* (long double)_M_max_load_factor);
|
420 |
|
|
return __fast_bkt[__n];
|
421 |
|
|
}
|
422 |
|
|
|
423 |
|
|
const unsigned long* __next_bkt
|
424 |
|
|
= std::lower_bound(__prime_list + 5, __prime_list + _S_n_primes,
|
425 |
|
|
__n);
|
426 |
|
|
_M_next_resize
|
427 |
|
|
= __builtin_ceil(*__next_bkt * (long double)_M_max_load_factor);
|
428 |
|
|
return *__next_bkt;
|
429 |
|
|
}
|
430 |
|
|
|
431 |
|
|
// Return the smallest integer p such that alpha p >= n, where alpha
|
432 |
|
|
// is the load factor.
|
433 |
|
|
inline std::size_t
|
434 |
|
|
_Prime_rehash_policy::
|
435 |
|
|
_M_bkt_for_elements(std::size_t __n) const
|
436 |
|
|
{ return __builtin_ceil(__n / (long double)_M_max_load_factor); }
|
437 |
|
|
|
438 |
|
|
// Finds the smallest prime p such that alpha p > __n_elt + __n_ins.
|
439 |
|
|
// If p > __n_bkt, return make_pair(true, p); otherwise return
|
440 |
|
|
// make_pair(false, 0). In principle this isn't very different from
|
441 |
|
|
// _M_bkt_for_elements.
|
442 |
|
|
|
443 |
|
|
// The only tricky part is that we're caching the element count at
|
444 |
|
|
// which we need to rehash, so we don't have to do a floating-point
|
445 |
|
|
// multiply for every insertion.
|
446 |
|
|
|
447 |
|
|
inline std::pair<bool, std::size_t>
|
448 |
|
|
_Prime_rehash_policy::
|
449 |
|
|
_M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
|
450 |
|
|
std::size_t __n_ins) const
|
451 |
|
|
{
|
452 |
|
|
if (__n_elt + __n_ins >= _M_next_resize)
|
453 |
|
|
{
|
454 |
|
|
long double __min_bkts = (__n_elt + __n_ins)
|
455 |
|
|
/ (long double)_M_max_load_factor;
|
456 |
|
|
if (__min_bkts >= __n_bkt)
|
457 |
|
|
return std::make_pair(true,
|
458 |
|
|
_M_next_bkt(std::max<std::size_t>(__builtin_floor(__min_bkts) + 1,
|
459 |
|
|
__n_bkt * _S_growth_factor)));
|
460 |
|
|
else
|
461 |
|
|
{
|
462 |
|
|
_M_next_resize
|
463 |
|
|
= __builtin_floor(__n_bkt * (long double)_M_max_load_factor);
|
464 |
|
|
return std::make_pair(false, 0);
|
465 |
|
|
}
|
466 |
|
|
}
|
467 |
|
|
else
|
468 |
|
|
return std::make_pair(false, 0);
|
469 |
|
|
}
|
470 |
|
|
|
471 |
|
|
// Base classes for std::_Hashtable. We define these base classes
|
472 |
|
|
// because in some cases we want to do different things depending on
|
473 |
|
|
// the value of a policy class. In some cases the policy class
|
474 |
|
|
// affects which member functions and nested typedefs are defined;
|
475 |
|
|
// we handle that by specializing base class templates. Several of
|
476 |
|
|
// the base class templates need to access other members of class
|
477 |
|
|
// template _Hashtable, so we use a variant of the "Curiously
|
478 |
|
|
// Recurring Template Pattern" (CRTP) technique.
|
479 |
|
|
|
480 |
|
|
/**
|
481 |
|
|
* Primary class template _Map_base.
|
482 |
|
|
*
|
483 |
|
|
* If the hashtable has a value type of the form pair<T1, T2> and a
|
484 |
|
|
* key extraction policy (_ExtractKey) that returns the first part
|
485 |
|
|
* of the pair, the hashtable gets a mapped_type typedef. If it
|
486 |
|
|
* satisfies those criteria and also has unique keys, then it also
|
487 |
|
|
* gets an operator[].
|
488 |
|
|
*/
|
489 |
|
|
template<typename _Key, typename _Value, typename _Alloc,
|
490 |
|
|
typename _ExtractKey, typename _Equal,
|
491 |
|
|
typename _H1, typename _H2, typename _Hash,
|
492 |
|
|
typename _RehashPolicy, typename _Traits,
|
493 |
|
|
bool _Unique_keys = _Traits::__unique_keys::value>
|
494 |
|
|
struct _Map_base { };
|
495 |
|
|
|
496 |
|
|
/// Partial specialization, __unique_keys set to false.
|
497 |
|
|
template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
|
498 |
|
|
typename _H1, typename _H2, typename _Hash,
|
499 |
|
|
typename _RehashPolicy, typename _Traits>
|
500 |
|
|
struct _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
|
501 |
|
|
_H1, _H2, _Hash, _RehashPolicy, _Traits, false>
|
502 |
|
|
{
|
503 |
|
|
using mapped_type = typename std::tuple_element<1, _Pair>::type;
|
504 |
|
|
};
|
505 |
|
|
|
506 |
|
|
/// Partial specialization, __unique_keys set to true.
|
507 |
|
|
template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
|
508 |
|
|
typename _H1, typename _H2, typename _Hash,
|
509 |
|
|
typename _RehashPolicy, typename _Traits>
|
510 |
|
|
struct _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
|
511 |
|
|
_H1, _H2, _Hash, _RehashPolicy, _Traits, true>
|
512 |
|
|
{
|
513 |
|
|
private:
|
514 |
|
|
using __hashtable_base = __detail::_Hashtable_base<_Key, _Pair,
|
515 |
|
|
_Select1st,
|
516 |
|
|
_Equal, _H1, _H2, _Hash,
|
517 |
|
|
_Traits>;
|
518 |
|
|
|
519 |
|
|
using __hashtable = _Hashtable<_Key, _Pair, _Alloc,
|
520 |
|
|
_Select1st, _Equal,
|
521 |
|
|
_H1, _H2, _Hash, _RehashPolicy, _Traits>;
|
522 |
|
|
|
523 |
|
|
using __hash_code = typename __hashtable_base::__hash_code;
|
524 |
|
|
using __node_type = typename __hashtable_base::__node_type;
|
525 |
|
|
|
526 |
|
|
public:
|
527 |
|
|
using key_type = typename __hashtable_base::key_type;
|
528 |
|
|
using iterator = typename __hashtable_base::iterator;
|
529 |
|
|
using mapped_type = typename std::tuple_element<1, _Pair>::type;
|
530 |
|
|
|
531 |
|
|
mapped_type&
|
532 |
|
|
operator[](const key_type& __k);
|
533 |
|
|
|
534 |
|
|
mapped_type&
|
535 |
|
|
operator[](key_type&& __k);
|
536 |
|
|
|
537 |
|
|
// _GLIBCXX_RESOLVE_LIB_DEFECTS
|
538 |
|
|
// DR 761. unordered_map needs an at() member function.
|
539 |
|
|
mapped_type&
|
540 |
|
|
at(const key_type& __k);
|
541 |
|
|
|
542 |
|
|
const mapped_type&
|
543 |
|
|
at(const key_type& __k) const;
|
544 |
|
|
};
|
545 |
|
|
|
546 |
|
|
template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
|
547 |
|
|
typename _H1, typename _H2, typename _Hash,
|
548 |
|
|
typename _RehashPolicy, typename _Traits>
|
549 |
|
|
typename _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
|
550 |
|
|
_H1, _H2, _Hash, _RehashPolicy, _Traits, true>
|
551 |
|
|
::mapped_type&
|
552 |
|
|
_Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
|
553 |
|
|
_H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
|
554 |
|
|
operator[](const key_type& __k)
|
555 |
|
|
{
|
556 |
|
|
__hashtable* __h = static_cast<__hashtable*>(this);
|
557 |
|
|
__hash_code __code = __h->_M_hash_code(__k);
|
558 |
|
|
std::size_t __n = __h->_M_bucket_index(__k, __code);
|
559 |
|
|
__node_type* __p = __h->_M_find_node(__n, __k, __code);
|
560 |
|
|
|
561 |
|
|
if (!__p)
|
562 |
|
|
{
|
563 |
|
|
__p = __h->_M_allocate_node(std::piecewise_construct,
|
564 |
|
|
std::tuple<const key_type&>(__k),
|
565 |
|
|
std::tuple<>());
|
566 |
|
|
return __h->_M_insert_unique_node(__n, __code, __p)->second;
|
567 |
|
|
}
|
568 |
|
|
|
569 |
|
|
return (__p->_M_v).second;
|
570 |
|
|
}
|
571 |
|
|
|
572 |
|
|
template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
|
573 |
|
|
typename _H1, typename _H2, typename _Hash,
|
574 |
|
|
typename _RehashPolicy, typename _Traits>
|
575 |
|
|
typename _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
|
576 |
|
|
_H1, _H2, _Hash, _RehashPolicy, _Traits, true>
|
577 |
|
|
::mapped_type&
|
578 |
|
|
_Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
|
579 |
|
|
_H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
|
580 |
|
|
operator[](key_type&& __k)
|
581 |
|
|
{
|
582 |
|
|
__hashtable* __h = static_cast<__hashtable*>(this);
|
583 |
|
|
__hash_code __code = __h->_M_hash_code(__k);
|
584 |
|
|
std::size_t __n = __h->_M_bucket_index(__k, __code);
|
585 |
|
|
__node_type* __p = __h->_M_find_node(__n, __k, __code);
|
586 |
|
|
|
587 |
|
|
if (!__p)
|
588 |
|
|
{
|
589 |
|
|
__p = __h->_M_allocate_node(std::piecewise_construct,
|
590 |
|
|
std::forward_as_tuple(std::move(__k)),
|
591 |
|
|
std::tuple<>());
|
592 |
|
|
return __h->_M_insert_unique_node(__n, __code, __p)->second;
|
593 |
|
|
}
|
594 |
|
|
|
595 |
|
|
return (__p->_M_v).second;
|
596 |
|
|
}
|
597 |
|
|
|
598 |
|
|
template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
|
599 |
|
|
typename _H1, typename _H2, typename _Hash,
|
600 |
|
|
typename _RehashPolicy, typename _Traits>
|
601 |
|
|
typename _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
|
602 |
|
|
_H1, _H2, _Hash, _RehashPolicy, _Traits, true>
|
603 |
|
|
::mapped_type&
|
604 |
|
|
_Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
|
605 |
|
|
_H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
|
606 |
|
|
at(const key_type& __k)
|
607 |
|
|
{
|
608 |
|
|
__hashtable* __h = static_cast<__hashtable*>(this);
|
609 |
|
|
__hash_code __code = __h->_M_hash_code(__k);
|
610 |
|
|
std::size_t __n = __h->_M_bucket_index(__k, __code);
|
611 |
|
|
__node_type* __p = __h->_M_find_node(__n, __k, __code);
|
612 |
|
|
|
613 |
|
|
if (!__p)
|
614 |
|
|
__throw_out_of_range(__N("_Map_base::at"));
|
615 |
|
|
return (__p->_M_v).second;
|
616 |
|
|
}
|
617 |
|
|
|
618 |
|
|
template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
|
619 |
|
|
typename _H1, typename _H2, typename _Hash,
|
620 |
|
|
typename _RehashPolicy, typename _Traits>
|
621 |
|
|
const typename _Map_base<_Key, _Pair, _Alloc, _Select1st,
|
622 |
|
|
_Equal, _H1, _H2, _Hash, _RehashPolicy,
|
623 |
|
|
_Traits, true>::mapped_type&
|
624 |
|
|
_Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
|
625 |
|
|
_H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
|
626 |
|
|
at(const key_type& __k) const
|
627 |
|
|
{
|
628 |
|
|
const __hashtable* __h = static_cast<const __hashtable*>(this);
|
629 |
|
|
__hash_code __code = __h->_M_hash_code(__k);
|
630 |
|
|
std::size_t __n = __h->_M_bucket_index(__k, __code);
|
631 |
|
|
__node_type* __p = __h->_M_find_node(__n, __k, __code);
|
632 |
|
|
|
633 |
|
|
if (!__p)
|
634 |
|
|
__throw_out_of_range(__N("_Map_base::at"));
|
635 |
|
|
return (__p->_M_v).second;
|
636 |
|
|
}
|
637 |
|
|
|
638 |
|
|
/**
|
639 |
|
|
* Primary class template _Insert_base.
|
640 |
|
|
*
|
641 |
|
|
* insert member functions appropriate to all _Hashtables.
|
642 |
|
|
*/
|
643 |
|
|
template<typename _Key, typename _Value, typename _Alloc,
|
644 |
|
|
typename _ExtractKey, typename _Equal,
|
645 |
|
|
typename _H1, typename _H2, typename _Hash,
|
646 |
|
|
typename _RehashPolicy, typename _Traits>
|
647 |
|
|
struct _Insert_base
|
648 |
|
|
{
|
649 |
|
|
using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
|
650 |
|
|
_Equal, _H1, _H2, _Hash,
|
651 |
|
|
_RehashPolicy, _Traits>;
|
652 |
|
|
|
653 |
|
|
using __hashtable_base = _Hashtable_base<_Key, _Value, _ExtractKey,
|
654 |
|
|
_Equal, _H1, _H2, _Hash,
|
655 |
|
|
_Traits>;
|
656 |
|
|
|
657 |
|
|
using value_type = typename __hashtable_base::value_type;
|
658 |
|
|
using iterator = typename __hashtable_base::iterator;
|
659 |
|
|
using const_iterator = typename __hashtable_base::const_iterator;
|
660 |
|
|
using size_type = typename __hashtable_base::size_type;
|
661 |
|
|
|
662 |
|
|
using __unique_keys = typename __hashtable_base::__unique_keys;
|
663 |
|
|
using __ireturn_type = typename __hashtable_base::__ireturn_type;
|
664 |
|
|
using __iconv_type = typename __hashtable_base::__iconv_type;
|
665 |
|
|
|
666 |
|
|
__hashtable&
|
667 |
|
|
_M_conjure_hashtable()
|
668 |
|
|
{ return *(static_cast<__hashtable*>(this)); }
|
669 |
|
|
|
670 |
|
|
__ireturn_type
|
671 |
|
|
insert(const value_type& __v)
|
672 |
|
|
{
|
673 |
|
|
__hashtable& __h = _M_conjure_hashtable();
|
674 |
|
|
return __h._M_insert(__v, __unique_keys());
|
675 |
|
|
}
|
676 |
|
|
|
677 |
|
|
iterator
|
678 |
|
|
insert(const_iterator, const value_type& __v)
|
679 |
|
|
{ return __iconv_type()(insert(__v)); }
|
680 |
|
|
|
681 |
|
|
void
|
682 |
|
|
insert(initializer_list<value_type> __l)
|
683 |
|
|
{ this->insert(__l.begin(), __l.end()); }
|
684 |
|
|
|
685 |
|
|
template<typename _InputIterator>
|
686 |
|
|
void
|
687 |
|
|
insert(_InputIterator __first, _InputIterator __last);
|
688 |
|
|
};
|
689 |
|
|
|
690 |
|
|
template<typename _Key, typename _Value, typename _Alloc,
|
691 |
|
|
typename _ExtractKey, typename _Equal,
|
692 |
|
|
typename _H1, typename _H2, typename _Hash,
|
693 |
|
|
typename _RehashPolicy, typename _Traits>
|
694 |
|
|
template<typename _InputIterator>
|
695 |
|
|
void
|
696 |
|
|
_Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
|
697 |
|
|
_RehashPolicy, _Traits>::
|
698 |
|
|
insert(_InputIterator __first, _InputIterator __last)
|
699 |
|
|
{
|
700 |
|
|
using __rehash_type = typename __hashtable::__rehash_type;
|
701 |
|
|
using __rehash_state = typename __hashtable::__rehash_state;
|
702 |
|
|
using pair_type = std::pair<bool, std::size_t>;
|
703 |
|
|
|
704 |
|
|
size_type __n_elt = __detail::__distance_fw(__first, __last);
|
705 |
|
|
|
706 |
|
|
__hashtable& __h = _M_conjure_hashtable();
|
707 |
|
|
__rehash_type& __rehash = __h._M_rehash_policy;
|
708 |
|
|
const __rehash_state& __saved_state = __rehash._M_state();
|
709 |
|
|
pair_type __do_rehash = __rehash._M_need_rehash(__h._M_bucket_count,
|
710 |
|
|
__h._M_element_count,
|
711 |
|
|
__n_elt);
|
712 |
|
|
|
713 |
|
|
if (__do_rehash.first)
|
714 |
|
|
__h._M_rehash(__do_rehash.second, __saved_state);
|
715 |
|
|
|
716 |
|
|
for (; __first != __last; ++__first)
|
717 |
|
|
this->insert(*__first);
|
718 |
|
|
}
|
719 |
|
|
|
720 |
|
|
/**
|
721 |
|
|
* Primary class template _Insert.
|
722 |
|
|
*
|
723 |
|
|
* Select insert member functions appropriate to _Hashtable policy choices.
|
724 |
|
|
*/
|
725 |
|
|
template<typename _Key, typename _Value, typename _Alloc,
|
726 |
|
|
typename _ExtractKey, typename _Equal,
|
727 |
|
|
typename _H1, typename _H2, typename _Hash,
|
728 |
|
|
typename _RehashPolicy, typename _Traits,
|
729 |
|
|
bool _Constant_iterators = _Traits::__constant_iterators::value,
|
730 |
|
|
bool _Unique_keys = _Traits::__unique_keys::value>
|
731 |
|
|
struct _Insert;
|
732 |
|
|
|
733 |
|
|
/// Specialization.
|
734 |
|
|
template<typename _Key, typename _Value, typename _Alloc,
|
735 |
|
|
typename _ExtractKey, typename _Equal,
|
736 |
|
|
typename _H1, typename _H2, typename _Hash,
|
737 |
|
|
typename _RehashPolicy, typename _Traits>
|
738 |
|
|
struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
|
739 |
|
|
_RehashPolicy, _Traits, true, true>
|
740 |
|
|
: public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
741 |
|
|
_H1, _H2, _Hash, _RehashPolicy, _Traits>
|
742 |
|
|
{
|
743 |
|
|
using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey,
|
744 |
|
|
_Equal, _H1, _H2, _Hash,
|
745 |
|
|
_RehashPolicy, _Traits>;
|
746 |
|
|
using value_type = typename __base_type::value_type;
|
747 |
|
|
using iterator = typename __base_type::iterator;
|
748 |
|
|
using const_iterator = typename __base_type::const_iterator;
|
749 |
|
|
|
750 |
|
|
using __unique_keys = typename __base_type::__unique_keys;
|
751 |
|
|
using __hashtable = typename __base_type::__hashtable;
|
752 |
|
|
|
753 |
|
|
using __base_type::insert;
|
754 |
|
|
|
755 |
|
|
std::pair<iterator, bool>
|
756 |
|
|
insert(value_type&& __v)
|
757 |
|
|
{
|
758 |
|
|
__hashtable& __h = this->_M_conjure_hashtable();
|
759 |
|
|
return __h._M_insert(std::move(__v), __unique_keys());
|
760 |
|
|
}
|
761 |
|
|
|
762 |
|
|
iterator
|
763 |
|
|
insert(const_iterator, value_type&& __v)
|
764 |
|
|
{ return insert(std::move(__v)).first; }
|
765 |
|
|
};
|
766 |
|
|
|
767 |
|
|
/// Specialization.
|
768 |
|
|
template<typename _Key, typename _Value, typename _Alloc,
|
769 |
|
|
typename _ExtractKey, typename _Equal,
|
770 |
|
|
typename _H1, typename _H2, typename _Hash,
|
771 |
|
|
typename _RehashPolicy, typename _Traits>
|
772 |
|
|
struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
|
773 |
|
|
_RehashPolicy, _Traits, true, false>
|
774 |
|
|
: public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
775 |
|
|
_H1, _H2, _Hash, _RehashPolicy, _Traits>
|
776 |
|
|
{
|
777 |
|
|
using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey,
|
778 |
|
|
_Equal, _H1, _H2, _Hash,
|
779 |
|
|
_RehashPolicy, _Traits>;
|
780 |
|
|
using value_type = typename __base_type::value_type;
|
781 |
|
|
using iterator = typename __base_type::iterator;
|
782 |
|
|
using const_iterator = typename __base_type::const_iterator;
|
783 |
|
|
|
784 |
|
|
using __unique_keys = typename __base_type::__unique_keys;
|
785 |
|
|
using __hashtable = typename __base_type::__hashtable;
|
786 |
|
|
|
787 |
|
|
using __base_type::insert;
|
788 |
|
|
|
789 |
|
|
iterator
|
790 |
|
|
insert(value_type&& __v)
|
791 |
|
|
{
|
792 |
|
|
__hashtable& __h = this->_M_conjure_hashtable();
|
793 |
|
|
return __h._M_insert(std::move(__v), __unique_keys());
|
794 |
|
|
}
|
795 |
|
|
|
796 |
|
|
iterator
|
797 |
|
|
insert(const_iterator, value_type&& __v)
|
798 |
|
|
{ return insert(std::move(__v)); }
|
799 |
|
|
};
|
800 |
|
|
|
801 |
|
|
/// Specialization.
|
802 |
|
|
template<typename _Key, typename _Value, typename _Alloc,
|
803 |
|
|
typename _ExtractKey, typename _Equal,
|
804 |
|
|
typename _H1, typename _H2, typename _Hash,
|
805 |
|
|
typename _RehashPolicy, typename _Traits, bool _Unique_keys>
|
806 |
|
|
struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
|
807 |
|
|
_RehashPolicy, _Traits, false, _Unique_keys>
|
808 |
|
|
: public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
809 |
|
|
_H1, _H2, _Hash, _RehashPolicy, _Traits>
|
810 |
|
|
{
|
811 |
|
|
using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey,
|
812 |
|
|
_Equal, _H1, _H2, _Hash,
|
813 |
|
|
_RehashPolicy, _Traits>;
|
814 |
|
|
using value_type = typename __base_type::value_type;
|
815 |
|
|
using iterator = typename __base_type::iterator;
|
816 |
|
|
using const_iterator = typename __base_type::const_iterator;
|
817 |
|
|
|
818 |
|
|
using __unique_keys = typename __base_type::__unique_keys;
|
819 |
|
|
using __hashtable = typename __base_type::__hashtable;
|
820 |
|
|
using __ireturn_type = typename __base_type::__ireturn_type;
|
821 |
|
|
using __iconv_type = typename __base_type::__iconv_type;
|
822 |
|
|
|
823 |
|
|
using __base_type::insert;
|
824 |
|
|
|
825 |
|
|
template<typename _Pair>
|
826 |
|
|
using __is_cons = std::is_constructible<value_type, _Pair&&>;
|
827 |
|
|
|
828 |
|
|
template<typename _Pair>
|
829 |
|
|
using _IFcons = std::enable_if<__is_cons<_Pair>::value>;
|
830 |
|
|
|
831 |
|
|
template<typename _Pair>
|
832 |
|
|
using _IFconsp = typename _IFcons<_Pair>::type;
|
833 |
|
|
|
834 |
|
|
template<typename _Pair, typename = _IFconsp<_Pair>>
|
835 |
|
|
__ireturn_type
|
836 |
|
|
insert(_Pair&& __v)
|
837 |
|
|
{
|
838 |
|
|
__hashtable& __h = this->_M_conjure_hashtable();
|
839 |
|
|
return __h._M_insert(std::forward<_Pair>(__v), __unique_keys());
|
840 |
|
|
}
|
841 |
|
|
|
842 |
|
|
template<typename _Pair, typename = _IFconsp<_Pair>>
|
843 |
|
|
iterator
|
844 |
|
|
insert(const_iterator, _Pair&& __v)
|
845 |
|
|
{ return __iconv_type()(insert(std::forward<_Pair>(__v))); }
|
846 |
|
|
};
|
847 |
|
|
|
848 |
|
|
/**
|
849 |
|
|
* Primary class template _Rehash_base.
|
850 |
|
|
*
|
851 |
|
|
* Give hashtable the max_load_factor functions and reserve iff the
|
852 |
|
|
* rehash policy is _Prime_rehash_policy.
|
853 |
|
|
*/
|
854 |
|
|
template<typename _Key, typename _Value, typename _Alloc,
|
855 |
|
|
typename _ExtractKey, typename _Equal,
|
856 |
|
|
typename _H1, typename _H2, typename _Hash,
|
857 |
|
|
typename _RehashPolicy, typename _Traits>
|
858 |
|
|
struct _Rehash_base;
|
859 |
|
|
|
860 |
|
|
/// Specialization.
|
861 |
|
|
template<typename _Key, typename _Value, typename _Alloc,
|
862 |
|
|
typename _ExtractKey, typename _Equal,
|
863 |
|
|
typename _H1, typename _H2, typename _Hash, typename _Traits>
|
864 |
|
|
struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
865 |
|
|
_H1, _H2, _Hash, _Prime_rehash_policy, _Traits>
|
866 |
|
|
{
|
867 |
|
|
using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
|
868 |
|
|
_Equal, _H1, _H2, _Hash,
|
869 |
|
|
_Prime_rehash_policy, _Traits>;
|
870 |
|
|
|
871 |
|
|
float
|
872 |
|
|
max_load_factor() const noexcept
|
873 |
|
|
{
|
874 |
|
|
const __hashtable* __this = static_cast<const __hashtable*>(this);
|
875 |
|
|
return __this->__rehash_policy().max_load_factor();
|
876 |
|
|
}
|
877 |
|
|
|
878 |
|
|
void
|
879 |
|
|
max_load_factor(float __z)
|
880 |
|
|
{
|
881 |
|
|
__hashtable* __this = static_cast<__hashtable*>(this);
|
882 |
|
|
__this->__rehash_policy(_Prime_rehash_policy(__z));
|
883 |
|
|
}
|
884 |
|
|
|
885 |
|
|
void
|
886 |
|
|
reserve(std::size_t __n)
|
887 |
|
|
{
|
888 |
|
|
__hashtable* __this = static_cast<__hashtable*>(this);
|
889 |
|
|
__this->rehash(__builtin_ceil(__n / max_load_factor()));
|
890 |
|
|
}
|
891 |
|
|
};
|
892 |
|
|
|
893 |
|
|
/**
|
894 |
|
|
* Primary class template _Hashtable_ebo_helper.
|
895 |
|
|
*
|
896 |
|
|
* Helper class using EBO when it is not forbidden, type is not
|
897 |
|
|
* final, and when it worth it, type is empty.
|
898 |
|
|
*/
|
899 |
|
|
template<int _Nm, typename _Tp,
|
900 |
|
|
bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
|
901 |
|
|
struct _Hashtable_ebo_helper;
|
902 |
|
|
|
903 |
|
|
/// Specialization using EBO.
|
904 |
|
|
template<int _Nm, typename _Tp>
|
905 |
|
|
struct _Hashtable_ebo_helper<_Nm, _Tp, true>
|
906 |
|
|
: private _Tp
|
907 |
|
|
{
|
908 |
|
|
_Hashtable_ebo_helper() = default;
|
909 |
|
|
|
910 |
|
|
_Hashtable_ebo_helper(const _Tp& __tp) : _Tp(__tp)
|
911 |
|
|
{ }
|
912 |
|
|
|
913 |
|
|
static const _Tp&
|
914 |
|
|
_S_cget(const _Hashtable_ebo_helper& __eboh)
|
915 |
|
|
{ return static_cast<const _Tp&>(__eboh); }
|
916 |
|
|
|
917 |
|
|
static _Tp&
|
918 |
|
|
_S_get(_Hashtable_ebo_helper& __eboh)
|
919 |
|
|
{ return static_cast<_Tp&>(__eboh); }
|
920 |
|
|
};
|
921 |
|
|
|
922 |
|
|
/// Specialization not using EBO.
|
923 |
|
|
template<int _Nm, typename _Tp>
|
924 |
|
|
struct _Hashtable_ebo_helper<_Nm, _Tp, false>
|
925 |
|
|
{
|
926 |
|
|
_Hashtable_ebo_helper() = default;
|
927 |
|
|
|
928 |
|
|
_Hashtable_ebo_helper(const _Tp& __tp) : _M_tp(__tp)
|
929 |
|
|
{ }
|
930 |
|
|
|
931 |
|
|
static const _Tp&
|
932 |
|
|
_S_cget(const _Hashtable_ebo_helper& __eboh)
|
933 |
|
|
{ return __eboh._M_tp; }
|
934 |
|
|
|
935 |
|
|
static _Tp&
|
936 |
|
|
_S_get(_Hashtable_ebo_helper& __eboh)
|
937 |
|
|
{ return __eboh._M_tp; }
|
938 |
|
|
|
939 |
|
|
private:
|
940 |
|
|
_Tp _M_tp;
|
941 |
|
|
};
|
942 |
|
|
|
943 |
|
|
/**
|
944 |
|
|
* Primary class template _Hash_code_base.
|
945 |
|
|
*
|
946 |
|
|
* Encapsulates two policy issues that aren't quite orthogonal.
|
947 |
|
|
* (1) the difference between using a ranged hash function and using
|
948 |
|
|
* the combination of a hash function and a range-hashing function.
|
949 |
|
|
* In the former case we don't have such things as hash codes, so
|
950 |
|
|
* we have a dummy type as placeholder.
|
951 |
|
|
* (2) Whether or not we cache hash codes. Caching hash codes is
|
952 |
|
|
* meaningless if we have a ranged hash function.
|
953 |
|
|
*
|
954 |
|
|
* We also put the key extraction objects here, for convenience.
|
955 |
|
|
* Each specialization derives from one or more of the template
|
956 |
|
|
* parameters to benefit from Ebo. This is important as this type
|
957 |
|
|
* is inherited in some cases by the _Local_iterator_base type used
|
958 |
|
|
* to implement local_iterator and const_local_iterator. As with
|
959 |
|
|
* any iterator type we prefer to make it as small as possible.
|
960 |
|
|
*
|
961 |
|
|
* Primary template is unused except as a hook for specializations.
|
962 |
|
|
*/
|
963 |
|
|
template<typename _Key, typename _Value, typename _ExtractKey,
|
964 |
|
|
typename _H1, typename _H2, typename _Hash,
|
965 |
|
|
bool __cache_hash_code>
|
966 |
|
|
struct _Hash_code_base;
|
967 |
|
|
|
968 |
|
|
/// Specialization: ranged hash function, no caching hash codes. H1
|
969 |
|
|
/// and H2 are provided but ignored. We define a dummy hash code type.
|
970 |
|
|
template<typename _Key, typename _Value, typename _ExtractKey,
|
971 |
|
|
typename _H1, typename _H2, typename _Hash>
|
972 |
|
|
struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash, false>
|
973 |
|
|
: private _Hashtable_ebo_helper<0, _ExtractKey>,
|
974 |
|
|
private _Hashtable_ebo_helper<1, _Hash>
|
975 |
|
|
{
|
976 |
|
|
private:
|
977 |
|
|
typedef _Hashtable_ebo_helper<0, _ExtractKey> _EboExtractKey;
|
978 |
|
|
typedef _Hashtable_ebo_helper<1, _Hash> _EboHash;
|
979 |
|
|
|
980 |
|
|
protected:
|
981 |
|
|
typedef void* __hash_code;
|
982 |
|
|
typedef _Hash_node<_Value, false> __node_type;
|
983 |
|
|
|
984 |
|
|
// We need the default constructor for the local iterators.
|
985 |
|
|
_Hash_code_base() = default;
|
986 |
|
|
|
987 |
|
|
_Hash_code_base(const _ExtractKey& __ex, const _H1&, const _H2&,
|
988 |
|
|
const _Hash& __h)
|
989 |
|
|
: _EboExtractKey(__ex), _EboHash(__h) { }
|
990 |
|
|
|
991 |
|
|
__hash_code
|
992 |
|
|
_M_hash_code(const _Key& __key) const
|
993 |
|
|
{ return 0; }
|
994 |
|
|
|
995 |
|
|
std::size_t
|
996 |
|
|
_M_bucket_index(const _Key& __k, __hash_code, std::size_t __n) const
|
997 |
|
|
{ return _M_ranged_hash()(__k, __n); }
|
998 |
|
|
|
999 |
|
|
std::size_t
|
1000 |
|
|
_M_bucket_index(const __node_type* __p, std::size_t __n) const
|
1001 |
|
|
{ return _M_ranged_hash()(_M_extract()(__p->_M_v), __n); }
|
1002 |
|
|
|
1003 |
|
|
void
|
1004 |
|
|
_M_store_code(__node_type*, __hash_code) const
|
1005 |
|
|
{ }
|
1006 |
|
|
|
1007 |
|
|
void
|
1008 |
|
|
_M_copy_code(__node_type*, const __node_type*) const
|
1009 |
|
|
{ }
|
1010 |
|
|
|
1011 |
|
|
void
|
1012 |
|
|
_M_swap(_Hash_code_base& __x)
|
1013 |
|
|
{
|
1014 |
|
|
std::swap(_M_extract(), __x._M_extract());
|
1015 |
|
|
std::swap(_M_ranged_hash(), __x._M_ranged_hash());
|
1016 |
|
|
}
|
1017 |
|
|
|
1018 |
|
|
protected:
|
1019 |
|
|
const _ExtractKey&
|
1020 |
|
|
_M_extract() const { return _EboExtractKey::_S_cget(*this); }
|
1021 |
|
|
|
1022 |
|
|
_ExtractKey&
|
1023 |
|
|
_M_extract() { return _EboExtractKey::_S_get(*this); }
|
1024 |
|
|
|
1025 |
|
|
const _Hash&
|
1026 |
|
|
_M_ranged_hash() const { return _EboHash::_S_cget(*this); }
|
1027 |
|
|
|
1028 |
|
|
_Hash&
|
1029 |
|
|
_M_ranged_hash() { return _EboHash::_S_get(*this); }
|
1030 |
|
|
};
|
1031 |
|
|
|
1032 |
|
|
// No specialization for ranged hash function while caching hash codes.
|
1033 |
|
|
// That combination is meaningless, and trying to do it is an error.
|
1034 |
|
|
|
1035 |
|
|
/// Specialization: ranged hash function, cache hash codes. This
|
1036 |
|
|
/// combination is meaningless, so we provide only a declaration
|
1037 |
|
|
/// and no definition.
|
1038 |
|
|
template<typename _Key, typename _Value, typename _ExtractKey,
|
1039 |
|
|
typename _H1, typename _H2, typename _Hash>
|
1040 |
|
|
struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash, true>;
|
1041 |
|
|
|
1042 |
|
|
/// Specialization: hash function and range-hashing function, no
|
1043 |
|
|
/// caching of hash codes.
|
1044 |
|
|
/// Provides typedef and accessor required by TR1.
|
1045 |
|
|
template<typename _Key, typename _Value, typename _ExtractKey,
|
1046 |
|
|
typename _H1, typename _H2>
|
1047 |
|
|
struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
|
1048 |
|
|
_Default_ranged_hash, false>
|
1049 |
|
|
: private _Hashtable_ebo_helper<0, _ExtractKey>,
|
1050 |
|
|
private _Hashtable_ebo_helper<1, _H1>,
|
1051 |
|
|
private _Hashtable_ebo_helper<2, _H2>
|
1052 |
|
|
{
|
1053 |
|
|
private:
|
1054 |
|
|
typedef _Hashtable_ebo_helper<0, _ExtractKey> _EboExtractKey;
|
1055 |
|
|
typedef _Hashtable_ebo_helper<1, _H1> _EboH1;
|
1056 |
|
|
typedef _Hashtable_ebo_helper<2, _H2> _EboH2;
|
1057 |
|
|
|
1058 |
|
|
public:
|
1059 |
|
|
typedef _H1 hasher;
|
1060 |
|
|
|
1061 |
|
|
hasher
|
1062 |
|
|
hash_function() const
|
1063 |
|
|
{ return _M_h1(); }
|
1064 |
|
|
|
1065 |
|
|
typedef std::size_t __hash_code;
|
1066 |
|
|
typedef _Hash_node<_Value, false> __node_type;
|
1067 |
|
|
|
1068 |
|
|
protected:
|
1069 |
|
|
// We need the default constructor for the local iterators.
|
1070 |
|
|
_Hash_code_base() = default;
|
1071 |
|
|
|
1072 |
|
|
_Hash_code_base(const _ExtractKey& __ex,
|
1073 |
|
|
const _H1& __h1, const _H2& __h2,
|
1074 |
|
|
const _Default_ranged_hash&)
|
1075 |
|
|
: _EboExtractKey(__ex), _EboH1(__h1), _EboH2(__h2) { }
|
1076 |
|
|
|
1077 |
|
|
__hash_code
|
1078 |
|
|
_M_hash_code(const _Key& __k) const
|
1079 |
|
|
{ return _M_h1()(__k); }
|
1080 |
|
|
|
1081 |
|
|
std::size_t
|
1082 |
|
|
_M_bucket_index(const _Key&, __hash_code __c, std::size_t __n) const
|
1083 |
|
|
{ return _M_h2()(__c, __n); }
|
1084 |
|
|
|
1085 |
|
|
std::size_t
|
1086 |
|
|
_M_bucket_index(const __node_type* __p,
|
1087 |
|
|
std::size_t __n) const
|
1088 |
|
|
{ return _M_h2()(_M_h1()(_M_extract()(__p->_M_v)), __n); }
|
1089 |
|
|
|
1090 |
|
|
void
|
1091 |
|
|
_M_store_code(__node_type*, __hash_code) const
|
1092 |
|
|
{ }
|
1093 |
|
|
|
1094 |
|
|
void
|
1095 |
|
|
_M_copy_code(__node_type*, const __node_type*) const
|
1096 |
|
|
{ }
|
1097 |
|
|
|
1098 |
|
|
void
|
1099 |
|
|
_M_swap(_Hash_code_base& __x)
|
1100 |
|
|
{
|
1101 |
|
|
std::swap(_M_extract(), __x._M_extract());
|
1102 |
|
|
std::swap(_M_h1(), __x._M_h1());
|
1103 |
|
|
std::swap(_M_h2(), __x._M_h2());
|
1104 |
|
|
}
|
1105 |
|
|
|
1106 |
|
|
const _ExtractKey&
|
1107 |
|
|
_M_extract() const { return _EboExtractKey::_S_cget(*this); }
|
1108 |
|
|
|
1109 |
|
|
_ExtractKey&
|
1110 |
|
|
_M_extract() { return _EboExtractKey::_S_get(*this); }
|
1111 |
|
|
|
1112 |
|
|
const _H1&
|
1113 |
|
|
_M_h1() const { return _EboH1::_S_cget(*this); }
|
1114 |
|
|
|
1115 |
|
|
_H1&
|
1116 |
|
|
_M_h1() { return _EboH1::_S_get(*this); }
|
1117 |
|
|
|
1118 |
|
|
const _H2&
|
1119 |
|
|
_M_h2() const { return _EboH2::_S_cget(*this); }
|
1120 |
|
|
|
1121 |
|
|
_H2&
|
1122 |
|
|
_M_h2() { return _EboH2::_S_get(*this); }
|
1123 |
|
|
};
|
1124 |
|
|
|
1125 |
|
|
/// Specialization: hash function and range-hashing function,
|
1126 |
|
|
/// caching hash codes. H is provided but ignored. Provides
|
1127 |
|
|
/// typedef and accessor required by TR1.
|
1128 |
|
|
template<typename _Key, typename _Value, typename _ExtractKey,
|
1129 |
|
|
typename _H1, typename _H2>
|
1130 |
|
|
struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
|
1131 |
|
|
_Default_ranged_hash, true>
|
1132 |
|
|
: private _Hashtable_ebo_helper<0, _ExtractKey>,
|
1133 |
|
|
private _Hashtable_ebo_helper<1, _H1>,
|
1134 |
|
|
private _Hashtable_ebo_helper<2, _H2>
|
1135 |
|
|
{
|
1136 |
|
|
private:
|
1137 |
|
|
typedef _Hashtable_ebo_helper<0, _ExtractKey> _EboExtractKey;
|
1138 |
|
|
typedef _Hashtable_ebo_helper<1, _H1> _EboH1;
|
1139 |
|
|
typedef _Hashtable_ebo_helper<2, _H2> _EboH2;
|
1140 |
|
|
|
1141 |
|
|
public:
|
1142 |
|
|
typedef _H1 hasher;
|
1143 |
|
|
|
1144 |
|
|
hasher
|
1145 |
|
|
hash_function() const
|
1146 |
|
|
{ return _M_h1(); }
|
1147 |
|
|
|
1148 |
|
|
typedef std::size_t __hash_code;
|
1149 |
|
|
typedef _Hash_node<_Value, true> __node_type;
|
1150 |
|
|
|
1151 |
|
|
protected:
|
1152 |
|
|
_Hash_code_base(const _ExtractKey& __ex,
|
1153 |
|
|
const _H1& __h1, const _H2& __h2,
|
1154 |
|
|
const _Default_ranged_hash&)
|
1155 |
|
|
: _EboExtractKey(__ex), _EboH1(__h1), _EboH2(__h2) { }
|
1156 |
|
|
|
1157 |
|
|
__hash_code
|
1158 |
|
|
_M_hash_code(const _Key& __k) const
|
1159 |
|
|
{ return _M_h1()(__k); }
|
1160 |
|
|
|
1161 |
|
|
std::size_t
|
1162 |
|
|
_M_bucket_index(const _Key&, __hash_code __c,
|
1163 |
|
|
std::size_t __n) const
|
1164 |
|
|
{ return _M_h2()(__c, __n); }
|
1165 |
|
|
|
1166 |
|
|
std::size_t
|
1167 |
|
|
_M_bucket_index(const __node_type* __p, std::size_t __n) const
|
1168 |
|
|
{ return _M_h2()(__p->_M_hash_code, __n); }
|
1169 |
|
|
|
1170 |
|
|
void
|
1171 |
|
|
_M_store_code(__node_type* __n, __hash_code __c) const
|
1172 |
|
|
{ __n->_M_hash_code = __c; }
|
1173 |
|
|
|
1174 |
|
|
void
|
1175 |
|
|
_M_copy_code(__node_type* __to, const __node_type* __from) const
|
1176 |
|
|
{ __to->_M_hash_code = __from->_M_hash_code; }
|
1177 |
|
|
|
1178 |
|
|
void
|
1179 |
|
|
_M_swap(_Hash_code_base& __x)
|
1180 |
|
|
{
|
1181 |
|
|
std::swap(_M_extract(), __x._M_extract());
|
1182 |
|
|
std::swap(_M_h1(), __x._M_h1());
|
1183 |
|
|
std::swap(_M_h2(), __x._M_h2());
|
1184 |
|
|
}
|
1185 |
|
|
|
1186 |
|
|
const _ExtractKey&
|
1187 |
|
|
_M_extract() const { return _EboExtractKey::_S_cget(*this); }
|
1188 |
|
|
|
1189 |
|
|
_ExtractKey&
|
1190 |
|
|
_M_extract() { return _EboExtractKey::_S_get(*this); }
|
1191 |
|
|
|
1192 |
|
|
const _H1&
|
1193 |
|
|
_M_h1() const { return _EboH1::_S_cget(*this); }
|
1194 |
|
|
|
1195 |
|
|
_H1&
|
1196 |
|
|
_M_h1() { return _EboH1::_S_get(*this); }
|
1197 |
|
|
|
1198 |
|
|
const _H2&
|
1199 |
|
|
_M_h2() const { return _EboH2::_S_cget(*this); }
|
1200 |
|
|
|
1201 |
|
|
_H2&
|
1202 |
|
|
_M_h2() { return _EboH2::_S_get(*this); }
|
1203 |
|
|
};
|
1204 |
|
|
|
1205 |
|
|
/**
|
1206 |
|
|
* Primary class template _Equal_helper.
|
1207 |
|
|
*
|
1208 |
|
|
*/
|
1209 |
|
|
template <typename _Key, typename _Value, typename _ExtractKey,
|
1210 |
|
|
typename _Equal, typename _HashCodeType,
|
1211 |
|
|
bool __cache_hash_code>
|
1212 |
|
|
struct _Equal_helper;
|
1213 |
|
|
|
1214 |
|
|
/// Specialization.
|
1215 |
|
|
template<typename _Key, typename _Value, typename _ExtractKey,
|
1216 |
|
|
typename _Equal, typename _HashCodeType>
|
1217 |
|
|
struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, true>
|
1218 |
|
|
{
|
1219 |
|
|
static bool
|
1220 |
|
|
_S_equals(const _Equal& __eq, const _ExtractKey& __extract,
|
1221 |
|
|
const _Key& __k, _HashCodeType __c, _Hash_node<_Value, true>* __n)
|
1222 |
|
|
{ return __c == __n->_M_hash_code && __eq(__k, __extract(__n->_M_v)); }
|
1223 |
|
|
};
|
1224 |
|
|
|
1225 |
|
|
/// Specialization.
|
1226 |
|
|
template<typename _Key, typename _Value, typename _ExtractKey,
|
1227 |
|
|
typename _Equal, typename _HashCodeType>
|
1228 |
|
|
struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, false>
|
1229 |
|
|
{
|
1230 |
|
|
static bool
|
1231 |
|
|
_S_equals(const _Equal& __eq, const _ExtractKey& __extract,
|
1232 |
|
|
const _Key& __k, _HashCodeType, _Hash_node<_Value, false>* __n)
|
1233 |
|
|
{ return __eq(__k, __extract(__n->_M_v)); }
|
1234 |
|
|
};
|
1235 |
|
|
|
1236 |
|
|
|
1237 |
|
|
/**
|
1238 |
|
|
* Primary class template _Local_iterator_base.
|
1239 |
|
|
*
|
1240 |
|
|
* Base class for local iterators, used to iterate within a bucket
|
1241 |
|
|
* but not between buckets.
|
1242 |
|
|
*/
|
1243 |
|
|
template<typename _Key, typename _Value, typename _ExtractKey,
|
1244 |
|
|
typename _H1, typename _H2, typename _Hash,
|
1245 |
|
|
bool __cache_hash_code>
|
1246 |
|
|
struct _Local_iterator_base;
|
1247 |
|
|
|
1248 |
|
|
/// Specialization.
|
1249 |
|
|
template<typename _Key, typename _Value, typename _ExtractKey,
|
1250 |
|
|
typename _H1, typename _H2, typename _Hash>
|
1251 |
|
|
struct _Local_iterator_base<_Key, _Value, _ExtractKey,
|
1252 |
|
|
_H1, _H2, _Hash, true>
|
1253 |
|
|
: private _H2
|
1254 |
|
|
{
|
1255 |
|
|
_Local_iterator_base() = default;
|
1256 |
|
|
_Local_iterator_base(_Hash_node<_Value, true>* __p,
|
1257 |
|
|
std::size_t __bkt, std::size_t __bkt_count)
|
1258 |
|
|
: _M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count) { }
|
1259 |
|
|
|
1260 |
|
|
void
|
1261 |
|
|
_M_incr()
|
1262 |
|
|
{
|
1263 |
|
|
_M_cur = _M_cur->_M_next();
|
1264 |
|
|
if (_M_cur)
|
1265 |
|
|
{
|
1266 |
|
|
std::size_t __bkt = _M_h2()(_M_cur->_M_hash_code, _M_bucket_count);
|
1267 |
|
|
if (__bkt != _M_bucket)
|
1268 |
|
|
_M_cur = nullptr;
|
1269 |
|
|
}
|
1270 |
|
|
}
|
1271 |
|
|
|
1272 |
|
|
const _H2& _M_h2() const
|
1273 |
|
|
{ return *this; }
|
1274 |
|
|
|
1275 |
|
|
_Hash_node<_Value, true>* _M_cur;
|
1276 |
|
|
std::size_t _M_bucket;
|
1277 |
|
|
std::size_t _M_bucket_count;
|
1278 |
|
|
};
|
1279 |
|
|
|
1280 |
|
|
/// Specialization.
|
1281 |
|
|
template<typename _Key, typename _Value, typename _ExtractKey,
|
1282 |
|
|
typename _H1, typename _H2, typename _Hash>
|
1283 |
|
|
struct _Local_iterator_base<_Key, _Value, _ExtractKey,
|
1284 |
|
|
_H1, _H2, _Hash, false>
|
1285 |
|
|
: private _Hash_code_base<_Key, _Value, _ExtractKey,
|
1286 |
|
|
_H1, _H2, _Hash, false>
|
1287 |
|
|
{
|
1288 |
|
|
_Local_iterator_base() = default;
|
1289 |
|
|
_Local_iterator_base(_Hash_node<_Value, false>* __p,
|
1290 |
|
|
std::size_t __bkt, std::size_t __bkt_count)
|
1291 |
|
|
: _M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count) { }
|
1292 |
|
|
|
1293 |
|
|
void
|
1294 |
|
|
_M_incr()
|
1295 |
|
|
{
|
1296 |
|
|
_M_cur = _M_cur->_M_next();
|
1297 |
|
|
if (_M_cur)
|
1298 |
|
|
{
|
1299 |
|
|
std::size_t __bkt = this->_M_bucket_index(_M_cur, _M_bucket_count);
|
1300 |
|
|
if (__bkt != _M_bucket)
|
1301 |
|
|
_M_cur = nullptr;
|
1302 |
|
|
}
|
1303 |
|
|
}
|
1304 |
|
|
|
1305 |
|
|
_Hash_node<_Value, false>* _M_cur;
|
1306 |
|
|
std::size_t _M_bucket;
|
1307 |
|
|
std::size_t _M_bucket_count;
|
1308 |
|
|
};
|
1309 |
|
|
|
1310 |
|
|
template<typename _Key, typename _Value, typename _ExtractKey,
|
1311 |
|
|
typename _H1, typename _H2, typename _Hash, bool __cache>
|
1312 |
|
|
inline bool
|
1313 |
|
|
operator==(const _Local_iterator_base<_Key, _Value, _ExtractKey,
|
1314 |
|
|
_H1, _H2, _Hash, __cache>& __x,
|
1315 |
|
|
const _Local_iterator_base<_Key, _Value, _ExtractKey,
|
1316 |
|
|
_H1, _H2, _Hash, __cache>& __y)
|
1317 |
|
|
{ return __x._M_cur == __y._M_cur; }
|
1318 |
|
|
|
1319 |
|
|
template<typename _Key, typename _Value, typename _ExtractKey,
|
1320 |
|
|
typename _H1, typename _H2, typename _Hash, bool __cache>
|
1321 |
|
|
inline bool
|
1322 |
|
|
operator!=(const _Local_iterator_base<_Key, _Value, _ExtractKey,
|
1323 |
|
|
_H1, _H2, _Hash, __cache>& __x,
|
1324 |
|
|
const _Local_iterator_base<_Key, _Value, _ExtractKey,
|
1325 |
|
|
_H1, _H2, _Hash, __cache>& __y)
|
1326 |
|
|
{ return __x._M_cur != __y._M_cur; }
|
1327 |
|
|
|
1328 |
|
|
/// local iterators
|
1329 |
|
|
template<typename _Key, typename _Value, typename _ExtractKey,
|
1330 |
|
|
typename _H1, typename _H2, typename _Hash,
|
1331 |
|
|
bool __constant_iterators, bool __cache>
|
1332 |
|
|
struct _Local_iterator
|
1333 |
|
|
: public _Local_iterator_base<_Key, _Value, _ExtractKey,
|
1334 |
|
|
_H1, _H2, _Hash, __cache>
|
1335 |
|
|
{
|
1336 |
|
|
typedef _Value value_type;
|
1337 |
|
|
typedef typename std::conditional<__constant_iterators,
|
1338 |
|
|
const _Value*, _Value*>::type
|
1339 |
|
|
pointer;
|
1340 |
|
|
typedef typename std::conditional<__constant_iterators,
|
1341 |
|
|
const _Value&, _Value&>::type
|
1342 |
|
|
reference;
|
1343 |
|
|
typedef std::ptrdiff_t difference_type;
|
1344 |
|
|
typedef std::forward_iterator_tag iterator_category;
|
1345 |
|
|
|
1346 |
|
|
_Local_iterator() = default;
|
1347 |
|
|
|
1348 |
|
|
explicit
|
1349 |
|
|
_Local_iterator(_Hash_node<_Value, __cache>* __p,
|
1350 |
|
|
std::size_t __bkt, std::size_t __bkt_count)
|
1351 |
|
|
: _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
|
1352 |
|
|
__cache>(__p, __bkt, __bkt_count)
|
1353 |
|
|
{ }
|
1354 |
|
|
|
1355 |
|
|
reference
|
1356 |
|
|
operator*() const
|
1357 |
|
|
{ return this->_M_cur->_M_v; }
|
1358 |
|
|
|
1359 |
|
|
pointer
|
1360 |
|
|
operator->() const
|
1361 |
|
|
{ return std::__addressof(this->_M_cur->_M_v); }
|
1362 |
|
|
|
1363 |
|
|
_Local_iterator&
|
1364 |
|
|
operator++()
|
1365 |
|
|
{
|
1366 |
|
|
this->_M_incr();
|
1367 |
|
|
return *this;
|
1368 |
|
|
}
|
1369 |
|
|
|
1370 |
|
|
_Local_iterator
|
1371 |
|
|
operator++(int)
|
1372 |
|
|
{
|
1373 |
|
|
_Local_iterator __tmp(*this);
|
1374 |
|
|
this->_M_incr();
|
1375 |
|
|
return __tmp;
|
1376 |
|
|
}
|
1377 |
|
|
};
|
1378 |
|
|
|
1379 |
|
|
/// local const_iterators
|
1380 |
|
|
template<typename _Key, typename _Value, typename _ExtractKey,
|
1381 |
|
|
typename _H1, typename _H2, typename _Hash,
|
1382 |
|
|
bool __constant_iterators, bool __cache>
|
1383 |
|
|
struct _Local_const_iterator
|
1384 |
|
|
: public _Local_iterator_base<_Key, _Value, _ExtractKey,
|
1385 |
|
|
_H1, _H2, _Hash, __cache>
|
1386 |
|
|
{
|
1387 |
|
|
typedef _Value value_type;
|
1388 |
|
|
typedef const _Value* pointer;
|
1389 |
|
|
typedef const _Value& reference;
|
1390 |
|
|
typedef std::ptrdiff_t difference_type;
|
1391 |
|
|
typedef std::forward_iterator_tag iterator_category;
|
1392 |
|
|
|
1393 |
|
|
_Local_const_iterator() = default;
|
1394 |
|
|
|
1395 |
|
|
explicit
|
1396 |
|
|
_Local_const_iterator(_Hash_node<_Value, __cache>* __p,
|
1397 |
|
|
std::size_t __bkt, std::size_t __bkt_count)
|
1398 |
|
|
: _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
|
1399 |
|
|
__cache>(__p, __bkt, __bkt_count)
|
1400 |
|
|
{ }
|
1401 |
|
|
|
1402 |
|
|
_Local_const_iterator(const _Local_iterator<_Key, _Value, _ExtractKey,
|
1403 |
|
|
_H1, _H2, _Hash,
|
1404 |
|
|
__constant_iterators,
|
1405 |
|
|
__cache>& __x)
|
1406 |
|
|
: _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
|
1407 |
|
|
__cache>(__x._M_cur, __x._M_bucket,
|
1408 |
|
|
__x._M_bucket_count)
|
1409 |
|
|
{ }
|
1410 |
|
|
|
1411 |
|
|
reference
|
1412 |
|
|
operator*() const
|
1413 |
|
|
{ return this->_M_cur->_M_v; }
|
1414 |
|
|
|
1415 |
|
|
pointer
|
1416 |
|
|
operator->() const
|
1417 |
|
|
{ return std::__addressof(this->_M_cur->_M_v); }
|
1418 |
|
|
|
1419 |
|
|
_Local_const_iterator&
|
1420 |
|
|
operator++()
|
1421 |
|
|
{
|
1422 |
|
|
this->_M_incr();
|
1423 |
|
|
return *this;
|
1424 |
|
|
}
|
1425 |
|
|
|
1426 |
|
|
_Local_const_iterator
|
1427 |
|
|
operator++(int)
|
1428 |
|
|
{
|
1429 |
|
|
_Local_const_iterator __tmp(*this);
|
1430 |
|
|
this->_M_incr();
|
1431 |
|
|
return __tmp;
|
1432 |
|
|
}
|
1433 |
|
|
};
|
1434 |
|
|
|
1435 |
|
|
/**
|
1436 |
|
|
* Primary class template _Hashtable_base.
|
1437 |
|
|
*
|
1438 |
|
|
* Helper class adding management of _Equal functor to
|
1439 |
|
|
* _Hash_code_base type.
|
1440 |
|
|
*
|
1441 |
|
|
* Base class templates are:
|
1442 |
|
|
* - __detail::_Hash_code_base
|
1443 |
|
|
* - __detail::_Hashtable_ebo_helper
|
1444 |
|
|
*/
|
1445 |
|
|
template<typename _Key, typename _Value,
|
1446 |
|
|
typename _ExtractKey, typename _Equal,
|
1447 |
|
|
typename _H1, typename _H2, typename _Hash, typename _Traits>
|
1448 |
|
|
struct _Hashtable_base
|
1449 |
|
|
: public _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
|
1450 |
|
|
_Traits::__hash_cached::value>,
|
1451 |
|
|
private _Hashtable_ebo_helper<0, _Equal>
|
1452 |
|
|
{
|
1453 |
|
|
public:
|
1454 |
|
|
typedef _Key key_type;
|
1455 |
|
|
typedef _Value value_type;
|
1456 |
|
|
typedef _Equal key_equal;
|
1457 |
|
|
typedef std::size_t size_type;
|
1458 |
|
|
typedef std::ptrdiff_t difference_type;
|
1459 |
|
|
|
1460 |
|
|
using __traits_type = _Traits;
|
1461 |
|
|
using __hash_cached = typename __traits_type::__hash_cached;
|
1462 |
|
|
using __constant_iterators = typename __traits_type::__constant_iterators;
|
1463 |
|
|
using __unique_keys = typename __traits_type::__unique_keys;
|
1464 |
|
|
|
1465 |
|
|
using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
|
1466 |
|
|
_H1, _H2, _Hash,
|
1467 |
|
|
__hash_cached::value>;
|
1468 |
|
|
|
1469 |
|
|
using __hash_code = typename __hash_code_base::__hash_code;
|
1470 |
|
|
using __node_type = typename __hash_code_base::__node_type;
|
1471 |
|
|
|
1472 |
|
|
using iterator = __detail::_Node_iterator<value_type,
|
1473 |
|
|
__constant_iterators::value,
|
1474 |
|
|
__hash_cached::value>;
|
1475 |
|
|
|
1476 |
|
|
using const_iterator = __detail::_Node_const_iterator<value_type,
|
1477 |
|
|
__constant_iterators::value,
|
1478 |
|
|
__hash_cached::value>;
|
1479 |
|
|
|
1480 |
|
|
using local_iterator = __detail::_Local_iterator<key_type, value_type,
|
1481 |
|
|
_ExtractKey, _H1, _H2, _Hash,
|
1482 |
|
|
__constant_iterators::value,
|
1483 |
|
|
__hash_cached::value>;
|
1484 |
|
|
|
1485 |
|
|
using const_local_iterator = __detail::_Local_const_iterator<key_type,
|
1486 |
|
|
value_type,
|
1487 |
|
|
_ExtractKey, _H1, _H2, _Hash,
|
1488 |
|
|
__constant_iterators::value,
|
1489 |
|
|
__hash_cached::value>;
|
1490 |
|
|
|
1491 |
|
|
using __ireturn_type = typename std::conditional<__unique_keys::value,
|
1492 |
|
|
std::pair<iterator, bool>,
|
1493 |
|
|
iterator>::type;
|
1494 |
|
|
|
1495 |
|
|
using __iconv_type = typename std::conditional<__unique_keys::value,
|
1496 |
|
|
_Select1st, _Identity
|
1497 |
|
|
>::type;
|
1498 |
|
|
private:
|
1499 |
|
|
using _EqualEBO = _Hashtable_ebo_helper<0, _Equal>;
|
1500 |
|
|
using _EqualHelper = _Equal_helper<_Key, _Value, _ExtractKey, _Equal,
|
1501 |
|
|
__hash_code, __hash_cached::value>;
|
1502 |
|
|
|
1503 |
|
|
protected:
|
1504 |
|
|
using __node_base = __detail::_Hash_node_base;
|
1505 |
|
|
using __bucket_type = __node_base*;
|
1506 |
|
|
|
1507 |
|
|
_Hashtable_base(const _ExtractKey& __ex, const _H1& __h1, const _H2& __h2,
|
1508 |
|
|
const _Hash& __hash, const _Equal& __eq)
|
1509 |
|
|
: __hash_code_base(__ex, __h1, __h2, __hash), _EqualEBO(__eq)
|
1510 |
|
|
{ }
|
1511 |
|
|
|
1512 |
|
|
bool
|
1513 |
|
|
_M_equals(const _Key& __k, __hash_code __c, __node_type* __n) const
|
1514 |
|
|
{
|
1515 |
|
|
return _EqualHelper::_S_equals(_M_eq(), this->_M_extract(),
|
1516 |
|
|
__k, __c, __n);
|
1517 |
|
|
}
|
1518 |
|
|
|
1519 |
|
|
void
|
1520 |
|
|
_M_swap(_Hashtable_base& __x)
|
1521 |
|
|
{
|
1522 |
|
|
__hash_code_base::_M_swap(__x);
|
1523 |
|
|
std::swap(_M_eq(), __x._M_eq());
|
1524 |
|
|
}
|
1525 |
|
|
|
1526 |
|
|
const _Equal&
|
1527 |
|
|
_M_eq() const { return _EqualEBO::_S_cget(*this); }
|
1528 |
|
|
|
1529 |
|
|
_Equal&
|
1530 |
|
|
_M_eq() { return _EqualEBO::_S_get(*this); }
|
1531 |
|
|
};
|
1532 |
|
|
|
1533 |
|
|
/**
|
1534 |
|
|
* struct _Equality_base.
|
1535 |
|
|
*
|
1536 |
|
|
* Common types and functions for class _Equality.
|
1537 |
|
|
*/
|
1538 |
|
|
struct _Equality_base
|
1539 |
|
|
{
|
1540 |
|
|
protected:
|
1541 |
|
|
template<typename _Uiterator>
|
1542 |
|
|
static bool
|
1543 |
|
|
_S_is_permutation(_Uiterator, _Uiterator, _Uiterator);
|
1544 |
|
|
};
|
1545 |
|
|
|
1546 |
|
|
// See std::is_permutation in N3068.
|
1547 |
|
|
template<typename _Uiterator>
|
1548 |
|
|
bool
|
1549 |
|
|
_Equality_base::
|
1550 |
|
|
_S_is_permutation(_Uiterator __first1, _Uiterator __last1,
|
1551 |
|
|
_Uiterator __first2)
|
1552 |
|
|
{
|
1553 |
|
|
for (; __first1 != __last1; ++__first1, ++__first2)
|
1554 |
|
|
if (!(*__first1 == *__first2))
|
1555 |
|
|
break;
|
1556 |
|
|
|
1557 |
|
|
if (__first1 == __last1)
|
1558 |
|
|
return true;
|
1559 |
|
|
|
1560 |
|
|
_Uiterator __last2 = __first2;
|
1561 |
|
|
std::advance(__last2, std::distance(__first1, __last1));
|
1562 |
|
|
|
1563 |
|
|
for (_Uiterator __it1 = __first1; __it1 != __last1; ++__it1)
|
1564 |
|
|
{
|
1565 |
|
|
_Uiterator __tmp = __first1;
|
1566 |
|
|
while (__tmp != __it1 && !bool(*__tmp == *__it1))
|
1567 |
|
|
++__tmp;
|
1568 |
|
|
|
1569 |
|
|
// We've seen this one before.
|
1570 |
|
|
if (__tmp != __it1)
|
1571 |
|
|
continue;
|
1572 |
|
|
|
1573 |
|
|
std::ptrdiff_t __n2 = 0;
|
1574 |
|
|
for (__tmp = __first2; __tmp != __last2; ++__tmp)
|
1575 |
|
|
if (*__tmp == *__it1)
|
1576 |
|
|
++__n2;
|
1577 |
|
|
|
1578 |
|
|
if (!__n2)
|
1579 |
|
|
return false;
|
1580 |
|
|
|
1581 |
|
|
std::ptrdiff_t __n1 = 0;
|
1582 |
|
|
for (__tmp = __it1; __tmp != __last1; ++__tmp)
|
1583 |
|
|
if (*__tmp == *__it1)
|
1584 |
|
|
++__n1;
|
1585 |
|
|
|
1586 |
|
|
if (__n1 != __n2)
|
1587 |
|
|
return false;
|
1588 |
|
|
}
|
1589 |
|
|
return true;
|
1590 |
|
|
}
|
1591 |
|
|
|
1592 |
|
|
/**
|
1593 |
|
|
* Primary class template _Equality.
|
1594 |
|
|
*
|
1595 |
|
|
* This is for implementing equality comparison for unordered
|
1596 |
|
|
* containers, per N3068, by John Lakos and Pablo Halpern.
|
1597 |
|
|
* Algorithmically, we follow closely the reference implementations
|
1598 |
|
|
* therein.
|
1599 |
|
|
*/
|
1600 |
|
|
template<typename _Key, typename _Value, typename _Alloc,
|
1601 |
|
|
typename _ExtractKey, typename _Equal,
|
1602 |
|
|
typename _H1, typename _H2, typename _Hash,
|
1603 |
|
|
typename _RehashPolicy, typename _Traits,
|
1604 |
|
|
bool _Unique_keys = _Traits::__unique_keys::value>
|
1605 |
|
|
struct _Equality;
|
1606 |
|
|
|
1607 |
|
|
/// Specialization.
|
1608 |
|
|
template<typename _Key, typename _Value, typename _Alloc,
|
1609 |
|
|
typename _ExtractKey, typename _Equal,
|
1610 |
|
|
typename _H1, typename _H2, typename _Hash,
|
1611 |
|
|
typename _RehashPolicy, typename _Traits>
|
1612 |
|
|
struct _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
1613 |
|
|
_H1, _H2, _Hash, _RehashPolicy, _Traits, true>
|
1614 |
|
|
{
|
1615 |
|
|
using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
1616 |
|
|
_H1, _H2, _Hash, _RehashPolicy, _Traits>;
|
1617 |
|
|
|
1618 |
|
|
bool
|
1619 |
|
|
_M_equal(const __hashtable&) const;
|
1620 |
|
|
};
|
1621 |
|
|
|
1622 |
|
|
template<typename _Key, typename _Value, typename _Alloc,
|
1623 |
|
|
typename _ExtractKey, typename _Equal,
|
1624 |
|
|
typename _H1, typename _H2, typename _Hash,
|
1625 |
|
|
typename _RehashPolicy, typename _Traits>
|
1626 |
|
|
bool
|
1627 |
|
|
_Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
1628 |
|
|
_H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
|
1629 |
|
|
_M_equal(const __hashtable& __other) const
|
1630 |
|
|
{
|
1631 |
|
|
const __hashtable* __this = static_cast<const __hashtable*>(this);
|
1632 |
|
|
|
1633 |
|
|
if (__this->size() != __other.size())
|
1634 |
|
|
return false;
|
1635 |
|
|
|
1636 |
|
|
for (auto __itx = __this->begin(); __itx != __this->end(); ++__itx)
|
1637 |
|
|
{
|
1638 |
|
|
const auto __ity = __other.find(_ExtractKey()(*__itx));
|
1639 |
|
|
if (__ity == __other.end() || !bool(*__ity == *__itx))
|
1640 |
|
|
return false;
|
1641 |
|
|
}
|
1642 |
|
|
return true;
|
1643 |
|
|
}
|
1644 |
|
|
|
1645 |
|
|
/// Specialization.
|
1646 |
|
|
template<typename _Key, typename _Value, typename _Alloc,
|
1647 |
|
|
typename _ExtractKey, typename _Equal,
|
1648 |
|
|
typename _H1, typename _H2, typename _Hash,
|
1649 |
|
|
typename _RehashPolicy, typename _Traits>
|
1650 |
|
|
struct _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
1651 |
|
|
_H1, _H2, _Hash, _RehashPolicy, _Traits, false>
|
1652 |
|
|
: public _Equality_base
|
1653 |
|
|
{
|
1654 |
|
|
using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
1655 |
|
|
_H1, _H2, _Hash, _RehashPolicy, _Traits>;
|
1656 |
|
|
|
1657 |
|
|
bool
|
1658 |
|
|
_M_equal(const __hashtable&) const;
|
1659 |
|
|
};
|
1660 |
|
|
|
1661 |
|
|
template<typename _Key, typename _Value, typename _Alloc,
|
1662 |
|
|
typename _ExtractKey, typename _Equal,
|
1663 |
|
|
typename _H1, typename _H2, typename _Hash,
|
1664 |
|
|
typename _RehashPolicy, typename _Traits>
|
1665 |
|
|
bool
|
1666 |
|
|
_Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
|
1667 |
|
|
_H1, _H2, _Hash, _RehashPolicy, _Traits, false>::
|
1668 |
|
|
_M_equal(const __hashtable& __other) const
|
1669 |
|
|
{
|
1670 |
|
|
const __hashtable* __this = static_cast<const __hashtable*>(this);
|
1671 |
|
|
|
1672 |
|
|
if (__this->size() != __other.size())
|
1673 |
|
|
return false;
|
1674 |
|
|
|
1675 |
|
|
for (auto __itx = __this->begin(); __itx != __this->end();)
|
1676 |
|
|
{
|
1677 |
|
|
const auto __xrange = __this->equal_range(_ExtractKey()(*__itx));
|
1678 |
|
|
const auto __yrange = __other.equal_range(_ExtractKey()(*__itx));
|
1679 |
|
|
|
1680 |
|
|
if (std::distance(__xrange.first, __xrange.second)
|
1681 |
|
|
!= std::distance(__yrange.first, __yrange.second))
|
1682 |
|
|
return false;
|
1683 |
|
|
|
1684 |
|
|
if (!_S_is_permutation(__xrange.first, __xrange.second,
|
1685 |
|
|
__yrange.first))
|
1686 |
|
|
return false;
|
1687 |
|
|
|
1688 |
|
|
__itx = __xrange.second;
|
1689 |
|
|
}
|
1690 |
|
|
return true;
|
1691 |
|
|
}
|
1692 |
|
|
|
1693 |
|
|
/**
|
1694 |
|
|
* This type is to combine a _Hash_node_base instance with an allocator
|
1695 |
|
|
* instance through inheritance to benefit from EBO when possible.
|
1696 |
|
|
*/
|
1697 |
|
|
template<typename _NodeAlloc>
|
1698 |
|
|
struct _Before_begin : public _NodeAlloc
|
1699 |
|
|
{
|
1700 |
|
|
_Hash_node_base _M_node;
|
1701 |
|
|
|
1702 |
|
|
_Before_begin(const _Before_begin&) = default;
|
1703 |
|
|
_Before_begin(_Before_begin&&) = default;
|
1704 |
|
|
|
1705 |
|
|
template<typename _Alloc>
|
1706 |
|
|
_Before_begin(_Alloc&& __a)
|
1707 |
|
|
: _NodeAlloc(std::forward<_Alloc>(__a))
|
1708 |
|
|
{ }
|
1709 |
|
|
};
|
1710 |
|
|
|
1711 |
|
|
//@} hashtable-detail
|
1712 |
|
|
_GLIBCXX_END_NAMESPACE_VERSION
|
1713 |
|
|
} // namespace __detail
|
1714 |
|
|
} // namespace std
|
1715 |
|
|
|
1716 |
|
|
#endif // _HASHTABLE_POLICY_H
|