-
Notifications
You must be signed in to change notification settings - Fork 1
/
HashTableElementary.cpp
120 lines (97 loc) · 2.75 KB
/
HashTableElementary.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
// Elementary Hash table implementation in C++
/*
* Hash value calculation : Hash key calculation DJB algorithm from Daniel J. Bernstein.
* Storage : Array of certain CAPACITY
* Keys type: For simplicity assume key are string
* Values type : Values can be anything
* Collision : Collision resolution through linked list
*/
#include <iostream>
#include <vector>
#include <string>
#include <array>
#include <climits>
const unsigned int CAPACITY = 2048;
class HashTable
{
private:
class Node
{
public:
const std::string key;
int value;
Node(const std::string& k, int v) : key(k), value(v) { }
void assignValue(int v) { value = v; }
};
std::array<std::vector<Node>, CAPACITY> table { };
public:
unsigned int keyHashIndex(const std::string& key)
{
unsigned int hash = 5381;
for (auto ch : key) {
hash = (hash << 5) + hash + static_cast<int>(ch);
}
return hash % CAPACITY;
}
HashTable() { }
~HashTable() = default;
// If value for key already exists then overwrite value
void put(const std::string& key, int value)
{
if (key.size() == 0) {
return;
}
// check if node is present
int index = keyHashIndex(key);
for (auto node : table[index]) {
if (key.compare(node.key) == 0) {
node.assignValue(value);
return;
}
}
Node node(key, value);
table[index].push_back(node);
}
int get(const std::string& key)
{
if (key.size() == 0) {
return INT_MIN;
}
// check if node is present
int index = keyHashIndex(key);
for (auto node : table[index]) {
if (key.compare(node.key) == 0) {
return node.value;
}
}
return INT_MIN;
}
int remove(const std::string& key)
{
if (key.size() == 0) {
return INT_MIN;
}
// check if node is present
int index = keyHashIndex(key);
for (auto it = table[index].begin(); it != table[index].end(); ++it) {
if ((*it).key.compare(key) == 0) {
int v = (*it).value;
// ----- FIXME -------
// it = table[index].erase(it);
return v;
}
}
return INT_MIN;
}
};
int main()
{
HashTable table;
table.put("alice", 46);
table.put("bob", 11);
table.put("charlie", 19);
std::cout << "Value of key alice : " << table.get("alice") << "\n";
std::cout << "Value of key bob : " << table.get("bob") << "\n";
std::cout << "Value of key charlie : " << table.get("charlie") << "\n";
return 0;
}