williamr@4
|
1 |
/*
|
williamr@4
|
2 |
*
|
williamr@4
|
3 |
* Copyright (c) 1994
|
williamr@4
|
4 |
* Hewlett-Packard Company
|
williamr@4
|
5 |
*
|
williamr@4
|
6 |
* Copyright (c) 1996,1997
|
williamr@4
|
7 |
* Silicon Graphics Computer Systems, Inc.
|
williamr@4
|
8 |
*
|
williamr@4
|
9 |
* Copyright (c) 1997
|
williamr@4
|
10 |
* Moscow Center for SPARC Technology
|
williamr@4
|
11 |
*
|
williamr@4
|
12 |
* Copyright (c) 1999
|
williamr@4
|
13 |
* Boris Fomitchev
|
williamr@4
|
14 |
*
|
williamr@4
|
15 |
* This material is provided "as is", with absolutely no warranty expressed
|
williamr@4
|
16 |
* or implied. Any use is at your own risk.
|
williamr@4
|
17 |
*
|
williamr@4
|
18 |
* Permission to use or copy this software for any purpose is hereby granted
|
williamr@4
|
19 |
* without fee, provided the above notices are retained on all copies.
|
williamr@4
|
20 |
* Permission to modify the code and to distribute modified code is granted,
|
williamr@4
|
21 |
* provided the above notices are retained, and a notice that the code was
|
williamr@4
|
22 |
* modified is included with the above copyright notice.
|
williamr@4
|
23 |
*
|
williamr@4
|
24 |
*/
|
williamr@4
|
25 |
|
williamr@4
|
26 |
/* NOTE: This is an internal header file, included by other STL headers.
|
williamr@4
|
27 |
* You should not attempt to use it directly.
|
williamr@4
|
28 |
*/
|
williamr@4
|
29 |
|
williamr@4
|
30 |
#ifndef _STLP_INTERNAL_HASHTABLE_H
|
williamr@4
|
31 |
#define _STLP_INTERNAL_HASHTABLE_H
|
williamr@4
|
32 |
|
williamr@4
|
33 |
#ifndef _STLP_INTERNAL_VECTOR_H
|
williamr@4
|
34 |
# include <stl/_vector.h>
|
williamr@4
|
35 |
#endif
|
williamr@4
|
36 |
|
williamr@4
|
37 |
#ifndef _STLP_INTERNAL_SLIST_H
|
williamr@4
|
38 |
# include <stl/_slist.h>
|
williamr@4
|
39 |
#endif
|
williamr@4
|
40 |
|
williamr@4
|
41 |
#ifndef _STLP_INTERNAL_ITERATOR_H
|
williamr@4
|
42 |
# include <stl/_iterator.h>
|
williamr@4
|
43 |
#endif
|
williamr@4
|
44 |
|
williamr@4
|
45 |
#ifndef _STLP_INTERNAL_FUNCTION_BASE_H
|
williamr@4
|
46 |
# include <stl/_function_base.h>
|
williamr@4
|
47 |
#endif
|
williamr@4
|
48 |
|
williamr@4
|
49 |
#ifndef _STLP_INTERNAL_ALGOBASE_H
|
williamr@4
|
50 |
# include <stl/_algobase.h>
|
williamr@4
|
51 |
#endif
|
williamr@4
|
52 |
|
williamr@4
|
53 |
#ifndef _STLP_HASH_FUN_H
|
williamr@4
|
54 |
# include <stl/_hash_fun.h>
|
williamr@4
|
55 |
#endif
|
williamr@4
|
56 |
|
williamr@4
|
57 |
/*
|
williamr@4
|
58 |
* Hashtable class, used to implement the hashed associative containers
|
williamr@4
|
59 |
* hash_set, hash_map, hash_multiset, hash_multimap,
|
williamr@4
|
60 |
* unordered_set, unordered_map, unordered_multiset, unordered_multimap.
|
williamr@4
|
61 |
*/
|
williamr@4
|
62 |
|
williamr@4
|
63 |
_STLP_BEGIN_NAMESPACE
|
williamr@4
|
64 |
|
williamr@4
|
65 |
#if defined (_STLP_USE_TEMPLATE_EXPORT)
|
williamr@4
|
66 |
//Export of the classes used to represent buckets in the hashtable implementation.
|
williamr@4
|
67 |
# if !defined (_STLP_USE_PTR_SPECIALIZATIONS)
|
williamr@4
|
68 |
//If pointer specialization is enabled vector<_Slist_node_base*> will use the void*
|
williamr@4
|
69 |
//storage type for which internal classes have already been exported.
|
williamr@4
|
70 |
_STLP_EXPORT_TEMPLATE_CLASS allocator<_STLP_PRIV _Slist_node_base*>;
|
williamr@4
|
71 |
|
williamr@4
|
72 |
_STLP_MOVE_TO_PRIV_NAMESPACE
|
williamr@4
|
73 |
_STLP_EXPORT_TEMPLATE_CLASS _STLP_alloc_proxy<_Slist_node_base**, _Slist_node_base*,
|
williamr@4
|
74 |
allocator<_Slist_node_base*> >;
|
williamr@4
|
75 |
_STLP_EXPORT_TEMPLATE_CLASS _Vector_base<_Slist_node_base*,
|
williamr@4
|
76 |
allocator<_Slist_node_base*> >;
|
williamr@4
|
77 |
_STLP_MOVE_TO_STD_NAMESPACE
|
williamr@4
|
78 |
# endif
|
williamr@4
|
79 |
|
williamr@4
|
80 |
# if defined (_STLP_DEBUG)
|
williamr@4
|
81 |
_STLP_MOVE_TO_PRIV_NAMESPACE
|
williamr@4
|
82 |
# define _STLP_NON_DBG_VECTOR _STLP_NON_DBG_NAME(vector)
|
williamr@4
|
83 |
_STLP_EXPORT_TEMPLATE_CLASS __construct_checker<_STLP_NON_DBG_VECTOR<_Slist_node_base*, allocator<_Slist_node_base*> > >;
|
williamr@4
|
84 |
_STLP_EXPORT_TEMPLATE_CLASS _STLP_NON_DBG_VECTOR<_Slist_node_base*, allocator<_Slist_node_base*> >;
|
williamr@4
|
85 |
# undef _STLP_NON_DBG_VECTOR
|
williamr@4
|
86 |
_STLP_MOVE_TO_STD_NAMESPACE
|
williamr@4
|
87 |
# endif
|
williamr@4
|
88 |
|
williamr@4
|
89 |
_STLP_EXPORT_TEMPLATE_CLASS vector<_STLP_PRIV _Slist_node_base*,
|
williamr@4
|
90 |
allocator<_STLP_PRIV _Slist_node_base*> >;
|
williamr@4
|
91 |
#endif
|
williamr@4
|
92 |
|
williamr@4
|
93 |
#if defined (_STLP_DEBUG)
|
williamr@4
|
94 |
# define hashtable _STLP_NON_DBG_NAME(hashtable)
|
williamr@4
|
95 |
_STLP_MOVE_TO_PRIV_NAMESPACE
|
williamr@4
|
96 |
#endif
|
williamr@4
|
97 |
|
williamr@4
|
98 |
// some compilers require the names of template parameters to be the same
|
williamr@4
|
99 |
template <class _Val, class _Key, class _HF,
|
williamr@4
|
100 |
class _Traits, class _ExK, class _EqK, class _All>
|
williamr@4
|
101 |
class hashtable;
|
williamr@4
|
102 |
|
williamr@4
|
103 |
#if !defined (_STLP_DEBUG)
|
williamr@4
|
104 |
_STLP_MOVE_TO_PRIV_NAMESPACE
|
williamr@4
|
105 |
#endif
|
williamr@4
|
106 |
|
williamr@4
|
107 |
template <class _BaseIte, class _Traits>
|
williamr@4
|
108 |
struct _Ht_iterator {
|
williamr@4
|
109 |
typedef typename _Traits::_ConstTraits _ConstTraits;
|
williamr@4
|
110 |
typedef typename _Traits::_NonConstTraits _NonConstTraits;
|
williamr@4
|
111 |
|
williamr@4
|
112 |
typedef _Ht_iterator<_BaseIte,_Traits> _Self;
|
williamr@4
|
113 |
|
williamr@4
|
114 |
typedef typename _Traits::value_type value_type;
|
williamr@4
|
115 |
typedef typename _Traits::pointer pointer;
|
williamr@4
|
116 |
typedef typename _Traits::reference reference;
|
williamr@4
|
117 |
typedef forward_iterator_tag iterator_category;
|
williamr@4
|
118 |
typedef ptrdiff_t difference_type;
|
williamr@4
|
119 |
typedef size_t size_type;
|
williamr@4
|
120 |
|
williamr@4
|
121 |
typedef _Ht_iterator<_BaseIte, _NonConstTraits> iterator;
|
williamr@4
|
122 |
typedef _Ht_iterator<_BaseIte, _ConstTraits> const_iterator;
|
williamr@4
|
123 |
|
williamr@4
|
124 |
_Ht_iterator() {}
|
williamr@4
|
125 |
//copy constructor for iterator and constructor from iterator for const_iterator
|
williamr@4
|
126 |
_Ht_iterator(const iterator& __it) : _M_ite(__it._M_ite) {}
|
williamr@4
|
127 |
_Ht_iterator(_BaseIte __it) : _M_ite(__it) {}
|
williamr@4
|
128 |
|
williamr@4
|
129 |
reference operator*() const {
|
williamr@4
|
130 |
return *_M_ite;
|
williamr@4
|
131 |
}
|
williamr@4
|
132 |
_STLP_DEFINE_ARROW_OPERATOR
|
williamr@4
|
133 |
|
williamr@4
|
134 |
_Self& operator++() {
|
williamr@4
|
135 |
++_M_ite;
|
williamr@4
|
136 |
return *this;
|
williamr@4
|
137 |
}
|
williamr@4
|
138 |
_Self operator++(int) {
|
williamr@4
|
139 |
_Self __tmp = *this;
|
williamr@4
|
140 |
++*this;
|
williamr@4
|
141 |
return __tmp;
|
williamr@4
|
142 |
}
|
williamr@4
|
143 |
|
williamr@4
|
144 |
bool operator == (const_iterator __rhs) const {
|
williamr@4
|
145 |
return _M_ite == __rhs._M_ite;
|
williamr@4
|
146 |
}
|
williamr@4
|
147 |
bool operator != (const_iterator __rhs) const {
|
williamr@4
|
148 |
return _M_ite != __rhs._M_ite;
|
williamr@4
|
149 |
}
|
williamr@4
|
150 |
|
williamr@4
|
151 |
_BaseIte _M_ite;
|
williamr@4
|
152 |
};
|
williamr@4
|
153 |
|
williamr@4
|
154 |
_STLP_MOVE_TO_STD_NAMESPACE
|
williamr@4
|
155 |
|
williamr@4
|
156 |
#if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
|
williamr@4
|
157 |
template <class _BaseIte, class _Traits>
|
williamr@4
|
158 |
struct __type_traits<_STLP_PRIV _Ht_iterator<_BaseIte, _Traits> > {
|
williamr@4
|
159 |
typedef __false_type has_trivial_default_constructor;
|
williamr@4
|
160 |
typedef __true_type has_trivial_copy_constructor;
|
williamr@4
|
161 |
typedef __true_type has_trivial_assignment_operator;
|
williamr@4
|
162 |
typedef __true_type has_trivial_destructor;
|
williamr@4
|
163 |
typedef __false_type is_POD_type;
|
williamr@4
|
164 |
};
|
williamr@4
|
165 |
#endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
|
williamr@4
|
166 |
|
williamr@4
|
167 |
#if defined (_STLP_USE_OLD_HP_ITERATOR_QUERIES)
|
williamr@4
|
168 |
template <class _BaseIte, class _Traits>
|
williamr@4
|
169 |
inline
|
williamr@4
|
170 |
# if defined (_STLP_NESTED_TYPE_PARAM_BUG)
|
williamr@4
|
171 |
_STLP_TYPENAME_ON_RETURN_TYPE _Traits::value_type *
|
williamr@4
|
172 |
# else
|
williamr@4
|
173 |
_STLP_TYPENAME_ON_RETURN_TYPE _STLP_PRIV _Ht_iterator<_BaseIte,_Traits>::value_type *
|
williamr@4
|
174 |
# endif
|
williamr@4
|
175 |
value_type(const _STLP_PRIV _Ht_iterator<_BaseIte,_Traits>&) {
|
williamr@4
|
176 |
typedef typename _STLP_PRIV _Ht_iterator<_BaseIte,_Traits>::value_type _Val;
|
williamr@4
|
177 |
return (_Val*) 0;
|
williamr@4
|
178 |
}
|
williamr@4
|
179 |
template <class _BaseIte, class _Traits>
|
williamr@4
|
180 |
inline forward_iterator_tag iterator_category(const _STLP_PRIV _Ht_iterator<_BaseIte,_Traits>&)
|
williamr@4
|
181 |
{ return forward_iterator_tag(); }
|
williamr@4
|
182 |
template <class _BaseIte, class _Traits>
|
williamr@4
|
183 |
inline ptrdiff_t* distance_type(const _STLP_PRIV _Ht_iterator<_BaseIte,_Traits>&)
|
williamr@4
|
184 |
{ return (ptrdiff_t*) 0; }
|
williamr@4
|
185 |
#endif
|
williamr@4
|
186 |
|
williamr@4
|
187 |
_STLP_MOVE_TO_PRIV_NAMESPACE
|
williamr@4
|
188 |
|
williamr@4
|
189 |
template <class _Dummy>
|
williamr@4
|
190 |
class _Stl_prime {
|
williamr@4
|
191 |
public:
|
williamr@4
|
192 |
//Returns the maximum number of buckets handled by the hashtable implementation
|
williamr@4
|
193 |
static size_t _STLP_CALL _S_max_nb_buckets();
|
williamr@4
|
194 |
|
williamr@4
|
195 |
//Returns the bucket size next to a required size
|
williamr@4
|
196 |
static size_t _STLP_CALL _S_next_size(size_t);
|
williamr@4
|
197 |
};
|
williamr@4
|
198 |
|
williamr@4
|
199 |
#if defined (_STLP_USE_TEMPLATE_EXPORT)
|
williamr@4
|
200 |
_STLP_EXPORT_TEMPLATE_CLASS _Stl_prime<bool>;
|
williamr@4
|
201 |
#endif
|
williamr@4
|
202 |
|
williamr@4
|
203 |
typedef _Stl_prime<bool> _Stl_prime_type;
|
williamr@4
|
204 |
|
williamr@4
|
205 |
#if !defined (_STLP_DEBUG)
|
williamr@4
|
206 |
_STLP_MOVE_TO_STD_NAMESPACE
|
williamr@4
|
207 |
#endif
|
williamr@4
|
208 |
|
williamr@4
|
209 |
/*
|
williamr@4
|
210 |
* Hashtables handle allocators a bit differently than other containers
|
williamr@4
|
211 |
* do. If we're using standard-conforming allocators, then a hashtable
|
williamr@4
|
212 |
* unconditionally has a member variable to hold its allocator, even if
|
williamr@4
|
213 |
* it so happens that all instances of the allocator type are identical.
|
williamr@4
|
214 |
* This is because, for hashtables, this extra storage is negligible.
|
williamr@4
|
215 |
* Additionally, a base class wouldn't serve any other purposes; it
|
williamr@4
|
216 |
* wouldn't, for example, simplify the exception-handling code.
|
williamr@4
|
217 |
*/
|
williamr@4
|
218 |
template <class _Val, class _Key, class _HF,
|
williamr@4
|
219 |
class _Traits, class _ExK, class _EqK, class _All>
|
williamr@4
|
220 |
class hashtable {
|
williamr@4
|
221 |
typedef hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All> _Self;
|
williamr@4
|
222 |
typedef typename _Traits::_NonConstTraits _NonConstTraits;
|
williamr@4
|
223 |
typedef typename _Traits::_ConstTraits _ConstTraits;
|
williamr@4
|
224 |
typedef typename _Traits::_NonConstLocalTraits _NonConstLocalTraits;
|
williamr@4
|
225 |
typedef typename _Traits::_ConstLocalTraits _ConstLocalTraits;
|
williamr@4
|
226 |
|
williamr@4
|
227 |
public:
|
williamr@4
|
228 |
typedef _Key key_type;
|
williamr@4
|
229 |
typedef _Val value_type;
|
williamr@4
|
230 |
typedef _HF hasher;
|
williamr@4
|
231 |
typedef _EqK key_equal;
|
williamr@4
|
232 |
|
williamr@4
|
233 |
typedef size_t size_type;
|
williamr@4
|
234 |
typedef ptrdiff_t difference_type;
|
williamr@4
|
235 |
typedef typename _NonConstTraits::pointer pointer;
|
williamr@4
|
236 |
typedef const value_type* const_pointer;
|
williamr@4
|
237 |
typedef typename _NonConstTraits::reference reference;
|
williamr@4
|
238 |
typedef const value_type& const_reference;
|
williamr@4
|
239 |
typedef forward_iterator_tag _Iterator_category;
|
williamr@4
|
240 |
|
williamr@4
|
241 |
hasher hash_funct() const { return _M_hash; }
|
williamr@4
|
242 |
key_equal key_eq() const { return _M_equals; }
|
williamr@4
|
243 |
|
williamr@4
|
244 |
private:
|
williamr@4
|
245 |
_STLP_FORCE_ALLOCATORS(_Val, _All)
|
williamr@4
|
246 |
#if defined (_STLP_DEBUG)
|
williamr@4
|
247 |
typedef _STLP_PRIV _STLP_NON_DBG_NAME(slist)<value_type, _All> _ElemsCont;
|
williamr@4
|
248 |
#else
|
williamr@4
|
249 |
typedef slist<value_type, _All> _ElemsCont;
|
williamr@4
|
250 |
#endif
|
williamr@4
|
251 |
typedef typename _ElemsCont::iterator _ElemsIte;
|
williamr@4
|
252 |
typedef typename _ElemsCont::const_iterator _ElemsConstIte;
|
williamr@4
|
253 |
typedef _STLP_PRIV _Slist_node_base _BucketType;
|
williamr@4
|
254 |
typedef typename _Alloc_traits<_BucketType*, _All>::allocator_type _M_bucket_allocator_type;
|
williamr@4
|
255 |
/*
|
williamr@4
|
256 |
* We are going to use vector of _Slist_node_base pointers for 2 reasons:
|
williamr@4
|
257 |
* - limit code bloat, all hashtable instanciation use the same buckets representation.
|
williamr@4
|
258 |
* - avoid _STLP_DEBUG performance trouble: with a vector of iterator on slist the resize
|
williamr@4
|
259 |
* method would be too slow because the slist::splice_after method become linear on
|
williamr@4
|
260 |
* the number of iterators in the buckets rather than constant in time as the iterator
|
williamr@4
|
261 |
* has to be move from a slist to the other.
|
williamr@4
|
262 |
*/
|
williamr@4
|
263 |
#if defined (_STLP_DEBUG)
|
williamr@4
|
264 |
typedef _STLP_PRIV _STLP_NON_DBG_NAME(vector)<_BucketType*, _M_bucket_allocator_type> _BucketVector;
|
williamr@4
|
265 |
#else
|
williamr@4
|
266 |
typedef vector<_BucketType*, _M_bucket_allocator_type> _BucketVector;
|
williamr@4
|
267 |
#endif
|
williamr@4
|
268 |
|
williamr@4
|
269 |
hasher _M_hash;
|
williamr@4
|
270 |
key_equal _M_equals;
|
williamr@4
|
271 |
_ExK _M_get_key;
|
williamr@4
|
272 |
_ElemsCont _M_elems;
|
williamr@4
|
273 |
_BucketVector _M_buckets;
|
williamr@4
|
274 |
size_type _M_num_elements;
|
williamr@4
|
275 |
float _M_max_load_factor;
|
williamr@4
|
276 |
_STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
|
williamr@4
|
277 |
|
williamr@4
|
278 |
public:
|
williamr@4
|
279 |
typedef _STLP_PRIV _Ht_iterator<_ElemsIte, _NonConstTraits> iterator;
|
williamr@4
|
280 |
typedef _STLP_PRIV _Ht_iterator<_ElemsIte, _ConstTraits> const_iterator;
|
williamr@4
|
281 |
//TODO: Avoids this debug check and make the local_iterator different from
|
williamr@4
|
282 |
//iterator in debug mode too.
|
williamr@4
|
283 |
#if !defined (_STLP_DEBUG)
|
williamr@4
|
284 |
typedef _STLP_PRIV _Ht_iterator<_ElemsIte, _NonConstLocalTraits> local_iterator;
|
williamr@4
|
285 |
typedef _STLP_PRIV _Ht_iterator<_ElemsIte, _ConstLocalTraits> const_local_iterator;
|
williamr@4
|
286 |
#else
|
williamr@4
|
287 |
typedef iterator local_iterator;
|
williamr@4
|
288 |
typedef const_iterator const_local_iterator;
|
williamr@4
|
289 |
#endif
|
williamr@4
|
290 |
|
williamr@4
|
291 |
typedef typename _Alloc_traits<_Val, _All>::allocator_type allocator_type;
|
williamr@4
|
292 |
allocator_type get_allocator() const { return _M_elems.get_allocator(); }
|
williamr@4
|
293 |
|
williamr@4
|
294 |
#if !defined (_STLP_DONT_SUP_DFLT_PARAM)
|
williamr@4
|
295 |
hashtable(size_type __n,
|
williamr@4
|
296 |
const _HF& __hf,
|
williamr@4
|
297 |
const _EqK& __eql,
|
williamr@4
|
298 |
const _ExK& __ext,
|
williamr@4
|
299 |
const allocator_type& __a = allocator_type())
|
williamr@4
|
300 |
#else
|
williamr@4
|
301 |
hashtable(size_type __n,
|
williamr@4
|
302 |
const _HF& __hf,
|
williamr@4
|
303 |
const _EqK& __eql,
|
williamr@4
|
304 |
const _ExK& __ext)
|
williamr@4
|
305 |
: _M_hash(__hf),
|
williamr@4
|
306 |
_M_equals(__eql),
|
williamr@4
|
307 |
_M_get_key(__ext),
|
williamr@4
|
308 |
_M_elems(allocator_type()),
|
williamr@4
|
309 |
_M_buckets(_STLP_CONVERT_ALLOCATOR(__a, _BucketType*)),
|
williamr@4
|
310 |
_M_num_elements(0),
|
williamr@4
|
311 |
_M_max_load_factor(1.0f)
|
williamr@4
|
312 |
{ _M_initialize_buckets(__n); }
|
williamr@4
|
313 |
|
williamr@4
|
314 |
hashtable(size_type __n,
|
williamr@4
|
315 |
const _HF& __hf,
|
williamr@4
|
316 |
const _EqK& __eql,
|
williamr@4
|
317 |
const _ExK& __ext,
|
williamr@4
|
318 |
const allocator_type& __a)
|
williamr@4
|
319 |
#endif
|
williamr@4
|
320 |
: _M_hash(__hf),
|
williamr@4
|
321 |
_M_equals(__eql),
|
williamr@4
|
322 |
_M_get_key(__ext),
|
williamr@4
|
323 |
_M_elems(__a),
|
williamr@4
|
324 |
_M_buckets(_STLP_CONVERT_ALLOCATOR(__a, _BucketType*)),
|
williamr@4
|
325 |
_M_num_elements(0),
|
williamr@4
|
326 |
_M_max_load_factor(1.0f)
|
williamr@4
|
327 |
{ _M_initialize_buckets(__n); }
|
williamr@4
|
328 |
|
williamr@4
|
329 |
#if !defined (_STLP_DONT_SUP_DFLT_PARAM)
|
williamr@4
|
330 |
hashtable(size_type __n,
|
williamr@4
|
331 |
const _HF& __hf,
|
williamr@4
|
332 |
const _EqK& __eql,
|
williamr@4
|
333 |
const allocator_type& __a = allocator_type())
|
williamr@4
|
334 |
#else
|
williamr@4
|
335 |
hashtable(size_type __n,
|
williamr@4
|
336 |
const _HF& __hf,
|
williamr@4
|
337 |
const _EqK& __eql)
|
williamr@4
|
338 |
: _M_hash(__hf),
|
williamr@4
|
339 |
_M_equals(__eql),
|
williamr@4
|
340 |
_M_get_key(_ExK()),
|
williamr@4
|
341 |
_M_elems(allocator_type()),
|
williamr@4
|
342 |
_M_buckets(_STLP_CONVERT_ALLOCATOR(__a, _BucketType*)),
|
williamr@4
|
343 |
_M_num_elements(0),
|
williamr@4
|
344 |
_M_max_load_factor(1.0f)
|
williamr@4
|
345 |
{ _M_initialize_buckets(__n); }
|
williamr@4
|
346 |
|
williamr@4
|
347 |
hashtable(size_type __n,
|
williamr@4
|
348 |
const _HF& __hf,
|
williamr@4
|
349 |
const _EqK& __eql,
|
williamr@4
|
350 |
const allocator_type& __a)
|
williamr@4
|
351 |
#endif
|
williamr@4
|
352 |
: _M_hash(__hf),
|
williamr@4
|
353 |
_M_equals(__eql),
|
williamr@4
|
354 |
_M_get_key(_ExK()),
|
williamr@4
|
355 |
_M_elems(__a),
|
williamr@4
|
356 |
_M_buckets(_STLP_CONVERT_ALLOCATOR(__a, _BucketType*)),
|
williamr@4
|
357 |
_M_num_elements(0),
|
williamr@4
|
358 |
_M_max_load_factor(1.0f)
|
williamr@4
|
359 |
{ _M_initialize_buckets(__n); }
|
williamr@4
|
360 |
|
williamr@4
|
361 |
hashtable(const _Self& __ht)
|
williamr@4
|
362 |
: _M_hash(__ht._M_hash),
|
williamr@4
|
363 |
_M_equals(__ht._M_equals),
|
williamr@4
|
364 |
_M_get_key(__ht._M_get_key),
|
williamr@4
|
365 |
_M_elems(__ht.get_allocator()),
|
williamr@4
|
366 |
_M_buckets(_STLP_CONVERT_ALLOCATOR(__ht.get_allocator(), _BucketType*)),
|
williamr@4
|
367 |
_M_num_elements(0),
|
williamr@4
|
368 |
_M_max_load_factor(1.0f)
|
williamr@4
|
369 |
{ _M_copy_from(__ht); }
|
williamr@4
|
370 |
|
williamr@4
|
371 |
hashtable(__move_source<_Self> src)
|
williamr@4
|
372 |
: _M_hash(_STLP_PRIV _AsMoveSource(src.get()._M_hash)),
|
williamr@4
|
373 |
_M_equals(_STLP_PRIV _AsMoveSource(src.get()._M_equals)),
|
williamr@4
|
374 |
_M_get_key(_STLP_PRIV _AsMoveSource(src.get()._M_get_key)),
|
williamr@4
|
375 |
_M_elems(__move_source<_ElemsCont>(src.get()._M_elems)),
|
williamr@4
|
376 |
_M_buckets(__move_source<_BucketVector>(src.get()._M_buckets)),
|
williamr@4
|
377 |
_M_num_elements(src.get()._M_num_elements),
|
williamr@4
|
378 |
_M_max_load_factor(src.get()._M_max_load_factor) {}
|
williamr@4
|
379 |
|
williamr@4
|
380 |
_Self& operator= (const _Self& __ht) {
|
williamr@4
|
381 |
if (&__ht != this) {
|
williamr@4
|
382 |
clear();
|
williamr@4
|
383 |
_M_hash = __ht._M_hash;
|
williamr@4
|
384 |
_M_equals = __ht._M_equals;
|
williamr@4
|
385 |
_M_get_key = __ht._M_get_key;
|
williamr@4
|
386 |
_M_copy_from(__ht);
|
williamr@4
|
387 |
}
|
williamr@4
|
388 |
return *this;
|
williamr@4
|
389 |
}
|
williamr@4
|
390 |
|
williamr@4
|
391 |
~hashtable() { clear(); }
|
williamr@4
|
392 |
|
williamr@4
|
393 |
size_type size() const { return _M_num_elements; }
|
williamr@4
|
394 |
size_type max_size() const { return size_type(-1); }
|
williamr@4
|
395 |
bool empty() const { return size() == 0; }
|
williamr@4
|
396 |
|
williamr@4
|
397 |
void swap(_Self& __ht) {
|
williamr@4
|
398 |
_STLP_STD::swap(_M_hash, __ht._M_hash);
|
williamr@4
|
399 |
_STLP_STD::swap(_M_equals, __ht._M_equals);
|
williamr@4
|
400 |
_STLP_STD::swap(_M_get_key, __ht._M_get_key);
|
williamr@4
|
401 |
_M_elems.swap(__ht._M_elems);
|
williamr@4
|
402 |
_M_buckets.swap(__ht._M_buckets);
|
williamr@4
|
403 |
_STLP_STD::swap(_M_num_elements, __ht._M_num_elements);
|
williamr@4
|
404 |
_STLP_STD::swap(_M_max_load_factor, __ht._M_max_load_factor);
|
williamr@4
|
405 |
}
|
williamr@4
|
406 |
|
williamr@4
|
407 |
iterator begin() { return _M_elems.begin(); }
|
williamr@4
|
408 |
iterator end() { return _M_elems.end(); }
|
williamr@4
|
409 |
local_iterator begin(size_type __n) { return _ElemsIte(_M_buckets[__n]); }
|
williamr@4
|
410 |
local_iterator end(size_type __n) { return _ElemsIte(_M_buckets[__n + 1]); }
|
williamr@4
|
411 |
|
williamr@4
|
412 |
const_iterator begin() const { return __CONST_CAST(_ElemsCont&, _M_elems).begin(); }
|
williamr@4
|
413 |
const_iterator end() const { return __CONST_CAST(_ElemsCont&, _M_elems).end(); }
|
williamr@4
|
414 |
const_local_iterator begin(size_type __n) const { return _ElemsIte(_M_buckets[__n]); }
|
williamr@4
|
415 |
const_local_iterator end(size_type __n) const { return _ElemsIte(_M_buckets[__n + 1]); }
|
williamr@4
|
416 |
|
williamr@4
|
417 |
//static bool _STLP_CALL _M_equal (const _Self&, const _Self&);
|
williamr@4
|
418 |
|
williamr@4
|
419 |
public:
|
williamr@4
|
420 |
//The number of buckets is size() - 1 because the last bucket always contains
|
williamr@4
|
421 |
//_M_elems.end() to make algo easier to implement.
|
williamr@4
|
422 |
size_type bucket_count() const { return _M_buckets.size() - 1; }
|
williamr@4
|
423 |
size_type max_bucket_count() const { return _STLP_PRIV _Stl_prime_type::_S_max_nb_buckets(); }
|
williamr@4
|
424 |
size_type elems_in_bucket(size_type __bucket) const
|
williamr@4
|
425 |
{ return distance(_ElemsIte(_M_buckets[__bucket]), _ElemsIte(_M_buckets[__bucket + 1])); }
|
williamr@4
|
426 |
|
williamr@4
|
427 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
428 |
size_type bucket(const _KT& __k) const { return _M_bkt_num_key(__k); }
|
williamr@4
|
429 |
|
williamr@4
|
430 |
// hash policy
|
williamr@4
|
431 |
float load_factor() const { return (float)size() / (float)bucket_count(); }
|
williamr@4
|
432 |
float max_load_factor() const { return _M_max_load_factor; }
|
williamr@4
|
433 |
void max_load_factor(float __z) { _M_max_load_factor = __z;}
|
williamr@4
|
434 |
|
williamr@4
|
435 |
pair<iterator, bool> insert_unique(const value_type& __obj) {
|
williamr@4
|
436 |
resize(_M_num_elements + 1);
|
williamr@4
|
437 |
return insert_unique_noresize(__obj);
|
williamr@4
|
438 |
}
|
williamr@4
|
439 |
|
williamr@4
|
440 |
iterator insert_equal(const value_type& __obj) {
|
williamr@4
|
441 |
resize(_M_num_elements + 1);
|
williamr@4
|
442 |
return insert_equal_noresize(__obj);
|
williamr@4
|
443 |
}
|
williamr@4
|
444 |
|
williamr@4
|
445 |
protected:
|
williamr@4
|
446 |
iterator _M_insert_noresize(size_type __n, const value_type& __obj);
|
williamr@4
|
447 |
public:
|
williamr@4
|
448 |
pair<iterator, bool> insert_unique_noresize(const value_type& __obj);
|
williamr@4
|
449 |
iterator insert_equal_noresize(const value_type& __obj);
|
williamr@4
|
450 |
|
williamr@4
|
451 |
#if defined (_STLP_MEMBER_TEMPLATES)
|
williamr@4
|
452 |
template <class _InputIterator>
|
williamr@4
|
453 |
void insert_unique(_InputIterator __f, _InputIterator __l)
|
williamr@4
|
454 |
{ insert_unique(__f, __l, _STLP_ITERATOR_CATEGORY(__f, _InputIterator)); }
|
williamr@4
|
455 |
|
williamr@4
|
456 |
template <class _InputIterator>
|
williamr@4
|
457 |
void insert_equal(_InputIterator __f, _InputIterator __l)
|
williamr@4
|
458 |
{ insert_equal(__f, __l, _STLP_ITERATOR_CATEGORY(__f, _InputIterator)); }
|
williamr@4
|
459 |
|
williamr@4
|
460 |
template <class _InputIterator>
|
williamr@4
|
461 |
void insert_unique(_InputIterator __f, _InputIterator __l,
|
williamr@4
|
462 |
const input_iterator_tag &) {
|
williamr@4
|
463 |
for ( ; __f != __l; ++__f)
|
williamr@4
|
464 |
insert_unique(*__f);
|
williamr@4
|
465 |
}
|
williamr@4
|
466 |
|
williamr@4
|
467 |
template <class _InputIterator>
|
williamr@4
|
468 |
void insert_equal(_InputIterator __f, _InputIterator __l,
|
williamr@4
|
469 |
const input_iterator_tag &) {
|
williamr@4
|
470 |
for ( ; __f != __l; ++__f)
|
williamr@4
|
471 |
insert_equal(*__f);
|
williamr@4
|
472 |
}
|
williamr@4
|
473 |
|
williamr@4
|
474 |
template <class _ForwardIterator>
|
williamr@4
|
475 |
void insert_unique(_ForwardIterator __f, _ForwardIterator __l,
|
williamr@4
|
476 |
const forward_iterator_tag &) {
|
williamr@4
|
477 |
size_type __n = distance(__f, __l);
|
williamr@4
|
478 |
resize(_M_num_elements + __n);
|
williamr@4
|
479 |
for ( ; __n > 0; --__n, ++__f)
|
williamr@4
|
480 |
insert_unique_noresize(*__f);
|
williamr@4
|
481 |
}
|
williamr@4
|
482 |
|
williamr@4
|
483 |
template <class _ForwardIterator>
|
williamr@4
|
484 |
void insert_equal(_ForwardIterator __f, _ForwardIterator __l,
|
williamr@4
|
485 |
const forward_iterator_tag &) {
|
williamr@4
|
486 |
size_type __n = distance(__f, __l);
|
williamr@4
|
487 |
resize(_M_num_elements + __n);
|
williamr@4
|
488 |
for ( ; __n > 0; --__n, ++__f)
|
williamr@4
|
489 |
insert_equal_noresize(*__f);
|
williamr@4
|
490 |
}
|
williamr@4
|
491 |
|
williamr@4
|
492 |
#else /* _STLP_MEMBER_TEMPLATES */
|
williamr@4
|
493 |
void insert_unique(const value_type* __f, const value_type* __l) {
|
williamr@4
|
494 |
size_type __n = __l - __f;
|
williamr@4
|
495 |
resize(_M_num_elements + __n);
|
williamr@4
|
496 |
for ( ; __n > 0; --__n, ++__f)
|
williamr@4
|
497 |
insert_unique_noresize(*__f);
|
williamr@4
|
498 |
}
|
williamr@4
|
499 |
|
williamr@4
|
500 |
void insert_equal(const value_type* __f, const value_type* __l) {
|
williamr@4
|
501 |
size_type __n = __l - __f;
|
williamr@4
|
502 |
resize(_M_num_elements + __n);
|
williamr@4
|
503 |
for ( ; __n > 0; --__n, ++__f)
|
williamr@4
|
504 |
insert_equal_noresize(*__f);
|
williamr@4
|
505 |
}
|
williamr@4
|
506 |
|
williamr@4
|
507 |
void insert_unique(const_iterator __f, const_iterator __l) {
|
williamr@4
|
508 |
size_type __n = distance(__f, __l);
|
williamr@4
|
509 |
resize(_M_num_elements + __n);
|
williamr@4
|
510 |
for ( ; __n > 0; --__n, ++__f)
|
williamr@4
|
511 |
insert_unique_noresize(*__f);
|
williamr@4
|
512 |
}
|
williamr@4
|
513 |
|
williamr@4
|
514 |
void insert_equal(const_iterator __f, const_iterator __l) {
|
williamr@4
|
515 |
size_type __n = distance(__f, __l);
|
williamr@4
|
516 |
resize(_M_num_elements + __n);
|
williamr@4
|
517 |
for ( ; __n > 0; --__n, ++__f)
|
williamr@4
|
518 |
insert_equal_noresize(*__f);
|
williamr@4
|
519 |
}
|
williamr@4
|
520 |
#endif /*_STLP_MEMBER_TEMPLATES */
|
williamr@4
|
521 |
|
williamr@4
|
522 |
//reference find_or_insert(const value_type& __obj);
|
williamr@4
|
523 |
|
williamr@4
|
524 |
private:
|
williamr@4
|
525 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
526 |
_ElemsIte _M_find(const _KT& __key) const {
|
williamr@4
|
527 |
size_type __n = _M_bkt_num_key(__key);
|
williamr@4
|
528 |
_ElemsIte __first(_M_buckets[__n]);
|
williamr@4
|
529 |
_ElemsIte __last(_M_buckets[__n + 1]);
|
williamr@4
|
530 |
for ( ; (__first != __last) && !_M_equals(_M_get_key(*__first), __key); ++__first);
|
williamr@4
|
531 |
if (__first != __last)
|
williamr@4
|
532 |
return __first;
|
williamr@4
|
533 |
else
|
williamr@4
|
534 |
return __CONST_CAST(_ElemsCont&, _M_elems).end();
|
williamr@4
|
535 |
}
|
williamr@4
|
536 |
|
williamr@4
|
537 |
public:
|
williamr@4
|
538 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
539 |
iterator find(const _KT& __key) { return _M_find(__key); }
|
williamr@4
|
540 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
541 |
const_iterator find(const _KT& __key) const { return _M_find(__key); }
|
williamr@4
|
542 |
|
williamr@4
|
543 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
544 |
size_type count(const _KT& __key) const {
|
williamr@4
|
545 |
const size_type __n = _M_bkt_num_key(__key);
|
williamr@4
|
546 |
|
williamr@4
|
547 |
_ElemsIte __cur(_M_buckets[__n]);
|
williamr@4
|
548 |
_ElemsIte __last(_M_buckets[__n + 1]);
|
williamr@4
|
549 |
for (; __cur != __last; ++__cur) {
|
williamr@4
|
550 |
if (_M_equals(_M_get_key(*__cur), __key)) {
|
williamr@4
|
551 |
size_type __result = 1;
|
williamr@4
|
552 |
for (++__cur;
|
williamr@4
|
553 |
__cur != __last && _M_equals(_M_get_key(*__cur), __key);
|
williamr@4
|
554 |
++__result, ++__cur);
|
williamr@4
|
555 |
return __result;
|
williamr@4
|
556 |
}
|
williamr@4
|
557 |
}
|
williamr@4
|
558 |
return 0;
|
williamr@4
|
559 |
}
|
williamr@4
|
560 |
|
williamr@4
|
561 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
562 |
pair<iterator, iterator> equal_range(const _KT& __key) {
|
williamr@4
|
563 |
typedef pair<iterator, iterator> _Pii;
|
williamr@4
|
564 |
const size_type __n = _M_bkt_num_key(__key);
|
williamr@4
|
565 |
|
williamr@4
|
566 |
for (_ElemsIte __first(_M_buckets[__n]), __last(_M_buckets[__n + 1]);
|
williamr@4
|
567 |
__first != __last; ++__first) {
|
williamr@4
|
568 |
if (_M_equals(_M_get_key(*__first), __key)) {
|
williamr@4
|
569 |
_ElemsIte __cur(__first);
|
williamr@4
|
570 |
for (++__cur; (__cur != __last) && _M_equals(_M_get_key(*__cur), __key); ++__cur);
|
williamr@4
|
571 |
return _Pii(__first, __cur);
|
williamr@4
|
572 |
}
|
williamr@4
|
573 |
}
|
williamr@4
|
574 |
return _Pii(end(), end());
|
williamr@4
|
575 |
}
|
williamr@4
|
576 |
|
williamr@4
|
577 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
578 |
pair<const_iterator, const_iterator> equal_range(const _KT& __key) const {
|
williamr@4
|
579 |
typedef pair<const_iterator, const_iterator> _Pii;
|
williamr@4
|
580 |
const size_type __n = _M_bkt_num_key(__key);
|
williamr@4
|
581 |
|
williamr@4
|
582 |
for (_ElemsIte __first(_M_buckets[__n]), __last(_M_buckets[__n + 1]);
|
williamr@4
|
583 |
__first != __last; ++__first) {
|
williamr@4
|
584 |
if (_M_equals(_M_get_key(*__first), __key)) {
|
williamr@4
|
585 |
_ElemsIte __cur(__first);
|
williamr@4
|
586 |
for (++__cur; (__cur != __last) && _M_equals(_M_get_key(*__cur), __key); ++__cur);
|
williamr@4
|
587 |
return _Pii(__first, __cur);
|
williamr@4
|
588 |
}
|
williamr@4
|
589 |
}
|
williamr@4
|
590 |
return _Pii(end(), end());
|
williamr@4
|
591 |
}
|
williamr@4
|
592 |
|
williamr@4
|
593 |
size_type erase(const key_type& __key);
|
williamr@4
|
594 |
void erase(const_iterator __it);
|
williamr@4
|
595 |
void erase(const_iterator __first, const_iterator __last);
|
williamr@4
|
596 |
|
williamr@4
|
597 |
private:
|
williamr@4
|
598 |
void _M_rehash(size_type __num_buckets);
|
williamr@4
|
599 |
#if defined (_STLP_DEBUG)
|
williamr@4
|
600 |
void _M_check() const;
|
williamr@4
|
601 |
#endif
|
williamr@4
|
602 |
|
williamr@4
|
603 |
public:
|
williamr@4
|
604 |
void rehash(size_type __num_buckets_hint);
|
williamr@4
|
605 |
void resize(size_type __num_elements_hint);
|
williamr@4
|
606 |
void clear();
|
williamr@4
|
607 |
|
williamr@4
|
608 |
// this is for hash_map::operator[]
|
williamr@4
|
609 |
reference _M_insert(const value_type& __obj);
|
williamr@4
|
610 |
|
williamr@4
|
611 |
private:
|
williamr@4
|
612 |
//__n is set to the first bucket that has to be modified if any
|
williamr@4
|
613 |
//erase/insert operation is done after the returned iterator.
|
williamr@4
|
614 |
iterator _M_before_begin(size_type &__n) const;
|
williamr@4
|
615 |
|
williamr@4
|
616 |
static iterator _S_before_begin(const _ElemsCont& __elems, const _BucketVector& __buckets,
|
williamr@4
|
617 |
size_type &__n);
|
williamr@4
|
618 |
|
williamr@4
|
619 |
void _M_initialize_buckets(size_type __n) {
|
williamr@4
|
620 |
const size_type __n_buckets = _STLP_PRIV _Stl_prime_type::_S_next_size(__n) + 1;
|
williamr@4
|
621 |
_M_buckets.reserve(__n_buckets);
|
williamr@4
|
622 |
_M_buckets.assign(__n_buckets, __STATIC_CAST(_BucketType*, 0));
|
williamr@4
|
623 |
}
|
williamr@4
|
624 |
|
williamr@4
|
625 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
626 |
size_type _M_bkt_num_key(const _KT& __key) const
|
williamr@4
|
627 |
{ return _M_bkt_num_key(__key, bucket_count()); }
|
williamr@4
|
628 |
|
williamr@4
|
629 |
size_type _M_bkt_num(const value_type& __obj) const
|
williamr@4
|
630 |
{ return _M_bkt_num_key(_M_get_key(__obj)); }
|
williamr@4
|
631 |
|
williamr@4
|
632 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
633 |
size_type _M_bkt_num_key(const _KT& __key, size_type __n) const
|
williamr@4
|
634 |
{ return _M_hash(__key) % __n; }
|
williamr@4
|
635 |
|
williamr@4
|
636 |
size_type _M_bkt_num(const value_type& __obj, size_t __n) const
|
williamr@4
|
637 |
{ return _M_bkt_num_key(_M_get_key(__obj), __n); }
|
williamr@4
|
638 |
|
williamr@4
|
639 |
void _M_copy_from(const _Self& __ht);
|
williamr@4
|
640 |
};
|
williamr@4
|
641 |
|
williamr@4
|
642 |
#if defined (_STLP_DEBUG)
|
williamr@4
|
643 |
# undef hashtable
|
williamr@4
|
644 |
_STLP_MOVE_TO_STD_NAMESPACE
|
williamr@4
|
645 |
#endif
|
williamr@4
|
646 |
|
williamr@4
|
647 |
_STLP_END_NAMESPACE
|
williamr@4
|
648 |
|
williamr@4
|
649 |
#if !defined (_STLP_LINK_TIME_INSTANTIATION)
|
williamr@4
|
650 |
# include <stl/_hashtable.c>
|
williamr@4
|
651 |
#endif
|
williamr@4
|
652 |
|
williamr@4
|
653 |
#if defined (_STLP_DEBUG)
|
williamr@4
|
654 |
# include <stl/debug/_hashtable.h>
|
williamr@4
|
655 |
#endif
|
williamr@4
|
656 |
|
williamr@4
|
657 |
_STLP_BEGIN_NAMESPACE
|
williamr@4
|
658 |
|
williamr@4
|
659 |
#define _STLP_TEMPLATE_HEADER template <class _Val, class _Key, class _HF, class _Traits, class _ExK, class _EqK, class _All>
|
williamr@4
|
660 |
#define _STLP_TEMPLATE_CONTAINER hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>
|
williamr@4
|
661 |
#include <stl/_relops_hash_cont.h>
|
williamr@4
|
662 |
#undef _STLP_TEMPLATE_CONTAINER
|
williamr@4
|
663 |
#undef _STLP_TEMPLATE_HEADER
|
williamr@4
|
664 |
|
williamr@4
|
665 |
#if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
|
williamr@4
|
666 |
template <class _Val, class _Key, class _HF, class _Traits, class _ExK, class _EqK, class _All>
|
williamr@4
|
667 |
struct __move_traits<hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All> > {
|
williamr@4
|
668 |
//Hashtables are movable:
|
williamr@4
|
669 |
typedef __stlp_movable implemented;
|
williamr@4
|
670 |
|
williamr@4
|
671 |
//Completeness depends on many template parameters, for the moment we consider it not complete:
|
williamr@4
|
672 |
typedef __false_type complete;
|
williamr@4
|
673 |
};
|
williamr@4
|
674 |
#endif
|
williamr@4
|
675 |
|
williamr@4
|
676 |
_STLP_END_NAMESPACE
|
williamr@4
|
677 |
|
williamr@4
|
678 |
#endif /* _STLP_INTERNAL_HASHTABLE_H */
|
williamr@4
|
679 |
|
williamr@4
|
680 |
// Local Variables:
|
williamr@4
|
681 |
// mode:C++
|
williamr@4
|
682 |
// End:
|