HashSet.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Container/HashBase.h"
  5. #include "../Container/Sort.h"
  6. #include "../Math/MathDefs.h"
  7. #include <cassert>
  8. #include <initializer_list>
  9. namespace Urho3D
  10. {
  11. /// Hash set template class.
  12. template <class T> class HashSet : public HashBase
  13. {
  14. public:
  15. /// Hash set node.
  16. struct Node : public HashNodeBase
  17. {
  18. /// Construct undefined.
  19. Node() = default;
  20. /// Construct with key.
  21. explicit Node(const T& key) :
  22. key_(key)
  23. {
  24. }
  25. /// Key.
  26. T key_;
  27. /// Return next node.
  28. Node* Next() const { return static_cast<Node*>(next_); }
  29. /// Return previous node.
  30. Node* Prev() const { return static_cast<Node*>(prev_); }
  31. /// Return next node in the bucket.
  32. Node* Down() const { return static_cast<Node*>(down_); }
  33. };
  34. /// Hash set node iterator.
  35. struct Iterator : public HashIteratorBase
  36. {
  37. /// Construct.
  38. Iterator() = default;
  39. /// Construct with a node pointer.
  40. explicit Iterator(Node* ptr) :
  41. HashIteratorBase(ptr)
  42. {
  43. }
  44. /// Preincrement the pointer.
  45. Iterator& operator ++()
  46. {
  47. GotoNext();
  48. return *this;
  49. }
  50. /// Postincrement the pointer.
  51. Iterator operator ++(int)
  52. {
  53. Iterator it = *this;
  54. GotoNext();
  55. return it;
  56. }
  57. /// Predecrement the pointer.
  58. Iterator& operator --()
  59. {
  60. GotoPrev();
  61. return *this;
  62. }
  63. /// Postdecrement the pointer.
  64. Iterator operator --(int)
  65. {
  66. Iterator it = *this;
  67. GotoPrev();
  68. return it;
  69. }
  70. /// Point to the key.
  71. const T* operator ->() const { return &(static_cast<Node*>(ptr_))->key_; }
  72. /// Dereference the key.
  73. const T& operator *() const { return (static_cast<Node*>(ptr_))->key_; }
  74. };
  75. /// Hash set node const iterator.
  76. struct ConstIterator : public HashIteratorBase
  77. {
  78. /// Construct.
  79. ConstIterator() = default;
  80. /// Construct with a node pointer.
  81. explicit ConstIterator(Node* ptr) :
  82. HashIteratorBase(ptr)
  83. {
  84. }
  85. /// Construct from a non-const iterator.
  86. ConstIterator(const Iterator& rhs) : // NOLINT(google-explicit-constructor)
  87. HashIteratorBase(rhs.ptr_)
  88. {
  89. }
  90. /// Assign from a non-const iterator.
  91. ConstIterator& operator =(const Iterator& rhs)
  92. {
  93. ptr_ = rhs.ptr_;
  94. return *this;
  95. }
  96. /// Preincrement the pointer.
  97. ConstIterator& operator ++()
  98. {
  99. GotoNext();
  100. return *this;
  101. }
  102. /// Postincrement the pointer.
  103. ConstIterator operator ++(int)
  104. {
  105. ConstIterator it = *this;
  106. GotoNext();
  107. return it;
  108. }
  109. /// Predecrement the pointer.
  110. ConstIterator& operator --()
  111. {
  112. GotoPrev();
  113. return *this;
  114. }
  115. /// Postdecrement the pointer.
  116. ConstIterator operator --(int)
  117. {
  118. ConstIterator it = *this;
  119. GotoPrev();
  120. return it;
  121. }
  122. /// Point to the key.
  123. const T* operator ->() const { return &(static_cast<Node*>(ptr_))->key_; }
  124. /// Dereference the key.
  125. const T& operator *() const { return (static_cast<Node*>(ptr_))->key_; }
  126. };
  127. /// Construct empty.
  128. HashSet()
  129. {
  130. // Reserve the tail node
  131. allocator_ = AllocatorInitialize((i32)sizeof(Node));
  132. head_ = tail_ = ReserveNode();
  133. }
  134. /// Construct from another hash set.
  135. HashSet(const HashSet<T>& set)
  136. {
  137. // Reserve the tail node + initial capacity according to the set's size
  138. allocator_ = AllocatorInitialize((i32)sizeof(Node), set.Size() + 1);
  139. head_ = tail_ = ReserveNode();
  140. *this = set;
  141. }
  142. /// Move-construct from another hash set.
  143. HashSet(HashSet<T>&& set) noexcept
  144. {
  145. Swap(set);
  146. }
  147. /// Aggregate initialization constructor.
  148. HashSet(const std::initializer_list<T>& list) : HashSet()
  149. {
  150. for (auto it = list.begin(); it != list.end(); it++)
  151. {
  152. Insert(*it);
  153. }
  154. }
  155. /// Destruct.
  156. ~HashSet()
  157. {
  158. if (allocator_)
  159. {
  160. Clear();
  161. FreeNode(Tail());
  162. AllocatorUninitialize(allocator_);
  163. delete[] ptrs_;
  164. }
  165. }
  166. /// Assign a hash set.
  167. HashSet& operator =(const HashSet<T>& rhs)
  168. {
  169. // In case of self-assignment do nothing
  170. if (&rhs != this)
  171. {
  172. Clear();
  173. Insert(rhs);
  174. }
  175. return *this;
  176. }
  177. /// Move-assign a hash set.
  178. HashSet& operator =(HashSet<T>&& rhs) noexcept
  179. {
  180. Swap(rhs);
  181. return *this;
  182. }
  183. /// Add-assign a value.
  184. HashSet& operator +=(const T& rhs)
  185. {
  186. Insert(rhs);
  187. return *this;
  188. }
  189. /// Add-assign a hash set.
  190. HashSet& operator +=(const HashSet<T>& rhs)
  191. {
  192. Insert(rhs);
  193. return *this;
  194. }
  195. /// Test for equality with another hash set.
  196. bool operator ==(const HashSet<T>& rhs) const
  197. {
  198. if (rhs.Size() != Size())
  199. return false;
  200. ConstIterator it = Begin();
  201. while (it != End())
  202. {
  203. if (!rhs.Contains(*it))
  204. return false;
  205. ++it;
  206. }
  207. return true;
  208. }
  209. /// Test for inequality with another hash set.
  210. bool operator !=(const HashSet<T>& rhs) const
  211. {
  212. if (rhs.Size() != Size())
  213. return true;
  214. ConstIterator it = Begin();
  215. while (it != End())
  216. {
  217. if (!rhs.Contains(*it))
  218. return true;
  219. ++it;
  220. }
  221. return false;
  222. }
  223. /// Insert a key. Return an iterator to it.
  224. Iterator Insert(const T& key)
  225. {
  226. // If no pointers yet, allocate with minimum bucket count
  227. if (!ptrs_)
  228. {
  229. AllocateBuckets(Size(), MIN_BUCKETS);
  230. Rehash();
  231. }
  232. unsigned hashKey = Hash(key);
  233. Node* existing = FindNode(key, hashKey);
  234. if (existing)
  235. return Iterator(existing);
  236. Node* newNode = InsertNode(Tail(), key);
  237. newNode->down_ = Ptrs()[hashKey];
  238. Ptrs()[hashKey] = newNode;
  239. // Rehash if the maximum load factor has been exceeded
  240. if (Size() > NumBuckets() * MAX_LOAD_FACTOR)
  241. {
  242. AllocateBuckets(Size(), NumBuckets() << 1);
  243. Rehash();
  244. }
  245. return Iterator(newNode);
  246. }
  247. /// Insert a key. Return an iterator and set exists flag according to whether the key already existed.
  248. Iterator Insert(const T& key, bool& exists)
  249. {
  250. i32 oldSize = Size();
  251. Iterator ret = Insert(key);
  252. exists = (Size() == oldSize);
  253. return ret;
  254. }
  255. /// Insert a set.
  256. void Insert(const HashSet<T>& set)
  257. {
  258. ConstIterator it = set.Begin();
  259. ConstIterator end = set.End();
  260. while (it != end)
  261. Insert(*it++);
  262. }
  263. /// Insert a key by iterator. Return iterator to the value.
  264. Iterator Insert(const ConstIterator& it)
  265. {
  266. return Iterator(InsertNode(*it));
  267. }
  268. /// Erase a key. Return true if was found.
  269. bool Erase(const T& key)
  270. {
  271. if (!ptrs_)
  272. return false;
  273. unsigned hashKey = Hash(key);
  274. Node* previous;
  275. Node* node = FindNode(key, hashKey, previous);
  276. if (!node)
  277. return false;
  278. if (previous)
  279. previous->down_ = node->down_;
  280. else
  281. Ptrs()[hashKey] = node->down_;
  282. EraseNode(node);
  283. return true;
  284. }
  285. /// Erase a key by iterator. Return iterator to the next key.
  286. Iterator Erase(const Iterator& it)
  287. {
  288. if (!ptrs_ || !it.ptr_)
  289. return End();
  290. auto* node = static_cast<Node*>(it.ptr_);
  291. Node* next = node->Next();
  292. unsigned hashKey = Hash(node->key_);
  293. Node* previous = 0;
  294. auto* current = static_cast<Node*>(Ptrs()[hashKey]);
  295. while (current && current != node)
  296. {
  297. previous = current;
  298. current = current->Down();
  299. }
  300. assert(current == node);
  301. if (previous)
  302. previous->down_ = node->down_;
  303. else
  304. Ptrs()[hashKey] = node->down_;
  305. EraseNode(node);
  306. return Iterator(next);
  307. }
  308. /// Clear the set.
  309. void Clear()
  310. {
  311. // Prevent Find() from returning anything while the map is being cleared
  312. ResetPtrs();
  313. if (Size())
  314. {
  315. for (Iterator i = Begin(); i != End();)
  316. {
  317. FreeNode(static_cast<Node*>(i++.ptr_));
  318. i.ptr_->prev_ = 0;
  319. }
  320. head_ = tail_;
  321. SetSize(0);
  322. }
  323. }
  324. /// Sort keys. After sorting the set can be iterated in order until new elements are inserted.
  325. void Sort()
  326. {
  327. i32 numKeys = Size();
  328. if (!numKeys)
  329. return;
  330. auto** ptrs = new Node* [numKeys];
  331. Node* ptr = Head();
  332. for (i32 i = 0; i < numKeys; ++i)
  333. {
  334. ptrs[i] = ptr;
  335. ptr = ptr->Next();
  336. }
  337. Urho3D::Sort(RandomAccessIterator<Node*>(ptrs), RandomAccessIterator<Node*>(ptrs + numKeys), CompareNodes);
  338. head_ = ptrs[0];
  339. ptrs[0]->prev_ = 0;
  340. for (i32 i = 1; i < numKeys; ++i)
  341. {
  342. ptrs[i - 1]->next_ = ptrs[i];
  343. ptrs[i]->prev_ = ptrs[i - 1];
  344. }
  345. ptrs[numKeys - 1]->next_ = tail_;
  346. tail_->prev_ = ptrs[numKeys - 1];
  347. delete[] ptrs;
  348. }
  349. /// Rehash to a specific bucket count, which must be a power of two. Return true if successful.
  350. bool Rehash(i32 numBuckets)
  351. {
  352. assert(numBuckets > 0);
  353. if (numBuckets == NumBuckets())
  354. return true;
  355. if (!numBuckets || numBuckets < Size() / MAX_LOAD_FACTOR)
  356. return false;
  357. if (!IsPowerOfTwo(numBuckets))
  358. return false;
  359. AllocateBuckets(Size(), numBuckets);
  360. Rehash();
  361. return true;
  362. }
  363. /// Return iterator to the key, or end iterator if not found.
  364. Iterator Find(const T& key)
  365. {
  366. if (!ptrs_)
  367. return End();
  368. unsigned hashKey = Hash(key);
  369. Node* node = FindNode(key, hashKey);
  370. if (node)
  371. return Iterator(node);
  372. else
  373. return End();
  374. }
  375. /// Return const iterator to the key, or end iterator if not found.
  376. ConstIterator Find(const T& key) const
  377. {
  378. if (!ptrs_)
  379. return End();
  380. unsigned hashKey = Hash(key);
  381. Node* node = FindNode(key, hashKey);
  382. if (node)
  383. return ConstIterator(node);
  384. else
  385. return End();
  386. }
  387. /// Return whether contains a key.
  388. bool Contains(const T& key) const
  389. {
  390. if (!ptrs_)
  391. return false;
  392. unsigned hashKey = Hash(key);
  393. return FindNode(key, hashKey) != 0;
  394. }
  395. /// Return iterator to the beginning.
  396. Iterator Begin() { return Iterator(Head()); }
  397. /// Return iterator to the beginning.
  398. ConstIterator Begin() const { return ConstIterator(Head()); }
  399. /// Return iterator to the end.
  400. Iterator End() { return Iterator(Tail()); }
  401. /// Return iterator to the end.
  402. ConstIterator End() const { return ConstIterator(Tail()); }
  403. /// Return first key.
  404. const T& Front() const { return *Begin(); }
  405. /// Return last key.
  406. const T& Back() const { return *(--End()); }
  407. private:
  408. /// Return the head node.
  409. Node* Head() const { return static_cast<Node*>(head_); }
  410. /// Return the tail node.
  411. Node* Tail() const { return static_cast<Node*>(tail_); }
  412. /// Find a node from the buckets. Do not call if the buckets have not been allocated.
  413. Node* FindNode(const T& key, unsigned hashKey) const
  414. {
  415. auto* node = static_cast<Node*>(Ptrs()[hashKey]);
  416. while (node)
  417. {
  418. if (node->key_ == key)
  419. return node;
  420. node = node->Down();
  421. }
  422. return 0;
  423. }
  424. /// Find a node and the previous node from the buckets. Do not call if the buckets have not been allocated.
  425. Node* FindNode(const T& key, unsigned hashKey, Node*& previous) const
  426. {
  427. previous = 0;
  428. auto* node = static_cast<Node*>(Ptrs()[hashKey]);
  429. while (node)
  430. {
  431. if (node->key_ == key)
  432. return node;
  433. previous = node;
  434. node = node->Down();
  435. }
  436. return 0;
  437. }
  438. /// Insert a node into the list. Return the new node.
  439. Node* InsertNode(Node* dest, const T& key)
  440. {
  441. if (!dest)
  442. return 0;
  443. Node* newNode = ReserveNode(key);
  444. Node* prev = dest->Prev();
  445. newNode->next_ = dest;
  446. newNode->prev_ = prev;
  447. if (prev)
  448. prev->next_ = newNode;
  449. dest->prev_ = newNode;
  450. // Reassign the head node if necessary
  451. if (dest == Head())
  452. head_ = newNode;
  453. SetSize(Size() + 1);
  454. return newNode;
  455. }
  456. /// Erase a node from the list. Return pointer to the next element, or to the end if could not erase.
  457. Node* EraseNode(Node* node)
  458. {
  459. // The tail node can not be removed
  460. if (!node || node == tail_)
  461. return Tail();
  462. Node* prev = node->Prev();
  463. Node* next = node->Next();
  464. if (prev)
  465. prev->next_ = next;
  466. next->prev_ = prev;
  467. // Reassign the head node if necessary
  468. if (node == Head())
  469. head_ = next;
  470. FreeNode(node);
  471. SetSize(Size() - 1);
  472. return next;
  473. }
  474. /// Reserve a node.
  475. Node* ReserveNode()
  476. {
  477. auto* newNode = static_cast<Node*>(AllocatorReserve(allocator_));
  478. new(newNode) Node();
  479. return newNode;
  480. }
  481. /// Reserve a node with specified key.
  482. Node* ReserveNode(const T& key)
  483. {
  484. auto* newNode = static_cast<Node*>(AllocatorReserve(allocator_));
  485. new(newNode) Node(key);
  486. return newNode;
  487. }
  488. /// Free a node.
  489. void FreeNode(Node* node)
  490. {
  491. (node)->~Node();
  492. AllocatorFree(allocator_, node);
  493. }
  494. /// Rehash the buckets.
  495. void Rehash()
  496. {
  497. for (Iterator it = Begin(); it != End(); ++it)
  498. {
  499. auto* node = static_cast<Node*>(it.ptr_);
  500. unsigned hashKey = Hash(*it);
  501. node->down_ = Ptrs()[hashKey];
  502. Ptrs()[hashKey] = node;
  503. }
  504. }
  505. /// Compare two nodes.
  506. static bool CompareNodes(Node*& lhs, Node*& rhs) { return lhs->key_ < rhs->key_; }
  507. /// Compute a hash based on the key and the bucket size.
  508. hash32 Hash(const T& key) const { return MakeHash(key) & (NumBuckets() - 1); }
  509. };
  510. template <class T> typename Urho3D::HashSet<T>::ConstIterator begin(const Urho3D::HashSet<T>& v) { return v.Begin(); }
  511. template <class T> typename Urho3D::HashSet<T>::ConstIterator end(const Urho3D::HashSet<T>& v) { return v.End(); }
  512. template <class T> typename Urho3D::HashSet<T>::Iterator begin(Urho3D::HashSet<T>& v) { return v.Begin(); }
  513. template <class T> typename Urho3D::HashSet<T>::Iterator end(Urho3D::HashSet<T>& v) { return v.End(); }
  514. }