-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoubleHashMap.hpp
185 lines (158 loc) · 4.67 KB
/
DoubleHashMap.hpp
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
/*
* DoubleHashMap.hpp
*
* This is an interface for a hash table using Double Hashing.
*/
#ifndef DOUBLEHASHMAP_HPP_
#define DOUBLEHASHMAP_HPP_
#include "Dictionary.hpp"
#include "seqLinearList.hpp"
#include <iostream>
#define SIZE_DEFAULT 503
namespace cs202
{
template<class Key, class Value>
class DoubleHashMap : public Dictionary<Key,Value>
{
void rehash();
private:
LinearList< Node<Key, Value> > _list;
int size;
LinearList<int> status;
/*
* Function rehash:
* Resizes the has table to the next convenient size.
* Called when all the slots are full and a new element needs to be inserted.
*/
public:
/*
* Constructor: DoubleHashMap
* Creates a Double Hash Hash Table with some default size.
* NOTE: Please try to ensure that the size is a prime number for better performance.
*/
DoubleHashMap(){
_list.set_capacity(SIZE_DEFAULT);
status.set_capacity(SIZE_DEFAULT);
size = 0;
}
/*
* Constructor:DoubleHashMap
* Creates an empty Double Hash Map with the ability to hold atleast num Key value pairs.
*/
DoubleHashMap(const int& num);
/*
* Constructor: Double Hash Map
* Creates a new Hash Table which is the exact copy of the given hash table.
**/
DoubleHashMap(DoubleHashMap<Key, Value>& ht);
/*
* Destructor
* Deletes the memory acquired by the given Hash Map.
*/
~DoubleHashMap(){
}
/*
* A convenience wrapper operator.
* Returns a reference to the value corresponding to the given key.
* If the key does'nt exist, then inserts the key in the table,
* with the default value of the Value type.
* This should enable you to write code like this:
* DoubleHashMap<int,int> ht;
* ht[1] = 2;
* ht[1] = 4;
* ht[2] = 3;
*/
void print()
{
for(int i=0; i<SIZE_DEFAULT;++i)
if(status[i]==1)
cout<<"Index :"<< i <<" Key: "<<_list[i].get_key()<<" "<<"Value: "<<_list[i].get_value()<<endl;
}
Value& operator[](const Key& key)
{
int index = this -> hash(key, SIZE_DEFAULT);
int offset = this -> double_hash(index);
while(status[index] != 0) {
if(status[index]==2)
break;
if(status[index] == 1) {
if(_list[index].get_key() == key) {
return _list[index].ref_val();
}
}
index=index+offset;
index %= SIZE_DEFAULT;
}
status[index] = 1;
_list[index].set_pair(key, Value());
size++;
return _list[index].ref_val();
}
int get_size(){ //returns size of hash map
return size;
}
bool has(const Key& key){
int index = this->hash(key,SIZE_DEFAULT);
int offset = this -> double_hash(index);
while(status[index]!=0)
{
if(status[index]==1 &&( _list[index].get_key()==key))
return true;
index=index+offset;
index = index%SIZE_DEFAULT;
}
return false;
}
void remove(const Key& key)
{
int index = this->hash(key,SIZE_DEFAULT);
int offset = this -> double_hash(index);
while(status[index]!=0)
{
if(status[index]==1 && _list[index].get_key()==key)
{
status[index] = 2;
size--;
return;
}
index=index+offset;
index = index%SIZE_DEFAULT;
}
}
Value get(const Key& key) {
int index = this -> hash(key, SIZE_DEFAULT);
int offset = this -> double_hash(index);
while(status[index] != 0) {
if(status[index] == 1) {
if(_list[index].get_key() == key) {
return _list[index].get_value();
}
}
index=index+offset;
index %= SIZE_DEFAULT;
}
return Value();
}
void put(const Key& key, const Value& value)
{
int index = this -> hash(key, SIZE_DEFAULT);
int offset = this -> double_hash(index);
cout<<index<<endl;
Node<Key, Value> new_node;
new_node.set_pair(key,value);
while(status[index] == 1) {
if(_list[index].get_key() == key) {
_list[index].set_pair(key, value);
return;
}
index=index+offset;
index %= SIZE_DEFAULT;
}
_list.insert(new_node, index);
int a=1;
status.insert(a, index);
size++;
}
};
}
#endif