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]);
186 size_type new_max_size = max<size_type>(mMaxSize << 1, 16);
187 if (new_max_size < mMaxSize)
189 JPH_ASSERT(
false,
"Overflow in hash table size, can't grow!");
195 KeyValue *old_data = mData;
196 const uint8 *old_control = mControl;
204 AllocateTable(new_max_size);
207 memset(mControl, cBucketEmpty, mMaxSize + 15);
209 if (old_data !=
nullptr)
212 for (
size_type i = 0; i < old_max_size; ++i)
213 if (old_control[i] & cBucketUsed)
216 KeyValue *element = old_data + i;
219 new (mData + index) KeyValue(
std::move(*element));
220 element->~KeyValue();
224 if constexpr (cNeedsAlignedAllocate)
235 return mData[inIndex];
240 template <
bool InsertAfterGrow = false>
247 if constexpr (InsertAfterGrow)
251 size_type num_deleted = sGetMaxLoad(mMaxSize) - mSize;
252 if (num_deleted * cMaxDeletedElementsDenominator > mMaxSize * cMaxDeletedElementsNumerator)
261 GetIndexAndControlValue(inKey, index, control);
265 size_type first_deleted_index = cNoDeleted;
279 if constexpr (!InsertAfterGrow)
291 while (control_equal != 0)
297 local_index += first_equal;
300 local_index &= bucket_mask;
303 if (equal(HashTableDetail::sGetKey(mData[local_index]), inKey))
306 outIndex = local_index;
311 control_equal >>= first_equal + 1;
316 if (first_deleted_index == cNoDeleted)
320 if (control_deleted != 0)
327 if (control_empty != 0)
332 if (first_deleted_index == cNoDeleted || InsertAfterGrow)
339 index = first_deleted_index;
343 index &= bucket_mask;
346 SetControlValue(index, control);
355 index = (index + 16) & bucket_mask;
361 class iterator :
public IteratorBase<HashTable, iterator>
363 using Base = IteratorBase<HashTable, iterator>;
378 using Base::operator *;
384 return this->mTable->mData[this->mIndex];
387 using Base::operator ->;
393 return this->mTable->mData + this->mIndex;
400 using Base = IteratorBase<const HashTable, const_iterator>;
405 using pointer =
const typename Base::value_type *;
430 mControl(ioRHS.mControl),
432 mMaxSize(ioRHS.mMaxSize),
433 mLoadLeft(ioRHS.mLoadLeft)
435 ioRHS.mData =
nullptr;
436 ioRHS.mControl =
nullptr;
463 mControl = ioRHS.mControl;
465 mMaxSize = ioRHS.mMaxSize;
466 mLoadLeft = ioRHS.mLoadLeft;
468 ioRHS.mData =
nullptr;
469 ioRHS.mControl =
nullptr;
496 memset(mControl, cBucketEmpty, mMaxSize + 15);
503 if constexpr (!std::is_trivially_destructible<KeyValue>())
506 if (mControl[i] & cBucketUsed)
507 mData[i].~KeyValue();
509 if (mData !=
nullptr)
512 if constexpr (cNeedsAlignedAllocate)
595 return float(cMaxLoadFactorNumerator) / float(cMaxLoadFactorDenominator);
602 bool inserted =
InsertKey(HashTableDetail::sGetKey(inValue), index);
604 new (mData + index) KeyValue(inValue);
605 return std::make_pair(
iterator(
this, index), inserted);
618 GetIndexAndControlValue(inKey, index, control);
641 while (control_equal != 0)
647 local_index += first_equal;
650 local_index &= bucket_mask;
653 if (equal(HashTableDetail::sGetKey(mData[local_index]), inKey))
660 control_equal >>= first_equal + 1;
666 if (control_empty != 0)
673 index = (index + 16) & bucket_mask;
697 SetControlValue(inIterator.mIndex, control_value);
700 mData[inIterator.mIndex].~KeyValue();
703 if (control_value == cBucketEmpty)
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);
738 uint8 &control = mControl[i];
743 control = cBucketEmpty;
750 control = cBucketDeleted;
757 mControl[mMaxSize + i] = mControl[i];
763 for (
size_type src = 0; src < mMaxSize; ++src)
764 if (mControl[src] == cBucketDeleted)
770 GetIndexAndControlValue(HashTableDetail::sGetKey(mData[src]), src_index, src_control);
779 if (control_free != 0)
788 dst = (dst + 16) & bucket_mask;
792 if (((dst - src_index) & probe_mask) == ((src - src_index) & probe_mask))
795 SetControlValue(src, src_control);
798 else if (mControl[dst] == cBucketEmpty)
801 SetControlValue(dst, src_control);
802 SetControlValue(src, cBucketEmpty);
803 new (mData + dst) KeyValue(std::move(mData[src]));
804 mData[src].~KeyValue();
811 SetControlValue(dst, src_control);
812 std::swap(mData[src], mData[dst]);
818 mLoadLeft = sGetMaxLoad(mMaxSize) - mSize;
823 static constexpr bool cNeedsAlignedAllocate =
alignof(KeyValue) > (JPH_CPU_ADDRESS_BITS == 32? 8 : 16);
826 static constexpr uint64 cMaxLoadFactorNumerator = 7;
827 static constexpr uint64 cMaxLoadFactorDenominator = 8;
830 static constexpr uint64 cMaxDeletedElementsNumerator = 1;
831 static constexpr uint64 cMaxDeletedElementsDenominator = 8;
834 static constexpr uint8 cBucketEmpty = 0;
835 static constexpr uint8 cBucketDeleted = 0x7f;
836 static constexpr uint8 cBucketUsed = 0x80;
839 KeyValue * mData =
nullptr;
842 uint8 * mControl =
nullptr;
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
Fallback hash function that calls T::GetHash()
Definition HashCombine.h:59