HashMap.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #ifndef HASHMAP_H
  2. #define HASHMAP_H
  3. #include <iostream>
  4. #include "utils/Array.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. clear();
  79. copy(other);
  80. return *this;
  81. }
  82. HashMap(HashMap&& other) : used(false) {
  83. move(other);
  84. }
  85. HashMap& operator=(HashMap&& other) {
  86. clear();
  87. move(other);
  88. return *this;
  89. }
  90. template<typename... Args>
  91. bool tryEmplace(const K& key, Args&&... args) {
  92. Search s = searchIndex(key);
  93. if(s.result == FREE_INDEX_FOUND) {
  94. used[s.index] = true;
  95. new (reinterpret_cast<K*> (keys) + s.index) K(key);
  96. new (reinterpret_cast<V*> (values) + s.index) V(args...);
  97. return false;
  98. }
  99. return true;
  100. }
  101. void add(const K& key, const V& value) {
  102. Search s = searchIndex(key);
  103. if(s.result == KEY_FOUND) {
  104. getValue(s.index) = value;
  105. } else if(s.result == FREE_INDEX_FOUND) {
  106. used[s.index] = true;
  107. new (reinterpret_cast<K*> (keys) + s.index) K(key);
  108. new (reinterpret_cast<V*> (values) + s.index) V(value);
  109. }
  110. }
  111. void add(const K& key, const V&& value) {
  112. Search s = searchIndex(key);
  113. if(s.result == KEY_FOUND) {
  114. getValue(s.index) = std::move(value);
  115. } else if(s.result == FREE_INDEX_FOUND) {
  116. used[s.index] = true;
  117. new (reinterpret_cast<K*> (keys) + s.index) K(key);
  118. new (reinterpret_cast<V*> (values) + s.index) V(std::move(value));
  119. }
  120. }
  121. const V& search(const K& key, const V& notFound) const {
  122. Search s = searchIndex(key);
  123. return s.result == KEY_FOUND ? getValue(s.index) : notFound;
  124. }
  125. V& search(const K& key, V& notFound) {
  126. Search s = searchIndex(key);
  127. return s.result == KEY_FOUND ? getValue(s.index) : notFound;
  128. }
  129. bool contains(const K& key) const {
  130. return searchIndex(key).result == KEY_FOUND;
  131. }
  132. void clear() {
  133. K* k = reinterpret_cast<K*> (keys);
  134. V* v = reinterpret_cast<V*> (values);
  135. for(int i = 0; i < CAPACITY; i++) {
  136. if(used[i]) {
  137. k[i].~K();
  138. v[i].~V();
  139. }
  140. }
  141. used.fill(false);
  142. }
  143. private:
  144. template<typename H>
  145. static int hash(const H& key) {
  146. return key.hashCode();
  147. }
  148. static int hash(int key) {
  149. return key;
  150. }
  151. };
  152. #endif