-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcustomized_Map_data_structure.cpp
More file actions
197 lines (182 loc) · 4.34 KB
/
customized_Map_data_structure.cpp
File metadata and controls
197 lines (182 loc) · 4.34 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <bits/stdc++.h>
using namespace std;
template <typename V>
class MapNode
{
public:
string key;
V value;
MapNode *next;
MapNode(string key, V value)
{
this->key = key;
this->value = value;
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;
}
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:
int getBucketIndex(string key)
{
int hashCode = 0;
int currentCoeff = 1;
for (int i = key.length() - 1; i >= 0; i--)
{
hashCode += key[i] * currentCoeff; // p^0 *s[0]+p^1s[1]+p^2s[2]....
hashCode = hashCode % numBuckets;
currentCoeff *= 37;
currentCoeff = currentCoeff % numBuckets;
}
return hashCode % numBuckets;
}
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:
void insert(string key, V value)
{
int bucketIndex = getBucketIndex(key);
MapNode<V> *head = buckets[bucketIndex];
while (head != 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();
}
}
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;
}
public:
double getLoadFactor()
{
return (1.0*count)/numBuckets;
}
};
int main()
{
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.getLoadFactor()<<endl;
}
cout<<map.size()<<endl;
map.remove("abc2");
map.remove("abc7");
for(int i=0;i<10;i++)
{
char c='0'+i;
string key="abc";
key=key+c;
cout<<key<<":"<< map.getValue(key)<<endl;
}
cout<<map.size()<<endl;
}