HashMap.h 4.2 KB

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