7
7
#include < stdexcept>
8
8
9
9
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 };
11
11
12
12
template <typename Key, typename Value>
13
13
class HashTable {
@@ -33,7 +33,7 @@ class HashTable{
33
33
size_t bucket_size (const size_t n) const ;
34
34
size_t bucket (const Key& key) const ;
35
35
private:
36
- unsigned _next_prime (unsigned n) ;
36
+ size_t _next_prime (const size_t n) const ;
37
37
private:
38
38
struct _KVNode {
39
39
Key _key;
@@ -45,7 +45,20 @@ class HashTable{
45
45
46
46
// Constructor and Destructor
47
47
template <typename Key, typename Value>
48
- HashTable<Key, Value>::HashTable() : _buckets(DefautBucketsCounts ), _size(0 ) {}
48
+ HashTable<Key, Value>::HashTable() : _buckets(*(_prime_list) ), _size(0 ) {}
49
49
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
+ }
50
63
51
64
#endif // HASHTABLE
0 commit comments