typescript-dsa-stl 1.4.1 → 2.0.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/README.md +342 -249
- package/dist/types/index.d.ts +50 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +30 -0
- package/dist/types/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,249 +1,342 @@
|
|
|
1
|
-
# TypeScript_DSA
|
|
2
|
-
|
|
3
|
-
**This is the GitHub repository** for the npm package **[typescript-dsa-stl](https://www.npmjs.com/package/typescript-dsa-stl)**.
|
|
4
|
-
|
|
5
|
-
STL-style data structures and algorithms for TypeScript: **Vector**, **Stack**, **Queue**, **List**, **PriorityQueue**, **OrderedMap** (Map), **UnorderedMap**, **OrderedSet** (Set), **UnorderedSet**, **OrderedMultiMap**, **OrderedMultiSet**, and algorithms (`sort`, `binarySearch`, `lowerBound`, `min`, `max`, etc.). Install from npm to use in your project; this repo holds the source code.
|
|
6
|
-
|
|
7
|
-
---
|
|
8
|
-
|
|
9
|
-
## Install
|
|
10
|
-
|
|
11
|
-
```bash
|
|
12
|
-
npm install typescript-dsa-stl
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
---
|
|
16
|
-
|
|
17
|
-
## Quick start
|
|
18
|
-
|
|
19
|
-
```ts
|
|
20
|
-
import {
|
|
21
|
-
Vector,
|
|
22
|
-
Stack,
|
|
23
|
-
Queue,
|
|
24
|
-
List,
|
|
25
|
-
PriorityQueue,
|
|
26
|
-
OrderedMap,
|
|
27
|
-
UnorderedMap,
|
|
28
|
-
OrderedSet,
|
|
29
|
-
UnorderedSet,
|
|
30
|
-
OrderedMultiMap,
|
|
31
|
-
OrderedMultiSet,
|
|
32
|
-
sort,
|
|
33
|
-
find,
|
|
34
|
-
binarySearch,
|
|
35
|
-
min,
|
|
36
|
-
max,
|
|
37
|
-
clamp,
|
|
38
|
-
range,
|
|
39
|
-
} from 'typescript-dsa-stl';
|
|
40
|
-
|
|
41
|
-
// Collections
|
|
42
|
-
const vec = new Vector<number>([1, 2, 3]);
|
|
43
|
-
vec.push(4);
|
|
44
|
-
console.log(vec.back()); // 4
|
|
45
|
-
|
|
46
|
-
const stack = new Stack<string>();
|
|
47
|
-
stack.push('a');
|
|
48
|
-
stack.push('b');
|
|
49
|
-
console.log(stack.top()); // 'b'
|
|
50
|
-
|
|
51
|
-
const queue = new Queue<number>();
|
|
52
|
-
queue.enqueue(1);
|
|
53
|
-
queue.enqueue(2);
|
|
54
|
-
console.log(queue.front()); // 1
|
|
55
|
-
|
|
56
|
-
const list = new List<number>();
|
|
57
|
-
list.pushBack(10);
|
|
58
|
-
const node = list.pushBack(20);
|
|
59
|
-
list.insertBefore(node, 15);
|
|
60
|
-
console.log(list.toArray()); // [10, 15, 20]
|
|
61
|
-
|
|
62
|
-
// PriorityQueue (max-heap by default)
|
|
63
|
-
const pq = new PriorityQueue<number>();
|
|
64
|
-
pq.push(3); pq.push(1); pq.push(4);
|
|
65
|
-
console.log(pq.top()); // 4
|
|
66
|
-
pq.pop();
|
|
67
|
-
|
|
68
|
-
// OrderedMap (keys sorted), UnorderedMap (hash)
|
|
69
|
-
const map = new UnorderedMap<string, number>();
|
|
70
|
-
map.set('a', 1); map.set('b', 2);
|
|
71
|
-
console.log(map.get('a')); // 1
|
|
72
|
-
|
|
73
|
-
// OrderedSet (sorted unique), UnorderedSet (hash)
|
|
74
|
-
const set = new UnorderedSet<number>([1, 2, 2, 3]);
|
|
75
|
-
console.log(set.size); // 3
|
|
76
|
-
|
|
77
|
-
// OrderedMultiSet (sorted, duplicates allowed)
|
|
78
|
-
const multiSet = new OrderedMultiSet<number>();
|
|
79
|
-
multiSet.add(3); multiSet.add(1); multiSet.add(2); multiSet.add(2);
|
|
80
|
-
console.log(multiSet.toArray()); // [1, 2, 2, 3]
|
|
81
|
-
console.log(multiSet.count(2)); // 2
|
|
82
|
-
|
|
83
|
-
// OrderedMultiMap (one key → multiple values, keys sorted)
|
|
84
|
-
const multiMap = new OrderedMultiMap<string, number>();
|
|
85
|
-
multiMap.set('scores', 90); multiMap.set('scores', 85); multiMap.set('scores', 95);
|
|
86
|
-
console.log(multiMap.getAll('scores')); // [90, 85, 95]
|
|
87
|
-
|
|
88
|
-
// Algorithms (work on arrays and iterables)
|
|
89
|
-
const arr = [3, 1, 4, 1, 5];
|
|
90
|
-
sort(arr); // [1, 1, 3, 4, 5]
|
|
91
|
-
find(arr, (n) => n > 3); // 4
|
|
92
|
-
min(arr); // 1
|
|
93
|
-
max(arr); // 5
|
|
94
|
-
|
|
95
|
-
const sorted = [1, 2, 4, 4, 5];
|
|
96
|
-
binarySearch(sorted, 4); // 2
|
|
97
|
-
lowerBound(sorted, 4); // 2
|
|
98
|
-
upperBound(sorted, 4); // 4
|
|
99
|
-
|
|
100
|
-
// Utils
|
|
101
|
-
clamp(42, 0, 10); // 10
|
|
102
|
-
range(0, 5); // [0, 1, 2, 3, 4]
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
### 2D and 3D vectors (like C++ `vector<vector<int>>`)
|
|
106
|
-
|
|
107
|
-
`Vector<T>` is generic, so you can nest it for 2D/3D grids:
|
|
108
|
-
|
|
109
|
-
| C++ | TypeScript |
|
|
110
|
-
|-----|------------|
|
|
111
|
-
| `vector<int>` | `Vector<number>` |
|
|
112
|
-
| `vector<vector<int>>` | `Vector<Vector<number>>` |
|
|
113
|
-
| `vector<vector<vector<int>>>` | `Vector<Vector<Vector<number>>>` |
|
|
114
|
-
|
|
115
|
-
**2D example:**
|
|
116
|
-
|
|
117
|
-
```ts
|
|
118
|
-
import { Vector } from 'typescript-dsa-stl';
|
|
119
|
-
|
|
120
|
-
const grid = new Vector<Vector<number>>();
|
|
121
|
-
|
|
122
|
-
const row0 = new Vector<number>([1, 2, 3]);
|
|
123
|
-
const row1 = new Vector<number>([4, 5, 6]);
|
|
124
|
-
grid.push(row0);
|
|
125
|
-
grid.push(row1);
|
|
126
|
-
|
|
127
|
-
grid.at(0).at(1); // 2 (first row, second column)
|
|
128
|
-
grid.at(1).at(0); // 4 (second row, first column)
|
|
129
|
-
|
|
130
|
-
grid.at(0).set(1, 99);
|
|
131
|
-
grid.at(0).push(10);
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
**3D example:**
|
|
135
|
-
|
|
136
|
-
```ts
|
|
137
|
-
const cube = new Vector<Vector<Vector<number>>>();
|
|
138
|
-
|
|
139
|
-
const layer0 = new Vector<Vector<number>>();
|
|
140
|
-
layer0.push(new Vector<number>([1, 2]));
|
|
141
|
-
layer0.push(new Vector<number>([3, 4]));
|
|
142
|
-
cube.push(layer0);
|
|
143
|
-
|
|
144
|
-
cube.at(0).at(1).at(0); // 3 (layer 0, row 1, col 0)
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
import type {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
1
|
+
# TypeScript_DSA
|
|
2
|
+
|
|
3
|
+
**This is the GitHub repository** for the npm package **[typescript-dsa-stl](https://www.npmjs.com/package/typescript-dsa-stl)**.
|
|
4
|
+
|
|
5
|
+
STL-style data structures and algorithms for TypeScript: **Vector**, **Stack**, **Queue**, **List**, **PriorityQueue**, **OrderedMap** (Map), **UnorderedMap**, **OrderedSet** (Set), **UnorderedSet**, **OrderedMultiMap**, **OrderedMultiSet**, and algorithms (`sort`, `binarySearch`, `lowerBound`, `min`, `max`, etc.). Install from npm to use in your project; this repo holds the source code.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install typescript-dsa-stl
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## Quick start
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import {
|
|
21
|
+
Vector,
|
|
22
|
+
Stack,
|
|
23
|
+
Queue,
|
|
24
|
+
List,
|
|
25
|
+
PriorityQueue,
|
|
26
|
+
OrderedMap,
|
|
27
|
+
UnorderedMap,
|
|
28
|
+
OrderedSet,
|
|
29
|
+
UnorderedSet,
|
|
30
|
+
OrderedMultiMap,
|
|
31
|
+
OrderedMultiSet,
|
|
32
|
+
sort,
|
|
33
|
+
find,
|
|
34
|
+
binarySearch,
|
|
35
|
+
min,
|
|
36
|
+
max,
|
|
37
|
+
clamp,
|
|
38
|
+
range,
|
|
39
|
+
} from 'typescript-dsa-stl';
|
|
40
|
+
|
|
41
|
+
// Collections
|
|
42
|
+
const vec = new Vector<number>([1, 2, 3]);
|
|
43
|
+
vec.push(4);
|
|
44
|
+
console.log(vec.back()); // 4
|
|
45
|
+
|
|
46
|
+
const stack = new Stack<string>();
|
|
47
|
+
stack.push('a');
|
|
48
|
+
stack.push('b');
|
|
49
|
+
console.log(stack.top()); // 'b'
|
|
50
|
+
|
|
51
|
+
const queue = new Queue<number>();
|
|
52
|
+
queue.enqueue(1);
|
|
53
|
+
queue.enqueue(2);
|
|
54
|
+
console.log(queue.front()); // 1
|
|
55
|
+
|
|
56
|
+
const list = new List<number>();
|
|
57
|
+
list.pushBack(10);
|
|
58
|
+
const node = list.pushBack(20);
|
|
59
|
+
list.insertBefore(node, 15);
|
|
60
|
+
console.log(list.toArray()); // [10, 15, 20]
|
|
61
|
+
|
|
62
|
+
// PriorityQueue (max-heap by default)
|
|
63
|
+
const pq = new PriorityQueue<number>();
|
|
64
|
+
pq.push(3); pq.push(1); pq.push(4);
|
|
65
|
+
console.log(pq.top()); // 4
|
|
66
|
+
pq.pop();
|
|
67
|
+
|
|
68
|
+
// OrderedMap (keys sorted), UnorderedMap (hash)
|
|
69
|
+
const map = new UnorderedMap<string, number>();
|
|
70
|
+
map.set('a', 1); map.set('b', 2);
|
|
71
|
+
console.log(map.get('a')); // 1
|
|
72
|
+
|
|
73
|
+
// OrderedSet (sorted unique), UnorderedSet (hash)
|
|
74
|
+
const set = new UnorderedSet<number>([1, 2, 2, 3]);
|
|
75
|
+
console.log(set.size); // 3
|
|
76
|
+
|
|
77
|
+
// OrderedMultiSet (sorted, duplicates allowed)
|
|
78
|
+
const multiSet = new OrderedMultiSet<number>();
|
|
79
|
+
multiSet.add(3); multiSet.add(1); multiSet.add(2); multiSet.add(2);
|
|
80
|
+
console.log(multiSet.toArray()); // [1, 2, 2, 3]
|
|
81
|
+
console.log(multiSet.count(2)); // 2
|
|
82
|
+
|
|
83
|
+
// OrderedMultiMap (one key → multiple values, keys sorted)
|
|
84
|
+
const multiMap = new OrderedMultiMap<string, number>();
|
|
85
|
+
multiMap.set('scores', 90); multiMap.set('scores', 85); multiMap.set('scores', 95);
|
|
86
|
+
console.log(multiMap.getAll('scores')); // [90, 85, 95]
|
|
87
|
+
|
|
88
|
+
// Algorithms (work on arrays and iterables)
|
|
89
|
+
const arr = [3, 1, 4, 1, 5];
|
|
90
|
+
sort(arr); // [1, 1, 3, 4, 5]
|
|
91
|
+
find(arr, (n) => n > 3); // 4
|
|
92
|
+
min(arr); // 1
|
|
93
|
+
max(arr); // 5
|
|
94
|
+
|
|
95
|
+
const sorted = [1, 2, 4, 4, 5];
|
|
96
|
+
binarySearch(sorted, 4); // 2
|
|
97
|
+
lowerBound(sorted, 4); // 2
|
|
98
|
+
upperBound(sorted, 4); // 4
|
|
99
|
+
|
|
100
|
+
// Utils
|
|
101
|
+
clamp(42, 0, 10); // 10
|
|
102
|
+
range(0, 5); // [0, 1, 2, 3, 4]
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### 2D and 3D vectors (like C++ `vector<vector<int>>`)
|
|
106
|
+
|
|
107
|
+
`Vector<T>` is generic, so you can nest it for 2D/3D grids:
|
|
108
|
+
|
|
109
|
+
| C++ | TypeScript |
|
|
110
|
+
|-----|------------|
|
|
111
|
+
| `vector<int>` | `Vector<number>` |
|
|
112
|
+
| `vector<vector<int>>` | `Vector<Vector<number>>` |
|
|
113
|
+
| `vector<vector<vector<int>>>` | `Vector<Vector<Vector<number>>>` |
|
|
114
|
+
|
|
115
|
+
**2D example:**
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
import { Vector } from 'typescript-dsa-stl';
|
|
119
|
+
|
|
120
|
+
const grid = new Vector<Vector<number>>();
|
|
121
|
+
|
|
122
|
+
const row0 = new Vector<number>([1, 2, 3]);
|
|
123
|
+
const row1 = new Vector<number>([4, 5, 6]);
|
|
124
|
+
grid.push(row0);
|
|
125
|
+
grid.push(row1);
|
|
126
|
+
|
|
127
|
+
grid.at(0).at(1); // 2 (first row, second column)
|
|
128
|
+
grid.at(1).at(0); // 4 (second row, first column)
|
|
129
|
+
|
|
130
|
+
grid.at(0).set(1, 99);
|
|
131
|
+
grid.at(0).push(10);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
**3D example:**
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
const cube = new Vector<Vector<Vector<number>>>();
|
|
138
|
+
|
|
139
|
+
const layer0 = new Vector<Vector<number>>();
|
|
140
|
+
layer0.push(new Vector<number>([1, 2]));
|
|
141
|
+
layer0.push(new Vector<number>([3, 4]));
|
|
142
|
+
cube.push(layer0);
|
|
143
|
+
|
|
144
|
+
cube.at(0).at(1).at(0); // 3 (layer 0, row 1, col 0)
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Graph adjacency lists (like C++ `vector<vector<type>> adj(n)`)
|
|
148
|
+
|
|
149
|
+
You can model C++-style adjacency lists using the graph helper types exported from `typescript-dsa-stl/types`.
|
|
150
|
+
|
|
151
|
+
#### Unweighted adjacency list
|
|
152
|
+
|
|
153
|
+
C++:
|
|
154
|
+
|
|
155
|
+
```cpp
|
|
156
|
+
int n = 5;
|
|
157
|
+
vector<vector<int>> adj(n);
|
|
158
|
+
adj[u].push_back(v); // or adj[u].pb(v);
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
TypeScript (manual `push`):
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
import type { AdjacencyList } from 'typescript-dsa-stl/types';
|
|
165
|
+
|
|
166
|
+
const n = 5;
|
|
167
|
+
// number of vertices = n, initially all neighbors empty
|
|
168
|
+
const adj: AdjacencyList<number> = Array.from({ length: n }, () => []);
|
|
169
|
+
|
|
170
|
+
// C++: adj[u].push_back(v);
|
|
171
|
+
adj[u].push(v);
|
|
172
|
+
|
|
173
|
+
// Iteration is the same idea as in C++
|
|
174
|
+
for (const v of adj[u]) {
|
|
175
|
+
// neighbor v
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
TypeScript (with helpers `addEdge` / `deleteEdge`):
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
import type { AdjacencyList } from 'typescript-dsa-stl/types';
|
|
183
|
+
import { addEdge, deleteEdge } from 'typescript-dsa-stl/types';
|
|
184
|
+
|
|
185
|
+
const n = 5;
|
|
186
|
+
const adj: AdjacencyList<number> = Array.from({ length: n }, () => []);
|
|
187
|
+
|
|
188
|
+
addEdge(adj, u, v); // add u -> v
|
|
189
|
+
deleteEdge(adj, u, v); // remove all edges u -> v
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
#### Weighted adjacency list
|
|
193
|
+
|
|
194
|
+
In C++ you might write:
|
|
195
|
+
|
|
196
|
+
```cpp
|
|
197
|
+
int n = 5;
|
|
198
|
+
vector<vector<pair<int,int>>> adj(n);
|
|
199
|
+
adj[u].push_back({v, w}); // edge u -> v with weight w
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
In TypeScript, use `WeightedEdge` and `WeightedAdjacencyList`:
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
import type {
|
|
206
|
+
WeightedEdge,
|
|
207
|
+
WeightedAdjacencyList,
|
|
208
|
+
} from 'typescript-dsa-stl/types';
|
|
209
|
+
|
|
210
|
+
const n = 5;
|
|
211
|
+
const adj: WeightedAdjacencyList<number, number> =
|
|
212
|
+
Array.from({ length: n }, () => []);
|
|
213
|
+
|
|
214
|
+
// C++: adj[u].push_back({v, w});
|
|
215
|
+
adj[u].push({ to: v, weight: w });
|
|
216
|
+
|
|
217
|
+
// When iterating, you get both neighbor and weight
|
|
218
|
+
for (const { to, weight } of adj[u]) {
|
|
219
|
+
// edge u -> to with cost = weight
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// If you prefer a different vertex or weight type, just change the generics:
|
|
223
|
+
// const adj: WeightedAdjacencyList<string, bigint> = ...
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
Or with the helper functions `addEdge` / `deleteEdge`:
|
|
227
|
+
|
|
228
|
+
```ts
|
|
229
|
+
import type { WeightedAdjacencyList } from 'typescript-dsa-stl/types';
|
|
230
|
+
import { addEdge, deleteEdge } from 'typescript-dsa-stl/types';
|
|
231
|
+
|
|
232
|
+
const n = 5;
|
|
233
|
+
const adj: WeightedAdjacencyList<number, number> =
|
|
234
|
+
Array.from({ length: n }, () => []);
|
|
235
|
+
|
|
236
|
+
addEdge(adj, u, v, w); // add u -> v with weight w
|
|
237
|
+
deleteEdge(adj, u, v, w); // delete all edges u -> v with weight w
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
## API overview
|
|
243
|
+
|
|
244
|
+
| Module | Exports |
|
|
245
|
+
|--------|--------|
|
|
246
|
+
| **Collections** | `Vector`, `Stack`, `Queue`, `List`, `ListNode`, `PriorityQueue`, `OrderedMap`, `UnorderedMap`, `OrderedSet`, `UnorderedSet`, `OrderedMultiMap`, `OrderedMultiSet` |
|
|
247
|
+
| **Algorithms** | `sort`, `find`, `findIndex`, `transform`, `filter`, `reduce`, `reverse`, `unique`, `binarySearch`, `lowerBound`, `upperBound`, `min`, `max`, `partition` |
|
|
248
|
+
| **Utils** | `clamp`, `range`, `noop`, `identity`, `swap` |
|
|
249
|
+
| **Types** | `Comparator`, `Predicate`, `UnaryFn`, `Reducer`, `IterableLike`, `toArray`, `WeightedEdge`, `AdjacencyList`, `WeightedAdjacencyList` |
|
|
250
|
+
|
|
251
|
+
### Subpath imports (tree-shaking)
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
import { Vector, Stack } from 'typescript-dsa-stl/collections';
|
|
255
|
+
import { sort, binarySearch } from 'typescript-dsa-stl/algorithms';
|
|
256
|
+
import { clamp, range } from 'typescript-dsa-stl/utils';
|
|
257
|
+
import type { Comparator } from 'typescript-dsa-stl/types';
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
---
|
|
261
|
+
|
|
262
|
+
## Data structures
|
|
263
|
+
|
|
264
|
+
| Structure | Access | Insert end | Insert middle | Remove end | Remove middle |
|
|
265
|
+
|-----------|--------|------------|---------------|------------|---------------|
|
|
266
|
+
| **Vector** | O(1) | O(1)* | O(n) | O(1) | O(n) |
|
|
267
|
+
| **Stack** | — | O(1) | — | O(1) | — |
|
|
268
|
+
| **Queue** | — | O(1)* | — | O(1)* | — |
|
|
269
|
+
| **List** | O(n) | O(1) | O(1)** | O(1) | O(1)** |
|
|
270
|
+
| **PriorityQueue** | — | O(log n) | — | O(log n) | — |
|
|
271
|
+
| **OrderedMap** (Map) | O(log n) get | O(log n) set | — | O(log n) delete | — |
|
|
272
|
+
| **UnorderedMap** | O(1)* get/set | O(1)* | — | O(1)* delete | — |
|
|
273
|
+
| **OrderedSet** (Set) | O(log n) has | O(log n) add | — | O(log n) delete | — |
|
|
274
|
+
| **UnorderedSet** | O(1)* has/add | O(1)* | — | O(1)* delete | — |
|
|
275
|
+
| **OrderedMultiMap** | O(log n) get | O(log n) set | — | O(log n) delete | — |
|
|
276
|
+
| **OrderedMultiSet** | O(log n) has/count | O(log n) add | — | O(log n) delete | — |
|
|
277
|
+
|
|
278
|
+
\* Amortized (hash).
|
|
279
|
+
\** At a known node.
|
|
280
|
+
|
|
281
|
+
---
|
|
282
|
+
|
|
283
|
+
## OrderedMultiMap and OrderedMultiSet — use cases
|
|
284
|
+
|
|
285
|
+
**OrderedMultiSet** is a sorted collection that allows duplicate elements (like C++ `std::multiset`). Use it when you need ordering and multiple copies of the same value.
|
|
286
|
+
|
|
287
|
+
| Use case | Example |
|
|
288
|
+
|----------|---------|
|
|
289
|
+
| **Sorted runs / leaderboard with ties** | Store scores; multiple users can have the same score. Iterate in sorted order, use `count(score)` for ties. |
|
|
290
|
+
| **Event timeline with repeated timestamps** | Add events by time; several events can share the same time. `add(timestamp)`, iterate in order. |
|
|
291
|
+
| **K-th smallest in a multiset** | Keep elements sorted; k-th element is at index `k - 1` in iteration. |
|
|
292
|
+
| **Range counts** | Combined with binary search ideas: count elements in `[low, high]` using `count` and iteration. |
|
|
293
|
+
|
|
294
|
+
**OrderedMultiMap** maps one key to multiple values while keeping keys sorted (like C++ `std::multimap`). Use it when a key can have several associated values and you need key order.
|
|
295
|
+
|
|
296
|
+
| Use case | Example |
|
|
297
|
+
|----------|---------|
|
|
298
|
+
| **Inverted index** | Key = term, values = document IDs containing that term. `set(term, docId)` for each occurrence; `getAll(term)` returns all doc IDs. |
|
|
299
|
+
| **Grouping by key** | Key = category, values = items. `set(category, item)`; iterate keys in order, use `getAll(key)` per group. |
|
|
300
|
+
| **One-to-many relations** | Key = user ID, values = session IDs. `set(userId, sessionId)`; `getAll(userId)` lists all sessions. |
|
|
301
|
+
| **Time-series by bucket** | Key = time bucket, values = events. Sorted keys give chronological buckets; `getAll(bucket)` gets events in that bucket. |
|
|
302
|
+
|
|
303
|
+
### OrderedMultiSet example
|
|
304
|
+
|
|
305
|
+
```ts
|
|
306
|
+
import { OrderedMultiSet } from 'typescript-dsa-stl';
|
|
307
|
+
|
|
308
|
+
const scores = new OrderedMultiSet<number>();
|
|
309
|
+
scores.add(85); scores.add(92); scores.add(85); scores.add(78);
|
|
310
|
+
console.log(scores.toArray()); // [78, 85, 85, 92]
|
|
311
|
+
console.log(scores.count(85)); // 2
|
|
312
|
+
scores.delete(85); // remove one 85
|
|
313
|
+
console.log(scores.count(85)); // 1
|
|
314
|
+
scores.deleteAll(85); // remove all 85s
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
### OrderedMultiMap example
|
|
318
|
+
|
|
319
|
+
```ts
|
|
320
|
+
import { OrderedMultiMap } from 'typescript-dsa-stl';
|
|
321
|
+
|
|
322
|
+
const index = new OrderedMultiMap<string, number>(); // term -> doc IDs
|
|
323
|
+
index.set('typescript', 1); index.set('typescript', 3); index.set('stl', 2);
|
|
324
|
+
console.log(index.getAll('typescript')); // [1, 3]
|
|
325
|
+
console.log(index.get('stl')); // 2
|
|
326
|
+
for (const [key, value] of index) {
|
|
327
|
+
console.log(key, value); // keys in sorted order
|
|
328
|
+
}
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
---
|
|
332
|
+
|
|
333
|
+
## For maintainers
|
|
334
|
+
|
|
335
|
+
- **Build:** `npm run build` (also runs before `npm publish` via `prepublishOnly`)
|
|
336
|
+
- **Publish:** `npm publish` (use `npm publish --access public` for a scoped package name)
|
|
337
|
+
|
|
338
|
+
---
|
|
339
|
+
|
|
340
|
+
## License
|
|
341
|
+
|
|
342
|
+
MIT
|
package/dist/types/index.d.ts
CHANGED
|
@@ -13,4 +13,54 @@ export type Reducer<T, U> = (acc: U, x: T) => U;
|
|
|
13
13
|
export type IterableLike<T> = Iterable<T> | ArrayLike<T>;
|
|
14
14
|
/** Convert IterableLike to array (single copy). Used internally by algorithms. */
|
|
15
15
|
export declare function toArray<T>(source: IterableLike<T>): T[];
|
|
16
|
+
/**
|
|
17
|
+
* Graph adjacency list helpers (C++-style).
|
|
18
|
+
*
|
|
19
|
+
* These are designed so you can mirror common C++ patterns like:
|
|
20
|
+
* vector<vector<int>> adj(n);
|
|
21
|
+
* vector<vector<pair<int,int>>> adj(n);
|
|
22
|
+
*/
|
|
23
|
+
/** Single weighted edge: `to` vertex with a `weight`. */
|
|
24
|
+
export interface WeightedEdge<Vertex = number, Weight = number> {
|
|
25
|
+
to: Vertex;
|
|
26
|
+
weight: Weight;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Unweighted adjacency list.
|
|
30
|
+
*
|
|
31
|
+
* C++: `vector<vector<int>> adj(n);`
|
|
32
|
+
* TS: `AdjacencyList<number>` (backed by `number[][]`).
|
|
33
|
+
*/
|
|
34
|
+
export type AdjacencyList<Vertex = number> = Vertex[][];
|
|
35
|
+
/**
|
|
36
|
+
* Weighted adjacency list.
|
|
37
|
+
*
|
|
38
|
+
* C++: `vector<vector<pair<int,int>>> adj(n);`
|
|
39
|
+
* TS: `WeightedAdjacencyList<number, number>`
|
|
40
|
+
* (backed by `WeightedEdge<number, number>[][]`).
|
|
41
|
+
*
|
|
42
|
+
* Typical use:
|
|
43
|
+
* const adj: WeightedAdjacencyList<number, number> =
|
|
44
|
+
* Array.from({ length: n }, () => []);
|
|
45
|
+
* adj[u].push({ to: v, weight: w });
|
|
46
|
+
*/
|
|
47
|
+
export type WeightedAdjacencyList<Vertex = number, Weight = number> = WeightedEdge<Vertex, Weight>[][];
|
|
48
|
+
/**
|
|
49
|
+
* Add an edge to a graph adjacency list.
|
|
50
|
+
*
|
|
51
|
+
* Overloads:
|
|
52
|
+
* - Unweighted: addEdge(adj, u, v)
|
|
53
|
+
* - Weighted: addEdge(adj, u, v, w)
|
|
54
|
+
*/
|
|
55
|
+
export declare function addEdge(adj: AdjacencyList<number>, u: number, v: number): void;
|
|
56
|
+
export declare function addEdge(adj: WeightedAdjacencyList<number, number>, u: number, v: number, w: number): void;
|
|
57
|
+
/**
|
|
58
|
+
* Delete all edges u -> v from a graph adjacency list.
|
|
59
|
+
*
|
|
60
|
+
* Overloads:
|
|
61
|
+
* - Unweighted: deleteEdge(adj, u, v)
|
|
62
|
+
* - Weighted: deleteEdge(adj, u, v, w)
|
|
63
|
+
*/
|
|
64
|
+
export declare function deleteEdge(adj: AdjacencyList<number>, u: number, v: number): void;
|
|
65
|
+
export declare function deleteEdge(adj: WeightedAdjacencyList<number, number>, u: number, v: number, w: number): void;
|
|
16
66
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAA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C1C,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,GACR,IAAI,CAAC"}
|
package/dist/types/index.js
CHANGED
|
@@ -10,4 +10,34 @@ export function toArray(source) {
|
|
|
10
10
|
}
|
|
11
11
|
return Array.from(source);
|
|
12
12
|
}
|
|
13
|
+
export function addEdge(adj, u, v, w) {
|
|
14
|
+
if (w === undefined) {
|
|
15
|
+
adj[u].push(v);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
adj[u].push({ to: v, weight: w });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export function deleteEdge(adj, u, v, w) {
|
|
22
|
+
const row = adj[u];
|
|
23
|
+
if (!row)
|
|
24
|
+
return;
|
|
25
|
+
if (w === undefined) {
|
|
26
|
+
// Unweighted: remove all neighbors equal to v.
|
|
27
|
+
for (let i = row.length - 1; i >= 0; i--) {
|
|
28
|
+
if (row[i] === v) {
|
|
29
|
+
row.splice(i, 1);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
// Weighted: remove all edges with { to: v, weight: w }.
|
|
35
|
+
for (let i = row.length - 1; i >= 0; i--) {
|
|
36
|
+
const edge = row[i];
|
|
37
|
+
if (edge.to === v && edge.weight === w) {
|
|
38
|
+
row.splice(i, 1);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
13
43
|
//# sourceMappingURL=index.js.map
|
package/dist/types/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAiBH,kFAAkF;AAClF,MAAM,UAAU,OAAO,CAAI,MAAuB;IAChD,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC;QAAE,OAAO,MAAM,CAAC;IACzC,IAAI,OAAQ,MAAsB,CAAC,MAAM,CAAC,QA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E,CAAC,EAAE,EAAE,CAAC;YACzC,IAAI,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC;gBACjB,GAAG,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;YACnB,CAAC;QACH,CAAC;IACH,CAAC;SAAM,CAAC;QACN,wDAAwD;QACxD,KAAK,IAAI,CAAC,GAAG,GAAG,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YACzC,MAAM,IAAI,GAAG,GAAG,CAAC,CAAC,CAAiC,CAAC;YACpD,IAAI,IAAI,CAAC,EAAE,KAAK,CAAC,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBACvC,GAAG,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;YACnB,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-dsa-stl",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "STL-style data structures and algorithms for TypeScript: Vector, Stack, Queue, List, PriorityQueue, Map, Set, sort, binarySearch. Use like C++ STL.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|