HashMap.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. /// Erase a pair by key. Return true if was found.
  303. bool Erase(const T& key)
  304. {
  305. if (!ptrs_)
  306. return false;
  307. unsigned hashKey = Hash(key);
  308. Node* previous;
  309. Node* node = FindNode(key, hashKey, previous);
  310. if (!node)
  311. return false;
  312. if (previous)
  313. previous->down_ = node->down_;
  314. else
  315. Ptrs()[hashKey] = node->down_;
  316. EraseNode(node);
  317. return true;
  318. }
  319. /// Erase a pair by iterator. Return iterator to the next pair.
  320. Iterator Erase(const Iterator& it)
  321. {
  322. if (!ptrs_ || !it.ptr_)
  323. return End();
  324. Node* node = static_cast<Node*>(it.ptr_);
  325. Node* next = node->Next();
  326. unsigned hashKey = Hash(node->pair_.first_);
  327. Node* previous = 0;
  328. Node* current = static_cast<Node*>(Ptrs()[hashKey]);
  329. while (current && current != node)
  330. {
  331. previous = current;
  332. current = current->Down();
  333. }
  334. assert(current == node);
  335. if (previous)
  336. previous->down_ = node->down_;
  337. else
  338. Ptrs()[hashKey] = node->down_;
  339. EraseNode(node);
  340. return Iterator(next);
  341. }
  342. /// Clear the map.
  343. void Clear()
  344. {
  345. if (Size())
  346. {
  347. for (Iterator i = Begin(); i != End();)
  348. {
  349. FreeNode(static_cast<Node*>(i++.ptr_));
  350. i.ptr_->prev_ = 0;
  351. }
  352. head_ = tail_;
  353. SetSize(0);
  354. }
  355. ResetPtrs();
  356. }
  357. /// Sort pairs. After sorting the map can be iterated in order until new elements are inserted.
  358. void Sort()
  359. {
  360. unsigned numKeys = Size();
  361. if (!numKeys)
  362. return;
  363. Node** ptrs = new Node* [numKeys];
  364. Node* ptr = Head();
  365. for (unsigned i = 0; i < numKeys; ++i)
  366. {
  367. ptrs[i] = ptr;
  368. ptr = ptr->Next();
  369. }
  370. Atomic::Sort(RandomAccessIterator<Node*>(ptrs), RandomAccessIterator<Node*>(ptrs + numKeys), CompareNodes);
  371. head_ = ptrs[0];
  372. ptrs[0]->prev_ = 0;
  373. for (unsigned i = 1; i < numKeys; ++i)
  374. {
  375. ptrs[i - 1]->next_ = ptrs[i];
  376. ptrs[i]->prev_ = ptrs[i - 1];
  377. }
  378. ptrs[numKeys - 1]->next_ = tail_;
  379. tail_->prev_ = ptrs[numKeys - 1];
  380. delete[] ptrs;
  381. }
  382. /// Rehash to a specific bucket count, which must be a power of two. Return true if successful.
  383. bool Rehash(unsigned numBuckets)
  384. {
  385. if (numBuckets == NumBuckets())
  386. return true;
  387. if (!numBuckets || numBuckets < Size() / MAX_LOAD_FACTOR)
  388. return false;
  389. // Check for being power of two
  390. unsigned check = numBuckets;
  391. while (!(check & 1))
  392. check >>= 1;
  393. if (check != 1)
  394. return false;
  395. AllocateBuckets(Size(), numBuckets);
  396. Rehash();
  397. return true;
  398. }
  399. /// Return iterator to the pair with key, or end iterator if not found.
  400. Iterator Find(const T& key)
  401. {
  402. if (!ptrs_)
  403. return End();
  404. unsigned hashKey = Hash(key);
  405. Node* node = FindNode(key, hashKey);
  406. if (node)
  407. return Iterator(node);
  408. else
  409. return End();
  410. }
  411. /// Return const iterator to the pair with key, or end iterator if not found.
  412. ConstIterator Find(const T& key) const
  413. {
  414. if (!ptrs_)
  415. return End();
  416. unsigned hashKey = Hash(key);
  417. Node* node = FindNode(key, hashKey);
  418. if (node)
  419. return ConstIterator(node);
  420. else
  421. return End();
  422. }
  423. /// Return whether contains a pair with key.
  424. bool Contains(const T& key) const
  425. {
  426. if (!ptrs_)
  427. return false;
  428. unsigned hashKey = Hash(key);
  429. return FindNode(key, hashKey) != 0;
  430. }
  431. /// Return all the keys.
  432. Vector<T> Keys() const
  433. {
  434. Vector<T> result;
  435. result.Reserve(Size());
  436. for (ConstIterator i = Begin(); i != End(); ++i)
  437. result.Push(i->first_);
  438. return result;
  439. }
  440. /// Return all the values.
  441. Vector<U> Values() const
  442. {
  443. Vector<U> result;
  444. result.Reserve(Size());
  445. for (ConstIterator i = Begin(); i != End(); ++i)
  446. result.Push(i->second_);
  447. return result;
  448. }
  449. /// Return iterator to the beginning.
  450. Iterator Begin() { return Iterator(Head()); }
  451. /// Return iterator to the beginning.
  452. ConstIterator Begin() const { return ConstIterator(Head()); }
  453. /// Return iterator to the end.
  454. Iterator End() { return Iterator(Tail()); }
  455. /// Return iterator to the end.
  456. ConstIterator End() const { return ConstIterator(Tail()); }
  457. /// Return first key.
  458. const T& Front() const { return *Begin(); }
  459. /// Return last key.
  460. const T& Back() const { return *(--End()); }
  461. private:
  462. /// Return the head node.
  463. Node* Head() const { return static_cast<Node*>(head_); }
  464. /// Return the tail node.
  465. Node* Tail() const { return static_cast<Node*>(tail_); }
  466. /// Find a node from the buckets. Do not call if the buckets have not been allocated.
  467. Node* FindNode(const T& key, unsigned hashKey) const
  468. {
  469. Node* node = static_cast<Node*>(Ptrs()[hashKey]);
  470. while (node)
  471. {
  472. if (node->pair_.first_ == key)
  473. return node;
  474. node = node->Down();
  475. }
  476. return 0;
  477. }
  478. /// Find a node and the previous node from the buckets. Do not call if the buckets have not been allocated.
  479. Node* FindNode(const T& key, unsigned hashKey, Node*& previous) const
  480. {
  481. previous = 0;
  482. Node* node = static_cast<Node*>(Ptrs()[hashKey]);
  483. while (node)
  484. {
  485. if (node->pair_.first_ == key)
  486. return node;
  487. previous = node;
  488. node = node->Down();
  489. }
  490. return 0;
  491. }
  492. /// Insert a key and value and return either the new or existing node.
  493. Node* InsertNode(const T& key, const U& value, bool findExisting = true)
  494. {
  495. // If no pointers yet, allocate with minimum bucket count
  496. if (!ptrs_)
  497. {
  498. AllocateBuckets(Size(), MIN_BUCKETS);
  499. Rehash();
  500. }
  501. unsigned hashKey = Hash(key);
  502. if (findExisting)
  503. {
  504. // If exists, just change the value
  505. Node* existing = FindNode(key, hashKey);
  506. if (existing)
  507. {
  508. existing->pair_.second_ = value;
  509. return existing;
  510. }
  511. }
  512. Node* newNode = InsertNode(Tail(), key, value);
  513. newNode->down_ = Ptrs()[hashKey];
  514. Ptrs()[hashKey] = newNode;
  515. // Rehash if the maximum load factor has been exceeded
  516. if (Size() > NumBuckets() * MAX_LOAD_FACTOR)
  517. {
  518. AllocateBuckets(Size(), NumBuckets() << 1);
  519. Rehash();
  520. }
  521. return newNode;
  522. }
  523. /// Insert a node into the list. Return the new node.
  524. Node* InsertNode(Node* dest, const T& key, const U& value)
  525. {
  526. if (!dest)
  527. return 0;
  528. Node* newNode = ReserveNode(key, value);
  529. Node* prev = dest->Prev();
  530. newNode->next_ = dest;
  531. newNode->prev_ = prev;
  532. if (prev)
  533. prev->next_ = newNode;
  534. dest->prev_ = newNode;
  535. // Reassign the head node if necessary
  536. if (dest == Head())
  537. head_ = newNode;
  538. SetSize(Size() + 1);
  539. return newNode;
  540. }
  541. /// Erase a node from the list. Return pointer to the next element, or to the end if could not erase.
  542. Node* EraseNode(Node* node)
  543. {
  544. // The tail node can not be removed
  545. if (!node || node == tail_)
  546. return Tail();
  547. Node* prev = node->Prev();
  548. Node* next = node->Next();
  549. if (prev)
  550. prev->next_ = next;
  551. next->prev_ = prev;
  552. // Reassign the head node if necessary
  553. if (node == Head())
  554. head_ = next;
  555. FreeNode(node);
  556. SetSize(Size() - 1);
  557. return next;
  558. }
  559. /// Reserve a node.
  560. Node* ReserveNode()
  561. {
  562. Node* newNode = static_cast<Node*>(AllocatorReserve(allocator_));
  563. new(newNode) Node();
  564. return newNode;
  565. }
  566. /// Reserve a node with specified key and value.
  567. Node* ReserveNode(const T& key, const U& value)
  568. {
  569. Node* newNode = static_cast<Node*>(AllocatorReserve(allocator_));
  570. new(newNode) Node(key, value);
  571. return newNode;
  572. }
  573. /// Free a node.
  574. void FreeNode(Node* node)
  575. {
  576. (node)->~Node();
  577. AllocatorFree(allocator_, node);
  578. }
  579. /// Rehash the buckets.
  580. void Rehash()
  581. {
  582. for (Iterator i = Begin(); i != End(); ++i)
  583. {
  584. Node* node = static_cast<Node*>(i.ptr_);
  585. unsigned hashKey = Hash(i->first_);
  586. node->down_ = Ptrs()[hashKey];
  587. Ptrs()[hashKey] = node;
  588. }
  589. }
  590. /// Compare two nodes.
  591. static bool CompareNodes(Node*& lhs, Node*& rhs) { return lhs->pair_.first_ < rhs->pair_.first_; }
  592. /// Compute a hash based on the key and the bucket size
  593. unsigned Hash(const T& key) const { return MakeHash(key) & (NumBuckets() - 1); }
  594. };
  595. }
  596. namespace std
  597. {
  598. template <class T, class U> typename Atomic::HashMap<T, U>::ConstIterator begin(const Atomic::HashMap<T, U>& v)
  599. {
  600. return v.Begin();
  601. }
  602. template <class T, class U> typename Atomic::HashMap<T, U>::ConstIterator end(const Atomic::HashMap<T, U>& v) { return v.End(); }
  603. template <class T, class U> typename Atomic::HashMap<T, U>::Iterator begin(Atomic::HashMap<T, U>& v) { return v.Begin(); }
  604. template <class T, class U> typename Atomic::HashMap<T, U>::Iterator end(Atomic::HashMap<T, U>& v) { return v.End(); }
  605. }