Skip to content

Commit

Permalink
update HashTable::_next_prime
Browse files Browse the repository at this point in the history
  • Loading branch information
bitdove committed Sep 30, 2024
1 parent a506f85 commit 468e7fd
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions data-structures/HashTable/include/HashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <stdexcept>

static const size_t _num_primes = 8;
static const unsigned _prime_list[_num_primes] = {11, 13, 17, 19, 23, 29, 31, 37};
static const size_t _prime_list[_num_primes] = {11, 13, 17, 19, 23, 29, 31, 37};

template <typename Key, typename Value>
class HashTable{
Expand All @@ -33,7 +33,7 @@ class HashTable{
size_t bucket_size(const size_t n) const;
size_t bucket(const Key& key) const;
private:
unsigned _next_prime(unsigned n);
size_t _next_prime(const size_t n) const;
private:
struct _KVNode{
Key _key;
Expand All @@ -45,7 +45,20 @@ class HashTable{

// Constructor and Destructor
template <typename Key, typename Value>
HashTable<Key, Value>::HashTable() : _buckets(DefautBucketsCounts), _size(0) {}
HashTable<Key, Value>::HashTable() : _buckets(*(_prime_list)), _size(0) {}

// Private Functions
template <typename Key, typename Value>
size_t HashTable<Key, Value>::_next_prime(const size_t n) const{
const size_t *first = _prime_list;
const size_t *last = _prime_list + _num_primes;
if(n <= (*first)) {return *first;}
if(n >= (*(last - 1))) {return *(last - 1);}
const size_t pos = first;
while(pos != last && (*pos) < n){
++pos;
}
return pos == last ? *(last - 1) : *pos;
}

#endif // HASHTABLE

0 comments on commit 468e7fd

Please sign in to comment.