HashMap.h 4.1 KB

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