typescript-dsa-stl 2.5.0 → 2.7.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 CHANGED
@@ -1,17 +1,58 @@
1
1
  # TypeScript_DSA
2
2
 
3
- **This is the GitHub repository** for the npm package **[typescript-dsa-stl](https://www.npmjs.com/package/typescript-dsa-stl)**.
3
+ **Repository** for the npm package **[typescript-dsa-stl](https://www.npmjs.com/package/typescript-dsa-stl)** · [GitHub](https://github.com/SajidAbdullah729/TypeScript_DSA)
4
4
 
5
- STL-style data structures and algorithms for TypeScript: **Vector**, **Stack**, **Queue**, **List**, **PriorityQueue**, **OrderedMap** (Map), **UnorderedMap**, **OrderedSet** (Set), **UnorderedSet**, **OrderedMultiMap**, **OrderedMultiSet**, **segment trees** (`SegmentTreeSum`, `SegmentTreeMin`, `SegmentTreeMax`, `SegmentTree`, `GeneralSegmentTree`, `LazySegmentTreeSum`), and algorithms (`sort`, `binarySearch`, `lowerBound`, `min`, `max`, **KnuthMorrisPratt**, **RabinKarp**, **StringRollingHash**, etc.). Install from npm to use in your project; this repo holds the source code.
5
+ STL-style data structures and algorithms for TypeScript: **Vector**, **Stack**, **Queue**, **Deque**, **List**, **PriorityQueue**, ordered/unordered **Map** and **Set**, **OrderedMultiMap** / **OrderedMultiSet**, **segment trees**, **graph** helpers (BFS, DFS, Dijkstra, Kruskal, DSU), and **string** algorithms (KMP, Rabin–Karp, rolling hash). Install from npm for your app; this repo is the source.
6
6
 
7
7
  ---
8
8
 
9
- ## Install
9
+ ## Table of contents
10
+
11
+ | Section | What you’ll find |
12
+ |--------|-------------------|
13
+ | [Installation](#installation) | npm install |
14
+ | [Package layout & imports](#package-layout--imports) | Barrel vs subpaths (tree-shaking) |
15
+ | [Quick start](#quick-start) | One file showing main APIs |
16
+ | [API reference](#api-reference) | Export tables |
17
+ | [Complexity](#complexity) | Big-O for collections and segment trees |
18
+ | [Collections](#collections) | Deque, nested vectors, multi-map / multi-set |
19
+ | [Segment trees](#segment-trees) | Sum/min/max, generic, lazy |
20
+ | [Graph algorithms](#graph-algorithms) | Adjacency lists, BFS/DFS, components, MST, shortest paths |
21
+ | [String algorithms](#string-algorithms) | KMP, Rabin–Karp, rolling hash |
22
+ | [For maintainers](#for-maintainers) | Build and publish |
23
+ | [License](#license) | MIT |
24
+
25
+ ---
26
+
27
+ ## Installation
10
28
 
11
29
  ```bash
12
30
  npm install typescript-dsa-stl
13
31
  ```
14
32
 
33
+ **Runtime:** Node **18+** (see `package.json` `engines`).
34
+
35
+ ---
36
+
37
+ ## Package layout & imports
38
+
39
+ Import everything from the root, or use subpaths for smaller bundles:
40
+
41
+ ```ts
42
+ import { Vector, Stack, Queue, Deque } from 'typescript-dsa-stl/collections';
43
+ import {
44
+ sort,
45
+ binarySearch,
46
+ breadthFirstSearch,
47
+ depthFirstSearch,
48
+ KnuthMorrisPratt,
49
+ RabinKarp,
50
+ StringRollingHash,
51
+ } from 'typescript-dsa-stl/algorithms';
52
+ import { clamp, range } from 'typescript-dsa-stl/utils';
53
+ import type { Comparator } from 'typescript-dsa-stl/types';
54
+ ```
55
+
15
56
  ---
16
57
 
17
58
  ## Quick start
@@ -21,6 +62,7 @@ import {
21
62
  Vector,
22
63
  Stack,
23
64
  Queue,
65
+ Deque,
24
66
  List,
25
67
  PriorityQueue,
26
68
  OrderedMap,
@@ -32,6 +74,8 @@ import {
32
74
  sort,
33
75
  find,
34
76
  binarySearch,
77
+ lowerBound,
78
+ upperBound,
35
79
  min,
36
80
  max,
37
81
  clamp,
@@ -53,6 +97,15 @@ queue.enqueue(1);
53
97
  queue.enqueue(2);
54
98
  console.log(queue.front()); // 1
55
99
 
100
+ // Deque — double-ended queue (like C++ std::deque): O(1) both ends + O(1) index access
101
+ const deque = new Deque<number>();
102
+ deque.pushBack(2);
103
+ deque.pushFront(1);
104
+ deque.pushBack(3);
105
+ console.log(deque.front()); // 1
106
+ console.log(deque.back()); // 3
107
+ console.log(deque.at(1)); // 2
108
+
56
109
  const list = new List<number>();
57
110
  list.pushBack(10);
58
111
  const node = list.pushBack(20);
@@ -102,6 +155,87 @@ clamp(42, 0, 10); // 10
102
155
  range(0, 5); // [0, 1, 2, 3, 4]
103
156
  ```
104
157
 
158
+ ---
159
+
160
+ ## API reference
161
+
162
+ ### Main export map
163
+
164
+ | Area | Exports |
165
+ |------|---------|
166
+ | **Collections** | `Vector`, `Stack`, `Queue`, `Deque`, `List`, `ListNode`, `PriorityQueue`, `OrderedMap`, `UnorderedMap`, `OrderedSet`, `UnorderedSet`, `OrderedMultiMap`, `OrderedMultiSet`, `GeneralSegmentTree`, `SegmentTree`, `SegmentTreeSum`, `SegmentTreeMin`, `SegmentTreeMax`, `LazySegmentTreeSum`, `WeightedEdge`, `AdjacencyList`, `WeightedAdjacencyList`, `createAdjacencyList`, `createWeightedAdjacencyList`, `addEdge`, `deleteEdge` |
167
+ | **Algorithms** | `sort`, `find`, `findIndex`, `transform`, `filter`, `reduce`, `reverse`, `unique`, `binarySearch`, `lowerBound`, `upperBound`, `min`, `max`, `partition`, `DisjointSetUnion`, `KnuthMorrisPratt`, `RabinKarp`, `RABIN_KARP_DEFAULT_MODS`, `StringRollingHash`, `breadthFirstSearch`, `depthFirstSearch`, `connectedComponents`, `kruskalMST`, `dijkstra`, `reconstructPath` |
168
+ | **Utils** | `clamp`, `range`, `noop`, `identity`, `swap` |
169
+ | **Types** | `Comparator`, `Predicate`, `UnaryFn`, `Reducer`, `IterableLike`, `toArray`, `RabinKarpTripleMods`, `WeightedUndirectedEdge`, `GeneralSegmentTreeConfig`, `SegmentCombine`, `SegmentMerge`, `SegmentLeafBuild` |
170
+
171
+ ### Subpath imports (tree-shaking)
172
+
173
+ ```ts
174
+ import { Vector, Stack, Queue, Deque } from 'typescript-dsa-stl/collections';
175
+ import { sort, binarySearch, breadthFirstSearch, depthFirstSearch, KnuthMorrisPratt, RabinKarp, StringRollingHash } from 'typescript-dsa-stl/algorithms';
176
+ import { clamp, range } from 'typescript-dsa-stl/utils';
177
+ import type { Comparator } from 'typescript-dsa-stl/types';
178
+ ```
179
+
180
+ ---
181
+
182
+ ## Complexity
183
+
184
+ ### Linear and associative structures
185
+
186
+ | Structure | Access | Insert end | Insert middle | Remove end | Remove middle |
187
+ |-----------|--------|------------|---------------|------------|---------------|
188
+ | **Vector** | O(1) | O(1)* | O(n) | O(1) | O(n) |
189
+ | **Stack** | — | O(1) | — | O(1) | — |
190
+ | **Queue** | — | O(1)* | — | O(1)* | — |
191
+ | **Deque** | O(1) | O(1)* (front/back) | — | O(1)* (front/back) | — |
192
+ | **List** | O(n) | O(1) | O(1)** | O(1) | O(1)** |
193
+ | **PriorityQueue** | — | O(log n) | — | O(log n) | — |
194
+ | **OrderedMap** (Map) | O(log n) get | O(log n) set | — | O(log n) delete | — |
195
+ | **UnorderedMap** | O(1)* get/set | O(1)* | — | O(1)* delete | — |
196
+ | **OrderedSet** (Set) | O(log n) has | O(log n) add | — | O(log n) delete | — |
197
+ | **UnorderedSet** | O(1)* has/add | O(1)* | — | O(1)* delete | — |
198
+ | **OrderedMultiMap** | O(log n) get | O(log n) set | — | O(log n) delete | — |
199
+ | **OrderedMultiSet** | O(log n) has/count | O(log n) add | — | O(log n) delete | — |
200
+
201
+ \* Amortized (hash).
202
+ \** At a known node.
203
+
204
+ ### Segment trees
205
+
206
+ | Structure | Build | Point update | Range query | Extra |
207
+ |-----------|-------|--------------|-------------|--------|
208
+ | **GeneralSegmentTree**, **SegmentTree**, **SegmentTreeSum** / **Min** / **Max** | O(n) | O(log n) | O(log n) | Inclusive `[l, r]`; **GeneralSegmentTree** keeps raw `V` and uses `merge` + `buildLeaf` |
209
+ | **LazySegmentTreeSum** | O(n) | `set`: O(log n) | `rangeSum`: O(log n) | `rangeAdd` on a range: O(log n) |
210
+
211
+ ---
212
+
213
+ ## Collections
214
+
215
+ ### Deque (like C++ `std::deque`)
216
+
217
+ A **double-ended queue**: amortized **O(1)** `pushFront` / `pushBack` / `popFront` / `popBack`, and **O(1)** random access via `at` / `set`. Implemented as a growable circular buffer (same asymptotics as a typical `std::deque` for these operations).
218
+
219
+ | C++ | TypeScript |
220
+ |-----|------------|
221
+ | `push_front` | `pushFront` |
222
+ | `push_back` | `pushBack` |
223
+ | `pop_front` | `popFront` |
224
+ | `pop_back` | `popBack` |
225
+ | `front` / `back` | `front()` / `back()` |
226
+ | `operator[]` / `at` | `at(i)` / `set(i, value)` |
227
+ | `size` / `empty` | `size` / `empty` |
228
+ | — | `capacity`, `reserve`, `shrinkToFit`, `toArray()`, iterator |
229
+
230
+ ```ts
231
+ import { Deque } from 'typescript-dsa-stl';
232
+
233
+ const d = new Deque<number>([1, 2, 3]); // copy initial elements, or `new Deque()` / `new Deque(64)` for capacity hint
234
+ d.pushFront(0);
235
+ d.popBack();
236
+ console.log(d.toArray()); // [0, 1, 2]
237
+ ```
238
+
105
239
  ### 2D and 3D vectors (like C++ `vector<vector<int>>`)
106
240
 
107
241
  `Vector<T>` is generic, so you can nest it for 2D/3D grids:
@@ -144,127 +278,339 @@ cube.push(layer0);
144
278
  cube.at(0).at(1).at(0); // 3 (layer 0, row 1, col 0)
145
279
  ```
146
280
 
147
- ### Graph adjacency list (like C++ `vector<vector<type>> graph(n)`)
281
+ ### OrderedMultiMap and OrderedMultiSet use cases
148
282
 
149
- You can model C++-style adjacency lists using the graph types and helpers exported from `typescript-dsa-stl/collections` (or the main package).
283
+ **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.
150
284
 
151
- #### Unweighted adjacency list
285
+ | Use case | Example |
286
+ |----------|---------|
287
+ | **Sorted runs / leaderboard with ties** | Store scores; multiple users can have the same score. Iterate in sorted order, use `count(score)` for ties. |
288
+ | **Event timeline with repeated timestamps** | Add events by time; several events can share the same time. `add(timestamp)`, iterate in order. |
289
+ | **K-th smallest in a multiset** | Keep elements sorted; k-th element is at index `k - 1` in iteration. |
290
+ | **Range counts** | Combined with binary search ideas: count elements in `[low, high]` using `count` and iteration. |
152
291
 
153
- C++:
292
+ **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.
154
293
 
155
- ```cpp
156
- int n = 5;
157
- vector<vector<int>> graph(n);
158
- graph[u].push_back(v); // or graph[u].pb(v);
159
- ```
294
+ | Use case | Example |
295
+ |----------|---------|
296
+ | **Inverted index** | Key = term, values = document IDs containing that term. `set(term, docId)` for each occurrence; `getAll(term)` returns all doc IDs. |
297
+ | **Grouping by key** | Key = category, values = items. `set(category, item)`; iterate keys in order, use `getAll(key)` per group. |
298
+ | **One-to-many relations** | Key = user ID, values = session IDs. `set(userId, sessionId)`; `getAll(userId)` lists all sessions. |
299
+ | **Time-series by bucket** | Key = time bucket, values = events. Sorted keys give chronological buckets; `getAll(bucket)` gets events in that bucket. |
160
300
 
161
- TypeScript (easy declaration with `createAdjacencyList`):
301
+ **OrderedMultiSet example:**
162
302
 
163
303
  ```ts
164
- import { createAdjacencyList } from 'typescript-dsa-stl/collections';
165
-
166
- const n = 5;
167
- const graph = createAdjacencyList(n); // empty graph with n vertices
168
-
169
- // C++: graph[u].push_back(v);
170
- graph[u].push(v);
304
+ import { OrderedMultiSet } from 'typescript-dsa-stl';
171
305
 
172
- // Iteration is the same idea as in C++
173
- for (const v of graph[u]) {
174
- // neighbor v
175
- }
306
+ const scores = new OrderedMultiSet<number>();
307
+ scores.add(85); scores.add(92); scores.add(85); scores.add(78);
308
+ console.log(scores.toArray()); // [78, 85, 85, 92]
309
+ console.log(scores.count(85)); // 2
310
+ scores.delete(85); // remove one 85
311
+ console.log(scores.count(85)); // 1
312
+ scores.deleteAll(85); // remove all 85s
176
313
  ```
177
314
 
178
- Or with helpers `addEdge` / `deleteEdge`:
315
+ **OrderedMultiMap example:**
179
316
 
180
317
  ```ts
181
- import { createAdjacencyList, addEdge, deleteEdge } from 'typescript-dsa-stl/collections';
182
-
183
- const graph = createAdjacencyList(5);
318
+ import { OrderedMultiMap } from 'typescript-dsa-stl';
184
319
 
185
- addEdge(graph, u, v); // add u -> v
186
- deleteEdge(graph, u, v); // remove all edges u -> v
320
+ const index = new OrderedMultiMap<string, number>(); // term -> doc IDs
321
+ index.set('typescript', 1); index.set('typescript', 3); index.set('stl', 2);
322
+ console.log(index.getAll('typescript')); // [1, 3]
323
+ console.log(index.get('stl')); // 2
324
+ for (const [key, value] of index) {
325
+ console.log(key, value); // keys in sorted order
326
+ }
187
327
  ```
188
328
 
189
- #### Weighted adjacency list
329
+ ---
190
330
 
191
- In C++ you might write:
331
+ ## Segment trees
192
332
 
193
- ```cpp
194
- int n = 5;
195
- vector<vector<pair<int,int>>> graph(n);
196
- graph[u].push_back({v, w}); // edge u -> v with weight w
197
- ```
333
+ Segment trees support **range queries** and **point updates** in **O(log n)**. Range endpoints are **inclusive**: `query(l, r)` covers indices `l` through `r`.
198
334
 
199
- In TypeScript, use `createWeightedAdjacencyList` for easy declaration:
335
+ ### Ready-made variants (`SegmentTreeSum`, `SegmentTreeMin`, `SegmentTreeMax`)
200
336
 
201
337
  ```ts
202
- import { createWeightedAdjacencyList } from 'typescript-dsa-stl/collections';
338
+ import {
339
+ SegmentTreeSum,
340
+ SegmentTreeMin,
341
+ SegmentTreeMax,
342
+ } from 'typescript-dsa-stl';
203
343
 
204
- const n = 5;
205
- const graph = createWeightedAdjacencyList(n); // empty weighted graph with n vertices
344
+ const sum = new SegmentTreeSum([1, 2, 3, 4]);
345
+ console.log(sum.query(0, 3)); // 10
346
+ sum.update(1, 10);
347
+ console.log(sum.query(0, 3)); // 1 + 10 + 3 + 4 = 18
206
348
 
207
- // C++: graph[u].push_back({v, w});
208
- graph[u].push({ to: v, weight: w });
349
+ const mn = new SegmentTreeMin([5, 2, 8, 1]);
350
+ console.log(mn.query(0, 3)); // 1
209
351
 
210
- // When iterating, you get both neighbor and weight
211
- for (const { to, weight } of graph[u]) {
212
- // edge u -> to with cost = weight
213
- }
352
+ const mx = new SegmentTreeMax([5, 2, 8, 1]);
353
+ console.log(mx.query(0, 3)); // 8
214
354
  ```
215
355
 
216
- Or with the helper functions `addEdge` / `deleteEdge`:
356
+ ### Generic `SegmentTree<T>` (custom combine + neutral)
357
+
358
+ Use the same type for array elements and aggregates. Pass an **associative** `combine` and a **neutral** value for query ranges that miss a segment (e.g. `0` for sum, `Infinity` for min).
217
359
 
218
360
  ```ts
219
- import { createWeightedAdjacencyList, addEdge, deleteEdge } from 'typescript-dsa-stl/collections';
361
+ import { SegmentTree } from 'typescript-dsa-stl';
220
362
 
221
- const graph = createWeightedAdjacencyList(5);
363
+ const gcdTree = new SegmentTree<number>(
364
+ [12, 18, 24],
365
+ (a, b) => {
366
+ let x = a;
367
+ let y = b;
368
+ while (y !== 0) {
369
+ const t = y;
370
+ y = x % y;
371
+ x = t;
372
+ }
373
+ return x;
374
+ },
375
+ 0
376
+ );
377
+ console.log(gcdTree.query(0, 2)); // gcd(12, 18, 24) === 6
222
378
 
223
- addEdge(graph, u, v, w); // add u -> v with weight w
224
- deleteEdge(graph, u, v, w); // delete all edges u -> v with weight w
379
+ // Non-numeric example: concatenate strings
380
+ const strTree = new SegmentTree<string>(
381
+ ['a', 'b', 'c'],
382
+ (a, b) => a + b,
383
+ ''
384
+ );
385
+ console.log(strTree.query(0, 2)); // 'abc'
225
386
  ```
226
387
 
227
- #### Graph adjacency list use cases
388
+ ### `GeneralSegmentTree<T, V>` (custom merge + buildLeaf)
228
389
 
229
- Use an **unweighted** graph (adjacency list) when you only care about connectivity; use a **weighted** graph when edges have costs (distance, time, capacity).
390
+ Use when **raw** values `V` differ from the **aggregate** type `T`:
230
391
 
231
- | Use case | When to use |
232
- |----------|-------------|
233
- | **BFS / DFS, connectivity** | Unweighted: shortest path in terms of hop count, connected components, cycle detection. |
234
- | **Shortest path (Dijkstra), MST** | Weighted: edge weights as distances or costs; run Dijkstra, Prim, or Kruskal on the list. |
235
- | **Social / dependency graphs** | Unweighted or weighted: followers, dependencies (e.g. build order), recommendation graphs. |
236
- | **Grid / game graphs** | Unweighted: 4- or 8-neighbor grids; weighted if movement costs differ per cell. |
237
- | **Network / flow** | Weighted: capacities or latencies on edges for max-flow or routing. |
392
+ - **`merge(left, right)`** combine two child aggregates (internal nodes).
393
+ - **`neutral`** — identity for `merge` when a query does not overlap a segment.
394
+ - **`buildLeaf(value, index)`** build the leaf from the raw array on initial construction and on every `update`.
238
395
 
239
- #### Breadth-first search (BFS) and depth-first search (DFS)
396
+ ```ts
397
+ import { GeneralSegmentTree } from 'typescript-dsa-stl';
240
398
 
241
- `breadthFirstSearch` and `depthFirstSearch` take the number of vertices `n`, an unweighted `AdjacencyList`, and a `start` vertex. They return the **visit order** for all vertices **reachable** from `start` (vertices outside that component are not included). For an undirected graph, add each edge in **both** directions (see `addEdge` below).
399
+ // Store sum of squares; raw array is plain numbers
400
+ const st = new GeneralSegmentTree<number, number>([1, 2, 3], {
401
+ merge: (a, b) => a + b,
402
+ neutral: 0,
403
+ buildLeaf: (v, i) => v * v + i,
404
+ });
405
+ console.log(st.query(0, 2)); // (1+0) + (4+1) + (9+2) = 17
406
+ st.update(1, 4);
407
+ console.log(st.rawAt(1)); // 4 — current raw value at index 1
408
+ ```
242
409
 
243
- **Example graph (diamond):** edges `0—1`, `0—2`, `1—3`, `2—3`.
410
+ ### `LazySegmentTreeSum` (range add + range sum)
244
411
 
245
- ```text
246
- 0
247
- / \
248
- 1 2
249
- \ /
250
- 3
412
+ **`rangeAdd(l, r, delta)`** adds `delta` to every element in the inclusive range. **`rangeSum(l, r)`** returns the sum. **`set(i, value)`** assigns one position (lazy tags are applied along the path). All are **O(log n)**.
413
+
414
+ ```ts
415
+ import { LazySegmentTreeSum } from 'typescript-dsa-stl';
416
+
417
+ const lazy = new LazySegmentTreeSum([0, 0, 0, 0]);
418
+ lazy.rangeAdd(1, 2, 5); // indices 1 and 2 get +5
419
+ console.log(lazy.rangeSum(0, 3)); // 10
420
+ lazy.set(0, 100);
421
+ console.log(lazy.rangeSum(0, 3)); // 100 + 5 + 5 + 0
251
422
  ```
252
423
 
253
- With neighbors listed in ascending vertex id (`0: [1,2]`, `1: [0,3]`, …), **BFS** from `0` visits by increasing distance from `0`: first `0`, then `1` and `2`, then `3` → order `[0, 1, 2, 3]`. **DFS** (preorder, first neighbor in each list first) goes `0 → 1 → 3` then `2` → order `[0, 1, 3, 2]`. The exact DFS order depends on how you order each adjacency list.
424
+ ### Real-world use cases
254
425
 
255
- ```ts
256
- import {
257
- createAdjacencyList,
258
- addEdge,
259
- breadthFirstSearch,
260
- depthFirstSearch,
261
- } from 'typescript-dsa-stl';
426
+ These patterns show up in backends and internal tools when you need **many** range queries and updates on a fixed sequence (length known up front), without scanning the whole array each time.
262
427
 
263
- const n = 4;
264
- const graph = createAdjacencyList(n);
428
+ #### 1. Analytics or reporting: totals over a time window (with corrections)
265
429
 
266
- // Undirected diamond: add both directions for each edge
267
- addEdge(graph, 0, 1);
430
+ Each index is a **fixed bucket** (hour of day, day of month, version slot, etc.). You repeatedly ask “what is the **sum** from bucket `a` through `b`?” and sometimes **fix one bucket** after late data or a reconciliation.
431
+
432
+ ```ts
433
+ import { SegmentTreeSum } from 'typescript-dsa-stl';
434
+
435
+ /** Revenue (or events, page views, API calls) per calendar day; index 0 = first day of period. */
436
+ class PeriodMetrics {
437
+ private readonly tree: SegmentTreeSum;
438
+
439
+ constructor(dailyValues: readonly number[]) {
440
+ this.tree = new SegmentTreeSum(dailyValues);
441
+ }
442
+
443
+ /** Total for an inclusive day range — e.g. chart drill-down or export row. */
444
+ totalBetweenDay(firstDayIndex: number, lastDayIndex: number): number {
445
+ return this.tree.query(firstDayIndex, lastDayIndex);
446
+ }
447
+
448
+ /** Backfill or correct one day without rebuilding the whole series. */
449
+ setDay(dayIndex: number, amount: number): void {
450
+ this.tree.update(dayIndex, amount);
451
+ }
452
+ }
453
+
454
+ const january = new PeriodMetrics([1200, 980, 1100, 1050, 1300]);
455
+ console.log(january.totalBetweenDay(0, 4)); // full period
456
+ january.setDay(2, 1150); // corrected day 2
457
+ console.log(january.totalBetweenDay(1, 3)); // sum over days 1..3
458
+ ```
459
+
460
+ In production you would usually **persist** the underlying series in a database and **rebuild** the tree when the period reloads; the tree stays useful in memory for dashboards, simulations, or request handlers that see heavy read/update traffic on the same window.
461
+
462
+ #### 2. Operations or finance: bulk adjustment on a slice, then aggregate
463
+
464
+ You apply the **same delta** to **every** element in an index range (tiered bonuses, prorated credits, simulation shocks), then need **range sums** for reporting. A lazy sum tree avoids touching each cell one by one.
465
+
466
+ ```ts
467
+ import { LazySegmentTreeSum } from 'typescript-dsa-stl';
468
+
469
+ /** Example: per-seat or per-row amounts; apply a flat bonus to ranks 10–50 (0-based 9..49), then sum a sub-range for a sub-team. */
470
+ function simulateBulkBonusAndSubtotal(seatCount: number): void {
471
+ // Initial per-seat values (e.g. base commission), built once
472
+ const base = Array.from({ length: seatCount }, (_, i) => 100 + i);
473
+ const amounts = new LazySegmentTreeSum(base);
474
+
475
+ // HR: +250 to everyone in seats 10–50 inclusive (indices 9..49)
476
+ amounts.rangeAdd(9, 49, 250);
477
+
478
+ // Finance: subtotal for seats 20–30 only
479
+ console.log(amounts.rangeSum(19, 29));
480
+ }
481
+
482
+ simulateBulkBonusAndSubtotal(100);
483
+ ```
484
+
485
+ The same idea applies to **inventory deltas** across bin ranges, **loyalty points** batch credits by user-ID band (when IDs map to contiguous indices), or **game/simulation** state where many cells gain the same buff and you query partial totals.
486
+
487
+ ---
488
+
489
+ ## Graph algorithms
490
+
491
+ Graph helpers live on the main package and under `typescript-dsa-stl/collections` for adjacency types and factories.
492
+
493
+ ### Adjacency list (like C++ `vector<vector<type>> graph(n)`)
494
+
495
+ You can model C++-style adjacency lists using the graph types and helpers exported from `typescript-dsa-stl/collections` (or the main package).
496
+
497
+ #### Unweighted adjacency list
498
+
499
+ C++:
500
+
501
+ ```cpp
502
+ int n = 5;
503
+ vector<vector<int>> graph(n);
504
+ graph[u].push_back(v); // or graph[u].pb(v);
505
+ ```
506
+
507
+ TypeScript (easy declaration with `createAdjacencyList`):
508
+
509
+ ```ts
510
+ import { createAdjacencyList } from 'typescript-dsa-stl/collections';
511
+
512
+ const n = 5;
513
+ const graph = createAdjacencyList(n); // empty graph with n vertices
514
+
515
+ // C++: graph[u].push_back(v);
516
+ graph[u].push(v);
517
+
518
+ // Iteration is the same idea as in C++
519
+ for (const v of graph[u]) {
520
+ // neighbor v
521
+ }
522
+ ```
523
+
524
+ Or with helpers `addEdge` / `deleteEdge`:
525
+
526
+ ```ts
527
+ import { createAdjacencyList, addEdge, deleteEdge } from 'typescript-dsa-stl/collections';
528
+
529
+ const graph = createAdjacencyList(5);
530
+
531
+ addEdge(graph, u, v); // add u -> v
532
+ deleteEdge(graph, u, v); // remove all edges u -> v
533
+ ```
534
+
535
+ #### Weighted adjacency list
536
+
537
+ In C++ you might write:
538
+
539
+ ```cpp
540
+ int n = 5;
541
+ vector<vector<pair<int,int>>> graph(n);
542
+ graph[u].push_back({v, w}); // edge u -> v with weight w
543
+ ```
544
+
545
+ In TypeScript, use `createWeightedAdjacencyList` for easy declaration:
546
+
547
+ ```ts
548
+ import { createWeightedAdjacencyList } from 'typescript-dsa-stl/collections';
549
+
550
+ const n = 5;
551
+ const graph = createWeightedAdjacencyList(n); // empty weighted graph with n vertices
552
+
553
+ // C++: graph[u].push_back({v, w});
554
+ graph[u].push({ to: v, weight: w });
555
+
556
+ // When iterating, you get both neighbor and weight
557
+ for (const { to, weight } of graph[u]) {
558
+ // edge u -> to with cost = weight
559
+ }
560
+ ```
561
+
562
+ Or with the helper functions `addEdge` / `deleteEdge`:
563
+
564
+ ```ts
565
+ import { createWeightedAdjacencyList, addEdge, deleteEdge } from 'typescript-dsa-stl/collections';
566
+
567
+ const graph = createWeightedAdjacencyList(5);
568
+
569
+ addEdge(graph, u, v, w); // add u -> v with weight w
570
+ deleteEdge(graph, u, v, w); // delete all edges u -> v with weight w
571
+ ```
572
+
573
+ #### Graph adjacency list — use cases
574
+
575
+ Use an **unweighted** graph (adjacency list) when you only care about connectivity; use a **weighted** graph when edges have costs (distance, time, capacity).
576
+
577
+ | Use case | When to use |
578
+ |----------|-------------|
579
+ | **BFS / DFS, connectivity** | Unweighted: shortest path in terms of hop count, connected components, cycle detection. |
580
+ | **Shortest path (Dijkstra), MST** | Weighted: edge weights as distances or costs; run Dijkstra, Prim, or Kruskal on the list. |
581
+ | **Social / dependency graphs** | Unweighted or weighted: followers, dependencies (e.g. build order), recommendation graphs. |
582
+ | **Grid / game graphs** | Unweighted: 4- or 8-neighbor grids; weighted if movement costs differ per cell. |
583
+ | **Network / flow** | Weighted: capacities or latencies on edges for max-flow or routing. |
584
+
585
+ ### Breadth-first search (BFS) and depth-first search (DFS)
586
+
587
+ `breadthFirstSearch` and `depthFirstSearch` take the number of vertices `n`, an unweighted `AdjacencyList`, and a `start` vertex. They return the **visit order** for all vertices **reachable** from `start` (vertices outside that component are not included). For an undirected graph, add each edge in **both** directions (see `addEdge` below).
588
+
589
+ **Example graph (diamond):** edges `0—1`, `0—2`, `1—3`, `2—3`.
590
+
591
+ ```text
592
+ 0
593
+ / \
594
+ 1 2
595
+ \ /
596
+ 3
597
+ ```
598
+
599
+ With neighbors listed in ascending vertex id (`0: [1,2]`, `1: [0,3]`, …), **BFS** from `0` visits by increasing distance from `0`: first `0`, then `1` and `2`, then `3` → order `[0, 1, 2, 3]`. **DFS** (preorder, first neighbor in each list first) goes `0 → 1 → 3` then `2` → order `[0, 1, 3, 2]`. The exact DFS order depends on how you order each adjacency list.
600
+
601
+ ```ts
602
+ import {
603
+ createAdjacencyList,
604
+ addEdge,
605
+ breadthFirstSearch,
606
+ depthFirstSearch,
607
+ } from 'typescript-dsa-stl';
608
+
609
+ const n = 4;
610
+ const graph = createAdjacencyList(n);
611
+
612
+ // Undirected diamond: add both directions for each edge
613
+ addEdge(graph, 0, 1);
268
614
  addEdge(graph, 1, 0);
269
615
  addEdge(graph, 0, 2);
270
616
  addEdge(graph, 2, 0);
@@ -300,7 +646,7 @@ console.log(breadthFirstSearch(5, withIsolated, 0)); // [0, 1] — not [0,1,2,3,
300
646
  - **Disconnected graphs:** run again from another unvisited `start`, or use `connectedComponents` to enumerate components first.
301
647
  - **Weighted graphs:** for traversal ignoring weights, use the same vertex lists as the unweighted graph (weights are ignored by these two functions).
302
648
 
303
- #### Disjoint Set Union (Union-Find)
649
+ ### Disjoint Set Union (Union-Find)
304
650
 
305
651
  Use Union-Find (DSU) to compute connected components efficiently. It merges endpoints of every edge in the adjacency list, so for directed graphs it returns weak connectivity components.
306
652
 
@@ -318,7 +664,7 @@ const comps = connectedComponents(n, graph);
318
664
  // e.g. [[0, 1], [2], [3, 4]]
319
665
  ```
320
666
 
321
- ##### Traverse the result
667
+ #### Traverse the result
322
668
 
323
669
  `connectedComponents(n, adj)` returns `number[][]` where each inner array is a component (list of vertices).
324
670
 
@@ -335,7 +681,7 @@ for (const comp of comps) {
335
681
  const sizes = comps.map(comp => comp.length);
336
682
  ```
337
683
 
338
- #### Kruskal MST (uses DSU)
684
+ ### Kruskal MST (uses DSU)
339
685
 
340
686
  For a weighted graph, `kruskalMST` builds a Minimum Spanning Tree (MST) using DSU.
341
687
 
@@ -359,7 +705,28 @@ const { edges, totalWeight } = kruskalMST(n, wGraph, { undirected: true });
359
705
  // edges: MST edges (chosen by weight), totalWeight: sum of weights
360
706
  ```
361
707
 
362
- #### Dijkstra shortest paths
708
+ #### Traverse the MST
709
+
710
+ `kruskalMST(...)` returns `{ edges, totalWeight }`. To traverse the MST like a graph, convert `edges` into an adjacency list:
711
+
712
+ ```ts
713
+ import { createWeightedAdjacencyList } from 'typescript-dsa-stl/collections';
714
+
715
+ const mstAdj = createWeightedAdjacencyList(n);
716
+
717
+ for (const { u, v, weight } of edges) {
718
+ // MST is undirected (we used { undirected: true })
719
+ mstAdj[u].push({ to: v, weight });
720
+ mstAdj[v].push({ to: u, weight });
721
+ }
722
+
723
+ // Example: iterate neighbors of vertex 0 in the MST
724
+ for (const { to, weight } of mstAdj[0]) {
725
+ // visit edge 0 -> to (weight)
726
+ }
727
+ ```
728
+
729
+ ### Dijkstra shortest paths
363
730
 
364
731
  `dijkstra` computes single-source shortest paths on a **weighted** graph with **non-negative** edge weights.
365
732
  It returns:
@@ -391,32 +758,15 @@ const path = reconstructPath(prev, 0, target); // [0, ..., target] or [] if unre
391
758
  console.log(path); // [0, 1, 2, 4]
392
759
  ```
393
760
 
394
- ##### Traverse the MST
395
-
396
- `kruskalMST(...)` returns `{ edges, totalWeight }`. To traverse the MST like a graph, convert `edges` into an adjacency list:
397
-
398
- ```ts
399
- import { createWeightedAdjacencyList } from 'typescript-dsa-stl/collections';
761
+ ---
400
762
 
401
- const mstAdj = createWeightedAdjacencyList(n);
763
+ ## String algorithms
402
764
 
403
- for (const { u, v, weight } of edges) {
404
- // MST is undirected (we used { undirected: true })
405
- mstAdj[u].push({ to: v, weight });
406
- mstAdj[v].push({ to: u, weight });
407
- }
408
-
409
- // Example: iterate neighbors of vertex 0 in the MST
410
- for (const { to, weight } of mstAdj[0]) {
411
- // visit edge 0 -> to (weight)
412
- }
413
- ```
414
-
415
- #### Knuth–Morris–Pratt (KMP), Rabin–Karp, and string rolling hash
765
+ ### Knuth–Morris–Pratt (KMP), Rabin–Karp, and string rolling hash
416
766
 
417
767
  All three work on **UTF-16 code units** (same as `String` indexing). They solve **different jobs**: KMP and Rabin–Karp are **pattern matchers** (list all start indices of a pattern in a text). `StringRollingHash` is a **substring-hash tool** on a **fixed** string—you combine it with your own logic (equality checks, binary search, etc.).
418
768
 
419
- ##### When to use which
769
+ #### When to use which
420
770
 
421
771
  | Goal | Prefer | Why |
422
772
  |------|--------|-----|
@@ -521,262 +871,6 @@ console.log(a.substringHash(2, 2) === b.fullHash()); // true — both are "na"
521
871
 
522
872
  ---
523
873
 
524
- ## Segment trees
525
-
526
- Segment trees support **range queries** and **point updates** in **O(log n)**. Range endpoints are **inclusive**: `query(l, r)` covers indices `l` through `r`.
527
-
528
- ### Ready-made variants (`SegmentTreeSum`, `SegmentTreeMin`, `SegmentTreeMax`)
529
-
530
- ```ts
531
- import {
532
- SegmentTreeSum,
533
- SegmentTreeMin,
534
- SegmentTreeMax,
535
- } from 'typescript-dsa-stl';
536
-
537
- const sum = new SegmentTreeSum([1, 2, 3, 4]);
538
- console.log(sum.query(0, 3)); // 10
539
- sum.update(1, 10);
540
- console.log(sum.query(0, 3)); // 1 + 10 + 3 + 4 = 18
541
-
542
- const mn = new SegmentTreeMin([5, 2, 8, 1]);
543
- console.log(mn.query(0, 3)); // 1
544
-
545
- const mx = new SegmentTreeMax([5, 2, 8, 1]);
546
- console.log(mx.query(0, 3)); // 8
547
- ```
548
-
549
- ### Generic `SegmentTree<T>` (custom combine + neutral)
550
-
551
- Use the same type for array elements and aggregates. Pass an **associative** `combine` and a **neutral** value for query ranges that miss a segment (e.g. `0` for sum, `Infinity` for min).
552
-
553
- ```ts
554
- import { SegmentTree } from 'typescript-dsa-stl';
555
-
556
- const gcdTree = new SegmentTree<number>(
557
- [12, 18, 24],
558
- (a, b) => {
559
- let x = a;
560
- let y = b;
561
- while (y !== 0) {
562
- const t = y;
563
- y = x % y;
564
- x = t;
565
- }
566
- return x;
567
- },
568
- 0
569
- );
570
- console.log(gcdTree.query(0, 2)); // gcd(12, 18, 24) === 6
571
-
572
- // Non-numeric example: concatenate strings
573
- const strTree = new SegmentTree<string>(
574
- ['a', 'b', 'c'],
575
- (a, b) => a + b,
576
- ''
577
- );
578
- console.log(strTree.query(0, 2)); // 'abc'
579
- ```
580
-
581
- ### `GeneralSegmentTree<T, V>` (custom merge + buildLeaf)
582
-
583
- Use when **raw** values `V` differ from the **aggregate** type `T`:
584
-
585
- - **`merge(left, right)`** — combine two child aggregates (internal nodes).
586
- - **`neutral`** — identity for `merge` when a query does not overlap a segment.
587
- - **`buildLeaf(value, index)`** — build the leaf from the raw array on initial construction and on every `update`.
588
-
589
- ```ts
590
- import { GeneralSegmentTree } from 'typescript-dsa-stl';
591
-
592
- // Store sum of squares; raw array is plain numbers
593
- const st = new GeneralSegmentTree<number, number>([1, 2, 3], {
594
- merge: (a, b) => a + b,
595
- neutral: 0,
596
- buildLeaf: (v, i) => v * v + i,
597
- });
598
- console.log(st.query(0, 2)); // (1+0) + (4+1) + (9+2) = 17
599
- st.update(1, 4);
600
- console.log(st.rawAt(1)); // 4 — current raw value at index 1
601
- ```
602
-
603
- ### `LazySegmentTreeSum` (range add + range sum)
604
-
605
- **`rangeAdd(l, r, delta)`** adds `delta` to every element in the inclusive range. **`rangeSum(l, r)`** returns the sum. **`set(i, value)`** assigns one position (lazy tags are applied along the path). All are **O(log n)**.
606
-
607
- ```ts
608
- import { LazySegmentTreeSum } from 'typescript-dsa-stl';
609
-
610
- const lazy = new LazySegmentTreeSum([0, 0, 0, 0]);
611
- lazy.rangeAdd(1, 2, 5); // indices 1 and 2 get +5
612
- console.log(lazy.rangeSum(0, 3)); // 10
613
- lazy.set(0, 100);
614
- console.log(lazy.rangeSum(0, 3)); // 100 + 5 + 5 + 0
615
- ```
616
-
617
- ### Real-world use cases
618
-
619
- These patterns show up in backends and internal tools when you need **many** range queries and updates on a fixed sequence (length known up front), without scanning the whole array each time.
620
-
621
- #### 1. Analytics or reporting: totals over a time window (with corrections)
622
-
623
- Each index is a **fixed bucket** (hour of day, day of month, version slot, etc.). You repeatedly ask “what is the **sum** from bucket `a` through `b`?” and sometimes **fix one bucket** after late data or a reconciliation.
624
-
625
- ```ts
626
- import { SegmentTreeSum } from 'typescript-dsa-stl';
627
-
628
- /** Revenue (or events, page views, API calls) per calendar day; index 0 = first day of period. */
629
- class PeriodMetrics {
630
- private readonly tree: SegmentTreeSum;
631
-
632
- constructor(dailyValues: readonly number[]) {
633
- this.tree = new SegmentTreeSum(dailyValues);
634
- }
635
-
636
- /** Total for an inclusive day range — e.g. chart drill-down or export row. */
637
- totalBetweenDay(firstDayIndex: number, lastDayIndex: number): number {
638
- return this.tree.query(firstDayIndex, lastDayIndex);
639
- }
640
-
641
- /** Backfill or correct one day without rebuilding the whole series. */
642
- setDay(dayIndex: number, amount: number): void {
643
- this.tree.update(dayIndex, amount);
644
- }
645
- }
646
-
647
- const january = new PeriodMetrics([1200, 980, 1100, 1050, 1300]);
648
- console.log(january.totalBetweenDay(0, 4)); // full period
649
- january.setDay(2, 1150); // corrected day 2
650
- console.log(january.totalBetweenDay(1, 3)); // sum over days 1..3
651
- ```
652
-
653
- In production you would usually **persist** the underlying series in a database and **rebuild** the tree when the period reloads; the tree stays useful in memory for dashboards, simulations, or request handlers that see heavy read/update traffic on the same window.
654
-
655
- #### 2. Operations or finance: bulk adjustment on a slice, then aggregate
656
-
657
- You apply the **same delta** to **every** element in an index range (tiered bonuses, prorated credits, simulation shocks), then need **range sums** for reporting. A lazy sum tree avoids touching each cell one by one.
658
-
659
- ```ts
660
- import { LazySegmentTreeSum } from 'typescript-dsa-stl';
661
-
662
- /** Example: per-seat or per-row amounts; apply a flat bonus to ranks 10–50 (0-based 9..49), then sum a sub-range for a sub-team. */
663
- function simulateBulkBonusAndSubtotal(seatCount: number): void {
664
- // Initial per-seat values (e.g. base commission), built once
665
- const base = Array.from({ length: seatCount }, (_, i) => 100 + i);
666
- const amounts = new LazySegmentTreeSum(base);
667
-
668
- // HR: +250 to everyone in seats 10–50 inclusive (indices 9..49)
669
- amounts.rangeAdd(9, 49, 250);
670
-
671
- // Finance: subtotal for seats 20–30 only
672
- console.log(amounts.rangeSum(19, 29));
673
- }
674
-
675
- simulateBulkBonusAndSubtotal(100);
676
- ```
677
-
678
- The same idea applies to **inventory deltas** across bin ranges, **loyalty points** batch credits by user-ID band (when IDs map to contiguous indices), or **game/simulation** state where many cells gain the same buff and you query partial totals.
679
-
680
- ---
681
-
682
- ## API overview
683
-
684
- | Module | Exports |
685
- |--------|--------|
686
- | **Collections** | `Vector`, `Stack`, `Queue`, `List`, `ListNode`, `PriorityQueue`, `OrderedMap`, `UnorderedMap`, `OrderedSet`, `UnorderedSet`, `OrderedMultiMap`, `OrderedMultiSet`, `GeneralSegmentTree`, `SegmentTree`, `SegmentTreeSum`, `SegmentTreeMin`, `SegmentTreeMax`, `LazySegmentTreeSum`, `WeightedEdge`, `AdjacencyList`, `WeightedAdjacencyList`, `createAdjacencyList`, `createWeightedAdjacencyList`, `addEdge`, `deleteEdge` |
687
- | **Algorithms** | `sort`, `find`, `findIndex`, `transform`, `filter`, `reduce`, `reverse`, `unique`, `binarySearch`, `lowerBound`, `upperBound`, `min`, `max`, `partition`, `DisjointSetUnion`, `KnuthMorrisPratt`, `RabinKarp`, `RABIN_KARP_DEFAULT_MODS`, `StringRollingHash`, `breadthFirstSearch`, `depthFirstSearch`, `connectedComponents`, `kruskalMST` |
688
- | **Utils** | `clamp`, `range`, `noop`, `identity`, `swap` |
689
- | **Types** | `Comparator`, `Predicate`, `UnaryFn`, `Reducer`, `IterableLike`, `toArray`, `RabinKarpTripleMods`, `GeneralSegmentTreeConfig`, `SegmentCombine`, `SegmentMerge`, `SegmentLeafBuild` |
690
-
691
- ### Subpath imports (tree-shaking)
692
-
693
- ```ts
694
- import { Vector, Stack } from 'typescript-dsa-stl/collections';
695
- import { sort, binarySearch, breadthFirstSearch, depthFirstSearch, KnuthMorrisPratt, RabinKarp, StringRollingHash } from 'typescript-dsa-stl/algorithms';
696
- import { clamp, range } from 'typescript-dsa-stl/utils';
697
- import type { Comparator } from 'typescript-dsa-stl/types';
698
- ```
699
-
700
- ---
701
-
702
- ## Data structures
703
-
704
- | Structure | Access | Insert end | Insert middle | Remove end | Remove middle |
705
- |-----------|--------|------------|---------------|------------|---------------|
706
- | **Vector** | O(1) | O(1)* | O(n) | O(1) | O(n) |
707
- | **Stack** | — | O(1) | — | O(1) | — |
708
- | **Queue** | — | O(1)* | — | O(1)* | — |
709
- | **List** | O(n) | O(1) | O(1)** | O(1) | O(1)** |
710
- | **PriorityQueue** | — | O(log n) | — | O(log n) | — |
711
- | **OrderedMap** (Map) | O(log n) get | O(log n) set | — | O(log n) delete | — |
712
- | **UnorderedMap** | O(1)* get/set | O(1)* | — | O(1)* delete | — |
713
- | **OrderedSet** (Set) | O(log n) has | O(log n) add | — | O(log n) delete | — |
714
- | **UnorderedSet** | O(1)* has/add | O(1)* | — | O(1)* delete | — |
715
- | **OrderedMultiMap** | O(log n) get | O(log n) set | — | O(log n) delete | — |
716
- | **OrderedMultiSet** | O(log n) has/count | O(log n) add | — | O(log n) delete | — |
717
-
718
- \* Amortized (hash).
719
- \** At a known node.
720
-
721
- ### Segment trees (range queries)
722
-
723
- | Structure | Build | Point update | Range query | Extra |
724
- |-----------|-------|--------------|-------------|--------|
725
- | **GeneralSegmentTree**, **SegmentTree**, **SegmentTreeSum** / **Min** / **Max** | O(n) | O(log n) | O(log n) | Inclusive `[l, r]`; **GeneralSegmentTree** keeps raw `V` and uses `merge` + `buildLeaf` |
726
- | **LazySegmentTreeSum** | O(n) | `set`: O(log n) | `rangeSum`: O(log n) | `rangeAdd` on a range: O(log n) |
727
-
728
- ---
729
-
730
- ## OrderedMultiMap and OrderedMultiSet — use cases
731
-
732
- **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.
733
-
734
- | Use case | Example |
735
- |----------|---------|
736
- | **Sorted runs / leaderboard with ties** | Store scores; multiple users can have the same score. Iterate in sorted order, use `count(score)` for ties. |
737
- | **Event timeline with repeated timestamps** | Add events by time; several events can share the same time. `add(timestamp)`, iterate in order. |
738
- | **K-th smallest in a multiset** | Keep elements sorted; k-th element is at index `k - 1` in iteration. |
739
- | **Range counts** | Combined with binary search ideas: count elements in `[low, high]` using `count` and iteration. |
740
-
741
- **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.
742
-
743
- | Use case | Example |
744
- |----------|---------|
745
- | **Inverted index** | Key = term, values = document IDs containing that term. `set(term, docId)` for each occurrence; `getAll(term)` returns all doc IDs. |
746
- | **Grouping by key** | Key = category, values = items. `set(category, item)`; iterate keys in order, use `getAll(key)` per group. |
747
- | **One-to-many relations** | Key = user ID, values = session IDs. `set(userId, sessionId)`; `getAll(userId)` lists all sessions. |
748
- | **Time-series by bucket** | Key = time bucket, values = events. Sorted keys give chronological buckets; `getAll(bucket)` gets events in that bucket. |
749
-
750
- ### OrderedMultiSet example
751
-
752
- ```ts
753
- import { OrderedMultiSet } from 'typescript-dsa-stl';
754
-
755
- const scores = new OrderedMultiSet<number>();
756
- scores.add(85); scores.add(92); scores.add(85); scores.add(78);
757
- console.log(scores.toArray()); // [78, 85, 85, 92]
758
- console.log(scores.count(85)); // 2
759
- scores.delete(85); // remove one 85
760
- console.log(scores.count(85)); // 1
761
- scores.deleteAll(85); // remove all 85s
762
- ```
763
-
764
- ### OrderedMultiMap example
765
-
766
- ```ts
767
- import { OrderedMultiMap } from 'typescript-dsa-stl';
768
-
769
- const index = new OrderedMultiMap<string, number>(); // term -> doc IDs
770
- index.set('typescript', 1); index.set('typescript', 3); index.set('stl', 2);
771
- console.log(index.getAll('typescript')); // [1, 3]
772
- console.log(index.get('stl')); // 2
773
- for (const [key, value] of index) {
774
- console.log(key, value); // keys in sorted order
775
- }
776
- ```
777
-
778
- ---
779
-
780
874
  ## For maintainers
781
875
 
782
876
  - **Build:** `npm run build` (also runs before `npm publish` via `prepublishOnly`)
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Double-ended queue (STL `std::deque`-style API in camelCase).
3
+ *
4
+ * Backed by a growable circular buffer: amortized **O(1)** `pushFront` / `pushBack` /
5
+ * `popFront` / `popBack`, and **O(1)** indexed `at` / `set` (unlike a linked list).
6
+ */
7
+ export declare class Deque<T> {
8
+ private _buf;
9
+ private _head;
10
+ private _size;
11
+ /**
12
+ * @param initial — initial capacity (number), or copy elements in order from an array.
13
+ */
14
+ constructor(initial?: readonly T[] | number);
15
+ get size(): number;
16
+ get empty(): boolean;
17
+ /** Current backing slot count (≥ size). */
18
+ get capacity(): number;
19
+ private grow;
20
+ private ensureSpace;
21
+ /** Ensure buffer has room for at least `n` elements total (may shrink logical size only via pop/clear). */
22
+ reserve(minCapacity: number): void;
23
+ /** Drop unused buffer capacity (keeps current elements). */
24
+ shrinkToFit(): void;
25
+ private indexPhysical;
26
+ at(index: number): T;
27
+ set(index: number, value: T): void;
28
+ /** Same as C++ `push_front`. */
29
+ pushFront(value: T): void;
30
+ /** Same as C++ `push_back`. */
31
+ pushBack(value: T): void;
32
+ /** Same as C++ `pop_front`. */
33
+ popFront(): T | undefined;
34
+ /** Same as C++ `pop_back`. */
35
+ popBack(): T | undefined;
36
+ /** Same as C++ `front`. */
37
+ front(): T;
38
+ /** Same as C++ `back`. */
39
+ back(): T;
40
+ clear(): void;
41
+ [Symbol.iterator](): Iterator<T>;
42
+ toArray(): T[];
43
+ }
44
+ //# sourceMappingURL=Deque.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Deque.d.ts","sourceRoot":"","sources":["../../src/collections/Deque.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,qBAAa,KAAK,CAAC,CAAC;IAClB,OAAO,CAAC,IAAI,CAAoB;IAChC,OAAO,CAAC,KAAK,CAAK;IAClB,OAAO,CAAC,KAAK,CAAK;IAElB;;OAEG;gBACS,OAAO,CAAC,EAAE,SAAS,CAAC,EAAE,GAAG,MAAM;IAkB3C,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,IAAI,KAAK,IAAI,OAAO,CAEnB;IAED,2CAA2C;IAC3C,IAAI,QAAQ,IAAI,MAAM,CAErB;IAED,OAAO,CAAC,IAAI;IAUZ,OAAO,CAAC,WAAW;IAKnB,2GAA2G;IAC3G,OAAO,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI;IAWlC,4DAA4D;IAC5D,WAAW,IAAI,IAAI;IAgBnB,OAAO,CAAC,aAAa;IAIrB,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,CAAC;IAOpB,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI;IAOlC,gCAAgC;IAChC,SAAS,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI;IAQzB,+BAA+B;IAC/B,QAAQ,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI;IAOxB,+BAA+B;IAC/B,QAAQ,IAAI,CAAC,GAAG,SAAS;IAUzB,8BAA8B;IAC9B,OAAO,IAAI,CAAC,GAAG,SAAS;IAUxB,2BAA2B;IAC3B,KAAK,IAAI,CAAC;IAKV,0BAA0B;IAC1B,IAAI,IAAI,CAAC;IAMT,KAAK,IAAI,IAAI;IAMb,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC;IAgBhC,OAAO,IAAI,CAAC,EAAE;CAQf"}
@@ -0,0 +1,178 @@
1
+ /**
2
+ * Double-ended queue (STL `std::deque`-style API in camelCase).
3
+ *
4
+ * Backed by a growable circular buffer: amortized **O(1)** `pushFront` / `pushBack` /
5
+ * `popFront` / `popBack`, and **O(1)** indexed `at` / `set` (unlike a linked list).
6
+ */
7
+ export class Deque {
8
+ /**
9
+ * @param initial — initial capacity (number), or copy elements in order from an array.
10
+ */
11
+ constructor(initial) {
12
+ this._head = 0;
13
+ this._size = 0;
14
+ if (initial === undefined) {
15
+ this._buf = new Array(16);
16
+ return;
17
+ }
18
+ if (typeof initial === 'number') {
19
+ this._buf = new Array(Math.max(4, initial));
20
+ return;
21
+ }
22
+ const n = initial.length;
23
+ const cap = Math.max(16, n <= 4 ? 4 : 2 ** Math.ceil(Math.log2(n)));
24
+ this._buf = new Array(cap);
25
+ for (let i = 0; i < n; i++) {
26
+ this._buf[i] = initial[i];
27
+ }
28
+ this._size = n;
29
+ }
30
+ get size() {
31
+ return this._size;
32
+ }
33
+ get empty() {
34
+ return this._size === 0;
35
+ }
36
+ /** Current backing slot count (≥ size). */
37
+ get capacity() {
38
+ return this._buf.length;
39
+ }
40
+ grow() {
41
+ const cap = this._buf.length;
42
+ const next = new Array(Math.max(4, cap * 2));
43
+ for (let i = 0; i < this._size; i++) {
44
+ next[i] = this._buf[(this._head + i) % cap];
45
+ }
46
+ this._buf = next;
47
+ this._head = 0;
48
+ }
49
+ ensureSpace() {
50
+ if (this._size < this._buf.length)
51
+ return;
52
+ this.grow();
53
+ }
54
+ /** Ensure buffer has room for at least `n` elements total (may shrink logical size only via pop/clear). */
55
+ reserve(minCapacity) {
56
+ if (minCapacity <= this._buf.length)
57
+ return;
58
+ const next = new Array(minCapacity);
59
+ const cap = this._buf.length;
60
+ for (let i = 0; i < this._size; i++) {
61
+ next[i] = this._buf[(this._head + i) % cap];
62
+ }
63
+ this._buf = next;
64
+ this._head = 0;
65
+ }
66
+ /** Drop unused buffer capacity (keeps current elements). */
67
+ shrinkToFit() {
68
+ if (this._size === this._buf.length)
69
+ return;
70
+ if (this._size === 0) {
71
+ this._buf = new Array(4);
72
+ this._head = 0;
73
+ return;
74
+ }
75
+ const next = new Array(this._size);
76
+ const cap = this._buf.length;
77
+ for (let i = 0; i < this._size; i++) {
78
+ next[i] = this._buf[(this._head + i) % cap];
79
+ }
80
+ this._buf = next;
81
+ this._head = 0;
82
+ }
83
+ indexPhysical(i) {
84
+ return (this._head + i) % this._buf.length;
85
+ }
86
+ at(index) {
87
+ if (index < 0 || index >= this._size) {
88
+ throw new RangeError(`Deque index ${index} out of range (size ${this._size})`);
89
+ }
90
+ return this._buf[this.indexPhysical(index)];
91
+ }
92
+ set(index, value) {
93
+ if (index < 0 || index >= this._size) {
94
+ throw new RangeError(`Deque index ${index} out of range (size ${this._size})`);
95
+ }
96
+ this._buf[this.indexPhysical(index)] = value;
97
+ }
98
+ /** Same as C++ `push_front`. */
99
+ pushFront(value) {
100
+ this.ensureSpace();
101
+ const cap = this._buf.length;
102
+ this._head = (this._head - 1 + cap) % cap;
103
+ this._buf[this._head] = value;
104
+ this._size++;
105
+ }
106
+ /** Same as C++ `push_back`. */
107
+ pushBack(value) {
108
+ this.ensureSpace();
109
+ const cap = this._buf.length;
110
+ this._buf[(this._head + this._size) % cap] = value;
111
+ this._size++;
112
+ }
113
+ /** Same as C++ `pop_front`. */
114
+ popFront() {
115
+ if (this._size === 0)
116
+ return undefined;
117
+ const cap = this._buf.length;
118
+ const value = this._buf[this._head];
119
+ this._buf[this._head] = undefined;
120
+ this._head = (this._head + 1) % cap;
121
+ this._size--;
122
+ return value;
123
+ }
124
+ /** Same as C++ `pop_back`. */
125
+ popBack() {
126
+ if (this._size === 0)
127
+ return undefined;
128
+ const cap = this._buf.length;
129
+ const idx = (this._head + this._size - 1 + cap) % cap;
130
+ const value = this._buf[idx];
131
+ this._buf[idx] = undefined;
132
+ this._size--;
133
+ return value;
134
+ }
135
+ /** Same as C++ `front`. */
136
+ front() {
137
+ if (this._size === 0)
138
+ throw new RangeError('Deque is empty');
139
+ return this._buf[this._head];
140
+ }
141
+ /** Same as C++ `back`. */
142
+ back() {
143
+ if (this._size === 0)
144
+ throw new RangeError('Deque is empty');
145
+ const cap = this._buf.length;
146
+ return this._buf[(this._head + this._size - 1 + cap) % cap];
147
+ }
148
+ clear() {
149
+ this._buf = new Array(this._buf.length);
150
+ this._head = 0;
151
+ this._size = 0;
152
+ }
153
+ [Symbol.iterator]() {
154
+ const buf = this._buf;
155
+ const cap = buf.length;
156
+ const head = this._head;
157
+ const size = this._size;
158
+ let i = 0;
159
+ return {
160
+ next() {
161
+ if (i >= size)
162
+ return { done: true, value: undefined };
163
+ const value = buf[(head + i) % cap];
164
+ i++;
165
+ return { done: false, value };
166
+ },
167
+ };
168
+ }
169
+ toArray() {
170
+ const out = [];
171
+ const cap = this._buf.length;
172
+ for (let i = 0; i < this._size; i++) {
173
+ out.push(this._buf[(this._head + i) % cap]);
174
+ }
175
+ return out;
176
+ }
177
+ }
178
+ //# sourceMappingURL=Deque.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Deque.js","sourceRoot":"","sources":["../../src/collections/Deque.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,OAAO,KAAK;IAKhB;;OAEG;IACH,YAAY,OAA+B;QANnC,UAAK,GAAG,CAA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AAI,CAAC,KAAK,GAAG,CAAC,CAAC;IACjB,CAAC;IAED,CAAC,MAAM,CAAC,QAAQ,CAAC;QACf,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC;QACtB,MAAM,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC;QACvB,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC;QACxB,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC;QACxB,IAAI,CAAC,GAAG,CAAC,CAAC;QACV,OAAO;YACL,IAAI;gBACF,IAAI,CAAC,IAAI,IAAI;oBAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;gBACvD,MAAM,KAAK,GAAG,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,GAAG,CAAE,CAAC;gBACrC,CAAC,EAAE,CAAC;gBACJ,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;YAChC,CAAC;SACF,CAAC;IACJ,CAAC;IAED,OAAO;QACL,MAAM,GAAG,GAAQ,EAAE,CAAC;QACpB,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC;QAC7B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC;YACpC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,GAAG,GAAG,CAAE,CAAC,CAAC;QAC/C,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC;CACF"}
@@ -1,6 +1,7 @@
1
1
  export { Vector } from './Vector.js';
2
2
  export { Stack } from './Stack.js';
3
3
  export { Queue } from './Queue.js';
4
+ export { Deque } from './Deque.js';
4
5
  export { List, ListNode } from './List.js';
5
6
  export { PriorityQueue } from './PriorityQueue.js';
6
7
  export { OrderedMap } from './OrderedMap.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/collections/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EACL,kBAAkB,EAClB,WAAW,EACX,cAAc,EACd,cAAc,EACd,cAAc,EACd,kBAAkB,GACnB,MAAM,kBAAkB,CAAC;AAC1B,YAAY,EACV,wBAAwB,EACxB,cAAc,EACd,gBAAgB,EAChB,YAAY,GACb,MAAM,kBAAkB,CAAC;AAC1B,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AACrF,OAAO,EAAE,mBAAmB,EAAE,2BAA2B,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/collections/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EACL,kBAAkB,EAClB,WAAW,EACX,cAAc,EACd,cAAc,EACd,cAAc,EACd,kBAAkB,GACnB,MAAM,kBAAkB,CAAC;AAC1B,YAAY,EACV,wBAAwB,EACxB,cAAc,EACd,gBAAgB,EAChB,YAAY,GACb,MAAM,kBAAkB,CAAC;AAC1B,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AACrF,OAAO,EAAE,mBAAmB,EAAE,2BAA2B,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC"}
@@ -1,6 +1,7 @@
1
1
  export { Vector } from './Vector.js';
2
2
  export { Stack } from './Stack.js';
3
3
  export { Queue } from './Queue.js';
4
+ export { Deque } from './Deque.js';
4
5
  export { List, ListNode } from './List.js';
5
6
  export { PriorityQueue } from './PriorityQueue.js';
6
7
  export { OrderedMap } from './OrderedMap.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/collections/index.ts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E,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EACL,kBAAkB,EAClB,WAAW,EACX,cAAc,EACd,cAAc,EACd,cAAc,EACd,kBAAkB,GACnB,MAAM,kBAAkB,CAAC;AAQ1B,OAAO,EAAE,mBAAmB,EAAE,2BAA2B,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC"}
package/dist/index.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * @example
6
6
  * import { Vector, Stack, Queue, List, sort, binarySearch, clamp } from 'typescript-dsa-stl';
7
7
  */
8
- export { Vector, Stack, Queue, List, ListNode, PriorityQueue, OrderedMap, UnorderedMap, OrderedSet, UnorderedSet, OrderedMultiSet, OrderedMultiMap, addEdge, deleteEdge, createAdjacencyList, createWeightedAdjacencyList, GeneralSegmentTree, SegmentTree, SegmentTreeSum, SegmentTreeMin, SegmentTreeMax, LazySegmentTreeSum, } from './collections/index.js';
8
+ export { Vector, Stack, Queue, Deque, List, ListNode, PriorityQueue, OrderedMap, UnorderedMap, OrderedSet, UnorderedSet, OrderedMultiSet, OrderedMultiMap, addEdge, deleteEdge, createAdjacencyList, createWeightedAdjacencyList, GeneralSegmentTree, SegmentTree, SegmentTreeSum, SegmentTreeMin, SegmentTreeMax, LazySegmentTreeSum, } from './collections/index.js';
9
9
  export type { WeightedEdge, AdjacencyList, WeightedAdjacencyList, GeneralSegmentTreeConfig, SegmentCombine, SegmentLeafBuild, SegmentMerge, } from './collections/index.js';
10
10
  export { sort, find, findIndex, transform, filter, reduce, reverse, unique, binarySearch, lowerBound, upperBound, min, max, partition, DisjointSetUnion, KnuthMorrisPratt, StringRollingHash, RabinKarp, RABIN_KARP_DEFAULT_MODS, breadthFirstSearch, depthFirstSearch, connectedComponents, dijkstra, reconstructPath, kruskalMST, } from './algorithms/index.js';
11
11
  export type { WeightedUndirectedEdge, RabinKarpTripleMods } from './algorithms/index.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,MAAM,EACN,KAAK,EACL,KAAK,EACL,IAAI,EACJ,QAAQ,EACR,aAAa,EACb,UAAU,EACV,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,eAAe,EACf,eAAe,EACf,OAAO,EACP,UAAU,EACV,mBAAmB,EACnB,2BAA2B,EAC3B,kBAAkB,EAClB,WAAW,EACX,cAAc,EACd,cAAc,EACd,cAAc,EACd,kBAAkB,GACnB,MAAM,wBAAwB,CAAC;AAChC,YAAY,EACV,YAAY,EACZ,aAAa,EACb,qBAAqB,EACrB,wBAAwB,EACxB,cAAc,EACd,gBAAgB,EAChB,YAAY,GACb,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,IAAI,EACJ,IAAI,EACJ,SAAS,EACT,SAAS,EACT,MAAM,EACN,MAAM,EACN,OAAO,EACP,MAAM,EACN,YAAY,EACZ,UAAU,EACV,UAAU,EACV,GAAG,EACH,GAAG,EACH,SAAS,EACT,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,SAAS,EACT,uBAAuB,EACvB,kBAAkB,EAClB,gBAAgB,EAChB,mBAAmB,EACnB,QAAQ,EACR,eAAe,EACf,UAAU,GACX,MAAM,uBAAuB,CAAC;AAC/B,YAAY,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AACzF,OAAO,EAAE,KAAK,EAAE,KAAK,EA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AC;AACtE,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAC9F,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC"}
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * @example
6
6
  * import { Vector, Stack, Queue, List, sort, binarySearch, clamp } from 'typescript-dsa-stl';
7
7
  */
8
- export { Vector, Stack, Queue, List, ListNode, PriorityQueue, OrderedMap, UnorderedMap, OrderedSet, UnorderedSet, OrderedMultiSet, OrderedMultiMap, addEdge, deleteEdge, createAdjacencyList, createWeightedAdjacencyList, GeneralSegmentTree, SegmentTree, SegmentTreeSum, SegmentTreeMin, SegmentTreeMax, LazySegmentTreeSum, } from './collections/index.js';
8
+ export { Vector, Stack, Queue, Deque, List, ListNode, PriorityQueue, OrderedMap, UnorderedMap, OrderedSet, UnorderedSet, OrderedMultiSet, OrderedMultiMap, addEdge, deleteEdge, createAdjacencyList, createWeightedAdjacencyList, GeneralSegmentTree, SegmentTree, SegmentTreeSum, SegmentTreeMin, SegmentTreeMax, LazySegmentTreeSum, } from './collections/index.js';
9
9
  export { sort, find, findIndex, transform, filter, reduce, reverse, unique, binarySearch, lowerBound, upperBound, min, max, partition, DisjointSetUnion, KnuthMorrisPratt, StringRollingHash, RabinKarp, RABIN_KARP_DEFAULT_MODS, breadthFirstSearch, depthFirstSearch, connectedComponents, dijkstra, reconstructPath, kruskalMST, } from './algorithms/index.js';
10
10
  export { clamp, range, noop, identity, swap } from './utils/index.js';
11
11
  export { toArray } from './types/index.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,MAAM,EACN,KAAK,EACL,KAAK,EACL,IAAI,EACJ,QAAQ,EACR,aAAa,EACb,UAAU,EACV,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,eAAe,EACf,eAAe,EACf,OAAO,EACP,UAAU,EACV,mBAAmB,EACnB,2BAA2B,EAC3B,kBAAkB,EAClB,WAAW,EACX,cAAc,EACd,cAAc,EACd,cAAc,EACd,kBAAkB,GACnB,MAAM,wBAAwB,CAAC;AAUhC,OAAO,EACL,IAAI,EACJ,IAAI,EACJ,SAAS,EACT,SAAS,EACT,MAAM,EACN,MAAM,EACN,OAAO,EACP,MAAM,EACN,YAAY,EACZ,UAAU,EACV,UAAU,EACV,GAAG,EACH,GAAG,EACH,SAAS,EACT,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,SAAS,EACT,uBAAuB,EACvB,kBAAkB,EAClB,gBAAgB,EAChB,mBAAmB,EACnB,QAAQ,EACR,eAAe,EACf,UAAU,GACX,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAEtE,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,MAAM,EACN,KAAK,EACL,KAAK,EACL,KAAK,EACL,IAAI,EACJ,QAAQ,EACR,aAAa,EACb,UAAU,EACV,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,eAAe,EACf,eAAe,EACf,OAAO,EACP,UAAU,EACV,mBAAmB,EACnB,2BAA2B,EAC3B,kBAAkB,EAClB,WAAW,EACX,cAAc,EACd,cAAc,EACd,cAAc,EACd,kBAAkB,GACnB,MAAM,wBAAwB,CAAC;AAUhC,OAAO,EACL,IAAI,EACJ,IAAI,EACJ,SAAS,EACT,SAAS,EACT,MAAM,EACN,MAAM,EACN,OAAO,EACP,MAAM,EACN,YAAY,EACZ,UAAU,EACV,UAAU,EACV,GAAG,EACH,GAAG,EACH,SAAS,EACT,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,SAAS,EACT,uBAAuB,EACvB,kBAAkB,EAClB,gBAAgB,EAChB,mBAAmB,EACnB,QAAQ,EACR,eAAe,EACf,UAAU,GACX,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAEtE,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "typescript-dsa-stl",
3
- "version": "2.5.0",
4
- "description": "STL-style data structures and algorithms for TypeScript: Vector, Stack, Queue, List, PriorityQueue, Map, Set, sort, binarySearch, graph utilities. Use like C++ STL.",
3
+ "version": "2.7.0",
4
+ "description": "STL-style data structures and algorithms for TypeScript: Vector, Stack, Queue, Deque (double-ended queue), List, PriorityQueue, Map, Set, sort, binarySearch, graph utilities. Use like C++ STL.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.js",
@@ -52,6 +52,8 @@
52
52
  "vector",
53
53
  "stack",
54
54
  "queue",
55
+ "deque",
56
+ "double-ended-queue",
55
57
  "list",
56
58
  "priority-queue",
57
59
  "map",