-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasics.cpp
180 lines (167 loc) · 4.09 KB
/
basics.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
#include <bits/stdc++.h>
using namespace std;
//Implement a Hashmap
//Separate Chaining Technique
template <typename T>
class Node
{
public:
string key;
T value;
Node<T> *next;
Node(string key, T val)
{
this->key = key;
value = val;
next = NULL;
}
~Node()
{
if (next != NULL)
{
delete next;
}
}
};
template <typename T>
class HashTable
{
Node<T> **table; //pointer to an array of pointers
int current_size;
int table_size;
int hashfn(string key)
{
int idx = 0;
int p = 1;
for (int j = 0; j < key.length(); j++)
{
idx = idx + (key[j] * p) % table_size;
idx = idx % table_size;
p = (p * 27) % table_size;
}
return idx;
}
void rehash()
{
Node<T> **oldtable = table;
int oldtablesize = table_size;
table_size = 2 * table_size;
table = new Node<T> *[table_size];
// Setting NULL to all values of the new table
for (int i = 0; i < table_size; i++)
{
table[i] = NULL;
}
current_size = 0;
//transferring from old table to new table
for (int i = 0; i < oldtablesize; i++)
{
Node<T> *temp = oldtable[i];
while (temp != NULL)
{
insert(temp->key, temp->value);
temp = temp->next;
}
if (oldtable[i] != NULL)
{
delete oldtable[i];
}
}
delete[] oldtable;
}
public:
HashTable(int ts = 7)
{
table_size = ts;
table = new Node<T> *[table_size];
current_size = 0;
for (int i = 0; i < table_size; i++)
{
table[i] = NULL;
}
}
void insert(string key, T val)
{
int index = hashfn(key);
Node<T> *n = new Node<T>(key, val);
n->next = table[index];
table[index] = n;
current_size++;
//rehashing
float load_factor = current_size / (1.0 * table_size);
if (load_factor > 0.7)
{
rehash();
}
}
void print()
{
for (int i = 0; i < table_size; i++)
{
cout << "Bucket " << i << " ->";
Node<T> *temp = table[i];
;
while (temp != NULL)
{
cout << temp->key << " ->";
temp = temp->next;
}
cout << endl;
}
}
//Logic behind T* is that if we do not find that key then we return NULL
T *search(string key)
{
int idx = hashfn(key);
Node<T> *temp = table[idx];
while (temp != NULL)
{
if (temp->key == key)
{
return &temp->value;
}
temp = temp->next;
}
return NULL;
}
// The return type of this is set to T& because when we update the value we
//want it to reflect in the original as well
T &operator[](string key)
{
T *f = search(key);
if (f == NULL)
{
T garbage;
insert(key, garbage);
f = search(key);
}
return *f;
}
};
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
HashTable<int> price_menu;
price_menu.insert("Burger", 120);
price_menu.insert("Pepsi", 20);
price_menu.insert("Noodles", 60);
price_menu.insert("French Fries", 25);
price_menu.insert("Biryani", 95);
price_menu["Dosa"] = 60;
price_menu["Dosa"] += 10;
cout << "Price of dosa :" << price_menu["Dosa"] << endl;
price_menu.print();
int *price = price_menu.search("Pepsi");
if (price == NULL)
{
cout << "Item not found " << endl;
}
else
{
cout << "Price is: " << *price << endl;
}
return 0;
}