12#ifndef ARCANE_UTILS_HASHTABLEMAP_H
13#define ARCANE_UTILS_HASHTABLEMAP_H
17#include "arcane/utils/HashTable.h"
30template <
typename KeyType,
typename ValueType>
53template <
typename KeyType,
typename ValueType,
typename KeyTraitsType = HashTraitsT<KeyType>>
59 typedef typename KeyTraitsType::KeyTypeConstRef KeyTypeConstRef;
60 typedef typename KeyTraitsType::KeyTypeValue KeyTypeValue;
61 typedef typename KeyTraitsType::Printable Printable;
62 typedef typename KeyTraitsType::HashValueType HashValueType;
73 :
m_key(KeyTypeValue())
79 Data* next() {
return m_next; }
80 void setNext(Data* anext) { this->m_next = anext; }
81 KeyTypeConstRef key() {
return m_key; }
100 Data* m_next =
nullptr;
155 m_buckets.resize(nb_bucket);
161 for (
Integer i = 0; i < nb_bucket; ++i)
162 for (Data* data = from_buckets[i]; data; data = data->next())
163 _add(i, data->key(), data->value());
174 for (Data* i = m_buckets[hf]; i; i = i->m_next) {
203 const Data*
lookup(KeyTypeConstRef
id)
const
215 Data* ht = _lookup(
id);
217 this->_throwNotFound(
id, Printable());
239 const Data* ht = _lookup(
id);
241 this->_throwNotFound(
id, Printable());
267 Data* ht = _lookupBucket(hf,
id);
283 Data* ht = _removeBucket(hf,
id);
300 HashValueType hf = _applyHash(
id);
301 Data* ht = _lookupBucket(_hashValueToBucket(hf),
id);
310 ht = _add(_hashValueToBucket(hf),
id, value);
326 HashValueType hf = _applyHash(
id);
327 Data* ht = _lookupBucket(_hashValueToBucket(hf),
id);
333 ht = _add(_hashValueToBucket(hf),
id,
ValueType());
356 ConstArrayView<Data*> buckets()
const
385 template <
class Lambda>
void
388 for (
Integer k = 0, n = m_buckets.size(); k < n; ++k) {
389 Data* nbid = m_buckets[k];
390 for (; nbid; nbid = nbid->next()) {
400 template <
class Lambda>
void
403 for (
Integer k = 0, n = m_buckets.size(); k < n; ++k) {
404 Data* nbid = m_buckets[k];
405 for (; nbid; nbid = nbid->next()) {
406 lambda(nbid->value());
420 m_buckets.resize(new_size);
425 for (
Integer z = 0, zs = old_buckets.
size(); z < zs; ++z) {
426 for (Data* i = old_buckets[z]; i; i = i->next()) {
429 _add(_keyToBucket(current->key()), current->key(), current->value());
447 mutable Int64 m_nb_collision = 0;
448 mutable Int64 m_nb_direct = 0;
462 _baseAdd(bucket, key, hd);
466 HashValueType _applyHash(KeyTypeConstRef
id)
const
469 return KeyTraitsType::hashFunction(
id);
472 Integer _keyToBucket(KeyTypeConstRef
id)
const
477 Integer _hashValueToBucket(KeyTypeValue
id)
const
482 Data* _baseLookupBucket(
Integer bucket, KeyTypeConstRef
id)
const
484 for (
Data* i = m_buckets[bucket]; i; i = i->next()) {
485 if (!(i->key() ==
id)) {
495 Data* _baseRemoveBucket(
Integer bucket, KeyTypeConstRef
id)
497 Data* i = m_buckets[bucket];
499 if (i->m_key ==
id) {
500 m_buckets[bucket] = i->next();
504 for (; i->next(); i = i->next()) {
505 if (i->next()->key() ==
id) {
507 i->setNext(i->next()->next());
513 this->_throwNotFound(
id, Printable());
517 inline Data* _baseLookup(KeyTypeConstRef
id)
const
519 return _baseLookupBucket(_keyToBucket(
id),
id);
522 inline Data* _baseRemove(KeyTypeConstRef
id)
524 return _baseRemoveBucket(_keyToBucket(
id),
id);
527 void _baseAdd(
Integer bucket, KeyTypeConstRef
id,
Data* hd)
529 Data* buck = m_buckets[bucket];
532 m_buckets[bucket] = hd;
536 Data* _lookup(KeyTypeConstRef
id)
538 return _baseLookup(
id);
541 const Data* _lookup(KeyTypeConstRef
id)
const
543 return _baseLookup(
id);
546 Data* _lookupBucket(
Integer bucket, KeyTypeConstRef
id)
const
548 return _baseLookupBucket(bucket,
id);
551 Data* _removeBucket(
Integer bucket, KeyTypeConstRef
id)
553 return _baseRemoveBucket(bucket,
id);
582 void _print(FalseType)
586 void _print(TrueType)
588 for (
Integer z = 0, zs = m_buckets.size(); z < zs; ++z) {
589 for (
Data* i = m_buckets[z]; i; i = i->next()) {
590 std::cout <<
"* KEY=" << i->key() <<
" bucket=" << z <<
'\n';
595 void _throwNotFound ARCANE_NORETURN(KeyTypeConstRef, FalseType)
const
597 HashTableBase::_throwNotFound();
600 void _throwNotFound ARCANE_NORETURN(KeyTypeConstRef
id, TrueType)
const
602 std::cout <<
"ERROR: can not find key=" <<
id <<
" bucket=" << _keyToBucket(
id) <<
"\n";
604 HashTableBase::_throwNotFound();
607 void _computeMaxCount()
626template <
typename KeyType,
typename ValueType>
627class HashTableMapEnumeratorT
634 HashTableMapEnumeratorT(
const HashType& rhs)
635 : m_buckets(rhs.buckets())
637 , m_current_bucket(-1)
645 m_current_data = m_current_data->next();
646 if (!m_current_data) {
647 while (m_current_data == 0 && (m_current_bucket + 1) < m_buckets.size()) {
649 m_current_data = m_buckets[m_current_bucket];
652 return m_current_data != 0;
654 ValueType& operator*() {
return m_current_data->value(); }
655 const ValueType& operator*()
const {
return m_current_data->value(); }
656 Data* data() {
return m_current_data; }
657 const Data* data()
const {
return m_current_data; }
662 Data* m_current_data;
Integer size() const
Nombre d'éléments du vecteur.
Vue modifiable d'un tableau d'un type T.
Vue constante d'un tableau de type T.
HashTableBase(Integer table_size, bool use_prime)
Crée une table de taille table_size.
Integer m_nb_bucket
Nombre de buckets.
Integer nearestPrimeNumber(Integer n)
Retourne le nombre premier le plus proche de n par excès. Le nombre premier le plus proche et supérie...
Integer m_count
Nombre d'éléments.
Énumérateur pour un HashTableMap.
Table de hachage pour tableaux associatifs.
const ValueType & operator[](KeyTypeConstRef id) const
Recherche la valeur correspondant à la clé id.
void rehash()
Repositionne les données après changement de valeur des clés.
HashTableMapT(Integer table_size, bool use_prime, Integer buffer_size)
Crée une table de taille table_size.
void nocheckAdd(KeyTypeConstRef id, const ValueType &value)
Ajoute la valeur value correspondant à la clé id.
const Data * lookup(KeyTypeConstRef id) const
Recherche la valeur correspondant à la clé id.
Data * lookup(KeyTypeConstRef id)
Recherche la valeur correspondant à la clé id.
Data * lookupAdd(KeyTypeConstRef id, const ValueType &value, bool &is_add)
Recherche ou ajoute la valeur correspondant à la clé id.
void remove(KeyTypeConstRef id)
Supprime la valeur associée à la clé id.
void _rehash(Integer new_size)
Repositionne les données après changement de valeur des clés.
void resize(Integer new_size, bool use_prime=false)
Redimensionne la table de hachage.
ValueType & lookupValue(KeyTypeConstRef id)
Recherche la valeur correspondant à la clé id.
ThatClass & operator=(const ThatClass &from)
Opérateur de recopie.
ValueType & operator[](KeyTypeConstRef id)
Recherche la valeur correspondant à la clé id.
bool add(KeyTypeConstRef id, const ValueType &value)
Ajoute la valeur value correspondant à la clé id.
void eachValue(const Lambda &lambda)
Applique le fonctor f à tous les éléments de la collection et utilise x->value() (de type ValueType) ...
void clear()
Supprime tous les éléments de la table.
void each(const Lambda &lambda)
Applique le fonctor f à tous les éléments de la collection.
MultiBufferT< Data > * m_buffer
const ValueType & lookupValue(KeyTypeConstRef id) const
Recherche la valeur correspondant à la clé id.
HashTableMapT(Integer table_size, bool use_prime)
Crée une table de taille table_size.
Data * lookupAdd(KeyTypeConstRef id)
Recherche ou ajoute la valeur correspondant à la clé id.
bool hasKey(KeyTypeConstRef id)
true si une valeur avec la clé id est présente
Tampon pour allocation multiple.
Vecteur 1D de données avec sémantique par valeur (style STL).
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int64_t Int64
Type entier signé sur 64 bits.
Int32 Integer
Type représentant un entier.
void setValue(const ValueType &avalue)
Modifie la valeur de l'instance.
ValueType m_value
Valeur de l'élément.
KeyTypeValue m_key
Clé de recherche.
void setKey(const KeyType &new_key)
Change la valeur de la clé.