HashSet.h 17 KB

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