forked from bishyboi/DSAProj_3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap.cpp
More file actions
141 lines (127 loc) · 3.3 KB
/
HashMap.cpp
File metadata and controls
141 lines (127 loc) · 3.3 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
#include <cmath>
#include "HashMap.h"
HashMap::HashMap(int cap)
{
capacity = cap;
size = 0;
container.resize(cap);
}
// hash function of the structure
int HashMap::hash(std::string language)
{
int hash = 0;
// string folding length two
for (int i = 0; i < language.size(); i += 2)
{
if (i == language.size() - 1)
{
hash += int(language[i]);
}
else
{
hash += std::stoi(std::to_string(int(language[i])) + std::to_string(int(language[i + 1])));
}
}
hash = hash % capacity;
return hash;
}
void HashMap::insert(song newSong)
{
// rehash if out of space
int hashNum;
if (size == capacity)
{
std::vector<std::vector<song>> temp = container;
container.clear();
capacity *= 2;
container.resize(capacity);
// reinsert all data back into new hash map
for (int i = 0; i < temp.size(); i++)
{
if (!temp[i].empty())
{
hashNum = hash(temp[i][0].language);
container[hashNum] = temp[i];
}
}
}
hashNum = hash(newSong.language);
bool placed = false;
while (!placed)
{
if (container[hashNum].empty())
{
container[hashNum].push_back(newSong);
placed = true;
}
else if (container[hashNum][0].language == newSong.language)
{
container[hashNum].push_back(newSong);
placed = true;
}
else
{
// linear collision policy
hashNum++;
hashNum = hashNum % capacity;
}
}
size++;
}
std::vector<song> *HashMap::search(std::string language)
{
int hashNum = hash(language);
while (true)
{
if (container[hashNum].empty())
{
// collision policy says doesnt exist
return nullptr;
}
if (container[hashNum][0].language == language)
{
return &container[hashNum];
}
else
{
// linear collision policy
hashNum++;
hashNum = hashNum % capacity;
}
}
}
std::tuple<std::vector<double>, std::string> HashMap::Algo(std::string language)
{
// start timer
auto start = std::chrono::high_resolution_clock::now();
// pull out the list of that language
std::vector<song> *langList = search(language);
// create data holders
std::vector<double> result;
result.resize((END_YEAR - START_YEAR) + 1);
std::vector<int> tally;
tally.resize((END_YEAR - START_YEAR) + 1);
// add the data onto the lists
for (int i = 0; i < langList->size(); i++)
{
int index = (*langList)[i].year - START_YEAR;
result[index] += (*langList)[i].ExpLyrics;
tally[index]++;
}
// average the data for each year and round to int
for (int i = 0; i < result.size(); i++)
{
if (tally[i] == 0)
{
result[i] = -1;
}
else
{
result[i] = result[i] / tally[i];
}
}
// stop timer
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
return std::make_tuple(result, std::to_string(duration.count()));
}