Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
HashTable.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2024 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <Jolt/Math/BVec16.h>
8
10
14template <class Key, class KeyValue, class HashTableDetail, class Hash, class KeyEqual>
16{
17public:
19 using value_type = KeyValue;
21 using difference_type = ptrdiff_t;
22
23private:
25 template <class Table, class Iterator>
26 class IteratorBase
27 {
28 public:
30 using difference_type = typename Table::difference_type;
31 using value_type = typename Table::value_type;
32 using iterator_category = std::forward_iterator_tag;
33
35 IteratorBase(const IteratorBase &inRHS) = default;
36
38 IteratorBase & operator = (const IteratorBase &inRHS) = default;
39
41 explicit IteratorBase(Table *inTable) :
42 mTable(inTable),
43 mIndex(0)
44 {
45 while (mIndex < mTable->mMaxSize && (mTable->mControl[mIndex] & cBucketUsed) == 0)
46 ++mIndex;
47 }
48
50 IteratorBase(Table *inTable, size_type inIndex) :
51 mTable(inTable),
52 mIndex(inIndex)
53 {
54 }
55
57 Iterator & operator ++ ()
58 {
59 JPH_ASSERT(IsValid());
60
61 do
62 {
63 ++mIndex;
64 }
65 while (mIndex < mTable->mMaxSize && (mTable->mControl[mIndex] & cBucketUsed) == 0);
66
67 return static_cast<Iterator &>(*this);
68 }
69
71 Iterator operator ++ (int)
72 {
73 Iterator result(mTable, mIndex);
74 ++(*this);
75 return result;
76 }
77
79 const KeyValue & operator * () const
80 {
81 JPH_ASSERT(IsValid());
82 return mTable->mData[mIndex];
83 }
84
86 const KeyValue * operator -> () const
87 {
88 JPH_ASSERT(IsValid());
89 return mTable->mData + mIndex;
90 }
91
93 bool operator == (const Iterator &inRHS) const
94 {
95 return mIndex == inRHS.mIndex && mTable == inRHS.mTable;
96 }
97
99 bool operator != (const Iterator &inRHS) const
100 {
101 return !(*this == inRHS);
102 }
103
105 bool IsValid() const
106 {
107 return mIndex < mTable->mMaxSize
108 && (mTable->mControl[mIndex] & cBucketUsed) != 0;
109 }
110
111 Table * mTable;
112 size_type mIndex;
113 };
114
116 static constexpr size_type sGetMaxLoad(size_type inBucketCount)
117 {
118 return uint32((cMaxLoadFactorNumerator * inBucketCount) / cMaxLoadFactorDenominator);
119 }
120
122 JPH_INLINE void SetControlValue(size_type inIndex, uint8 inValue)
123 {
124 JPH_ASSERT(inIndex < mMaxSize);
125 mControl[inIndex] = inValue;
126
127 // Mirror the first 15 bytes to the 15 bytes beyond mMaxSize
128 // Note that this is equivalent to:
129 // if (inIndex < 15)
130 // mControl[inIndex + mMaxSize] = inValue
131 // else
132 // mControl[inIndex] = inValue
133 // Which performs a needless write if inIndex >= 15 but at least it is branch-less
134 mControl[((inIndex - 15) & (mMaxSize - 1)) + 15] = inValue;
135 }
136
138 JPH_INLINE void GetIndexAndControlValue(const Key &inKey, size_type &outIndex, uint8 &outControl) const
139 {
140 // Calculate hash
141 uint64 hash_value = Hash { } (inKey);
142
143 // Split hash into index and control value
144 outIndex = size_type(hash_value >> 7) & (mMaxSize - 1);
145 outControl = cBucketUsed | uint8(hash_value);
146 }
147
149 void AllocateTable(size_type inMaxSize)
150 {
151 JPH_ASSERT(mData == nullptr);
152
153 mMaxSize = inMaxSize;
154 mLoadLeft = sGetMaxLoad(inMaxSize);
155 size_t required_size = size_t(mMaxSize) * (sizeof(KeyValue) + 1) + 15; // Add 15 bytes to mirror the first 15 bytes of the control values
156 if constexpr (cNeedsAlignedAllocate)
157 mData = reinterpret_cast<KeyValue *>(AlignedAllocate(required_size, alignof(KeyValue)));
158 else
159 mData = reinterpret_cast<KeyValue *>(Allocate(required_size));
160 mControl = reinterpret_cast<uint8 *>(mData + mMaxSize);
161 }
162
164 void CopyTable(const HashTable &inRHS)
165 {
166 if (inRHS.empty())
167 return;
168
169 AllocateTable(inRHS.mMaxSize);
170
171 // Copy control bytes
172 memcpy(mControl, inRHS.mControl, mMaxSize + 15);
173
174 // Copy elements
175 uint index = 0;
176 for (const uint8 *control = mControl, *control_end = mControl + mMaxSize; control != control_end; ++control, ++index)
177 if (*control & cBucketUsed)
178 new (mData + index) KeyValue(inRHS.mData[index]);
179 mSize = inRHS.mSize;
180 }
181
183 void GrowTable()
184 {
185 // Calculate new size
186 size_type new_max_size = max<size_type>(mMaxSize << 1, 16);
187 if (new_max_size < mMaxSize)
188 {
189 JPH_ASSERT(false, "Overflow in hash table size, can't grow!");
190 return;
191 }
192
193 // Move the old table to a temporary structure
194 size_type old_max_size = mMaxSize;
195 KeyValue *old_data = mData;
196 const uint8 *old_control = mControl;
197 mData = nullptr;
198 mControl = nullptr;
199 mSize = 0;
200 mMaxSize = 0;
201 mLoadLeft = 0;
202
203 // Allocate new table
204 AllocateTable(new_max_size);
205
206 // Reset all control bytes
207 memset(mControl, cBucketEmpty, mMaxSize + 15);
208
209 if (old_data != nullptr)
210 {
211 // Copy all elements from the old table
212 for (size_type i = 0; i < old_max_size; ++i)
213 if (old_control[i] & cBucketUsed)
214 {
215 size_type index;
216 KeyValue *element = old_data + i;
217 JPH_IF_ENABLE_ASSERTS(bool inserted =) InsertKey</* InsertAfterGrow= */ true>(HashTableDetail::sGetKey(*element), index);
218 JPH_ASSERT(inserted);
219 new (mData + index) KeyValue(std::move(*element));
220 element->~KeyValue();
221 }
222
223 // Free memory
224 if constexpr (cNeedsAlignedAllocate)
225 AlignedFree(old_data);
226 else
227 Free(old_data);
228 }
229 }
230
231protected:
233 KeyValue & GetElement(size_type inIndex) const
234 {
235 return mData[inIndex];
236 }
237
240 template <bool InsertAfterGrow = false>
241 bool InsertKey(const Key &inKey, size_type &outIndex)
242 {
243 // Ensure we have enough space
244 if (mLoadLeft == 0)
245 {
246 // Should not be growing if we're already growing!
247 if constexpr (InsertAfterGrow)
248 JPH_ASSERT(false);
249
250 // Decide if we need to clean up all tombstones or if we need to grow the map
251 size_type num_deleted = sGetMaxLoad(mMaxSize) - mSize;
252 if (num_deleted * cMaxDeletedElementsDenominator > mMaxSize * cMaxDeletedElementsNumerator)
253 rehash(0);
254 else
255 GrowTable();
256 }
257
258 // Split hash into index and control value
259 size_type index;
260 uint8 control;
261 GetIndexAndControlValue(inKey, index, control);
262
263 // Keeps track of the index of the first deleted bucket we found
264 constexpr size_type cNoDeleted = ~size_type(0);
265 size_type first_deleted_index = cNoDeleted;
266
267 // Linear probing
268 KeyEqual equal;
269 size_type bucket_mask = mMaxSize - 1;
270 BVec16 control16 = BVec16::sReplicate(control);
271 BVec16 bucket_empty = BVec16::sZero();
272 BVec16 bucket_deleted = BVec16::sReplicate(cBucketDeleted);
273 for (;;)
274 {
275 // Read 16 control values (note that we added 15 bytes at the end of the control values that mirror the first 15 bytes)
276 BVec16 control_bytes = BVec16::sLoadByte16(mControl + index);
277
278 // Check if we must find the element before we can insert
279 if constexpr (!InsertAfterGrow)
280 {
281 // Check for the control value we're looking for
282 // Note that when deleting we can create empty buckets instead of deleted buckets.
283 // This means we must unconditionally check all buckets in this batch for equality
284 // (also beyond the first empty bucket).
285 uint32 control_equal = uint32(BVec16::sEquals(control_bytes, control16).GetTrues());
286
287 // Index within the 16 buckets
288 size_type local_index = index;
289
290 // Loop while there's still buckets to process
291 while (control_equal != 0)
292 {
293 // Get the first equal bucket
294 uint first_equal = CountTrailingZeros(control_equal);
295
296 // Skip to the bucket
297 local_index += first_equal;
298
299 // Make sure that our index is not beyond the end of the table
300 local_index &= bucket_mask;
301
302 // We found a bucket with same control value
303 if (equal(HashTableDetail::sGetKey(mData[local_index]), inKey))
304 {
305 // Element already exists
306 outIndex = local_index;
307 return false;
308 }
309
310 // Skip past this bucket
311 control_equal >>= first_equal + 1;
312 local_index++;
313 }
314
315 // Check if we're still scanning for deleted buckets
316 if (first_deleted_index == cNoDeleted)
317 {
318 // Check if any buckets have been deleted, if so store the first one
319 uint32 control_deleted = uint32(BVec16::sEquals(control_bytes, bucket_deleted).GetTrues());
320 if (control_deleted != 0)
321 first_deleted_index = index + CountTrailingZeros(control_deleted);
322 }
323 }
324
325 // Check for empty buckets
326 uint32 control_empty = uint32(BVec16::sEquals(control_bytes, bucket_empty).GetTrues());
327 if (control_empty != 0)
328 {
329 // If we found a deleted bucket, use it.
330 // It doesn't matter if it is before or after the first empty bucket we found
331 // since we will always be scanning in batches of 16 buckets.
332 if (first_deleted_index == cNoDeleted || InsertAfterGrow)
333 {
334 index += CountTrailingZeros(control_empty);
335 --mLoadLeft; // Using an empty bucket decreases the load left
336 }
337 else
338 {
339 index = first_deleted_index;
340 }
341
342 // Make sure that our index is not beyond the end of the table
343 index &= bucket_mask;
344
345 // Update control byte
346 SetControlValue(index, control);
347 ++mSize;
348
349 // Return index to newly allocated bucket
350 outIndex = index;
351 return true;
352 }
353
354 // Move to next batch of 16 buckets
355 index = (index + 16) & bucket_mask;
356 }
357 }
358
359public:
361 class iterator : public IteratorBase<HashTable, iterator>
362 {
363 using Base = IteratorBase<HashTable, iterator>;
364
365 public:
367 using reference = typename Base::value_type &;
368 using pointer = typename Base::value_type *;
369
371 explicit iterator(HashTable *inTable) : Base(inTable) { }
372 iterator(HashTable *inTable, size_type inIndex) : Base(inTable, inIndex) { }
373 iterator(const iterator &inIterator) : Base(inIterator) { }
374
376 iterator & operator = (const iterator &inRHS) { Base::operator = (inRHS); return *this; }
377
378 using Base::operator *;
379
381 KeyValue & operator * ()
382 {
383 JPH_ASSERT(this->IsValid());
384 return this->mTable->mData[this->mIndex];
385 }
386
387 using Base::operator ->;
388
390 KeyValue * operator -> ()
391 {
392 JPH_ASSERT(this->IsValid());
393 return this->mTable->mData + this->mIndex;
394 }
395 };
396
398 class const_iterator : public IteratorBase<const HashTable, const_iterator>
399 {
400 using Base = IteratorBase<const HashTable, const_iterator>;
401
402 public:
404 using reference = const typename Base::value_type &;
405 using pointer = const typename Base::value_type *;
406
408 explicit const_iterator(const HashTable *inTable) : Base(inTable) { }
409 const_iterator(const HashTable *inTable, size_type inIndex) : Base(inTable, inIndex) { }
410 const_iterator(const const_iterator &inRHS) : Base(inRHS) { }
411 const_iterator(const iterator &inIterator) : Base(inIterator.mTable, inIterator.mIndex) { }
412
414 const_iterator & operator = (const iterator &inRHS) { this->mTable = inRHS.mTable; this->mIndex = inRHS.mIndex; return *this; }
415 const_iterator & operator = (const const_iterator &inRHS) { Base::operator = (inRHS); return *this; }
416 };
417
419 HashTable() = default;
420
422 HashTable(const HashTable &inRHS)
423 {
424 CopyTable(inRHS);
425 }
426
428 HashTable(HashTable &&ioRHS) noexcept :
429 mData(ioRHS.mData),
430 mControl(ioRHS.mControl),
431 mSize(ioRHS.mSize),
432 mMaxSize(ioRHS.mMaxSize),
433 mLoadLeft(ioRHS.mLoadLeft)
434 {
435 ioRHS.mData = nullptr;
436 ioRHS.mControl = nullptr;
437 ioRHS.mSize = 0;
438 ioRHS.mMaxSize = 0;
439 ioRHS.mLoadLeft = 0;
440 }
441
444 {
445 if (this != &inRHS)
446 {
447 clear();
448
449 CopyTable(inRHS);
450 }
451
452 return *this;
453 }
454
456 HashTable & operator = (HashTable &&ioRHS) noexcept
457 {
458 if (this != &ioRHS)
459 {
460 clear();
461
462 mData = ioRHS.mData;
463 mControl = ioRHS.mControl;
464 mSize = ioRHS.mSize;
465 mMaxSize = ioRHS.mMaxSize;
466 mLoadLeft = ioRHS.mLoadLeft;
467
468 ioRHS.mData = nullptr;
469 ioRHS.mControl = nullptr;
470 ioRHS.mSize = 0;
471 ioRHS.mMaxSize = 0;
472 ioRHS.mLoadLeft = 0;
473 }
474
475 return *this;
476 }
477
480 {
481 clear();
482 }
483
485 void reserve(size_type inMaxSize)
486 {
487 // Calculate max size based on load factor
488 size_type max_size = GetNextPowerOf2(max<uint32>((cMaxLoadFactorDenominator * inMaxSize) / cMaxLoadFactorNumerator, 16));
489 if (max_size <= mMaxSize)
490 return;
491
492 // Allocate buffers
493 AllocateTable(max_size);
494
495 // Reset all control bytes
496 memset(mControl, cBucketEmpty, mMaxSize + 15);
497 }
498
500 void clear()
501 {
502 // Delete all elements
503 if constexpr (!std::is_trivially_destructible<KeyValue>())
504 if (!empty())
505 for (size_type i = 0; i < mMaxSize; ++i)
506 if (mControl[i] & cBucketUsed)
507 mData[i].~KeyValue();
508
509 if (mData != nullptr)
510 {
511 // Free memory
512 if constexpr (cNeedsAlignedAllocate)
513 AlignedFree(mData);
514 else
515 Free(mData);
516
517 // Reset members
518 mData = nullptr;
519 mControl = nullptr;
520 mSize = 0;
521 mMaxSize = 0;
522 mLoadLeft = 0;
523 }
524 }
525
528 {
529 return iterator(this);
530 }
531
534 {
535 return iterator(this, mMaxSize);
536 }
537
540 {
541 return const_iterator(this);
542 }
543
546 {
547 return const_iterator(this, mMaxSize);
548 }
549
552 {
553 return const_iterator(this);
554 }
555
558 {
559 return const_iterator(this, mMaxSize);
560 }
561
564 {
565 return mMaxSize;
566 }
567
569 constexpr size_type max_bucket_count() const
570 {
571 return size_type(1) << (sizeof(size_type) * 8 - 1);
572 }
573
575 bool empty() const
576 {
577 return mSize == 0;
578 }
579
582 {
583 return mSize;
584 }
585
587 constexpr size_type max_size() const
588 {
589 return size_type((uint64(max_bucket_count()) * cMaxLoadFactorNumerator) / cMaxLoadFactorDenominator);
590 }
591
593 constexpr float max_load_factor() const
594 {
595 return float(cMaxLoadFactorNumerator) / float(cMaxLoadFactorDenominator);
596 }
597
599 std::pair<iterator, bool> insert(const value_type &inValue)
600 {
601 size_type index;
602 bool inserted = InsertKey(HashTableDetail::sGetKey(inValue), index);
603 if (inserted)
604 new (mData + index) KeyValue(inValue);
605 return std::make_pair(iterator(this, index), inserted);
606 }
607
609 const_iterator find(const Key &inKey) const
610 {
611 // Check if we have any data
612 if (empty())
613 return cend();
614
615 // Split hash into index and control value
616 size_type index;
617 uint8 control;
618 GetIndexAndControlValue(inKey, index, control);
619
620 // Linear probing
621 KeyEqual equal;
622 size_type bucket_mask = mMaxSize - 1;
623 BVec16 control16 = BVec16::sReplicate(control);
624 BVec16 bucket_empty = BVec16::sZero();
625 for (;;)
626 {
627 // Read 16 control values
628 // (note that we added 15 bytes at the end of the control values that mirror the first 15 bytes)
629 BVec16 control_bytes = BVec16::sLoadByte16(mControl + index);
630
631 // Check for the control value we're looking for
632 // Note that when deleting we can create empty buckets instead of deleted buckets.
633 // This means we must unconditionally check all buckets in this batch for equality
634 // (also beyond the first empty bucket).
635 uint32 control_equal = uint32(BVec16::sEquals(control_bytes, control16).GetTrues());
636
637 // Index within the 16 buckets
638 size_type local_index = index;
639
640 // Loop while there's still buckets to process
641 while (control_equal != 0)
642 {
643 // Get the first equal bucket
644 uint first_equal = CountTrailingZeros(control_equal);
645
646 // Skip to the bucket
647 local_index += first_equal;
648
649 // Make sure that our index is not beyond the end of the table
650 local_index &= bucket_mask;
651
652 // We found a bucket with same control value
653 if (equal(HashTableDetail::sGetKey(mData[local_index]), inKey))
654 {
655 // Element found
656 return const_iterator(this, local_index);
657 }
658
659 // Skip past this bucket
660 control_equal >>= first_equal + 1;
661 local_index++;
662 }
663
664 // Check for empty buckets
665 uint32 control_empty = uint32(BVec16::sEquals(control_bytes, bucket_empty).GetTrues());
666 if (control_empty != 0)
667 {
668 // An empty bucket was found, we didn't find the element
669 return cend();
670 }
671
672 // Move to next batch of 16 buckets
673 index = (index + 16) & bucket_mask;
674 }
675 }
676
678 void erase(const const_iterator &inIterator)
679 {
680 JPH_ASSERT(inIterator.IsValid());
681
682 // Read 16 control values before and after the current index
683 // (note that we added 15 bytes at the end of the control values that mirror the first 15 bytes)
684 BVec16 control_bytes_before = BVec16::sLoadByte16(mControl + ((inIterator.mIndex - 16) & (mMaxSize - 1)));
685 BVec16 control_bytes_after = BVec16::sLoadByte16(mControl + inIterator.mIndex);
686 BVec16 bucket_empty = BVec16::sZero();
687 uint32 control_empty_before = uint32(BVec16::sEquals(control_bytes_before, bucket_empty).GetTrues());
688 uint32 control_empty_after = uint32(BVec16::sEquals(control_bytes_after, bucket_empty).GetTrues());
689
690 // If (this index including) there exist 16 consecutive non-empty slots (represented by a bit being 0) then
691 // a probe looking for some element needs to continue probing so we cannot mark the bucket as empty
692 // but must mark it as deleted instead.
693 // Note that we use: CountLeadingZeros(uint16) = CountLeadingZeros(uint32) - 16.
694 uint8 control_value = CountLeadingZeros(control_empty_before) - 16 + CountTrailingZeros(control_empty_after) < 16? cBucketEmpty : cBucketDeleted;
695
696 // Mark the bucket as empty/deleted
697 SetControlValue(inIterator.mIndex, control_value);
698
699 // Destruct the element
700 mData[inIterator.mIndex].~KeyValue();
701
702 // If we marked the bucket as empty we can increase the load left
703 if (control_value == cBucketEmpty)
704 ++mLoadLeft;
705
706 // Decrease size
707 --mSize;
708 }
709
711 size_type erase(const Key &inKey)
712 {
713 const_iterator it = find(inKey);
714 if (it == cend())
715 return 0;
716
717 erase(it);
718 return 1;
719 }
720
722 void swap(HashTable &ioRHS) noexcept
723 {
724 std::swap(mData, ioRHS.mData);
725 std::swap(mControl, ioRHS.mControl);
726 std::swap(mSize, ioRHS.mSize);
727 std::swap(mMaxSize, ioRHS.mMaxSize);
728 std::swap(mLoadLeft, ioRHS.mLoadLeft);
729 }
730
734 {
735 // Update the control value for all buckets
736 for (size_type i = 0; i < mMaxSize; ++i)
737 {
738 uint8 &control = mControl[i];
739 switch (control)
740 {
741 case cBucketDeleted:
742 // Deleted buckets become empty
743 control = cBucketEmpty;
744 break;
745 case cBucketEmpty:
746 // Remains empty
747 break;
748 default:
749 // Mark all occupied as deleted, to indicate it needs to move to the correct place
750 control = cBucketDeleted;
751 break;
752 }
753 }
754
755 // Replicate control values to the last 15 entries
756 for (size_type i = 0; i < 15; ++i)
757 mControl[mMaxSize + i] = mControl[i];
758
759 // Loop over all elements that have been 'deleted' and move them to their new spot
760 BVec16 bucket_used = BVec16::sReplicate(cBucketUsed);
761 size_type bucket_mask = mMaxSize - 1;
762 uint32 probe_mask = bucket_mask & ~uint32(0b1111); // Mask out lower 4 bits because we test 16 buckets at a time
763 for (size_type src = 0; src < mMaxSize; ++src)
764 if (mControl[src] == cBucketDeleted)
765 for (;;)
766 {
767 // Split hash into index and control value
768 size_type src_index;
769 uint8 src_control;
770 GetIndexAndControlValue(HashTableDetail::sGetKey(mData[src]), src_index, src_control);
771
772 // Linear probing
773 size_type dst = src_index;
774 for (;;)
775 {
776 // Check if any buckets are free
777 BVec16 control_bytes = BVec16::sLoadByte16(mControl + dst);
778 uint32 control_free = uint32(BVec16::sAnd(control_bytes, bucket_used).GetTrues()) ^ 0xffff;
779 if (control_free != 0)
780 {
781 // Select this bucket as destination
782 dst += CountTrailingZeros(control_free);
783 dst &= bucket_mask;
784 break;
785 }
786
787 // Move to next batch of 16 buckets
788 dst = (dst + 16) & bucket_mask;
789 }
790
791 // Check if we stay in the same probe group
792 if (((dst - src_index) & probe_mask) == ((src - src_index) & probe_mask))
793 {
794 // We stay in the same group, we can stay where we are
795 SetControlValue(src, src_control);
796 break;
797 }
798 else if (mControl[dst] == cBucketEmpty)
799 {
800 // There's an empty bucket, move us there
801 SetControlValue(dst, src_control);
802 SetControlValue(src, cBucketEmpty);
803 new (mData + dst) KeyValue(std::move(mData[src]));
804 mData[src].~KeyValue();
805 break;
806 }
807 else
808 {
809 // There's an element in the bucket we want to move to, swap them
810 JPH_ASSERT(mControl[dst] == cBucketDeleted);
811 SetControlValue(dst, src_control);
812 std::swap(mData[src], mData[dst]);
813 // Iterate again with the same source bucket
814 }
815 }
816
817 // Reinitialize load left
818 mLoadLeft = sGetMaxLoad(mMaxSize) - mSize;
819 }
820
821private:
823 static constexpr bool cNeedsAlignedAllocate = alignof(KeyValue) > (JPH_CPU_ADDRESS_BITS == 32? 8 : 16);
824
826 static constexpr uint64 cMaxLoadFactorNumerator = 7;
827 static constexpr uint64 cMaxLoadFactorDenominator = 8;
828
830 static constexpr uint64 cMaxDeletedElementsNumerator = 1;
831 static constexpr uint64 cMaxDeletedElementsDenominator = 8;
832
834 static constexpr uint8 cBucketEmpty = 0;
835 static constexpr uint8 cBucketDeleted = 0x7f;
836 static constexpr uint8 cBucketUsed = 0x80; // Lowest 7 bits are lowest 7 bits of the hash value
837
839 KeyValue * mData = nullptr;
840
842 uint8 * mControl = nullptr;
843
845 size_type mSize = 0;
846
848 size_type mMaxSize = 0;
849
851 size_type mLoadLeft = 0;
852};
853
std::uint8_t uint8
Definition Core.h:482
std::uint64_t uint64
Definition Core.h:485
unsigned int uint
Definition Core.h:481
#define JPH_NAMESPACE_END
Definition Core.h:414
std::uint32_t uint32
Definition Core.h:484
#define JPH_NAMESPACE_BEGIN
Definition Core.h:408
#define JPH_IF_ENABLE_ASSERTS(...)
Definition IssueReporting.h:35
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
uint CountTrailingZeros(uint32 inValue)
Compute number of trailing zero bits (how many low bits are zero)
Definition Math.h:98
uint CountLeadingZeros(uint32 inValue)
Compute the number of leading zero bits (how many high bits are zero)
Definition Math.h:134
uint32 GetNextPowerOf2(uint32 inValue)
Get the next higher power of 2 of a value, or the value itself if the value is already a power of 2.
Definition Math.h:185
AllocateFunction Allocate
Definition Memory.cpp:68
FreeFunction Free
Definition Memory.cpp:70
AlignedFreeFunction AlignedFree
Definition Memory.cpp:72
AlignedAllocateFunction AlignedAllocate
Definition Memory.cpp:71
A vector consisting of 16 bytes.
Definition BVec16.h:11
static JPH_INLINE BVec16 sZero()
Vector with all zeros.
Definition BVec16.inl:46
static JPH_INLINE BVec16 sEquals(BVec16Arg inV1, BVec16Arg inV2)
Equals (component wise), highest bit of each component that is set is considered true.
Definition BVec16.inl:83
static JPH_INLINE BVec16 sReplicate(uint8 inV)
Replicate int inV across all components.
Definition BVec16.inl:57
static JPH_INLINE BVec16 sAnd(BVec16Arg inV1, BVec16Arg inV2)
Logical and (component wise)
Definition BVec16.inl:124
static JPH_INLINE BVec16 sLoadByte16(const uint8 *inV)
Load 16 bytes from memory.
Definition BVec16.inl:72
Const iterator.
Definition HashTable.h:399
const_iterator(const HashTable *inTable, size_type inIndex)
Definition HashTable.h:409
const_iterator & operator=(const iterator &inRHS)
Assignment.
Definition HashTable.h:414
const typename Base::value_type * pointer
Definition HashTable.h:405
const_iterator(const iterator &inIterator)
Definition HashTable.h:411
const_iterator(const HashTable *inTable)
Constructors.
Definition HashTable.h:408
const typename Base::value_type & reference
Properties.
Definition HashTable.h:404
const_iterator(const const_iterator &inRHS)
Definition HashTable.h:410
Non-const iterator.
Definition HashTable.h:362
typename Base::value_type * pointer
Definition HashTable.h:368
typename Base::value_type & reference
Properties.
Definition HashTable.h:367
KeyValue & operator*()
Non-const access to key value pair.
Definition HashTable.h:381
KeyValue * operator->()
Non-const access to key value pair.
Definition HashTable.h:390
iterator(HashTable *inTable)
Constructors.
Definition HashTable.h:371
iterator & operator=(const iterator &inRHS)
Assignment.
Definition HashTable.h:376
iterator(HashTable *inTable, size_type inIndex)
Definition HashTable.h:372
iterator(const iterator &inIterator)
Definition HashTable.h:373
Definition HashTable.h:16
void reserve(size_type inMaxSize)
Reserve memory for a certain number of elements.
Definition HashTable.h:485
ptrdiff_t difference_type
Definition HashTable.h:21
void clear()
Destroy the entire hash table.
Definition HashTable.h:500
KeyValue & GetElement(size_type inIndex) const
Get an element by index.
Definition HashTable.h:233
iterator end()
Iterator to one beyond last element.
Definition HashTable.h:533
constexpr size_type max_bucket_count() const
Max number of buckets that the table can have.
Definition HashTable.h:569
HashTable(const HashTable &inRHS)
Copy constructor.
Definition HashTable.h:422
size_type size() const
Number of elements in the table.
Definition HashTable.h:581
HashTable()=default
Default constructor.
void swap(HashTable &ioRHS) noexcept
Swap the contents of two hash tables.
Definition HashTable.h:722
const_iterator cbegin() const
Iterator to first element.
Definition HashTable.h:551
~HashTable()
Destructor.
Definition HashTable.h:479
iterator begin()
Iterator to first element.
Definition HashTable.h:527
const_iterator end() const
Iterator to one beyond last element.
Definition HashTable.h:545
const_iterator find(const Key &inKey) const
Find an element, returns iterator to element or end() if not found.
Definition HashTable.h:609
const_iterator cend() const
Iterator to one beyond last element.
Definition HashTable.h:557
HashTable(HashTable &&ioRHS) noexcept
Move constructor.
Definition HashTable.h:428
constexpr size_type max_size() const
Max number of elements that the table can hold.
Definition HashTable.h:587
KeyValue value_type
Properties.
Definition HashTable.h:19
bool InsertKey(const Key &inKey, size_type &outIndex)
Definition HashTable.h:241
uint32 size_type
Definition HashTable.h:20
bool empty() const
Check if there are no elements in the table.
Definition HashTable.h:575
constexpr float max_load_factor() const
Get the max load factor for this table (max number of elements / number of buckets)
Definition HashTable.h:593
void rehash(size_type)
Definition HashTable.h:733
std::pair< iterator, bool > insert(const value_type &inValue)
Insert a new element, returns iterator and if the element was inserted.
Definition HashTable.h:599
void erase(const const_iterator &inIterator)
Erase an element by iterator.
Definition HashTable.h:678
const_iterator begin() const
Iterator to first element.
Definition HashTable.h:539
size_type bucket_count() const
Number of buckets in the table.
Definition HashTable.h:563
size_type erase(const Key &inKey)
Erase an element by key.
Definition HashTable.h:711
HashTable & operator=(const HashTable &inRHS)
Assignment operator.
Definition HashTable.h:443
Definition Array.h:590
Fallback hash function that calls T::GetHash()
Definition HashCombine.h:59