typescript-dsa-stl 2.0.2 → 2.2.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
@@ -2,7 +2,7 @@
2
2
 
3
3
  **This is the GitHub repository** for the npm package **[typescript-dsa-stl](https://www.npmjs.com/package/typescript-dsa-stl)**.
4
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.
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`, **KnuthMorrisPratt**, **RabinKarp**, **StringRollingHash**, etc.). Install from npm to use in your project; this repo holds the source code.
6
6
 
7
7
  ---
8
8
 
@@ -236,6 +236,193 @@ Use an **unweighted** graph (adjacency list) when you only care about connectivi
236
236
  | **Grid / game graphs** | Unweighted: 4- or 8-neighbor grids; weighted if movement costs differ per cell. |
237
237
  | **Network / flow** | Weighted: capacities or latencies on edges for max-flow or routing. |
238
238
 
239
+ #### Disjoint Set Union (Union-Find)
240
+
241
+ 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.
242
+
243
+ ```ts
244
+ import { createAdjacencyList, connectedComponents } from 'typescript-dsa-stl';
245
+
246
+ const n = 5;
247
+ const graph = createAdjacencyList(n);
248
+ graph[0].push(1);
249
+ graph[1].push(0);
250
+ graph[3].push(4);
251
+ graph[4].push(3);
252
+
253
+ const comps = connectedComponents(n, graph);
254
+ // e.g. [[0, 1], [2], [3, 4]]
255
+ ```
256
+
257
+ ##### Traverse the result
258
+
259
+ `connectedComponents(n, adj)` returns `number[][]` where each inner array is a component (list of vertices).
260
+
261
+ ```ts
262
+ // 1) Iterate each component
263
+ for (const comp of comps) {
264
+ // comp is like [v0, v1, ...]
265
+ for (const v of comp) {
266
+ // do something with vertex v
267
+ }
268
+ }
269
+
270
+ // 2) Component sizes
271
+ const sizes = comps.map(comp => comp.length);
272
+ ```
273
+
274
+ #### Kruskal MST (uses DSU)
275
+
276
+ For a weighted graph, `kruskalMST` builds a Minimum Spanning Tree (MST) using DSU.
277
+
278
+ ```ts
279
+ import {
280
+ createWeightedAdjacencyList,
281
+ addEdge,
282
+ kruskalMST,
283
+ } from 'typescript-dsa-stl';
284
+
285
+ const n = 4;
286
+ const wGraph = createWeightedAdjacencyList(n);
287
+
288
+ // Add undirected edges by adding both directions.
289
+ addEdge(wGraph, 0, 1, 1); addEdge(wGraph, 1, 0, 1);
290
+ addEdge(wGraph, 0, 2, 4); addEdge(wGraph, 2, 0, 4);
291
+ addEdge(wGraph, 1, 2, 2); addEdge(wGraph, 2, 1, 2);
292
+ addEdge(wGraph, 1, 3, 3); addEdge(wGraph, 3, 1, 3);
293
+
294
+ const { edges, totalWeight } = kruskalMST(n, wGraph, { undirected: true });
295
+ // edges: MST edges (chosen by weight), totalWeight: sum of weights
296
+ ```
297
+
298
+ ##### Traverse the MST
299
+
300
+ `kruskalMST(...)` returns `{ edges, totalWeight }`. To traverse the MST like a graph, convert `edges` into an adjacency list:
301
+
302
+ ```ts
303
+ import { createWeightedAdjacencyList } from 'typescript-dsa-stl/collections';
304
+
305
+ const mstAdj = createWeightedAdjacencyList(n);
306
+
307
+ for (const { u, v, weight } of edges) {
308
+ // MST is undirected (we used { undirected: true })
309
+ mstAdj[u].push({ to: v, weight });
310
+ mstAdj[v].push({ to: u, weight });
311
+ }
312
+
313
+ // Example: iterate neighbors of vertex 0 in the MST
314
+ for (const { to, weight } of mstAdj[0]) {
315
+ // visit edge 0 -> to (weight)
316
+ }
317
+ ```
318
+
319
+ #### Knuth–Morris–Pratt (KMP), Rabin–Karp, and string rolling hash
320
+
321
+ 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.).
322
+
323
+ ##### When to use which
324
+
325
+ | Goal | Prefer | Why |
326
+ |------|--------|-----|
327
+ | **Find every occurrence** of **one pattern** in **one text**, with **worst-case** O(n + m), **no hashing**, predictable behaviour | **KnuthMorrisPratt** | LPS table; only character comparisons; no modular arithmetic. |
328
+ | **Find every occurrence** of a pattern using a **sliding window** and **hashes** (triple moduli + final verify) | **RabinKarp** | Same asymptotic average case; good when you think in rolling hashes or batch **same-length** patterns. |
329
+ | **Many O(1) hash queries** on **substrings of one string** you already hold (compare two ranges, palindrome / LCP style checks, rolling checks without slicing) | **StringRollingHash** | O(n) preprocess, O(1) per `substringHash`; **not** a drop-in “find all matches” API—use KMP or Rabin–Karp for that. |
330
+
331
+ **Concrete situations**
332
+
333
+ - **Use KMP** when you need a **guaranteed** linear scan (interviews, strict time bounds, large alphabets), or the pattern is **reused** across many searches (`new KnuthMorrisPratt(pattern)` once, `.search(text)` many times).
334
+ - **Use Rabin–Karp** when a **rolling hash** model fits (e.g. one long stream, one pattern), or you later generalize to **several patterns of the same length** (compare each pattern’s triple hash to each window hash). Triple hashing keeps false hash positives negligible; **verification** still guarantees correct indices.
335
+ - **Use `StringRollingHash`** when the problem is **“hash of s[l..r)”** many times on **one** `s`—e.g. check `s[i..i+k) === s[j..j+k)` via hash equality (then confirm if needed), or algorithms that **binary search** on length using substring hashes. For **only** “list all starts of P in T”, pick **KMP** or **Rabin–Karp** instead of building substring hashes by hand.
336
+
337
+ ```ts
338
+ import { KnuthMorrisPratt, RabinKarp, StringRollingHash } from 'typescript-dsa-stl';
339
+
340
+ // A) Pattern fixed, searched in many buffers — build KMP once, call .search() repeatedly (LPS reused).
341
+ const multiDoc = new KnuthMorrisPratt('ERROR');
342
+ multiDoc.search(serverLog1);
343
+ multiDoc.search(serverLog2);
344
+
345
+ // B) One haystack, one needle — both matchers return the same indices; KMP = no mods, Rabin–Karp = triple hash + verify.
346
+ const hay = '...long text...';
347
+ KnuthMorrisPratt.findOccurrences(hay, 'needle');
348
+ new RabinKarp('needle').search(hay);
349
+
350
+ // C) Same static string, many range-equality checks — rolling hash (not for “find all pattern starts” by itself).
351
+ const s = 'banana';
352
+ const rh = new StringRollingHash(s);
353
+ // Does s[1..4) equal s[3..6)? (both length 3)
354
+ const maybe = rh.substringHash(1, 3) === rh.substringHash(3, 3); // then compare slices if you need certainty
355
+ ```
356
+
357
+ **How KMP works:** Build an LPS (longest proper prefix that is also a suffix) table for the pattern, then scan the text once. On a mismatch, the LPS tells you how far to shift the pattern without moving the text pointer backward—**O(n + m)** time, **O(m)** extra space for the pattern’s LPS.
358
+
359
+ **How Rabin–Karp works:** Use **triple hashing**—three independent polynomial hashes (same base, three distinct prime moduli, see `RABIN_KARP_DEFAULT_MODS`). A position is a candidate only when **all three** window hashes match the pattern’s triple; then a **character-by-character** check runs so reported matches are always correct. Spurious triple collisions are negligible; average **O(n + m)**.
360
+
361
+ **How rolling hash works:** Precompute prefix hashes and powers of a base modulo a prime. The hash of any substring `s[start .. start + length)` is derived from two prefix values—**O(n)** build, **O(1)** per substring query. Hashes can collide; for critical equality checks, compare actual strings after a hash match.
362
+
363
+ ```ts
364
+ import {
365
+ KnuthMorrisPratt,
366
+ StringRollingHash,
367
+ RabinKarp,
368
+ RABIN_KARP_DEFAULT_MODS,
369
+ } from 'typescript-dsa-stl';
370
+
371
+ // --- KMP: construct with the pattern; LPS is built inside the constructor ---
372
+ const kmp = new KnuthMorrisPratt('aba');
373
+ // LPS for "aba" is [0, 0, 1] — used to skip redundant comparisons after a partial match
374
+
375
+ const positions = kmp.search('ababa');
376
+ // "aba" appears starting at index 0 ("ababa") and index 2 ("aba" at the end)
377
+ console.log(positions); // [0, 2]
378
+
379
+ // One-shot search without storing an instance (same result as above for this pattern/text)
380
+ console.log(KnuthMorrisPratt.findOccurrences('ababa', 'aba')); // [0, 2]
381
+
382
+ // Overlapping matches are all reported (each valid start index)
383
+ const overlaps = KnuthMorrisPratt.findOccurrences('aaaa', 'aa');
384
+ // Starts at 0, 1, 2 — three overlapping occurrences of "aa"
385
+ console.log(overlaps); // [0, 1, 2]
386
+
387
+ // Empty pattern: no matches returned (empty array)
388
+ console.log(new KnuthMorrisPratt('').search('anything')); // []
389
+
390
+ // --- Rabin–Karp: triple moduli (defaults) + verification; same results as KMP for real matches ---
391
+ const rk = new RabinKarp('aba');
392
+ // RABIN_KARP_DEFAULT_MODS = [1e9+7, 1e9+9, 998244353] — three hashes must agree, then verify
393
+ console.log(RABIN_KARP_DEFAULT_MODS.length); // 3
394
+ console.log(rk.search('ababa')); // [0, 2]
395
+ console.log(RabinKarp.findOccurrences('ababa', 'aba')); // [0, 2]
396
+ // new RabinKarp(pattern, base?, [mod0, mod1, mod2]) — override the three primes if needed
397
+
398
+ // Overlapping matches (same as KMP)
399
+ console.log(RabinKarp.findOccurrences('aaaa', 'aa')); // [0, 1, 2]
400
+
401
+ // Empty pattern: no matches
402
+ console.log(new RabinKarp('').search('text')); // []
403
+
404
+ // --- String rolling hash: default base 131, mod 1_000_000_007 (both configurable) ---
405
+ const rh = new StringRollingHash('hello');
406
+ // Internally: prefix[] and pow[] so substring hashes are O(1)
407
+
408
+ // Hash of the entire string (same as substring from 0 with full length)
409
+ console.log(rh.fullHash()); // bigint — concrete value depends on base/mod
410
+ console.log(rh.substringHash(0, rh.length())); // same bigint as fullHash()
411
+
412
+ // Hash of substring "ell" — starts at index 1, length 3
413
+ console.log(rh.substringHash(1, 3)); // bigint for "ell"
414
+
415
+ // Two equal strings with the same base/mod yield equal full hashes
416
+ const same = new StringRollingHash('hello');
417
+ console.log(rh.fullHash() === same.fullHash()); // true
418
+
419
+ // Compare a substring of one string to another range (useful for pattern checks)
420
+ const a = new StringRollingHash('banana');
421
+ const b = new StringRollingHash('na');
422
+ // Hash of "na" in "banana" at index 2 should match b’s full hash if substrings equal
423
+ console.log(a.substringHash(2, 2) === b.fullHash()); // true — both are "na"
424
+ ```
425
+
239
426
  ---
240
427
 
241
428
  ## API overview
@@ -243,15 +430,15 @@ Use an **unweighted** graph (adjacency list) when you only care about connectivi
243
430
  | Module | Exports |
244
431
  |--------|--------|
245
432
  | **Collections** | `Vector`, `Stack`, `Queue`, `List`, `ListNode`, `PriorityQueue`, `OrderedMap`, `UnorderedMap`, `OrderedSet`, `UnorderedSet`, `OrderedMultiMap`, `OrderedMultiSet`, `WeightedEdge`, `AdjacencyList`, `WeightedAdjacencyList`, `createAdjacencyList`, `createWeightedAdjacencyList`, `addEdge`, `deleteEdge` |
246
- | **Algorithms** | `sort`, `find`, `findIndex`, `transform`, `filter`, `reduce`, `reverse`, `unique`, `binarySearch`, `lowerBound`, `upperBound`, `min`, `max`, `partition` |
433
+ | **Algorithms** | `sort`, `find`, `findIndex`, `transform`, `filter`, `reduce`, `reverse`, `unique`, `binarySearch`, `lowerBound`, `upperBound`, `min`, `max`, `partition`, `DisjointSetUnion`, `KnuthMorrisPratt`, `RabinKarp`, `RABIN_KARP_DEFAULT_MODS`, `StringRollingHash`, `connectedComponents`, `kruskalMST` |
247
434
  | **Utils** | `clamp`, `range`, `noop`, `identity`, `swap` |
248
- | **Types** | `Comparator`, `Predicate`, `UnaryFn`, `Reducer`, `IterableLike`, `toArray` |
435
+ | **Types** | `Comparator`, `Predicate`, `UnaryFn`, `Reducer`, `IterableLike`, `toArray`, `RabinKarpTripleMods` |
249
436
 
250
437
  ### Subpath imports (tree-shaking)
251
438
 
252
439
  ```ts
253
440
  import { Vector, Stack } from 'typescript-dsa-stl/collections';
254
- import { sort, binarySearch } from 'typescript-dsa-stl/algorithms';
441
+ import { sort, binarySearch, KnuthMorrisPratt, RabinKarp, StringRollingHash } from 'typescript-dsa-stl/algorithms';
255
442
  import { clamp, range } from 'typescript-dsa-stl/utils';
256
443
  import type { Comparator } from 'typescript-dsa-stl/types';
257
444
  ```
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Disjoint Set Union (Union-Find) with:
3
+ * - Path compression (iterative)
4
+ * - Union by size
5
+ *
6
+ * Optimized for vertex indices in range [0..n-1].
7
+ */
8
+ export declare class DisjointSetUnion {
9
+ private readonly parent;
10
+ private readonly size;
11
+ constructor(n: number);
12
+ /**
13
+ * Find the representative(root) of x.
14
+ * Amortized almost O(1) with path compression.
15
+ */
16
+ find(x: number): number;
17
+ /** Returns true if a merge happened (were in different sets). */
18
+ union(a: number, b: number): boolean;
19
+ connected(a: number, b: number): boolean;
20
+ /** Size of the connected component containing x. */
21
+ componentSize(x: number): number;
22
+ /**
23
+ * Get component representative id for each vertex.
24
+ * Runs `find(i)` for each i (so it also compresses paths).
25
+ */
26
+ roots(): Int32Array;
27
+ /**
28
+ * Group vertices into components.
29
+ * Complexity: O(n * alpha(n)).
30
+ */
31
+ components(nVertices?: number): number[][];
32
+ }
33
+ //# sourceMappingURL=disjointSetUnion.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"disjointSetUnion.d.ts","sourceRoot":"","sources":["../../src/algorithms/disjointSetUnion.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAa;IACpC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAa;gBAEtB,CAAC,EAAE,MAAM;IAUrB;;;OAGG;IACH,IAAI,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAiBvB,iEAAiE;IACjE,KAAK,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO;IAiBpC,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO;IAIxC,oDAAoD;IACpD,aAAa,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAIhC;;;OAGG;IACH,KAAK,IAAI,UAAU;IAQnB;;;OAGG;IACH,UAAU,CAAC,SAAS,SAAqB,GAAG,MAAM,EAAE,EAAE;CAqBvD"}
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Disjoint Set Union (Union-Find) with:
3
+ * - Path compression (iterative)
4
+ * - Union by size
5
+ *
6
+ * Optimized for vertex indices in range [0..n-1].
7
+ */
8
+ export class DisjointSetUnion {
9
+ constructor(n) {
10
+ // Assumes valid vertex count and indices are provided by the caller.
11
+ this.parent = new Int32Array(n);
12
+ this.size = new Int32Array(n);
13
+ for (let i = 0; i < n; i++) {
14
+ this.parent[i] = i;
15
+ this.size[i] = 1;
16
+ }
17
+ }
18
+ /**
19
+ * Find the representative(root) of x.
20
+ * Amortized almost O(1) with path compression.
21
+ */
22
+ find(x) {
23
+ // Find root.
24
+ let root = x;
25
+ while (this.parent[root] !== root) {
26
+ root = this.parent[root];
27
+ }
28
+ // Compress path.
29
+ while (this.parent[x] !== x) {
30
+ const p = this.parent[x];
31
+ this.parent[x] = root;
32
+ x = p;
33
+ }
34
+ return root;
35
+ }
36
+ /** Returns true if a merge happened (were in different sets). */
37
+ union(a, b) {
38
+ let ra = this.find(a);
39
+ let rb = this.find(b);
40
+ if (ra === rb)
41
+ return false;
42
+ // Union by size: attach smaller tree under larger tree.
43
+ if (this.size[ra] < this.size[rb]) {
44
+ const tmp = ra;
45
+ ra = rb;
46
+ rb = tmp;
47
+ }
48
+ this.parent[rb] = ra;
49
+ this.size[ra] += this.size[rb];
50
+ return true;
51
+ }
52
+ connected(a, b) {
53
+ return this.find(a) === this.find(b);
54
+ }
55
+ /** Size of the connected component containing x. */
56
+ componentSize(x) {
57
+ return this.size[this.find(x)];
58
+ }
59
+ /**
60
+ * Get component representative id for each vertex.
61
+ * Runs `find(i)` for each i (so it also compresses paths).
62
+ */
63
+ roots() {
64
+ const out = new Int32Array(this.parent.length);
65
+ for (let i = 0; i < out.length; i++) {
66
+ out[i] = this.find(i);
67
+ }
68
+ return out;
69
+ }
70
+ /**
71
+ * Group vertices into components.
72
+ * Complexity: O(n * alpha(n)).
73
+ */
74
+ components(nVertices = this.parent.length) {
75
+ // Use an array lookup instead of Map for faster grouping.
76
+ const rootToIndex = new Int32Array(this.parent.length);
77
+ rootToIndex.fill(-1);
78
+ const groups = [];
79
+ const n = Math.min(nVertices, this.parent.length);
80
+ for (let i = 0; i < n; i++) {
81
+ const r = this.find(i);
82
+ let idx = rootToIndex[r];
83
+ if (idx === -1) {
84
+ idx = groups.length;
85
+ rootToIndex[r] = idx;
86
+ groups.push([]);
87
+ }
88
+ groups[idx].push(i);
89
+ }
90
+ return groups;
91
+ }
92
+ }
93
+ //# sourceMappingURL=disjointSetUnion.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"disjointSetUnion.js","sourceRoot":"","sources":["../../src/algorithms/disjointSetUnion.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,OAAO,gBAAgB;IAI3B,YAAY,CAAS;QACnB,qEAAqE;QACrE,IAAI,CAAC,MAAM,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC;QAChC,IAAI,CAAC,IAAI,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC;QAC9B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YAC3B,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;YACnB,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QACnB,CAAC;IACH,CAAC;IAED;;;OAGG;IACH,IAAI,CAAC,CAAS;QACZ,aAAa;QACb,IAAI,IAAI,GAAG,CAAC,CAAC;QACb,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;YAClC,IAAI,GAAG,IAAI,CAAC,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;IAChB,CAAC;CACF"}
@@ -0,0 +1,29 @@
1
+ import type { AdjacencyList, WeightedAdjacencyList } from '../collections/index.js';
2
+ export interface WeightedUndirectedEdge {
3
+ u: number;
4
+ v: number;
5
+ weight: number;
6
+ }
7
+ /**
8
+ * Connected components in an (unweighted) graph using DSU.
9
+ * Complexity: O((n + m) * alpha(n)).
10
+ *
11
+ * Notes:
12
+ * - Assumes vertices are numbered `0..n-1`.
13
+ * - If your adjacency list stores each edge once or twice, DSU will still work.
14
+ */
15
+ export declare function connectedComponents(n: number, adj: AdjacencyList<number>): number[][];
16
+ /**
17
+ * Kruskal's algorithm for Minimum Spanning Tree (MST) using DSU.
18
+ *
19
+ * Complexity: O(m log m) for sorting edges + O(m * alpha(n)).
20
+ *
21
+ * @returns a spanning forest if the graph is disconnected.
22
+ */
23
+ export declare function kruskalMST(n: number, adj: WeightedAdjacencyList<number, number>, options?: {
24
+ undirected?: boolean;
25
+ }): {
26
+ edges: WeightedUndirectedEdge[];
27
+ totalWeight: number;
28
+ };
29
+ //# sourceMappingURL=graph.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph.d.ts","sourceRoot":"","sources":["../../src/algorithms/graph.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAGpF,MAAM,WAAW,sBAAsB;IACrC,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,CAAC,EAAE,MAAM,EACT,GAAG,EAAE,aAAa,CAAC,MAAM,CAAC,GACzB,MAAM,EAAE,EAAE,CAQZ;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CACxB,CAAC,EAAE,MAAM,EACT,GAAG,EAAE,qBAAqB,CAAC,MAAM,EAAE,MAAM,CAAC,EAC1C,OAAO,CAAC,EAAE;IAAE,UAAU,CAAC,EAAE,OAAO,CAAA;CAAE,GACjC;IAAE,KAAK,EAAE,sBAAsB,EAAE,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,CAgC1D"}
@@ -0,0 +1,57 @@
1
+ import { DisjointSetUnion } from './disjointSetUnion.js';
2
+ /**
3
+ * Connected components in an (unweighted) graph using DSU.
4
+ * Complexity: O((n + m) * alpha(n)).
5
+ *
6
+ * Notes:
7
+ * - Assumes vertices are numbered `0..n-1`.
8
+ * - If your adjacency list stores each edge once or twice, DSU will still work.
9
+ */
10
+ export function connectedComponents(n, adj) {
11
+ const dsu = new DisjointSetUnion(n);
12
+ for (let u = 0; u < n; u++) {
13
+ for (const v of adj[u] ?? []) {
14
+ dsu.union(u, v);
15
+ }
16
+ }
17
+ return dsu.components(n);
18
+ }
19
+ /**
20
+ * Kruskal's algorithm for Minimum Spanning Tree (MST) using DSU.
21
+ *
22
+ * Complexity: O(m log m) for sorting edges + O(m * alpha(n)).
23
+ *
24
+ * @returns a spanning forest if the graph is disconnected.
25
+ */
26
+ export function kruskalMST(n, adj, options) {
27
+ const undirected = options?.undirected ?? true;
28
+ // Extract edges from adjacency list.
29
+ // For undirected graphs represented as both-direction adjacency,
30
+ // take only u < v to avoid duplicates.
31
+ const edges = [];
32
+ for (let u = 0; u < n; u++) {
33
+ for (const { to: v, weight } of adj[u] ?? []) {
34
+ if (undirected) {
35
+ if (u < v)
36
+ edges.push({ u, v, weight });
37
+ }
38
+ else {
39
+ edges.push({ u, v, weight });
40
+ }
41
+ }
42
+ }
43
+ edges.sort((a, b) => a.weight - b.weight);
44
+ const dsu = new DisjointSetUnion(n);
45
+ const mstEdges = [];
46
+ let totalWeight = 0;
47
+ for (const e of edges) {
48
+ if (dsu.union(e.u, e.v)) {
49
+ mstEdges.push(e);
50
+ totalWeight += e.weight;
51
+ if (mstEdges.length === n - 1)
52
+ break; // MST complete for connected graphs.
53
+ }
54
+ }
55
+ return { edges: mstEdges, totalWeight };
56
+ }
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CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;YAC/B,CAAC;QACH,CAAC;IACH,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC;IAE1C,MAAM,GAAG,GAAG,IAAI,gBAAgB,CAAC,CAAC,CAAC,CAAC;IACpC,MAAM,QAAQ,GAA6B,EAAE,CAAC;IAC9C,IAAI,WAAW,GAAG,CAAC,CAAC;IAEpB,KAAK,MAAM,CAAC,IAAI,KAAK,EAAE,CAAC;QACtB,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YACxB,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YACjB,WAAW,IAAI,CAAC,CAAC,MAAM,CAAC;YACxB,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,GAAG,CAAC;gBAAE,MAAM,CAAC,qCAAqC;QAC7E,CAAC;IACH,CAAC;IAED,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAC;AAC1C,CAAC"}
@@ -1,2 +1,9 @@
1
1
  export { sort, find, findIndex, transform, filter, reduce, reverse, unique, binarySearch, lowerBound, upperBound, min, max, partition, } from './array.js';
2
+ export { DisjointSetUnion } from './disjointSetUnion.js';
3
+ export { KnuthMorrisPratt } from './kmp.js';
4
+ export { StringRollingHash } from './stringHashing.js';
5
+ export { RabinKarp, RABIN_KARP_DEFAULT_MODS } from './rabinKarp.js';
6
+ export type { RabinKarpTripleMods } from './rabinKarp.js';
7
+ export type { WeightedUndirectedEdge } from './graph.js';
8
+ export { connectedComponents, kruskalMST } from './graph.js';
2
9
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/algorithms/index.ts"],"names":[],"mappings":"AAAA,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,GACV,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/algorithms/index.ts"],"names":[],"mappings":"AAAA,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,GACV,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAC5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AACpE,YAAY,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAC1D,YAAY,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AACzD,OAAO,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC"}
@@ -1,2 +1,7 @@
1
1
  export { sort, find, findIndex, transform, filter, reduce, reverse, unique, binarySearch, lowerBound, upperBound, min, max, partition, } from './array.js';
2
+ export { DisjointSetUnion } from './disjointSetUnion.js';
3
+ export { KnuthMorrisPratt } from './kmp.js';
4
+ export { StringRollingHash } from './stringHashing.js';
5
+ export { RabinKarp, RABIN_KARP_DEFAULT_MODS } from './rabinKarp.js';
6
+ export { connectedComponents, kruskalMST } from './graph.js';
2
7
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/algorithms/index.ts"],"names":[],"mappings":"AAAA,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,GACV,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/algorithms/index.ts"],"names":[],"mappings":"AAAA,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,GACV,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAC5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAGpE,OAAO,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Knuth–Morris–Pratt (KMP) string matching.
3
+ * Precomputes the LPS (longest proper prefix which is also suffix) table for the pattern,
4
+ * then scans text in O(n + m) time.
5
+ */
6
+ export declare class KnuthMorrisPratt {
7
+ private readonly pattern;
8
+ private readonly lps;
9
+ constructor(pattern: string);
10
+ /** LPS array: lps[i] = length of longest proper prefix of pattern[0..i] that is also a suffix. */
11
+ static buildLps(pattern: string): number[];
12
+ /**
13
+ * All start indices in `text` where `this.pattern` occurs.
14
+ * Empty pattern yields no matches.
15
+ */
16
+ search(text: string): number[];
17
+ /** One-shot: all occurrences of `pattern` in `text`. */
18
+ static findOccurrences(text: string, pattern: string): number[];
19
+ }
20
+ //# sourceMappingURL=kmp.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"kmp.d.ts","sourceRoot":"","sources":["../../src/algorithms/kmp.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAW;gBAEnB,OAAO,EAAE,MAAM;IAK3B,kGAAkG;IAClG,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE;IAoB1C;;;OAGG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE;IA8B9B,wDAAwD;IACxD,MAAM,CAAC,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE;CAGhE"}
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Knuth–Morris–Pratt (KMP) string matching.
3
+ * Precomputes the LPS (longest proper prefix which is also suffix) table for the pattern,
4
+ * then scans text in O(n + m) time.
5
+ */
6
+ export class KnuthMorrisPratt {
7
+ constructor(pattern) {
8
+ this.pattern = pattern;
9
+ this.lps = KnuthMorrisPratt.buildLps(pattern);
10
+ }
11
+ /** LPS array: lps[i] = length of longest proper prefix of pattern[0..i] that is also a suffix. */
12
+ static buildLps(pattern) {
13
+ const m = pattern.length;
14
+ const lps = new Array(m).fill(0);
15
+ let len = 0;
16
+ let i = 1;
17
+ while (i < m) {
18
+ if (pattern[i] === pattern[len]) {
19
+ len++;
20
+ lps[i] = len;
21
+ i++;
22
+ }
23
+ else if (len !== 0) {
24
+ len = lps[len - 1];
25
+ }
26
+ else {
27
+ lps[i] = 0;
28
+ i++;
29
+ }
30
+ }
31
+ return lps;
32
+ }
33
+ /**
34
+ * All start indices in `text` where `this.pattern` occurs.
35
+ * Empty pattern yields no matches.
36
+ */
37
+ search(text) {
38
+ const m = this.pattern.length;
39
+ if (m === 0)
40
+ return [];
41
+ const n = text.length;
42
+ const matches = [];
43
+ let i = 0;
44
+ let j = 0;
45
+ while (i < n) {
46
+ if (text[i] === this.pattern[j]) {
47
+ i++;
48
+ j++;
49
+ }
50
+ if (j === m) {
51
+ matches.push(i - j);
52
+ j = this.lps[j - 1];
53
+ }
54
+ else if (i < n && text[i] !== this.pattern[j]) {
55
+ if (j !== 0) {
56
+ j = this.lps[j - 1];
57
+ }
58
+ else {
59
+ i++;
60
+ }
61
+ }
62
+ }
63
+ return matches;
64
+ }
65
+ /** One-shot: all occurrences of `pattern` in `text`. */
66
+ static findOccurrences(text, pattern) {
67
+ return new KnuthMorrisPratt(pattern).search(text);
68
+ }
69
+ }
70
+ //# sourceMappingURL=kmp.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"kmp.js","sourceRoot":"","sources":["../../src/algorithms/kmp.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,OAAO,gBAAgB;IAI3B,YAAY,OAAe;QACzB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,GAAG,GAAG,gBAAgB,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IAChD,CAAC;IAED,kGAAkG;IAClG,MAAM,CAAC,QAAQ,CAAC,OAAe;QAC7B,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC;QACzB,MAAM,GAAG,GAAG,IAAI,KAAK,CAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACzC,IAAI,GAAG,GAAG,CAAC,CAAC;QACZ,IAAI,CAAC,GAAG,CAAC,CAAC;QACV,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;YACb,IAAI,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;gBAChC,GAAG,EAAE,CAAC;gBACN,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC;gBACb,CAAC,EAAE,CAAC;YACN,CAAC;iBAAM,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C;YACH,CAAC;QACH,CAAC;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,wDAAwD;IACxD,MAAM,CAAC,eAAe,CAAC,IAAY,EAAE,OAAe;QAClD,OAAO,IAAI,gBAAgB,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACpD,CAAC;CACF"}
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Rabin–Karp string matching with **triple hashing**: three independent polynomial hashes
3
+ * (same base, distinct prime moduli). A window is a candidate only when all three hashes
4
+ * match the pattern’s triple—collision probability is negligible; we still verify with a
5
+ * direct character comparison so results are always correct.
6
+ */
7
+ /** Default moduli: distinct large primes (common CP / hashing choices). */
8
+ export declare const RABIN_KARP_DEFAULT_MODS: readonly [1000000007n, 1000000009n, 998244353n];
9
+ export type RabinKarpTripleMods = readonly [bigint, bigint, bigint];
10
+ export declare class RabinKarp {
11
+ private readonly pattern;
12
+ private readonly base;
13
+ private readonly mods;
14
+ /**
15
+ * @param pattern Pattern to search for (UTF-16 code units, same as `charCodeAt`).
16
+ * @param base Shared radix (default 131), same idea as `StringRollingHash`.
17
+ * @param mods Three distinct prime moduli for independent rolling hashes.
18
+ */
19
+ constructor(pattern: string, base?: bigint, mods?: RabinKarpTripleMods);
20
+ private static substringsEqual;
21
+ /** Iterative hash of `s[0..length)` for each modulus. */
22
+ private hashPrefix;
23
+ /** `base^(m-1) mod mods[j]` for sliding the left character out of the window. */
24
+ private powHighForWindow;
25
+ private slideWindow;
26
+ private triplesEqual;
27
+ /**
28
+ * All start indices in `text` where `this.pattern` occurs.
29
+ * Empty pattern yields no matches.
30
+ */
31
+ search(text: string): number[];
32
+ /** One-shot: all occurrences of `pattern` in `text`. */
33
+ static findOccurrences(text: string, pattern: string, base?: bigint, mods?: RabinKarpTripleMods): number[];
34
+ }
35
+ //# sourceMappingURL=rabinKarp.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rabinKarp.d.ts","sourceRoot":"","sources":["../../src/algorithms/rabinKarp.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAQH,2EAA2E;AAC3E,eAAO,MAAM,uBAAuB,iDAI1B,CAAC;AAEX,MAAM,MAAM,mBAAmB,GAAG,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAEpE,qBAAa,SAAS;IACpB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAsB;IAE3C;;;;OAIG;gBAED,OAAO,EAAE,MAAM,EACf,IAAI,SAAO,EACX,IAAI,GAAE,mBAA6C;IAerD,OAAO,CAAC,MAAM,CAAC,eAAe;IAY9B,yDAAyD;IACzD,OAAO,CAAC,UAAU;IAYlB,iFAAiF;IACjF,OAAO,CAAC,gBAAgB;IAcxB,OAAO,CAAC,WAAW;IAoBnB,OAAO,CAAC,YAAY;IAIpB;;;OAGG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE;IA4B9B,wDAAwD;IACxD,MAAM,CAAC,eAAe,CACpB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EACf,IAAI,SAAO,EACX,IAAI,GAAE,mBAA6C,GAClD,MAAM,EAAE;CAGZ"}
@@ -0,0 +1,119 @@
1
+ /**
2
+ * Rabin–Karp string matching with **triple hashing**: three independent polynomial hashes
3
+ * (same base, distinct prime moduli). A window is a candidate only when all three hashes
4
+ * match the pattern’s triple—collision probability is negligible; we still verify with a
5
+ * direct character comparison so results are always correct.
6
+ */
7
+ function modNormalize(value, mod) {
8
+ let x = value % mod;
9
+ if (x < 0n)
10
+ x += mod;
11
+ return x;
12
+ }
13
+ /** Default moduli: distinct large primes (common CP / hashing choices). */
14
+ export const RABIN_KARP_DEFAULT_MODS = [
15
+ 1000000007n,
16
+ 1000000009n,
17
+ 998244353n,
18
+ ];
19
+ export class RabinKarp {
20
+ /**
21
+ * @param pattern Pattern to search for (UTF-16 code units, same as `charCodeAt`).
22
+ * @param base Shared radix (default 131), same idea as `StringRollingHash`.
23
+ * @param mods Three distinct prime moduli for independent rolling hashes.
24
+ */
25
+ constructor(pattern, base = 131n, mods = RABIN_KARP_DEFAULT_MODS) {
26
+ if (base <= 0n) {
27
+ throw new RangeError('base must be positive');
28
+ }
29
+ for (let j = 0; j < 3; j++) {
30
+ if (mods[j] <= 0n) {
31
+ throw new RangeError('each mod must be positive');
32
+ }
33
+ }
34
+ this.pattern = pattern;
35
+ this.base = base;
36
+ this.mods = mods;
37
+ }
38
+ static substringsEqual(text, start, pattern, length) {
39
+ for (let k = 0; k < length; k++) {
40
+ if (text[start + k] !== pattern[k])
41
+ return false;
42
+ }
43
+ return true;
44
+ }
45
+ /** Iterative hash of `s[0..length)` for each modulus. */
46
+ hashPrefix(s, length) {
47
+ const h = [0n, 0n, 0n];
48
+ const { base, mods } = this;
49
+ for (let i = 0; i < length; i++) {
50
+ const c = BigInt(s.charCodeAt(i));
51
+ for (let j = 0; j < 3; j++) {
52
+ h[j] = modNormalize(h[j] * base + c, mods[j]);
53
+ }
54
+ }
55
+ return h;
56
+ }
57
+ /** `base^(m-1) mod mods[j]` for sliding the left character out of the window. */
58
+ powHighForWindow(m) {
59
+ const out = [1n, 1n, 1n];
60
+ const { base, mods } = this;
61
+ for (let j = 0; j < 3; j++) {
62
+ let p = 1n;
63
+ const mod = mods[j];
64
+ for (let i = 0; i < m - 1; i++) {
65
+ p = modNormalize(p * base, mod);
66
+ }
67
+ out[j] = p;
68
+ }
69
+ return out;
70
+ }
71
+ slideWindow(window, left, right, powHigh) {
72
+ const next = [0n, 0n, 0n];
73
+ const { base, mods } = this;
74
+ for (let j = 0; j < 3; j++) {
75
+ const mod = mods[j];
76
+ const w = window[j];
77
+ const ph = powHigh[j];
78
+ next[j] = modNormalize(modNormalize(w - modNormalize(left * ph, mod), mod) * base + right, mod);
79
+ }
80
+ return next;
81
+ }
82
+ triplesEqual(a, b) {
83
+ return a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
84
+ }
85
+ /**
86
+ * All start indices in `text` where `this.pattern` occurs.
87
+ * Empty pattern yields no matches.
88
+ */
89
+ search(text) {
90
+ const pat = this.pattern;
91
+ const m = pat.length;
92
+ const n = text.length;
93
+ if (m === 0)
94
+ return [];
95
+ if (m > n)
96
+ return [];
97
+ const patHash = this.hashPrefix(pat, m);
98
+ let windowHash = this.hashPrefix(text, m);
99
+ const powHigh = this.powHighForWindow(m);
100
+ const matches = [];
101
+ for (let i = 0; i <= n - m; i++) {
102
+ if (this.triplesEqual(windowHash, patHash) &&
103
+ RabinKarp.substringsEqual(text, i, pat, m)) {
104
+ matches.push(i);
105
+ }
106
+ if (i + m >= n)
107
+ break;
108
+ const left = BigInt(text.charCodeAt(i));
109
+ const right = BigInt(text.charCodeAt(i + m));
110
+ windowHash = this.slideWindow(windowHash, left, right, powHigh);
111
+ }
112
+ return matches;
113
+ }
114
+ /** One-shot: all occurrences of `pattern` in `text`. */
115
+ static findOccurrences(text, pattern, base = 131n, mods = RABIN_KARP_DEFAULT_MODS) {
116
+ return new RabinKarp(pattern, base, mods).search(text);
117
+ }
118
+ }
119
+ //# sourceMappingURL=rabinKarp.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rabinKarp.js","sourceRoot":"","sources":["../../src/algorithms/rabinKarp.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,SAAS,YAAY,CAAC,KAAa,EAAE,GAAW;IAC9C,IAAI,CAAC,GAAG,KAAK,GAAG,GAAG,CAAC;IACpB,IAAI,CAAC,GAAG,EAAE;QAAE,CAAC,IAAI,GAAG,CAAC;IACrB,OAAO,CAAC,CAAC;AACX,CAAC;AAED,2EAA2E;AAC3E,MAAM,CAAC,MAAM,uBAAuB,GAAG;IACrC,WAAc;IACd,WAAc;IACd,UAAY;CACJ,CAAC;AAIX,MAAM,OAAO,SAAS;IAKpB;;;;OAIG;IACH,YACE,OAA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E,IAAI,CAAC,YAAY,CAAC,UAAU,EAAE,OAAO,CAAC;gBACtC,SAAS,CAAC,eAAe,CAAC,IAAI,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,EAC1C,CAAC;gBACD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YAClB,CAAC;YACD,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;gBAAE,MAAM;YACtB,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;YACxC,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YAC7C,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,UAAU,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;QAClE,CAAC;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,wDAAwD;IACxD,MAAM,CAAC,eAAe,CACpB,IAAY,EACZ,OAAe,EACf,IAAI,GAAG,IAAI,EACX,OAA4B,uBAAuB;QAEnD,OAAO,IAAI,SAAS,CAAC,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACzD,CAAC;CACF"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Polynomial rolling hash on a string: prefix hashes and powers of base
3
+ * support O(1) substring hash queries after O(n) preprocessing.
4
+ */
5
+ export declare class StringRollingHash {
6
+ private readonly n;
7
+ private readonly base;
8
+ private readonly mod;
9
+ private readonly prefix;
10
+ private readonly pow;
11
+ /**
12
+ * @param s Source string (UTF-16 code units, same as `charCodeAt`).
13
+ * @param base Must be positive and coprime to `mod` for typical use.
14
+ * @param mod Prime modulus (e.g. 1_000_000_007n).
15
+ */
16
+ constructor(s: string, base?: bigint, mod?: bigint);
17
+ /** Full string hash (same as substringHash(0, length)). */
18
+ fullHash(): bigint;
19
+ /**
20
+ * Hash of `s.substring(start, start + length)` using the configured base and mod.
21
+ * Indices are in [0, n); `length` must be non-negative and `start + length <= n`.
22
+ */
23
+ substringHash(start: number, length: number): bigint;
24
+ /** Underlying string length. */
25
+ length(): number;
26
+ }
27
+ //# sourceMappingURL=stringHashing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stringHashing.d.ts","sourceRoot":"","sources":["../../src/algorithms/stringHashing.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAQH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAS;IAC3B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAS;IAC7B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAW;IAClC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAW;IAE/B;;;;OAIG;gBACS,CAAC,EAAE,MAAM,EAAE,IAAI,SAAO,EAAE,GAAG,SAAiB;IAqBxD,2DAA2D;IAC3D,QAAQ,IAAI,MAAM;IAIlB;;;OAGG;IACH,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IAapD,gCAAgC;IAChC,MAAM,IAAI,MAAM;CAGjB"}
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Polynomial rolling hash on a string: prefix hashes and powers of base
3
+ * support O(1) substring hash queries after O(n) preprocessing.
4
+ */
5
+ function modNormalize(value, mod) {
6
+ let x = value % mod;
7
+ if (x < 0n)
8
+ x += mod;
9
+ return x;
10
+ }
11
+ export class StringRollingHash {
12
+ /**
13
+ * @param s Source string (UTF-16 code units, same as `charCodeAt`).
14
+ * @param base Must be positive and coprime to `mod` for typical use.
15
+ * @param mod Prime modulus (e.g. 1_000_000_007n).
16
+ */
17
+ constructor(s, base = 131n, mod = 1000000007n) {
18
+ if (mod <= 0n) {
19
+ throw new RangeError('mod must be positive');
20
+ }
21
+ if (base <= 0n) {
22
+ throw new RangeError('base must be positive');
23
+ }
24
+ this.n = s.length;
25
+ this.base = base;
26
+ this.mod = mod;
27
+ this.prefix = new Array(this.n + 1);
28
+ this.pow = new Array(this.n + 1);
29
+ this.prefix[0] = 0n;
30
+ this.pow[0] = 1n;
31
+ for (let i = 0; i < this.n; i++) {
32
+ this.pow[i + 1] = modNormalize(this.pow[i] * base, mod);
33
+ const c = BigInt(s.charCodeAt(i));
34
+ this.prefix[i + 1] = modNormalize(this.prefix[i] * base + c, mod);
35
+ }
36
+ }
37
+ /** Full string hash (same as substringHash(0, length)). */
38
+ fullHash() {
39
+ return this.prefix[this.n];
40
+ }
41
+ /**
42
+ * Hash of `s.substring(start, start + length)` using the configured base and mod.
43
+ * Indices are in [0, n); `length` must be non-negative and `start + length <= n`.
44
+ */
45
+ substringHash(start, length) {
46
+ if (length < 0) {
47
+ throw new RangeError('length must be non-negative');
48
+ }
49
+ if (start < 0 || start + length > this.n) {
50
+ throw new RangeError('substring range out of bounds');
51
+ }
52
+ const end = start + length;
53
+ const high = this.prefix[end];
54
+ const low = modNormalize(this.prefix[start] * this.pow[length], this.mod);
55
+ return modNormalize(high - low, this.mod);
56
+ }
57
+ /** Underlying string length. */
58
+ length() {
59
+ return this.n;
60
+ }
61
+ }
62
+ //# sourceMappingURL=stringHashing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stringHashing.js","sourceRoot":"","sources":["../../src/algorithms/stringHashing.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,SAAS,YAAY,CAAC,KAAa,EAAE,GAAW;IAC9C,IAAI,CAAC,GAAG,KAAK,GAAG,GAAG,CAAC;IACpB,IAAI,CAAC,GAAG,EAAE;QAAE,CAAC,IAAI,GAAG,CAAC;IACrB,OAAO,CAAC,CAAC;AACX,CAAC;AAED,MAAM,OAAO,iBAAiB;IAO5B;;;;OAIG;IACH,YAAY,CAAS,EAAE,IAAI,GAAG,IAAI,EAAE,GAAG,GAAG,WAAc;QACtD,IAAI,GAAG,IAA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,CAAC;IAED;;;OAGG;IACH,aAAa,CAAC,KAAa,EAAE,MAAc;QACzC,IAAI,MAAM,GAAG,CAAC,EAAE,CAAC;YACf,MAAM,IAAI,UAAU,CAAC,6BAA6B,CAAC,CAAC;QACtD,CAAC;QACD,IAAI,KAAK,GAAG,CAAC,IAAI,KAAK,GAAG,MAAM,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC;YACzC,MAAM,IAAI,UAAU,CAAC,+BAA+B,CAAC,CAAC;QACxD,CAAC;QACD,MAAM,GAAG,GAAG,KAAK,GAAG,MAAM,CAAC;QAC3B,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAE,CAAC;QAC/B,MAAM,GAAG,GAAG,YAAY,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAE,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAE,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;QAC5E,OAAO,YAAY,CAAC,IAAI,GAAG,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;IAC5C,CAAC;IAED,gCAAgC;IAChC,MAAM;QACJ,OAAO,IAAI,CAAC,CAAC,CAAC;IAChB,CAAC;CACF"}
package/dist/index.d.ts CHANGED
@@ -7,7 +7,8 @@
7
7
  */
8
8
  export { Vector, Stack, Queue, List, ListNode, PriorityQueue, OrderedMap, UnorderedMap, OrderedSet, UnorderedSet, OrderedMultiSet, OrderedMultiMap, addEdge, deleteEdge, createAdjacencyList, createWeightedAdjacencyList, } from './collections/index.js';
9
9
  export type { WeightedEdge, AdjacencyList, WeightedAdjacencyList } from './collections/index.js';
10
- export { sort, find, findIndex, transform, filter, reduce, reverse, unique, binarySearch, lowerBound, upperBound, min, max, partition, } from './algorithms/index.js';
10
+ export { sort, find, findIndex, transform, filter, reduce, reverse, unique, binarySearch, lowerBound, upperBound, min, max, partition, DisjointSetUnion, KnuthMorrisPratt, StringRollingHash, RabinKarp, RABIN_KARP_DEFAULT_MODS, connectedComponents, kruskalMST, } from './algorithms/index.js';
11
+ export type { WeightedUndirectedEdge, RabinKarpTripleMods } from './algorithms/index.js';
11
12
  export { clamp, range, noop, identity, swap } from './utils/index.js';
12
13
  export type { Comparator, Predicate, UnaryFn, Reducer, IterableLike } from './types/index.js';
13
14
  export { toArray } from './types/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,GAC5B,MAAM,wBAAwB,CAAC;AAChC,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AACjG,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,GACV,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACtE,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAC9F,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC"}
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,GAC5B,MAAM,wBAAwB,CAAC;AAChC,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AACjG,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,mBAAmB,EACnB,UAAU,GACX,MAAM,uBAAuB,CAAC;AAC/B,YAAY,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AACzF,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;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
@@ -6,7 +6,7 @@
6
6
  * import { Vector, Stack, Queue, List, sort, binarySearch, clamp } from 'typescript-dsa-stl';
7
7
  */
8
8
  export { Vector, Stack, Queue, List, ListNode, PriorityQueue, OrderedMap, UnorderedMap, OrderedSet, UnorderedSet, OrderedMultiSet, OrderedMultiMap, addEdge, deleteEdge, createAdjacencyList, createWeightedAdjacencyList, } from './collections/index.js';
9
- export { sort, find, findIndex, transform, filter, reduce, reverse, unique, binarySearch, lowerBound, upperBound, min, max, partition, } from './algorithms/index.js';
9
+ export { sort, find, findIndex, transform, filter, reduce, reverse, unique, binarySearch, lowerBound, upperBound, min, max, partition, DisjointSetUnion, KnuthMorrisPratt, StringRollingHash, RabinKarp, RABIN_KARP_DEFAULT_MODS, connectedComponents, 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';
12
12
  //# sourceMappingURL=index.js.map
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,GAC5B,MAAM,wBAAwB,CAAC;AAEhC,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,GACV,MAAM,uBAAuB,CAAC;AAC/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,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,GAC5B,MAAM,wBAAwB,CAAC;AAEhC,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,mBAAmB,EACnB,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,85 +1,85 @@
1
- {
2
- "name": "typescript-dsa-stl",
3
- "version": "2.0.2",
4
- "description": "STL-style data structures and algorithms for TypeScript: Vector, Stack, Queue, List, PriorityQueue, Map, Set, sort, binarySearch. Use like C++ STL.",
5
- "type": "module",
6
- "main": "dist/index.js",
7
- "module": "dist/index.js",
8
- "types": "dist/index.d.ts",
9
- "sideEffects": false,
10
- "exports": {
11
- ".": {
12
- "types": "./dist/index.d.ts",
13
- "import": "./dist/index.js",
14
- "default": "./dist/index.js"
15
- },
16
- "./collections": {
17
- "types": "./dist/collections/index.d.ts",
18
- "import": "./dist/collections/index.js",
19
- "default": "./dist/collections/index.js"
20
- },
21
- "./algorithms": {
22
- "types": "./dist/algorithms/index.d.ts",
23
- "import": "./dist/algorithms/index.js",
24
- "default": "./dist/algorithms/index.js"
25
- },
26
- "./utils": {
27
- "types": "./dist/utils/index.d.ts",
28
- "import": "./dist/utils/index.js",
29
- "default": "./dist/utils/index.js"
30
- },
31
- "./types": {
32
- "types": "./dist/types/index.d.ts",
33
- "import": "./dist/types/index.js",
34
- "default": "./dist/types/index.js"
35
- }
36
- },
37
- "files": [
38
- "dist",
39
- "README.md",
40
- "LICENSE"
41
- ],
42
- "scripts": {
43
- "build": "tsc",
44
- "prepublishOnly": "npm run build"
45
- },
46
- "keywords": [
47
- "typescript",
48
- "data-structures",
49
- "algorithms",
50
- "stl",
51
- "dsa",
52
- "vector",
53
- "stack",
54
- "queue",
55
- "list",
56
- "priority-queue",
57
- "map",
58
- "set",
59
- "ordered-map",
60
- "unordered-map",
61
- "ordered-multimap",
62
- "ordered-multiset",
63
- "multimap",
64
- "multiset",
65
- "binary-search",
66
- "collections",
67
- "sdk"
68
- ],
69
- "author": "",
70
- "license": "MIT",
71
- "repository": {
72
- "type": "git",
73
- "url": "https://github.com/SajidAbdullah729/TypeScript_DSA.git"
74
- },
75
- "homepage": "https://github.com/SajidAbdullah729/TypeScript_DSA#readme",
76
- "bugs": {
77
- "url": "https://github.com/SajidAbdullah729/TypeScript_DSA/issues"
78
- },
79
- "engines": {
80
- "node": ">=18"
81
- },
82
- "devDependencies": {
83
- "typescript": "^5.3.0"
84
- }
85
- }
1
+ {
2
+ "name": "typescript-dsa-stl",
3
+ "version": "2.2.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.",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "module": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "sideEffects": false,
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js",
14
+ "default": "./dist/index.js"
15
+ },
16
+ "./collections": {
17
+ "types": "./dist/collections/index.d.ts",
18
+ "import": "./dist/collections/index.js",
19
+ "default": "./dist/collections/index.js"
20
+ },
21
+ "./algorithms": {
22
+ "types": "./dist/algorithms/index.d.ts",
23
+ "import": "./dist/algorithms/index.js",
24
+ "default": "./dist/algorithms/index.js"
25
+ },
26
+ "./utils": {
27
+ "types": "./dist/utils/index.d.ts",
28
+ "import": "./dist/utils/index.js",
29
+ "default": "./dist/utils/index.js"
30
+ },
31
+ "./types": {
32
+ "types": "./dist/types/index.d.ts",
33
+ "import": "./dist/types/index.js",
34
+ "default": "./dist/types/index.js"
35
+ }
36
+ },
37
+ "files": [
38
+ "dist",
39
+ "README.md",
40
+ "LICENSE"
41
+ ],
42
+ "scripts": {
43
+ "build": "tsc",
44
+ "prepublishOnly": "npm run build"
45
+ },
46
+ "keywords": [
47
+ "typescript",
48
+ "data-structures",
49
+ "algorithms",
50
+ "stl",
51
+ "dsa",
52
+ "vector",
53
+ "stack",
54
+ "queue",
55
+ "list",
56
+ "priority-queue",
57
+ "map",
58
+ "set",
59
+ "ordered-map",
60
+ "unordered-map",
61
+ "ordered-multimap",
62
+ "ordered-multiset",
63
+ "multimap",
64
+ "multiset",
65
+ "binary-search",
66
+ "collections",
67
+ "sdk"
68
+ ],
69
+ "author": "",
70
+ "license": "MIT",
71
+ "repository": {
72
+ "type": "git",
73
+ "url": "https://github.com/SajidAbdullah729/TypeScript_DSA.git"
74
+ },
75
+ "homepage": "https://github.com/SajidAbdullah729/TypeScript_DSA#readme",
76
+ "bugs": {
77
+ "url": "https://github.com/SajidAbdullah729/TypeScript_DSA/issues"
78
+ },
79
+ "engines": {
80
+ "node": ">=18"
81
+ },
82
+ "devDependencies": {
83
+ "typescript": "^5.3.0"
84
+ }
85
+ }