HashMap.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #ifndef HASHMAP_H
  2. #define HASHMAP_H
  3. #include "utils/Array.h"
  4. #include "utils/List.h"
  5. #include "utils/StringBuffer.h"
  6. #include "utils/Types.h"
  7. #include "utils/Utils.h"
  8. #include <unordered_map>
  9. template<typename K, typename V>
  10. struct HashMap final {
  11. struct Node {
  12. friend HashMap;
  13. friend List<Node>;
  14. const K key;
  15. V value;
  16. private:
  17. int next;
  18. Node(const K& key, const V& value) : key(key), value(value), next(-1) {
  19. }
  20. Node(const K& key, V&& value)
  21. : key(key), value(std::move(value)), next(-1) {
  22. }
  23. template<typename... Args>
  24. Node(const K& key, Args&&... args)
  25. : key(key), value(std::forward<Args>(args)...), next(-1) {
  26. }
  27. };
  28. private:
  29. List<int> nodePointers;
  30. List<Node> nodes;
  31. public:
  32. HashMap(int minCapacity = 8) {
  33. nodePointers.resize(1 << Utils::roundUpLog2(minCapacity), -1);
  34. }
  35. template<typename... Args>
  36. bool tryEmplace(const K& key, Args&&... args) {
  37. int pointer = prepareAdd(key);
  38. if(pointer == -1) {
  39. nodes.add(key, std::forward<Args>(args)...);
  40. return false;
  41. }
  42. return true;
  43. }
  44. HashMap& add(const K& key, const V& value) {
  45. int pointer = prepareAdd(key);
  46. if(pointer != -1) {
  47. nodes[pointer].value = value;
  48. } else {
  49. nodes.add(key, value);
  50. }
  51. return *this;
  52. }
  53. HashMap& add(const K& key, V&& value) {
  54. int pointer = prepareAdd(key);
  55. if(pointer != -1) {
  56. nodes[pointer].value = std::move(value);
  57. } else {
  58. nodes.add(key, std::move(value));
  59. }
  60. return *this;
  61. }
  62. const V* search(const K& key) const {
  63. Hash h = hash(key);
  64. int pointer = nodePointers[h];
  65. while(pointer != -1) {
  66. if(nodes[pointer].key == key) {
  67. return &(nodes[pointer].value);
  68. }
  69. pointer = nodes[pointer].next;
  70. }
  71. return nullptr;
  72. }
  73. V* search(const K& key) {
  74. return const_cast<V*>(static_cast<const HashMap*>(this)->search(key));
  75. }
  76. bool contains(const K& key) const {
  77. return search(key) != nullptr;
  78. }
  79. HashMap& clear() {
  80. for(int& pointer : nodePointers) {
  81. pointer = -1;
  82. }
  83. nodes.clear();
  84. return *this;
  85. }
  86. Node* begin() {
  87. return nodes.begin();
  88. }
  89. Node* end() {
  90. return nodes.end();
  91. }
  92. const Node* begin() const {
  93. return nodes.begin();
  94. }
  95. const Node* end() const {
  96. return nodes.end();
  97. }
  98. template<int L>
  99. void toString(StringBuffer<L>& s) const {
  100. s.append("[");
  101. bool c = false;
  102. for(const Node& n : nodes) {
  103. if(c) {
  104. s.append(", ");
  105. }
  106. s.append(n.key).append(" = ").append(n.value);
  107. c = true;
  108. }
  109. s.append("]");
  110. }
  111. private:
  112. template<typename H>
  113. Hash hash(const H& key) const {
  114. return fullHash(key) & (nodePointers.getLength() - 1);
  115. }
  116. template<typename H>
  117. Hash fullHash(const H& key) const {
  118. return key.hashCode();
  119. }
  120. Hash fullHash(int key) const {
  121. return key;
  122. }
  123. void rehash() {
  124. if(nodes.getLength() < nodePointers.getLength()) {
  125. return;
  126. }
  127. HashMap<K, V> map(nodePointers.getLength() * 2);
  128. for(const Node& n : nodes) {
  129. map.add(n.key, std::move(n.value));
  130. }
  131. *this = std::move(map);
  132. }
  133. int prepareAdd(const K& key) {
  134. rehash();
  135. Hash h = hash(key);
  136. int pointer = nodePointers[h];
  137. if(pointer == -1) {
  138. nodePointers[h] = nodes.getLength();
  139. return -1;
  140. }
  141. while(true) {
  142. if(nodes[pointer].key == key) {
  143. return pointer;
  144. } else if(nodes[pointer].next == -1) {
  145. nodes[pointer].next = nodes.getLength();
  146. return -1;
  147. }
  148. pointer = nodes[pointer].next;
  149. }
  150. }
  151. };
  152. #endif