11 using namespace Primes;
13 using namespace Aleph;
43 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
50 typedef Key Item_Type;
53 typedef size_t (*Hash_Fct)(
const Key &);
55 enum Status { EMPTY, BUSY, DELETED };
62 Bucket() : status(EMPTY) {}
63 void reset() { status = EMPTY; }
66 static Bucket * key_to_bucket(Key * rec)
69 long offset = (long) &ret_val->key;
70 return (
Bucket*) ((long) rec - offset);
87 bool is_valid_bucket(Bucket * bucket)
const
89 if (bucket < &table[0] or bucket >= &table[len])
92 int offset_with_base = (
char*) bucket - (
char*) &table[0];
94 return offset_with_base %
sizeof(*bucket) == 0;
102 Hash_Fct null_hash_fct = NULL,
103 size_t __len = Primes::DefaultPrime,
104 const float & __lower_alpha = hash_default_lower_alpha,
105 const float & __upper_alpha = hash_default_upper_alpha,
106 const bool __with_resize =
true)
107 : table(NULL), N(0), len(Primes::next_prime(__len)),
108 lower_alpha(__lower_alpha), upper_alpha(__upper_alpha),
109 hash_fct(__hash_fct), with_resize(__with_resize)
111 table =
new Bucket [len];
123 std::swap(table, other.table);
124 std::swap(N, other.N);
125 std::swap(len, other.len);
126 std::swap(hash_fct, other.hash_fct);
127 std::swap(lower_alpha, other.lower_alpha);
128 std::swap(upper_alpha, other.upper_alpha);
129 std::swap(with_resize, other.with_resize);
134 other.lower_alpha, other.upper_alpha, other.with_resize)
136 copy_from_table(other);
141 other.lower_alpha, other.upper_alpha, other.with_resize)
155 Bucket * new_table =
new Bucket [other.len];
160 hash_fct = other.hash_fct;
161 lower_alpha = other.lower_alpha;
162 upper_alpha = other.upper_alpha;
165 copy_from_table(other);
180 long i = (*hash_fct)(key) % len, c = 0;
182 while (c < len and table[i].status != EMPTY)
184 if (table[i].status == BUSY and Cmp () (table[i].key, key))
185 return &table[i].key;
197 Bucket * allocate_bucket(
const Key & key)
200 throw std::overflow_error(
"Hash table is full");
202 size_t i = (*hash_fct)(key) % len;
204 while (table[i].status == BUSY)
206 if (Cmp () (key, table[i].key))
213 Bucket * bucket = &table[i];
216 bucket->status = BUSY;
227 void deallocate_bucket(Bucket * bucket)
229 if (not is_valid_bucket(bucket))
230 throw std::invalid_argument(
"record address is not inside table's range");
232 if (bucket->status != BUSY)
233 throw std::domain_error(
"Bucket containing record is not busy");
236 int i = bucket - &table[0];
237 table[i].status = DELETED;
252 void remove(
const Key & key)
254 Key * key_ptr =
search(key);
256 throw std::domain_error(
"Key not in hash table");
263 Generic_Traverse(Key);
264 Functional_Methods(Key)
270 size_t num_busy = 0, num_deleted = 0, num_empty = 0;
271 size_t max_len = std::numeric_limits<size_t>::min();
272 for (
int i = 0; i < len; ++i)
273 switch (table[i].status)
278 const Key & key = table[i].key;
279 long i = (*hash_fct)(key) % len;
284 if (table[i].status == BUSY and Cmp () (table[i].key, key))
291 max_len = std::max(max_len, count);
292 update_stat_len(lens, count);
297 update_stat_len(lens, 0);
304 float avg = 0, sum = 0;
305 for (
int i = 0; i < lens.
size(); ++i)
313 for (
int i = 0; i < lens.
size(); ++i)
321 stats.num_busy = num_busy;
322 stats.num_deleted = num_deleted;
323 stats.num_empty = num_empty;
324 std::swap(lens, stats.lens);
327 stats.max_len = max_len;
334 # endif // TPL_OLHASH_H
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Definition: ahAlgo.H:127
Definition: tpl_olhash.H:57
size_t size() const
Retorna dimensión actual (más lejano índice escrito)
Definition: tpl_dynArray.H:523
Itor1 search(Itor1 beg, const Itor1 &end, Itor2 searchBeg, const Itor2 &searchEnd, BinaryPredicate op=BinaryPredicate())
Definition: ahAlgo.H:327
OLhashTable(Hash_Fct __hash_fct=Aleph::dft_hash_fct< Key >, Hash_Fct null_hash_fct=NULL, size_t __len=Primes::DefaultPrime, const float &__lower_alpha=hash_default_lower_alpha, const float &__upper_alpha=hash_default_upper_alpha, const bool __with_resize=true)
Definition: tpl_olhash.H:101
Key * search(const Key &key) const
Definition: tpl_olhash.H:178
~OLhashTable()
Libera toda la memoria ocupada.
Definition: tpl_olhash.H:115
Definition: tpl_dynArray.H:70
Definition: tpl_olhash.H:44