-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashMap.cpp
203 lines (180 loc) · 5.35 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
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
198
199
200
201
202
203
#include<iostream>
#include<cassert>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;
template <class v>
struct HashItem
{
int key;
v value;
short int status;
};
template <class v>
class HashMap
{
protected:
HashItem<v>* hashArray;
int capacity;
int currentElements;
void doubleCapacity()
{
int oldCapacity = capacity;
this->capacity = capacity*2;
HashItem<v>* newHashArray = new HashItem<v> [capacity] ();
cout<<endl<<"Doubling the capacity from "<<oldCapacity<<" to "<<this->capacity<<" and rearranging all the key value pairs.\n";
for(int i=0; i<oldCapacity; i++)
if(hashArray[i].status==2)//if occupied
insert_helper(hashArray[i].key, hashArray[i].value, newHashArray);
this->hashArray=newHashArray;
newHashArray = nullptr;
}
virtual int getNextCandidateIndex(int key, int i, HashItem<v>* hashArr)
{
int index = (key+i)%capacity;
if(hashArr[index].status==0)//if this next candidate index is free, then send its index number or check for the next one
return index;
return getNextCandidateIndex(key, ++i, hashArr);
}
void insert_helper(int const key, v const value, HashItem<v>* arr)
{
int index = key%capacity;
if (arr[index].status==2)//if occupied
index = getNextCandidateIndex(key, 1, arr);
arr[index].key=key;
arr[index].value=value;
arr[index].status=2;//occupied
cout<<"index: "<<index<<" ,key: "<<key<<" ,value: "<<value<<endl;
}
public:
HashMap()
{
capacity = 7;//prime number
currentElements = 0;
hashArray = new HashItem<v> [capacity] ();
}
HashMap(int const _capacity)
{
assert(capacity>1);
capacity = _capacity;
currentElements = 0;
hashArray = new HashItem<v> [capacity] ();
}
HashItem<v>* getHashArray()
{
return this->hashArray;
}
void insert(int const key, v const value, HashItem<v>* hashArr)
{
if( ( ((float)(currentElements)) / ((float)(capacity)) ) >= 0.75 )//load factor
doubleCapacity();
insert_helper(key, value, hashArr);
currentElements++;
}
bool deleteKey(int const key) const
{
for(int i=0; i<this->capacity; i++)
if(hashArray[i].key == key)
{
hashArray[i].status = 1;//key deleted
cout<<key<<" deleted.\n";
return true;
}
cout<<"key not found.\n";
return false;
}
v* get(int const key) const
{
for(int i=0; i<this->capacity; i++)
if(hashArray[i].key == key)
{
cout<<"key found at index"<<i<<endl;
return (&hashArray[i].value);
}
cout<<"key not found.\n";
return nullptr;
}
~HashMap()
{
if(hashArray!=nullptr)
hashArray=nullptr;
delete hashArray;
}
};
template<class v>
class QHashMap : public HashMap<v>
{
public:
int getNextCandidateIndex(int key, int i, HashItem<v>* hashArr)
{
// int _key = (key + (i*i));
// if(_key<0)
// _key+=this->capacity;
int index = (key + (i*i)) % this->capacity;
if(hashArr[index].status==0)//if free
return index;
return getNextCandidateIndex(key, ++i, hashArr);
}
};
template<class v>
class DHashMap : public HashMap<v>
{
public:
int getNextCandidateIndex(int key, int i, HashItem<v>* hashArr)
{
int first_value = key % this->capacity;
int second_value = 7 - (key % 7);
int candidate_index = (first_value + (i*second_value)) % this->capacity;
if(hashArr[candidate_index].status==0)//if free
return candidate_index;
return getNextCandidateIndex(key, ++i, hashArr);
}
};
void populateHash(string filename, HashMap<string> *hash)
{
ifstream fin;
fin.open(filename);
int Tsize;
fin>>Tsize;
fin.ignore();
while(Tsize>0)
{
string name;
getline(fin, name);
int att = name.find(' ');
string _key = name.substr(0, att);
//int id = stoi(_key, nullptr, 10);
stringstream id(_key);
int ID=0;
id>>ID;
name=name.substr(att+1, name.size());
hash->insert(ID, name, hash->getHashArray());
Tsize--;
}
}
int main()
{
cout<<"LINEAR PROBING\n";
HashMap<string> *map=new HashMap<string>;
populateHash("students.txt", map);
cout<<"Calling get function:\t"<<*map->get(9)<<endl<<endl;
cout<<"Calling deleteKey function:\t";map->deleteKey(9);cout<<endl;
//assert((map->get(9)==nullptr));
free (map);
cout<<"QUADRATIC PROBING\n";
QHashMap<string> *Qmap=new QHashMap<string>;
populateHash("students.txt", Qmap);
cout<<"Calling get function:\t"<<*Qmap->get(9)<<endl<<endl;
cout<<"Calling deleteKey function:\t";Qmap->deleteKey(9);cout<<endl;
//assert(Qmap->get(9)==nullptr);
free (Qmap);
cout<<"DOUBLE HASHING\n";
DHashMap<string> *Dmap=new DHashMap<string>;
populateHash("students.txt", Dmap);
cout<<"Calling get function:\t"<<*Dmap->get(9)<<endl<<endl;
cout<<"Calling deleteKey function:\t";Dmap->deleteKey(9);cout<<endl;
//assert(Dmap->get(9)==nullptr);
free (Dmap);
return 0;
}