HashMap.cppm 11 KB

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