Skip to content

Commit 468e7fd

Browse files
committed
update HashTable::_next_prime
1 parent a506f85 commit 468e7fd

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

data-structures/HashTable/include/HashTable.h

+16-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <stdexcept>
88

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

1212
template <typename Key, typename Value>
1313
class HashTable{
@@ -33,7 +33,7 @@ class HashTable{
3333
size_t bucket_size(const size_t n) const;
3434
size_t bucket(const Key& key) const;
3535
private:
36-
unsigned _next_prime(unsigned n);
36+
size_t _next_prime(const size_t n) const;
3737
private:
3838
struct _KVNode{
3939
Key _key;
@@ -45,7 +45,20 @@ class HashTable{
4545

4646
// Constructor and Destructor
4747
template <typename Key, typename Value>
48-
HashTable<Key, Value>::HashTable() : _buckets(DefautBucketsCounts), _size(0) {}
48+
HashTable<Key, Value>::HashTable() : _buckets(*(_prime_list)), _size(0) {}
4949

50+
// Private Functions
51+
template <typename Key, typename Value>
52+
size_t HashTable<Key, Value>::_next_prime(const size_t n) const{
53+
const size_t *first = _prime_list;
54+
const size_t *last = _prime_list + _num_primes;
55+
if(n <= (*first)) {return *first;}
56+
if(n >= (*(last - 1))) {return *(last - 1);}
57+
const size_t pos = first;
58+
while(pos != last && (*pos) < n){
59+
++pos;
60+
}
61+
return pos == last ? *(last - 1) : *pos;
62+
}
5063

5164
#endif // HASHTABLE

0 commit comments

Comments
 (0)