-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.cpp
More file actions
168 lines (144 loc) · 3.78 KB
/
map.cpp
File metadata and controls
168 lines (144 loc) · 3.78 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
#include <vector>
#include <utility>
#include <stdexcept>
#include <string>
using namespace std;
template <typename K, typename V>
class map
{
private:
struct Node
{
K key;
V value;
Node(const K &k, const V &v) : key(k), value(v) {}
};
vector<vector<Node>> buckets;
size_t currentSize;
double maxLoadFactor;
size_t customHash(const string &key) const
{
const size_t p = 31;
const size_t m = 1000000009ULL;
size_t hashVal = 0;
size_t p_pow = 1;
for (char c : key)
{
hashVal = (hashVal + (c - 'a' + 1) * p_pow) % m;
p_pow = (p_pow * p) % m;
}
return hashVal;
}
size_t customHash(long long key) const
{
return (size_t)(key * 2654435761ULL);
}
size_t getBucketIndex(const K &key) const
{
if constexpr (is_same<K, string>::value)
return customHash(key) % buckets.size();
else if constexpr (is_integral<K>::value)
return customHash((long long)key) % buckets.size();
else
throw runtime_error("Unsupported key type");
}
void rehash(size_t newBucketCount)
{
vector<vector<Node>> newBuckets(newBucketCount);
for (auto &bucket : buckets)
{
for (auto &node : bucket)
{
size_t newIndex = getBucketIndex(node.key);
newBuckets[newIndex].push_back(node);
}
}
buckets.swap(newBuckets);
}
public:
map(size_t initBucketCount = 8, double loadFactor = 0.75)
: buckets(initBucketCount), currentSize(0), maxLoadFactor(loadFactor) {}
bool count(const K &key) const
{
size_t idx = getBucketIndex(key);
for (const auto &node : buckets[idx])
{
if (node.key == key)
return true;
}
return false;
}
V &operator[](const K &key)
{
size_t idx = getBucketIndex(key);
for (auto &node : buckets[idx])
{
if (node.key == key)
return node.value;
}
if ((double)(currentSize + 1) / buckets.size() > maxLoadFactor)
{
rehash(buckets.size() * 2);
idx = getBucketIndex(key);
}
buckets[idx].emplace_back(key, V{});
currentSize++;
return buckets[idx].back().value;
}
void insert(const K &key, const V &value)
{
size_t idx = getBucketIndex(key);
for (auto &node : buckets[idx])
{
if (node.key == key)
{
node.value = value;
return;
}
}
if ((double)(currentSize + 1) / buckets.size() > maxLoadFactor)
{
rehash(buckets.size() * 2);
idx = getBucketIndex(key);
}
buckets[idx].emplace_back(key, value);
currentSize++;
}
void erase(const K &key)
{
size_t idx = getBucketIndex(key);
auto &bucket = buckets[idx];
for (size_t i = 0; i < bucket.size(); i++)
{
if (bucket[i].key == key)
{
bucket[i] = bucket.back(); // swap with last
bucket.pop_back(); // remove last
currentSize--;
return;
}
}
}
size_t size() const
{
return currentSize;
}
bool empty() const
{
return currentSize == 0;
}
vector<pair<K, V>> items() const
{
vector<pair<K, V>> all;
all.reserve(currentSize);
for (const auto &bucket : buckets)
{
for (const auto &node : bucket)
{
all.push_back({node.key, node.value});
}
}
return all;
}
};