-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashMap.cpp
More file actions
78 lines (72 loc) · 2.21 KB
/
hashMap.cpp
File metadata and controls
78 lines (72 loc) · 2.21 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
#include "hashMap.h"
// Time Complexity: O(1)
int hashMap::hash(std::string &key)
{
return key.length() % capacity;
}
// Time Complexity: O(n) where n is the number of keys in the bucket
bool hashMap::search(std::string key)
{
int bucketIndex = hash(key);
for(auto element:map[bucketIndex])
{
if(element.first == key)
{
return true;
}
}
return false;
}
// Time Complexity: O(1);
void hashMap::insert(std::string key, std::vector<std::string> values)
{
if(size >= capacity * 0.75)
{
resizeAndRehash(capacity * 2);
}
int bucketIndex = hash(key);
if(search(key))
{
return;
}
std::pair<std::string, std::vector<std::string>> pair(key, values);
map[bucketIndex].push_back(pair);
size++;
}
// Time Complexity: O(n * m) where n is the number of elements in the bucket and m is the number of
// keys in each bucket.
void hashMap::resizeAndRehash(int newCapacity)
{
std::vector<std::vector<std::pair<std::string, std::vector<std::string>>>> newMap(newCapacity);
for(auto bucket:map)
{
for(auto key:bucket)
{
int newBucketIndex = hash(key.first);
std::pair<std::string, std::vector<std::string>> pair(key.first, key.second);
newMap[newBucketIndex].push_back(pair);
}
}
map = newMap;
capacity = newCapacity;
}
// Time Complexity: O(n*m) where n is the number of buckets and m the number of keys
// because each bucket's keys are being searched to find possible matches for the criteria.
std::vector<std::pair<std::string, std::vector<std::string>>> hashMap::compare(std::string rating, std::string year, std::string duration)
{
std::vector<std::pair<std::string, std::vector<std::string>>> results;
for(auto it:map)
{
for(auto bit:it)
{
if(bit.second[0] != "" && bit.second[2] != "")
{
if(std::stod(bit.second[0]) >= std::stod(rating) && bit.second[1] >= year && bit.second[2] <= duration)
{
results.push_back(bit);
}
}
}
}
return results;
}