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>;
381 using Base::operator *;
387 return this->mTable->mData[this->mIndex];
390 using Base::operator ->;
396 return this->mTable->mData + this->mIndex;
403 using Base = IteratorBase<const HashTable, const_iterator>;
410 using pointer =
const typename Base::value_type *;
435 mControl(ioRHS.mControl),
437 mMaxSize(ioRHS.mMaxSize),
438 mLoadLeft(ioRHS.mLoadLeft)
440 ioRHS.mData =
nullptr;
441 ioRHS.mControl =
nullptr;
468 mControl = ioRHS.mControl;
470 mMaxSize = ioRHS.mMaxSize;
471 mLoadLeft = ioRHS.mLoadLeft;
473 ioRHS.mData =
nullptr;
474 ioRHS.mControl =
nullptr;
504 if constexpr (!std::is_trivially_destructible<KeyValue>())
507 if (mControl[i] & cBucketUsed)
508 mData[i].~KeyValue();
510 if (mData !=
nullptr)
513 if constexpr (cNeedsAlignedAllocate)
531 if constexpr (!std::is_trivially_destructible<KeyValue>())
534 if (mControl[i] & cBucketUsed)
535 mData[i].~KeyValue();
539 size_type max_load = sGetMaxLoad(mMaxSize);
540 if (mLoadLeft != max_load)
543 memset(mControl, cBucketEmpty, mMaxSize + 15);
544 mLoadLeft = max_load;
617 return float(cMaxLoadFactorNumerator) / float(cMaxLoadFactorDenominator);
624 bool inserted =
InsertKey(HashTableDetail::sGetKey(inValue), index);
626 new (mData + index) KeyValue(inValue);
627 return std::make_pair(
iterator(
this, index), inserted);
640 GetIndexAndControlValue(inKey, index, control);
663 while (control_equal != 0)
669 local_index += first_equal;
672 local_index &= bucket_mask;
675 if (equal(HashTableDetail::sGetKey(mData[local_index]), inKey))
682 control_equal >>= first_equal + 1;
688 if (control_empty != 0)
695 index = (index + 16) & bucket_mask;
719 SetControlValue(inIterator.mIndex, control_value);
722 mData[inIterator.mIndex].~KeyValue();
725 if (control_value == cBucketEmpty)
746 std::swap(mData, ioRHS.mData);
747 std::swap(mControl, ioRHS.mControl);
748 std::swap(mSize, ioRHS.mSize);
749 std::swap(mMaxSize, ioRHS.mMaxSize);
750 std::swap(mLoadLeft, ioRHS.mLoadLeft);
760 uint8 &control = mControl[i];
765 control = cBucketEmpty;
772 control = cBucketDeleted;
779 mControl[mMaxSize + i] = mControl[i];
785 for (
size_type src = 0; src < mMaxSize; ++src)
786 if (mControl[src] == cBucketDeleted)
792 GetIndexAndControlValue(HashTableDetail::sGetKey(mData[src]), src_index, src_control);
801 if (control_free != 0)
810 dst = (dst + 16) & bucket_mask;
814 if (((dst - src_index) & probe_mask) == ((src - src_index) & probe_mask))
817 SetControlValue(src, src_control);
820 else if (mControl[dst] == cBucketEmpty)
823 SetControlValue(dst, src_control);
824 SetControlValue(src, cBucketEmpty);
825 new (mData + dst) KeyValue(std::move(mData[src]));
826 mData[src].~KeyValue();
833 SetControlValue(dst, src_control);
834 std::swap(mData[src], mData[dst]);
840 mLoadLeft = sGetMaxLoad(mMaxSize) - mSize;
845 static constexpr bool cNeedsAlignedAllocate =
alignof(KeyValue) > (JPH_CPU_ADDRESS_BITS == 32? 8 : 16);
848 static constexpr uint64 cMaxLoadFactorNumerator = 7;
849 static constexpr uint64 cMaxLoadFactorDenominator = 8;
852 static constexpr uint64 cMaxDeletedElementsNumerator = 1;
853 static constexpr uint64 cMaxDeletedElementsDenominator = 8;
856 static constexpr uint8 cBucketEmpty = 0;
857 static constexpr uint8 cBucketDeleted = 0x7f;
858 static constexpr uint8 cBucketUsed = 0x80;
861 KeyValue * mData =
nullptr;
864 uint8 * mControl =
nullptr;
std::uint8_t uint8
Definition: Core.h:493
std::uint64_t uint64
Definition: Core.h:496
unsigned int uint
Definition: Core.h:492
#define JPH_NAMESPACE_END
Definition: Core.h:419
std::uint32_t uint32
Definition: Core.h:495
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:413
#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:402
const_iterator(const HashTable *inTable, size_type inIndex)
Definition: HashTable.h:414
const_iterator & operator=(const iterator &inRHS)
Assignment.
Definition: HashTable.h:419
const typename Base::value_type * pointer
Definition: HashTable.h:410
const_iterator(const iterator &inIterator)
Definition: HashTable.h:416
const_iterator(const HashTable *inTable)
Constructors.
Definition: HashTable.h:413
const typename Base::value_type & reference
Properties.
Definition: HashTable.h:409
const_iterator(const const_iterator &inRHS)
Definition: HashTable.h:415
Non-const iterator.
Definition: HashTable.h:363
typename Base::value_type * pointer
Definition: HashTable.h:371
typename Base::value_type & reference
Properties.
Definition: HashTable.h:370
KeyValue & operator*()
Non-const access to key value pair.
Definition: HashTable.h:384
KeyValue * operator->()
Non-const access to key value pair.
Definition: HashTable.h:393
iterator(HashTable *inTable)
Constructors.
Definition: HashTable.h:374
iterator & operator=(const iterator &inRHS)
Assignment.
Definition: HashTable.h:379
iterator(HashTable *inTable, size_type inIndex)
Definition: HashTable.h:375
iterator(const iterator &inIterator)
Definition: HashTable.h:376
Definition: HashTable.h:16
void reserve(size_type inMaxSize)
Reserve memory for a certain number of elements.
Definition: HashTable.h:490
ptrdiff_t difference_type
Definition: HashTable.h:21
void clear()
Destroy the entire hash table.
Definition: HashTable.h:501
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:555
constexpr size_type max_bucket_count() const
Max number of buckets that the table can have.
Definition: HashTable.h:591
HashTable(const HashTable &inRHS)
Copy constructor.
Definition: HashTable.h:427
size_type size() const
Number of elements in the table.
Definition: HashTable.h:603
HashTable()=default
Default constructor.
void swap(HashTable &ioRHS) noexcept
Swap the contents of two hash tables.
Definition: HashTable.h:744
const_iterator cbegin() const
Iterator to first element.
Definition: HashTable.h:573
~HashTable()
Destructor.
Definition: HashTable.h:484
iterator begin()
Iterator to first element.
Definition: HashTable.h:549
const_iterator end() const
Iterator to one beyond last element.
Definition: HashTable.h:567
const_iterator find(const Key &inKey) const
Find an element, returns iterator to element or end() if not found.
Definition: HashTable.h:631
const_iterator cend() const
Iterator to one beyond last element.
Definition: HashTable.h:579
HashTable(HashTable &&ioRHS) noexcept
Move constructor.
Definition: HashTable.h:433
constexpr size_type max_size() const
Max number of elements that the table can hold.
Definition: HashTable.h:609
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:528
uint32 size_type
Definition: HashTable.h:20
bool empty() const
Check if there are no elements in the table.
Definition: HashTable.h:597
constexpr float max_load_factor() const
Get the max load factor for this table (max number of elements / number of buckets)
Definition: HashTable.h:615
void rehash(size_type)
Definition: HashTable.h:755
std::pair< iterator, bool > insert(const value_type &inValue)
Insert a new element, returns iterator and if the element was inserted.
Definition: HashTable.h:621
void erase(const const_iterator &inIterator)
Erase an element by iterator.
Definition: HashTable.h:700
const_iterator begin() const
Iterator to first element.
Definition: HashTable.h:561
size_type bucket_count() const
Number of buckets in the table.
Definition: HashTable.h:585
size_type erase(const Key &inKey)
Erase an element by key.
Definition: HashTable.h:733
HashTable & operator=(const HashTable &inRHS)
Assignment operator.
Definition: HashTable.h:448
Fallback hash function that calls T::GetHash()
Definition: HashCombine.h:59