-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.h
More file actions
101 lines (88 loc) 路 2.63 KB
/
Copy pathHashTable.h
File metadata and controls
101 lines (88 loc) 路 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include "../../Array/include/Vector.h"
#include "../../LinkedList/include/SinglyLinkedList.h"
#include <cstddef>
#include <stdexcept>
static const size_t _num_primes = 8;
static const size_t _prime_list[_num_primes] = {11, 13, 17, 19, 23, 29, 31, 37};
template <typename Key, typename Value>
class HashTable{
public:
HashTable();
~HashTable();
public:
// Capacity
bool empty() {return _size == 0;}
size_t size() {return _size;}
// Element access
Value& at(const Key& key);
Value& operator[](const Key& key);
// Element lookup
size_t count(const Key& key) const;
// Modifiers
void insert(const Key& key, const Value& value);
size_t erase(const Key& key);
void clear();
// Buckets
size_t buckets_count() const;
size_t max_buckets_count() const;
size_t bucket_size(const size_t n) const;
size_t bucket(const Key& key) const;
private:
size_t _next_prime(const size_t n) const;
size_t _bucket_num(const Key& key, size_t n);
size_t _hash(const Key& key);
private:
struct _KVNode{
Key _key;
Value _value;
};
Vector<SinglyLinkedList<_KVNode>> _buckets;
size_t _size;
};
// Constructor and Destructor
template <typename Key, typename Value>
HashTable<Key, Value>::HashTable() : _buckets(*(_prime_list)), _size(0) {}
// Elements access
template <typename Key, typename Value>
Value& HashTable<Key, Value>::at(const Key &key){
}
// Buckets
template <typename Key, typename Value>
size_t HashTable<Key, Value>::buckets_count() const{
return _buckets.size();
}
template <typename Key, typename Value>
size_t HashTable<Key, Value>::max_buckets_count() const{
return *(_prime_list + _num_primes - 1);
}
template <typename Key, typename Value>
size_t HashTable<Key, Value>::bucket_size(const size_t n) const{
if(n >= _buckets.size()){
throw std::out_of_range("HashTable::bucket_size(): Index out of range");
}
return _buckets[n].size();
}
// 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;
}
template <typename Key, typename Value>
size_t HashTable<Key, Value>::_bucket_num(const Key &key, size_t n){
return _hash(key) % n;
}
template <typename Value>
size_t HashTable<int, Value>::_hash(const int &key){
return key;
}
#endif // HASHTABLE_H