-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhashmap_4.cpp
More file actions
176 lines (153 loc) · 4.18 KB
/
Copy pathhashmap_4.cpp
File metadata and controls
176 lines (153 loc) · 4.18 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <iostream>
#include <string>
using namespace std;
template <typename V>
class mapNode //apne map jo ki array type ka hoga usme harek point pr node hi rahega
{ //usi ke liye ye class hai
string key;
V value;
mapNode* next;
mapNode(string key,V value){
this->key=key;
this->value = value;
this->next = NULL;
}
~mapNode(){
delete next;
}
};
template<typename V>
class ourMap
{
mapNode<V>** buckets;
int count;
int numBuckets;
public:
ourMap(){
count = 0;
numBuckets=5;
buckets = new mapNode<V>*[numBuckets];
for(int i=0;i<numBuckets;i++){
buckets[i]=NULL;
}
}
~ourMap(){
for(int i=0;i<numBuckets;i++){
delete buckets[i];
}
delete []buckets;
}
int size(){
return count;
}
//to return a value
V getValue(string key){
int bucketIndex = getBucketIndex(key);
mapNode<V>* head = buckets[bucketIndex];
while(head!=NULL){
if(head->key==key){
return head->value;
}
head=head->next;
}
return 0;
}
private:
//ye ek helper function hai
int getBucketIndex(string key){
int hashCode=0;
int currentCoeff=1;
for(int i=key.length()-1;i>=0;i--){
hashCode += key[i] * currentCoeff;
hashCode = hashCode%numBuckets;
currentCoeff*=37; //37 apne man se ek prime number le liye hain
currentCoeff = currentCoeff%numBuckets; //taki current coeff jyada bda na ho
}
return hashCode%numBuckets;
}
//rehashing function
void rehash(){
mapNode<V>** temp = buckets;
buckets=new mapNode<V>*[2*numBuckets];
for(int i=0;i<2*numBuckets;i++){
buckets[i]=NULL;
}
int oldBucketCount=numBuckets;
numBuckets*=2;
count=0;
for(int i=0;i<oldBucketCount;i++){
mapNode<V>* head=temp[i];
while(head!=NULL){
string key=head->key;
V value= head->value;
insert(key,value);
head=head->next;
}
}
for(int i=0;i<oldBucketCount;i++){
mapNode<V>* head=temp[i];
delete head;
}
delete []temp;
}
public:
//insert
void insert(string key,V value){
int bucketIndex = getBucketIndex(key);
mapNode<V>* head = buckets[bucketIndex];
while(head->next!=NULL){
if(head->key == key){
head->value = value;
return;
}
head=head->next;
}
head = buckets[bucketIndex];
mapNode<V>* node = new mapNode<V>(key,value);
node->next = head;
buckets[bucketIndex] = node;
count++;
double loadFactor = (1.0*count)/numBuckets;
if(loadFactor>0.7){
rehash();
}
}
//remove
V remove(string key){
int bucketIndex = getBucketIndex(key);
mapNode<V>* head = buckets[bucketIndex];
mapNode<V>* prev = NULL;
while(head!=NULL){
if(head->key==key){
if(prev==NULL){
buckets[bucketIndex]=head->next;
}else{
prev->next=head->next;
}
V value = head->value;
head->next=NULL;
delete head;
count--;
return value;
}
prev=head;
head=head->next;
}
return 0;
}
};
int main()
{
cout<<"-------------Program started-------------"<<endl;
ourMap<int> map;
for(int i=0;i<10;i++){
char c = '0'+i;
string key = "abc";
key = key+c;
int value = i+1;
//map.insert(key,value);
}
cout<<"Map size is: "<<map.size()<<endl;
//hashmaps ka last video ek baar dekh kr phir se implement krne ka try krna hai
return 0;
}