-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.cpp
43 lines (34 loc) · 999 Bytes
/
test.cpp
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
// https://aozturk.medium.com/simple-hash-map-hash-table-implementation-in-c-931965904250
/*
Compilation
$ g++ -I$(pwd) test.cpp
Memory leak check
$ valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose ./a.out
$ valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose ./a.out
*/
#include "hashmap.h"
#include <string>
#include <cstddef>
struct StringKeyHash {
unsigned long operator()(const std::string& key, size_t tableSize) const
{
unsigned long hash = 5381;
for (auto ch : key) {
hash = ((hash << 5) + hash) + static_cast<int>(ch);
}
return hash % tableSize;
}
};
int main()
{
HashMap<std::string, std::string, 1024, StringKeyHash> map;
map.put("abc", "alice");
map.put("xyz", "bob");
map.put("mno", "charlie");
map.put("pqr", "eve");
map.remove("pqr");
std::string value;
map.get("abc", value);
std::cout << value << "\n";
return 0;
}