heap-typed 2.1.1 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (281) hide show
  1. package/dist/cjs/index.cjs +1020 -0
  2. package/dist/cjs/index.cjs.map +1 -0
  3. package/dist/esm/index.mjs +1012 -0
  4. package/dist/esm/index.mjs.map +1 -0
  5. package/dist/types/data-structures/base/index.d.ts +2 -1
  6. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -2
  7. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -2
  8. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -2
  9. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
  10. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
  11. package/dist/types/data-structures/binary-tree/bst.d.ts +413 -12
  12. package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
  13. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -3
  14. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
  15. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -2
  16. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +270 -2
  17. package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
  18. package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
  19. package/dist/types/data-structures/graph/index.d.ts +2 -1
  20. package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
  21. package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
  22. package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
  23. package/dist/types/data-structures/hash/index.d.ts +0 -1
  24. package/dist/types/data-structures/heap/heap.d.ts +503 -5
  25. package/dist/types/data-structures/heap/index.d.ts +2 -0
  26. package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
  27. package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
  28. package/dist/types/data-structures/index.d.ts +7 -7
  29. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
  30. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
  31. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
  32. package/dist/types/data-structures/matrix/index.d.ts +1 -1
  33. package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
  34. package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
  35. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
  36. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
  37. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
  38. package/dist/types/data-structures/queue/deque.d.ts +431 -4
  39. package/dist/types/data-structures/queue/queue.d.ts +308 -4
  40. package/dist/types/data-structures/stack/stack.d.ts +306 -2
  41. package/dist/types/data-structures/tree/tree.d.ts +62 -1
  42. package/dist/types/data-structures/trie/trie.d.ts +350 -4
  43. package/dist/types/index.d.ts +11 -2
  44. package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +1 -1
  45. package/dist/types/types/data-structures/base/index.d.ts +1 -0
  46. package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  47. package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
  48. package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
  49. package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
  50. package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
  51. package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
  52. package/dist/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
  53. package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
  54. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
  55. package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  56. package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
  57. package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
  58. package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
  59. package/dist/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
  60. package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
  61. package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
  62. package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
  63. package/dist/types/types/data-structures/hash/index.d.ts +2 -0
  64. package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
  65. package/dist/types/types/data-structures/heap/index.d.ts +1 -0
  66. package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
  67. package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
  68. package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
  69. package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
  70. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
  71. package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
  72. package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
  73. package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
  74. package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
  75. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
  76. package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
  77. package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
  78. package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
  79. package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
  80. package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
  81. package/dist/types/types/index.d.ts +3 -0
  82. package/dist/types/types/utils/index.d.ts +2 -0
  83. package/dist/types/types/utils/utils.d.ts +22 -0
  84. package/dist/types/utils/index.d.ts +1 -1
  85. package/dist/types/utils/utils.d.ts +209 -22
  86. package/dist/umd/heap-typed.js +1023 -0
  87. package/dist/umd/heap-typed.js.map +1 -0
  88. package/dist/umd/heap-typed.min.js +22 -0
  89. package/dist/umd/heap-typed.min.js.map +1 -0
  90. package/package.json +25 -5
  91. package/src/data-structures/binary-tree/avl-tree-counter.ts +4 -4
  92. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
  93. package/src/data-structures/binary-tree/avl-tree.ts +2 -2
  94. package/src/data-structures/binary-tree/binary-tree.ts +4 -4
  95. package/src/data-structures/binary-tree/bst.ts +1 -1
  96. package/src/data-structures/binary-tree/red-black-tree.ts +2 -2
  97. package/src/data-structures/binary-tree/tree-counter.ts +4 -4
  98. package/src/data-structures/binary-tree/tree-multi-map.ts +1 -1
  99. package/src/data-structures/heap/heap.ts +5 -5
  100. package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
  101. package/src/interfaces/binary-tree.ts +1 -1
  102. package/tsconfig.base.json +23 -0
  103. package/tsconfig.json +8 -34
  104. package/tsconfig.test.json +8 -0
  105. package/tsconfig.types.json +15 -0
  106. package/tsup.config.js +28 -0
  107. package/tsup.node.config.js +37 -0
  108. package/dist/common/index.js +0 -28
  109. package/dist/constants/index.js +0 -8
  110. package/dist/data-structures/base/index.d.ts +0 -2
  111. package/dist/data-structures/base/index.js +0 -18
  112. package/dist/data-structures/base/iterable-element-base.js +0 -243
  113. package/dist/data-structures/base/iterable-entry-base.js +0 -183
  114. package/dist/data-structures/base/linear-base.js +0 -415
  115. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
  116. package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
  117. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
  118. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
  119. package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
  120. package/dist/data-structures/binary-tree/avl-tree.js +0 -611
  121. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
  122. package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
  123. package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
  124. package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
  125. package/dist/data-structures/binary-tree/bst.d.ts +0 -413
  126. package/dist/data-structures/binary-tree/bst.js +0 -903
  127. package/dist/data-structures/binary-tree/index.js +0 -26
  128. package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
  129. package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
  130. package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
  131. package/dist/data-structures/binary-tree/segment-tree.js +0 -297
  132. package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
  133. package/dist/data-structures/binary-tree/tree-counter.js +0 -413
  134. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
  135. package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
  136. package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
  137. package/dist/data-structures/graph/abstract-graph.js +0 -896
  138. package/dist/data-structures/graph/directed-graph.d.ts +0 -207
  139. package/dist/data-structures/graph/directed-graph.js +0 -525
  140. package/dist/data-structures/graph/index.js +0 -20
  141. package/dist/data-structures/graph/map-graph.d.ts +0 -78
  142. package/dist/data-structures/graph/map-graph.js +0 -107
  143. package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
  144. package/dist/data-structures/graph/undirected-graph.js +0 -424
  145. package/dist/data-structures/hash/hash-map.d.ts +0 -345
  146. package/dist/data-structures/hash/hash-map.js +0 -692
  147. package/dist/data-structures/hash/index.d.ts +0 -1
  148. package/dist/data-structures/hash/index.js +0 -17
  149. package/dist/data-structures/heap/heap.d.ts +0 -503
  150. package/dist/data-structures/heap/heap.js +0 -901
  151. package/dist/data-structures/heap/index.d.ts +0 -3
  152. package/dist/data-structures/heap/index.js +0 -19
  153. package/dist/data-structures/heap/max-heap.d.ts +0 -32
  154. package/dist/data-structures/heap/max-heap.js +0 -40
  155. package/dist/data-structures/heap/min-heap.d.ts +0 -33
  156. package/dist/data-structures/heap/min-heap.js +0 -31
  157. package/dist/data-structures/index.js +0 -28
  158. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
  159. package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
  160. package/dist/data-structures/linked-list/index.js +0 -19
  161. package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
  162. package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
  163. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
  164. package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
  165. package/dist/data-structures/matrix/index.js +0 -18
  166. package/dist/data-structures/matrix/matrix.d.ts +0 -168
  167. package/dist/data-structures/matrix/matrix.js +0 -448
  168. package/dist/data-structures/matrix/navigator.d.ts +0 -55
  169. package/dist/data-structures/matrix/navigator.js +0 -111
  170. package/dist/data-structures/priority-queue/index.js +0 -19
  171. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
  172. package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
  173. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
  174. package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
  175. package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
  176. package/dist/data-structures/priority-queue/priority-queue.js +0 -20
  177. package/dist/data-structures/queue/deque.d.ts +0 -431
  178. package/dist/data-structures/queue/deque.js +0 -879
  179. package/dist/data-structures/queue/index.js +0 -18
  180. package/dist/data-structures/queue/queue.d.ts +0 -308
  181. package/dist/data-structures/queue/queue.js +0 -473
  182. package/dist/data-structures/stack/index.js +0 -17
  183. package/dist/data-structures/stack/stack.d.ts +0 -306
  184. package/dist/data-structures/stack/stack.js +0 -401
  185. package/dist/data-structures/tree/index.js +0 -17
  186. package/dist/data-structures/tree/tree.d.ts +0 -62
  187. package/dist/data-structures/tree/tree.js +0 -107
  188. package/dist/data-structures/trie/index.js +0 -17
  189. package/dist/data-structures/trie/trie.d.ts +0 -350
  190. package/dist/data-structures/trie/trie.js +0 -610
  191. package/dist/index.d.ts +0 -12
  192. package/dist/index.js +0 -28
  193. package/dist/interfaces/binary-tree.js +0 -2
  194. package/dist/interfaces/doubly-linked-list.js +0 -2
  195. package/dist/interfaces/graph.js +0 -2
  196. package/dist/interfaces/heap.js +0 -2
  197. package/dist/interfaces/index.js +0 -24
  198. package/dist/interfaces/navigator.js +0 -2
  199. package/dist/interfaces/priority-queue.js +0 -2
  200. package/dist/interfaces/segment-tree.js +0 -2
  201. package/dist/interfaces/singly-linked-list.js +0 -2
  202. package/dist/types/common.js +0 -2
  203. package/dist/types/data-structures/base/base.js +0 -2
  204. package/dist/types/data-structures/base/index.js +0 -17
  205. package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
  206. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
  207. package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
  208. package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
  209. package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
  210. package/dist/types/data-structures/binary-tree/bst.js +0 -2
  211. package/dist/types/data-structures/binary-tree/index.js +0 -25
  212. package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
  213. package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
  214. package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
  215. package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
  216. package/dist/types/data-structures/graph/abstract-graph.js +0 -2
  217. package/dist/types/data-structures/graph/directed-graph.js +0 -2
  218. package/dist/types/data-structures/graph/index.js +0 -19
  219. package/dist/types/data-structures/graph/map-graph.js +0 -2
  220. package/dist/types/data-structures/graph/undirected-graph.js +0 -2
  221. package/dist/types/data-structures/hash/hash-map.js +0 -2
  222. package/dist/types/data-structures/hash/index.js +0 -17
  223. package/dist/types/data-structures/heap/heap.js +0 -2
  224. package/dist/types/data-structures/heap/index.js +0 -17
  225. package/dist/types/data-structures/heap/max-heap.js +0 -2
  226. package/dist/types/data-structures/heap/min-heap.js +0 -2
  227. package/dist/types/data-structures/index.js +0 -28
  228. package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
  229. package/dist/types/data-structures/linked-list/index.js +0 -19
  230. package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
  231. package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
  232. package/dist/types/data-structures/matrix/index.js +0 -18
  233. package/dist/types/data-structures/matrix/matrix.js +0 -2
  234. package/dist/types/data-structures/matrix/navigator.js +0 -2
  235. package/dist/types/data-structures/priority-queue/index.js +0 -19
  236. package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
  237. package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
  238. package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
  239. package/dist/types/data-structures/queue/deque.js +0 -2
  240. package/dist/types/data-structures/queue/index.js +0 -18
  241. package/dist/types/data-structures/queue/queue.js +0 -2
  242. package/dist/types/data-structures/stack/index.js +0 -17
  243. package/dist/types/data-structures/stack/stack.js +0 -2
  244. package/dist/types/data-structures/tree/index.js +0 -17
  245. package/dist/types/data-structures/tree/tree.js +0 -2
  246. package/dist/types/data-structures/trie/index.js +0 -17
  247. package/dist/types/data-structures/trie/trie.js +0 -2
  248. package/dist/types/index.js +0 -19
  249. package/dist/types/utils/index.js +0 -18
  250. package/dist/types/utils/utils.js +0 -2
  251. package/dist/types/utils/validate-type.js +0 -2
  252. package/dist/utils/index.d.ts +0 -2
  253. package/dist/utils/index.js +0 -18
  254. package/dist/utils/number.js +0 -24
  255. package/dist/utils/utils.d.ts +0 -209
  256. package/dist/utils/utils.js +0 -353
  257. package/dist/{common → types/common}/index.d.ts +0 -0
  258. package/dist/{constants → types/constants}/index.d.ts +0 -0
  259. package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
  260. package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
  261. package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
  262. package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
  263. package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
  264. package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
  265. package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
  266. package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
  267. package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
  268. package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
  269. package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
  270. package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
  271. package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
  272. package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
  273. package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
  274. package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
  275. /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
  276. /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
  277. /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
  278. /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
  279. /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
  280. /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
  281. /package/dist/{utils → types/utils}/number.d.ts +0 -0
@@ -1,692 +0,0 @@
1
- "use strict";
2
- /**
3
- * data-structure-typed
4
- *
5
- * @author Pablo Zeng
6
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
7
- * @license MIT License
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.LinkedHashMap = exports.HashMap = void 0;
11
- const base_1 = require("../base");
12
- const utils_1 = require("../../utils");
13
- /**
14
- * Hash-based map. Supports object keys and custom hashing; offers O(1) average set/get/has.
15
- * @remarks Time O(1), Space O(1)
16
- * @template K
17
- * @template V
18
- * @template R
19
- * 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key map to a value.
20
- * 2. Fast Lookup: It's used when you need to quickly find, insert, or delete entries based on a key.
21
- * 3. Unique Keys: Keys are unique.
22
- * If you try to insert another entry with the same key, the new one will replace the old entry.
23
- * 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
24
- * @example
25
- * // should maintain insertion order
26
- * const linkedHashMap = new LinkedHashMap<number, string>();
27
- * linkedHashMap.set(1, 'A');
28
- * linkedHashMap.set(2, 'B');
29
- * linkedHashMap.set(3, 'C');
30
- *
31
- * const result = Array.from(linkedHashMap);
32
- * console.log(result); // [
33
- * // [1, 'A'],
34
- * // [2, 'B'],
35
- * // [3, 'C']
36
- * // ]
37
- * @example
38
- * // fast lookup of values by key
39
- * const hashMap = new HashMap<number, string>();
40
- * hashMap.set(1, 'A');
41
- * hashMap.set(2, 'B');
42
- * hashMap.set(3, 'C');
43
- *
44
- * console.log(hashMap.get(1)); // 'A'
45
- * console.log(hashMap.get(2)); // 'B'
46
- * console.log(hashMap.get(3)); // 'C'
47
- * console.log(hashMap.get(99)); // undefined
48
- * @example
49
- * // remove duplicates when adding multiple entries
50
- * const hashMap = new HashMap<number, string>();
51
- * hashMap.set(1, 'A');
52
- * hashMap.set(2, 'B');
53
- * hashMap.set(1, 'C'); // Update value for key 1
54
- *
55
- * console.log(hashMap.size); // 2
56
- * console.log(hashMap.get(1)); // 'C'
57
- * console.log(hashMap.get(2)); // 'B'
58
- * @example
59
- * // count occurrences of keys
60
- * const data = [1, 2, 1, 3, 2, 1];
61
- *
62
- * const countMap = new HashMap<number, number>();
63
- * for (const key of data) {
64
- * countMap.set(key, (countMap.get(key) || 0) + 1);
65
- * }
66
- *
67
- * console.log(countMap.get(1)); // 3
68
- * console.log(countMap.get(2)); // 2
69
- * console.log(countMap.get(3)); // 1
70
- */
71
- class HashMap extends base_1.IterableEntryBase {
72
- /**
73
- * Create a HashMap and optionally bulk-insert entries.
74
- * @remarks Time O(N), Space O(N)
75
- * @param [entryOrRawElements] - Iterable of entries or raw elements to insert.
76
- * @param [options] - Options: hash function and optional record-to-entry converter.
77
- * @returns New HashMap instance.
78
- */
79
- constructor(entryOrRawElements = [], options) {
80
- super();
81
- this._store = {};
82
- this._objMap = new Map();
83
- this._size = 0;
84
- this._hashFn = (key) => String(key);
85
- if (options) {
86
- const { hashFn, toEntryFn } = options;
87
- if (hashFn)
88
- this._hashFn = hashFn;
89
- if (toEntryFn)
90
- this._toEntryFn = toEntryFn;
91
- }
92
- if (entryOrRawElements)
93
- this.setMany(entryOrRawElements);
94
- }
95
- /**
96
- * Get the internal store for non-object keys.
97
- * @remarks Time O(1), Space O(1)
98
- * @returns Internal record of string→{key,value}.
99
- */
100
- get store() {
101
- return this._store;
102
- }
103
- /**
104
- * Get the internal Map used for object/function keys.
105
- * @remarks Time O(1), Space O(1)
106
- * @returns Map of object→value.
107
- */
108
- get objMap() {
109
- return this._objMap;
110
- }
111
- /**
112
- * Get the raw→entry converter function if present.
113
- * @remarks Time O(1), Space O(1)
114
- * @returns Converter function or undefined.
115
- */
116
- get toEntryFn() {
117
- return this._toEntryFn;
118
- }
119
- /**
120
- * Get the number of distinct keys stored.
121
- * @remarks Time O(1), Space O(1)
122
- * @returns Current size.
123
- */
124
- get size() {
125
- return this._size;
126
- }
127
- /**
128
- * Get the current hash function for non-object keys.
129
- * @remarks Time O(1), Space O(1)
130
- * @returns Hash function.
131
- */
132
- get hashFn() {
133
- return this._hashFn;
134
- }
135
- /**
136
- * Check whether the map is empty.
137
- * @remarks Time O(1), Space O(1)
138
- * @returns True if size is 0.
139
- */
140
- isEmpty() {
141
- return this._size === 0;
142
- }
143
- /**
144
- * Remove all entries and reset counters.
145
- * @remarks Time O(N), Space O(1)
146
- * @returns void
147
- */
148
- clear() {
149
- this._store = {};
150
- this._objMap.clear();
151
- this._size = 0;
152
- }
153
- /**
154
- * Type guard: check if a raw value is a [key, value] entry.
155
- * @remarks Time O(1), Space O(1)
156
- * @returns True if the value is a 2-tuple.
157
- */
158
- isEntry(rawElement) {
159
- return Array.isArray(rawElement) && rawElement.length === 2;
160
- }
161
- /**
162
- * Insert or replace a single entry.
163
- * @remarks Time O(1), Space O(1)
164
- * @param key - Key.
165
- * @param value - Value.
166
- * @returns True when the operation succeeds.
167
- */
168
- set(key, value) {
169
- if (this._isObjKey(key)) {
170
- if (!this.objMap.has(key))
171
- this._size++;
172
- this.objMap.set(key, value);
173
- }
174
- else {
175
- const strKey = this._getNoObjKey(key);
176
- if (this.store[strKey] === undefined)
177
- this._size++;
178
- this._store[strKey] = { key, value };
179
- }
180
- return true;
181
- }
182
- /**
183
- * Insert many entries from an iterable.
184
- * @remarks Time O(N), Space O(N)
185
- * @param entryOrRawElements - Iterable of entries or raw elements to insert.
186
- * @returns Array of per-entry results.
187
- */
188
- setMany(entryOrRawElements) {
189
- const results = [];
190
- for (const rawEle of entryOrRawElements) {
191
- let key, value;
192
- if (this.isEntry(rawEle))
193
- [key, value] = rawEle;
194
- else if (this._toEntryFn)
195
- [key, value] = this._toEntryFn(rawEle);
196
- if (key !== undefined && value !== undefined)
197
- results.push(this.set(key, value));
198
- }
199
- return results;
200
- }
201
- /**
202
- * Get the value for a key.
203
- * @remarks Time O(1), Space O(1)
204
- * @param key - Key to look up.
205
- * @returns Value or undefined.
206
- */
207
- get(key) {
208
- var _a;
209
- if (this._isObjKey(key))
210
- return this.objMap.get(key);
211
- const strKey = this._getNoObjKey(key);
212
- return (_a = this._store[strKey]) === null || _a === void 0 ? void 0 : _a.value;
213
- }
214
- /**
215
- * Check if a key exists.
216
- * @remarks Time O(1), Space O(1)
217
- * @param key - Key to test.
218
- * @returns True if present.
219
- */
220
- has(key) {
221
- if (this._isObjKey(key))
222
- return this.objMap.has(key);
223
- const strKey = this._getNoObjKey(key);
224
- return strKey in this.store;
225
- }
226
- /**
227
- * Delete an entry by key.
228
- * @remarks Time O(1), Space O(1)
229
- * @param key - Key to delete.
230
- * @returns True if the key was found and removed.
231
- */
232
- delete(key) {
233
- if (this._isObjKey(key)) {
234
- if (this.objMap.has(key))
235
- this._size--;
236
- return this.objMap.delete(key);
237
- }
238
- const strKey = this._getNoObjKey(key);
239
- if (strKey in this.store) {
240
- delete this.store[strKey];
241
- this._size--;
242
- return true;
243
- }
244
- return false;
245
- }
246
- /**
247
- * Replace the hash function and rehash the non-object store.
248
- * @remarks Time O(N), Space O(N)
249
- * @param fn - New hash function for non-object keys.
250
- * @returns This map instance.
251
- */
252
- setHashFn(fn) {
253
- if (this._hashFn === fn)
254
- return this;
255
- this._hashFn = fn;
256
- this._rehashNoObj();
257
- return this;
258
- }
259
- /**
260
- * Deep clone this map, preserving hashing behavior.
261
- * @remarks Time O(N), Space O(N)
262
- * @returns A new map with the same content.
263
- */
264
- clone() {
265
- const opts = { hashFn: this._hashFn, toEntryFn: this._toEntryFn };
266
- return this._createLike(this, opts);
267
- }
268
- /**
269
- * Map values to a new map with the same keys.
270
- * @remarks Time O(N), Space O(N)
271
- * @template VM
272
- * @param callbackfn - Mapping function (key, value, index, map) → newValue.
273
- * @param [thisArg] - Value for `this` inside the callback.
274
- * @returns A new map with transformed values.
275
- */
276
- map(callbackfn, thisArg) {
277
- const out = this._createLike();
278
- let index = 0;
279
- for (const [key, value] of this)
280
- out.set(key, callbackfn.call(thisArg, key, value, index++, this));
281
- return out;
282
- }
283
- /**
284
- * Filter entries into a new map.
285
- * @remarks Time O(N), Space O(N)
286
- * @param predicate - Predicate (key, value, index, map) → boolean.
287
- * @param [thisArg] - Value for `this` inside the predicate.
288
- * @returns A new map containing entries that satisfied the predicate.
289
- */
290
- filter(predicate, thisArg) {
291
- const out = this._createLike();
292
- let index = 0;
293
- for (const [key, value] of this)
294
- if (predicate.call(thisArg, key, value, index++, this))
295
- out.set(key, value);
296
- return out;
297
- }
298
- /**
299
- * (Protected) Create a like-kind instance and seed it from an iterable.
300
- * @remarks Time O(N), Space O(N)
301
- * @template TK
302
- * @template TV
303
- * @template TR
304
- * @param [entries] - Iterable used to seed the new map.
305
- * @param [options] - Options forwarded to the constructor.
306
- * @returns A like-kind map instance.
307
- */
308
- _createLike(entries = [], options) {
309
- const Ctor = this.constructor;
310
- return new Ctor(entries, options);
311
- }
312
- _rehashNoObj() {
313
- const fresh = {};
314
- for (const { key, value } of Object.values(this._store)) {
315
- const sk = this._getNoObjKey(key);
316
- fresh[sk] = { key, value };
317
- }
318
- this._store = fresh;
319
- }
320
- *_getIterator() {
321
- for (const node of Object.values(this.store))
322
- yield [node.key, node.value];
323
- for (const node of this.objMap)
324
- yield node;
325
- }
326
- _isObjKey(key) {
327
- const keyType = typeof key;
328
- return (keyType === 'object' || keyType === 'function') && key !== null;
329
- }
330
- _getNoObjKey(key) {
331
- const keyType = typeof key;
332
- let strKey;
333
- if (keyType !== 'string' && keyType !== 'number' && keyType !== 'symbol') {
334
- strKey = this._hashFn(key);
335
- }
336
- else {
337
- if (keyType === 'number') {
338
- strKey = key;
339
- }
340
- else {
341
- strKey = key;
342
- }
343
- }
344
- return strKey;
345
- }
346
- }
347
- exports.HashMap = HashMap;
348
- /**
349
- * Hash-based map that preserves insertion order via a doubly-linked list.
350
- * @remarks Time O(1), Space O(1)
351
- * @template K
352
- * @template V
353
- * @template R
354
- * @example examples will be generated by unit test
355
- */
356
- class LinkedHashMap extends base_1.IterableEntryBase {
357
- /**
358
- * Create a LinkedHashMap and optionally bulk-insert entries.
359
- * @remarks Time O(N), Space O(N)
360
- * @param [entryOrRawElements] - Iterable of entries or raw elements to insert.
361
- * @param [options] - Options: hash functions and optional record-to-entry converter.
362
- * @returns New LinkedHashMap instance.
363
- */
364
- constructor(entryOrRawElements = [], options) {
365
- super();
366
- this._hashFn = (key) => String(key);
367
- this._objHashFn = (key) => key;
368
- this._noObjMap = {};
369
- this._objMap = new WeakMap();
370
- this._toEntryFn = (rawElement) => {
371
- if (this.isEntry(rawElement)) {
372
- return rawElement;
373
- }
374
- throw new Error('If `entryOrRawElements` does not adhere to [key,value], provide `options.toEntryFn` to transform raw records.');
375
- };
376
- this._size = 0;
377
- this._sentinel = {};
378
- this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
379
- if (options) {
380
- const { hashFn, objHashFn, toEntryFn } = options;
381
- if (hashFn)
382
- this._hashFn = hashFn;
383
- if (objHashFn)
384
- this._objHashFn = objHashFn;
385
- if (toEntryFn)
386
- this._toEntryFn = toEntryFn;
387
- }
388
- if (entryOrRawElements)
389
- this.setMany(entryOrRawElements);
390
- }
391
- get hashFn() {
392
- return this._hashFn;
393
- }
394
- /**
395
- * Get the hash function for object/weak keys.
396
- * @remarks Time O(1), Space O(1)
397
- * @returns Object-hash function.
398
- */
399
- get objHashFn() {
400
- return this._objHashFn;
401
- }
402
- /**
403
- * Get the internal record for non-object keys.
404
- * @remarks Time O(1), Space O(1)
405
- * @returns Record of hash→node.
406
- */
407
- get noObjMap() {
408
- return this._noObjMap;
409
- }
410
- get objMap() {
411
- return this._objMap;
412
- }
413
- /**
414
- * Get the head node (first entry) sentinel link.
415
- * @remarks Time O(1), Space O(1)
416
- * @returns Head node or sentinel.
417
- */
418
- get head() {
419
- return this._head;
420
- }
421
- /**
422
- * Get the tail node (last entry) sentinel link.
423
- * @remarks Time O(1), Space O(1)
424
- * @returns Tail node or sentinel.
425
- */
426
- get tail() {
427
- return this._tail;
428
- }
429
- get toEntryFn() {
430
- return this._toEntryFn;
431
- }
432
- get size() {
433
- return this._size;
434
- }
435
- /**
436
- * Get the first [key, value] pair.
437
- * @remarks Time O(1), Space O(1)
438
- * @returns First entry or undefined when empty.
439
- */
440
- get first() {
441
- if (this._size === 0)
442
- return;
443
- return [this.head.key, this.head.value];
444
- }
445
- /**
446
- * Get the last [key, value] pair.
447
- * @remarks Time O(1), Space O(1)
448
- * @returns Last entry or undefined when empty.
449
- */
450
- get last() {
451
- if (this._size === 0)
452
- return;
453
- return [this.tail.key, this.tail.value];
454
- }
455
- /**
456
- * Iterate from head → tail.
457
- * @remarks Time O(N), Space O(1)
458
- * @returns Iterator of [key, value].
459
- */
460
- *begin() {
461
- let node = this.head;
462
- while (node !== this._sentinel) {
463
- yield [node.key, node.value];
464
- node = node.next;
465
- }
466
- }
467
- /**
468
- * Iterate from tail → head.
469
- * @remarks Time O(N), Space O(1)
470
- * @returns Iterator of [key, value].
471
- */
472
- *reverseBegin() {
473
- let node = this.tail;
474
- while (node !== this._sentinel) {
475
- yield [node.key, node.value];
476
- node = node.prev;
477
- }
478
- }
479
- /**
480
- * Insert or replace a single entry; preserves insertion order.
481
- * @remarks Time O(1), Space O(1)
482
- * @param key - Key.
483
- * @param [value] - Value.
484
- * @returns True when the operation succeeds.
485
- */
486
- set(key, value) {
487
- let node;
488
- const isNewKey = !this.has(key);
489
- if ((0, utils_1.isWeakKey)(key)) {
490
- const hash = this._objHashFn(key);
491
- node = this.objMap.get(hash);
492
- if (!node && isNewKey) {
493
- node = { key: hash, value, prev: this.tail, next: this._sentinel };
494
- this.objMap.set(hash, node);
495
- }
496
- else if (node) {
497
- node.value = value;
498
- }
499
- }
500
- else {
501
- const hash = this._hashFn(key);
502
- node = this.noObjMap[hash];
503
- if (!node && isNewKey) {
504
- this.noObjMap[hash] = node = { key, value, prev: this.tail, next: this._sentinel };
505
- }
506
- else if (node) {
507
- node.value = value;
508
- }
509
- }
510
- if (node && isNewKey) {
511
- if (this._size === 0) {
512
- this._head = node;
513
- this._sentinel.next = node;
514
- }
515
- else {
516
- this.tail.next = node;
517
- node.prev = this.tail;
518
- }
519
- this._tail = node;
520
- this._sentinel.prev = node;
521
- this._size++;
522
- }
523
- return true;
524
- }
525
- setMany(entryOrRawElements) {
526
- const results = [];
527
- for (const rawEle of entryOrRawElements) {
528
- let key, value;
529
- if (this.isEntry(rawEle))
530
- [key, value] = rawEle;
531
- else if (this._toEntryFn)
532
- [key, value] = this._toEntryFn(rawEle);
533
- if (key !== undefined && value !== undefined)
534
- results.push(this.set(key, value));
535
- }
536
- return results;
537
- }
538
- has(key) {
539
- if ((0, utils_1.isWeakKey)(key)) {
540
- const hash = this._objHashFn(key);
541
- return this.objMap.has(hash);
542
- }
543
- const hash = this._hashFn(key);
544
- return hash in this.noObjMap;
545
- }
546
- get(key) {
547
- if ((0, utils_1.isWeakKey)(key)) {
548
- const hash = this._objHashFn(key);
549
- const node = this.objMap.get(hash);
550
- return node ? node.value : undefined;
551
- }
552
- const hash = this._hashFn(key);
553
- const node = this.noObjMap[hash];
554
- return node ? node.value : undefined;
555
- }
556
- /**
557
- * Get the value at a given index in insertion order.
558
- * @remarks Time O(N), Space O(1)
559
- * @param index - Zero-based index.
560
- * @returns Value at the index.
561
- */
562
- at(index) {
563
- (0, utils_1.rangeCheck)(index, 0, this._size - 1);
564
- let node = this.head;
565
- while (index--)
566
- node = node.next;
567
- return node.value;
568
- }
569
- delete(key) {
570
- let node;
571
- if ((0, utils_1.isWeakKey)(key)) {
572
- const hash = this._objHashFn(key);
573
- node = this.objMap.get(hash);
574
- if (!node)
575
- return false;
576
- this.objMap.delete(hash);
577
- }
578
- else {
579
- const hash = this._hashFn(key);
580
- node = this.noObjMap[hash];
581
- if (!node)
582
- return false;
583
- delete this.noObjMap[hash];
584
- }
585
- return this._deleteNode(node);
586
- }
587
- /**
588
- * Delete the first entry that matches a predicate.
589
- * @remarks Time O(N), Space O(1)
590
- * @param predicate - Function (key, value, index, map) → boolean to decide deletion.
591
- * @returns True if an entry was removed.
592
- */
593
- deleteWhere(predicate) {
594
- let node = this._head;
595
- let i = 0;
596
- while (node !== this._sentinel) {
597
- const cur = node;
598
- node = node.next;
599
- if (predicate(cur.key, cur.value, i++, this)) {
600
- if ((0, utils_1.isWeakKey)(cur.key)) {
601
- this._objMap.delete(cur.key);
602
- }
603
- else {
604
- const hash = this._hashFn(cur.key);
605
- delete this._noObjMap[hash];
606
- }
607
- return this._deleteNode(cur);
608
- }
609
- }
610
- return false;
611
- }
612
- /**
613
- * Delete the entry at a given index.
614
- * @remarks Time O(N), Space O(1)
615
- * @param index - Zero-based index.
616
- * @returns True if removed.
617
- */
618
- deleteAt(index) {
619
- (0, utils_1.rangeCheck)(index, 0, this._size - 1);
620
- let node = this.head;
621
- while (index--)
622
- node = node.next;
623
- return this._deleteNode(node);
624
- }
625
- isEmpty() {
626
- return this._size === 0;
627
- }
628
- isEntry(rawElement) {
629
- return Array.isArray(rawElement) && rawElement.length === 2;
630
- }
631
- clear() {
632
- this._noObjMap = {};
633
- this._size = 0;
634
- this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
635
- }
636
- clone() {
637
- const opts = { hashFn: this._hashFn, objHashFn: this._objHashFn };
638
- return this._createLike(this, opts);
639
- }
640
- filter(predicate, thisArg) {
641
- const out = this._createLike();
642
- let index = 0;
643
- for (const [key, value] of this) {
644
- if (predicate.call(thisArg, key, value, index, this))
645
- out.set(key, value);
646
- index++;
647
- }
648
- return out;
649
- }
650
- /**
651
- * Map each entry to a new [key, value] pair and preserve order.
652
- * @remarks Time O(N), Space O(N)
653
- * @template MK
654
- * @template MV
655
- * @param callback - Mapping function (key, value, index, map) → [newKey, newValue].
656
- * @param [thisArg] - Value for `this` inside the callback.
657
- * @returns A new map of the same class with transformed entries.
658
- */
659
- map(callback, thisArg) {
660
- const out = this._createLike();
661
- let index = 0;
662
- for (const [key, value] of this) {
663
- const [newKey, newValue] = callback.call(thisArg, key, value, index, this);
664
- out.set(newKey, newValue);
665
- index++;
666
- }
667
- return out;
668
- }
669
- *_getIterator() {
670
- let node = this.head;
671
- while (node !== this._sentinel) {
672
- yield [node.key, node.value];
673
- node = node.next;
674
- }
675
- }
676
- _deleteNode(node) {
677
- const { prev, next } = node;
678
- prev.next = next;
679
- next.prev = prev;
680
- if (node === this.head)
681
- this._head = next;
682
- if (node === this.tail)
683
- this._tail = prev;
684
- this._size -= 1;
685
- return true;
686
- }
687
- _createLike(entries = [], options) {
688
- const Ctor = this.constructor;
689
- return new Ctor(entries, options);
690
- }
691
- }
692
- exports.LinkedHashMap = LinkedHashMap;
@@ -1 +0,0 @@
1
- export * from './hash-map';
@@ -1,17 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./hash-map"), exports);