HashMap.h 20 KB

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