-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash.cpp
214 lines (199 loc) · 6.08 KB
/
Hash.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
204
205
206
207
208
209
210
211
212
213
214
#pragma once
#include <cstring>
#include <iostream>
#include "datatype.hpp"
//#include "binary_Heap.cpp"
#include "tempfile.cpp"
using namespace std;
class Hash_map
{
public:
LinkedList *l1;
int size = 5; //used in case more categories are added to the heap(to increase size)
Heap *hashPtr; //for udemy courses
Heap *hashPtr2; //for coursera courses
bool is_Occupied[100];
Hash_map()
{
hashPtr = new Heap[size];
hashPtr2 = new Heap[100];
is_Occupied[100]={false};
}
int HashFunc(string str) //Hash function for udemy data set
{
if (str.compare("business finance") == 0)
return 1;
else if (str.compare("graphic design") == 0)
return 2;
else if (str.compare("musical instruments") == 0)
return 3;
else if (str.compare("web development") == 0)
return 4;
else
return 5;
}
int HashFuncC(string str) //Hash function for coursera
{
int ascii1 = 0;
int ascii2 = 0;
int ascii3 = 0;
int length = 0;
int hashval;
char c;
size_t i = 0;
ascii1 = (int)str[0];
ascii2 = (int)str[1];
ascii3 = (int)str[2];
hashval = ((ascii1*ascii1*ascii1)+(ascii2*ascii2)+(ascii3))%100;//hashing algorithm
if(!is_Occupied[hashval])//for collisions
return hashval;
while(!is_Occupied[hashval]==true)//quadratic hashing
{
i++;
hashval=hashval + (i*i);
}
return hashval;
}
void inserthash(int index, Course *obj) //to be used when adding new hash maps;
{
hashPtr[index - 1].insert(obj, obj->data.rating);
}
void inserthash2(int index, Course *obj) //to be used when adding new hash maps;
{
hashPtr2[index].insert(obj, obj->data.rating);
}
void searchMap(string str) //searchesudemy courses
{
int index = HashFunc(str);
LinkedList *newList = new LinkedList();
hashPtr[index - 1].returnList(newList);
cout << "Display first how many results?(Press 0 if you wish to display all)" << endl;
int count = 0;
cin >> count;
newList->printList(count, 0);
}
void searchMapC(string str) //searches coursera courses
{
string str1;
int index = HashFuncC(str);
if (! hashPtr2[index].is_empty() )
str1 = hashPtr2[index].root->node->data.tags[0];
int len = str.length();
str1 = str1.substr(0,len);
transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
if (str != str1)
{
cout << "No results found. Please refine your query." << endl;
return;
}
LinkedList *newList = new LinkedList();
if (! hashPtr2[index].is_empty() )
hashPtr2[index].returnList(newList);
//cout << "Display first how many results?(Press 0 if you wish to display all)" << endl;
int count = 2;
//cin >> count;
newList->printList(count, 0);
}
};
/*
#pragma once
#include <cstring>
#include <iostream>
#include "datatype.hpp"
//#include "binary_Heap.cpp"
#include "tempfile.cpp"
using namespace std;
class Hash_map
{
public:
LinkedList *l1;
int size = 5; //used in case more categories are added to the heap(to increase size)
Heap *hashPtr; //for udemy courses
Heap *hashPtr2; //for coursera courses
Heap* temp;
bool is_occupied[100]={false};
Hash_map()
{
hashPtr2 = new Heap[100];
}
int HashFunc(string str) //Hash function for udemy data set
{
if (str.compare("business finance") == 0)
return 1;
else if (str.compare("graphic design") == 0)
return 2;
else if (str.compare("musical instruments") == 0)
return 3;
else if (str.compare("web development") == 0)
return 4;
else
return 5;
}
int HashFuncC(string str) //Hash function for coursera
{
int ascii1 = 0;
int ascii2 = 0;
int ascii3 = 0;
int length = 0;
int hashval;
char c;
size_t i = 0;
ascii1 = (int)str[0];
ascii2 = (int)str[1];
ascii3 = (int)str[2];
hashval = ((ascii1*ascii1*ascii1)+(ascii2*ascii2*ascii2)+(ascii3*ascii3*ascii3))%100;
cout<<"Hello"<<endl;
if(!is_occupied[hashval])
return hashval;
while(!is_occupied[hashval]==true)
{
i++;
hashval=hashval + (i*i);
}
return hashval;
}
void inserthash(int index, Course *obj) //to be used when adding new hash maps;
{
hashPtr[index - 1].insert(obj, obj->data.rating);
}
void inserthash2(int index, Course *obj) //to be used when adding new hash maps;
{
cout<<"Hello1"<<endl;
hashPtr2[index].insert(obj,obj->data.rating);
}
void searchMap(string str) //searchesudemy courses
{
int index = HashFunc(str);
LinkedList *newList = new LinkedList();
hashPtr[index - 1].returnList(newList);
cout << "Display first how many results?(Press 0 if you wish to display all)" << endl;
int count = 0;
cin >> count;
newList->printList(count, 0);
}
void searchMapC(string str) //searches coursera courses
{
int index = HashFuncC(str);
string str1 = hashPtr2[index].root->node->data.tags[0];
size_t i = 0;
for (; i < str.length(); i++)
{
if(isspace(str[i]))
break;
}
str1 = str1.substr(0,i);
transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
if (str != str1)
{
cout << "No results found. Please refine your query." << endl;
return;
}
LinkedList *newList = new LinkedList();
hashPtr2[index].returnList(newList);
cout << "Display first how many results?(Press 0 if you wish to display all)" << endl;
int count = 0;
cin >> count;
newList->printList(count, 0);
}
};
*/