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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300<snippet>
<content><![CDATA[
O(1) hashmap
// https://nyaannyaan.github.io/library/hashmap/hashmap.hpp
namespace HashMapImpl {
using u32 = uint32_t;
using u64 = uint64_t;
template <typename Key, typename Data> struct HashMapBase;
template <typename Key, typename Data> struct itrB : iterator<bidirectional_iterator_tag, Data, ptrdiff_t, Data *, Data &> {
using base = iterator<bidirectional_iterator_tag, Data, ptrdiff_t, Data *, Data &>;
using ptr = typename base::pointer;
using ref = typename base::reference;
u32 i;
HashMapBase<Key, Data> *p;
explicit constexpr itrB() : i(0), p(nullptr) {}
explicit constexpr itrB(u32 _i, HashMapBase<Key, Data> *_p) : i(_i), p(_p) {}
explicit constexpr itrB(u32 _i, const HashMapBase<Key, Data> *_p) : i(_i), p(const_cast<HashMapBase<Key, Data> *>(_p)) {}
friend void swap(itrB &l, itrB &r) { swap(l.i, r.i), swap(l.p, r.p); }
friend bool operator==(const itrB &l, const itrB &r) { return l.i == r.i; }
friend bool operator!=(const itrB &l, const itrB &r) { return l.i != r.i; }
const ref operator*() const { return const_cast<const HashMapBase<Key, Data> *>(p)->data[i]; }
ref operator*() { return p->data[i]; }
ptr operator->() const { return &(p->data[i]); }
itrB &operator++() {
assert(i != p->cap && "itr::operator++()");
do {
i++;
if (i == p->cap) break;
if (p->flag[i] == true && p->dflag[i] == false) break;
} while (true);
return (*this);
}
itrB operator++(int) {
itrB it(*this);
++(*this);
return it;
}
itrB &operator--() {
do {
i--;
if (p->flag[i] == true && p->dflag[i] == false) break;
assert(i != 0 && "itr::operator--()");
} while (true);
return (*this);
}
itrB operator--(int) {
itrB it(*this);
--(*this);
return it;
}
};
template <typename Key, typename Data> struct HashMapBase {
using u32 = uint32_t;
using u64 = uint64_t;
using iterator = itrB<Key, Data>;
using itr = iterator;
protected:
template <typename K> inline u64 randomized(const K &key) const { return u64(key) ^ r; }
template <typename K, enable_if_t<is_same<K, Key>::value, nullptr_t> = nullptr, enable_if_t<is_integral<K>::value, nullptr_t> = nullptr>
inline u32 inner_hash(const K &key) const {
return (randomized(key) * 11995408973635179863ULL) >> shift;
}
template <typename K, enable_if_t<is_same<K, Key>::value, nullptr_t> = nullptr, enable_if_t<is_integral<decltype(K::first)>::value, nullptr_t> = nullptr,
enable_if_t<is_integral<decltype(K::second)>::value, nullptr_t> = nullptr>
inline u32 inner_hash(const K &key) const {
u64 a = randomized(key.first), b = randomized(key.second);
a *= 11995408973635179863ULL;
b *= 10150724397891781847ULL;
return (a + b) >> shift;
}
template <typename K, enable_if_t<is_same<K, Key>::value, nullptr_t> = nullptr,
enable_if_t<is_integral<typename K::value_type>::value, nullptr_t> = nullptr>
inline u32 inner_hash(const K &key) const {
static constexpr u64 mod = (1LL << 61) - 1;
static constexpr u64 base = 950699498548472943ULL;
u64 res = 0;
for (auto &elem : key) {
__uint128_t x = __uint128_t(res) * base + (randomized(elem) & mod);
res = (x & mod) + (x >> 61);
}
__uint128_t x = __uint128_t(res) * base;
res = (x & mod) + (x >> 61);
if (res >= mod) res -= mod;
return res >> (shift - 3);
}
template <typename D = Data, enable_if_t<is_same<D, Key>::value, nullptr_t> = nullptr> inline u32 hash(const D &dat) const { return inner_hash(dat); }
template <typename D = Data, enable_if_t<is_same<decltype(D::first), Key>::value, nullptr_t> = nullptr> inline u32 hash(const D &dat) const {
return inner_hash(dat.first);
}
template <typename D = Data, enable_if_t<is_same<D, Key>::value, nullptr_t> = nullptr> inline Key dtok(const D &dat) const { return dat; }
template <typename D = Data, enable_if_t<is_same<decltype(D::first), Key>::value, nullptr_t> = nullptr> inline Key dtok(const D &dat) const {
return dat.first;
}
void reallocate(u32 ncap) {
vector<Data> ndata(ncap);
vector<bool> nf(ncap);
shift = 64 - __lg(ncap);
for (u32 i = 0; i < cap; i++) {
if (flag[i] == true && dflag[i] == false) {
u32 h = hash(data[i]);
while (nf[h]) h = (h + 1) & (ncap - 1);
ndata[h] = move(data[i]);
nf[h] = true;
}
}
data.swap(ndata);
flag.swap(nf);
cap = ncap;
dflag.resize(cap);
fill(std::begin(dflag), std::end(dflag), false);
}
inline bool extend_rate(u32 x) const { return x * 2 >= cap; }
inline bool shrink_rate(u32 x) const { return HASHMAP_DEFAULT_SIZE < cap && x * 10 <= cap; }
inline void extend() { reallocate(cap << 1); }
inline void shrink() { reallocate(cap >> 1); }
public:
u32 cap, s;
vector<Data> data;
vector<bool> flag, dflag;
u32 shift;
static u64 r;
static constexpr uint32_t HASHMAP_DEFAULT_SIZE = 4;
explicit HashMapBase() : cap(HASHMAP_DEFAULT_SIZE), s(0), data(cap), flag(cap), dflag(cap), shift(64 - __lg(cap)) {}
itr begin() const {
u32 h = 0;
while (h != cap) {
if (flag[h] == true && dflag[h] == false) break;
h++;
}
return itr(h, this);
}
itr end() const { return itr(this->cap, this); }
friend itr begin(const HashMapBase &h) { return h.begin(); }
friend itr end(const HashMapBase &h) { return h.end(); }
itr find(const Key &key) const {
u32 h = inner_hash(key);
while (true) {
if (flag[h] == false) return this->end();
if (dtok(data[h]) == key) {
if (dflag[h] == true) return this->end();
return itr(h, this);
}
h = (h + 1) & (cap - 1);
}
}
bool contain(const Key &key) const { return find(key) != this->end(); }
itr insert(const Data &d) {
u32 h = hash(d);
while (true) {
if (flag[h] == false) {
if (extend_rate(s + 1)) {
extend();
h = hash(d);
continue;
}
data[h] = d;
flag[h] = true;
++s;
return itr(h, this);
}
if (dtok(data[h]) == dtok(d)) {
if (dflag[h] == true) {
data[h] = d;
dflag[h] = false;
++s;
}
return itr(h, this);
}
h = (h + 1) & (cap - 1);
}
}
// tips for speed up :
// if return value is unnecessary, make argument_2 false.
itr erase(itr it, bool get_next = true) {
if (it == this->end()) return this->end();
s--;
if (shrink_rate(s)) {
Data d = data[it.i];
shrink();
it = find(dtok(d));
}
int ni = (it.i + 1) & (cap - 1);
if (this->flag[ni]) {
this->dflag[it.i] = true;
} else {
this->flag[it.i] = false;
}
if (get_next) ++it;
return it;
}
itr erase(const Key &key) { return erase(find(key)); }
bool empty() const { return s == 0; }
int size() const { return s; }
void clear() {
fill(std::begin(flag), std::end(flag), false);
fill(std::begin(dflag), std::end(dflag), false);
s = 0;
}
void reserve(int n) {
if (n <= 0) return;
n = 1 << min(23, __lg(n) + 2);
if (cap < u32(n)) reallocate(n);
}
};
template <typename Key, typename Data>
uint64_t HashMapBase<Key, Data>::r = chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count();
} // namespace HashMapImpl
/**
* @brief Hash Map(base)γ(γγγ·γ₯γγγγ»εΊεΊγ―γ©γΉ)
*/
template <typename Key, typename Val> struct HashMap : HashMapImpl::HashMapBase<Key, pair<Key, Val>> {
using base = typename HashMapImpl::HashMapBase<Key, pair<Key, Val>>;
using HashMapImpl::HashMapBase<Key, pair<Key, Val>>::HashMapBase;
using Data = pair<Key, Val>;
Val &operator[](const Key &k) {
typename base::u32 h = base::inner_hash(k);
while (true) {
if (base::flag[h] == false) {
if (base::extend_rate(base::s + 1)) {
base::extend();
h = base::hash(k);
continue;
}
base::data[h].first = k;
base::data[h].second = Val();
base::flag[h] = true;
++base::s;
return base::data[h].second;
}
if (base::data[h].first == k) {
if (base::dflag[h] == true) base::data[h].second = Val();
return base::data[h].second;
}
h = (h + 1) & (base::cap - 1);
}
}
typename base::itr emplace(const Key &key, const Val &val) { return base::insert(Data(key, val)); }
};
/*
* @brief γγγ·γ₯γγγ(ι£ζ³ι
ε)
* @docs docs/hashmap/hashmap.md
**/
template <typename Key> struct HashSet : HashMapImpl::HashMapBase<Key, Key> { using HashMapImpl::HashMapBase<Key, Key>::HashMapBase; };
/*
* @brief γγγ·γ₯γ»γγ(ιε)
* @docs docs/hashmap/hashset.md
**/
//HashMap<int,int> m;
//using #define int long long may cause trouble
//~count~ => contain
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>hash map randomised</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<scope>source.c++</scope>
</snippet>