14template <
class Key,
class KeyValue,
class HashTableDetail,
class Hash,
class KeyEqual>
25 template <
class Table,
class Iterator>
32 using iterator_category = std::forward_iterator_tag;
35 IteratorBase(
const IteratorBase &inRHS) =
default;
38 IteratorBase & operator = (
const IteratorBase &inRHS) =
default;
41 explicit IteratorBase(Table *inTable) :
45 while (mIndex < mTable->mMaxSize && (mTable->mControl[mIndex] & cBucketUsed) == 0)
50 IteratorBase(Table *inTable,
size_type inIndex) :
57 Iterator & operator ++ ()
65 while (mIndex < mTable->mMaxSize && (mTable->mControl[mIndex] & cBucketUsed) == 0);
67 return static_cast<Iterator &
>(*this);
71 Iterator operator ++ (
int)
73 Iterator result(mTable, mIndex);
79 const KeyValue & operator * ()
const
82 return mTable->mData[mIndex];
86 const KeyValue * operator -> ()
const
89 return mTable->mData + mIndex;
93 bool operator == (
const Iterator &inRHS)
const
95 return mIndex == inRHS.mIndex && mTable == inRHS.mTable;
99 bool operator != (
const Iterator &inRHS)
const
101 return !(*
this == inRHS);
107 return mIndex < mTable->mMaxSize
108 && (mTable->mControl[mIndex] & cBucketUsed) != 0;
118 return uint32((cMaxLoadFactorNumerator * inBucketCount) / cMaxLoadFactorDenominator);
125 mControl[inIndex] = inValue;
134 mControl[((inIndex - 15) & (mMaxSize - 1)) + 15] = inValue;
138 JPH_INLINE
void GetIndexAndControlValue(
const Key &inKey,
size_type &outIndex,
uint8 &outControl)
const
144 outIndex =
size_type(hash_value >> 7) & (mMaxSize - 1);
145 outControl = cBucketUsed |
uint8(hash_value);
153 mMaxSize = inMaxSize;
154 mLoadLeft = sGetMaxLoad(inMaxSize);
155 size_t required_size = size_t(mMaxSize) * (
sizeof(KeyValue) + 1) + 15;
156 if constexpr (cNeedsAlignedAllocate)
157 mData =
reinterpret_cast<KeyValue *
>(
AlignedAllocate(required_size,
alignof(KeyValue)));
159 mData =
reinterpret_cast<KeyValue *
>(
Allocate(required_size));
160 mControl =
reinterpret_cast<uint8 *
>(mData + mMaxSize);
169 AllocateTable(inRHS.mMaxSize);
172 memcpy(mControl, inRHS.mControl, mMaxSize + 15);
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]);
187 KeyValue *old_data = mData;
188 const uint8 *old_control = mControl;
196 AllocateTable(inNewMaxSize);
199 memset(mControl, cBucketEmpty, mMaxSize + 15);
201 if (old_data !=
nullptr)
204 for (
size_type i = 0; i < old_max_size; ++i)
205 if (old_control[i] & cBucketUsed)
208 KeyValue *element = old_data + i;
211 new (mData + index) KeyValue(
std::move(*element));
212 element->~KeyValue();
216 if constexpr (cNeedsAlignedAllocate)
227 return mData[inIndex];
232 template <
bool InsertAfterGrow = false>
239 if constexpr (InsertAfterGrow)
243 size_type num_deleted = sGetMaxLoad(mMaxSize) - mSize;
244 if (num_deleted * cMaxDeletedElementsDenominator > mMaxSize * cMaxDeletedElementsNumerator)
249 size_type new_max_size = max<size_type>(mMaxSize << 1, 16);
250 if (new_max_size < mMaxSize)
252 JPH_ASSERT(
false,
"Overflow in hash table size, can't grow!");
255 GrowTable(new_max_size);
262 GetIndexAndControlValue(inKey, index, control);
266 size_type first_deleted_index = cNoDeleted;
280 if constexpr (!InsertAfterGrow)
292 while (control_equal != 0)
298 local_index += first_equal;
301 local_index &= bucket_mask;
304 if (equal(HashTableDetail::sGetKey(mData[local_index]), inKey))
307 outIndex = local_index;
312 control_equal >>= first_equal + 1;
317 if (first_deleted_index == cNoDeleted)
321 if (control_deleted != 0)
328 if (control_empty != 0)
333 if (first_deleted_index == cNoDeleted || InsertAfterGrow)
340 index = first_deleted_index;
344 index &= bucket_mask;
347 SetControlValue(index, control);
356 index = (index + 16) & bucket_mask;
362 class iterator :
public IteratorBase<HashTable, iterator>
364 using Base = IteratorBase<HashTable, iterator>;
379 using Base::operator *;
385 return this->mTable->mData[this->mIndex];
388 using Base::operator ->;
394 return this->mTable->mData + this->mIndex;
401 using Base = IteratorBase<const HashTable, const_iterator>;
406 using pointer =
const typename Base::value_type *;
431 mControl(ioRHS.mControl),
433 mMaxSize(ioRHS.mMaxSize),
434 mLoadLeft(ioRHS.mLoadLeft)
436 ioRHS.mData =
nullptr;
437 ioRHS.mControl =
nullptr;
464 mControl = ioRHS.mControl;
466 mMaxSize = ioRHS.mMaxSize;
467 mLoadLeft = ioRHS.mLoadLeft;
469 ioRHS.mData =
nullptr;
470 ioRHS.mControl =
nullptr;
500 if constexpr (!std::is_trivially_destructible<KeyValue>())
503 if (mControl[i] & cBucketUsed)
504 mData[i].~KeyValue();
506 if (mData !=
nullptr)
509 if constexpr (cNeedsAlignedAllocate)
527 if constexpr (!std::is_trivially_destructible<KeyValue>())
530 if (mControl[i] & cBucketUsed)
531 mData[i].~KeyValue();
535 size_type max_load = sGetMaxLoad(mMaxSize);
536 if (mLoadLeft != max_load)
539 memset(mControl, cBucketEmpty, mMaxSize + 15);
540 mLoadLeft = max_load;
613 return float(cMaxLoadFactorNumerator) / float(cMaxLoadFactorDenominator);
620 bool inserted =
InsertKey(HashTableDetail::sGetKey(inValue), index);
622 new (mData + index) KeyValue(inValue);
623 return std::make_pair(
iterator(
this, index), inserted);
636 GetIndexAndControlValue(inKey, index, control);
659 while (control_equal != 0)
665 local_index += first_equal;
668 local_index &= bucket_mask;
671 if (equal(HashTableDetail::sGetKey(mData[local_index]), inKey))
678 control_equal >>= first_equal + 1;
684 if (control_empty != 0)
691 index = (index + 16) & bucket_mask;
715 SetControlValue(inIterator.mIndex, control_value);
718 mData[inIterator.mIndex].~KeyValue();
721 if (control_value == cBucketEmpty)
742 std::swap(mData, ioRHS.mData);
743 std::swap(mControl, ioRHS.mControl);
744 std::swap(mSize, ioRHS.mSize);
745 std::swap(mMaxSize, ioRHS.mMaxSize);
746 std::swap(mLoadLeft, ioRHS.mLoadLeft);
756 uint8 &control = mControl[i];
761 control = cBucketEmpty;
768 control = cBucketDeleted;
775 mControl[mMaxSize + i] = mControl[i];
781 for (
size_type src = 0; src < mMaxSize; ++src)
782 if (mControl[src] == cBucketDeleted)
788 GetIndexAndControlValue(HashTableDetail::sGetKey(mData[src]), src_index, src_control);
797 if (control_free != 0)
806 dst = (dst + 16) & bucket_mask;
810 if (((dst - src_index) & probe_mask) == ((src - src_index) & probe_mask))
813 SetControlValue(src, src_control);
816 else if (mControl[dst] == cBucketEmpty)
819 SetControlValue(dst, src_control);
820 SetControlValue(src, cBucketEmpty);
821 new (mData + dst) KeyValue(std::move(mData[src]));
822 mData[src].~KeyValue();
829 SetControlValue(dst, src_control);
830 std::swap(mData[src], mData[dst]);
836 mLoadLeft = sGetMaxLoad(mMaxSize) - mSize;
841 static constexpr bool cNeedsAlignedAllocate =
alignof(KeyValue) > (JPH_CPU_ADDRESS_BITS == 32? 8 : 16);
844 static constexpr uint64 cMaxLoadFactorNumerator = 7;
845 static constexpr uint64 cMaxLoadFactorDenominator = 8;
848 static constexpr uint64 cMaxDeletedElementsNumerator = 1;
849 static constexpr uint64 cMaxDeletedElementsDenominator = 8;
852 static constexpr uint8 cBucketEmpty = 0;
853 static constexpr uint8 cBucketDeleted = 0x7f;
854 static constexpr uint8 cBucketUsed = 0x80;
857 KeyValue * mData =
nullptr;
860 uint8 * mControl =
nullptr;
std::uint8_t uint8
Definition Core.h:487
std::uint64_t uint64
Definition Core.h:490
unsigned int uint
Definition Core.h:486
#define JPH_NAMESPACE_END
Definition Core.h:418
std::uint32_t uint32
Definition Core.h:489
#define JPH_NAMESPACE_BEGIN
Definition Core.h:412
#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:400
const_iterator(const HashTable *inTable, size_type inIndex)
Definition HashTable.h:410
const_iterator & operator=(const iterator &inRHS)
Assignment.
Definition HashTable.h:415
const typename Base::value_type * pointer
Definition HashTable.h:406
const_iterator(const iterator &inIterator)
Definition HashTable.h:412
const_iterator(const HashTable *inTable)
Constructors.
Definition HashTable.h:409
const typename Base::value_type & reference
Properties.
Definition HashTable.h:405
const_iterator(const const_iterator &inRHS)
Definition HashTable.h:411
Non-const iterator.
Definition HashTable.h:363
typename Base::value_type * pointer
Definition HashTable.h:369
typename Base::value_type & reference
Properties.
Definition HashTable.h:368
KeyValue & operator*()
Non-const access to key value pair.
Definition HashTable.h:382
KeyValue * operator->()
Non-const access to key value pair.
Definition HashTable.h:391
iterator(HashTable *inTable)
Constructors.
Definition HashTable.h:372
iterator & operator=(const iterator &inRHS)
Assignment.
Definition HashTable.h:377
iterator(HashTable *inTable, size_type inIndex)
Definition HashTable.h:373
iterator(const iterator &inIterator)
Definition HashTable.h:374
Definition HashTable.h:16
void reserve(size_type inMaxSize)
Reserve memory for a certain number of elements.
Definition HashTable.h:486
ptrdiff_t difference_type
Definition HashTable.h:21
void clear()
Destroy the entire hash table.
Definition HashTable.h:497
KeyValue & GetElement(size_type inIndex) const
Get an element by index.
Definition HashTable.h:225
iterator end()
Iterator to one beyond last element.
Definition HashTable.h:551
constexpr size_type max_bucket_count() const
Max number of buckets that the table can have.
Definition HashTable.h:587
HashTable(const HashTable &inRHS)
Copy constructor.
Definition HashTable.h:423
size_type size() const
Number of elements in the table.
Definition HashTable.h:599
HashTable()=default
Default constructor.
void swap(HashTable &ioRHS) noexcept
Swap the contents of two hash tables.
Definition HashTable.h:740
const_iterator cbegin() const
Iterator to first element.
Definition HashTable.h:569
~HashTable()
Destructor.
Definition HashTable.h:480
iterator begin()
Iterator to first element.
Definition HashTable.h:545
const_iterator end() const
Iterator to one beyond last element.
Definition HashTable.h:563
const_iterator find(const Key &inKey) const
Find an element, returns iterator to element or end() if not found.
Definition HashTable.h:627
const_iterator cend() const
Iterator to one beyond last element.
Definition HashTable.h:575
HashTable(HashTable &&ioRHS) noexcept
Move constructor.
Definition HashTable.h:429
constexpr size_type max_size() const
Max number of elements that the table can hold.
Definition HashTable.h:605
KeyValue value_type
Properties.
Definition HashTable.h:19
bool InsertKey(const Key &inKey, size_type &outIndex)
Definition HashTable.h:233
void ClearAndKeepMemory()
Destroy the entire hash table but keeps the memory allocated.
Definition HashTable.h:524
uint32 size_type
Definition HashTable.h:20
bool empty() const
Check if there are no elements in the table.
Definition HashTable.h:593
constexpr float max_load_factor() const
Get the max load factor for this table (max number of elements / number of buckets)
Definition HashTable.h:611
void rehash(size_type)
Definition HashTable.h:751
std::pair< iterator, bool > insert(const value_type &inValue)
Insert a new element, returns iterator and if the element was inserted.
Definition HashTable.h:617
void erase(const const_iterator &inIterator)
Erase an element by iterator.
Definition HashTable.h:696
const_iterator begin() const
Iterator to first element.
Definition HashTable.h:557
size_type bucket_count() const
Number of buckets in the table.
Definition HashTable.h:581
size_type erase(const Key &inKey)
Erase an element by key.
Definition HashTable.h:729
HashTable & operator=(const HashTable &inRHS)
Assignment operator.
Definition HashTable.h:444
Fallback hash function that calls T::GetHash()
Definition HashCombine.h:59