GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
HashSet.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_DATA_HASH_SET_HPP
5#define GREM_CORE_DATA_HASH_SET_HPP
6
8
11
12#include <algorithm> // std::min, std::max, std::equal, std::lexicographical_compare
13#include <bit> // std::countr_zero, std::countl_zero
14#include <cstddef> // std::size_t, std::ptrdiff_t
15#include <cstdint> // std::uint32_t, std::uint64_t
16#include <cstring> // std::memset, std::memcpy
17#include <functional> // std::hash, std::equal_to
18#include <initializer_list> // std::initializer_list
19#include <iterator> // std::input_iterator, std::sentinel_for, std::iterator_traits, std::..._iterator_tag, std::next, std::begin, std::end
20#include <limits> // std::numeric_limits
21#include <memory> // std::allocator, std::allocator_traits, std::to_address
22#include <memory_resource> // std::pmr::polymorphic_allocator
23#include <stdexcept> // std::length_error
24#include <type_traits> // std::is_..._v, std::remove_..._t, std::conditional_t
25#include <utility> // std::move, std::forward, std::exchange, std::swap
26
27namespace grem {
28
29template <typename Key, typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>, typename Allocator = std::allocator<Key>>
30class HashSet {
31private:
32 static constexpr bool IS_TRANSPARENT = requires {
33 typename Hash::is_transparent;
34 typename KeyEqual::is_transparent;
35 };
36
37public:
38 using key_type = Key;
39 using value_type = Key;
40 using reference = Key&;
41 using const_reference = const Key&;
42 using pointer = typename std::allocator_traits<Allocator>::pointer;
43 using const_pointer = typename std::allocator_traits<Allocator>::const_pointer;
44 using difference_type = std::ptrdiff_t;
45 using size_type = std::size_t;
46 using hasher = Hash;
47 using key_equal = KeyEqual;
48 using allocator_type = Allocator;
49
50 class iterator {
51 public:
52 using value_type = Key;
53 using reference = const Key&;
54 using pointer = const Key*;
55 using difference_type = std::ptrdiff_t;
56 using iterator_category = std::bidirectional_iterator_tag;
57
58 iterator() noexcept = default;
59
60 constexpr iterator(const HashSet* set, size_type index) noexcept
61 : set(set)
62 , index(index) {}
63
64 [[nodiscard]] constexpr reference operator*() const {
65 GREM_ASSERT(set);
66 GREM_ASSERT(index < set->elementCapacity);
67 return set->keys[index];
68 }
69
70 [[nodiscard]] constexpr pointer operator->() const {
71 return &**this;
72 }
73
74 constexpr iterator& operator++() {
75 GREM_ASSERT(set);
76 GREM_ASSERT(index < set->elementCapacity);
77 index = set->findActiveIndexUnbounded(index + 1);
78 return *this;
79 }
80
81 constexpr iterator operator++(int) {
82 iterator old = *this;
83 ++*this;
84 return old;
85 }
86
87 constexpr iterator& operator--() {
88 GREM_ASSERT(set);
89 GREM_ASSERT(index > 0);
90 GREM_ASSERT(set->elementCount > 0);
91 index = set->findActiveIndexReverseUnbounded(index - 1);
92 return *this;
93 }
94
95 constexpr iterator operator--(int) {
96 iterator old = *this;
97 --*this;
98 return old;
99 }
100
101 [[nodiscard]] constexpr bool operator==(const iterator& other) const noexcept {
102 GREM_ASSERT(set == other.set);
103 return index == other.index;
104 }
105
106 [[nodiscard]] constexpr size_type getIndex() const noexcept {
107 return index;
108 }
109
110 private:
111 friend HashSet;
112
113 const HashSet* set = nullptr;
114 size_type index = 0;
115 };
116
118
119 constexpr HashSet() noexcept = default;
120
121 constexpr explicit HashSet(size_type bucket_count, const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual(), const Allocator& allocator = Allocator())
122 : HashSet(hash, equal, allocator) {
123 reserve(bucket_count);
124 }
125
126 constexpr HashSet(size_type bucket_count, const Allocator& allocator)
127 : HashSet(bucket_count, Hash(), KeyEqual(), allocator) {}
128
129 constexpr HashSet(size_type bucket_count, const Hash& hash, const Allocator& allocator)
130 : HashSet(bucket_count, hash, KeyEqual(), allocator) {}
131
132 constexpr explicit HashSet(const Allocator& allocator)
133 : HashSet(Hash(), KeyEqual(), allocator) {}
134
135 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
136 constexpr HashSet(InputIterator first, Sentinel last, size_type bucket_count = 0, const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual(),
137 const Allocator& allocator = Allocator())
138 : HashSet(bucket_count, hash, equal, allocator) {
139 insert(first, last);
140 }
141
142 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
143 constexpr HashSet(InputIterator first, Sentinel last, size_type bucket_count, const Allocator& allocator)
144 : HashSet(first, last, bucket_count, Hash(), KeyEqual(), allocator) {}
145
146 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
147 constexpr HashSet(InputIterator first, Sentinel last, size_type bucket_count, const Hash& hash, const Allocator& allocator)
148 : HashSet(first, last, bucket_count, hash, KeyEqual(), allocator) {}
149
150 constexpr HashSet(std::initializer_list<value_type> ilist, size_type bucket_count = 0, const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual(),
151 const Allocator& allocator = Allocator())
152 : HashSet(ilist.begin(), ilist.end(), bucket_count, hash, equal, allocator) {}
153
154 constexpr HashSet(std::initializer_list<value_type> ilist, size_type bucket_count, const Allocator& allocator = Allocator())
155 : HashSet(ilist, bucket_count, Hash(), KeyEqual(), allocator) {}
156
157 constexpr HashSet(std::initializer_list<value_type> ilist, size_type bucket_count, const Hash& hash = Hash(), const Allocator& allocator = Allocator())
158 : HashSet(ilist, bucket_count, hash, KeyEqual(), allocator) {}
159
160 constexpr HashSet(const HashSet& other)
161 : HashSet(other, std::allocator_traits<Allocator>::select_on_container_copy_construction(other.get_allocator())) {}
162
163 constexpr HashSet(const HashSet& other, const Allocator& allocator)
164 : HashSet(allocator) {
165 *this = other;
166 }
167
168 constexpr HashSet(HashSet&& other) noexcept
169 : activeBits(std::exchange(other.activeBits, nullptr))
170 , bucketExtents(std::exchange(other.bucketExtents, nullptr))
171 , keys(std::exchange(other.keys, nullptr))
172 , elementCount(std::exchange(other.elementCount, size_type{0}))
173 , elementCapacity(std::exchange(other.elementCapacity, size_type{0}))
174 , hash(std::move(other.hash))
175 , equal(std::move(other.equal))
176 , allocator(other.get_allocator()) {}
177
178 constexpr HashSet(HashSet&& other, const Allocator& allocator) noexcept // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
179 : HashSet(allocator) {
180 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
181 if (allocator != other.get_allocator()) {
182 reserve(other.size());
183 for (reference element : other) {
184 if constexpr (std::is_nothrow_move_constructible_v<Key>) {
185 insert(std::move(const_cast<Key&>(element)));
186 } else {
187 insert(element);
188 }
189 }
190 other.clear();
191 return;
192 }
193 }
194 activeBits = std::exchange(other.activeBits, nullptr);
195 bucketExtents = std::exchange(other.bucketExtents, nullptr);
196 keys = std::exchange(other.keys, nullptr);
197 elementCount = std::exchange(other.elementCount, size_type{0});
198 elementCapacity = std::exchange(other.elementCapacity, size_type{0});
199 hash = std::move(other.hash);
200 equal = std::move(other.equal);
201 }
202
203 constexpr ~HashSet() {
204 reset();
205 }
206
207 constexpr HashSet& operator=(const HashSet& other) {
208 if (this == &other) {
209 return *this;
210 }
211 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_copy_assignment::value) {
212 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
213 if (allocator != other.get_allocator()) {
214 reset();
215 }
216 }
217 allocator = other.get_allocator();
218 }
219 clear();
220 if (elementCapacity == other.elementCapacity) {
221 if (other.elementCount > 0) {
222 GREM_ASSERT(elementCapacity > 0);
223 GREM_ASSERT(elementCapacity % 64 == 0);
224 const size_type begin = other.findActiveIndexUnbounded(0);
225 for (size_type index = begin; index < elementCapacity; index = other.findActiveIndexUnbounded(index + 1)) {
226 try {
227 std::allocator_traits<Allocator>::construct(allocator, std::to_address(keys) + index, other.keys[index]);
228 } catch (...) {
229 while (index != begin) {
230 index = other.findActiveIndexReverseUnbounded(index - 1);
231 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(keys) + index);
232 }
233 throw;
234 }
235 }
236 std::memcpy(std::to_address(activeBits), std::to_address(other.activeBits), (elementCapacity / 64) * sizeof(std::uint64_t));
237 std::memcpy(std::to_address(bucketExtents), std::to_address(other.bucketExtents), elementCapacity * sizeof(std::uint32_t));
238 elementCount = other.elementCount;
239 }
240 } else {
241 for (const_reference element : other) {
242 insert(element);
243 }
244 }
245 return *this;
246 }
247
248 constexpr HashSet& operator=(HashSet&& other) noexcept {
249 if (this == &other) {
250 return *this;
251 }
252 if constexpr (!std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) {
253 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
254 if (allocator != other.get_allocator()) {
255 clear();
256 reserve(other.size());
257 for (const Key& element : other) {
258 if constexpr (std::is_nothrow_move_constructible_v<Key>) {
259 insert(std::move(const_cast<Key&>(element)));
260 } else {
261 insert(element);
262 }
263 }
264 other.clear();
265 return *this;
266 }
267 }
268 }
269 reset();
270 activeBits = std::exchange(other.activeBits, nullptr);
271 bucketExtents = std::exchange(other.bucketExtents, nullptr);
272 keys = std::exchange(other.keys, nullptr);
273 elementCount = std::exchange(other.elementCount, size_type{0});
274 elementCapacity = std::exchange(other.elementCapacity, size_type{0});
275 hash = std::move(other.hash);
276 equal = std::move(other.equal);
277 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) {
278 allocator = other.get_allocator();
279 }
280 return *this;
281 }
282
283 constexpr HashSet& operator=(std::initializer_list<value_type> ilist) {
284 clear();
285 insert(ilist);
286 return *this;
287 }
288
289 constexpr void swap(HashSet& other) noexcept(std::allocator_traits<Allocator>::is_always_equal::value) { // NOLINT(cppcoreguidelines-noexcept-swap, performance-noexcept-swap)
290 if (this == &other) {
291 return;
292 }
293 using std::swap;
294 swap(activeBits, other.activeBits);
295 swap(bucketExtents, other.bucketExtents);
296 swap(keys, other.keys);
297 swap(elementCount, other.elementCount);
298 swap(elementCapacity, other.elementCapacity);
299 swap(hash, other.hash);
300 swap(equal, other.equal);
301 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_swap::value) {
302 swap(allocator, other.allocator);
303 } else if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
304 GREM_ASSERT(allocator == other.allocator);
305 }
306 }
307
308 friend constexpr void swap(HashSet& a, HashSet& b) noexcept(noexcept(a.swap(b))) { // NOLINT(cppcoreguidelines-noexcept-swap, performance-noexcept-swap)
309 a.swap(b);
310 }
311
312 constexpr void clear() noexcept {
313 if (elementCount > 0) {
314 GREM_ASSERT(elementCapacity > 0);
315 GREM_ASSERT(elementCapacity % 64 == 0);
316 for (size_type index = findActiveIndexUnbounded(0); index < elementCapacity; index = findActiveIndexUnbounded(index + 1)) {
317 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(keys) + index);
318 }
319 std::memset(std::to_address(activeBits), 0, (elementCapacity / 64) * sizeof(std::uint64_t));
320 std::memset(std::to_address(bucketExtents), 0, elementCapacity * sizeof(std::uint32_t));
321 elementCount = 0;
322 }
323 }
324
325 constexpr void reserve(size_type capacity) {
326 size_type newCapacity = std::max(elementCapacity, size_type{64});
327 while (static_cast<float>(capacity) > static_cast<float>(newCapacity) * MAX_SIZE_PER_CAPACITY) {
328 if (newCapacity >= std::numeric_limits<size_type>::max() / 2) {
329 throw std::length_error{"Maximum capacity exceeded."};
330 }
331 newCapacity *= 2;
332 }
333 if (newCapacity <= elementCapacity) {
334 return;
335 }
336
337 GREM_ASSERT(newCapacity % 64 == 0);
338 GREM_ASSERT((newCapacity & (newCapacity - 1)) == 0);
339
340 HashSet newSet{hash, equal, allocator};
341 UInt64Allocator uInt64Allocator = allocator;
342 UInt32Allocator uInt32Allocator = allocator;
343 newSet.activeBits = std::allocator_traits<UInt64Allocator>::allocate(uInt64Allocator, newCapacity / 64 + 1);
344 try {
345 newSet.bucketExtents = std::allocator_traits<UInt32Allocator>::allocate(uInt32Allocator, newCapacity);
346 try {
347 newSet.keys = std::allocator_traits<Allocator>::allocate(allocator, newCapacity);
348 } catch (...) {
349 std::allocator_traits<UInt32Allocator>::deallocate(uInt32Allocator, newSet.bucketExtents, newCapacity);
350 throw;
351 }
352 } catch (...) {
353 std::allocator_traits<UInt64Allocator>::deallocate(uInt64Allocator, newSet.activeBits, newCapacity / 64 + 1);
354 throw;
355 }
356 std::memset(std::to_address(newSet.activeBits), 0, (newCapacity / 64) * sizeof(std::uint64_t));
357 newSet.activeBits[newCapacity / 64] = 1;
358 std::memset(std::to_address(newSet.bucketExtents), 0, newCapacity * sizeof(std::uint32_t));
359 newSet.elementCapacity = newCapacity;
360
361 for (const Key& element : *this) {
362 if constexpr (std::is_nothrow_move_constructible_v<Key>) {
363 newSet.insert(std::move(const_cast<Key&>(element)));
364 } else {
365 newSet.insert(element);
366 }
367 }
368 GREM_ASSERT(allocator == newSet.get_allocator());
369 *this = std::move(newSet);
370 }
371
372 constexpr Pair<iterator, bool> insert(const value_type& value) {
373 return insertKey(value_type(value));
374 }
375
377 return insertKey(std::move(value));
378 }
379
380 template <typename P>
381 constexpr Pair<iterator, bool> insert(P&& value) requires(std::is_constructible_v<value_type, P &&>) {
382 return insertKey(value_type(std::forward<P>(value)));
383 }
384
385 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
386 constexpr void insert(InputIterator first, Sentinel last) {
387 if constexpr (std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag>) {
388 reserve(size() + static_cast<size_type>(last - first));
389 }
390 while (first != last) {
391 insert(*first++);
392 }
393 }
394
395 constexpr void insert(std::initializer_list<value_type> ilist) {
396 insert(ilist.begin(), ilist.end());
397 }
398
399 template <typename R>
400 constexpr void insert_range(R&& r) { // NOLINT(cppcoreguidelines-missing-std-forward)
401 insert(std::begin(r), std::end(r));
402 }
403
404 template <typename... Args>
405 constexpr Pair<iterator, bool> emplace(Args&&... args) {
406 return insertKey(value_type(std::forward<Args>(args)...));
407 }
408
409 constexpr iterator erase(const_iterator pos) {
410 return iterator{this, eraseAtIndex(pos.index)};
411 }
412
413 constexpr iterator erase(const_iterator first, const_iterator last) {
414 while (first != last) {
415 first = erase(first);
416 }
417 return iterator{this, first.index};
418 }
419
420 constexpr size_type erase(const Key& key) {
421 return eraseKey(key);
422 }
423
424 template <typename K>
425 constexpr size_type erase(K&& x) requires(IS_TRANSPARENT && !std::is_convertible_v<K, iterator> && !std::is_convertible_v<K, const_iterator>) {
426 return eraseKey(std::forward<K>(x));
427 }
428
429 template <typename Predicate>
430 friend constexpr size_type erase_if(HashSet& c, Predicate predicate) {
431 const size_type oldSize = c.size();
432 iterator it = c.begin();
433 while (it != c.end()) {
434 if (predicate(*it)) {
435 it = c.erase(it);
436 } else {
437 ++it;
438 }
439 }
440 return oldSize - c.size();
441 }
442
443 [[nodiscard]] constexpr hasher hash_function() const {
444 return hash;
445 }
446
447 [[nodiscard]] constexpr key_equal key_eq() const {
448 return equal;
449 }
450
451 [[nodiscard]] constexpr allocator_type get_allocator() const noexcept {
452 return allocator;
453 }
454
455 [[nodiscard]] constexpr bool empty() const noexcept {
456 return elementCount == 0;
457 }
458
459 [[nodiscard]] constexpr size_type size() const noexcept {
460 return elementCount;
461 }
462
463 [[nodiscard]] constexpr size_type max_size() const noexcept {
464 static_assert(MAX_SIZE_PER_CAPACITY <= 1.0f);
465 return static_cast<size_type>(
466 static_cast<float>(std::min({
467 static_cast<size_type>(std::numeric_limits<difference_type>::max()),
468 static_cast<size_type>(std::numeric_limits<size_type>::max() / sizeof(std::uint32_t)),
469 static_cast<size_type>(std::numeric_limits<size_type>::max() / sizeof(Key)),
470 static_cast<size_type>(std::allocator_traits<UInt32Allocator>::max_size(allocator)),
471 static_cast<size_type>(std::allocator_traits<Allocator>::max_size(allocator)),
472 })) *
473 MAX_SIZE_PER_CAPACITY);
474 }
475
476 [[nodiscard]] constexpr bool contains(const Key& key) const {
477 return find(key) != end();
478 }
479
480 template <typename K>
481 [[nodiscard]] constexpr bool contains(const K& x) const requires(IS_TRANSPARENT) {
482 return find(x) != end();
483 }
484
485 [[nodiscard]] constexpr size_type count(const Key& key) const {
486 return (contains(key)) ? 1 : 0;
487 }
488
489 template <typename K>
490 [[nodiscard]] constexpr size_type count(const K& x) const requires(IS_TRANSPARENT) {
491 return (contains(x)) ? 1 : 0;
492 }
493
494 [[nodiscard]] constexpr iterator find(const Key& key) {
495 return iterator{this, findIndex(key)};
496 }
497
498 [[nodiscard]] constexpr const_iterator find(const Key& key) const {
499 return const_iterator{this, findIndex(key)};
500 }
501
502 template <typename K>
503 [[nodiscard]] constexpr iterator find(const K& x) requires(IS_TRANSPARENT) {
504 return iterator{this, findIndex(x)};
505 }
506
507 template <typename K>
508 [[nodiscard]] constexpr const_iterator find(const K& x) const requires(IS_TRANSPARENT) {
509 return const_iterator{this, findIndex(x)};
510 }
511
512 [[nodiscard]] constexpr iterator getIteratorAtIndex(size_type index) {
513 GREM_ASSERT(index <= elementCapacity);
514 return iterator{this, index};
515 }
516
517 [[nodiscard]] constexpr const_iterator getIteratorAtIndex(size_type index) const {
518 GREM_ASSERT(index <= elementCapacity);
519 return const_iterator{this, index};
520 }
521
522 [[nodiscard]] constexpr reference getAtIndex(size_type index) {
523 GREM_ASSERT(index <= elementCapacity);
524 GREM_ASSERT(isActive(index));
525 return *iterator{this, index};
526 }
527
528 [[nodiscard]] constexpr const_reference getAtIndex(size_type index) const {
529 GREM_ASSERT(index <= elementCapacity);
530 GREM_ASSERT(isActive(index));
531 return *const_iterator{this, index};
532 }
533
534 [[nodiscard]] constexpr Pair<iterator, iterator> equal_range(const Key& key) {
535 const iterator it = find(key);
536 if (it != end()) {
537 return {it, std::next(it)};
538 }
539 return {it, it};
540 }
541
542 [[nodiscard]] constexpr Pair<const_iterator, const_iterator> equal_range(const Key& key) const {
543 const const_iterator it = find(key);
544 if (it != end()) {
545 return {it, std::next(it)};
546 }
547 return {it, it};
548 }
549
550 template <typename K>
551 [[nodiscard]] constexpr Pair<iterator, iterator> equal_range(const K& x) requires(IS_TRANSPARENT) {
552 const iterator it = find(x);
553 if (it != end()) {
554 return {it, std::next(it)};
555 }
556 return {it, it};
557 }
558
559 template <typename K>
560 [[nodiscard]] constexpr Pair<const_iterator, const_iterator> equal_range(const K& x) const requires(IS_TRANSPARENT) {
561 const const_iterator it = find(x);
562 if (it != end()) {
563 return {it, std::next(it)};
564 }
565 return {it, it};
566 }
567
568 [[nodiscard]] constexpr iterator begin() noexcept {
569 return iterator{this, (elementCount > 0) ? findActiveIndexUnbounded(0) : elementCapacity};
570 }
571
572 [[nodiscard]] constexpr const_iterator begin() const noexcept {
573 return const_iterator{this, (elementCount > 0) ? findActiveIndexUnbounded(0) : elementCapacity};
574 }
575
576 [[nodiscard]] constexpr const_iterator cbegin() const noexcept {
577 return begin();
578 }
579
580 [[nodiscard]] constexpr iterator end() noexcept {
581 return iterator{this, elementCapacity};
582 }
583
584 [[nodiscard]] constexpr const_iterator end() const noexcept {
585 return const_iterator{this, elementCapacity};
586 }
587
588 [[nodiscard]] constexpr const_iterator cend() const noexcept {
589 return end();
590 }
591
592 [[nodiscard]] constexpr bool operator==(const HashSet& other) const {
593 return std::equal(begin(), end(), other.begin(), other.end());
594 }
595
596 [[nodiscard]] constexpr bool operator<(const HashSet& other) const {
597 return std::lexicographical_compare(begin(), end(), other.begin(), other.end());
598 }
599
600 [[nodiscard]] constexpr bool operator<=(const HashSet& other) const {
601 return !(other < *this);
602 }
603
604 [[nodiscard]] constexpr bool operator>(const HashSet& other) const {
605 return other < *this;
606 }
607
608 [[nodiscard]] constexpr bool operator>=(const HashSet& other) const {
609 return !(*this < other);
610 }
611
612private:
613 static constexpr float MAX_SIZE_PER_CAPACITY = 0.7f;
614
615 using UInt64Allocator = typename std::allocator_traits<Allocator>::template rebind_alloc<std::uint64_t>;
616 using UInt32Allocator = typename std::allocator_traits<Allocator>::template rebind_alloc<std::uint32_t>;
617
618 constexpr HashSet(const Hash& hash, const KeyEqual& equal, const Allocator& allocator)
619 : hash(hash)
620 , equal(equal)
621 , allocator(allocator) {}
622
623 [[nodiscard]] constexpr size_type getBucketIndexOfHash(std::size_t hashCode) const noexcept {
624 GREM_ASSERT((elementCapacity & (elementCapacity - 1)) == 0);
625 return static_cast<size_type>(hashCode) & static_cast<size_type>(elementCapacity - 1);
626 }
627
628 [[nodiscard]] constexpr size_type getWrappedIndex(size_type index) const noexcept {
629 GREM_ASSERT(elementCapacity > 0);
630 GREM_ASSERT((elementCapacity & (elementCapacity - 1)) == 0);
631 return index & static_cast<size_type>(elementCapacity - 1);
632 }
633
634 [[nodiscard]] constexpr bool isActive(size_type index) const {
635 return (activeBits[index / 64] & (std::uint64_t{1} << (index % 64))) != 0;
636 }
637
638 constexpr void setActive(size_type index) {
639 activeBits[index / 64] |= std::uint64_t{1} << (index % 64);
640 }
641
642 constexpr void clearActive(size_type index) {
643 activeBits[index / 64] &= ~(std::uint64_t{1} << (index % 64));
644 }
645
646 [[nodiscard]] constexpr size_type findActiveIndexUnbounded(size_type index) const {
647 while (true) {
648 const std::uint64_t bitsIndex = index / 64;
649 const std::uint64_t bitOffset = index % 64;
650 const std::uint64_t bits = activeBits[bitsIndex] >> bitOffset;
651 if (bits != 0) {
652 index += static_cast<size_type>(std::countr_zero(bits));
653 break;
654 }
655 index += 64 - bitOffset;
656 }
657 return index;
658 }
659
660 [[nodiscard]] constexpr size_type findActiveIndexReverseUnbounded(size_type index) const {
661 while (true) {
662 const std::uint64_t bitsIndex = index / 64;
663 const std::uint64_t bitOffset = index % 64;
664 const std::uint64_t bits = activeBits[bitsIndex] << (63 - bitOffset);
665 if (bits != 0) {
666 index -= static_cast<size_type>(std::countl_zero(bits));
667 break;
668 }
669 index -= bitOffset + 1;
670 }
671 return index;
672 }
673
674 constexpr void reset() noexcept {
675 clear();
676 if (elementCapacity > 0) {
677 UInt64Allocator uInt64Allocator = allocator;
678 UInt32Allocator uInt32Allocator = allocator;
679 std::allocator_traits<UInt64Allocator>::deallocate(uInt64Allocator, activeBits, (elementCapacity / 64) + 1);
680 std::allocator_traits<UInt32Allocator>::deallocate(uInt32Allocator, bucketExtents, elementCapacity);
681 std::allocator_traits<Allocator>::deallocate(allocator, keys, elementCapacity);
682 activeBits = nullptr;
683 bucketExtents = nullptr;
684 keys = nullptr;
685 elementCapacity = 0;
686 }
687 }
688
689 [[nodiscard]] constexpr Pair<iterator, bool> insertKey(Key&& k) {
690 if (size() >= max_size()) {
691 throw std::length_error{"size() >= max_size()"};
692 }
693
694 reserve(elementCount + 1);
695
696 const size_type bucketBegin = getBucketIndexOfHash(hash(k));
697 const size_type bucketExtent = bucketExtents[bucketBegin];
698 const size_type bucketEnd = getWrappedIndex(bucketBegin + bucketExtent);
699 for (size_type index = bucketBegin; index != bucketEnd; index = getWrappedIndex(index + 1)) {
700 if (isActive(index) && equal(keys[index], k)) {
701 return {iterator{this, index}, false};
702 }
703 }
704 size_type index = bucketBegin;
705 size_type newBucketExtent = 1;
706 while (isActive(index)) {
707 index = getWrappedIndex(index + 1);
708 if (newBucketExtent >= size_type{std::numeric_limits<std::uint32_t>::max()}) {
709 throw std::length_error{"Maximum bucket size exceeded."};
710 }
711 ++newBucketExtent;
712 }
713
714 std::allocator_traits<Allocator>::construct(allocator, std::to_address(keys) + index, std::move(k));
715
716 if (newBucketExtent > bucketExtent) {
717 bucketExtents[bucketBegin] = static_cast<std::uint32_t>(newBucketExtent);
718 }
719 setActive(index);
720 ++elementCount;
721
722 return {iterator{this, index}, true};
723 }
724
725 [[nodiscard]] size_type eraseAtIndex(size_type index) {
726 GREM_ASSERT(index < elementCapacity);
727 GREM_ASSERT(isActive(index));
728
729 const size_type bucketBegin = getBucketIndexOfHash(hash(keys[index]));
730 size_type bucketExtent = bucketExtents[bucketBegin];
731 size_type bucketEnd = getWrappedIndex(bucketBegin + bucketExtent);
732 GREM_ASSERT(bucketExtent > 0);
733 GREM_ASSERT(bucketBegin != bucketEnd);
734 GREM_ASSERT(index != bucketEnd);
735
736 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(keys) + index);
737 clearActive(index);
738 --elementCount;
739
740 if (getWrappedIndex(index + 1) == bucketEnd) {
741 --bucketExtent;
742 bucketEnd = index;
743 while (bucketExtent > 0) {
744 const size_type previousIndex = getWrappedIndex(bucketEnd - 1);
745 if (isActive(previousIndex)) {
746 break;
747 }
748 --bucketExtent;
749 bucketEnd = previousIndex;
750 }
751 bucketExtents[bucketBegin] = static_cast<std::uint32_t>(bucketExtent);
752 }
753
754 return findActiveIndexUnbounded(index + 1);
755 }
756
757 template <typename K>
758 [[nodiscard]] size_type eraseKey(const K& k) {
759 if (empty()) {
760 return 0;
761 }
762
763 const size_type bucketBegin = getBucketIndexOfHash(hash(k));
764 size_type bucketExtent = bucketExtents[bucketBegin];
765 size_type bucketEnd = getWrappedIndex(bucketBegin + bucketExtent);
766 size_type index = bucketBegin;
767 while (index != bucketEnd && (!isActive(index) || !equal(keys[index], k))) {
768 index = getWrappedIndex(index + 1);
769 }
770 if (index == bucketEnd) {
771 return 0;
772 }
773
774 GREM_ASSERT(bucketExtent > 0);
775 GREM_ASSERT(bucketBegin != bucketEnd);
776
777 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(keys) + index);
778 clearActive(index);
779 --elementCount;
780
781 if (getWrappedIndex(index + 1) == bucketEnd) {
782 --bucketExtent;
783 bucketEnd = index;
784 while (bucketExtent > 0) {
785 const size_type previousIndex = getWrappedIndex(bucketEnd - 1);
786 if (isActive(previousIndex)) {
787 break;
788 }
789 --bucketExtent;
790 bucketEnd = previousIndex;
791 }
792 bucketExtents[bucketBegin] = static_cast<std::uint32_t>(bucketExtent);
793 }
794
795 return 1;
796 }
797
798 template <typename K>
799 [[nodiscard]] size_type findIndex(const K& k) const {
800 if (!empty()) {
801 const size_type bucketBegin = getBucketIndexOfHash(hash(k));
802 const size_type bucketExtent = bucketExtents[bucketBegin];
803 const size_type bucketEnd = getWrappedIndex(bucketBegin + bucketExtent);
804 size_type index = bucketBegin;
805 while (index != bucketEnd && (!isActive(index) || !equal(keys[index], k))) {
806 index = getWrappedIndex(index + 1);
807 }
808 if (index != bucketEnd) {
809 return index;
810 }
811 }
812 return elementCapacity;
813 }
814
815 typename std::allocator_traits<UInt64Allocator>::pointer activeBits{};
816 typename std::allocator_traits<UInt32Allocator>::pointer bucketExtents{};
817 typename std::allocator_traits<Allocator>::pointer keys{};
818 size_type elementCount = 0;
819 size_type elementCapacity = 0;
820 [[no_unique_address]] hasher hash;
821 [[no_unique_address]] key_equal equal;
822 [[no_unique_address]] Allocator allocator;
823};
824
825} // namespace grem
826
827namespace grem::pmr {
828
829template <typename Key, typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
831
832} // namespace grem::pmr
833
834#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition HashSet.hpp:50
constexpr iterator & operator--()
Definition HashSet.hpp:87
constexpr iterator operator++(int)
Definition HashSet.hpp:81
constexpr size_type getIndex() const noexcept
Definition HashSet.hpp:106
std::bidirectional_iterator_tag iterator_category
Definition HashSet.hpp:56
constexpr iterator operator--(int)
Definition HashSet.hpp:95
constexpr reference operator*() const
Definition HashSet.hpp:64
constexpr pointer operator->() const
Definition HashSet.hpp:70
Key value_type
Definition HashSet.hpp:52
constexpr bool operator==(const iterator &other) const noexcept
Definition HashSet.hpp:101
constexpr iterator & operator++()
Definition HashSet.hpp:74
std::ptrdiff_t difference_type
Definition HashSet.hpp:55
iterator() noexcept=default
const Key & reference
Definition HashSet.hpp:53
Definition HashSet.hpp:30
constexpr bool operator>(const HashSet &other) const
Definition HashSet.hpp:604
friend constexpr void swap(HashSet &a, HashSet &b) noexcept(noexcept(a.swap(b)))
Definition HashSet.hpp:308
constexpr HashSet(const HashSet &other, const Allocator &allocator)
Definition HashSet.hpp:163
constexpr Pair< iterator, bool > insert(P &&value)
Definition HashSet.hpp:381
constexpr iterator getIteratorAtIndex(size_type index)
Definition HashSet.hpp:512
friend constexpr size_type erase_if(HashSet &c, Predicate predicate)
Definition HashSet.hpp:430
constexpr HashSet(size_type bucket_count, const Hash &hash, const Allocator &allocator)
Definition HashSet.hpp:129
constexpr HashSet(InputIterator first, Sentinel last, size_type bucket_count=0, const Hash &hash=Hash(), const KeyEqual &equal=KeyEqual(), const Allocator &allocator=Allocator())
Definition HashSet.hpp:136
constexpr HashSet & operator=(std::initializer_list< value_type > ilist)
Definition HashSet.hpp:283
constexpr const_reference getAtIndex(size_type index) const
Definition HashSet.hpp:528
constexpr iterator find(const K &x)
Definition HashSet.hpp:503
constexpr iterator end() noexcept
Definition HashSet.hpp:580
constexpr const_iterator cbegin() const noexcept
Definition HashSet.hpp:576
constexpr ~HashSet()
Definition HashSet.hpp:203
constexpr Pair< iterator, bool > insert(const value_type &value)
Definition HashSet.hpp:372
constexpr iterator erase(const_iterator pos)
Definition HashSet.hpp:409
constexpr void swap(HashSet &other) noexcept(std::allocator_traits< Allocator >::is_always_equal::value)
Definition HashSet.hpp:289
constexpr size_type erase(const Key &key)
Definition HashSet.hpp:420
constexpr const_iterator begin() const noexcept
Definition HashSet.hpp:572
constexpr iterator erase(const_iterator first, const_iterator last)
Definition HashSet.hpp:413
Key value_type
Definition HashSet.hpp:39
constexpr allocator_type get_allocator() const noexcept
Definition HashSet.hpp:451
constexpr bool contains(const K &x) const
Definition HashSet.hpp:481
typename std::allocator_traits< Allocator >::const_pointer const_pointer
Definition HashSet.hpp:43
const Key & const_reference
Definition HashSet.hpp:41
constexpr size_type count(const Key &key) const
Definition HashSet.hpp:485
std::ptrdiff_t difference_type
Definition HashSet.hpp:44
constexpr size_type count(const K &x) const
Definition HashSet.hpp:490
constexpr HashSet(std::initializer_list< value_type > ilist, size_type bucket_count, const Allocator &allocator=Allocator())
Definition HashSet.hpp:154
constexpr bool operator>=(const HashSet &other) const
Definition HashSet.hpp:608
Key & reference
Definition HashSet.hpp:40
iterator const_iterator
Definition HashSet.hpp:117
constexpr bool operator<=(const HashSet &other) const
Definition HashSet.hpp:600
constexpr HashSet(std::initializer_list< value_type > ilist, size_type bucket_count, const Hash &hash=Hash(), const Allocator &allocator=Allocator())
Definition HashSet.hpp:157
constexpr void clear() noexcept
Definition HashSet.hpp:312
constexpr iterator find(const Key &key)
Definition HashSet.hpp:494
constexpr iterator begin() noexcept
Definition HashSet.hpp:568
constexpr hasher hash_function() const
Definition HashSet.hpp:443
Allocator allocator_type
Definition HashSet.hpp:48
constexpr const_iterator getIteratorAtIndex(size_type index) const
Definition HashSet.hpp:517
constexpr const_iterator find(const Key &key) const
Definition HashSet.hpp:498
constexpr Pair< const_iterator, const_iterator > equal_range(const Key &key) const
Definition HashSet.hpp:542
constexpr bool operator<(const HashSet &other) const
Definition HashSet.hpp:596
constexpr void insert(InputIterator first, Sentinel last)
Definition HashSet.hpp:386
constexpr Pair< iterator, bool > emplace(Args &&... args)
Definition HashSet.hpp:405
constexpr HashSet(const HashSet &other)
Definition HashSet.hpp:160
constexpr void reserve(size_type capacity)
Definition HashSet.hpp:325
constexpr Pair< iterator, iterator > equal_range(const K &x)
Definition HashSet.hpp:551
constexpr reference getAtIndex(size_type index)
Definition HashSet.hpp:522
constexpr HashSet & operator=(const HashSet &other)
Definition HashSet.hpp:207
constexpr size_type erase(K &&x)
Definition HashSet.hpp:425
constexpr HashSet(InputIterator first, Sentinel last, size_type bucket_count, const Hash &hash, const Allocator &allocator)
Definition HashSet.hpp:147
constexpr HashSet(const Allocator &allocator)
Definition HashSet.hpp:132
constexpr Pair< iterator, iterator > equal_range(const Key &key)
Definition HashSet.hpp:534
Hash hasher
Definition HashSet.hpp:46
constexpr const_iterator end() const noexcept
Definition HashSet.hpp:584
std::size_t size_type
Definition HashSet.hpp:45
constexpr HashSet(InputIterator first, Sentinel last, size_type bucket_count, const Allocator &allocator)
Definition HashSet.hpp:143
constexpr HashSet(HashSet &&other, const Allocator &allocator) noexcept
Definition HashSet.hpp:178
constexpr HashSet(std::initializer_list< value_type > ilist, size_type bucket_count=0, const Hash &hash=Hash(), const KeyEqual &equal=KeyEqual(), const Allocator &allocator=Allocator())
Definition HashSet.hpp:150
constexpr const_iterator find(const K &x) const
Definition HashSet.hpp:508
constexpr Pair< iterator, bool > insert(value_type &&value)
Definition HashSet.hpp:376
constexpr bool contains(const Key &key) const
Definition HashSet.hpp:476
constexpr HashSet(HashSet &&other) noexcept
Definition HashSet.hpp:168
constexpr size_type max_size() const noexcept
Definition HashSet.hpp:463
constexpr const_iterator cend() const noexcept
Definition HashSet.hpp:588
constexpr HashSet(size_type bucket_count, const Allocator &allocator)
Definition HashSet.hpp:126
constexpr bool empty() const noexcept
Definition HashSet.hpp:455
KeyEqual key_equal
Definition HashSet.hpp:47
Key key_type
Definition HashSet.hpp:38
constexpr size_type size() const noexcept
Definition HashSet.hpp:459
constexpr HashSet & operator=(HashSet &&other) noexcept
Definition HashSet.hpp:248
constexpr void insert(std::initializer_list< value_type > ilist)
Definition HashSet.hpp:395
constexpr bool operator==(const HashSet &other) const
Definition HashSet.hpp:592
constexpr HashSet() noexcept=default
constexpr Pair< const_iterator, const_iterator > equal_range(const K &x) const
Definition HashSet.hpp:560
constexpr key_equal key_eq() const
Definition HashSet.hpp:447
constexpr void insert_range(R &&r)
Definition HashSet.hpp:400
Definition concepts.hpp:106
Definition Allocation.hpp:554
grem::HashSet< Key, Hash, KeyEqual, std::pmr::polymorphic_allocator< Key > > HashSet
Definition HashSet.hpp:830
Definition aliases.hpp:9
constexpr bool equal(input_range auto &&rangeA, input_range auto &&rangeB)
Definition algorithms.hpp:358
Pair(T1, T2) -> Pair< T1, T2 >
Definition Pair.hpp:53