data-structure-typed 2.5.2 → 2.6.0
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.
- package/.husky/pre-commit +3 -0
- package/CHANGELOG.md +3 -1
- package/MIGRATION.md +217 -0
- package/README.md +80 -8
- package/README_CN.md +569 -143
- package/SPECIFICATION.md +44 -14
- package/SPECIFICATION.zh-CN.md +44 -14
- package/dist/cjs/binary-tree.cjs +5841 -1678
- package/dist/cjs/graph.cjs +422 -14
- package/dist/cjs/hash.cjs +95 -7
- package/dist/cjs/heap.cjs +174 -16
- package/dist/cjs/index.cjs +7751 -2449
- package/dist/cjs/linked-list.cjs +443 -2
- package/dist/cjs/matrix.cjs +56 -0
- package/dist/cjs/priority-queue.cjs +172 -14
- package/dist/cjs/queue.cjs +435 -0
- package/dist/cjs/stack.cjs +103 -4
- package/dist/cjs/trie.cjs +106 -0
- package/dist/cjs-legacy/binary-tree.cjs +5933 -1772
- package/dist/cjs-legacy/graph.cjs +422 -14
- package/dist/cjs-legacy/hash.cjs +95 -7
- package/dist/cjs-legacy/heap.cjs +174 -16
- package/dist/cjs-legacy/index.cjs +8154 -2854
- package/dist/cjs-legacy/linked-list.cjs +443 -2
- package/dist/cjs-legacy/matrix.cjs +56 -0
- package/dist/cjs-legacy/priority-queue.cjs +172 -14
- package/dist/cjs-legacy/queue.cjs +435 -0
- package/dist/cjs-legacy/stack.cjs +103 -4
- package/dist/cjs-legacy/trie.cjs +106 -0
- package/dist/esm/binary-tree.mjs +5841 -1678
- package/dist/esm/graph.mjs +422 -14
- package/dist/esm/hash.mjs +95 -7
- package/dist/esm/heap.mjs +174 -16
- package/dist/esm/index.mjs +7751 -2449
- package/dist/esm/linked-list.mjs +443 -2
- package/dist/esm/matrix.mjs +56 -0
- package/dist/esm/priority-queue.mjs +172 -14
- package/dist/esm/queue.mjs +435 -0
- package/dist/esm/stack.mjs +103 -4
- package/dist/esm/trie.mjs +106 -0
- package/dist/esm-legacy/binary-tree.mjs +5933 -1772
- package/dist/esm-legacy/graph.mjs +422 -14
- package/dist/esm-legacy/hash.mjs +95 -7
- package/dist/esm-legacy/heap.mjs +174 -16
- package/dist/esm-legacy/index.mjs +8154 -2854
- package/dist/esm-legacy/linked-list.mjs +443 -2
- package/dist/esm-legacy/matrix.mjs +56 -0
- package/dist/esm-legacy/priority-queue.mjs +172 -14
- package/dist/esm-legacy/queue.mjs +435 -0
- package/dist/esm-legacy/stack.mjs +103 -4
- package/dist/esm-legacy/trie.mjs +106 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
- package/dist/types/data-structures/base/linear-base.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +86 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +98 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +191 -15
- package/dist/types/data-structures/binary-tree/bst.d.ts +171 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +136 -8
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +42 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1061 -167
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1232 -355
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +916 -194
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1078 -141
- package/dist/types/data-structures/graph/directed-graph.d.ts +70 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +84 -6
- package/dist/types/data-structures/heap/heap.d.ts +140 -12
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +150 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +106 -1
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +126 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +56 -0
- package/dist/types/data-structures/queue/deque.d.ts +171 -0
- package/dist/types/data-structures/queue/queue.d.ts +97 -0
- package/dist/types/data-structures/stack/stack.d.ts +72 -2
- package/dist/types/data-structures/trie/trie.d.ts +84 -0
- package/dist/types/interfaces/binary-tree.d.ts +2 -3
- package/dist/umd/data-structure-typed.js +7784 -2484
- package/dist/umd/data-structure-typed.min.js +4 -4
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +188 -200
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +11 -11
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +62 -62
- package/docs-site-docusaurus/docs/api/classes/BST.md +183 -195
- package/docs-site-docusaurus/docs/api/classes/BSTNode.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +15 -15
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +143 -155
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/Deque.md +99 -85
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +73 -73
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +100 -70
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +12 -12
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +38 -38
- package/docs-site-docusaurus/docs/api/classes/Heap.md +96 -85
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +25 -25
- package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +48 -48
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +52 -52
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +46 -42
- package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +6 -6
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +74 -74
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +73 -73
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +31 -31
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/Navigator.md +5 -5
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +103 -88
- package/docs-site-docusaurus/docs/api/classes/Queue.md +111 -59
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +200 -212
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +10 -10
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +75 -75
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +6 -6
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +37 -37
- package/docs-site-docusaurus/docs/api/classes/Stack.md +42 -42
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +107 -36
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +43 -43
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +106 -35
- package/docs-site-docusaurus/docs/api/classes/Trie.md +43 -43
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +72 -72
- package/docs-site-docusaurus/docs/guide/architecture.md +75 -7
- package/docs-site-docusaurus/docs/guide/concepts.md +53 -34
- package/docs-site-docusaurus/docs/guide/faq.md +53 -0
- package/docs-site-docusaurus/docs/guide/guides.md +8 -9
- package/docs-site-docusaurus/docs/guide/integrations.md +74 -177
- package/docs-site-docusaurus/docs/guide/overview.md +131 -17
- package/docs-site-docusaurus/src/pages/index.tsx +4 -0
- package/docs-site-docusaurus/typedoc.json +1 -0
- package/jest.integration.config.js +1 -2
- package/package.json +10 -7
- package/src/data-structures/base/iterable-element-base.ts +32 -0
- package/src/data-structures/base/linear-base.ts +11 -0
- package/src/data-structures/binary-tree/avl-tree.ts +88 -5
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +98 -0
- package/src/data-structures/binary-tree/binary-tree.ts +242 -81
- package/src/data-structures/binary-tree/bst.ts +173 -7
- package/src/data-structures/binary-tree/red-black-tree.ts +139 -15
- package/src/data-structures/binary-tree/segment-tree.ts +42 -0
- package/src/data-structures/binary-tree/tree-map.ts +948 -36
- package/src/data-structures/binary-tree/tree-multi-map.ts +893 -13
- package/src/data-structures/binary-tree/tree-multi-set.ts +761 -33
- package/src/data-structures/binary-tree/tree-set.ts +1260 -251
- package/src/data-structures/graph/directed-graph.ts +71 -1
- package/src/data-structures/graph/undirected-graph.ts +64 -1
- package/src/data-structures/hash/hash-map.ts +100 -12
- package/src/data-structures/heap/heap.ts +149 -19
- package/src/data-structures/linked-list/doubly-linked-list.ts +178 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
- package/src/data-structures/linked-list/skip-linked-list.ts +126 -0
- package/src/data-structures/matrix/matrix.ts +56 -0
- package/src/data-structures/queue/deque.ts +187 -0
- package/src/data-structures/queue/queue.ts +109 -0
- package/src/data-structures/stack/stack.ts +75 -5
- package/src/data-structures/trie/trie.ts +84 -0
- package/src/interfaces/binary-tree.ts +1 -9
- package/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +0 -12984
- package/.vitepress/cache/deps_temp_51f5f1b0/package.json +0 -3
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +0 -4505
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +0 -9731
- package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +0 -347
package/CHANGELOG.md
CHANGED
|
@@ -8,7 +8,9 @@ All notable changes to this project will be documented in this file.
|
|
|
8
8
|
- [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
|
|
9
9
|
- [`auto-changelog`](https://github.com/CookPete/auto-changelog)
|
|
10
10
|
|
|
11
|
-
## [v2.
|
|
11
|
+
## [v2.6.0](https://github.com/zrwusa/data-structure-typed/compare/v2.5.3...main) (upcoming)
|
|
12
|
+
|
|
13
|
+
## [v2.5.3](https://github.com/zrwusa/data-structure-typed/compare/v2.5.1...v2.5.3) (31 March 2026)
|
|
12
14
|
|
|
13
15
|
## [v2.5.1](https://github.com/zrwusa/data-structure-typed/compare/v2.5.0...v2.5.1) (28 March 2026)
|
|
14
16
|
|
package/MIGRATION.md
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
# Migration Guide
|
|
2
|
+
|
|
3
|
+
## Upgrading to v2.5.3
|
|
4
|
+
|
|
5
|
+
### Breaking Changes
|
|
6
|
+
|
|
7
|
+
#### 1. `HashMap/LinkedHashMap.set()` returns `this` instead of `boolean`
|
|
8
|
+
|
|
9
|
+
Aligns with the native JS `Map.set()` spec. Enables chaining.
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
// Before
|
|
13
|
+
const success: boolean = map.set('key', 'value');
|
|
14
|
+
|
|
15
|
+
// After
|
|
16
|
+
map.set('key', 'value'); // returns this
|
|
17
|
+
const chain = map.set('a', 1).set('b', 2); // chaining works
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
#### 2. `BinaryTree/BST/AVL/RedBlackTree.delete()` returns `boolean` instead of `BinaryTreeDeleteResult[]`
|
|
21
|
+
|
|
22
|
+
Simplified to match standard container semantics.
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
// Before
|
|
26
|
+
const results: BinaryTreeDeleteResult[] = tree.delete(key);
|
|
27
|
+
const success = results.length > 0 && results[0].deleted !== undefined;
|
|
28
|
+
|
|
29
|
+
// After
|
|
30
|
+
const success: boolean = tree.delete(key); // true if found and removed
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
#### 3. `BST.deleteWhere()` returns `boolean` instead of `BinaryTreeDeleteResult[]`
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
// Before
|
|
37
|
+
const results: BinaryTreeDeleteResult[] = bst.deleteWhere(pred);
|
|
38
|
+
|
|
39
|
+
// After
|
|
40
|
+
const deleted: boolean = bst.deleteWhere(pred); // true if any match removed
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
#### 4. `Stack.deleteAt()` returns `E | undefined` instead of `boolean`
|
|
44
|
+
|
|
45
|
+
Consistent with `LinkedList.deleteAt()`, `Queue.deleteAt()`, `Deque.deleteAt()`.
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
// Before
|
|
49
|
+
const success: boolean = stack.deleteAt(2);
|
|
50
|
+
|
|
51
|
+
// After
|
|
52
|
+
const removed: E | undefined = stack.deleteAt(2); // returns removed element
|
|
53
|
+
if (removed !== undefined) { /* success */ }
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
#### 5. `LinkedHashMap.deleteAt()` returns `[K, V] | undefined` instead of `boolean`
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
// Before
|
|
60
|
+
const success: boolean = lhm.deleteAt(0);
|
|
61
|
+
|
|
62
|
+
// After
|
|
63
|
+
const entry: [K, V] | undefined = lhm.deleteAt(0);
|
|
64
|
+
if (entry) {
|
|
65
|
+
const [key, value] = entry;
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
#### 6. `FibonacciHeap.push()` returns `boolean` instead of `this`
|
|
70
|
+
|
|
71
|
+
Consistent with `Heap.add()`.
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
// Before
|
|
75
|
+
const heap: FibonacciHeap = fh.push(42); // returned this
|
|
76
|
+
|
|
77
|
+
// After
|
|
78
|
+
const success: boolean = fh.push(42); // returns true
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
#### 7. `refill()` removed from `BinaryTree` and `Heap`
|
|
82
|
+
|
|
83
|
+
Replace with `clear()` + `addMany()`/`setMany()`.
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
// Before (Heap)
|
|
87
|
+
heap.refill([5, 3, 1, 4]);
|
|
88
|
+
|
|
89
|
+
// After (Heap)
|
|
90
|
+
heap.clear();
|
|
91
|
+
heap.addMany([5, 3, 1, 4]);
|
|
92
|
+
|
|
93
|
+
// Before (BinaryTree)
|
|
94
|
+
tree.refill(entries);
|
|
95
|
+
|
|
96
|
+
// After (BinaryTree)
|
|
97
|
+
tree.clear();
|
|
98
|
+
tree.setMany(entries);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
### Deprecations
|
|
104
|
+
|
|
105
|
+
These still work but will be removed in a future major version.
|
|
106
|
+
|
|
107
|
+
#### `Heap.poll()` → use `Heap.pop()`
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
// Deprecated
|
|
111
|
+
const top = heap.poll();
|
|
112
|
+
|
|
113
|
+
// Preferred
|
|
114
|
+
const top = heap.pop();
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
#### `Heap.deleteBy()` → use `Heap.deleteWhere()`
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
// Deprecated
|
|
121
|
+
heap.deleteBy(e => e.id === 42);
|
|
122
|
+
|
|
123
|
+
// Preferred
|
|
124
|
+
heap.deleteWhere(e => e.id === 42);
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
#### `DoublyLinkedList.getBackward()` → use `findLast()`
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
// Deprecated
|
|
131
|
+
list.getBackward(node => node.value > 5);
|
|
132
|
+
|
|
133
|
+
// Preferred (aligns with ES2023 Array.findLast)
|
|
134
|
+
list.findLast(node => node.value > 5);
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
### New Features
|
|
140
|
+
|
|
141
|
+
#### Array-Compatible APIs (v2.5.4+)
|
|
142
|
+
|
|
143
|
+
All `IterableElementBase` containers (Queue, Deque, Stack, LinkedList, Heap, Trie) now support:
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
structure.includes(element); // alias for has()
|
|
147
|
+
[...structure.entries()]; // [[0, val0], [1, val1], ...]
|
|
148
|
+
[...structure.keys()]; // [0, 1, 2, ...]
|
|
149
|
+
structure.toReversed(); // new instance, reversed (linear containers)
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
#### Back-to-Front Search (v2.5.4+)
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
// Deque + DoublyLinkedList
|
|
156
|
+
deque.findLast(v => v > 10); // last matching value
|
|
157
|
+
deque.findLastIndex(v => v > 10); // last matching index, or -1
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
#### TreeSet ES2025 Set Operations (v2.5.4+)
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
const a = new TreeSet([1, 2, 3, 4, 5]);
|
|
164
|
+
const b = new TreeSet([3, 4, 5, 6, 7]);
|
|
165
|
+
|
|
166
|
+
a.union(b); // TreeSet [1,2,3,4,5,6,7]
|
|
167
|
+
a.intersection(b); // TreeSet [3,4,5]
|
|
168
|
+
a.difference(b); // TreeSet [1,2]
|
|
169
|
+
a.symmetricDifference(b); // TreeSet [1,2,6,7]
|
|
170
|
+
a.isSubsetOf(b); // false
|
|
171
|
+
a.isSupersetOf(b); // false
|
|
172
|
+
a.isDisjointFrom(b); // false
|
|
173
|
+
|
|
174
|
+
// Works with any Iterable
|
|
175
|
+
a.union([10, 11]);
|
|
176
|
+
a.intersection(new Set([2, 4]));
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
#### `deleteWhere()` — conditional deletion
|
|
180
|
+
|
|
181
|
+
Available on: `TreeMap`, `TreeSet`, `TreeMultiMap`, `TreeMultiSet`, `DoublyLinkedList`, `Queue`, `Heap`
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
// Delete first even number from queue
|
|
185
|
+
queue.deleteWhere((value, index) => value % 2 === 0);
|
|
186
|
+
|
|
187
|
+
// Delete entries where value > 100 from TreeMap
|
|
188
|
+
treeMap.deleteWhere((key, value) => value > 100);
|
|
189
|
+
|
|
190
|
+
// Delete keys > 50 from TreeSet
|
|
191
|
+
treeSet.deleteWhere((key) => key > 50);
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
#### `Queue.peek()` / `Deque.peek()`
|
|
195
|
+
|
|
196
|
+
Alias for the `first` getter. Familiar API for users coming from other languages.
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
const queue = new Queue([10, 20, 30]);
|
|
200
|
+
queue.peek(); // 10 (same as queue.first)
|
|
201
|
+
queue.first; // 10
|
|
202
|
+
|
|
203
|
+
const deque = new Deque([10, 20, 30]);
|
|
204
|
+
deque.peek(); // 10 (same as deque.first)
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
#### `Heap.pop()` — primary top-removal method
|
|
208
|
+
|
|
209
|
+
Consistent naming across all containers (`Stack.pop()`, `Deque.pop()`, `Heap.pop()`).
|
|
210
|
+
|
|
211
|
+
```typescript
|
|
212
|
+
const heap = new Heap([3, 1, 4], { comparator: (a, b) => a - b });
|
|
213
|
+
heap.pop(); // 1 (min element)
|
|
214
|
+
heap.pop(); // 3
|
|
215
|
+
heap.pop(); // 4
|
|
216
|
+
heap.pop(); // undefined
|
|
217
|
+
```
|
package/README.md
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
English | [简体中文](./README_CN.md)
|
|
4
4
|
|
|
5
|
-
A production-ready TypeScript data structures library featuring **Heap,
|
|
5
|
+
A production-ready TypeScript data structures library featuring **Heap, Linked List, Deque, Trie, Graph, Red-Black Tree, TreeMap, TreeSet, SkipList, Segment Tree**, and more — with APIs that align with JavaScript's native **Array, Map, and Set**. Zero dependencies. Type-safe. ES2025 Set operations. O(log n) rank & range queries.
|
|
6
6
|
|
|
7
|
-
> **Looking for a TreeMap or
|
|
7
|
+
> **Looking for a TreeMap, TreeSet, or PriorityQueue in TypeScript/JavaScript?** Familiar API, set operations, rank queries, sorted access — no more repeated `Array.sort()`.
|
|
8
8
|
|
|
9
9
|

|
|
10
10
|

|
|
@@ -83,10 +83,10 @@ npm i avl-tree-typed bst-typed heap-typed
|
|
|
83
83
|
|
|
84
84
|
Try it instantly:
|
|
85
85
|
|
|
86
|
-
- [Node.js TypeScript](https://stackblitz.com/
|
|
87
|
-
- [Node.js JavaScript](https://stackblitz.com/
|
|
88
|
-
- [React TypeScript](https://stackblitz.com/
|
|
89
|
-
- [NestJS](https://stackblitz.com/
|
|
86
|
+
- [Node.js TypeScript](https://stackblitz.com/github/zrwusa/dst-playgrounds/tree/main/apps/nodejs-ts?file=src%2Findex.ts&title=data-structure-typed%20%E2%80%94%20Node.js%20TypeScript)
|
|
87
|
+
- [Node.js JavaScript](https://stackblitz.com/github/zrwusa/dst-playgrounds/tree/main/apps/nodejs-js?file=src%2Findex.js&title=data-structure-typed%20%E2%80%94%20Node.js%20JavaScript)
|
|
88
|
+
- [React TypeScript](https://stackblitz.com/github/zrwusa/dst-playgrounds/tree/main/apps/reactjs?file=src%2FApp.tsx&title=data-structure-typed%20%E2%80%94%20React%20Playground)
|
|
89
|
+
- [NestJS](https://stackblitz.com/github/zrwusa/dst-playgrounds/tree/main/apps/nestjs?file=src%2Fproduct%2Fservices%2Fproduct-price-index.service.ts&title=data-structure-typed%20%E2%80%94%20NestJS%20Product%20API)
|
|
90
90
|
|
|
91
91
|
---
|
|
92
92
|
|
|
@@ -216,7 +216,7 @@ const value = tree.get(1); // Type: string | undefined
|
|
|
216
216
|
|
|
217
217
|
### ✨ Zero Friction
|
|
218
218
|
|
|
219
|
-
Works everywhere. Spread it `[...]`, loop it `for..of`, convert it instantly.
|
|
219
|
+
Works everywhere. Spread it `[...]`, loop it `for..of`, convert it instantly. Pass raw data with `toEntryFn`/`toElementFn` — no pre-processing needed.
|
|
220
220
|
|
|
221
221
|
```javascript
|
|
222
222
|
// All data structures work with iterator protocol
|
|
@@ -225,8 +225,51 @@ const sorted = [...tree]; // Spread operator
|
|
|
225
225
|
for (const item of tree) {
|
|
226
226
|
} // for...of loop
|
|
227
227
|
const set = new Set(tree); // Set constructor
|
|
228
|
+
|
|
229
|
+
// Pass raw data directly
|
|
230
|
+
const map = new TreeMap(users, { toEntryFn: u => [u.id, u.name] });
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### 🔄 Working with Raw Data
|
|
234
|
+
|
|
235
|
+
Got raw objects? Three ways to use them — pick based on what you want to store:
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
interface User {
|
|
239
|
+
id: number;
|
|
240
|
+
name: string;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const users: User[] = [
|
|
244
|
+
{ id: 3, name: 'Charlie' },
|
|
245
|
+
{ id: 1, name: 'Alice' },
|
|
246
|
+
{ id: 2, name: 'Bob' }
|
|
247
|
+
];
|
|
248
|
+
|
|
249
|
+
// 1. Extract a field — store only that field
|
|
250
|
+
const ids = new TreeSet<number, User>(
|
|
251
|
+
users,
|
|
252
|
+
{ toElementFn: u => u.id }
|
|
253
|
+
);
|
|
254
|
+
// [1, 2, 3] — numbers only, original objects not kept
|
|
255
|
+
|
|
256
|
+
// 2. Store full objects — sort by a field
|
|
257
|
+
const fullSet = new TreeSet<User>(
|
|
258
|
+
users,
|
|
259
|
+
{ comparator: (a, b) => a.id - b.id }
|
|
260
|
+
);
|
|
261
|
+
// [{ id: 1, name: 'Alice' }, { id: 2, ... }, { id: 3, ... }]
|
|
262
|
+
|
|
263
|
+
// 3. Split into key-value — field as key, anything as value
|
|
264
|
+
const map = new TreeMap<number, User, User>(
|
|
265
|
+
users,
|
|
266
|
+
{ toEntryFn: u => [u.id, u] }
|
|
267
|
+
);
|
|
268
|
+
// map.get(1) → { id: 1, name: 'Alice' }
|
|
228
269
|
```
|
|
229
270
|
|
|
271
|
+
Works across all data structures — `toElementFn` for single-value types (Heap, Queue, Stack, LinkedList, Trie), `toEntryFn` for key-value types (TreeMap, HashMap, SkipList), and `comparator` for any sorted structure.
|
|
272
|
+
|
|
230
273
|
---
|
|
231
274
|
|
|
232
275
|
## 💡 When Should I Consider This Library?
|
|
@@ -238,6 +281,7 @@ const set = new Set(tree); // Set constructor
|
|
|
238
281
|
- Priority queues with fast position-based access
|
|
239
282
|
- Time-series data with range queries
|
|
240
283
|
- Red-Black Tree / Heap performance without learning new APIs
|
|
284
|
+
- **Pass raw objects directly** — no `.map()` pre-processing needed (unique to this library in JS/TS)
|
|
241
285
|
|
|
242
286
|
✅ **When your current code has:**
|
|
243
287
|
|
|
@@ -246,6 +290,7 @@ const set = new Set(tree); // Set constructor
|
|
|
246
290
|
- `Array.shift()` on large lists (queues)
|
|
247
291
|
- Custom sorting logic you repeat across files
|
|
248
292
|
- Map that needs to be ordered
|
|
293
|
+
- `.map()` calls just to reshape data before putting it in a collection
|
|
249
294
|
|
|
250
295
|
---
|
|
251
296
|
|
|
@@ -314,7 +359,25 @@ const taskQueue = new MaxPriorityQueue<{priority: number; task: string}>([], {
|
|
|
314
359
|
taskQueue.add({ priority: 5, task: 'Email' });
|
|
315
360
|
taskQueue.add({ priority: 9, task: 'Alert' }); // Instant priority handling
|
|
316
361
|
|
|
317
|
-
const nextTask = taskQueue.
|
|
362
|
+
const nextTask = taskQueue.pop(); // { priority: 9, task: 'Alert' }
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
### Set Operations (ES2025)
|
|
366
|
+
|
|
367
|
+
```typescript
|
|
368
|
+
import { TreeSet } from 'data-structure-typed';
|
|
369
|
+
|
|
370
|
+
const a = new TreeSet([1, 2, 3, 4, 5]);
|
|
371
|
+
const b = new TreeSet([3, 4, 5, 6, 7]);
|
|
372
|
+
|
|
373
|
+
[...a.union(b)]; // [1,2,3,4,5,6,7]
|
|
374
|
+
[...a.intersection(b)]; // [3,4,5]
|
|
375
|
+
[...a.difference(b)]; // [1,2]
|
|
376
|
+
[...a.symmetricDifference(b)]; // [1,2,6,7]
|
|
377
|
+
a.isSubsetOf(b); // false
|
|
378
|
+
|
|
379
|
+
// Works with any Iterable — native Set, arrays, generators
|
|
380
|
+
a.intersection(new Set([2, 4, 6])); // TreeSet [2, 4]
|
|
318
381
|
```
|
|
319
382
|
|
|
320
383
|
### Fast Queue (FIFO)
|
|
@@ -701,6 +764,15 @@ Yes. 2600+ tests, 99%+ code coverage, zero dependencies, and used in production.
|
|
|
701
764
|
|
|
702
765
|
`data-structure-typed` offers more data structures (20+), a unified Array-like API across all structures, tree-shakeable subpath exports, and active maintenance. See [PERFORMANCE.md](./docs/PERFORMANCE.md) for benchmark comparisons.
|
|
703
766
|
|
|
767
|
+
### Can I pass raw data without converting it first?
|
|
768
|
+
|
|
769
|
+
Yes. Three patterns:
|
|
770
|
+
- **`toElementFn`** — extract a field, store only that (TreeSet, Heap, Queue, Stack, LinkedList, Trie)
|
|
771
|
+
- **`comparator`** — store full objects, sort by a field (all sorted structures)
|
|
772
|
+
- **`toEntryFn`** — split into key-value pairs (TreeMap, HashMap, SkipList)
|
|
773
|
+
|
|
774
|
+
See the [Raw Data section](#-working-with-raw-data) for examples.
|
|
775
|
+
|
|
704
776
|
### What is the bundle size?
|
|
705
777
|
|
|
706
778
|
UMD bundle: ~143KB minified. With subpath imports (e.g., `data-structure-typed/heap`), you only load what you use — as small as 18KB for Stack, 30KB for Heap. `sideEffects: false` enables full tree-shaking.
|