-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_map.cpp
More file actions
170 lines (147 loc) · 3.27 KB
/
hash_map.cpp
File metadata and controls
170 lines (147 loc) · 3.27 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
/** implementation of a custom hash map **/
#include <iostream>
#include <cmath>
using namespace std;
template <class K, class V>
class hash_value
{
public:
K key;
V value;
hash_value():key(-1),value(-1){}
hash_value(K key, V value):key(key),value(value)
{}
};
template <class K, class V>
class hash_map
{
int size;
int capacity;
hash_value<K,V>** map;
hash_value<K,V>* dummy;
public:
hash_map():size(0),capacity(5)
{
dummy=new hash_value<K,V>(-1,-1);
map=new hash_value<K,V>*[capacity];
for(int i=0;i<capacity;i++)
map[i]=NULL;
}
/** generate hash code for a key **/
int hash_code(K k)
{
return k%capacity;
}
/** expand the size of the hash map **/
void reserve(int new_Size,bool copy)
{
hash_value<K,V>** newmap=new hash_value<K,V>* [new_Size];
int old_size=capacity;
capacity=new_Size;
for(int i=0;i<old_size;i++)
{
if(map[i]!=NULL || map[i]->key!=-1)
{
int new_index=hash_code(map[i]->key);
while(newmap[new_index]!=NULL)
{
new_index++;
new_index=new_index%new_Size;
}
newmap[new_index]=map[i];
}
}
map=newmap;
capacity=new_Size;
}
/** insert <key,value> pair in the hash map **/
void insert(K k, V v)
{
hash_value<K,V>* newval=new hash_value<K,V>(k,v);
int index=hash_code(k);
while(map[index]!=NULL && map[index]->key!=newval->key
&& map[index]->key!=-1)
{
index++;
index=index%capacity;
}
if(map[index]==NULL || map[index]->key==-1)
{
size++;
}
map[index]=newval;
if(size==capacity)
{
reserve(2*capacity,true);
}
}
/** remove an element **/
void remove(K k)
{
int index=hash_code(k);
while(map[index]!=NULL)
{
if(map[index]->key==k)
{
map[index]=dummy;
size--;
}
index++;
index=index%capacity;
}
}
/**search for a element**/
V get(K k)
{
int index=hash_code(k);
while(map[index]!=NULL)
{
if(map[index]->key==k)
return map[index]->value;
index++;
index=index%capacity;
}
return dummy->value;
}
/** return current size of the hash map**/
int curr_size()
{
return size;
}
/** return maximum size of the hash map **/
int max_size()
{
return capacity;
}
/** display the current contents of the hash map**/
void disp()
{
cout<<endl<<"Hash_map contents: "<<endl;
for(int i=0;i<capacity;i++)
{
if(map[i]!=NULL)
{
cout<<map[i]->key<<" "<<map[i]->value<<endl;
}
}
}
};
int main()
{
//testing the member functions
hash_map<int,int> h;
cout<<h.curr_size()<<" "<<h.max_size()<<endl;
h.insert(1,2);
h.insert(2,3);
h.insert(3,4);
cout<<endl<<h.max_size();
h.insert(5,6);
cout<<endl<<h.max_size();
h.insert(7,8);
cout<<endl<<h.max_size();
h.insert(9,10);
cout<<endl<<h.max_size();
// h.disp();
cout<<endl<<h.get(3)<<" "<<h.get(11)<<" "<<h.get(1)<<" "<<h.get(7)<<" "<<h.get(9);
return 0;
}