HashSet.h 16 KB

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