data-structure-typed 2.5.3 → 2.6.1

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 (158) hide show
  1. package/.github/workflows/ci.yml +7 -2
  2. package/.github/workflows/release-package.yml +9 -2
  3. package/.husky/pre-commit +3 -0
  4. package/CHANGELOG.md +1 -1
  5. package/MIGRATION.md +48 -0
  6. package/README.md +20 -2
  7. package/README_CN.md +20 -2
  8. package/SPECIFICATION.md +24 -0
  9. package/SPECIFICATION.zh-CN.md +24 -0
  10. package/dist/cjs/binary-tree.cjs +1897 -19
  11. package/dist/cjs/graph.cjs +174 -0
  12. package/dist/cjs/hash.cjs +33 -0
  13. package/dist/cjs/heap.cjs +71 -0
  14. package/dist/cjs/index.cjs +2383 -3
  15. package/dist/cjs/linked-list.cjs +224 -2
  16. package/dist/cjs/matrix.cjs +24 -0
  17. package/dist/cjs/priority-queue.cjs +71 -0
  18. package/dist/cjs/queue.cjs +221 -1
  19. package/dist/cjs/stack.cjs +59 -0
  20. package/dist/cjs/trie.cjs +62 -0
  21. package/dist/cjs-legacy/binary-tree.cjs +1897 -19
  22. package/dist/cjs-legacy/graph.cjs +174 -0
  23. package/dist/cjs-legacy/hash.cjs +33 -0
  24. package/dist/cjs-legacy/heap.cjs +71 -0
  25. package/dist/cjs-legacy/index.cjs +2383 -3
  26. package/dist/cjs-legacy/linked-list.cjs +224 -2
  27. package/dist/cjs-legacy/matrix.cjs +24 -0
  28. package/dist/cjs-legacy/priority-queue.cjs +71 -0
  29. package/dist/cjs-legacy/queue.cjs +221 -1
  30. package/dist/cjs-legacy/stack.cjs +59 -0
  31. package/dist/cjs-legacy/trie.cjs +62 -0
  32. package/dist/esm/binary-tree.mjs +1897 -19
  33. package/dist/esm/graph.mjs +174 -0
  34. package/dist/esm/hash.mjs +33 -0
  35. package/dist/esm/heap.mjs +71 -0
  36. package/dist/esm/index.mjs +2383 -3
  37. package/dist/esm/linked-list.mjs +224 -2
  38. package/dist/esm/matrix.mjs +24 -0
  39. package/dist/esm/priority-queue.mjs +71 -0
  40. package/dist/esm/queue.mjs +221 -1
  41. package/dist/esm/stack.mjs +59 -0
  42. package/dist/esm/trie.mjs +62 -0
  43. package/dist/esm-legacy/binary-tree.mjs +1897 -19
  44. package/dist/esm-legacy/graph.mjs +174 -0
  45. package/dist/esm-legacy/hash.mjs +33 -0
  46. package/dist/esm-legacy/heap.mjs +71 -0
  47. package/dist/esm-legacy/index.mjs +2383 -3
  48. package/dist/esm-legacy/linked-list.mjs +224 -2
  49. package/dist/esm-legacy/matrix.mjs +24 -0
  50. package/dist/esm-legacy/priority-queue.mjs +71 -0
  51. package/dist/esm-legacy/queue.mjs +221 -1
  52. package/dist/esm-legacy/stack.mjs +59 -0
  53. package/dist/esm-legacy/trie.mjs +62 -0
  54. package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
  55. package/dist/types/data-structures/base/linear-base.d.ts +6 -0
  56. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +36 -0
  57. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +42 -0
  58. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +75 -0
  59. package/dist/types/data-structures/binary-tree/bst.d.ts +72 -0
  60. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +57 -0
  61. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +18 -0
  62. package/dist/types/data-structures/binary-tree/tree-map.d.ts +375 -0
  63. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +389 -0
  64. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +330 -0
  65. package/dist/types/data-structures/binary-tree/tree-set.d.ts +438 -0
  66. package/dist/types/data-structures/graph/directed-graph.d.ts +30 -0
  67. package/dist/types/data-structures/graph/undirected-graph.d.ts +27 -0
  68. package/dist/types/data-structures/hash/hash-map.d.ts +33 -0
  69. package/dist/types/data-structures/heap/heap.d.ts +42 -0
  70. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +75 -2
  71. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +45 -0
  72. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +54 -0
  73. package/dist/types/data-structures/matrix/matrix.d.ts +24 -0
  74. package/dist/types/data-structures/queue/deque.d.ts +90 -1
  75. package/dist/types/data-structures/queue/queue.d.ts +36 -0
  76. package/dist/types/data-structures/stack/stack.d.ts +30 -0
  77. package/dist/types/data-structures/trie/trie.d.ts +36 -0
  78. package/dist/umd/data-structure-typed.js +2383 -3
  79. package/dist/umd/data-structure-typed.min.js +3 -3
  80. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +108 -108
  81. package/docs-site-docusaurus/docs/api/classes/BST.md +101 -101
  82. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -13
  83. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +66 -66
  84. package/docs-site-docusaurus/docs/api/classes/Deque.md +235 -51
  85. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +21 -21
  86. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +231 -67
  87. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
  88. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
  89. package/docs-site-docusaurus/docs/api/classes/HashMap.md +14 -14
  90. package/docs-site-docusaurus/docs/api/classes/Heap.md +117 -34
  91. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +83 -13
  92. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +124 -20
  93. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +140 -32
  94. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +30 -26
  95. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +159 -51
  96. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +20 -20
  97. package/docs-site-docusaurus/docs/api/classes/Matrix.md +23 -23
  98. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +117 -34
  99. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +117 -34
  100. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -34
  101. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +117 -34
  102. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +117 -34
  103. package/docs-site-docusaurus/docs/api/classes/Queue.md +142 -34
  104. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +117 -117
  105. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +8 -8
  106. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +158 -50
  107. package/docs-site-docusaurus/docs/api/classes/SkipList.md +21 -21
  108. package/docs-site-docusaurus/docs/api/classes/Stack.md +108 -26
  109. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +33 -33
  110. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +75 -39
  111. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +301 -39
  112. package/docs-site-docusaurus/docs/api/classes/Trie.md +110 -28
  113. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +20 -20
  114. package/jest.integration.config.js +1 -2
  115. package/package.json +51 -50
  116. package/src/common/error.ts +15 -32
  117. package/src/common/index.ts +0 -3
  118. package/src/data-structures/base/iterable-element-base.ts +32 -3
  119. package/src/data-structures/base/linear-base.ts +13 -36
  120. package/src/data-structures/binary-tree/avl-tree.ts +31 -493
  121. package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -530
  122. package/src/data-structures/binary-tree/binary-tree.ts +326 -1236
  123. package/src/data-structures/binary-tree/bst.ts +158 -1010
  124. package/src/data-structures/binary-tree/red-black-tree.ts +451 -1233
  125. package/src/data-structures/binary-tree/segment-tree.ts +73 -333
  126. package/src/data-structures/binary-tree/tree-map.ts +462 -4749
  127. package/src/data-structures/binary-tree/tree-multi-map.ts +310 -4530
  128. package/src/data-structures/binary-tree/tree-multi-set.ts +300 -3652
  129. package/src/data-structures/binary-tree/tree-set.ts +437 -4443
  130. package/src/data-structures/graph/abstract-graph.ts +98 -167
  131. package/src/data-structures/graph/directed-graph.ts +137 -532
  132. package/src/data-structures/graph/map-graph.ts +0 -3
  133. package/src/data-structures/graph/undirected-graph.ts +132 -484
  134. package/src/data-structures/hash/hash-map.ts +154 -549
  135. package/src/data-structures/heap/heap.ts +200 -753
  136. package/src/data-structures/linked-list/doubly-linked-list.ts +153 -809
  137. package/src/data-structures/linked-list/singly-linked-list.ts +122 -749
  138. package/src/data-structures/linked-list/skip-linked-list.ts +211 -864
  139. package/src/data-structures/matrix/matrix.ts +179 -494
  140. package/src/data-structures/matrix/navigator.ts +0 -1
  141. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
  142. package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
  143. package/src/data-structures/priority-queue/priority-queue.ts +1 -2
  144. package/src/data-structures/queue/deque.ts +241 -807
  145. package/src/data-structures/queue/queue.ts +102 -589
  146. package/src/data-structures/stack/stack.ts +76 -475
  147. package/src/data-structures/trie/trie.ts +98 -592
  148. package/src/types/common.ts +0 -10
  149. package/src/types/data-structures/binary-tree/bst.ts +0 -7
  150. package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
  151. package/src/types/data-structures/graph/abstract-graph.ts +0 -2
  152. package/src/types/data-structures/hash/hash-map.ts +0 -3
  153. package/src/types/data-structures/hash/index.ts +0 -1
  154. package/src/types/data-structures/matrix/navigator.ts +0 -2
  155. package/src/types/utils/utils.ts +0 -7
  156. package/src/types/utils/validate-type.ts +0 -7
  157. package/src/utils/number.ts +0 -2
  158. package/src/utils/utils.ts +0 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "2.5.3",
3
+ "version": "2.6.1",
4
4
  "description": "Production-ready TypeScript data structures: Heap, Deque, Trie, Graph, Red-Black Tree, TreeMap, TreeSet, and more. Zero dependencies, type-safe, with getRank/getByRank/rangeByRank support.",
5
5
  "browser": "dist/umd/data-structure-typed.min.js",
6
6
  "umd:main": "dist/umd/data-structure-typed.min.js",
@@ -128,48 +128,6 @@
128
128
  "node": ">=12.20.0",
129
129
  "npm": ">=6.14.0"
130
130
  },
131
- "scripts": {
132
- "build": "npm run build:node && npm run build:types && npm run build:umd",
133
- "build:node": "tsup",
134
- "build:umd": "tsup --config tsup.umd.config.js",
135
- "build:types": "rm -rf dist/types && tsc -p tsconfig.types.json",
136
- "build:leetcode": "tsup --config tsup.leetcode.config.js",
137
- "build:typedoc-plugin": "tsc scripts/typedoc-plugin-example-rewrite.ts --outDir scripts --esModuleInterop --module commonjs --target es2020 --moduleResolution node --skipLibCheck",
138
- "gen:examples": "ts-node scripts/test-to-example.ts",
139
- "generate:schema": "ts-node scripts/generate-schema.ts",
140
- "test": "jest --runInBand",
141
- "test:coverage": "jest --runInBand --coverage",
142
- "test:integration": "npm run update:subs && jest --config jest.integration.config.js && tsc test/integration/compile.test.ts && node test/integration/compile.mjs",
143
- "test:perf": "npm run build && NODE_OPTIONS='--expose-gc --max-old-space-size=8192' node test/performance/benchmark-runner-enhanced.mjs",
144
- "test:perf:report": "node test/performance/reportor-enhanced.mjs",
145
- "check": "npm run check:src && npm run check:test",
146
- "check:src": "tsc --noEmit",
147
- "check:test": "tsc -p tsconfig.test.json --noEmit",
148
- "check:circular-refs": "dependency-cruiser src",
149
- "lint": "npm run lint:src && npm run lint:test",
150
- "lint:src": "eslint --fix 'src/**/*.{js,ts}'",
151
- "lint:test": "eslint --fix 'test/**/*.{js,ts}'",
152
- "format": "npm run format:src && npm run format:test",
153
- "format:src": "prettier --write 'src/**/*.{js,ts}'",
154
- "format:test": "prettier --write 'test/**/*.{js,ts}'",
155
- "inspect": "npm run build && npm run check && npm run lint",
156
- "ci": "env && git fetch --tags && npm run update:subs && npm run inspect && npm run test:coverage && npm run changelog",
157
- "docs:sync": "node scripts/sync-docs-to-site.mjs",
158
- "docs:api": "npm run gen:examples && npm run build:typedoc-plugin && cd docs-site-docusaurus && npx typedoc --options typedoc.json && node sort-protected.mjs",
159
- "docs:dev": "cd docs-site-docusaurus && npm run start",
160
- "docs:build": "cd docs-site-docusaurus && rm -rf docs/api && npx typedoc --options typedoc.json && node sort-protected.mjs && npm run build",
161
- "docs:preview": "cd docs-site-docusaurus && npm run serve",
162
- "update:subs": "npm i avl-tree-typed binary-tree-typed bst-typed heap-typed data-structure-typed --save-dev",
163
- "install:all-subs": "npm i avl-tree-typed binary-tree-typed bst-typed deque-typed directed-graph-typed doubly-linked-list-typed graph-typed heap-typed linked-list-typed max-heap-typed max-priority-queue-typed min-heap-typed min-priority-queue-typed priority-queue-typed singly-linked-list-typed stack-typed tree-multimap-typed trie-typed undirected-graph-typed queue-typed --save-dev",
164
- "copy:to-subs": "sh scripts/copy_to_all_subs.sh",
165
- "publish:subs": "npm run copy:to-subs && sh scripts/publish_all_subs.sh",
166
- "publish:docs": "sh scripts/publish_docs.sh",
167
- "publish:all": "npm run ci && npm publish && npm run publish:docs && npm run check:exist-latest && npm run publish:subs",
168
- "check:exist-latest": "sh scripts/check_exist_remotely.sh",
169
- "changelog": "auto-changelog",
170
- "coverage:badge": "istanbul-badges-readme",
171
- "toc": "doctoc README.md"
172
- },
173
131
  "repository": {
174
132
  "type": "git",
175
133
  "url": "git+https://github.com/zrwusa/data-structure-typed.git"
@@ -194,11 +152,11 @@
194
152
  "@typescript-eslint/eslint-plugin": "^8.12.1",
195
153
  "@typescript-eslint/parser": "^8.12.1",
196
154
  "auto-changelog": "^2.5.0",
197
- "avl-tree-typed": "^2.5.2",
155
+ "avl-tree-typed": "^2.5.3",
198
156
  "benchmark": "^2.1.4",
199
- "binary-tree-typed": "^2.5.2",
200
- "bst-typed": "^2.5.2",
201
- "data-structure-typed": "^2.5.2",
157
+ "binary-tree-typed": "^2.5.3",
158
+ "bst-typed": "^2.5.3",
159
+ "data-structure-typed": "^2.5.3",
202
160
  "dependency-cruiser": "^16.5.0",
203
161
  "doctoc": "^2.2.1",
204
162
  "eslint": "^9.13.0",
@@ -207,7 +165,8 @@
207
165
  "eslint-import-resolver-typescript": "^3.6.3",
208
166
  "eslint-plugin-import": "^2.31.0",
209
167
  "fast-glob": "^3.3.2",
210
- "heap-typed": "^2.5.2",
168
+ "heap-typed": "^2.5.3",
169
+ "husky": "^9.1.7",
211
170
  "istanbul-badges-readme": "^1.9.0",
212
171
  "jest": "^29.7.0",
213
172
  "js-sdsl": "^4.4.2",
@@ -288,5 +247,47 @@
288
247
  "UMD",
289
248
  "collection",
290
249
  "sorted collection"
291
- ]
292
- }
250
+ ],
251
+ "scripts": {
252
+ "build": "pnpm build:node && pnpm build:types && pnpm build:umd",
253
+ "build:node": "tsup",
254
+ "build:umd": "tsup --config tsup.umd.config.js",
255
+ "build:types": "rm -rf dist/types && tsc -p tsconfig.types.json",
256
+ "build:leetcode": "tsup --config tsup.leetcode.config.js",
257
+ "build:typedoc-plugin": "tsc scripts/typedoc-plugin-example-rewrite.ts --outDir scripts --esModuleInterop --module commonjs --target es2020 --moduleResolution node --skipLibCheck",
258
+ "gen:examples": "ts-node scripts/test-to-example.ts",
259
+ "generate:schema": "ts-node scripts/generate-schema.ts",
260
+ "test": "jest --runInBand",
261
+ "test:coverage": "jest --runInBand --coverage",
262
+ "test:integration": "pnpm update:subs && jest --config jest.integration.config.js && tsc test/integration/compile.test.ts && node test/integration/compile.mjs",
263
+ "test:perf": "pnpm build && NODE_OPTIONS='--expose-gc --max-old-space-size=8192' node test/performance/benchmark-runner-enhanced.mjs",
264
+ "test:perf:report": "node test/performance/reportor-enhanced.mjs",
265
+ "check": "pnpm check:src && pnpm check:test",
266
+ "check:src": "tsc --noEmit",
267
+ "check:test": "tsc -p tsconfig.test.json --noEmit",
268
+ "check:circular-refs": "dependency-cruiser src",
269
+ "lint": "pnpm lint:src && pnpm lint:test",
270
+ "lint:src": "eslint --fix 'src/**/*.{js,ts}'",
271
+ "lint:test": "eslint --fix 'test/**/*.{js,ts}'",
272
+ "format": "pnpm format:src && pnpm format:test",
273
+ "format:src": "prettier --write 'src/**/*.{js,ts}'",
274
+ "format:test": "prettier --write 'test/**/*.{js,ts}'",
275
+ "inspect": "pnpm build && pnpm check && pnpm lint",
276
+ "ci": "env && git fetch --tags && pnpm update:subs && pnpm inspect && pnpm test:coverage && pnpm changelog",
277
+ "docs:sync": "node scripts/sync-docs-to-site.mjs",
278
+ "docs:api": "pnpm gen:examples && pnpm build:typedoc-plugin && cd docs-site-docusaurus && npx typedoc --options typedoc.json && node sort-protected.mjs",
279
+ "docs:dev": "cd docs-site-docusaurus && pnpm start",
280
+ "docs:build": "cd docs-site-docusaurus && rm -rf docs/api && npx typedoc --options typedoc.json && node sort-protected.mjs && pnpm build",
281
+ "docs:preview": "cd docs-site-docusaurus && pnpm serve",
282
+ "update:subs": "pnpm add avl-tree-typed binary-tree-typed bst-typed heap-typed data-structure-typed --save-dev",
283
+ "install:all-subs": "pnpm add avl-tree-typed binary-tree-typed bst-typed deque-typed directed-graph-typed doubly-linked-list-typed graph-typed heap-typed linked-list-typed max-heap-typed max-priority-queue-typed min-heap-typed min-priority-queue-typed priority-queue-typed singly-linked-list-typed stack-typed tree-multimap-typed trie-typed undirected-graph-typed queue-typed --save-dev",
284
+ "copy:to-subs": "sh scripts/copy_to_all_subs.sh",
285
+ "publish:subs": "pnpm copy:to-subs && sh scripts/publish_all_subs.sh",
286
+ "publish:docs": "sh scripts/publish_docs.sh",
287
+ "publish:all": "pnpm install --frozen-lockfile && pnpm publish && pnpm publish:docs && pnpm check:exist-latest && pnpm publish:subs",
288
+ "check:exist-latest": "sh scripts/check_exist_remotely.sh",
289
+ "changelog": "auto-changelog",
290
+ "coverage:badge": "istanbul-badges-readme",
291
+ "toc": "doctoc README.md"
292
+ }
293
+ }
@@ -5,10 +5,7 @@
5
5
  * @param ErrorClass - The error constructor (Error, TypeError, RangeError, etc.)
6
6
  * @param message - The error message.
7
7
  */
8
- export function raise(
9
- ErrorClass: new (msg: string) => Error,
10
- message: string
11
- ): never {
8
+ export function raise(ErrorClass: new (msg: string) => Error, message: string): never {
12
9
  throw new ErrorClass(message);
13
10
  }
14
11
 
@@ -21,56 +18,42 @@ export const ERR = {
21
18
  indexOutOfRange: (index: number, min: number, max: number, ctx?: string) =>
22
19
  `${ctx ? ctx + ': ' : ''}Index ${index} is out of range [${min}, ${max}].`,
23
20
 
24
- invalidIndex: (ctx?: string) =>
25
- `${ctx ? ctx + ': ' : ''}Index must be an integer.`,
21
+ invalidIndex: (ctx?: string) => `${ctx ? ctx + ': ' : ''}Index must be an integer.`,
26
22
 
27
23
  // Type / argument
28
- invalidArgument: (reason: string, ctx?: string) =>
29
- `${ctx ? ctx + ': ' : ''}${reason}`,
24
+ invalidArgument: (reason: string, ctx?: string) => `${ctx ? ctx + ': ' : ''}${reason}`,
30
25
 
31
26
  comparatorRequired: (ctx?: string) =>
32
27
  `${ctx ? ctx + ': ' : ''}Comparator is required for non-number/non-string/non-Date keys.`,
33
28
 
34
- invalidKey: (reason: string, ctx?: string) =>
35
- `${ctx ? ctx + ': ' : ''}${reason}`,
29
+ invalidKey: (reason: string, ctx?: string) => `${ctx ? ctx + ': ' : ''}${reason}`,
36
30
 
37
- notAFunction: (name: string, ctx?: string) =>
38
- `${ctx ? ctx + ': ' : ''}${name} must be a function.`,
31
+ notAFunction: (name: string, ctx?: string) => `${ctx ? ctx + ': ' : ''}${name} must be a function.`,
39
32
 
40
- invalidEntry: (ctx?: string) =>
41
- `${ctx ? ctx + ': ' : ''}Each entry must be a [key, value] tuple.`,
33
+ invalidEntry: (ctx?: string) => `${ctx ? ctx + ': ' : ''}Each entry must be a [key, value] tuple.`,
42
34
 
43
- invalidNaN: (ctx?: string) =>
44
- `${ctx ? ctx + ': ' : ''}NaN is not a valid key.`,
35
+ invalidNaN: (ctx?: string) => `${ctx ? ctx + ': ' : ''}NaN is not a valid key.`,
45
36
 
46
- invalidDate: (ctx?: string) =>
47
- `${ctx ? ctx + ': ' : ''}Invalid Date key.`,
37
+ invalidDate: (ctx?: string) => `${ctx ? ctx + ': ' : ''}Invalid Date key.`,
48
38
 
49
- reduceEmpty: (ctx?: string) =>
50
- `${ctx ? ctx + ': ' : ''}Reduce of empty structure with no initial value.`,
39
+ reduceEmpty: (ctx?: string) => `${ctx ? ctx + ': ' : ''}Reduce of empty structure with no initial value.`,
51
40
 
52
41
  callbackReturnType: (expected: string, got: string, ctx?: string) =>
53
42
  `${ctx ? ctx + ': ' : ''}Callback must return ${expected}; got ${got}.`,
54
43
 
55
44
  // State / operation
56
- invalidOperation: (reason: string, ctx?: string) =>
57
- `${ctx ? ctx + ': ' : ''}${reason}`,
45
+ invalidOperation: (reason: string, ctx?: string) => `${ctx ? ctx + ': ' : ''}${reason}`,
58
46
 
59
47
  // Matrix
60
- matrixDimensionMismatch: (op: string) =>
61
- `Matrix: Dimensions must be compatible for ${op}.`,
48
+ matrixDimensionMismatch: (op: string) => `Matrix: Dimensions must be compatible for ${op}.`,
62
49
 
63
- matrixSingular: () =>
64
- 'Matrix: Singular matrix, inverse does not exist.',
50
+ matrixSingular: () => 'Matrix: Singular matrix, inverse does not exist.',
65
51
 
66
- matrixNotSquare: () =>
67
- 'Matrix: Must be square for inversion.',
52
+ matrixNotSquare: () => 'Matrix: Must be square for inversion.',
68
53
 
69
- matrixNotRectangular: () =>
70
- 'Matrix: Must be rectangular for transposition.',
54
+ matrixNotRectangular: () => 'Matrix: Must be rectangular for transposition.',
71
55
 
72
- matrixRowMismatch: (expected: number, got: number) =>
73
- `Matrix: Expected row length ${expected}, but got ${got}.`,
56
+ matrixRowMismatch: (expected: number, got: number) => `Matrix: Expected row length ${expected}, but got ${got}.`,
74
57
 
75
58
  // Order statistic
76
59
  orderStatisticNotEnabled: (method: string, ctx?: string) =>
@@ -1,10 +1,8 @@
1
1
  export { ERR, raise } from './error';
2
-
3
2
  export enum DFSOperation {
4
3
  VISIT = 0,
5
4
  PROCESS = 1
6
5
  }
7
-
8
6
  export class Range<K> {
9
7
  constructor(
10
8
  public low: K,
@@ -15,7 +13,6 @@ export class Range<K> {
15
13
  // if (!(isComparable(low) && isComparable(high))) throw new RangeError('low or high is not comparable');
16
14
  // if (low > high) throw new RangeError('low must be less than or equal to high');
17
15
  }
18
-
19
16
  // Determine whether a key is within the range
20
17
  isInRange(key: K, comparator: (a: K, b: K) => number): boolean {
21
18
  const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
@@ -162,7 +162,6 @@ export abstract class IterableElementBase<E, R> implements Iterable<E> {
162
162
  */
163
163
  find<S extends E>(predicate: ElementCallback<E, R, S>, thisArg?: unknown): S | undefined;
164
164
  find(predicate: ElementCallback<E, R, unknown>, thisArg?: unknown): E | undefined;
165
-
166
165
  // Implementation signature
167
166
  find(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): E | undefined {
168
167
  let index = 0;
@@ -191,6 +190,38 @@ export abstract class IterableElementBase<E, R> implements Iterable<E> {
191
190
  return false;
192
191
  }
193
192
 
193
+ /**
194
+ * Check whether a value exists (Array-compatible alias for `has`).
195
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
196
+ * @param element - Element to search for (uses `===`).
197
+ * @returns `true` if found.
198
+ */
199
+ includes(element: E): boolean {
200
+ return this.has(element);
201
+ }
202
+
203
+ /**
204
+ * Return an iterator of `[index, value]` pairs (Array-compatible).
205
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
206
+ */
207
+ *entries(): IterableIterator<[number, E]> {
208
+ let index = 0;
209
+ for (const value of this) {
210
+ yield [index++, value];
211
+ }
212
+ }
213
+
214
+ /**
215
+ * Return an iterator of numeric indices (Array-compatible).
216
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
217
+ */
218
+ *keys(): IterableIterator<number> {
219
+ let index = 0;
220
+ for (const _ of this) {
221
+ yield index++;
222
+ }
223
+ }
224
+
194
225
  reduce(callbackfn: ReduceElementCallback<E, R>): E;
195
226
  reduce(callbackfn: ReduceElementCallback<E, R>, initialValue: E): E;
196
227
  reduce<U>(callbackfn: ReduceElementCallback<E, R, U>, initialValue: U): U;
@@ -220,7 +251,6 @@ export abstract class IterableElementBase<E, R> implements Iterable<E> {
220
251
  let index = 0;
221
252
  const iter = this[Symbol.iterator]();
222
253
  let acc: U;
223
-
224
254
  if (arguments.length >= 2) {
225
255
  acc = initialValue as U;
226
256
  } else {
@@ -229,7 +259,6 @@ export abstract class IterableElementBase<E, R> implements Iterable<E> {
229
259
  acc = first.value as unknown as U;
230
260
  index = 1;
231
261
  }
232
-
233
262
  for (const value of iter as unknown as Iterable<E>) {
234
263
  acc = callbackfn(acc, value, index++, this);
235
264
  }
@@ -75,7 +75,7 @@ export abstract class LinearBase<
75
75
  * @param options - `{ maxLen?, ... }` bounds/behavior options.
76
76
  * @remarks Time O(1), Space O(1)
77
77
  */
78
- constructor(options?: LinearBaseOptions<E, R>) {
78
+ constructor(options?: LinearBaseOptions<E, R>) {
79
79
  super(options);
80
80
  if (options) {
81
81
  const { maxLen } = options;
@@ -112,12 +112,10 @@ export abstract class LinearBase<
112
112
  if (this.length === 0) return -1;
113
113
  if (fromIndex < 0) fromIndex = this.length + fromIndex;
114
114
  if (fromIndex < 0) fromIndex = 0;
115
-
116
115
  for (let i = fromIndex; i < this.length; i++) {
117
116
  const element = this.at(i);
118
117
  if (element === searchElement) return i;
119
118
  }
120
-
121
119
  return -1;
122
120
  }
123
121
 
@@ -132,12 +130,10 @@ export abstract class LinearBase<
132
130
  if (this.length === 0) return -1;
133
131
  if (fromIndex >= this.length) fromIndex = this.length - 1;
134
132
  if (fromIndex < 0) fromIndex = this.length + fromIndex;
135
-
136
133
  for (let i = fromIndex; i >= 0; i--) {
137
134
  const element = this.at(i);
138
135
  if (element === searchElement) return i;
139
136
  }
140
-
141
137
  return -1;
142
138
  }
143
139
 
@@ -164,7 +160,6 @@ export abstract class LinearBase<
164
160
  */
165
161
  concat(...items: (E | this)[]): this {
166
162
  const newList = this.clone();
167
-
168
163
  for (const item of items) {
169
164
  if (item instanceof LinearBase) {
170
165
  newList.pushMany(item);
@@ -172,7 +167,6 @@ export abstract class LinearBase<
172
167
  newList.push(item);
173
168
  }
174
169
  }
175
-
176
170
  return newList;
177
171
  }
178
172
 
@@ -200,22 +194,18 @@ export abstract class LinearBase<
200
194
  */
201
195
  splice(start: number, deleteCount: number = 0, ...items: E[]): this {
202
196
  const removedList = this._createInstance();
203
-
204
197
  start = start < 0 ? this.length + start : start;
205
198
  start = Math.max(0, Math.min(start, this.length));
206
199
  deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
207
-
208
200
  for (let i = 0; i < deleteCount; i++) {
209
201
  const removed = this.deleteAt(start);
210
202
  if (removed !== undefined) {
211
203
  removedList.push(removed);
212
204
  }
213
205
  }
214
-
215
206
  for (let i = 0; i < items.length; i++) {
216
207
  this.addAt(start + i, items[i]);
217
208
  }
218
-
219
209
  return removedList;
220
210
  }
221
211
 
@@ -243,7 +233,6 @@ export abstract class LinearBase<
243
233
  }
244
234
 
245
235
  reduceRight(callbackfn: ReduceLinearCallback<E>): E;
246
-
247
236
  reduceRight(callbackfn: ReduceLinearCallback<E>, initialValue: E): E;
248
237
 
249
238
  /**
@@ -254,7 +243,6 @@ export abstract class LinearBase<
254
243
  * @remarks Time O(n), Space O(1)
255
244
  */
256
245
  reduceRight<U>(callbackfn: ReduceLinearCallback<E, U>, initialValue: U): U;
257
-
258
246
  reduceRight<U>(callbackfn: ReduceLinearCallback<E, U>, initialValue?: U): U {
259
247
  let accumulator = initialValue ?? (0 as U);
260
248
  for (let i = this.length - 1; i >= 0; i--) {
@@ -273,7 +261,6 @@ export abstract class LinearBase<
273
261
  slice(start: number = 0, end: number = this.length): this {
274
262
  start = start < 0 ? this.length + start : start;
275
263
  end = end < 0 ? this.length + end : end;
276
-
277
264
  const newList = this._createInstance();
278
265
  for (let i = start; i < end; i++) {
279
266
  newList.push(this.at(i)!);
@@ -292,15 +279,12 @@ export abstract class LinearBase<
292
279
  fill(value: E, start = 0, end = this.length): this {
293
280
  start = start < 0 ? this.length + start : start;
294
281
  end = end < 0 ? this.length + end : end;
295
-
296
282
  if (start < 0) start = 0;
297
283
  if (end > this.length) end = this.length;
298
284
  if (start >= end) return this;
299
-
300
285
  for (let i = start; i < end; i++) {
301
286
  this.setAt(i, value);
302
287
  }
303
-
304
288
  return this;
305
289
  }
306
290
 
@@ -327,6 +311,17 @@ export abstract class LinearBase<
327
311
  */
328
312
  abstract reverse(): this;
329
313
 
314
+ /**
315
+ * Return a new instance of the same type with elements in reverse order (non-mutating).
316
+ * @remarks Provided for familiarity when migrating from Array (ES2023 `toReversed`). Time O(n), Space O(n).
317
+ * @returns A new reversed instance.
318
+ */
319
+ toReversed(): this {
320
+ const cloned = this.clone();
321
+ cloned.reverse();
322
+ return cloned as this;
323
+ }
324
+
330
325
  /**
331
326
  * Append one element or node to the tail.
332
327
  * @param elementOrNode - Element or node.
@@ -404,7 +399,7 @@ export abstract class LinearLinkedBase<
404
399
  R = any,
405
400
  NODE extends LinkedListNode<E> = LinkedListNode<E>
406
401
  > extends LinearBase<E, R, NODE> {
407
- constructor(options?: LinearBaseOptions<E, R>) {
402
+ constructor(options?: LinearBaseOptions<E, R>) {
408
403
  super(options);
409
404
  if (options) {
410
405
  const { maxLen } = options;
@@ -422,19 +417,16 @@ export abstract class LinearLinkedBase<
422
417
  override indexOf(searchElement: E, fromIndex: number = 0): number {
423
418
  const iterator = this._getIterator();
424
419
  let current = iterator.next();
425
-
426
420
  let index = 0;
427
421
  while (index < fromIndex) {
428
422
  current = iterator.next();
429
423
  index++;
430
424
  }
431
-
432
425
  while (!current.done) {
433
426
  if (current.value === searchElement) return index;
434
427
  current = iterator.next();
435
428
  index++;
436
429
  }
437
-
438
430
  return -1;
439
431
  }
440
432
 
@@ -448,19 +440,16 @@ export abstract class LinearLinkedBase<
448
440
  override lastIndexOf(searchElement: E, fromIndex: number = this.length - 1): number {
449
441
  const iterator = this._getReverseIterator();
450
442
  let current = iterator.next();
451
-
452
443
  let index = this.length - 1;
453
444
  while (index > fromIndex) {
454
445
  current = iterator.next();
455
446
  index--;
456
447
  }
457
-
458
448
  while (!current.done) {
459
449
  if (current.value === searchElement) return index;
460
450
  current = iterator.next();
461
451
  index--;
462
452
  }
463
-
464
453
  return -1;
465
454
  }
466
455
 
@@ -472,7 +461,6 @@ export abstract class LinearLinkedBase<
472
461
  */
473
462
  override concat(...items: (E | LinearBase<E, R>)[]): this {
474
463
  const newList = this.clone();
475
-
476
464
  for (const item of items) {
477
465
  if (item instanceof LinearBase) {
478
466
  newList.pushMany(item);
@@ -480,7 +468,6 @@ export abstract class LinearLinkedBase<
480
468
  newList.push(item);
481
469
  }
482
470
  }
483
-
484
471
  return newList;
485
472
  }
486
473
 
@@ -494,7 +481,6 @@ export abstract class LinearLinkedBase<
494
481
  override slice(start: number = 0, end: number = this.length): this {
495
482
  start = start < 0 ? this.length + start : start;
496
483
  end = end < 0 ? this.length + end : end;
497
-
498
484
  const newList = this._createInstance();
499
485
  const iterator = this._getIterator();
500
486
  let current = iterator.next();
@@ -507,7 +493,6 @@ export abstract class LinearLinkedBase<
507
493
  newList.push(current.value);
508
494
  current = iterator.next();
509
495
  }
510
-
511
496
  return newList;
512
497
  }
513
498
 
@@ -521,15 +506,12 @@ export abstract class LinearLinkedBase<
521
506
  */
522
507
  override splice(start: number, deleteCount: number = 0, ...items: E[]): this {
523
508
  const removedList = this._createInstance();
524
-
525
509
  start = start < 0 ? this.length + start : start;
526
510
  start = Math.max(0, Math.min(start, this.length));
527
511
  deleteCount = Math.max(0, deleteCount);
528
-
529
512
  let currentIndex = 0;
530
513
  let currentNode: NODE | undefined = undefined;
531
514
  let previousNode: NODE | undefined = undefined;
532
-
533
515
  const iterator = this._getNodeIterator();
534
516
  for (const node of iterator) {
535
517
  if (currentIndex === start) {
@@ -539,14 +521,12 @@ export abstract class LinearLinkedBase<
539
521
  previousNode = node;
540
522
  currentIndex++;
541
523
  }
542
-
543
524
  for (let i = 0; i < deleteCount && currentNode; i++) {
544
525
  removedList.push(currentNode.value);
545
526
  const nextNode = currentNode.next;
546
527
  this.delete(currentNode);
547
528
  currentNode = nextNode as NODE;
548
529
  }
549
-
550
530
  for (let i = 0; i < items.length; i++) {
551
531
  if (previousNode) {
552
532
  this.addAfter(previousNode, items[i]);
@@ -556,12 +536,10 @@ export abstract class LinearLinkedBase<
556
536
  previousNode = this._getNodeIterator().next().value;
557
537
  }
558
538
  }
559
-
560
539
  return removedList;
561
540
  }
562
541
 
563
542
  override reduceRight(callbackfn: ReduceLinearCallback<E>): E;
564
-
565
543
  override reduceRight(callbackfn: ReduceLinearCallback<E>, initialValue: E): E;
566
544
 
567
545
  /**
@@ -572,7 +550,6 @@ export abstract class LinearLinkedBase<
572
550
  * @remarks Time O(n), Space O(1)
573
551
  */
574
552
  override reduceRight<U>(callbackfn: ReduceLinearCallback<E, U>, initialValue: U): U;
575
-
576
553
  override reduceRight<U>(callbackfn: ReduceLinearCallback<E, U>, initialValue?: U): U {
577
554
  let accumulator = initialValue ?? (0 as U);
578
555
  let index = this.length - 1;