HashMap.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. template<typename K, typename V, int N_MIN>
  9. class HashMap final {
  10. static constexpr int CAPACITY = 1 << Utils::roundUpLog2(N_MIN);
  11. static constexpr int MASK = CAPACITY - 1;
  12. Array<int, CAPACITY> used;
  13. List<K> keys;
  14. List<V> values;
  15. enum SearchResult { FREE_INDEX_FOUND, KEY_FOUND, NOTHING_FOUND };
  16. struct Search {
  17. int index;
  18. SearchResult result;
  19. Search(int index, SearchResult result) : index(index), result(result) {
  20. }
  21. };
  22. Search searchIndex(const K& key) const {
  23. int base = hash(key);
  24. for(int i = 0; i < CAPACITY; i++) {
  25. int h = (base + i) & MASK;
  26. if(used[h] == -1) {
  27. return Search(h, FREE_INDEX_FOUND);
  28. } else if(keys[used[h]] == key) {
  29. return Search(h, KEY_FOUND);
  30. }
  31. }
  32. return Search(-1, NOTHING_FOUND);
  33. }
  34. template<typename H>
  35. static Hash hash(const H& key) {
  36. return key.hashCode();
  37. }
  38. static Hash hash(int key) {
  39. return key;
  40. }
  41. public:
  42. HashMap() : used(-1) {
  43. }
  44. HashMap(const HashMap& other)
  45. : used(other.used), keys(other.keys), values(other.values) {
  46. }
  47. HashMap& operator=(const HashMap& other) {
  48. if(&other != this) {
  49. used = other.used;
  50. keys = other.keys;
  51. values = other.values;
  52. }
  53. return *this;
  54. }
  55. HashMap(HashMap&& other)
  56. : used(other.used), keys(std::move(other.keys)),
  57. values(std::move(other.values)) {
  58. other.used.fill(-1);
  59. }
  60. HashMap& operator=(HashMap&& other) {
  61. if(&other != this) {
  62. used = std::move(other.used);
  63. keys = std::move(other.keys);
  64. values = std::move(other.values);
  65. other.used.fill(-1);
  66. }
  67. return *this;
  68. }
  69. template<typename... Args>
  70. bool tryEmplace(const K& key, Args&&... args) {
  71. Search s = searchIndex(key);
  72. if(s.result == FREE_INDEX_FOUND) {
  73. used[s.index] = keys.getLength();
  74. keys.add(key);
  75. values.add(std::forward<Args>(args)...);
  76. return false;
  77. }
  78. return true;
  79. }
  80. HashMap& add(const K& key, const V& value) {
  81. Search s = searchIndex(key);
  82. if(s.result == KEY_FOUND) {
  83. values[used[s.index]] = value;
  84. } else if(s.result == FREE_INDEX_FOUND) {
  85. used[s.index] = keys.getLength();
  86. keys.add(key);
  87. values.add(value);
  88. }
  89. return *this;
  90. }
  91. HashMap& add(const K& key, const V&& value) {
  92. Search s = searchIndex(key);
  93. if(s.result == KEY_FOUND) {
  94. values[used[s.index]] = std::move(value);
  95. } else if(s.result == FREE_INDEX_FOUND) {
  96. used[s.index] = keys.getLength();
  97. keys.add(key);
  98. values.add(std::move(value));
  99. }
  100. return *this;
  101. }
  102. const V& search(const K& key, const V& notFound) const {
  103. Search s = searchIndex(key);
  104. return s.result == KEY_FOUND ? values[used[s.index]] : notFound;
  105. }
  106. V& search(const K& key, V& notFound) {
  107. Search s = searchIndex(key);
  108. return s.result == KEY_FOUND ? values[used[s.index]] : notFound;
  109. }
  110. bool contains(const K& key) const {
  111. return searchIndex(key).result == KEY_FOUND;
  112. }
  113. HashMap& clear() {
  114. keys.clear();
  115. values.clear();
  116. used.fill(-1);
  117. return *this;
  118. }
  119. template<int L>
  120. void toString(StringBuffer<L>& s) const {
  121. s.append("[");
  122. bool c = false;
  123. for(int i = 0; i < CAPACITY; i++) {
  124. if(used[i] == -1) {
  125. continue;
  126. } else if(c) {
  127. s.append(", ");
  128. }
  129. s.append(keys[used[i]]).append(" = ").append(values[used[i]]);
  130. c = true;
  131. }
  132. s.append("]");
  133. }
  134. V* begin() {
  135. return values.begin();
  136. }
  137. V* end() {
  138. return values.end();
  139. }
  140. const V* begin() const {
  141. return values.begin();
  142. }
  143. const V* end() const {
  144. return values.end();
  145. }
  146. };
  147. #endif