27 # ifndef TPL_DYNSETHASH_H 28 # define TPL_DYNSETHASH_H 33 # include <ahIterator.H> 36 # include <tpl_dynArray.H> 37 # include <tpl_dynMapOhash.H> 38 # include <tpl_dynLhash.H> 39 # include <tpl_linHash.H> 58 template <
typename Key,
59 template <
typename,
class>
class HashTable =
LhashTable,
60 class Cmp = Aleph::equal_to<Key>>
62 :
public HashTable<Key, Cmp>,
66 public GenericKeys<DynHashTable<Key, HashTable, Cmp>, Key>,
68 public StlAlephIterator<DynHashTable<Key, HashTable, Cmp>>
72 using Base = HashTable<Key, Cmp>;
74 using Bucket =
typename HashTable<Key, Cmp>::Bucket;
81 using Hash_Fct_Ptr =
typename Base::Hash_Fct_Ptr;
85 using Item_Type = Key;
98 Hash_Fct_Ptr hash_fct = Aleph::dft_hash_fct<Key>,
100 float lower_alpha = hash_default_lower_alpha,
101 float upper_alpha = hash_default_upper_alpha)
102 : Base(len, hash_fct, cmp, lower_alpha, upper_alpha, true, true)
108 float lower_alpha,
float upper_alpha)
109 : Base(len, hash_fct, cmp, lower_alpha, upper_alpha,
true,
true)
118 for (
typename Base::Iterator it(other); it.has_curr(); it.next_ne())
120 Bucket * bucket = (Bucket*) it.get_curr_ne();
121 insert(bucket->get_key());
128 : Base(other.len, other.hash_fct,
129 const_cast<DynHashTable&>(other).get_compare(),
130 other.lower_alpha, other.upper_alpha,
true,
true)
136 : Base(other.len, other.hash_fct, other.get_compare(),
137 other.lower_alpha, other.upper_alpha,
true,
true)
168 Key * insert_bucket(Bucket * bucket)
170 Bucket * ret_val = (Bucket*) this->Base::insert(bucket);
171 if (ret_val ==
nullptr)
177 return &ret_val->get_key();
180 tuple<Key*, bool> search_or_insert_bucket(Bucket * bucket)
182 Bucket * ret_val = (Bucket*) this->Base::search_or_insert(bucket);
183 if (ret_val != bucket)
186 return make_tuple(&ret_val->get_key(),
true);
189 return make_tuple(&ret_val->get_key(),
false);
198 return insert_bucket(
new Bucket(key));
203 return insert_bucket(
new Bucket(std::forward<Key>(key)));
206 Key * search_or_insert(
const Key & key)
208 return get<0>(search_or_insert_bucket(
new Bucket(key)));
211 Key * search_or_insert(Key && key)
213 return get<0>(search_or_insert_bucket(
new Bucket(std::forward<Key>(key))));
218 pair<Key*, bool> contains_or_insert(
const Key & key)
220 return search_or_insert_bucket(
new Bucket(key));
223 pair<Key*, bool> contains_or_insert(Key && key)
225 return search_or_insert_bucket(
new Bucket(std::forward<Key>(key)));
228 Key * add(
const Key & key)
230 return insert_bucket(
new Bucket(key));
233 Key * add(Key && key)
235 return insert_bucket(
new Bucket(std::forward<Key>(key)));
238 Key * append(
const Key & key)
240 return insert_bucket(
new Bucket(key));
243 Key * append(Key && key)
245 return insert_bucket(
new Bucket(std::forward<Key>(key)));
253 Bucket * bucket = (Bucket*) this->Base::search(key);
254 return bucket !=
nullptr ? &bucket->get_key() :
nullptr;
257 bool has(
const Key & key)
const 259 return this->Base::search(key) !=
nullptr;
262 bool contains(
const Key & key)
const {
return has(key); }
264 const Key & find(
const Key & key)
const 266 Bucket * bucket = (Bucket*) this->Base::search(key);
267 if (bucket ==
nullptr)
268 throw std::domain_error(
"Key not found in hash");
270 return bucket->get_key();
273 Key & find(
const Key & key)
275 Bucket * bucket = (Bucket*) this->Base::search(key);
277 if (bucket ==
nullptr)
278 throw std::domain_error(
"Key not found in hash");
280 return bucket->get_key();
285 static Bucket * key_to_bucket(Key * key)
287 Bucket * ret_val = 0;
288 size_t offset =
reinterpret_cast<size_t>(&ret_val->get_key());
290 return reinterpret_cast<Bucket*
>(
reinterpret_cast<size_t>(key) - offset);
297 void remove(Key * key)
299 Bucket * bucket = key_to_bucket(key);
300 this->Base::remove(bucket);
304 Key
remove(
const Key & key)
306 Bucket * bucket = (Bucket*) this->Base::search(key);
307 if (bucket ==
nullptr)
308 throw std::domain_error(
"Key not found in hash table");
310 this->Base::remove(bucket);
311 auto ret_val = bucket->get_key();
316 class Iterator :
public Base::Iterator
320 using Item_Type = Key;
324 Iterator(
const DynHashTable & table) : Base::Iterator(table) {}
326 Key & get_curr_ne() noexcept
328 return this->Base::Iterator::get_curr_ne()->get_key();
331 const Key & get_curr_ne()
const noexcept
333 return const_cast<Iterator*
>(
this)->get_curr_ne();
336 const Key & get_curr()
const 338 return const_cast<Iterator*
>(
this)->get_curr();
343 return this->Base::Iterator::get_curr()->get_key();
346 void del() {
delete this->Base::Iterator::del(); }
349 Key & get_first()
const 351 return this->
get_it().get_curr();
354 Key & get_last()
const 358 return it.get_curr();
366 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
367 struct DynSetLhash :
public DynHashTable<Key, LhashTable, Cmp>
377 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
378 struct DynSetLinHash :
public DynHashTable<Key, LinearHashTable, Cmp>
388 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
395 template <
typename Key,
typename Data,
396 template <
class,
class>
class HashTable =
LhashTable,
397 class Cmp = Aleph::equal_to<Key>>
398 class DynMapHashTable
400 HashTable, Dft_Pair_Cmp<Key, Data, Cmp>>
402 using Pair = std::pair<Key, Data>;
407 using Bucket =
typename Base::Bucket;
411 static Data & get_data(
const Key & key)
413 return key_to_pair<Key, Data>(&
const_cast<Key&
>(key))->second;
416 static const Key & get_key(Data * data_ptr)
418 return data_to_pair<Key, Data>(data_ptr)->first;
421 using Value_Type = Data;
425 using Iterator =
typename Base::Iterator;
427 using Hash_Fct = std::function<size_t(const Key&)>;
428 using Hash_Fct_Ptr = size_t (*) (
const Key &);
430 DynMapHashTable(
size_t len = Primes::DefaultPrime,
431 Hash_Fct_Ptr hash_fct = dft_hash_fct,
433 float lower_alpha = hash_default_lower_alpha,
434 float upper_alpha = hash_default_upper_alpha)
435 : Base(len, std::bind(map_hash_fct<Key, Data, Hash_Fct>,
436 hash_fct, std::placeholders::_1),
437 Dft_Pair_Cmp<Key, Data, Cmp>(cmp), lower_alpha, upper_alpha) {}
442 Pair *
insert(
const Key & key,
const Data & data)
444 return this->insert_bucket(
new typename Base::Bucket (Pair(key, data)));
447 Pair *
insert(
const Key & key, Data && data)
449 return this->insert_bucket
450 (
new typename Base::Bucket(Pair(key, forward<Data>(data))));
453 Pair *
insert(Key && key, Data && data)
455 return this->insert_bucket
456 (
new typename Base::Bucket(Pair(forward<Key>(key), forward<Data>(data))));
459 Pair *
insert(Key && key,
const Data & data)
461 return this->insert_bucket
462 (
new typename Base::Bucket(Pair(forward<Key>(key), data)));
468 Pair *
search(
const Key & key)
const 470 return this->Base::search(Pair(key, Data()));
473 Pair *
search(Key && key)
const 475 return this->Base::search(Pair(forward<Key>(key), Data()));
478 bool has(
const Key & key)
const {
return search(key) !=
nullptr; }
480 bool has(Key && key)
const {
return search(forward<Key>(key)) !=
nullptr; }
482 bool contains(
const Key & key)
const {
return has(key); }
484 bool contains(Key && key)
const {
return has(forward<Key>(key)); }
486 Data & find(
const Key & key)
488 return Base::find(Pair(key, Data())).second;
491 const Data & find(
const Key & key)
const 493 return Base::find(Pair(key, Data())).second;
496 Data & operator [] (
const Key & key)
498 return this->search_or_insert(Pair(key, Data()))->second;
501 const Data & operator [] (
const Key & key)
const 503 return this->find(key);
506 Data & operator [] (Key && key)
508 return this->search_or_insert(Pair(forward<Key>(key), Data()))->second;
511 const Data & operator [] (Key && key)
const 513 return this->find(forward<Key>(key));
518 void remove_by_data(Data & data)
520 Base::remove(data_to_pair<Key, Data>(&data));
523 Data
remove(
const Key & key)
525 auto p = Base::remove(Pair(key, Data()));
529 Data
remove(Key && key)
531 auto p = Base::remove(Pair(forward<Key>(key), Data()));
537 return this->
template maps<Key>([] (
auto p) {
return p.first; });
542 return this->
template maps<Data>([] (
auto p) {
return p.second; });
548 for (Iterator it(*
this); it.has_curr(); it.next_ne())
549 ret.
append(&it.get_curr_ne().second);
556 for (Iterator it(*
this); it.has_curr(); it.next_ne())
557 ret.
append(&it.get_curr_ne());
566 template <
typename Key,
typename Data,
567 class Cmp = Aleph::equal_to<Key>>
568 using DynMapLinHash = DynMapHashTable<Key, Data, LinearHashTable, Cmp>;
574 template <
typename Key,
typename Data,
575 class Cmp = Aleph::equal_to<Key>>
576 using DynMapHash = DynMapHashTable<Key, Data, LhashTable, Cmp>;
581 template <
typename T,
template <
typename>
class Container>
inline 584 DynSetLhash<T> table(c1);
585 c2.for_each([&table] (
const T & item)
592 template <
typename T,
template <
typename>
class Container =
DynList>
inline 593 DynList<T> intercept(
const Container<T> & c1,
const Container<T> & c2)
595 DynSetLhash<T> set1 = c1;
596 DynSetLhash<T> set2 = c2;
597 return set1.filter([&set2] (
const T & i) {
return set2.contains(i); });
600 template <
typename T,
template <
typename>
class Container>
inline 603 return DynSetLhash<T>(c).
keys();
606 template <
typename T,
template <
typename>
class Container>
inline 610 DynSetLhash<T> table;
612 c.
for_each([&table, &ret] (
const T & i)
614 auto * ptr = table.insert(i);
622 template <
typename T,
template <
typename>
class Container>
inline 626 DynSetLhash<T> table;
629 c.
for_each([&table, &ret, &i] (
const T & item)
631 auto * ptr = table.insert(item);
633 ret.
append(std::pair<T, size_t>(item, i));
642 # endif // TPL_DYNSETHASH_H
Definition: htlist.H:1290
DynList< T > keys() const
Definition: htlist.H:1306
auto get_it() const
Definition: htlist.H:151
void for_each(Operation &operation) noexcept(noexcept(operation))
Definition: htlist.H:589
Key * search(const Key &key) const
Definition: tpl_dynSetHash.H:251
DynHashTable(size_t len=Primes::DefaultPrime, Hash_Fct_Ptr hash_fct=Aleph::dft_hash_fct< Key >, Cmp cmp=Cmp(), float lower_alpha=hash_default_lower_alpha, float upper_alpha=hash_default_upper_alpha)
Definition: tpl_dynSetHash.H:97
Node * join(Node *t1, Node *t2, Node *&dup, Compare cmp=Compare()) noexcept
Definition: tpl_binNodeUtils.H:1855
typename Base::Hash_Fct Hash_Fct
El tipo de función hash.
Definition: tpl_dynSetHash.H:79
Key * insert(const Key &key)
Definition: tpl_dynSetHash.H:196
Definition: tpl_dynSetHash.H:61
Definition: tpl_lhash.H:660
T & append(const T &item)
Definition: htlist.H:1471
Definition: htlist.H:1323