HashTable.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. /// Properties
  312. using reference = typename Base::value_type &;
  313. using pointer = typename Base::value_type *;
  314. /// Constructors
  315. explicit iterator(HashTable *inTable) : Base(inTable) { }
  316. iterator(HashTable *inTable, size_type inIndex) : Base(inTable, inIndex) { }
  317. iterator(const iterator &inIterator) : Base(inIterator) { }
  318. /// Assignment
  319. iterator & operator = (const iterator &inRHS) { Base::operator = (inRHS); return *this; }
  320. using Base::operator *;
  321. /// Non-const access to key value pair
  322. KeyValue & operator * ()
  323. {
  324. JPH_ASSERT(this->IsValid());
  325. return this->mTable->mData[this->mIndex];
  326. }
  327. using Base::operator ->;
  328. /// Non-const access to key value pair
  329. KeyValue * operator -> ()
  330. {
  331. JPH_ASSERT(this->IsValid());
  332. return this->mTable->mData + this->mIndex;
  333. }
  334. };
  335. /// Const iterator
  336. class const_iterator : public IteratorBase<const HashTable, const_iterator>
  337. {
  338. using Base = IteratorBase<const HashTable, const_iterator>;
  339. public:
  340. /// Properties
  341. using reference = const typename Base::value_type &;
  342. using pointer = const typename Base::value_type *;
  343. /// Constructors
  344. explicit const_iterator(const HashTable *inTable) : Base(inTable) { }
  345. const_iterator(const HashTable *inTable, size_type inIndex) : Base(inTable, inIndex) { }
  346. const_iterator(const const_iterator &inRHS) : Base(inRHS) { }
  347. const_iterator(const iterator &inIterator) : Base(inIterator.mTable, inIterator.mIndex) { }
  348. /// Assignment
  349. const_iterator & operator = (const iterator &inRHS) { this->mTable = inRHS.mTable; this->mIndex = inRHS.mIndex; return *this; }
  350. const_iterator & operator = (const const_iterator &inRHS) { Base::operator = (inRHS); return *this; }
  351. };
  352. /// Default constructor
  353. HashTable() = default;
  354. /// Copy constructor
  355. HashTable(const HashTable &inRHS)
  356. {
  357. CopyTable(inRHS);
  358. }
  359. /// Move constructor
  360. HashTable(HashTable &&ioRHS) noexcept :
  361. mData(ioRHS.mData),
  362. mControl(ioRHS.mControl),
  363. mSize(ioRHS.mSize),
  364. mMaxSize(ioRHS.mMaxSize),
  365. mLoadLeft(ioRHS.mLoadLeft)
  366. {
  367. ioRHS.mData = nullptr;
  368. ioRHS.mControl = nullptr;
  369. ioRHS.mSize = 0;
  370. ioRHS.mMaxSize = 0;
  371. ioRHS.mLoadLeft = 0;
  372. }
  373. /// Assignment operator
  374. HashTable & operator = (const HashTable &inRHS)
  375. {
  376. if (this != &inRHS)
  377. {
  378. clear();
  379. CopyTable(inRHS);
  380. }
  381. return *this;
  382. }
  383. /// Move assignment operator
  384. HashTable & operator = (HashTable &&ioRHS) noexcept
  385. {
  386. if (this != &ioRHS)
  387. {
  388. clear();
  389. mData = ioRHS.mData;
  390. mControl = ioRHS.mControl;
  391. mSize = ioRHS.mSize;
  392. mMaxSize = ioRHS.mMaxSize;
  393. mLoadLeft = ioRHS.mLoadLeft;
  394. ioRHS.mData = nullptr;
  395. ioRHS.mControl = nullptr;
  396. ioRHS.mSize = 0;
  397. ioRHS.mMaxSize = 0;
  398. ioRHS.mLoadLeft = 0;
  399. }
  400. return *this;
  401. }
  402. /// Destructor
  403. ~HashTable()
  404. {
  405. clear();
  406. }
  407. /// Reserve memory for a certain number of elements
  408. void reserve(size_type inMaxSize)
  409. {
  410. // Calculate max size based on load factor
  411. size_type max_size = GetNextPowerOf2(max<uint32>((cMaxLoadFactorDenominator * inMaxSize) / cMaxLoadFactorNumerator, 16));
  412. if (max_size <= mMaxSize)
  413. return;
  414. GrowTable(max_size);
  415. }
  416. /// Destroy the entire hash table
  417. void clear()
  418. {
  419. // Delete all elements
  420. if constexpr (!std::is_trivially_destructible<KeyValue>())
  421. if (!empty())
  422. for (size_type i = 0; i < mMaxSize; ++i)
  423. if (mControl[i] & cBucketUsed)
  424. mData[i].~KeyValue();
  425. if (mData != nullptr)
  426. {
  427. // Free memory
  428. if constexpr (cNeedsAlignedAllocate)
  429. AlignedFree(mData);
  430. else
  431. Free(mData);
  432. // Reset members
  433. mData = nullptr;
  434. mControl = nullptr;
  435. mSize = 0;
  436. mMaxSize = 0;
  437. mLoadLeft = 0;
  438. }
  439. }
  440. /// Destroy the entire hash table but keeps the memory allocated
  441. void ClearAndKeepMemory()
  442. {
  443. // Destruct elements
  444. if constexpr (!std::is_trivially_destructible<KeyValue>())
  445. if (!empty())
  446. for (size_type i = 0; i < mMaxSize; ++i)
  447. if (mControl[i] & cBucketUsed)
  448. mData[i].~KeyValue();
  449. mSize = 0;
  450. // If there are elements that are not marked cBucketEmpty, we reset them
  451. size_type max_load = sGetMaxLoad(mMaxSize);
  452. if (mLoadLeft != max_load)
  453. {
  454. // Reset all control bytes
  455. memset(mControl, cBucketEmpty, mMaxSize + 15);
  456. mLoadLeft = max_load;
  457. }
  458. }
  459. /// Iterator to first element
  460. iterator begin()
  461. {
  462. return iterator(this);
  463. }
  464. /// Iterator to one beyond last element
  465. iterator end()
  466. {
  467. return iterator(this, mMaxSize);
  468. }
  469. /// Iterator to first element
  470. const_iterator begin() const
  471. {
  472. return const_iterator(this);
  473. }
  474. /// Iterator to one beyond last element
  475. const_iterator end() const
  476. {
  477. return const_iterator(this, mMaxSize);
  478. }
  479. /// Iterator to first element
  480. const_iterator cbegin() const
  481. {
  482. return const_iterator(this);
  483. }
  484. /// Iterator to one beyond last element
  485. const_iterator cend() const
  486. {
  487. return const_iterator(this, mMaxSize);
  488. }
  489. /// Number of buckets in the table
  490. size_type bucket_count() const
  491. {
  492. return mMaxSize;
  493. }
  494. /// Max number of buckets that the table can have
  495. constexpr size_type max_bucket_count() const
  496. {
  497. return size_type(1) << (sizeof(size_type) * 8 - 1);
  498. }
  499. /// Check if there are no elements in the table
  500. bool empty() const
  501. {
  502. return mSize == 0;
  503. }
  504. /// Number of elements in the table
  505. size_type size() const
  506. {
  507. return mSize;
  508. }
  509. /// Max number of elements that the table can hold
  510. constexpr size_type max_size() const
  511. {
  512. return size_type((uint64(max_bucket_count()) * cMaxLoadFactorNumerator) / cMaxLoadFactorDenominator);
  513. }
  514. /// Get the max load factor for this table (max number of elements / number of buckets)
  515. constexpr float max_load_factor() const
  516. {
  517. return float(cMaxLoadFactorNumerator) / float(cMaxLoadFactorDenominator);
  518. }
  519. /// Insert a new element, returns iterator and if the element was inserted
  520. std::pair<iterator, bool> insert(const value_type &inValue)
  521. {
  522. size_type index;
  523. bool inserted = InsertKey(HashTableDetail::sGetKey(inValue), index);
  524. if (inserted)
  525. new (mData + index) KeyValue(inValue);
  526. return std::make_pair(iterator(this, index), inserted);
  527. }
  528. /// Find an element, returns iterator to element or end() if not found
  529. const_iterator find(const Key &inKey) const
  530. {
  531. // Check if we have any data
  532. if (empty())
  533. return cend();
  534. // Split hash into index and control value
  535. size_type index;
  536. uint8 control;
  537. GetIndexAndControlValue(inKey, index, control);
  538. // Linear probing
  539. KeyEqual equal;
  540. size_type bucket_mask = mMaxSize - 1;
  541. BVec16 control16 = BVec16::sReplicate(control);
  542. BVec16 bucket_empty = BVec16::sZero();
  543. for (;;)
  544. {
  545. // Read 16 control values
  546. // (note that we added 15 bytes at the end of the control values that mirror the first 15 bytes)
  547. BVec16 control_bytes = BVec16::sLoadByte16(mControl + index);
  548. // Check for the control value we're looking for
  549. // Note that when deleting we can create empty buckets instead of deleted buckets.
  550. // This means we must unconditionally check all buckets in this batch for equality
  551. // (also beyond the first empty bucket).
  552. uint32 control_equal = uint32(BVec16::sEquals(control_bytes, control16).GetTrues());
  553. // Index within the 16 buckets
  554. size_type local_index = index;
  555. // Loop while there's still buckets to process
  556. while (control_equal != 0)
  557. {
  558. // Get the first equal bucket
  559. uint first_equal = CountTrailingZeros(control_equal);
  560. // Skip to the bucket
  561. local_index += first_equal;
  562. // Make sure that our index is not beyond the end of the table
  563. local_index &= bucket_mask;
  564. // We found a bucket with same control value
  565. if (equal(HashTableDetail::sGetKey(mData[local_index]), inKey))
  566. {
  567. // Element found
  568. return const_iterator(this, local_index);
  569. }
  570. // Skip past this bucket
  571. control_equal >>= first_equal + 1;
  572. local_index++;
  573. }
  574. // Check for empty buckets
  575. uint32 control_empty = uint32(BVec16::sEquals(control_bytes, bucket_empty).GetTrues());
  576. if (control_empty != 0)
  577. {
  578. // An empty bucket was found, we didn't find the element
  579. return cend();
  580. }
  581. // Move to next batch of 16 buckets
  582. index = (index + 16) & bucket_mask;
  583. }
  584. }
  585. /// @brief Erase an element by iterator
  586. void erase(const const_iterator &inIterator)
  587. {
  588. JPH_ASSERT(inIterator.IsValid());
  589. // Read 16 control values before and after the current index
  590. // (note that we added 15 bytes at the end of the control values that mirror the first 15 bytes)
  591. BVec16 control_bytes_before = BVec16::sLoadByte16(mControl + ((inIterator.mIndex - 16) & (mMaxSize - 1)));
  592. BVec16 control_bytes_after = BVec16::sLoadByte16(mControl + inIterator.mIndex);
  593. BVec16 bucket_empty = BVec16::sZero();
  594. uint32 control_empty_before = uint32(BVec16::sEquals(control_bytes_before, bucket_empty).GetTrues());
  595. uint32 control_empty_after = uint32(BVec16::sEquals(control_bytes_after, bucket_empty).GetTrues());
  596. // If (this index including) there exist 16 consecutive non-empty slots (represented by a bit being 0) then
  597. // a probe looking for some element needs to continue probing so we cannot mark the bucket as empty
  598. // but must mark it as deleted instead.
  599. // Note that we use: CountLeadingZeros(uint16) = CountLeadingZeros(uint32) - 16.
  600. uint8 control_value = CountLeadingZeros(control_empty_before) - 16 + CountTrailingZeros(control_empty_after) < 16? cBucketEmpty : cBucketDeleted;
  601. // Mark the bucket as empty/deleted
  602. SetControlValue(inIterator.mIndex, control_value);
  603. // Destruct the element
  604. mData[inIterator.mIndex].~KeyValue();
  605. // If we marked the bucket as empty we can increase the load left
  606. if (control_value == cBucketEmpty)
  607. ++mLoadLeft;
  608. // Decrease size
  609. --mSize;
  610. }
  611. /// @brief Erase an element by key
  612. size_type erase(const Key &inKey)
  613. {
  614. const_iterator it = find(inKey);
  615. if (it == cend())
  616. return 0;
  617. erase(it);
  618. return 1;
  619. }
  620. /// Swap the contents of two hash tables
  621. void swap(HashTable &ioRHS) noexcept
  622. {
  623. std::swap(mData, ioRHS.mData);
  624. std::swap(mControl, ioRHS.mControl);
  625. std::swap(mSize, ioRHS.mSize);
  626. std::swap(mMaxSize, ioRHS.mMaxSize);
  627. std::swap(mLoadLeft, ioRHS.mLoadLeft);
  628. }
  629. /// In place re-hashing of all elements in the table. Removes all cBucketDeleted elements
  630. /// The std version takes a bucket count, but we just re-hash to the same size.
  631. void rehash(size_type)
  632. {
  633. // Update the control value for all buckets
  634. for (size_type i = 0; i < mMaxSize; ++i)
  635. {
  636. uint8 &control = mControl[i];
  637. switch (control)
  638. {
  639. case cBucketDeleted:
  640. // Deleted buckets become empty
  641. control = cBucketEmpty;
  642. break;
  643. case cBucketEmpty:
  644. // Remains empty
  645. break;
  646. default:
  647. // Mark all occupied as deleted, to indicate it needs to move to the correct place
  648. control = cBucketDeleted;
  649. break;
  650. }
  651. }
  652. // Replicate control values to the last 15 entries
  653. for (size_type i = 0; i < 15; ++i)
  654. mControl[mMaxSize + i] = mControl[i];
  655. // Loop over all elements that have been 'deleted' and move them to their new spot
  656. BVec16 bucket_used = BVec16::sReplicate(cBucketUsed);
  657. size_type bucket_mask = mMaxSize - 1;
  658. uint32 probe_mask = bucket_mask & ~uint32(0b1111); // Mask out lower 4 bits because we test 16 buckets at a time
  659. for (size_type src = 0; src < mMaxSize; ++src)
  660. if (mControl[src] == cBucketDeleted)
  661. for (;;)
  662. {
  663. // Split hash into index and control value
  664. size_type src_index;
  665. uint8 src_control;
  666. GetIndexAndControlValue(HashTableDetail::sGetKey(mData[src]), src_index, src_control);
  667. // Linear probing
  668. size_type dst = src_index;
  669. for (;;)
  670. {
  671. // Check if any buckets are free
  672. BVec16 control_bytes = BVec16::sLoadByte16(mControl + dst);
  673. uint32 control_free = uint32(BVec16::sAnd(control_bytes, bucket_used).GetTrues()) ^ 0xffff;
  674. if (control_free != 0)
  675. {
  676. // Select this bucket as destination
  677. dst += CountTrailingZeros(control_free);
  678. dst &= bucket_mask;
  679. break;
  680. }
  681. // Move to next batch of 16 buckets
  682. dst = (dst + 16) & bucket_mask;
  683. }
  684. // Check if we stay in the same probe group
  685. if (((dst - src_index) & probe_mask) == ((src - src_index) & probe_mask))
  686. {
  687. // We stay in the same group, we can stay where we are
  688. SetControlValue(src, src_control);
  689. break;
  690. }
  691. else if (mControl[dst] == cBucketEmpty)
  692. {
  693. // There's an empty bucket, move us there
  694. SetControlValue(dst, src_control);
  695. SetControlValue(src, cBucketEmpty);
  696. new (mData + dst) KeyValue(std::move(mData[src]));
  697. mData[src].~KeyValue();
  698. break;
  699. }
  700. else
  701. {
  702. // There's an element in the bucket we want to move to, swap them
  703. JPH_ASSERT(mControl[dst] == cBucketDeleted);
  704. SetControlValue(dst, src_control);
  705. std::swap(mData[src], mData[dst]);
  706. // Iterate again with the same source bucket
  707. }
  708. }
  709. // Reinitialize load left
  710. mLoadLeft = sGetMaxLoad(mMaxSize) - mSize;
  711. }
  712. private:
  713. /// If this allocator needs to fall back to aligned allocations because the type requires it
  714. static constexpr bool cNeedsAlignedAllocate = alignof(KeyValue) > (JPH_CPU_ADDRESS_BITS == 32? 8 : 16);
  715. /// Max load factor is cMaxLoadFactorNumerator / cMaxLoadFactorDenominator
  716. static constexpr uint64 cMaxLoadFactorNumerator = 7;
  717. static constexpr uint64 cMaxLoadFactorDenominator = 8;
  718. /// If we can recover this fraction of deleted elements, we'll reshuffle the buckets in place rather than growing the table
  719. static constexpr uint64 cMaxDeletedElementsNumerator = 1;
  720. static constexpr uint64 cMaxDeletedElementsDenominator = 8;
  721. /// Values that the control bytes can have
  722. static constexpr uint8 cBucketEmpty = 0;
  723. static constexpr uint8 cBucketDeleted = 0x7f;
  724. static constexpr uint8 cBucketUsed = 0x80; // Lowest 7 bits are lowest 7 bits of the hash value
  725. /// The buckets, an array of size mMaxSize
  726. KeyValue * mData = nullptr;
  727. /// Control bytes, an array of size mMaxSize + 15
  728. uint8 * mControl = nullptr;
  729. /// Number of elements in the table
  730. size_type mSize = 0;
  731. /// Max number of elements that can be stored in the table
  732. size_type mMaxSize = 0;
  733. /// Number of elements we can add to the table before we need to grow
  734. size_type mLoadLeft = 0;
  735. };
  736. JPH_NAMESPACE_END