-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.cpp
More file actions
137 lines (124 loc) · 3.28 KB
/
Copy pathhash.cpp
File metadata and controls
137 lines (124 loc) · 3.28 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
// CPP program to implement hashing with chaining
#include <iostream>
#include "hash.hpp"
#include <sstream>
using namespace std;
node* HashTable::createNode(string restaurantName, node* next)
{
node *new_node = new node;
new_node->pq = new PriorityQ(50);
new_node->next = next;
new_node->restaurantName = restaurantName;
return new_node;
}
HashTable::HashTable(int bsize)
{
numCollision = 0;
tableSize = bsize;
table = new node*[tableSize];
for (int i = 0; i < bsize; i++)
table[i] = nullptr;
}
HashTable::~HashTable()
{
node *temp = nullptr;
node *next = nullptr;
for(int i = 0; i < tableSize; i++)
{
temp = table[i];
while(temp != nullptr)
{
next = temp->next;
delete temp->pq;
delete temp;
temp = next;
}
}
delete[] table;
}
void HashTable::displayTable()
{
node *temp = nullptr;
for(int i = 0; i < tableSize; i++)
{
cout << i << "|";
temp = table[i];
while(temp)
{
cout << temp->restaurantName << "-->";
temp = temp->next;
}
cout << "NULL" << endl;
}
}
unsigned int HashTable::hashFunction(string restaurantName)
{
// will return 0,1,2,3...
// add up the letters and then take result and mode the table size
int char_total = 0;
for(int i = 0; i < restaurantName.length(); i++)
{
char_total = char_total + restaurantName[i];
}
return (char_total % tableSize);
}
node* HashTable::searchItem(string restaurantName)
{
// look for the resturan in the hash table
// if doesnt exist return null
// if exists return a pointer to that memory location
int hash_val = hashFunction(restaurantName);
node *temp = table[hash_val];
while(temp != nullptr)
{
if(temp->restaurantName == restaurantName)
{
return temp;
}
temp = temp->next;
}
return temp;
}
void HashTable::insertItem(ReviewInfo restaurant)
{
// searchItem() see if it is in the hash table/linked list
// if !found
// create a node
//node new node[name]
//also creat a pq of size 50
node *item = searchItem(restaurant.restaurantName);
if(item == nullptr)
{
int hash_val = hashFunction(restaurant.restaurantName);
node *new_resturant = createNode(restaurant.restaurantName, table[hash_val]);
table[hash_val] = new_resturant;
if(table[hash_val]->next != nullptr)
{
numCollision++;
}
item = new_resturant;
}
item->pq->insertElement(restaurant);
}
void HashTable::setup(string fname)
{
// read from the file
// sstream
// searchItem and pass the resturan name
// reviewInfo istances for each item.
// reviewInfo r;
ifstream file(fname);
string line, resturant_name, review, reviwer_name, time;
int time_int, index = 0;
while(getline(file, line))
{
stringstream ss(line);
getline(ss, resturant_name, ';');
getline(ss, review, ';');
getline(ss, reviwer_name, ';');
getline(ss, time, ';');
ReviewInfo review_info{resturant_name, review, reviwer_name, stoi(time)};
insertItem(review_info);
index++;
}
}