10
10
#include < iostream>
11
11
#include < cstddef>
12
12
13
- template <typename K, std:: size_t size >
13
+ template <typename K>
14
14
struct KeyHash
15
15
{
16
- unsigned long operator () (const K& key)
16
+ unsigned long operator () (const K& key, size_t tableSize )
17
17
{
18
- return reinterpret_cast <unsigned long >(key) % size ;
18
+ return reinterpret_cast <unsigned long >(key) % tableSize ;
19
19
}
20
20
};
21
21
22
- template <typename K, typename V, std::size_t size = 1024 , typename F = KeyHash<K, size >>
22
+ template <typename K, typename V, std::size_t tableSize = 1024 , typename F = KeyHash<K>>
23
23
class HashMap
24
24
{
25
25
private:
26
- std::array<std::vector<HashNode<K, V>>, size > table { };
26
+ std::array<std::vector<HashNode<K, V>>, tableSize > table { };
27
27
F hashFunc;
28
28
29
29
public:
@@ -32,8 +32,9 @@ class HashMap
32
32
33
33
void put (const K& key, const V& value)
34
34
{
35
- unsigned long hash = hashFunc (key);
35
+ unsigned long hash = hashFunc (key, tableSize );
36
36
37
+ std::cout << key << " , " << value << " , " << hash << " \n " ;
37
38
for (auto node : table[hash]) {
38
39
if (node.getKey () == key) {
39
40
// key already present in map. Simply overwrite value
@@ -49,7 +50,7 @@ class HashMap
49
50
50
51
void remove (const K& key)
51
52
{
52
- unsigned long hash = hashFunc (key);
53
+ unsigned long hash = hashFunc (key, tableSize );
53
54
54
55
if (table[hash].size () == 1 ) {
55
56
if (table[hash][0 ].getKey () == key) {
@@ -67,7 +68,7 @@ class HashMap
67
68
68
69
bool get (const K& key, V& value)
69
70
{
70
- unsigned long hash = hashFunc (key);
71
+ unsigned long hash = hashFunc (key, tableSize );
71
72
72
73
for (auto node : table[hash]) {
73
74
if (node.getKey () == key) {
0 commit comments