12#ifndef ARCANE_UTILS_HASHTABLEMAP_H
13#define ARCANE_UTILS_HASHTABLEMAP_H
17#include "arcane/utils/HashTable.h"
30template <
typename KeyType,
typename ValueType>
52template <
typename KeyType,
typename ValueType,
typename KeyTraitsType = HashTraitsT<KeyType>>
58 typedef typename KeyTraitsType::KeyTypeConstRef KeyTypeConstRef;
59 typedef typename KeyTraitsType::KeyTypeValue KeyTypeValue;
60 typedef typename KeyTraitsType::Printable Printable;
61 typedef typename KeyTraitsType::HashValueType HashValueType;
72 :
m_key(KeyTypeValue())
78 Data* next() {
return m_next; }
79 void setNext(Data* anext) { this->m_next = anext; }
80 KeyTypeConstRef key() {
return m_key; }
99 Data* m_next =
nullptr;
154 m_buckets.resize(nb_bucket);
160 for (
Integer i = 0; i < nb_bucket; ++i)
161 for (Data* data = from_buckets[i]; data; data = data->next())
162 _add(i, data->key(), data->value());
173 for (Data* i = m_buckets[hf]; i; i = i->m_next) {
202 const Data*
lookup(KeyTypeConstRef
id)
const
214 Data* ht = _lookup(
id);
216 this->_throwNotFound(
id, Printable());
238 const Data* ht = _lookup(
id);
240 this->_throwNotFound(
id, Printable());
266 Data* ht = _lookupBucket(hf,
id);
282 Data* ht = _removeBucket(hf,
id);
283 ht->setNext(m_first_free);
299 HashValueType hf = _applyHash(
id);
300 Data* ht = _lookupBucket(_hashValueToBucket(hf),
id);
309 ht = _add(_hashValueToBucket(hf),
id, value);
325 HashValueType hf = _applyHash(
id);
326 Data* ht = _lookupBucket(_hashValueToBucket(hf),
id);
332 ht = _add(_hashValueToBucket(hf),
id,
ValueType());
355 ConstArrayView<Data*> buckets()
const
384 template <
class Lambda>
void
387 for (
Integer k = 0, n = m_buckets.size(); k < n; ++k) {
388 Data* nbid = m_buckets[k];
389 for (; nbid; nbid = nbid->next()) {
399 template <
class Lambda>
void
402 for (
Integer k = 0, n = m_buckets.size(); k < n; ++k) {
403 Data* nbid = m_buckets[k];
404 for (; nbid; nbid = nbid->next()) {
405 lambda(nbid->value());
419 m_buckets.resize(new_size);
424 for (
Integer z = 0, zs = old_buckets.size(); z < zs; ++z) {
425 for (Data* i = old_buckets[z]; i; i = i->next()) {
428 _add(_keyToBucket(current->key()), current->key(), current->value());
441 MultiBufferT<Data>* m_buffer =
nullptr;
442 Data* m_first_free =
nullptr;
446 mutable Int64 m_nb_collision = 0;
447 mutable Int64 m_nb_direct = 0;
451 Data* _add(
Integer bucket, KeyTypeConstRef key,
const ValueType& value)
456 m_first_free = m_first_free->next();
459 hd = m_buffer->allocOne();
461 _baseAdd(bucket, key, hd);
465 HashValueType _applyHash(KeyTypeConstRef
id)
const
468 return KeyTraitsType::hashFunction(
id);
471 Integer _keyToBucket(KeyTypeConstRef
id)
const
476 Integer _hashValueToBucket(KeyTypeValue
id)
const
481 Data* _baseLookupBucket(
Integer bucket, KeyTypeConstRef
id)
const
483 for (
Data* i = m_buckets[bucket]; i; i = i->next()) {
484 if (!(i->key() ==
id)) {
494 Data* _baseRemoveBucket(
Integer bucket, KeyTypeConstRef
id)
496 Data* i = m_buckets[bucket];
498 if (i->m_key ==
id) {
499 m_buckets[bucket] = i->next();
503 for (; i->next(); i = i->next()) {
504 if (i->next()->key() ==
id) {
506 i->setNext(i->next()->next());
512 this->_throwNotFound(
id, Printable());
516 inline Data* _baseLookup(KeyTypeConstRef
id)
const
518 return _baseLookupBucket(_keyToBucket(
id),
id);
521 inline Data* _baseRemove(KeyTypeConstRef
id)
523 return _baseRemoveBucket(_keyToBucket(
id),
id);
526 void _baseAdd(
Integer bucket, KeyTypeConstRef
id,
Data* hd)
528 Data* buck = m_buckets[bucket];
531 m_buckets[bucket] = hd;
535 Data* _lookup(KeyTypeConstRef
id)
537 return _baseLookup(
id);
540 const Data* _lookup(KeyTypeConstRef
id)
const
542 return _baseLookup(
id);
545 Data* _lookupBucket(
Integer bucket, KeyTypeConstRef
id)
const
547 return _baseLookupBucket(bucket,
id);
550 Data* _removeBucket(
Integer bucket, KeyTypeConstRef
id)
552 return _baseRemoveBucket(bucket,
id);
581 void _print(FalseType)
585 void _print(TrueType)
587 for (
Integer z = 0, zs = m_buckets.size(); z < zs; ++z) {
588 for (
Data* i = m_buckets[z]; i; i = i->next()) {
589 std::cout <<
"* KEY=" << i->key() <<
" bucket=" << z <<
'\n';
594 void _throwNotFound ARCANE_NORETURN(KeyTypeConstRef, FalseType)
const
596 HashTableBase::_throwNotFound();
599 void _throwNotFound ARCANE_NORETURN(KeyTypeConstRef
id, TrueType)
const
601 std::cout <<
"ERROR: can not find key=" <<
id <<
" bucket=" << _keyToBucket(
id) <<
"\n";
603 HashTableBase::_throwNotFound();
606 void _computeMaxCount()
615 UniqueArray<Data*> m_buckets;
625template <
typename KeyType,
typename ValueType>
626class HashTableMapEnumeratorT
633 HashTableMapEnumeratorT(
const HashType& rhs)
634 : m_buckets(rhs.buckets())
636 , m_current_bucket(-1)
644 m_current_data = m_current_data->next();
645 if (!m_current_data) {
646 while (m_current_data == 0 && (m_current_bucket + 1) < m_buckets.size()) {
648 m_current_data = m_buckets[m_current_bucket];
651 return m_current_data != 0;
653 ValueType& operator*() {
return m_current_data->value(); }
654 const ValueType& operator*()
const {
return m_current_data->value(); }
655 Data* data() {
return m_current_data; }
656 const Data* data()
const {
return m_current_data; }
661 Data* m_current_data;
Modifiable view of an array of type T.
Constant view of an array of type T.
HashTableBase(Integer table_size, bool use_prime)
Creates a table of size table_size.
Integer m_nb_bucket
Number of buckets.
Integer nearestPrimeNumber(Integer n)
Returns the nearest prime number greater than n. The nearest prime number greater than n is returned ...
Integer m_count
Number of elements.
const ValueType & operator[](KeyTypeConstRef id) const
Searches for the value corresponding to key id.
void rehash()
Rehashes the data after changing key values.
HashTableMapT(Integer table_size, bool use_prime, Integer buffer_size)
Creates a table of size table_size.
void nocheckAdd(KeyTypeConstRef id, const ValueType &value)
Adds the value value corresponding to the key id.
const Data * lookup(KeyTypeConstRef id) const
Searches for the value corresponding to key id.
Data * lookup(KeyTypeConstRef id)
Searches for the value corresponding to key id.
Data * lookupAdd(KeyTypeConstRef id, const ValueType &value, bool &is_add)
Searches for or adds the value corresponding to key id.
void remove(KeyTypeConstRef id)
Removes the value associated with key id.
void resize(Integer new_size, bool use_prime=false)
Resizes the hash table.
ValueType & lookupValue(KeyTypeConstRef id)
Searches for the value corresponding to key id.
ThatClass & operator=(const ThatClass &from)
Copy assignment operator.
ValueType & operator[](KeyTypeConstRef id)
Searches for the value corresponding to key id.
bool add(KeyTypeConstRef id, const ValueType &value)
Adds the value value corresponding to key id.
void eachValue(const Lambda &lambda)
Applies the functor f to all elements of the collection and uses x->value() (of type ValueType) as an...
void clear()
Deletes all elements from the table.
void each(const Lambda &lambda)
Applies the functor f to all elements of the collection.
const ValueType & lookupValue(KeyTypeConstRef id) const
Searches for the value corresponding to key id.
HashTableMapT(Integer table_size, bool use_prime)
Creates a table of size table_size.
Data * lookupAdd(KeyTypeConstRef id)
Searches for or adds the value corresponding to key id.
bool hasKey(KeyTypeConstRef id)
true if a value with key id is present
1D data vector with value semantics (STL style).
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
void setValue(const ValueType &avalue)
Modifies the value of the instance.
ValueType m_value
Element value.
KeyTypeValue m_key
Search key.
void setKey(const KeyType &new_key)
Changes the value of the key.