-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
79 lines (65 loc) · 2.21 KB
/
Copy pathtest.cpp
File metadata and controls
79 lines (65 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
79
#include <iostream>
#include <cstring>
#include "hashtable.h"
#include "murmurhash.h"
// Example Key-Value Node (inherits from HNode)
struct KVNode {
HNode node; // required for hashtable
std::string key; // use std::string for convenience
std::string val;
};
// Equality function for keys
bool kv_eq(HNode *a, HNode *b) {
auto *ka = reinterpret_cast<KVNode*>(a);
auto *kb = reinterpret_cast<KVNode*>(b);
return ka->key == kb->key;
}
// Helper to create a KVNode
KVNode* make_kv(const std::string &key, const std::string &val) {
KVNode *kv = new KVNode();
kv->key = key;
kv->val = val;
kv->node.hashed_key = murmurhash64(key.c_str(), key.size(), 0x1234);
kv->node.next = nullptr;
return kv;
}
// Print helper
void print_lookup(HMap &map, const std::string &key) {
KVNode temp;
temp.key = key;
temp.node.hashed_key = murmurhash64(key.c_str(), key.size(), 0x1234);
HNode *found = hm_lookup(&map, &temp.node, kv_eq);
if (found) {
auto *kv = reinterpret_cast<KVNode*>(found);
std::cout << "Found: " << kv->key << " => " << kv->val << "\n";
} else {
std::cout << "Key \"" << key << "\" not found.\n";
}
}
int main() {
HMap map{}; // initialize empty
// Insert key-value pairs
hm_insert(&map, &make_kv("apple", "red")->node, kv_eq);
hm_insert(&map, &make_kv("banana", "yellow")->node, kv_eq);
hm_insert(&map, &make_kv("grape", "purple")->node, kv_eq);
// Lookup some keys
print_lookup(map, "apple");
print_lookup(map, "banana");
print_lookup(map, "orange"); // doesn't exist
// Delete a key
KVNode temp;
temp.key = "banana";
temp.node.hashed_key = murmurhash64("banana", 6, 0x1234);
HNode *deleted = hm_delete(&map, &temp.node, kv_eq);
if (deleted) {
auto *kv = reinterpret_cast<KVNode*>(deleted);
std::cout << "Deleted: " << kv->key << " => " << kv->val << "\n";
delete kv; // free memory
}
// Lookup after deletion
print_lookup(map, "banana");
// Cleanup: clear all remaining nodes manually
// (in a real program you'd track them in a list or custom allocator)
// For this test, we skip full cleanup since the process ends.
return 0;
}