-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHashMap.cpp
121 lines (115 loc) · 3.54 KB
/
HashMap.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
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
template <typename K, typename V> class HashNode{
public:
K key;
V value;
HashNode<K,V>* next;
HashNode(K key, V value) {
this->key = key;
this->value = value;
}
} ;
template <typename K, typename V> class HashMap{
vector<HashNode<K,V>*> *bucketArray;
int numBuckets;
int size;
public:
HashMap(){
bucketArray = new vector<HashNode<K,V>*>();
numBuckets = 10;
size = 0;
for( int i = 0; i<numBuckets; i ++)
bucketArray->emplace_back(nullptr);
}
//Function name and variable name cant be same;
int Size() { return size;}
bool isEmpty() { return size == 0;}
int getBucketIndex( K key){
unsigned long long hashCode = std::hash<K>{}(key);
int index = hashCode % numBuckets;
return index;
}
V remove(K key){
int bucketIndex = getBucketIndex(key);
HashNode<K,V>* head = bucketArray->at(bucketIndex);
HashNode<K,V>* prev = nullptr;
while(head != nullptr){
if(head->key== key)
break;
prev = head;
head = head->next;
}
// key not found
if(head == nullptr)
return NULL;
size--;
if(prev != nullptr)
prev->next = head->next;
else
bucketArray->at(bucketIndex) = head->next;
return head->value;
}
V get(K key){
int bucketIndex = getBucketIndex(key);
HashNode<K,V>* head = bucketArray->at(bucketIndex);
while(head!= nullptr){
if(head->key == key)
return head->value;
head = head->next;
}
return NULL;
}
void add(K key, V value){
int bucketIndex = getBucketIndex(key);
HashNode<K,V>* head = bucketArray->at(bucketIndex);
while(head!= nullptr){
if(head->key == key){
head->value= value;
return;
}
head = head->next;
}
size++;
head = bucketArray->at(bucketIndex);
HashNode<K,V>* newNode = new HashNode<K,V>(key, value);
newNode->next = head;
bucketArray->at(bucketIndex) = newNode;
if((1.0*size)/numBuckets >= 0.7){
vector<HashNode<K,V>*>*temp = bucketArray;
bucketArray = new vector<HashNode<K,V>*>;
numBuckets = 2 * numBuckets;
size = 0;
for (int i = 0; i < numBuckets; i ++)
bucketArray->emplace_back(nullptr);
HashNode<K,V>* delNode;
for(HashNode<K,V>* headNode : *temp){
while(headNode != nullptr){
add(headNode->key, headNode->value);
delNode = headNode;
headNode = headNode->next;
delete(delNode);
}
}
temp->clear();
}
}
};
int main(){
HashMap<string, int> map;
map.add(string("this"),1);
cout << map.Size() <<endl;
map.add(string("coder"),2);
cout << map.Size() <<endl;
map.add(string("this"),4);
cout << map.Size() <<endl;
map.add(string("hi"),5);
cout <<"Size:"<< map.Size() <<endl;
cout <<"Remove returns "<< map.remove("this") <<endl;
cout <<"Size:" << map.Size() <<endl;
cout <<"Empty :" << map.isEmpty() <<endl;
return 0;
}