57 typedef typename KeyTraitsType::KeyTypeConstRef KeyTypeConstRef;
58 typedef typename KeyTraitsType::KeyTypeValue KeyTypeValue;
59 typedef typename KeyTraitsType::Printable Printable;
60 typedef typename KeyTraitsType::HashValueType HashValueType;
71 :
m_key(KeyTypeValue())
77 Data* next() {
return m_next; }
78 void setNext(
Data* anext) { this->m_next = anext; }
79 KeyTypeConstRef key() {
return m_key; }
80 const ValueType& value()
const {
return m_value; }
81 ValueType& value() {
return m_value; }
98 Data* m_next =
nullptr;
149 Integer nb_bucket = from.m_nb_bucket;
153 m_buckets.resize(nb_bucket);
159 for (Integer i = 0; i < nb_bucket; ++i)
160 for (
Data* data = from_buckets[i]; data; data = data->next())
161 _add(i, data->key(), data->value());
171 Integer hf = _keyToBucket(
id);
172 for (
Data* i = m_buckets[hf]; i; i = i->m_next) {
213 Data* ht = _lookup(
id);
215 this->_throwNotFound(
id, Printable());
237 const Data* ht = _lookup(
id);
239 this->_throwNotFound(
id, Printable());
262 bool add(KeyTypeConstRef
id,
const ValueType& value)
264 Integer hf = _keyToBucket(
id);
265 Data* ht = _lookupBucket(hf,
id);
280 Integer hf = _keyToBucket(
id);
281 Data* ht = _removeBucket(hf,
id);
282 ht->setNext(m_first_free);
298 HashValueType hf = _applyHash(
id);
299 Data* ht = _lookupBucket(_hashValueToBucket(hf),
id);
308 ht = _add(_hashValueToBucket(hf),
id, value);
324 HashValueType hf = _applyHash(
id);
325 Data* ht = _lookupBucket(_hashValueToBucket(hf),
id);
331 ht = _add(_hashValueToBucket(hf),
id, ValueType());
345 Integer hf = _keyToBucket(
id);
354 ConstArrayView<Data*> buckets()
const
360 void resize(Integer new_size,
bool use_prime =
false)
383 template <
class Lambda>
void
386 for (Integer k = 0, n = m_buckets.size(); k < n; ++k) {
387 Data* nbid = m_buckets[k];
388 for (; nbid; nbid = nbid->next()) {
398 template <
class Lambda>
void
401 for (Integer k = 0, n = m_buckets.size(); k < n; ++k) {
402 Data* nbid = m_buckets[k];
403 for (; nbid; nbid = nbid->next()) {
404 lambda(nbid->value());
412 void _rehash(Integer new_size)
418 m_buckets.resize(new_size);
423 for (Integer z = 0, zs = old_buckets.size(); z < zs; ++z) {
424 for (Data* i = old_buckets[z]; i; i = i->next()) {
427 _add(_keyToBucket(current->key()), current->key(), current->value());
440 MultiBufferT<Data>* m_buffer;
441 Data* m_first_free =
nullptr;
445 mutable Int64 m_nb_collision = 0;
446 mutable Int64 m_nb_direct = 0;
450 Data* _add(Integer bucket, KeyTypeConstRef key,
const ValueType& value)
455 m_first_free = m_first_free->next();
458 hd = m_buffer->allocOne();
460 _baseAdd(bucket, key, hd);
464 HashValueType _applyHash(KeyTypeConstRef
id)
const
467 return KeyTraitsType::hashFunction(
id);
470 Integer _keyToBucket(KeyTypeConstRef
id)
const
475 Integer _hashValueToBucket(KeyTypeValue
id)
const
480 Data* _baseLookupBucket(Integer bucket, KeyTypeConstRef
id)
const
482 for (Data* i = m_buckets[bucket]; i; i = i->next()) {
483 if (!(i->key() ==
id)) {
493 Data* _baseRemoveBucket(Integer bucket, KeyTypeConstRef
id)
495 Data* i = m_buckets[bucket];
497 if (i->m_key ==
id) {
498 m_buckets[bucket] = i->next();
502 for (; i->next(); i = i->next()) {
503 if (i->next()->key() ==
id) {
505 i->setNext(i->next()->next());
511 this->_throwNotFound(
id, Printable());
515 inline Data* _baseLookup(KeyTypeConstRef
id)
const
517 return _baseLookupBucket(_keyToBucket(
id),
id);
520 inline Data* _baseRemove(KeyTypeConstRef
id)
522 return _baseRemoveBucket(_keyToBucket(
id),
id);
525 void _baseAdd(Integer bucket, KeyTypeConstRef
id, Data* hd)
527 Data* buck = m_buckets[bucket];
530 m_buckets[bucket] = hd;
534 Data* _lookup(KeyTypeConstRef
id)
536 return _baseLookup(
id);
539 const Data* _lookup(KeyTypeConstRef
id)
const
541 return _baseLookup(
id);
544 Data* _lookupBucket(Integer bucket, KeyTypeConstRef
id)
const
546 return _baseLookupBucket(bucket,
id);
549 Data* _removeBucket(Integer bucket, KeyTypeConstRef
id)
551 return _baseRemoveBucket(bucket,
id);
580 void _print(FalseType)
584 void _print(TrueType)
586 for (Integer z = 0, zs = m_buckets.size(); z < zs; ++z) {
587 for (Data* i = m_buckets[z]; i; i = i->next()) {
588 cout <<
"* KEY=" << i->key() <<
" bucket=" << z <<
'\n';
593 void _throwNotFound ARCANE_NORETURN(KeyTypeConstRef, FalseType)
const
595 HashTableBase::_throwNotFound();
598 void _throwNotFound ARCANE_NORETURN(KeyTypeConstRef
id, TrueType)
const
600 std::cout <<
"ERROR: can not find key=" <<
id <<
" bucket=" << _keyToBucket(
id) <<
"\n";
602 HashTableBase::_throwNotFound();
605 void _computeMaxCount()
614 UniqueArray<Data*> m_buckets;