-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringset.cpp
More file actions
97 lines (84 loc) · 2.51 KB
/
Copy pathstringset.cpp
File metadata and controls
97 lines (84 loc) · 2.51 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
/*
* Name: Lucas Boyer
* Date Submitted: 10-10-21
* Lab Section: 002
* Assignment Name: Spell Checker Using a Hash Table
*/
#include "stringset.h"
#include <algorithm>
using namespace std;
//helper function that takes in word and length of vector table
//hashes the word mod size of vector and returns its position to be inserted
int hashString(const string word, const int tSize) {
hash<string> stringHash;
int hashed = stringHash(word) % tSize;
return hashed;
}
Stringset::Stringset() : table(4), num_elems(0), size(4) {}
//Used in test cases and testStringset() in main.cpp, do not modify
vector<list<string>> Stringset::getTable() const
{
return table;
}
//Used in test cases and testStringset() in main.cpp, do not modify
int Stringset::getNumElems() const
{
return num_elems;
}
//Used in test cases and testStringset() in main.cpp, do not modify
int Stringset::getSize() const
{
return size;
}
//inserts string into list after hashing it
//if num_elems equals size it increases the size of the vector by size * 2
void Stringset::insert(string word)
{
hash<string> stringHash;
//if old table is full, make new one and rehash data
if (num_elems == size) {
size *= 2;
vector<list<string>> temp(size); //makes temp table
for (int i = 0; i < size / 2; i++) {
for (auto itr : table[i]) {
int hashed = stringHash(itr) % size; //rehashes everything in old table
temp[hashed].push_back(itr); //adds it to temp table
}
}
table.clear(); //clears old table
table = temp; //puts temp in table
temp.clear(); //clears temp
}
int hashed = hashString(word, size);
//if not already in list add it
if (!find(word)) {
table[hashed].push_front(word);
num_elems++;
}
}
//This function will return a Boolean Will return true if the word is in the list, otherwise it will return false
//searches list by using hash function
bool Stringset::find(string word) const {
int hashed = hashString(word, size);
for (auto iter : table[hashed]) {
if (iter == word) {
return true;
}
}
return false;
}
//removes word if in list
//uses hash function to find
void Stringset::remove(string word)
{
int hashed = hashString(word, size);
if(find(word)) {
for (auto iter : table[hashed]) {
if (iter == word) {
table[hashed].remove(iter);
num_elems--;
break;
}
}
}
}