HashMap.cppm 11 KB

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