X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=include%2Fhfta%2Fhash_table.h;h=2d800359740e97e847bf18e1d53cc3bcde4fc805;hb=393a42a5b1ba6e64bd3eabf7d0ce2f197e966355;hp=e82940cc66f6e7472ff8c250fd5a2cb62fa72374;hpb=c22232e56a6b37faa24300a008436fe776074419;p=com%2Fgs-lite.git diff --git a/include/hfta/hash_table.h b/include/hfta/hash_table.h index e82940c..2d80035 100644 --- a/include/hfta/hash_table.h +++ b/include/hfta/hash_table.h @@ -40,7 +40,11 @@ public : value_type second; data_item* next; // next data item in overflow chain - data_item(const key_type& k, const value_type& v) : first(k), second(v), next(NULL) { } + data_item(const key_type& k, const value_type& v) { + first = k; + second = v; + next = NULL; + } }; struct hash_bucket { @@ -101,15 +105,19 @@ private: equal_func equal; double load_factor; -// double max_load; size_t _size; size_t _max_size; size_t num_buckets; size_t hash_mask; + // bucket allocation happens on first insert + // this flag indicates that buckets has been allocated + unsigned allocated; + + hash_bucket* first_bucket; // first used bucket - hash_bucket* last_bucket; // last used bucket + hash_bucket* last_bucket; // last used bucket hash_bucket* buckets; @@ -119,24 +127,38 @@ private: public: - hash_table(const size_t n_buckets = 65536, const double load = 0.5) { - load_factor = load; + hash_table(const size_t expected_size = 100000, const double max_load = 1.25) { + + size_t min_buckets = (size_t)(expected_size / max_load); + load_factor = max_load; int nb; - for(nb=2;nbnext = temp->next; } - delete (*temp).first; - delete (*temp).second; +// delete (*temp).first; +// delete (*temp).second; delete temp; total_memory -= sizeof(data_item); } @@ -326,35 +364,51 @@ public: last_bucket = NULL; _size = 0; total_memory = num_buckets * sizeof(hash_bucket); - } - int rehash() { + int resize() { if (_size) { - fprintf(stderr, "Error: rehashing non-empty hash table\n"); + fprintf(stderr, "Error: resizing non-empty hash table\n"); exit(1); } double max_load = (1.0 * _max_size) / num_buckets; - if (max_load > load_factor ) { - delete[] buckets; - // roughly double the size of the table - num_buckets *= 2; - hash_mask = num_buckets-1; - - buckets = new hash_bucket[num_buckets]; - total_memory = num_buckets * sizeof(hash_bucket); + // reize table if its maximum load exceed the load factor + // OR it was less then half of the load factor + size_t min_buckets = 0; + if ((max_load > load_factor) || ((2 * max_load) < load_factor)) { + min_buckets = ceil(_max_size / load_factor); + } + + + if (min_buckets) { + // find power-of-2 size large than min_buckets; + int nb; + for(nb=2;nb load_factor ) { - delete[] buckets; - // roughly double the size of the table - - num_buckets *= 2; - hash_mask = num_buckets -1; - buckets = new hash_bucket[num_buckets]; + // reize table if its maximum load exceed the load factor + // OR it was less then half of the load factor + size_t min_buckets = 0; + if ((max_load > load_factor) || ((2 * max_load) < load_factor)) { + min_buckets = ceil(_max_size / load_factor); + } + + if (min_buckets) { + // find power-of-2 size large than min_buckets; + int nb; + for(nb=2;nb