-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTableImplementationGeneric.cpp
executable file
·122 lines (100 loc) · 3.35 KB
/
HashTableImplementationGeneric.cpp
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// http://stackoverflow.com/questions/16243711/how-to-implement-a-generic-hash-function-in-c
// Should making "hash()" generic as well
#include <iostream>
#include <vector>
#include <functional> // Contains function for hash
using namespace std;
static const uint32_t HASHTABLE_SIZE = 100;
static size_t
myComputeHash(const int& key)
{
// http://stackoverflow.com/questions/8094790/how-to-get-hash-code-of-a-string-in-c
size_t hashVal = hash<int>()(key);
return hashVal % 100;
}
// Make Hash function generic as well
template<typename K, typename V, typename H = hash<K> >
class HashTable
{
private:
class HashEntry; // forward declaration
size_t _size;
vector<HashEntry> _hashTable;
class HashEntry
{
public:
K _key;
V _value;
HashEntry(K key, V value) : _key(key),
_value(value) { }
K getKey() { return _key; }
V getValue() {return _value; }
};
public:
HashTable();
HashTable(size_t size);
void put(K key, V value);
V get(K key);
size_t computeHash(const K& key);
};
// Default Parameter should be called out only in the declaration
// http://stackoverflow.com/questions/12989062/having-trouble-with-template-classes-having-template-member-functions
template<typename K, typename V, typename H >
HashTable<K, V, H>::HashTable(size_t size)
{
this->_size = size;
_hashTable.reserve(_size);
}
template<typename K, typename V, typename H >
HashTable<K, V, H>::HashTable()
{
this->_size = HASHTABLE_SIZE;
_hashTable.reserve(_size);
}
template<typename K, typename V, typename H >
size_t
HashTable<K, V, H>::computeHash(const K& key)
{
// http://stackoverflow.com/questions/8094790/how-to-get-hash-code-of-a-string-in-c
size_t hashVal = H()(key);
return hashVal % _size;
}
template<typename K, typename V, typename H >
void
HashTable<K, V, H>::put(K key, V value)
{
// Compute a new key based on the given key
size_t newKey = computeHash(key);
// Create a Hash entry with user's given key and value and push into the vector
// Index where the entry should be pushed is computed from compute Hash
// Replace existing value with new value if already exists
//
// TODO: Make it a list to add support for chaining
_hashTable.insert(_hashTable.begin() + newKey, HashEntry (key, value));
}
template<typename K, typename V, typename H >
V
HashTable<K, V, H>::get(K key)
{
// Compute a new key based on the given key
size_t newKey = computeHash(key);
return _hashTable[newKey].getValue();
}
int main()
{
// http://www.drdobbs.com/windows/user-defined-hash-functions-for-unordere/231600210
// http://stackoverflow.com/questions/17016175/c-unordered-map-using-a-custom-class-type-as-the-key
// http://stackoverflow.com/questions/15810620/unordered-map-with-custom-hashing-equal-functions-functions-dont-get-called
//HashTable<int, string, myComputeHash>* ht = new HashTable<int, string, myComputeHash>();
HashTable<int, string>* ht = new HashTable<int, string>();
for (uint32_t i = 0; i < 10; i++)
{
ht->put(i, "Value: " + to_string(i));
}
for (uint32_t i = 0; i < 10; i++)
{
cout << ht->get(i) << ", ";
}
cout << endl;
return 0;
}