1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
|
/*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Copyright (c) 1996,1997
* Silicon Graphics Computer Systems, Inc.
*
* Copyright (c) 1997
* Moscow Center for SPARC Technology
*
* Copyright (c) 1999
* Boris Fomitchev
*
* This material is provided "as is", with absolutely no warranty expressed
* or implied. Any use is at your own risk.
*
* Permission to use or copy this software for any purpose is hereby granted
* without fee, provided the above notices are retained on all copies.
* Permission to modify the code and to distribute modified code is granted,
* provided the above notices are retained, and a notice that the code was
* modified is included with the above copyright notice.
*
*/
#ifndef _STLP_HASHTABLE_C
#define _STLP_HASHTABLE_C
#ifndef _STLP_INTERNAL_HASHTABLE_H
# include <stl/_hashtable.h>
#endif
_STLP_BEGIN_NAMESPACE
#if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION)
_STLP_MOVE_TO_PRIV_NAMESPACE
# define __PRIME_LIST_BODY { \
7ul, 23ul, \
53ul, 97ul, 193ul, 389ul, 769ul, \
1543ul, 3079ul, 6151ul, 12289ul, 24593ul, \
49157ul, 98317ul, 196613ul, 393241ul, 786433ul, \
1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul, \
50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,\
1610612741ul, 3221225473ul, 4294967291ul \
}
template <class _Dummy>
size_t _STLP_CALL
_Stl_prime<_Dummy>::_S_max_nb_buckets() {
const size_t _list[] = __PRIME_LIST_BODY;
# ifndef __MWERKS__
return _list[(sizeof(_list)/sizeof(_list[0])) - 1];
# else
return _list[30/sizeof(size_t) - 1]; // stupid MWERKS!
# endif
}
template <class _Dummy>
size_t _STLP_CALL
_Stl_prime<_Dummy>::_S_next_size(size_t __n) {
static const size_t _list[] = __PRIME_LIST_BODY;
const size_t* __first = _list;
# ifndef __MWERKS__
const size_t* __last = _list + (sizeof(_list)/sizeof(_list[0]));
# else
const size_t* __last = _list + (30/sizeof(size_t)); // stupid MWERKS
# endif
const size_t* pos = __lower_bound(__first, __last, __n,
__less((size_t*)0), __less((size_t*)0), (ptrdiff_t*)0);
return (pos == __last ? *(__last - 1) : *pos);
}
# undef __PRIME_LIST_BODY
_STLP_MOVE_TO_STD_NAMESPACE
#endif
#if defined (_STLP_DEBUG)
# define hashtable _STLP_NON_DBG_NAME(hashtable)
_STLP_MOVE_TO_PRIV_NAMESPACE
#endif
// fbp: these defines are for outline methods definitions.
// needed to definitions to be portable. Should not be used in method bodies.
#if defined ( _STLP_NESTED_TYPE_PARAM_BUG )
# define __size_type__ size_t
# define size_type size_t
# define value_type _Val
# define key_type _Key
# define __reference__ _Val&
# define __iterator__ _Ht_iterator<_Val, _STLP_HEADER_TYPENAME _Traits::_NonConstTraits, \
_Key, _HF, _ExK, _EqK, _All>
# define __const_iterator__ _Ht_iterator<_Val, _STLP_HEADER_TYPENAME _Traits::_ConstTraits, \
_Key, _HF, _ExK, _EqK, _All>
#else
# define __size_type__ _STLP_TYPENAME_ON_RETURN_TYPE hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All>::size_type
# define __reference__ _STLP_TYPENAME_ON_RETURN_TYPE hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All>::reference
# define __iterator__ _STLP_TYPENAME_ON_RETURN_TYPE hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All>::iterator
# define __const_iterator__ _STLP_TYPENAME_ON_RETURN_TYPE hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All>::const_iterator
#endif
/*
* This method is too difficult to implement for hashtable that do not
* require a sorted operation on the stored type.
template <class _Val, class _Key, class _HF,
class _Traits, class _ExK, class _EqK, class _All>
bool hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>::_M_equal(
const hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>& __ht1,
const hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>& __ht2) {
return __ht1._M_buckets == __ht2._M_buckets &&
__ht1._M_elems == __ht2._M_elems;
}
*/
/* Returns the iterator before the first iterator of the bucket __n and set
* __n to the first previous bucket having the same first iterator as bucket
* __n.
*/
template <class _Val, class _Key, class _HF,
class _Traits, class _ExK, class _EqK, class _All>
__iterator__
hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>
::_M_before_begin(size_type &__n) const {
return _S_before_begin(_M_elems, _M_buckets, __n);
}
template <class _Val, class _Key, class _HF,
class _Traits, class _ExK, class _EqK, class _All>
__iterator__
hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>
::_S_before_begin(const _ElemsCont& __elems, const _BucketVector& __buckets,
size_type &__n) {
_ElemsCont &__mutable_elems = __CONST_CAST(_ElemsCont&, __elems);
typename _BucketVector::const_iterator __bpos(__buckets.begin() + __n);
_ElemsIte __pos(*__bpos);
if (__pos == __mutable_elems.begin()) {
__n = 0;
return __mutable_elems.before_begin();
}
typename _BucketVector::const_iterator __bcur(__bpos);
_BucketType *__pos_node = __pos._M_node;
for (--__bcur; __pos_node == *__bcur; --__bcur);
__n = __bcur - __buckets.begin() + 1;
_ElemsIte __cur(*__bcur);
_ElemsIte __prev = __cur++;
for (; __cur != __pos; ++__prev, ++__cur);
return __prev;
}
template <class _Val, class _Key, class _HF,
class _Traits, class _ExK, class _EqK, class _All>
__iterator__
hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>
::_M_insert_noresize(size_type __n, const value_type& __obj) {
//We always insert this element as 1st in the bucket to not break
//the elements order as equal elements must be kept next to each other.
size_type __prev = __n;
_ElemsIte __pos = _M_before_begin(__prev)._M_ite;
fill(_M_buckets.begin() + __prev, _M_buckets.begin() + __n + 1,
_M_elems.insert_after(__pos, __obj)._M_node);
++_M_num_elements;
return iterator(_ElemsIte(_M_buckets[__n]));
}
template <class _Val, class _Key, class _HF,
class _Traits, class _ExK, class _EqK, class _All>
pair<__iterator__, bool>
hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>
::insert_unique_noresize(const value_type& __obj) {
const size_type __n = _M_bkt_num(__obj);
_ElemsIte __cur(_M_buckets[__n]);
_ElemsIte __last(_M_buckets[__n + 1]);
if (__cur != __last) {
for (; __cur != __last; ++__cur) {
if (_M_equals(_M_get_key(*__cur), _M_get_key(__obj))) {
//We check that equivalent keys have equals hash code as otherwise, on resize,
//equivalent value might not be in the same bucket
_STLP_ASSERT(_M_hash(_M_get_key(*__cur)) == _M_hash(_M_get_key(__obj)))
return pair<iterator, bool>(iterator(__cur), false);
}
}
/* Here we do not rely on the _M_insert_noresize method as we know
* that we cannot break element orders, elements are unique, and
* insertion after the first bucket element is faster than what is
* done in _M_insert_noresize.
*/
__cur = _M_elems.insert_after(_ElemsIte(_M_buckets[__n]), __obj);
++_M_num_elements;
return pair<iterator, bool>(iterator(__cur), true);
}
return pair<iterator, bool>(_M_insert_noresize(__n, __obj), true);
}
template <class _Val, class _Key, class _HF,
class _Traits, class _ExK, class _EqK, class _All>
__iterator__
hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>
::insert_equal_noresize(const value_type& __obj) {
const size_type __n = _M_bkt_num(__obj);
{
_ElemsIte __cur(_M_buckets[__n]);
_ElemsIte __last(_M_buckets[__n + 1]);
for (; __cur != __last; ++__cur) {
if (_M_equals(_M_get_key(*__cur), _M_get_key(__obj))) {
//We check that equivalent keys have equals hash code as otherwise, on resize,
//equivalent value might not be in the same bucket
_STLP_ASSERT(_M_hash(_M_get_key(*__cur)) == _M_hash(_M_get_key(__obj)))
++_M_num_elements;
return _M_elems.insert_after(__cur, __obj);
}
}
}
return _M_insert_noresize(__n, __obj);
}
template <class _Val, class _Key, class _HF,
class _Traits, class _ExK, class _EqK, class _All>
__reference__
hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>
::_M_insert(const value_type& __obj) {
resize(_M_num_elements + 1);
return *insert_unique_noresize(__obj).first;
}
/*
template <class _Val, class _Key, class _HF,
class _Traits, class _ExK, class _EqK, class _All>
__reference__
hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>
::find_or_insert(const value_type& __obj) {
_Node* __first = _M_find(_M_get_key(__obj));
if (__first)
return __first->_M_val;
else
return _M_insert(__obj);
}
*/
template <class _Val, class _Key, class _HF,
class _Traits, class _ExK, class _EqK, class _All>
__size_type__
hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>
::erase(const key_type& __key) {
const size_type __n = _M_bkt_num_key(__key);
_ElemsIte __cur(_M_buckets[__n]);
_ElemsIte __last(_M_buckets[__n + 1]);
if (__cur == __last)
return 0;
size_type __erased = 0;
if (_M_equals(_M_get_key(*__cur), __key)) {
//We look for the pos before __cur:
size_type __prev_b = __n;
_ElemsIte __prev = _M_before_begin(__prev_b)._M_ite;
do {
__cur = _M_elems.erase_after(__prev);
++__erased;
} while ((__cur != __last) && _M_equals(_M_get_key(*__cur), __key));
fill(_M_buckets.begin() + __prev_b, _M_buckets.begin() + __n + 1, __cur._M_node);
}
else {
_ElemsIte __prev = __cur++;
for (; __cur != __last; ++__prev, ++__cur) {
if (_M_equals(_M_get_key(*__cur), __key)) {
do {
__cur = _M_elems.erase_after(__prev);
++__erased;
} while ((__cur != __last) && _M_equals(_M_get_key(*__cur), __key));
break;
}
}
}
_M_num_elements -= __erased;
return __erased;
}
template <class _Val, class _Key, class _HF,
class _Traits, class _ExK, class _EqK, class _All>
void hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>
::erase(const_iterator __it) {
const size_type __n = _M_bkt_num(*__it);
_ElemsIte __cur(_M_buckets[__n]);
if (__cur == __it._M_ite) {
size_type __prev_b = __n;
_ElemsIte __prev = _M_before_begin(__prev_b)._M_ite;
fill(_M_buckets.begin() + __prev_b, _M_buckets.begin() + __n + 1,
_M_elems.erase_after(__prev)._M_node);
--_M_num_elements;
}
else {
_ElemsIte __prev = __cur++;
_ElemsIte __last(_M_buckets[__n + 1]);
for (; __cur != __last; ++__prev, ++__cur) {
if (__cur == __it._M_ite) {
_M_elems.erase_after(__prev);
--_M_num_elements;
break;
}
}
}
}
template <class _Val, class _Key, class _HF,
class _Traits, class _ExK, class _EqK, class _All>
void hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>
::erase(const_iterator __first, const_iterator __last) {
if (__first == __last)
return;
size_type __f_bucket = _M_bkt_num(*__first);
size_type __l_bucket = __last != end() ? _M_bkt_num(*__last) : (_M_buckets.size() - 1);
_ElemsIte __cur(_M_buckets[__f_bucket]);
_ElemsIte __prev;
if (__cur == __first._M_ite) {
__prev = _M_before_begin(__f_bucket)._M_ite;
}
else {
_ElemsIte __last(_M_buckets[++__f_bucket]);
__prev = __cur++;
for (; (__cur != __last) && (__cur != __first._M_ite); ++__prev, ++__cur);
}
//We do not use the slist::erase_after method taking a range to count the
//number of erased elements:
while (__cur != __last._M_ite) {
__cur = _M_elems.erase_after(__prev);
--_M_num_elements;
}
fill(_M_buckets.begin() + __f_bucket, _M_buckets.begin() + __l_bucket + 1, __cur._M_node);
}
template <class _Val, class _Key, class _HF,
class _Traits, class _ExK, class _EqK, class _All>
void hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>
::rehash(size_type __num_buckets_hint) {
if ((bucket_count() >= __num_buckets_hint) &&
(max_load_factor() > load_factor()))
return;
//Here if max_load_factor is lower than 1.0 the resulting value might not be representable
//as a size_type. The result concerning the respect of the max_load_factor will then be
//undefined.
__num_buckets_hint = (max) (__num_buckets_hint, (size_type)((float)size() / max_load_factor()));
size_type __num_buckets = _STLP_PRIV _Stl_prime_type::_S_next_size(__num_buckets_hint);
_M_rehash(__num_buckets);
}
template <class _Val, class _Key, class _HF,
class _Traits, class _ExK, class _EqK, class _All>
void hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>
::resize(size_type __num_elements_hint) {
if (((float)__num_elements_hint / (float)bucket_count() <= max_load_factor()) &&
(max_load_factor() >= load_factor())) {
return;
}
size_type __num_buckets_hint = (size_type)((float)(max) (__num_elements_hint, size()) / max_load_factor());
size_type __num_buckets = _STLP_PRIV _Stl_prime_type::_S_next_size(__num_buckets_hint);
#if defined (_STLP_DEBUG)
_M_check();
#endif
_M_rehash(__num_buckets);
}
template <class _Val, class _Key, class _HF,
class _Traits, class _ExK, class _EqK, class _All>
void hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>
::_M_rehash(size_type __num_buckets) {
_ElemsCont __tmp_elems(_M_elems.get_allocator());
_BucketVector __tmp(__num_buckets + 1, __STATIC_CAST(_BucketType*, 0), _M_buckets.get_allocator());
_ElemsIte __cur, __last(_M_elems.end());
while (!_M_elems.empty()) {
__cur = _M_elems.begin();
size_type __new_bucket = _M_bkt_num(*__cur, __num_buckets);
_ElemsIte __ite(__cur), __before_ite(__cur);
for (++__ite;
__ite != __last && _M_equals(_M_get_key(*__cur), _M_get_key(*__ite));
++__ite, ++__before_ite);
size_type __prev_bucket = __new_bucket;
_ElemsIte __prev = _S_before_begin(__tmp_elems, __tmp, __prev_bucket)._M_ite;
__tmp_elems.splice_after(__prev, _M_elems, _M_elems.before_begin(), __before_ite);
fill(__tmp.begin() + __prev_bucket, __tmp.begin() + __new_bucket + 1, __cur._M_node);
}
_M_elems.swap(__tmp_elems);
_M_buckets.swap(__tmp);
}
#if defined (_STLP_DEBUG)
template <class _Val, class _Key, class _HF,
class _Traits, class _ExK, class _EqK, class _All>
void hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>::_M_check() const {
//We check that hash code of stored keys haven't change and also that equivalent
//relation hasn't been modified
size_t __num_buckets = bucket_count();
for (size_t __b = 0; __b < __num_buckets; ++__b) {
_ElemsIte __cur(_M_buckets[__b]), __last(_M_buckets[__b + 1]);
_ElemsIte __fst(__cur), __snd(__cur);
for (; __cur != __last; ++__cur) {
_STLP_ASSERT( _M_bkt_num(*__cur, __num_buckets) == __b )
_STLP_ASSERT( !_M_equals(_M_get_key(*__fst), _M_get_key(*__cur)) || _M_equals(_M_get_key(*__snd), _M_get_key(*__cur)) )
if (__fst != __snd)
++__fst;
if (__snd != __cur)
++__snd;
}
}
}
#endif
template <class _Val, class _Key, class _HF,
class _Traits, class _ExK, class _EqK, class _All>
void hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>::clear() {
_M_elems.clear();
_M_buckets.assign(_M_buckets.size(), __STATIC_CAST(_BucketType*, 0));
_M_num_elements = 0;
}
template <class _Val, class _Key, class _HF,
class _Traits, class _ExK, class _EqK, class _All>
void hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>
::_M_copy_from(const hashtable<_Val,_Key,_HF,_Traits,_ExK,_EqK,_All>& __ht) {
_M_elems.clear();
_M_elems.insert(_M_elems.end(), __ht._M_elems.begin(), __ht._M_elems.end());
_M_buckets.resize(__ht._M_buckets.size());
_ElemsConstIte __src(__ht._M_elems.begin()), __src_end(__ht._M_elems.end());
_ElemsIte __dst(_M_elems.begin());
typename _BucketVector::const_iterator __src_b(__ht._M_buckets.begin()),
__src_end_b(__ht._M_buckets.end());
typename _BucketVector::iterator __dst_b(_M_buckets.begin()), __dst_end_b(_M_buckets.end());
for (; __src != __src_end; ++__src, ++__dst) {
for (; __src_b != __src_end_b; ++__src_b, ++__dst_b) {
if (*__src_b == __src._M_node) {
*__dst_b = __dst._M_node;
}
else
break;
}
}
fill(__dst_b, __dst_end_b, __STATIC_CAST(_BucketType*, 0));
_M_num_elements = __ht._M_num_elements;
_M_max_load_factor = __ht._M_max_load_factor;
}
#undef __iterator__
#undef const_iterator
#undef __size_type__
#undef __reference__
#undef size_type
#undef value_type
#undef key_type
#undef __stl_num_primes
#if defined (_STLP_DEBUG)
# undef hashtable
_STLP_MOVE_TO_STD_NAMESPACE
#endif
_STLP_END_NAMESPACE
#endif /* _STLP_HASHTABLE_C */
// Local Variables:
// mode:C++
// End:
|