1 # ifndef TPL_DYNSETHASH_H
2 # define TPL_DYNSETHASH_H
9 # include <tpl_dynArray.H>
10 # include <tpl_dynSetOhash.H>
11 # include <tpl_lhash.H>
12 # include <tpl_linHash.H>
31 template <
typename Key,
33 template <
typename,
class>
class HashTable =
LhashTable>
38 typedef HashTable<Key, Cmp> Base;
40 typedef typename HashTable<Key, Cmp>::Bucket Bucket;
49 typedef Key Item_Type;
62 const size_t len = Primes::DefaultPrime,
63 const float lower_alpha = hash_default_lower_alpha,
64 const float upper_alpha = hash_default_upper_alpha)
65 :
Base(hash_fct, len, lower_alpha, upper_alpha, true, true)
74 for (
typename Base::Iterator it(other); it.has_curr(); it.next())
76 Bucket * bucket = (Bucket*) it.get_curr();
78 I(*
search(bucket->get_key()) == bucket->get_key());
85 : Base(other.hash_fct, other.len,
86 other.lower_alpha, other.upper_alpha, true, true)
92 : Base(other.hash_fct, other.len,
93 other.lower_alpha, other.upper_alpha, true, true)
122 Key * insert_bucket(Bucket * bucket)
124 Bucket * ret_val = (Bucket*) this->Base::insert(bucket);
131 return &ret_val->get_key();
140 return insert_bucket(
new Bucket (key));
145 return insert_bucket(
new Bucket (std::move(key)));
148 Key * add(
const Key & key)
150 return insert_bucket(
new Bucket (key));
153 Key * add(Key && key)
155 return insert_bucket(
new Bucket (std::move(key)));
165 return bucket != NULL ? &bucket->get_key() : NULL;
168 bool has(
const Key & key)
const
173 bool contains(
const Key & key)
const {
return has(key); }
175 Key & find(
const Key & key)
180 throw std::domain_error(
"Key not found in hash");
182 return bucket->get_key();
190 static Bucket * key_to_bucket(Key * key)
192 Bucket * ret_val = 0;
193 size_t offset =
reinterpret_cast<size_t>(&ret_val->get_key());
195 return reinterpret_cast<Bucket*
>(
reinterpret_cast<size_t>(key) - offset);
202 void remove(Key * key)
204 Bucket * bucket = key_to_bucket(key);
205 this->Base::remove(bucket);
209 void remove(
const Key & key)
213 throw std::domain_error(
"Key not in hash table");
215 this->Base::remove(bucket);
247 Generic_Traverse(Key);
248 Functional_Methods(Key);
257 const Key & get_curr()
259 return this->Base::Iterator::get_curr()->get_key();
262 void del() {
delete this->Base::Iterator::del(); }
267 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
275 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
284 template <
typename Key,
typename Data,
288 :
public DynHashTable<std::pair<Key, Data>, Cmp, HashTable>
290 typedef std::pair<Key, Data> Pair;
294 typedef typename Base::Bucket Bucket;
301 Data & get_data(
const Key & key)
303 return key_to_pair<Key, Data>(&
const_cast<Key&
>(key))->second;
306 const Key & get_key(Data * data_ptr)
308 return data_to_pair<Key, Data>(data_ptr)->first;
311 typedef Data Value_Type;
313 typedef size_t (*
Hash_Fct)(
const Key&);
317 template <
size_t (*fct)(const Key & k)>
static
318 size_t wrapper(
const std::pair<Key, Data> & p)
320 return (*fct)(p.first);
335 const size_t len = Primes::DefaultPrime,
336 const float lower_alpha = hash_default_lower_alpha,
337 const float upper_alpha = hash_default_upper_alpha)
338 :
Base(hash_fct, len, lower_alpha, upper_alpha)
346 Key *
insert(
const Key & key,
const Data & data)
348 Pair * p = this->insert_bucket(
new typename Base::Bucket (Pair(key, data)));
350 return p != NULL ? &p->first : NULL;
353 Key *
insert(
const Key & key, Data && data)
355 Pair * p = this->insert_bucket
356 (
new typename Base::Bucket (Pair(key, std::move(data))));
358 return p != NULL ? &p->first : NULL;
361 Key *
insert(Key && key, Data && data)
363 Pair * p = this->insert_bucket
364 (
new typename Base::Bucket (Pair(std::move(key), std::move(data))));
366 return p != NULL ? &p->first : NULL;
369 Key *
insert(Key && key,
const Data & data)
371 Pair * p= this->insert_bucket
372 (
new typename Base::Bucket (Pair(std::move(key), data)));
374 return p != NULL ? &p->first : NULL;
383 return p != NULL ? &p->second : NULL;
386 bool has(
const Key & key)
391 Data & find(
const Key & key)
393 return Base::find(Pair(key, Data())).second;
403 void remove(
const Key & key)
408 Map_Sequences_Methods();
413 template <
typename Key,
423 template <
typename Key,
437 # endif // TPL_DYNSETHASH_H
DynHashTable(Hash_Fct hash_fct=Aleph::dft_hash_fct< Key >, const size_t len=Primes::DefaultPrime, const float lower_alpha=hash_default_lower_alpha, const float upper_alpha=hash_default_upper_alpha)
Definition: tpl_dynSetHash.H:61
Key * insert(const Key &key)
Definition: tpl_dynSetHash.H:138
Data * search(const Key &key) const
Definition: tpl_dynSetHash.H:380
Definition: tpl_dynSetHash.H:426
Definition: tpl_dynSetHash.H:287
Definition: tpl_dynSetHash.H:416
Itor1 search(Itor1 beg, const Itor1 &end, Itor2 searchBeg, const Itor2 &searchEnd, BinaryPredicate op=BinaryPredicate())
Definition: ahAlgo.H:327
DynMapHashTable(typename Base::Hash_Fct hash_fct=wrapper< Aleph::dft_hash_fct< Key >>, const size_t len=Primes::DefaultPrime, const float lower_alpha=hash_default_lower_alpha, const float upper_alpha=hash_default_upper_alpha)
Definition: tpl_dynSetHash.H:334
Definition: tpl_dynSetHash.H:276
Key * search(const Key &key) const
Definition: tpl_dynSetHash.H:161
void remove_by_data(Data &data)
Definition: tpl_dynSetHash.H:398
Definition: tpl_linHash.H:55
Definition: tpl_lhash.H:531
void remove(std::pair< Key, Data > *key)
Definition: tpl_dynSetHash.H:202
Definition: tpl_dynSetHash.H:251
Key * insert(const Key &key, const Data &data)
Definition: tpl_dynSetHash.H:346
Generic_Keys(Key)
returns a container with all the keys of the table
Base::Hash_Fct Hash_Fct
El tipo de función hash.
Definition: tpl_dynSetHash.H:45
Definition: tpl_dynSetHash.H:34
Definition: tpl_dynSetHash.H:268
Definition: ahFunction.H:115
Definition: tpl_linHash.H:560