-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_table_search_step.cpp
More file actions
236 lines (196 loc) · 6.88 KB
/
Copy pathhash_table_search_step.cpp
File metadata and controls
236 lines (196 loc) · 6.88 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// # *********************************************************
// Program: hash_table_search_step.cpp
// Course: CCP6214 Algorithm Design and Analysis
// Lecture Class: TC2L
// Tutorial Class: TT5L
// Trimester: 2610
// Member_1: HEW WEE BO | hew.wee.bo@student.mmu.edu.my | 0128803121
// Member_2: ID | JEVAANRAJ A/L RAJA KUMARAN | jevaanraj.raja.kumaran@student.mmu.edu.my | 0179651973
// Member_3: ID | SHANJIF CAKRAVRTHI A/L KUPPAN @ SIVA KUMAR | shanjif.cakravthi@student.mmu.edu.my | 0195601010
// Member_4: ID | TEH ZHAO JIN | teh.zhao.jin@student.mmu.edu.my | 01111279290
// # *********************************************************
// Task Distribution
// Member_1: Hew Wee Bo
// Member_2: Jevaanraj
// Member_3: Shanjif
// Member_4: Teh Zhao Jin
// # *********************************************************
/* Purpose:
Reads a dataset CSv, inserts all records into a hash table
using separate chaining (linked list), then searches for
a specified target key and logs every step of the search process
to an output file */
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
static int choosePrimeTableSize(int minSize) {
if (minSize < 2) return 2;
int candidate = (minSize % 2 == 0) ? minSize + 1 : minSize;
while (true) {
bool isPrime = true;
for (int i = 2; (long long)i * i <= candidate; ++i) {
if (candidate % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) return candidate;
candidate += 2;
}
}
struct Record {
long long key; // 10-digit unique int
string value; // 5-letter lowercase string
};
struct Node {
Record data;
Node* next;
};
static string makeReportFilename(const string& datasetSizeStr) {
return "dataset_" + datasetSizeStr + "_hash_table_search_step.txt";
}
class HashTable {
private:
int tableSize;
vector<Node*> table;
public:
HashTable(int size) : tableSize(size), table(size, nullptr) {}
~HashTable() {
for (int i = 0; i < tableSize; ++i) {
Node* current = table[i];
while (current) {
Node* temp = current;
current = current->next;
delete temp;
}
}
}
int hashFunction(long long key) const{
return (int)((unsigned long long)key% (unsigned long long)tableSize);
}
void insert(const Record& rec) {
int idx = hashFunction(rec.key);
Node* newNode = new Node();
newNode->data = rec;
newNode->next = table[idx];
table[idx] = newNode;
}
bool searchWithSteps(long long targetKey, ofstream& out) const {
int idx = hashFunction(targetKey);
out << "Searching for target: " << targetKey << "\n";
out << "Hash bucket index : " << idx << "\n";
out << "-------------------------------------------\n";
Node* curr = table[idx];
int compareCount = 0;
while (curr != nullptr) {
compareCount++;
out << "Comparison " << compareCount
<< ": comparing with " << curr->data.key
<< "/" << curr->data.value;
if (curr->data.key == targetKey) {
out << " --> MATCH\n";
out << "-------------------------------------------\n";
out << targetKey << " = "
<< curr->data.key << "/" << curr->data.value << "\n";
return true;
} else {
out << " (no match)\n";
}
curr = curr->next;
}
out << "-------------------------------------------\n";
out << "-1 != " << targetKey << "\n";
return false;
}
};
vector<Record> parseCSV(const string& filename, ostream* report = nullptr) {
vector<Record> records;
ifstream inFile(filename);
if (!inFile.is_open()) {
if (report) {
*report << "Error opening file: " << filename << "\n";
}
return records;
}
string line;
while (getline(inFile, line)) {
if (line.empty()) continue;
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
stringstream ss(line);
string keyStr, valueStr;
if (getline(ss, keyStr, ',') && getline(ss, valueStr)) {
try {
Record rec;
rec.key = stoll(keyStr);
rec.value = valueStr;
records.push_back(rec);
}catch (...){
if (report) {
*report << "Error parsing line: " << line << "\n";
}
}
}
}
inFile.close();
return records;
}
string extractDatasetSize(const string& filename) {
size_t underPos = filename.rfind('_');
size_t dotPos = filename.rfind('.');
if (underPos != string::npos && dotPos != string::npos && underPos < dotPos) {
return filename.substr(underPos + 1, dotPos - underPos - 1);
}
return "unknown";
}
void runSearch(const HashTable& ht,
long long targetKey,
ofstream& outFile) {
outFile << "\n--- Searching for target: " << targetKey << " ---\n";
bool found = ht.searchWithSteps(targetKey, outFile);
if (found) {
outFile << "Result: FOUND (" << targetKey << " = " << targetKey << ")\n";
}
else {
outFile << "Result: NOT FOUND (" << targetKey << " != -1)\n";
}
}
int main() {
// Comment/uncomment lines to choose which datasets to run.
// Multiple uncommented lines will run one after another.
string datasetFile = "dataset_1000.csv";
//string datasetFile = "dataset_10000.csv";
//string datasetFile = "dataset_100000.csv";
string datasetSizeStr = extractDatasetSize(datasetFile);
string reportFilename = makeReportFilename(datasetSizeStr);
ofstream outFile(reportFilename);
if (!outFile.is_open()) {
return 1;
}
outFile << "Reading dataset from: " << datasetFile << " ...\n";
cout << "Reading dataset from: " << datasetFile << endl;
vector<Record> records = parseCSV(datasetFile, &outFile);
if (records.empty()) {
outFile << "ERROR: No records loaded. Check the file path and format.\n";
return 1;
}
outFile << "Loaded " << records.size() << " records.\n";
int tableSize = choosePrimeTableSize((int)records.size() * 2);
outFile << "Building hash table with " << tableSize << " buckets ...\n";
HashTable ht(tableSize);
for (const Record& rec : records) {
ht.insert(rec);
}
outFile << "Hash table built successfully.\n";
long long TARGET_FOUND = records.front().key;
long long TARGET_NOT_FOUND = -1;
runSearch(ht, TARGET_FOUND, outFile);
runSearch(ht, TARGET_NOT_FOUND, outFile);
outFile << "\nDone.\n";
cout << "Results written to file: " << reportFilename << "\n";
return 0;
}