HashMap.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #ifndef HASHMAP_H
  2. #define HASHMAP_H
  3. #include "utils/Array.h"
  4. #include "utils/StringBuffer.h"
  5. template<typename K, typename V, int N_MIN>
  6. class HashMap final {
  7. static constexpr int getCapacity() {
  8. int i = 1;
  9. while(i < N_MIN) {
  10. i <<= 1;
  11. }
  12. return i;
  13. }
  14. static constexpr int CAPACITY = getCapacity();
  15. static constexpr int MASK = CAPACITY - 1;
  16. Array<bool, CAPACITY> used;
  17. char keys[sizeof (K) * CAPACITY];
  18. char values[sizeof (V) * CAPACITY];
  19. const K& getKey(int index) const {
  20. return reinterpret_cast<const K*> (keys)[index];
  21. }
  22. V& getValue(int index) {
  23. return reinterpret_cast<V*> (values)[index];
  24. }
  25. const V& getValue(int index) const {
  26. return reinterpret_cast<const V*> (values)[index];
  27. }
  28. enum SearchResult {
  29. FREE_INDEX_FOUND, KEY_FOUND, NOTHING_FOUND
  30. };
  31. struct Search {
  32. int index;
  33. SearchResult result;
  34. Search(int index, SearchResult result) : index(index), result(result) {
  35. }
  36. };
  37. Search searchIndex(const K& key) const {
  38. int base = hash(key);
  39. for(int i = 0; i < CAPACITY; i++) {
  40. int h = (base + i) & MASK;
  41. if(!used[h]) {
  42. return Search(h, FREE_INDEX_FOUND);
  43. } else if(getKey(h) == key) {
  44. return Search(h, KEY_FOUND);
  45. }
  46. }
  47. return Search(-1, NOTHING_FOUND);
  48. }
  49. void copy(const HashMap& other) {
  50. for(int i = 0; i < other.CAPACITY; i++) {
  51. if(other.used[i]) {
  52. used[i] = true;
  53. new (reinterpret_cast<K*> (keys) + i) K(other.getKey(i));
  54. new (reinterpret_cast<V*> (values) + i) V(other.getValue(i));
  55. }
  56. }
  57. }
  58. void move(HashMap& other) {
  59. for(int i = 0; i < other.CAPACITY; i++) {
  60. if(other.used[i]) {
  61. used[i] = true;
  62. new (reinterpret_cast<K*> (keys) + i) K(std::move(other.getKey(i)));
  63. new (reinterpret_cast<V*> (values) + i) V(std::move(other.getValue(i)));
  64. }
  65. }
  66. other.clear();
  67. }
  68. public:
  69. HashMap() : used(false) {
  70. }
  71. ~HashMap() {
  72. clear();
  73. }
  74. HashMap(const HashMap& other) : used(false) {
  75. copy(other);
  76. }
  77. HashMap& operator=(const HashMap& other) {
  78. if(&other != this) {
  79. clear();
  80. copy(other);
  81. }
  82. return *this;
  83. }
  84. HashMap(HashMap&& other) : used(false) {
  85. move(other);
  86. }
  87. HashMap& operator=(HashMap&& other) {
  88. if(&other != this) {
  89. clear();
  90. move(other);
  91. }
  92. return *this;
  93. }
  94. template<typename... Args>
  95. bool tryEmplace(const K& key, Args&&... args) {
  96. Search s = searchIndex(key);
  97. if(s.result == FREE_INDEX_FOUND) {
  98. used[s.index] = true;
  99. new (reinterpret_cast<K*> (keys) + s.index) K(key);
  100. new (reinterpret_cast<V*> (values) + s.index) V(args...);
  101. return false;
  102. }
  103. return true;
  104. }
  105. HashMap& add(const K& key, const V& value) {
  106. Search s = searchIndex(key);
  107. if(s.result == KEY_FOUND) {
  108. getValue(s.index) = value;
  109. } else if(s.result == FREE_INDEX_FOUND) {
  110. used[s.index] = true;
  111. new (reinterpret_cast<K*> (keys) + s.index) K(key);
  112. new (reinterpret_cast<V*> (values) + s.index) V(value);
  113. }
  114. return *this;
  115. }
  116. HashMap& add(const K& key, const V&& value) {
  117. Search s = searchIndex(key);
  118. if(s.result == KEY_FOUND) {
  119. getValue(s.index) = std::move(value);
  120. } else if(s.result == FREE_INDEX_FOUND) {
  121. used[s.index] = true;
  122. new (reinterpret_cast<K*> (keys) + s.index) K(key);
  123. new (reinterpret_cast<V*> (values) + s.index) V(std::move(value));
  124. }
  125. return *this;
  126. }
  127. const V& search(const K& key, const V& notFound) const {
  128. Search s = searchIndex(key);
  129. return s.result == KEY_FOUND ? getValue(s.index) : notFound;
  130. }
  131. V& search(const K& key, V& notFound) {
  132. Search s = searchIndex(key);
  133. return s.result == KEY_FOUND ? getValue(s.index) : notFound;
  134. }
  135. bool contains(const K& key) const {
  136. return searchIndex(key).result == KEY_FOUND;
  137. }
  138. HashMap& clear() {
  139. K* k = reinterpret_cast<K*> (keys);
  140. V* v = reinterpret_cast<V*> (values);
  141. for(int i = 0; i < CAPACITY; i++) {
  142. if(used[i]) {
  143. k[i].~K();
  144. v[i].~V();
  145. }
  146. }
  147. used.fill(false);
  148. return *this;
  149. }
  150. template<int L>
  151. void toString(StringBuffer<L>& s) const {
  152. s.append("[");
  153. bool c = false;
  154. for(int i = 0; i < CAPACITY; i++) {
  155. if(!used[i]) {
  156. continue;
  157. } else if(c) {
  158. s.append(", ");
  159. }
  160. s.append(getKey(i)).append(" = ").append(getValue(i));
  161. c = true;
  162. }
  163. s.append("]");
  164. }
  165. private:
  166. template<typename H>
  167. static int hash(const H& key) {
  168. return key.hashCode();
  169. }
  170. static int hash(int key) {
  171. return key;
  172. }
  173. };
  174. #endif