HashMap.cppm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. module;
  2. export module Core.HashMap;
  3. export import Core.Types;
  4. export import Core.Utility;
  5. import Core.List;
  6. import Core.ToString;
  7. import Core.AlignedData;
  8. import Core.Math;
  9. import Core.Meta;
  10. export using ::operator new;
  11. export template<typename T>
  12. concept Hashable = requires(const T& t) { t.hashCode(); };
  13. export template<typename T>
  14. concept HashCast = requires(const T& t) { static_cast<size_t>(t); };
  15. export namespace Core {
  16. template<Hashable H>
  17. inline size_t hashCode(const H& key) {
  18. return key.hashCode();
  19. }
  20. template<HashCast H>
  21. inline size_t hashCode(const H& key) {
  22. return static_cast<size_t>(key);
  23. }
  24. template<typename K, typename V>
  25. struct HashMap final {
  26. template<typename Value>
  27. class Node final {
  28. friend HashMap;
  29. friend List<Node>;
  30. K key;
  31. public:
  32. Value& value;
  33. const K& getKey() const {
  34. return key;
  35. }
  36. size_t toString(char* s, size_t n) const {
  37. size_t total = 0;
  38. addString(key, s, n, total);
  39. addString(" = ", s, n, total);
  40. addString(value, s, n, total);
  41. return total;
  42. }
  43. private:
  44. Node(const K& key_, Value& value_) : key(key_), value(value_) {
  45. }
  46. };
  47. private:
  48. static inline K INVALID = {};
  49. template<typename Value, typename R, R (*A)(const K&, Value&)>
  50. class Iterator final {
  51. const K* currentKey;
  52. const K* endKey;
  53. Value* currentValue;
  54. public:
  55. Iterator(
  56. const K* key, const K* endKey_, Value* value, bool invalidSet) :
  57. currentKey(key), endKey(endKey_), currentValue(value) {
  58. if(!invalidSet) {
  59. skip();
  60. }
  61. }
  62. Iterator& operator++() {
  63. ++currentKey;
  64. ++currentValue;
  65. skip();
  66. return *this;
  67. }
  68. bool operator!=(const Iterator& other) const {
  69. return currentKey != other.currentKey;
  70. }
  71. R operator*() const {
  72. return A(*currentKey, *currentValue);
  73. }
  74. private:
  75. void skip() {
  76. while(currentKey != endKey && *currentKey == INVALID) {
  77. ++currentKey;
  78. ++currentValue;
  79. }
  80. }
  81. };
  82. template<typename Value>
  83. static Node<Value> access(const K& key, Value& value) {
  84. return Node<Value>(key, value);
  85. }
  86. template<typename Value>
  87. static Value& accessValue(const K&, Value& value) {
  88. return value;
  89. }
  90. static const K& accessKey(const K& key, const V&) {
  91. return key;
  92. }
  93. template<typename Value>
  94. using BaseEntryIterator = Iterator<Value, Node<Value>, access<Value>>;
  95. using EntryIterator = BaseEntryIterator<V>;
  96. using ConstEntryIterator = BaseEntryIterator<const V>;
  97. template<typename Value>
  98. using BaseValueIterator = Iterator<Value, Value&, accessValue<Value>>;
  99. using ValueIterator = BaseValueIterator<V>;
  100. using ConstValueIterator = BaseValueIterator<const V>;
  101. using ConstKeyIterator = Iterator<const V, const K&, accessKey>;
  102. template<typename M, typename I>
  103. struct IteratorAdapter final {
  104. M& map;
  105. I begin() const {
  106. return {
  107. map.keys.begin(), map.keys.end(), map.values,
  108. map.invalidSet};
  109. }
  110. I end() const {
  111. return {
  112. map.keys.end(), map.keys.end(), nullptr, map.invalidSet};
  113. }
  114. };
  115. using ValueIteratorAdapter = IteratorAdapter<HashMap, ValueIterator>;
  116. using ConstValueIteratorAdapter =
  117. IteratorAdapter<const HashMap, ConstValueIterator>;
  118. using ConstKeyIteratorAdapter =
  119. IteratorAdapter<const HashMap, ConstKeyIterator>;
  120. private:
  121. List<K> keys{};
  122. V* values = nullptr;
  123. List<i8> jumps{};
  124. size_t entries = 0;
  125. bool invalidSet = false;
  126. public:
  127. HashMap() = default;
  128. HashMap(const HashMap& other) {
  129. for(const auto& e : other) {
  130. add(e.getKey(), e.value);
  131. }
  132. }
  133. HashMap(HashMap&& other) noexcept {
  134. swap(other);
  135. }
  136. ~HashMap() {
  137. size_t length = keys.getLength();
  138. if(length > 0) {
  139. for(size_t i = 1; i < length; i++) {
  140. if(keys[i] != INVALID) {
  141. values[i].~V();
  142. }
  143. }
  144. if(invalidSet) {
  145. values[length].~V();
  146. }
  147. }
  148. deleteWithSourceN<AlignedType<V>>(
  149. reinterpret_cast<AlignedType<V>*>(values));
  150. }
  151. HashMap& operator=(HashMap other) noexcept {
  152. swap(other);
  153. return *this;
  154. }
  155. void rehash(size_t minCapacity) {
  156. if(minCapacity <= keys.getLength()) {
  157. return;
  158. }
  159. HashMap<K, V> map;
  160. size_t l = (1lu << roundUpLog2(max(minCapacity, 8lu))) + 1;
  161. map.keys.resize(l, INVALID);
  162. map.values =
  163. reinterpret_cast<V*>(newWithSourceN<AlignedType<V>>(l));
  164. map.jumps.resize(l, 0);
  165. size_t length = keys.getLength();
  166. if(length > 0) {
  167. for(size_t i = 1; i < length; i++) {
  168. if(keys[i] != INVALID) {
  169. map.add(keys[i], Core::move(values[i]));
  170. }
  171. }
  172. if(invalidSet) {
  173. map.add(INVALID, Core::move(values[length]));
  174. }
  175. }
  176. swap(map);
  177. }
  178. template<typename... Args>
  179. bool tryEmplace(V*& v, const K& key, Args&&... args) {
  180. size_t index = 0;
  181. if(key == INVALID) {
  182. if(invalidSet) {
  183. return false;
  184. }
  185. rehash(1);
  186. invalidSet = true;
  187. } else {
  188. index = searchSlot(key);
  189. if(keys[index] == key) {
  190. return false;
  191. }
  192. }
  193. keys[index] = key;
  194. v = new(values + index) V(Core::forward<Args>(args)...);
  195. entries++;
  196. markSlot(key);
  197. return true;
  198. }
  199. template<typename VA>
  200. V& put(const K& key, VA&& value) {
  201. size_t index = 0;
  202. if(key == INVALID) {
  203. if(invalidSet) {
  204. return (values[0] = Core::forward<VA>(value));
  205. }
  206. rehash(1);
  207. invalidSet = true;
  208. } else {
  209. index = searchSlot(key);
  210. if(keys[index] == key) {
  211. return (values[index] = Core::forward<VA>(value));
  212. }
  213. }
  214. new(values + index) V(Core::forward<VA>(value));
  215. entries++;
  216. keys[index] = key;
  217. markSlot(key);
  218. return values[index];
  219. }
  220. template<typename VA>
  221. HashMap& add(const K& key, VA&& value) {
  222. put(key, Core::forward<VA>(value));
  223. return *this;
  224. }
  225. bool remove(const K& key) {
  226. size_t index = 0;
  227. if(key == INVALID) {
  228. if(!invalidSet) {
  229. return false;
  230. }
  231. invalidSet = false;
  232. } else {
  233. index = searchSlot(key);
  234. if(keys[index] != key) {
  235. return false;
  236. }
  237. }
  238. values[index].~V();
  239. entries--;
  240. demarkSlot(key);
  241. keys[index] = INVALID;
  242. return true;
  243. }
  244. const V* search(const K& key) const {
  245. return searchValue<const V>(key);
  246. }
  247. V* search(const K& key) {
  248. return searchValue<V>(key);
  249. }
  250. bool contains(const K& key) const {
  251. return search(key) != nullptr;
  252. }
  253. HashMap& clear() {
  254. HashMap<K, V> map;
  255. swap(map);
  256. return *this;
  257. }
  258. ConstKeyIteratorAdapter getKeys() const {
  259. return {*this};
  260. }
  261. ValueIteratorAdapter getValues() {
  262. return {*this};
  263. }
  264. ConstValueIteratorAdapter getValues() const {
  265. return {*this};
  266. }
  267. EntryIterator begin() {
  268. return {keys.begin(), keys.end(), values, invalidSet};
  269. }
  270. EntryIterator end() {
  271. return {keys.end(), keys.end(), nullptr, invalidSet};
  272. }
  273. ConstEntryIterator begin() const {
  274. return {keys.begin(), keys.end(), values, invalidSet};
  275. }
  276. ConstEntryIterator end() const {
  277. return {keys.end(), keys.end(), nullptr, invalidSet};
  278. }
  279. void swap(HashMap& o) noexcept {
  280. Core::swap(o.keys, keys);
  281. Core::swap(o.values, values);
  282. Core::swap(o.jumps, jumps);
  283. Core::swap(o.entries, entries);
  284. Core::swap(o.invalidSet, invalidSet);
  285. }
  286. private:
  287. // clang-format off
  288. #define FOR_EACH_HASH_START() \
  289. do { \
  290. size_t baseHash = hashCode(key) * 514'685'581u; \
  291. size_t end = keys.getLength() - 2; \
  292. for(size_t i = 0; i <= 5; i++) { \
  293. size_t hash = 1 + ((baseHash + i) & end)
  294. #define FOR_EACH_HASH_STOP() \
  295. } \
  296. } while(false)
  297. // clang-format on
  298. size_t searchSlot(const K& key) {
  299. rehash(1);
  300. while(true) {
  301. // rehash on bad clustering
  302. FOR_EACH_HASH_START();
  303. if((keys[hash] == INVALID && jumps[hash] == 0) ||
  304. keys[hash] == key) {
  305. return hash;
  306. }
  307. FOR_EACH_HASH_STOP();
  308. rehash(keys.getLength() + 1);
  309. }
  310. }
  311. void markSlot(const K& key) {
  312. FOR_EACH_HASH_START();
  313. if(keys[hash] == key) {
  314. return;
  315. }
  316. jumps[hash]++;
  317. FOR_EACH_HASH_STOP();
  318. }
  319. void demarkSlot(const K& key) {
  320. FOR_EACH_HASH_START();
  321. if(keys[hash] == key) {
  322. return;
  323. }
  324. jumps[hash]--;
  325. FOR_EACH_HASH_STOP();
  326. }
  327. template<typename Value>
  328. Value* searchValue(const K& key) const {
  329. if(keys.getLength() != 0) {
  330. if(key == INVALID) {
  331. return invalidSet ? values : nullptr;
  332. }
  333. FOR_EACH_HASH_START();
  334. if(keys[hash] == key) {
  335. return values + hash;
  336. } else if(jumps[hash] == 0) {
  337. return nullptr;
  338. }
  339. FOR_EACH_HASH_STOP();
  340. }
  341. return nullptr;
  342. }
  343. };
  344. }