HashTable.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Math/BVec16.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// Helper class for implementing an UnorderedSet or UnorderedMap
  8. /// Based on CppCon 2017: Matt Kulukundis "Designing a Fast, Efficient, Cache-friendly Hash Table, Step by Step"
  9. /// See: https://www.youtube.com/watch?v=ncHmEUmJZf4
  10. template <class Key, class KeyValue, class HashTableDetail, class Hash, class KeyEqual>
  11. class HashTable
  12. {
  13. public:
  14. /// Properties
  15. using value_type = KeyValue;
  16. using size_type = uint32;
  17. using difference_type = ptrdiff_t;
  18. private:
  19. /// Base class for iterators
  20. template <class Table, class Iterator>
  21. class IteratorBase
  22. {
  23. public:
  24. /// Properties
  25. using difference_type = typename Table::difference_type;
  26. using value_type = typename Table::value_type;
  27. using iterator_category = std::forward_iterator_tag;
  28. /// Copy constructor
  29. IteratorBase(const IteratorBase &inRHS) = default;
  30. /// Assignment operator
  31. IteratorBase & operator = (const IteratorBase &inRHS) = default;
  32. /// Iterator at start of table
  33. explicit IteratorBase(Table *inTable) :
  34. mTable(inTable),
  35. mIndex(0)
  36. {
  37. while (mIndex < mTable->mMaxSize && (mTable->mControl[mIndex] & cBucketUsed) == 0)
  38. ++mIndex;
  39. }
  40. /// Iterator at specific index
  41. IteratorBase(Table *inTable, size_type inIndex) :
  42. mTable(inTable),
  43. mIndex(inIndex)
  44. {
  45. }
  46. /// Prefix increment
  47. Iterator & operator ++ ()
  48. {
  49. JPH_ASSERT(IsValid());
  50. do
  51. {
  52. ++mIndex;
  53. }
  54. while (mIndex < mTable->mMaxSize && (mTable->mControl[mIndex] & cBucketUsed) == 0);
  55. return static_cast<Iterator &>(*this);
  56. }
  57. /// Postfix increment
  58. Iterator operator ++ (int)
  59. {
  60. Iterator result(mTable, mIndex);
  61. ++(*this);
  62. return result;
  63. }
  64. /// Access to key value pair
  65. const KeyValue & operator * () const
  66. {
  67. JPH_ASSERT(IsValid());
  68. return mTable->mData[mIndex];
  69. }
  70. /// Access to key value pair
  71. const KeyValue * operator -> () const
  72. {
  73. JPH_ASSERT(IsValid());
  74. return mTable->mData + mIndex;
  75. }
  76. /// Equality operator
  77. bool operator == (const Iterator &inRHS) const
  78. {
  79. return mIndex == inRHS.mIndex && mTable == inRHS.mTable;
  80. }
  81. /// Inequality operator
  82. bool operator != (const Iterator &inRHS) const
  83. {
  84. return !(*this == inRHS);
  85. }
  86. /// Check that the iterator is valid
  87. bool IsValid() const
  88. {
  89. return mIndex < mTable->mMaxSize
  90. && (mTable->mControl[mIndex] & cBucketUsed) != 0;
  91. }
  92. Table * mTable;
  93. size_type mIndex;
  94. };
  95. /// Get the maximum number of elements that we can support given a number of buckets
  96. static constexpr size_type sGetMaxLoad(size_type inBucketCount)
  97. {
  98. return uint32((cMaxLoadFactorNumerator * inBucketCount) / cMaxLoadFactorDenominator);
  99. }
  100. /// Update the control value for a bucket
  101. JPH_INLINE void SetControlValue(size_type inIndex, uint8 inValue)
  102. {
  103. JPH_ASSERT(inIndex < mMaxSize);
  104. mControl[inIndex] = inValue;
  105. // Mirror the first 15 bytes to the 15 bytes beyond mMaxSize
  106. // Note that this is equivalent to:
  107. // if (inIndex < 15)
  108. // mControl[inIndex + mMaxSize] = inValue
  109. // else
  110. // mControl[inIndex] = inValue
  111. // Which performs a needless write if inIndex >= 15 but at least it is branch-less
  112. mControl[((inIndex - 15) & (mMaxSize - 1)) + 15] = inValue;
  113. }
  114. /// Get the index and control value for a particular key
  115. JPH_INLINE void GetIndexAndControlValue(const Key &inKey, size_type &outIndex, uint8 &outControl) const
  116. {
  117. // Calculate hash
  118. uint64 hash_value = Hash { } (inKey);
  119. // Split hash into index and control value
  120. outIndex = size_type(hash_value >> 7) & (mMaxSize - 1);
  121. outControl = cBucketUsed | uint8(hash_value);
  122. }
  123. /// Allocate space for the hash table
  124. void AllocateTable(size_type inMaxSize)
  125. {
  126. JPH_ASSERT(mData == nullptr);
  127. mMaxSize = inMaxSize;
  128. mLoadLeft = sGetMaxLoad(inMaxSize);
  129. size_t required_size = size_t(mMaxSize) * (sizeof(KeyValue) + 1) + 15; // Add 15 bytes to mirror the first 15 bytes of the control values
  130. if constexpr (cNeedsAlignedAllocate)
  131. mData = reinterpret_cast<KeyValue *>(AlignedAllocate(required_size, alignof(KeyValue)));
  132. else
  133. mData = reinterpret_cast<KeyValue *>(Allocate(required_size));
  134. mControl = reinterpret_cast<uint8 *>(mData + mMaxSize);
  135. }
  136. /// Copy the contents of another hash table
  137. void CopyTable(const HashTable &inRHS)
  138. {
  139. if (inRHS.empty())
  140. return;
  141. AllocateTable(inRHS.mMaxSize);
  142. // Copy control bytes
  143. memcpy(mControl, inRHS.mControl, mMaxSize + 15);
  144. // Copy elements
  145. uint index = 0;
  146. for (const uint8 *control = mControl, *control_end = mControl + mMaxSize; control != control_end; ++control, ++index)
  147. if (*control & cBucketUsed)
  148. new (mData + index) KeyValue(inRHS.mData[index]);
  149. mSize = inRHS.mSize;
  150. }
  151. /// Grow the table to a new size
  152. void GrowTable(size_type inNewMaxSize)
  153. {
  154. // Move the old table to a temporary structure
  155. size_type old_max_size = mMaxSize;
  156. KeyValue *old_data = mData;
  157. const uint8 *old_control = mControl;
  158. mData = nullptr;
  159. mControl = nullptr;
  160. mSize = 0;
  161. mMaxSize = 0;
  162. mLoadLeft = 0;
  163. // Allocate new table
  164. AllocateTable(inNewMaxSize);
  165. // Reset all control bytes
  166. memset(mControl, cBucketEmpty, mMaxSize + 15);
  167. if (old_data != nullptr)
  168. {
  169. // Copy all elements from the old table
  170. for (size_type i = 0; i < old_max_size; ++i)
  171. if (old_control[i] & cBucketUsed)
  172. {
  173. size_type index;
  174. KeyValue *element = old_data + i;
  175. JPH_IF_ENABLE_ASSERTS(bool inserted =) InsertKey</* InsertAfterGrow= */ true>(HashTableDetail::sGetKey(*element), index);
  176. JPH_ASSERT(inserted);
  177. new (mData + index) KeyValue(std::move(*element));
  178. element->~KeyValue();
  179. }
  180. // Free memory
  181. if constexpr (cNeedsAlignedAllocate)
  182. AlignedFree(old_data);
  183. else
  184. Free(old_data);
  185. }
  186. }
  187. protected:
  188. /// Get an element by index
  189. KeyValue & GetElement(size_type inIndex) const
  190. {
  191. return mData[inIndex];
  192. }
  193. /// Insert a key into the map, returns true if the element was inserted, false if it already existed.
  194. /// outIndex is the index at which the element should be constructed / where it is located.
  195. template <bool InsertAfterGrow = false>
  196. bool InsertKey(const Key &inKey, size_type &outIndex)
  197. {
  198. // Ensure we have enough space
  199. if (mLoadLeft == 0)
  200. {
  201. // Should not be growing if we're already growing!
  202. if constexpr (InsertAfterGrow)
  203. JPH_ASSERT(false);
  204. // Decide if we need to clean up all tombstones or if we need to grow the map
  205. size_type num_deleted = sGetMaxLoad(mMaxSize) - mSize;
  206. if (num_deleted * cMaxDeletedElementsDenominator > mMaxSize * cMaxDeletedElementsNumerator)
  207. rehash(0);
  208. else
  209. {
  210. // Grow by a power of 2
  211. size_type new_max_size = max<size_type>(mMaxSize << 1, 16);
  212. if (new_max_size < mMaxSize)
  213. {
  214. JPH_ASSERT(false, "Overflow in hash table size, can't grow!");
  215. return false;
  216. }
  217. GrowTable(new_max_size);
  218. }
  219. }
  220. // Split hash into index and control value
  221. size_type index;
  222. uint8 control;
  223. GetIndexAndControlValue(inKey, index, control);
  224. // Keeps track of the index of the first deleted bucket we found
  225. constexpr size_type cNoDeleted = ~size_type(0);
  226. size_type first_deleted_index = cNoDeleted;
  227. // Linear probing
  228. KeyEqual equal;
  229. size_type bucket_mask = mMaxSize - 1;
  230. BVec16 control16 = BVec16::sReplicate(control);
  231. BVec16 bucket_empty = BVec16::sZero();
  232. BVec16 bucket_deleted = BVec16::sReplicate(cBucketDeleted);
  233. for (;;)
  234. {
  235. // Read 16 control values (note that we added 15 bytes at the end of the control values that mirror the first 15 bytes)
  236. BVec16 control_bytes = BVec16::sLoadByte16(mControl + index);
  237. // Check if we must find the element before we can insert
  238. if constexpr (!InsertAfterGrow)
  239. {
  240. // Check for the control value we're looking for
  241. // Note that when deleting we can create empty buckets instead of deleted buckets.
  242. // This means we must unconditionally check all buckets in this batch for equality
  243. // (also beyond the first empty bucket).
  244. uint32 control_equal = uint32(BVec16::sEquals(control_bytes, control16).GetTrues());
  245. // Index within the 16 buckets
  246. size_type local_index = index;
  247. // Loop while there's still buckets to process
  248. while (control_equal != 0)
  249. {
  250. // Get the first equal bucket
  251. uint first_equal = CountTrailingZeros(control_equal);
  252. // Skip to the bucket
  253. local_index += first_equal;
  254. // Make sure that our index is not beyond the end of the table
  255. local_index &= bucket_mask;
  256. // We found a bucket with same control value
  257. if (equal(HashTableDetail::sGetKey(mData[local_index]), inKey))
  258. {
  259. // Element already exists
  260. outIndex = local_index;
  261. return false;
  262. }
  263. // Skip past this bucket
  264. control_equal >>= first_equal + 1;
  265. local_index++;
  266. }
  267. // Check if we're still scanning for deleted buckets
  268. if (first_deleted_index == cNoDeleted)
  269. {
  270. // Check if any buckets have been deleted, if so store the first one
  271. uint32 control_deleted = uint32(BVec16::sEquals(control_bytes, bucket_deleted).GetTrues());
  272. if (control_deleted != 0)
  273. first_deleted_index = index + CountTrailingZeros(control_deleted);
  274. }
  275. }
  276. // Check for empty buckets
  277. uint32 control_empty = uint32(BVec16::sEquals(control_bytes, bucket_empty).GetTrues());
  278. if (control_empty != 0)
  279. {
  280. // If we found a deleted bucket, use it.
  281. // It doesn't matter if it is before or after the first empty bucket we found
  282. // since we will always be scanning in batches of 16 buckets.
  283. if (first_deleted_index == cNoDeleted || InsertAfterGrow)
  284. {
  285. index += CountTrailingZeros(control_empty);
  286. --mLoadLeft; // Using an empty bucket decreases the load left
  287. }
  288. else
  289. {
  290. index = first_deleted_index;
  291. }
  292. // Make sure that our index is not beyond the end of the table
  293. index &= bucket_mask;
  294. // Update control byte
  295. SetControlValue(index, control);
  296. ++mSize;
  297. // Return index to newly allocated bucket
  298. outIndex = index;
  299. return true;
  300. }
  301. // Move to next batch of 16 buckets
  302. index = (index + 16) & bucket_mask;
  303. }
  304. }
  305. public:
  306. /// Non-const iterator
  307. class iterator : public IteratorBase<HashTable, iterator>
  308. {
  309. using Base = IteratorBase<HashTable, iterator>;
  310. public:
  311. using IteratorBase<HashTable, iterator>::operator ==;
  312. /// Properties
  313. using reference = typename Base::value_type &;
  314. using pointer = typename Base::value_type *;
  315. /// Constructors
  316. explicit iterator(HashTable *inTable) : Base(inTable) { }
  317. iterator(HashTable *inTable, size_type inIndex) : Base(inTable, inIndex) { }
  318. iterator(const iterator &inIterator) : Base(inIterator) { }
  319. /// Assignment
  320. iterator & operator = (const iterator &inRHS) { Base::operator = (inRHS); return *this; }
  321. using Base::operator *;
  322. /// Non-const access to key value pair
  323. KeyValue & operator * ()
  324. {
  325. JPH_ASSERT(this->IsValid());
  326. return this->mTable->mData[this->mIndex];
  327. }
  328. using Base::operator ->;
  329. /// Non-const access to key value pair
  330. KeyValue * operator -> ()
  331. {
  332. JPH_ASSERT(this->IsValid());
  333. return this->mTable->mData + this->mIndex;
  334. }
  335. };
  336. /// Const iterator
  337. class const_iterator : public IteratorBase<const HashTable, const_iterator>
  338. {
  339. using Base = IteratorBase<const HashTable, const_iterator>;
  340. public:
  341. using IteratorBase<const HashTable, const_iterator>::operator ==;
  342. /// Properties
  343. using reference = const typename Base::value_type &;
  344. using pointer = const typename Base::value_type *;
  345. /// Constructors
  346. explicit const_iterator(const HashTable *inTable) : Base(inTable) { }
  347. const_iterator(const HashTable *inTable, size_type inIndex) : Base(inTable, inIndex) { }
  348. const_iterator(const const_iterator &inRHS) : Base(inRHS) { }
  349. const_iterator(const iterator &inIterator) : Base(inIterator.mTable, inIterator.mIndex) { }
  350. /// Assignment
  351. const_iterator & operator = (const iterator &inRHS) { this->mTable = inRHS.mTable; this->mIndex = inRHS.mIndex; return *this; }
  352. const_iterator & operator = (const const_iterator &inRHS) { Base::operator = (inRHS); return *this; }
  353. };
  354. /// Default constructor
  355. HashTable() = default;
  356. /// Copy constructor
  357. HashTable(const HashTable &inRHS)
  358. {
  359. CopyTable(inRHS);
  360. }
  361. /// Move constructor
  362. HashTable(HashTable &&ioRHS) noexcept :
  363. mData(ioRHS.mData),
  364. mControl(ioRHS.mControl),
  365. mSize(ioRHS.mSize),
  366. mMaxSize(ioRHS.mMaxSize),
  367. mLoadLeft(ioRHS.mLoadLeft)
  368. {
  369. ioRHS.mData = nullptr;
  370. ioRHS.mControl = nullptr;
  371. ioRHS.mSize = 0;
  372. ioRHS.mMaxSize = 0;
  373. ioRHS.mLoadLeft = 0;
  374. }
  375. /// Assignment operator
  376. HashTable & operator = (const HashTable &inRHS)
  377. {
  378. if (this != &inRHS)
  379. {
  380. clear();
  381. CopyTable(inRHS);
  382. }
  383. return *this;
  384. }
  385. /// Move assignment operator
  386. HashTable & operator = (HashTable &&ioRHS) noexcept
  387. {
  388. if (this != &ioRHS)
  389. {
  390. clear();
  391. mData = ioRHS.mData;
  392. mControl = ioRHS.mControl;
  393. mSize = ioRHS.mSize;
  394. mMaxSize = ioRHS.mMaxSize;
  395. mLoadLeft = ioRHS.mLoadLeft;
  396. ioRHS.mData = nullptr;
  397. ioRHS.mControl = nullptr;
  398. ioRHS.mSize = 0;
  399. ioRHS.mMaxSize = 0;
  400. ioRHS.mLoadLeft = 0;
  401. }
  402. return *this;
  403. }
  404. /// Destructor
  405. ~HashTable()
  406. {
  407. clear();
  408. }
  409. /// Reserve memory for a certain number of elements
  410. void reserve(size_type inMaxSize)
  411. {
  412. // Calculate max size based on load factor
  413. size_type max_size = GetNextPowerOf2(max<uint32>((cMaxLoadFactorDenominator * inMaxSize) / cMaxLoadFactorNumerator, 16));
  414. if (max_size <= mMaxSize)
  415. return;
  416. GrowTable(max_size);
  417. }
  418. /// Destroy the entire hash table
  419. void clear()
  420. {
  421. // Delete all elements
  422. if constexpr (!std::is_trivially_destructible<KeyValue>())
  423. if (!empty())
  424. for (size_type i = 0; i < mMaxSize; ++i)
  425. if (mControl[i] & cBucketUsed)
  426. mData[i].~KeyValue();
  427. if (mData != nullptr)
  428. {
  429. // Free memory
  430. if constexpr (cNeedsAlignedAllocate)
  431. AlignedFree(mData);
  432. else
  433. Free(mData);
  434. // Reset members
  435. mData = nullptr;
  436. mControl = nullptr;
  437. mSize = 0;
  438. mMaxSize = 0;
  439. mLoadLeft = 0;
  440. }
  441. }
  442. /// Destroy the entire hash table but keeps the memory allocated
  443. void ClearAndKeepMemory()
  444. {
  445. // Destruct elements
  446. if constexpr (!std::is_trivially_destructible<KeyValue>())
  447. if (!empty())
  448. for (size_type i = 0; i < mMaxSize; ++i)
  449. if (mControl[i] & cBucketUsed)
  450. mData[i].~KeyValue();
  451. mSize = 0;
  452. // If there are elements that are not marked cBucketEmpty, we reset them
  453. size_type max_load = sGetMaxLoad(mMaxSize);
  454. if (mLoadLeft != max_load)
  455. {
  456. // Reset all control bytes
  457. memset(mControl, cBucketEmpty, mMaxSize + 15);
  458. mLoadLeft = max_load;
  459. }
  460. }
  461. /// Iterator to first element
  462. iterator begin()
  463. {
  464. return iterator(this);
  465. }
  466. /// Iterator to one beyond last element
  467. iterator end()
  468. {
  469. return iterator(this, mMaxSize);
  470. }
  471. /// Iterator to first element
  472. const_iterator begin() const
  473. {
  474. return const_iterator(this);
  475. }
  476. /// Iterator to one beyond last element
  477. const_iterator end() const
  478. {
  479. return const_iterator(this, mMaxSize);
  480. }
  481. /// Iterator to first element
  482. const_iterator cbegin() const
  483. {
  484. return const_iterator(this);
  485. }
  486. /// Iterator to one beyond last element
  487. const_iterator cend() const
  488. {
  489. return const_iterator(this, mMaxSize);
  490. }
  491. /// Number of buckets in the table
  492. size_type bucket_count() const
  493. {
  494. return mMaxSize;
  495. }
  496. /// Max number of buckets that the table can have
  497. constexpr size_type max_bucket_count() const
  498. {
  499. return size_type(1) << (sizeof(size_type) * 8 - 1);
  500. }
  501. /// Check if there are no elements in the table
  502. bool empty() const
  503. {
  504. return mSize == 0;
  505. }
  506. /// Number of elements in the table
  507. size_type size() const
  508. {
  509. return mSize;
  510. }
  511. /// Max number of elements that the table can hold
  512. constexpr size_type max_size() const
  513. {
  514. return size_type((uint64(max_bucket_count()) * cMaxLoadFactorNumerator) / cMaxLoadFactorDenominator);
  515. }
  516. /// Get the max load factor for this table (max number of elements / number of buckets)
  517. constexpr float max_load_factor() const
  518. {
  519. return float(cMaxLoadFactorNumerator) / float(cMaxLoadFactorDenominator);
  520. }
  521. /// Insert a new element, returns iterator and if the element was inserted
  522. std::pair<iterator, bool> insert(const value_type &inValue)
  523. {
  524. size_type index;
  525. bool inserted = InsertKey(HashTableDetail::sGetKey(inValue), index);
  526. if (inserted)
  527. new (mData + index) KeyValue(inValue);
  528. return std::make_pair(iterator(this, index), inserted);
  529. }
  530. /// Find an element, returns iterator to element or end() if not found
  531. const_iterator find(const Key &inKey) const
  532. {
  533. // Check if we have any data
  534. if (empty())
  535. return cend();
  536. // Split hash into index and control value
  537. size_type index;
  538. uint8 control;
  539. GetIndexAndControlValue(inKey, index, control);
  540. // Linear probing
  541. KeyEqual equal;
  542. size_type bucket_mask = mMaxSize - 1;
  543. BVec16 control16 = BVec16::sReplicate(control);
  544. BVec16 bucket_empty = BVec16::sZero();
  545. for (;;)
  546. {
  547. // Read 16 control values
  548. // (note that we added 15 bytes at the end of the control values that mirror the first 15 bytes)
  549. BVec16 control_bytes = BVec16::sLoadByte16(mControl + index);
  550. // Check for the control value we're looking for
  551. // Note that when deleting we can create empty buckets instead of deleted buckets.
  552. // This means we must unconditionally check all buckets in this batch for equality
  553. // (also beyond the first empty bucket).
  554. uint32 control_equal = uint32(BVec16::sEquals(control_bytes, control16).GetTrues());
  555. // Index within the 16 buckets
  556. size_type local_index = index;
  557. // Loop while there's still buckets to process
  558. while (control_equal != 0)
  559. {
  560. // Get the first equal bucket
  561. uint first_equal = CountTrailingZeros(control_equal);
  562. // Skip to the bucket
  563. local_index += first_equal;
  564. // Make sure that our index is not beyond the end of the table
  565. local_index &= bucket_mask;
  566. // We found a bucket with same control value
  567. if (equal(HashTableDetail::sGetKey(mData[local_index]), inKey))
  568. {
  569. // Element found
  570. return const_iterator(this, local_index);
  571. }
  572. // Skip past this bucket
  573. control_equal >>= first_equal + 1;
  574. local_index++;
  575. }
  576. // Check for empty buckets
  577. uint32 control_empty = uint32(BVec16::sEquals(control_bytes, bucket_empty).GetTrues());
  578. if (control_empty != 0)
  579. {
  580. // An empty bucket was found, we didn't find the element
  581. return cend();
  582. }
  583. // Move to next batch of 16 buckets
  584. index = (index + 16) & bucket_mask;
  585. }
  586. }
  587. /// @brief Erase an element by iterator
  588. void erase(const const_iterator &inIterator)
  589. {
  590. JPH_ASSERT(inIterator.IsValid());
  591. // Read 16 control values before and after the current index
  592. // (note that we added 15 bytes at the end of the control values that mirror the first 15 bytes)
  593. BVec16 control_bytes_before = BVec16::sLoadByte16(mControl + ((inIterator.mIndex - 16) & (mMaxSize - 1)));
  594. BVec16 control_bytes_after = BVec16::sLoadByte16(mControl + inIterator.mIndex);
  595. BVec16 bucket_empty = BVec16::sZero();
  596. uint32 control_empty_before = uint32(BVec16::sEquals(control_bytes_before, bucket_empty).GetTrues());
  597. uint32 control_empty_after = uint32(BVec16::sEquals(control_bytes_after, bucket_empty).GetTrues());
  598. // If (this index including) there exist 16 consecutive non-empty slots (represented by a bit being 0) then
  599. // a probe looking for some element needs to continue probing so we cannot mark the bucket as empty
  600. // but must mark it as deleted instead.
  601. // Note that we use: CountLeadingZeros(uint16) = CountLeadingZeros(uint32) - 16.
  602. uint8 control_value = CountLeadingZeros(control_empty_before) - 16 + CountTrailingZeros(control_empty_after) < 16? cBucketEmpty : cBucketDeleted;
  603. // Mark the bucket as empty/deleted
  604. SetControlValue(inIterator.mIndex, control_value);
  605. // Destruct the element
  606. mData[inIterator.mIndex].~KeyValue();
  607. // If we marked the bucket as empty we can increase the load left
  608. if (control_value == cBucketEmpty)
  609. ++mLoadLeft;
  610. // Decrease size
  611. --mSize;
  612. }
  613. /// @brief Erase an element by key
  614. size_type erase(const Key &inKey)
  615. {
  616. const_iterator it = find(inKey);
  617. if (it == cend())
  618. return 0;
  619. erase(it);
  620. return 1;
  621. }
  622. /// Swap the contents of two hash tables
  623. void swap(HashTable &ioRHS) noexcept
  624. {
  625. std::swap(mData, ioRHS.mData);
  626. std::swap(mControl, ioRHS.mControl);
  627. std::swap(mSize, ioRHS.mSize);
  628. std::swap(mMaxSize, ioRHS.mMaxSize);
  629. std::swap(mLoadLeft, ioRHS.mLoadLeft);
  630. }
  631. /// In place re-hashing of all elements in the table. Removes all cBucketDeleted elements
  632. /// The std version takes a bucket count, but we just re-hash to the same size.
  633. void rehash(size_type)
  634. {
  635. // Update the control value for all buckets
  636. for (size_type i = 0; i < mMaxSize; ++i)
  637. {
  638. uint8 &control = mControl[i];
  639. switch (control)
  640. {
  641. case cBucketDeleted:
  642. // Deleted buckets become empty
  643. control = cBucketEmpty;
  644. break;
  645. case cBucketEmpty:
  646. // Remains empty
  647. break;
  648. default:
  649. // Mark all occupied as deleted, to indicate it needs to move to the correct place
  650. control = cBucketDeleted;
  651. break;
  652. }
  653. }
  654. // Replicate control values to the last 15 entries
  655. for (size_type i = 0; i < 15; ++i)
  656. mControl[mMaxSize + i] = mControl[i];
  657. // Loop over all elements that have been 'deleted' and move them to their new spot
  658. BVec16 bucket_used = BVec16::sReplicate(cBucketUsed);
  659. size_type bucket_mask = mMaxSize - 1;
  660. uint32 probe_mask = bucket_mask & ~uint32(0b1111); // Mask out lower 4 bits because we test 16 buckets at a time
  661. for (size_type src = 0; src < mMaxSize; ++src)
  662. if (mControl[src] == cBucketDeleted)
  663. for (;;)
  664. {
  665. // Split hash into index and control value
  666. size_type src_index;
  667. uint8 src_control;
  668. GetIndexAndControlValue(HashTableDetail::sGetKey(mData[src]), src_index, src_control);
  669. // Linear probing
  670. size_type dst = src_index;
  671. for (;;)
  672. {
  673. // Check if any buckets are free
  674. BVec16 control_bytes = BVec16::sLoadByte16(mControl + dst);
  675. uint32 control_free = uint32(BVec16::sAnd(control_bytes, bucket_used).GetTrues()) ^ 0xffff;
  676. if (control_free != 0)
  677. {
  678. // Select this bucket as destination
  679. dst += CountTrailingZeros(control_free);
  680. dst &= bucket_mask;
  681. break;
  682. }
  683. // Move to next batch of 16 buckets
  684. dst = (dst + 16) & bucket_mask;
  685. }
  686. // Check if we stay in the same probe group
  687. if (((dst - src_index) & probe_mask) == ((src - src_index) & probe_mask))
  688. {
  689. // We stay in the same group, we can stay where we are
  690. SetControlValue(src, src_control);
  691. break;
  692. }
  693. else if (mControl[dst] == cBucketEmpty)
  694. {
  695. // There's an empty bucket, move us there
  696. SetControlValue(dst, src_control);
  697. SetControlValue(src, cBucketEmpty);
  698. new (mData + dst) KeyValue(std::move(mData[src]));
  699. mData[src].~KeyValue();
  700. break;
  701. }
  702. else
  703. {
  704. // There's an element in the bucket we want to move to, swap them
  705. JPH_ASSERT(mControl[dst] == cBucketDeleted);
  706. SetControlValue(dst, src_control);
  707. std::swap(mData[src], mData[dst]);
  708. // Iterate again with the same source bucket
  709. }
  710. }
  711. // Reinitialize load left
  712. mLoadLeft = sGetMaxLoad(mMaxSize) - mSize;
  713. }
  714. private:
  715. /// If this allocator needs to fall back to aligned allocations because the type requires it
  716. static constexpr bool cNeedsAlignedAllocate = alignof(KeyValue) > JPH_DEFAULT_ALLOCATE_ALIGNMENT;
  717. /// Max load factor is cMaxLoadFactorNumerator / cMaxLoadFactorDenominator
  718. static constexpr uint64 cMaxLoadFactorNumerator = 7;
  719. static constexpr uint64 cMaxLoadFactorDenominator = 8;
  720. /// If we can recover this fraction of deleted elements, we'll reshuffle the buckets in place rather than growing the table
  721. static constexpr uint64 cMaxDeletedElementsNumerator = 1;
  722. static constexpr uint64 cMaxDeletedElementsDenominator = 8;
  723. /// Values that the control bytes can have
  724. static constexpr uint8 cBucketEmpty = 0;
  725. static constexpr uint8 cBucketDeleted = 0x7f;
  726. static constexpr uint8 cBucketUsed = 0x80; // Lowest 7 bits are lowest 7 bits of the hash value
  727. /// The buckets, an array of size mMaxSize
  728. KeyValue * mData = nullptr;
  729. /// Control bytes, an array of size mMaxSize + 15
  730. uint8 * mControl = nullptr;
  731. /// Number of elements in the table
  732. size_type mSize = 0;
  733. /// Max number of elements that can be stored in the table
  734. size_type mMaxSize = 0;
  735. /// Number of elements we can add to the table before we need to grow
  736. size_type mLoadLeft = 0;
  737. };
  738. JPH_NAMESPACE_END