-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashmap.h
126 lines (108 loc) · 3.32 KB
/
hashmap.h
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
//
// Created by hunor on 11/29/2023.
//
#ifndef HASH_HASHMAP_H
#define HASH_HASHMAP_H
#include <string>
#include <iostream>
#include <vector>
#include <utility>
class hashmap
{
private:
//things to store
std::vector<std::pair<std::string, std::string>> table;
int size;
double loadFactor;
int resizeThreshold;
//private functions :3
unsigned int hashFunction(const std::string& key, int table_size);
unsigned int findNextEmptySlot(unsigned int index);
void resize();
void checkResize();
public:
explicit hashmap(int initialSize = 100, double initialLoadFactor = 0.7)
: table(initialSize, std::make_pair("","")), size(0), loadFactor(initialLoadFactor),
resizeThreshold(static_cast<int>(initialLoadFactor * initialSize)) {}
void insert(const std::string& key, const std::string& value);
std::string search(const std::string& key);
void remove(const std::string& key);
};
unsigned int hashmap::hashFunction(const std::string &key, int table_size)
{
unsigned int hash = 0;
for (char ch : key) {
hash = (hash * 11 + ch) % table_size;
}
return hash;
}
unsigned int hashmap::findNextEmptySlot(unsigned int index)
{
//Checks if current index isnt used or deleted and if used, +1
while (!table[index].first.empty()) {
index = (index + 1) % table.size();
}
return index;
}
void hashmap::resize()
{
//new vector of 2*size (empty)
int newSize = table.size() * 2;
std::vector<std::pair<std::string, std::string>> newTable(newSize, std::make_pair("", ""));
//goes through prev table and rehashes
for (const auto& node : table) {
if (!node.first.empty()) {
unsigned int index = hashFunction(node.first, newSize);
while (!newTable[index].first.empty()) {
index = (index + 1) % newTable.size();
}
newTable[index].first = node.first;
newTable[index].second = node.second;
}
}
//cppreference told me to use
table = std::move(newTable);
resizeThreshold = static_cast<int>(loadFactor * table.size());
}
void hashmap::checkResize()
{
//wrapper for resize()
//adds check
if (size >= resizeThreshold) {
resize();
}
}
void hashmap::insert(const std::string &key, const std::string &value)
{
checkResize();
unsigned int index = hashFunction(key, table.size());
index = findNextEmptySlot(index);
table[index].first = key;
table[index].second = value;
size++;
}
std::string hashmap::search(const std::string &key)
{
unsigned int index = hashFunction(key, table.size());
while (!table[index].first.empty()) {
if (table[index].first == key) {
return table[index].second;
}
index = (index + 1) % table.size();
}
return "Key not found";
}
void hashmap::remove(const std::string &key)
{
unsigned int index = hashFunction(key, table.size());
while (!table[index].first.empty()) {
if (table[index].first == key) {
table[index].first = "";
table[index].second = "";
size--;
return;
}
index = (index + 1) % table.size();
}
}
#endif //HASH_HASHMAP_H