00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 # ifndef TPL_LINHASH_H
00045 # define TPL_LINHASH_H
00046 # include <primes.H>
00047 # include <tpl_dynArray.H>
00048 # include <tpl_lhash.H>
00049
00050 # ifdef N
00051 # define NBACKUP N
00052 # undef N
00053 # endif
00054
00055 # ifdef M
00056 # define MBACKUP M
00057 # undef M
00058 # endif
00059
00060 using namespace Aleph;
00061
00062 namespace Aleph {
00063
00089 template <typename Key,
00090 template <class> class BucketType,
00091 class Cmp = Aleph::equal_to<Key> >
00092 class GenLinearHashTable
00093 {
00094
00095 public:
00096
00099 typedef size_t (*Hash_Fct)(const Key &);
00100
00102 typedef BucketType<Key> Bucket;
00103
00105 static const float default_upper_alpha;
00106
00108 static const float default_lower_alpha;
00109
00110 private:
00111
00112 typedef Dnode<Key> BucketList;
00113
00114 typedef typename Dnode<Key>::Iterator BucketItor;
00115
00116 static size_t multiply_by_two(const size_t & n) { return n << 1; }
00117
00118 static size_t divide_by_two(const size_t & n) { return n >> 1; }
00119
00120
00121 DynArray<BucketList> table;
00122 Hash_Fct hash_fct;
00123 size_t M;
00124 size_t N;
00125 size_t busy_slots_counter;
00126
00127 bool remove_all_buckets;
00128
00129
00130 float upper_alpha;
00131 float lower_alpha;
00132 size_t p;
00133 size_t l;
00134 size_t MP;
00135 size_t MM;
00136 const size_t len;
00137 size_t call_hash_fct(const Key & key) const
00138 {
00139 const size_t hash = (*hash_fct)(key);
00140
00141 const size_t i = hash % M;
00142
00143 return i < p ? hash % MM : i;
00144 }
00145 void expand()
00146 {
00147
00148 for (float alpha = 1.0*N/MP; alpha >= upper_alpha; alpha = 1.0*N/MP)
00149 {
00150 BucketList * src_list_ptr = table.test(p);
00151
00152 if (src_list_ptr != NULL)
00153 if (not src_list_ptr->is_empty())
00154 {
00155 BucketList * tgt_list_ptr = NULL;
00156
00157
00158 for (BucketItor it(*src_list_ptr); it.has_current(); )
00159 {
00160 Bucket * bucket = static_cast<Bucket*>(it.get_current());
00161
00162 it.next();
00163
00164 const Key & key = bucket->get_key();
00165
00166 const int i = (*hash_fct)(key) % MM;
00167
00168 if (i == p)
00169 continue;
00170
00171 if (tgt_list_ptr == NULL)
00172 tgt_list_ptr = &table.touch(MP);
00173
00174
00175
00176 bucket->del();
00177 tgt_list_ptr->append(bucket);
00178 }
00179
00180 if (src_list_ptr->is_empty())
00181 --busy_slots_counter;
00182
00183 ++busy_slots_counter;
00184 }
00185 ++p;
00186 ++MP;
00187 if (p == M)
00188 {
00189 ++l;
00190 p = 0;
00191 MP = M = MM;
00192 MM = multiply_by_two(MM);
00193 }
00194 }
00195 }
00196 void contract()
00197 {
00198
00199 for (float alpha = (1.0*N)/MP; alpha <= lower_alpha and MP > len;
00200 alpha = (1.0*N)/MP)
00201 {
00202 if (p == 0)
00203 {
00204 --l;
00205 MM = M;
00206 M = divide_by_two(M);
00207 p = M - 1;
00208 }
00209 else
00210
00211 --p;
00212
00213 --MP;
00214 if (MP < table.size())
00215 {
00216 BucketList * src_list_ptr = table.test(MP);
00217
00218 if (src_list_ptr != NULL)
00219 {
00220 if (not src_list_ptr->is_empty())
00221 {
00222 BucketList & tgt_list = table.touch(p);
00223
00224 tgt_list.concat_list(src_list_ptr);
00225
00226 --busy_slots_counter;
00227 }
00228
00229 table.cut(MP);
00230 }
00231 }
00232 }
00233 }
00234
00235 public:
00236
00256 GenLinearHashTable(Hash_Fct __hash_fct,
00257 const size_t & __len,
00258 const float & __lower_alpha,
00259 const float & __upper_alpha,
00260 const bool & __remove_all_buckets)
00261 throw(std::exception, std::length_error, std::domain_error,
00262 std::bad_alloc, std::overflow_error)
00263 : table(__len), hash_fct(__hash_fct), M(__len), N(0),
00264 busy_slots_counter(0), remove_all_buckets(__remove_all_buckets),
00265 upper_alpha(__upper_alpha), lower_alpha(__lower_alpha),
00266 p(0), l(0), MP(M), MM(multiply_by_two(M)), len(__len)
00267 {
00268 if (M == 0)
00269 std::length_error("table's length is zero");
00270
00271 if (MM > table.max_size())
00272 throw std::length_error("table's length too big");
00273
00274 if (upper_alpha <= lower_alpha)
00275 throw std::domain_error("lower alpha is greater than lower alpha");
00276 }
00277
00281 void set_upper_alpha(const float & __upper_alpha)
00282 {
00283 if (__upper_alpha <= lower_alpha)
00284 throw std::domain_error("upper_alpha lower than lower_alpha");
00285
00286 upper_alpha = __upper_alpha;
00287 }
00288
00292 void set_lower_alpha(const float & __lower_alpha)
00293 {
00294 if (__lower_alpha >= upper_alpha)
00295 throw std::domain_error("lower_alpha greater than upper_alpha");
00296
00297 lower_alpha = __lower_alpha;
00298 }
00299
00302 void empty()
00303 {
00304
00305 for (int i = 0; i < MP; ++i)
00306 {
00307 BucketList * list = table.test(i);
00308
00309 if (list != NULL)
00310 list->remove_all_and_delete();
00311 }
00312
00313 M = MP = len;
00314 MM = multiply_by_two(M);
00315 N = p = l = 0;
00316
00317 table.cut(len);
00318 }
00319
00320 ~GenLinearHashTable()
00321 {
00322 if (remove_all_buckets)
00323 empty();
00324 }
00328 Bucket * search(const Key & key)
00329 {
00330 const int i = call_hash_fct(key);
00331
00332 BucketList * list = table.test(i);
00333
00334 if (list == NULL)
00335 return NULL;
00336
00337 if (list->is_empty())
00338 return NULL;
00339
00340
00341 for (BucketItor it(*list); it.has_current(); it.next())
00342 {
00343 Bucket * bucket = static_cast<Bucket*>(it.get_current());
00344
00345 if (Cmp() (key, bucket->get_key()))
00346 return bucket;
00347 }
00348
00349 return NULL;
00350 }
00351
00352
00353
00355 const size_t & size() const { return N; }
00356
00358 const size_t & capacity() const { return MP; }
00359
00361 const size_t & busy_slots() const { return busy_slots_counter; }
00362
00365 const size_t & expansions() const { return l; }
00366
00368 Bucket* insert(Bucket * bucket)
00369 {
00370 const int i = call_hash_fct(bucket->get_key());
00371
00372 BucketList & list = table.touch(i);
00373
00374 if (list.is_empty())
00375 ++busy_slots_counter;
00376
00377 list.append(bucket);
00378
00379 ++N;
00380
00381 expand();
00382
00383 return bucket;
00384 }
00387 Bucket * remove(Bucket * bucket)
00388 {
00389 Bucket * next = static_cast<Bucket*>(bucket->get_next());
00390
00391 bucket->del();
00392
00393 if (next->is_empty())
00394 --busy_slots_counter;
00395
00396 --N;
00397
00398 contract();
00399
00400 return bucket;
00401 }
00402 void print()
00403 {
00404 for (int i = 0; i < MP; ++i)
00405 {
00406 cout << "table[" << i << "] = [ ";
00407
00408 if (table.exist(i))
00409 {
00410 BucketList & list = table.access(i);
00411
00412 if (not list.is_empty())
00413 for (BucketItor it(list); it.has_current(); it.next())
00414 {
00415 Bucket * bucket = static_cast<Bucket*>(it.get_current());
00416
00417 const Key & key = bucket->get_key();
00418
00419 cout << key << ",";
00420 }
00421 }
00422 cout << "]" << endl;
00423 }
00424 }
00425 };
00426
00427 template <typename Key, template <class> class BucketType, class Cmp>
00428 const float
00429 GenLinearHashTable<Key, BucketType, Cmp>::default_upper_alpha = 0.95;
00430
00431 template <typename Key, template <class> class BucketType, class Cmp>
00432 const float
00433 GenLinearHashTable<Key, BucketType, Cmp>::default_lower_alpha = 0.25;
00434
00435
00455 template <typename Key, class Cmp = Aleph::equal_to<Key> >
00456 class LinearHashTable : public GenLinearHashTable<Key, LhashBucket, Cmp>
00457 {
00458
00459
00460 typedef GenLinearHashTable<Key, LhashBucket, Cmp> Base;
00461
00462 public:
00463
00465 typedef typename GenLinearHashTable<Key, LhashBucket, Cmp>::Bucket Bucket;
00466
00468 typedef typename GenLhashTable<Key, LhashBucket<Key>, Cmp>::Hash_Fct Hash_Fct;
00469
00491 LinearHashTable(Hash_Fct hash_fct,
00492 const size_t & len,
00493 const float & lower_alpha = Base::default__lower_alpha,
00494 const float & upper_alpha = Base::default_upper_alpha,
00495 const bool & remove_all_buckets = true)
00496 throw(std::exception, std::length_error, std::runtime_error,
00497 std::bad_alloc, std::overflow_error)
00498 : Base(hash_fct, len, lower_alpha, upper_alpha, remove_all_buckets)
00499 {
00500
00501 }
00502
00503 };
00504
00524 template <typename Key, class Cmp = Aleph::equal_to<Key> >
00525 class LinearHashTableVtl : public GenLinearHashTable<Key, LhashBucketVtl, Cmp>
00526 {
00527
00528
00529 typedef GenLinearHashTable<Key, LhashBucketVtl, Cmp> Base;
00530
00531 public:
00532
00534 typedef typename GenLinearHashTable<Key, LhashBucketVtl, Cmp>::Bucket Bucket;
00535
00537 typedef typename
00538 GenLhashTable<Key, LhashBucketVtl<Key>, Cmp >::Hash_Fct Hash_Fct;
00539
00561 LinearHashTableVtl(Hash_Fct hash_fct,
00562 const size_t & len,
00563 const float & lower_alpha = Base::default_lower_alpha,
00564 const float & upper_alpha = Base::default_upper_alpha,
00565 const bool & remove_all_buckets = true)
00566 throw(std::exception, std::length_error, std::runtime_error,
00567 std::bad_alloc, std::overflow_error)
00568 : Base(hash_fct, len, lower_alpha, upper_alpha, remove_all_buckets)
00569 {
00570
00571 }
00572
00573 };
00574
00575 }
00576
00577 # ifdef NBACKUP
00578 # define N NBACKUP
00579 # undef NBACKUP
00580 # endif
00581
00582 # ifdef MBACKUP
00583 # define M MBACKUP
00584 # undef MBACKUP
00585 # endif
00586
00587 # endif // TPL_LINHASH_H
00588