12#ifndef ARCANE_UTILS_HASHTABLEMAP_H
13#define ARCANE_UTILS_HASHTABLEMAP_H
17#include "arcane/utils/HashTable.h"
28template <
typename KeyType,
typename ValueType>
49template <
typename KeyType,
typename ValueType,
typename KeyTraitsType = HashTraitsT<KeyType>>
55 typedef typename KeyTraitsType::KeyTypeConstRef KeyTypeConstRef;
56 typedef typename KeyTraitsType::KeyTypeValue KeyTypeValue;
57 typedef typename KeyTraitsType::Printable Printable;
58 typedef typename KeyTraitsType::HashValueType HashValueType;
69 :
m_key(KeyTypeValue())
75 Data* next() {
return m_next; }
76 void setNext(Data* anext) { this->m_next = anext; }
77 KeyTypeConstRef key() {
return m_key; }
96 Data* m_next =
nullptr;
151 m_buckets.resize(nb_bucket);
157 for (
Integer i = 0; i < nb_bucket; ++i)
158 for (Data* data = from_buckets[i]; data; data = data->next())
159 _add(i, data->key(), data->value());
170 for (Data* i = m_buckets[hf]; i; i = i->m_next) {
199 const Data*
lookup(KeyTypeConstRef
id)
const
211 Data* ht = _lookup(
id);
213 this->_throwNotFound(
id, Printable());
235 const Data* ht = _lookup(
id);
237 this->_throwNotFound(
id, Printable());
263 Data* ht = _lookupBucket(hf,
id);
279 Data* ht = _removeBucket(hf,
id);
296 HashValueType hf = _applyHash(
id);
297 Data* ht = _lookupBucket(_hashValueToBucket(hf),
id);
306 ht = _add(_hashValueToBucket(hf),
id, value);
322 HashValueType hf = _applyHash(
id);
323 Data* ht = _lookupBucket(_hashValueToBucket(hf),
id);
329 ht = _add(_hashValueToBucket(hf),
id,
ValueType());
352 ConstArrayView<Data*> buckets()
const
381 template <
class Lambda>
void
384 for (
Integer k = 0, n = m_buckets.size(); k < n; ++k) {
385 Data* nbid = m_buckets[k];
386 for (; nbid; nbid = nbid->next()) {
396 template <
class Lambda>
void
399 for (
Integer k = 0, n = m_buckets.size(); k < n; ++k) {
400 Data* nbid = m_buckets[k];
401 for (; nbid; nbid = nbid->next()) {
402 lambda(nbid->value());
416 m_buckets.resize(new_size);
421 for (
Integer z = 0, zs = old_buckets.
size(); z < zs; ++z) {
422 for (Data* i = old_buckets[z]; i; i = i->next()) {
425 _add(_keyToBucket(current->key()), current->key(), current->value());
443 mutable Int64 m_nb_collision = 0;
444 mutable Int64 m_nb_direct = 0;
458 _baseAdd(bucket, key, hd);
462 HashValueType _applyHash(KeyTypeConstRef
id)
const
465 return KeyTraitsType::hashFunction(
id);
468 Integer _keyToBucket(KeyTypeConstRef
id)
const
473 Integer _hashValueToBucket(KeyTypeValue
id)
const
478 Data* _baseLookupBucket(
Integer bucket, KeyTypeConstRef
id)
const
480 for (
Data* i = m_buckets[bucket]; i; i = i->next()) {
481 if (!(i->key() ==
id)) {
491 Data* _baseRemoveBucket(
Integer bucket, KeyTypeConstRef
id)
493 Data* i = m_buckets[bucket];
495 if (i->m_key ==
id) {
496 m_buckets[bucket] = i->next();
500 for (; i->next(); i = i->next()) {
501 if (i->next()->key() ==
id) {
503 i->setNext(i->next()->next());
509 this->_throwNotFound(
id, Printable());
513 inline Data* _baseLookup(KeyTypeConstRef
id)
const
515 return _baseLookupBucket(_keyToBucket(
id),
id);
518 inline Data* _baseRemove(KeyTypeConstRef
id)
520 return _baseRemoveBucket(_keyToBucket(
id),
id);
523 void _baseAdd(
Integer bucket, KeyTypeConstRef
id,
Data* hd)
525 Data* buck = m_buckets[bucket];
528 m_buckets[bucket] = hd;
532 Data* _lookup(KeyTypeConstRef
id)
534 return _baseLookup(
id);
537 const Data* _lookup(KeyTypeConstRef
id)
const
539 return _baseLookup(
id);
542 Data* _lookupBucket(
Integer bucket, KeyTypeConstRef
id)
const
544 return _baseLookupBucket(bucket,
id);
547 Data* _removeBucket(
Integer bucket, KeyTypeConstRef
id)
549 return _baseRemoveBucket(bucket,
id);
578 void _print(FalseType)
582 void _print(TrueType)
584 for (
Integer z = 0, zs = m_buckets.size(); z < zs; ++z) {
585 for (
Data* i = m_buckets[z]; i; i = i->next()) {
586 cout <<
"* KEY=" << i->key() <<
" bucket=" << z <<
'\n';
591 void _throwNotFound ARCANE_NORETURN(KeyTypeConstRef, FalseType)
const
593 HashTableBase::_throwNotFound();
596 void _throwNotFound ARCANE_NORETURN(KeyTypeConstRef
id, TrueType)
const
598 std::cout <<
"ERROR: can not find key=" <<
id <<
" bucket=" << _keyToBucket(
id) <<
"\n";
600 HashTableBase::_throwNotFound();
603 void _computeMaxCount()
622template <
typename KeyType,
typename ValueType>
623class HashTableMapEnumeratorT
630 HashTableMapEnumeratorT(
const HashType& rhs)
631 : m_buckets(rhs.buckets())
633 , m_current_bucket(-1)
641 m_current_data = m_current_data->next();
642 if (!m_current_data) {
643 while (m_current_data == 0 && (m_current_bucket + 1) < m_buckets.size()) {
645 m_current_data = m_buckets[m_current_bucket];
648 return m_current_data != 0;
650 ValueType& operator*() {
return m_current_data->value(); }
651 const ValueType& operator*()
const {
return m_current_data->value(); }
652 Data* data() {
return m_current_data; }
653 const Data* data()
const {
return m_current_data; }
658 Data* m_current_data;
Integer size() const
Number of elements in the vector.
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.
Enumerator for a HashTableMap.
Hash table for associative arrays.
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 _rehash(Integer new_size)
Rehashes the data after changing key values.
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.
MultiBufferT< Data > * m_buffer
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
Buffer for multiple allocation.
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.