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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
// Copyright 2025 Steven Le Rouzic
//
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#include "asl/base/annotations.hpp"
#include "asl/base/utility.hpp"
#include "asl/base/meta.hpp"
#include "asl/memory/allocator.hpp"
#include "asl/memory/memory.hpp"
#include "asl/types/maybe_uninit.hpp"
#include "asl/hashing/hash.hpp"
namespace asl
{
template<typename H, typename T>
concept key_hasher = requires (const T& value)
{
{ H::hash(value) } -> same_as<uint64_t>;
};
template<hashable T>
struct default_key_hasher
{
constexpr static uint64_t hash(const T& value)
{
return hash_value(value);
}
};
template<typename C, typename U, typename V = U>
concept key_comparator = requires(const U& a, const V& b)
{
{ C::eq(a, b) } -> same_as<bool>;
};
template<equality_comparable T>
struct default_key_comparator
{
constexpr static bool eq(const T& a, const T& b)
{
return a == b;
}
};
template<
is_object T,
allocator Allocator = DefaultAllocator,
key_hasher<T> KeyHasher = default_key_hasher<T>,
key_comparator<T> KeyComparator = default_key_comparator<T>
>
requires moveable<T>
class hash_set
{
protected:
static constexpr uint8_t kHasValue = 0x80;
static constexpr uint8_t kHashMask = 0x7f;
static constexpr uint8_t kEmpty = 0x00;
static constexpr uint8_t kTombstone = 0x01;
static constexpr isize_t kMinCapacity = 8;
// Important so we can memzero the tags
static_assert(kEmpty == 0);
uint8_t* m_tags{};
maybe_uninit<T>* m_values{};
isize_t m_capacity{};
isize_t m_size{};
ASL_NO_UNIQUE_ADDRESS Allocator m_allocator;
[[nodiscard]] constexpr isize_t max_size() const
{
// Max load factor is 75%
return (m_capacity >> 1) + (m_capacity >> 2); // NOLINT(*-signed-bitwise)
}
static isize_t size_to_capacity(isize_t size)
{
ASL_ASSERT(size > 0);
return max<isize_t>(
kMinCapacity,
static_cast<isize_t>(round_up_pow2((static_cast<uint64_t>(size) * 4 + 2) / 3)));
}
static void insert_inner(
T&& value,
uint8_t* tags,
maybe_uninit<T>* values,
isize_t capacity,
isize_t* size)
{
ASL_ASSERT(*size < capacity);
const auto result = find_slot_insert(value, tags, values, capacity);
// NOLINTBEGIN(*-pointer-arithmetic)
ASL_ASSERT(result.first_available_index >= 0);
if (result.already_present_index != result.first_available_index)
{
ASL_ASSERT((tags[result.first_available_index] & kHasValue) == 0);
if (result.already_present_index >= 0)
{
ASL_ASSERT((tags[result.already_present_index] & kHasValue) != 0);
values[result.already_present_index].destroy_unsafe();
tags[result.already_present_index] = kTombstone;
}
else
{
*size += 1;
}
values[result.first_available_index].construct_unsafe(std::move(value));
tags[result.first_available_index] = result.tag;
}
// NOLINTEND(*-pointer-arithmetic)
}
void grow_and_rehash()
{
grow_and_rehash(max(kMinCapacity, m_capacity * 2));
}
void grow_and_rehash(isize_t new_capacity)
{
ASL_ASSERT(new_capacity >= kMinCapacity && is_pow2(new_capacity) && new_capacity > m_capacity);
auto* new_tags = static_cast<uint8_t*>(m_allocator.alloc(layout::array<uint8_t>(new_capacity)));
auto* new_values = static_cast<maybe_uninit<T>*>(m_allocator.alloc(layout::array<maybe_uninit<T>>(new_capacity)));
asl::memzero(new_tags, new_capacity);
isize_t new_size = 0;
if (m_size > 0)
{
// NOLINTBEGIN(*-pointer-arithmetic)
for (isize_t i = 0; i < m_capacity; ++i)
{
if ((m_tags[i] & kHasValue) == 0) { continue; }
insert_inner(std::move(m_values[i].as_init_unsafe()), new_tags, new_values, new_capacity, &new_size);
// Destroy now so that destroy() has less things to do
m_values[i].destroy_unsafe();
m_tags[i] = kTombstone;
}
// NOLINTEND(*-pointer-arithmetic)
}
ASL_ASSERT(new_size == m_size);
m_size = 0;
destroy();
m_tags = new_tags;
m_values = new_values;
m_capacity = new_capacity;
m_size = new_size;
}
void clear_values()
{
if constexpr (!trivially_destructible<T>)
{
if (m_size > 0)
{
for (isize_t i = 0; i < m_capacity; ++i)
{
if ((m_tags[i] & kHasValue) != 0) // NOLINT(*-pointer-arithmetic)
{
m_values[i].destroy_unsafe(); // NOLINT(*-pointer-arithmetic)
}
}
}
}
}
void copy_from(const hash_set& other)
{
if (other.size() > 0)
{
isize_t min_capacity = size_to_capacity(other.size());
if (m_capacity < min_capacity)
{
grow_and_rehash(min_capacity);
}
ASL_ASSERT(m_capacity >= min_capacity);
for (isize_t i = 0; i < other.m_capacity; ++i)
{
if ((other.m_tags[i] & kHasValue) != 0) // NOLINT(*-pointer-arithmetic)
{
insert(other.m_values[i].as_init_unsafe()); // NOLINT(*-pointer-arithmetic)
}
}
}
}
struct FindSlotResult
{
uint8_t tag{};
isize_t first_available_index = -1;
isize_t already_present_index = -1;
};
template<typename U>
requires key_hasher<KeyHasher, U> && key_comparator<KeyComparator, T, U>
static FindSlotResult find_slot_insert(
const U& value,
const uint8_t* tags,
const maybe_uninit<T>* values,
isize_t capacity)
{
ASL_ASSERT(is_pow2(capacity));
FindSlotResult result{};
const isize_t capacity_mask = capacity - 1;
const uint64_t hash = KeyHasher::hash(value);
const auto starting_index = static_cast<isize_t>(hash >> 7) & capacity_mask;
result.tag = static_cast<uint8_t>(hash & kHashMask) | kHasValue;
// NOLINTBEGIN(*-pointer-arithmetic)
for (
isize_t i = starting_index;
i != starting_index || result.first_available_index < 0;
i = (i + 1) & capacity_mask)
{
uint8_t t = tags[i];
if ((t & kHasValue) == 0 && result.first_available_index < 0)
{
result.first_available_index = i;
}
if (t == result.tag && KeyComparator::eq(values[i].as_init_unsafe(), value))
{
ASL_ASSERT(result.already_present_index < 0);
result.already_present_index = i;
if (result.first_available_index < 0)
{
result.first_available_index = i;
}
break;
}
if (t == kEmpty) { break; }
}
// NOLINTEND(*-pointer-arithmetic)
return result;
}
template<typename U>
requires key_hasher<KeyHasher, U> && key_comparator<KeyComparator, T, U>
isize_t find_slot_lookup(const U& value) const
{
if (m_size <= 0) { return -1; };
ASL_ASSERT(is_pow2(m_capacity));
const isize_t capacity_mask = m_capacity - 1;
const uint64_t hash = KeyHasher::hash(value);
const uint8_t tag = static_cast<uint8_t>(hash & kHashMask) | kHasValue;
const auto starting_index = static_cast<isize_t>(hash >> 7) & capacity_mask;
// NOLINTBEGIN(*-pointer-arithmetic)
isize_t i = starting_index;
do
{
const uint8_t t = m_tags[i];
if (t == tag && KeyComparator::eq(m_values[i].as_init_unsafe(), value)) { return i; }
if (t == kEmpty) { break; }
i = (i + 1) & capacity_mask;
} while (i != starting_index);
// NOLINTEND(*-pointer-arithmetic)
return -1;
}
template<typename U>
requires key_hasher<KeyHasher, U> && key_comparator<KeyComparator, T, U>
FindSlotResult find_slot_insert(const U& value)
{
return find_slot_insert(value, m_tags, m_values, m_capacity);
}
void maybe_grow_to_fit_one_more()
{
if (m_size >= max_size())
{
grow_and_rehash();
}
}
public:
constexpr hash_set() requires default_constructible<Allocator>
: m_allocator{}
{}
explicit constexpr hash_set(Allocator allocator)
: m_allocator{std::move(allocator)}
{}
hash_set(const hash_set& other)
requires copy_constructible<Allocator> && copyable<T>
: hash_set{other.m_allocator}
{
copy_from(other);
}
hash_set& operator=(const hash_set& other)
requires copyable<T>
{
if (&other != this)
{
clear();
copy_from(other);
}
return *this;
}
hash_set(hash_set&& other)
requires move_constructible<Allocator>
: m_tags{exchange(other.m_tags, nullptr)}
, m_values{exchange(other.m_values, nullptr)}
, m_capacity{exchange(other.m_capacity, 0)}
, m_size{exchange(other.m_size, 0)}
, m_allocator{std::move(other.m_allocator)}
{}
hash_set& operator=(hash_set&& other)
{
if (&other != this)
{
destroy();
m_tags = exchange(other.m_tags, nullptr);
m_values = exchange(other.m_values, nullptr);
m_capacity = exchange(other.m_capacity, 0);
m_size = exchange(other.m_size, 0);
m_allocator = std::move(other.m_allocator);
}
return *this;
}
~hash_set()
{
destroy();
}
void destroy()
{
clear_values();
m_size = 0;
if (m_capacity > 0)
{
m_allocator.dealloc(m_tags, layout::array<uint8_t>(m_capacity));
m_allocator.dealloc(m_values, layout::array<maybe_uninit<T>>(m_capacity));
m_capacity = 0;
}
}
void clear()
{
clear_values();
m_size = 0;
if (m_capacity > 0)
{
asl::memzero(m_tags, m_capacity);
}
}
[[nodiscard]] constexpr isize_t size() const { return m_size; }
template<typename... Args>
void insert(Args&&... args)
requires constructible_from<T, Args&&...>
{
maybe_grow_to_fit_one_more();
ASL_ASSERT(m_size < max_size());
insert_inner(T{std::forward<Args>(args)...}, m_tags, m_values, m_capacity, &m_size);
}
template<typename U>
requires key_hasher<KeyHasher, U> && key_comparator<KeyComparator, T, U>
bool contains(const U& value) const
{
return find_slot_lookup(value) >= 0;
}
template<typename U>
requires key_hasher<KeyHasher, U> && key_comparator<KeyComparator, T, U>
bool remove(const U& value)
{
isize_t slot = find_slot_lookup(value);
if (slot < 0) { return false; }
m_values[slot].destroy_unsafe(); // NOLINT(*-pointer-arithmetic)
m_tags[slot] = kTombstone; // NOLINT(*-pointer-arithmetic)
m_size -= 1;
return true;
}
};
} // namespace asl
|