@ac-kit/algo 0.6.0 → 0.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/CHANGELOG.md +67 -0
- package/README.md +44 -15
- package/dist/combinatorics/cartesian-product.d.ts +46 -0
- package/dist/combinatorics/cartesian-product.d.ts.map +1 -0
- package/dist/combinatorics/cartesian-product.js +79 -0
- package/dist/combinatorics/cartesian-product.js.map +1 -0
- package/dist/combinatorics/permutations.d.ts +41 -0
- package/dist/combinatorics/permutations.d.ts.map +1 -0
- package/dist/combinatorics/permutations.js +70 -0
- package/dist/combinatorics/permutations.js.map +1 -0
- package/dist/combinatorics/power-set.d.ts +40 -0
- package/dist/combinatorics/power-set.d.ts.map +1 -0
- package/dist/combinatorics/power-set.js +67 -0
- package/dist/combinatorics/power-set.js.map +1 -0
- package/dist/graph/bfs.d.ts +66 -0
- package/dist/graph/bfs.d.ts.map +1 -0
- package/dist/graph/bfs.js +89 -0
- package/dist/graph/bfs.js.map +1 -0
- package/dist/graph/connected-components.d.ts +35 -0
- package/dist/graph/connected-components.d.ts.map +1 -0
- package/dist/graph/connected-components.js +75 -0
- package/dist/graph/connected-components.js.map +1 -0
- package/dist/graph/floyd-warshall.d.ts +67 -0
- package/dist/graph/floyd-warshall.d.ts.map +1 -0
- package/dist/graph/floyd-warshall.js +106 -0
- package/dist/graph/floyd-warshall.js.map +1 -0
- package/dist/graph/graph-cycle-error.d.ts +16 -0
- package/dist/graph/graph-cycle-error.d.ts.map +1 -0
- package/dist/graph/graph-cycle-error.js +20 -0
- package/dist/graph/graph-cycle-error.js.map +1 -0
- package/dist/graph/lowest-common-ancestor.d.ts +32 -0
- package/dist/graph/lowest-common-ancestor.d.ts.map +1 -0
- package/dist/graph/lowest-common-ancestor.js +56 -0
- package/dist/graph/lowest-common-ancestor.js.map +1 -0
- package/dist/graph/topological-sort.d.ts +51 -0
- package/dist/graph/topological-sort.d.ts.map +1 -0
- package/dist/graph/topological-sort.js +124 -0
- package/dist/graph/topological-sort.js.map +1 -0
- package/dist/group/_nest-by.d.ts +19 -0
- package/dist/group/_nest-by.d.ts.map +1 -0
- package/dist/group/_nest-by.js +85 -0
- package/dist/group/_nest-by.js.map +1 -0
- package/dist/group/count-by.d.ts +31 -0
- package/dist/group/count-by.d.ts.map +1 -0
- package/dist/group/count-by.js +35 -0
- package/dist/group/count-by.js.map +1 -0
- package/dist/group/flat-group-by.d.ts +28 -0
- package/dist/group/flat-group-by.d.ts.map +1 -0
- package/dist/group/flat-group-by.js +30 -0
- package/dist/group/flat-group-by.js.map +1 -0
- package/dist/group/group-adjacent-by.d.ts +31 -0
- package/dist/group/group-adjacent-by.d.ts.map +1 -0
- package/dist/group/group-adjacent-by.js +50 -0
- package/dist/group/group-adjacent-by.js.map +1 -0
- package/dist/group/group-sort.d.ts +38 -0
- package/dist/group/group-sort.d.ts.map +1 -0
- package/dist/group/group-sort.js +49 -0
- package/dist/group/group-sort.js.map +1 -0
- package/dist/group/index-by.d.ts +33 -0
- package/dist/group/index-by.d.ts.map +1 -0
- package/dist/group/index-by.js +42 -0
- package/dist/group/index-by.js.map +1 -0
- package/dist/group/nested-map.d.ts +12 -0
- package/dist/group/nested-map.d.ts.map +1 -0
- package/dist/group/nested-map.js +2 -0
- package/dist/group/nested-map.js.map +1 -0
- package/dist/group/rollup-by.d.ts +62 -0
- package/dist/group/rollup-by.d.ts.map +1 -0
- package/dist/group/rollup-by.js +72 -0
- package/dist/group/rollup-by.js.map +1 -0
- package/dist/group/run-length-encode.d.ts +41 -0
- package/dist/group/run-length-encode.d.ts.map +1 -0
- package/dist/group/run-length-encode.js +66 -0
- package/dist/group/run-length-encode.js.map +1 -0
- package/dist/index.d.ts +32 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +32 -1
- package/dist/index.js.map +1 -1
- package/dist/ordered/is-sorted.d.ts +33 -0
- package/dist/ordered/is-sorted.d.ts.map +1 -0
- package/dist/ordered/is-sorted.js +39 -0
- package/dist/ordered/is-sorted.js.map +1 -0
- package/dist/sample/sample-without-replacement.d.ts +26 -0
- package/dist/sample/sample-without-replacement.d.ts.map +1 -0
- package/dist/sample/sample-without-replacement.js +41 -0
- package/dist/sample/sample-without-replacement.js.map +1 -0
- package/dist/sample/shuffle.d.ts +2 -2
- package/dist/sample/shuffle.d.ts.map +1 -1
- package/dist/sample/shuffle.js +3 -3
- package/dist/sample/shuffle.js.map +1 -1
- package/dist/sample/weighted-choice-index.d.ts +29 -0
- package/dist/sample/weighted-choice-index.d.ts.map +1 -0
- package/dist/sample/weighted-choice-index.js +62 -0
- package/dist/sample/weighted-choice-index.js.map +1 -0
- package/dist/sample/weighted-choice.d.ts +28 -0
- package/dist/sample/weighted-choice.d.ts.map +1 -0
- package/dist/sample/weighted-choice.js +31 -0
- package/dist/sample/weighted-choice.js.map +1 -0
- package/dist/select/minmax.d.ts +52 -0
- package/dist/select/minmax.d.ts.map +1 -0
- package/dist/select/minmax.js +109 -0
- package/dist/select/minmax.js.map +1 -0
- package/dist/select/{quickselect.d.ts → quickselect-in-place.d.ts} +2 -2
- package/dist/select/quickselect-in-place.d.ts.map +1 -0
- package/dist/select/{quickselect.js → quickselect-in-place.js} +2 -2
- package/dist/select/quickselect-in-place.js.map +1 -0
- package/dist/sequence/_matching-runs.d.ts +16 -0
- package/dist/sequence/_matching-runs.d.ts.map +1 -0
- package/dist/sequence/_matching-runs.js +178 -0
- package/dist/sequence/_matching-runs.js.map +1 -0
- package/dist/sequence/common-prefix-length.d.ts +26 -0
- package/dist/sequence/common-prefix-length.d.ts.map +1 -0
- package/dist/sequence/common-prefix-length.js +34 -0
- package/dist/sequence/common-prefix-length.js.map +1 -0
- package/dist/sequence/diff-sequences.d.ts +59 -0
- package/dist/sequence/diff-sequences.d.ts.map +1 -0
- package/dist/sequence/diff-sequences.js +67 -0
- package/dist/sequence/diff-sequences.js.map +1 -0
- package/dist/sequence/levenshtein-distance.d.ts +43 -0
- package/dist/sequence/levenshtein-distance.d.ts.map +1 -0
- package/dist/sequence/levenshtein-distance.js +102 -0
- package/dist/sequence/levenshtein-distance.js.map +1 -0
- package/dist/sequence/longest-common-subsequence.d.ts +28 -0
- package/dist/sequence/longest-common-subsequence.d.ts.map +1 -0
- package/dist/sequence/longest-common-subsequence.js +39 -0
- package/dist/sequence/longest-common-subsequence.js.map +1 -0
- package/dist/sequence/longest-increasing-subsequence.d.ts +28 -0
- package/dist/sequence/longest-increasing-subsequence.d.ts.map +1 -0
- package/dist/sequence/longest-increasing-subsequence.js +47 -0
- package/dist/sequence/longest-increasing-subsequence.js.map +1 -0
- package/dist/sort/_sort-primitives.d.ts +6 -0
- package/dist/sort/_sort-primitives.d.ts.map +1 -0
- package/dist/sort/_sort-primitives.js +18 -0
- package/dist/sort/_sort-primitives.js.map +1 -0
- package/dist/sort/counting-sort.d.ts +40 -0
- package/dist/sort/counting-sort.d.ts.map +1 -0
- package/dist/sort/counting-sort.js +51 -0
- package/dist/sort/counting-sort.js.map +1 -0
- package/dist/sort/merge-sort.d.ts +26 -0
- package/dist/sort/merge-sort.d.ts.map +1 -0
- package/dist/sort/merge-sort.js +73 -0
- package/dist/sort/merge-sort.js.map +1 -0
- package/dist/sort/quick-sort-in-place.d.ts +35 -0
- package/dist/sort/quick-sort-in-place.d.ts.map +1 -0
- package/dist/sort/quick-sort-in-place.js +98 -0
- package/dist/sort/quick-sort-in-place.js.map +1 -0
- package/dist/sort/rank.d.ts +64 -0
- package/dist/sort/rank.d.ts.map +1 -0
- package/dist/sort/rank.js +107 -0
- package/dist/sort/rank.js.map +1 -0
- package/dist/sort/sort-range.d.ts.map +1 -1
- package/dist/sort/sort-range.js +1 -16
- package/dist/sort/sort-range.js.map +1 -1
- package/package.json +6 -6
- package/dist/select/quickselect.d.ts.map +0 -1
- package/dist/select/quickselect.js.map +0 -1
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { assertInteger } from "@ac-kit/core";
|
|
2
|
+
import { randomIndex } from "./_random-index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Draws `size` distinct positions' elements of `items`, uniformly: every
|
|
5
|
+
* ordered selection of `size` positions is equally likely, so the result is
|
|
6
|
+
* also in random order. A raffle, a random subset for review, a test sample.
|
|
7
|
+
*
|
|
8
|
+
* The first `size` steps of a Fisher–Yates shuffle, with the positions it would
|
|
9
|
+
* have swapped kept in a sparse `Map` rather than in a copy of `items`, so the
|
|
10
|
+
* cost follows the sample, not the input: drawing 10 of a million is 10 steps.
|
|
11
|
+
*
|
|
12
|
+
* Time complexity: O(size), `size` calls to `random`. Memory: O(size).
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* sampleWithoutReplacement(entrants, 3, Math.random); // three winners
|
|
17
|
+
* ```;
|
|
18
|
+
*
|
|
19
|
+
* @param items - The population; not copied or modified. Equal elements at
|
|
20
|
+
* different positions are distinct draws.
|
|
21
|
+
* @param size - How many to draw, an integer in `[0, items.length]`.
|
|
22
|
+
* @param random - A source in `[0, 1)`, called once per draw.
|
|
23
|
+
* @returns A fresh array of `size` elements.
|
|
24
|
+
* @throws {RangeError} When `size` is not an integer in `[0, items.length]`.
|
|
25
|
+
*/
|
|
26
|
+
export function sampleWithoutReplacement(items, size, random) {
|
|
27
|
+
const length = items.length;
|
|
28
|
+
assertInteger(size, "sampleWithoutReplacement: size", 0, length);
|
|
29
|
+
// displaced.get(p): the position whose element a swap left at position p;
|
|
30
|
+
// absent means p still holds its own.
|
|
31
|
+
const displaced = new Map();
|
|
32
|
+
const sample = Array.from({ length: size });
|
|
33
|
+
for (let step = 0; step < size; step++) {
|
|
34
|
+
const pick = step + randomIndex(length - step, random);
|
|
35
|
+
const picked = displaced.get(pick) ?? pick;
|
|
36
|
+
sample[step] = items[picked];
|
|
37
|
+
displaced.set(pick, displaced.get(step) ?? step);
|
|
38
|
+
}
|
|
39
|
+
return sample;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=sample-without-replacement.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sample-without-replacement.js","sourceRoot":"","sources":["../../src/sample/sample-without-replacement.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAiB,MAAM,cAAc,CAAC;AAE5D,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,wBAAwB,CACvC,KAAmB,EACnB,IAAY,EACZ,MAAgB;IAEhB,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;IAC5B,aAAa,CAAC,IAAI,EAAE,gCAAgC,EAAE,CAAC,EAAE,MAAM,CAAC,CAAC;IACjE,0EAA0E;IAC1E,sCAAsC;IACtC,MAAM,SAAS,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC5C,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;IAC/C,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC;QACxC,MAAM,IAAI,GAAG,IAAI,GAAG,WAAW,CAAC,MAAM,GAAG,IAAI,EAAE,MAAM,CAAC,CAAC;QACvD,MAAM,MAAM,GAAG,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC;QAC3C,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,MAAM,CAAE,CAAC;QAC9B,SAAS,CAAC,GAAG,CAAC,IAAI,EAAE,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,CAAC;IAClD,CAAC;IACD,OAAO,MAAM,CAAC;AACf,CAAC"}
|
package/dist/sample/shuffle.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { RandomFn } from "@ac-kit/core";
|
|
2
2
|
export type ShuffleAlgorithm = "fisherYates" | "sattolo";
|
|
3
3
|
export type ShuffleOptions = {
|
|
4
4
|
/**
|
|
@@ -7,7 +7,7 @@ export type ShuffleOptions = {
|
|
|
7
7
|
*
|
|
8
8
|
* Defaults to `Math.random` if not provided.
|
|
9
9
|
*/
|
|
10
|
-
|
|
10
|
+
random?: RandomFn;
|
|
11
11
|
/**
|
|
12
12
|
* The shuffle algorithm to use.
|
|
13
13
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shuffle.d.ts","sourceRoot":"","sources":["../../src/sample/shuffle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"shuffle.d.ts","sourceRoot":"","sources":["../../src/sample/shuffle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAI7C,MAAM,MAAM,gBAAgB,GAAG,aAAa,GAAG,SAAS,CAAC;AAEzD,MAAM,MAAM,cAAc,GAAG;IAC5B;;;;;OAKG;IACH,MAAM,CAAC,EAAE,QAAQ,CAAC;IAElB;;;;OAIG;IACH,SAAS,CAAC,EAAE,gBAAgB,CAAC;CAC7B,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,CAAC,EAAE,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,CAAC,EAAE,CAuB7E"}
|
package/dist/sample/shuffle.js
CHANGED
|
@@ -12,14 +12,14 @@ import { randomIndex } from "./_random-index.js";
|
|
|
12
12
|
* @returns A new array containing the shuffled elements of the input.
|
|
13
13
|
*/
|
|
14
14
|
export function shuffle(items, options) {
|
|
15
|
-
const
|
|
15
|
+
const random = options?.random ?? Math.random;
|
|
16
16
|
const algorithm = options?.algorithm ?? "fisherYates";
|
|
17
17
|
const randomize = (i) => {
|
|
18
18
|
if (algorithm === "sattolo") {
|
|
19
|
-
return randomIndex(i,
|
|
19
|
+
return randomIndex(i, random);
|
|
20
20
|
}
|
|
21
21
|
else {
|
|
22
|
-
return randomIndex(i + 1,
|
|
22
|
+
return randomIndex(i + 1, random);
|
|
23
23
|
}
|
|
24
24
|
};
|
|
25
25
|
const out = items.slice();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shuffle.js","sourceRoot":"","sources":["../../src/sample/shuffle.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAqBjD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,OAAO,CAAI,KAAmB,EAAE,OAAwB;IACvE,MAAM,
|
|
1
|
+
{"version":3,"file":"shuffle.js","sourceRoot":"","sources":["../../src/sample/shuffle.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAqBjD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,OAAO,CAAI,KAAmB,EAAE,OAAwB;IACvE,MAAM,MAAM,GAAG,OAAO,EAAE,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC;IAC9C,MAAM,SAAS,GAAG,OAAO,EAAE,SAAS,IAAI,aAAa,CAAC;IAEtD,MAAM,SAAS,GAAG,CAAC,CAAS,EAAU,EAAE;QACvC,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;YAC7B,OAAO,WAAW,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;QAC/B,CAAC;aAAM,CAAC;YACP,OAAO,WAAW,CAAC,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,CAAC;QACnC,CAAC;IACF,CAAC,CAAC;IAEF,MAAM,GAAG,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;IAE1B,KAAK,IAAI,CAAC,GAAG,GAAG,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QACzC,MAAM,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC;QAEvB,MAAM,GAAG,GAAG,GAAG,CAAC,CAAC,CAAE,CAAC;QACpB,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAE,CAAC;QACjB,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC;IACd,CAAC;IAED,OAAO,GAAG,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type RandomFn } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Picks the position of one element of `items` with probability proportional to
|
|
4
|
+
* its weight — the position-returning form of {@link weightedChoice}, for a
|
|
5
|
+
* caller holding parallel arrays.
|
|
6
|
+
*
|
|
7
|
+
* Reads every weight once, then walks the cumulative weights to where one draw
|
|
8
|
+
* of `random` lands. For many draws from one distribution, a precomputed alias
|
|
9
|
+
* table (Vose) answers each in O(1) instead.
|
|
10
|
+
*
|
|
11
|
+
* Time complexity: O(n), one call to `random`. Memory: O(n) for the weights,
|
|
12
|
+
* read once so `weightOf` need not be cheap or repeatable.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* weightedChoiceIndex(["rare", "common"], (item) => (item === "rare" ? 1 : 9), random);
|
|
17
|
+
* // 1 nine times in ten
|
|
18
|
+
* ```;
|
|
19
|
+
*
|
|
20
|
+
* @param items - The candidates.
|
|
21
|
+
* @param weightOf - The weight of an item, a finite number `≥ 0`; an item of
|
|
22
|
+
* weight `0` is never picked.
|
|
23
|
+
* @param random - A source in `[0, 1)`; not called when no weight is positive.
|
|
24
|
+
* @returns A position, or `-1` when `items` is empty or every weight is `0`.
|
|
25
|
+
* @throws {RangeError} When a weight is negative, `NaN` or infinite, or the
|
|
26
|
+
* total weight overflows to `Infinity`.
|
|
27
|
+
*/
|
|
28
|
+
export declare function weightedChoiceIndex<T>(items: readonly T[], weightOf: (item: T, index: number) => number, random: RandomFn): number;
|
|
29
|
+
//# sourceMappingURL=weighted-choice-index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"weighted-choice-index.d.ts","sourceRoot":"","sources":["../../src/sample/weighted-choice-index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE5D;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EACpC,KAAK,EAAE,SAAS,CAAC,EAAE,EACnB,QAAQ,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,EAC5C,MAAM,EAAE,QAAQ,GACd,MAAM,CAoCR"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { describeValue } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Picks the position of one element of `items` with probability proportional to
|
|
4
|
+
* its weight — the position-returning form of {@link weightedChoice}, for a
|
|
5
|
+
* caller holding parallel arrays.
|
|
6
|
+
*
|
|
7
|
+
* Reads every weight once, then walks the cumulative weights to where one draw
|
|
8
|
+
* of `random` lands. For many draws from one distribution, a precomputed alias
|
|
9
|
+
* table (Vose) answers each in O(1) instead.
|
|
10
|
+
*
|
|
11
|
+
* Time complexity: O(n), one call to `random`. Memory: O(n) for the weights,
|
|
12
|
+
* read once so `weightOf` need not be cheap or repeatable.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* weightedChoiceIndex(["rare", "common"], (item) => (item === "rare" ? 1 : 9), random);
|
|
17
|
+
* // 1 nine times in ten
|
|
18
|
+
* ```;
|
|
19
|
+
*
|
|
20
|
+
* @param items - The candidates.
|
|
21
|
+
* @param weightOf - The weight of an item, a finite number `≥ 0`; an item of
|
|
22
|
+
* weight `0` is never picked.
|
|
23
|
+
* @param random - A source in `[0, 1)`; not called when no weight is positive.
|
|
24
|
+
* @returns A position, or `-1` when `items` is empty or every weight is `0`.
|
|
25
|
+
* @throws {RangeError} When a weight is negative, `NaN` or infinite, or the
|
|
26
|
+
* total weight overflows to `Infinity`.
|
|
27
|
+
*/
|
|
28
|
+
export function weightedChoiceIndex(items, weightOf, random) {
|
|
29
|
+
const length = items.length;
|
|
30
|
+
const weights = new Float64Array(length);
|
|
31
|
+
let total = 0;
|
|
32
|
+
let lastPositive = -1;
|
|
33
|
+
for (let index = 0; index < length; index++) {
|
|
34
|
+
const weight = weightOf(items[index], index);
|
|
35
|
+
if (!(weight >= 0 && weight < Infinity)) {
|
|
36
|
+
throw new RangeError(`weightedChoiceIndex: the weight of items[${index}] must be a finite number ≥ 0, got ${describeValue(weight)}`);
|
|
37
|
+
}
|
|
38
|
+
weights[index] = weight;
|
|
39
|
+
total += weight;
|
|
40
|
+
if (weight > 0) {
|
|
41
|
+
lastPositive = index;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (total === Infinity) {
|
|
45
|
+
throw new RangeError("weightedChoiceIndex: the total weight overflows");
|
|
46
|
+
}
|
|
47
|
+
if (lastPositive < 0) {
|
|
48
|
+
return -1;
|
|
49
|
+
}
|
|
50
|
+
const target = random() * total;
|
|
51
|
+
let cumulative = 0;
|
|
52
|
+
for (let index = 0; index < lastPositive; index++) {
|
|
53
|
+
cumulative += weights[index];
|
|
54
|
+
// `>` rather than `≥` steps over zero weights, which add nothing.
|
|
55
|
+
if (cumulative > target) {
|
|
56
|
+
return index;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// Rounding in the running sum can leave `target` just past it.
|
|
60
|
+
return lastPositive;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=weighted-choice-index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"weighted-choice-index.js","sourceRoot":"","sources":["../../src/sample/weighted-choice-index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAiB,MAAM,cAAc,CAAC;AAE5D;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,mBAAmB,CAClC,KAAmB,EACnB,QAA4C,EAC5C,MAAgB;IAEhB,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;IAC5B,MAAM,OAAO,GAAG,IAAI,YAAY,CAAC,MAAM,CAAC,CAAC;IACzC,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,IAAI,YAAY,GAAG,CAAC,CAAC,CAAC;IACtB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QAC7C,MAAM,MAAM,GAAG,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAE,EAAE,KAAK,CAAC,CAAC;QAC9C,IAAI,CAAC,CAAC,MAAM,IAAI,CAAC,IAAI,MAAM,GAAG,QAAQ,CAAC,EAAE,CAAC;YACzC,MAAM,IAAI,UAAU,CACnB,4CAA4C,KAAK,sCAAsC,aAAa,CAAC,MAAM,CAAC,EAAE,CAC9G,CAAC;QACH,CAAC;QACD,OAAO,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC;QACxB,KAAK,IAAI,MAAM,CAAC;QAChB,IAAI,MAAM,GAAG,CAAC,EAAE,CAAC;YAChB,YAAY,GAAG,KAAK,CAAC;QACtB,CAAC;IACF,CAAC;IACD,IAAI,KAAK,KAAK,QAAQ,EAAE,CAAC;QACxB,MAAM,IAAI,UAAU,CAAC,iDAAiD,CAAC,CAAC;IACzE,CAAC;IACD,IAAI,YAAY,GAAG,CAAC,EAAE,CAAC;QACtB,OAAO,CAAC,CAAC,CAAC;IACX,CAAC;IAED,MAAM,MAAM,GAAG,MAAM,EAAE,GAAG,KAAK,CAAC;IAChC,IAAI,UAAU,GAAG,CAAC,CAAC;IACnB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,YAAY,EAAE,KAAK,EAAE,EAAE,CAAC;QACnD,UAAU,IAAI,OAAO,CAAC,KAAK,CAAE,CAAC;QAC9B,kEAAkE;QAClE,IAAI,UAAU,GAAG,MAAM,EAAE,CAAC;YACzB,OAAO,KAAK,CAAC;QACd,CAAC;IACF,CAAC;IACD,+DAA+D;IAC/D,OAAO,YAAY,CAAC;AACrB,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { RandomFn } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Picks one element of `items` with probability proportional to its weight: a
|
|
4
|
+
* loot table, a weighted A/B split, a sampled next token.
|
|
5
|
+
*
|
|
6
|
+
* One draw of {@link weightedChoiceIndex}, whose contract it shares. For many
|
|
7
|
+
* draws from one distribution, a precomputed alias table (Vose) answers each in
|
|
8
|
+
* O(1) instead.
|
|
9
|
+
*
|
|
10
|
+
* Time complexity: O(n), one call to `random`. Memory: O(n).
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* weightedChoice(prizes, (prize) => prize.odds, Math.random);
|
|
15
|
+
* ```;
|
|
16
|
+
*
|
|
17
|
+
* @param items - The candidates.
|
|
18
|
+
* @param weightOf - The weight of an item, a finite number `≥ 0`; an item of
|
|
19
|
+
* weight `0` is never picked.
|
|
20
|
+
* @param random - A source in `[0, 1)`; not called when no weight is positive.
|
|
21
|
+
* @returns An element, or `undefined` when `items` is empty or every weight is
|
|
22
|
+
* `0` (so an array that holds `undefined` cannot tell the two apart: use
|
|
23
|
+
* {@link weightedChoiceIndex}).
|
|
24
|
+
* @throws {RangeError} When a weight is negative, `NaN` or infinite, or the
|
|
25
|
+
* total weight overflows to `Infinity`.
|
|
26
|
+
*/
|
|
27
|
+
export declare function weightedChoice<T>(items: readonly T[], weightOf: (item: T, index: number) => number, random: RandomFn): T | undefined;
|
|
28
|
+
//# sourceMappingURL=weighted-choice.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"weighted-choice.d.ts","sourceRoot":"","sources":["../../src/sample/weighted-choice.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAI7C;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAC/B,KAAK,EAAE,SAAS,CAAC,EAAE,EACnB,QAAQ,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,EAC5C,MAAM,EAAE,QAAQ,GACd,CAAC,GAAG,SAAS,CAGf"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { weightedChoiceIndex } from "./weighted-choice-index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Picks one element of `items` with probability proportional to its weight: a
|
|
4
|
+
* loot table, a weighted A/B split, a sampled next token.
|
|
5
|
+
*
|
|
6
|
+
* One draw of {@link weightedChoiceIndex}, whose contract it shares. For many
|
|
7
|
+
* draws from one distribution, a precomputed alias table (Vose) answers each in
|
|
8
|
+
* O(1) instead.
|
|
9
|
+
*
|
|
10
|
+
* Time complexity: O(n), one call to `random`. Memory: O(n).
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* weightedChoice(prizes, (prize) => prize.odds, Math.random);
|
|
15
|
+
* ```;
|
|
16
|
+
*
|
|
17
|
+
* @param items - The candidates.
|
|
18
|
+
* @param weightOf - The weight of an item, a finite number `≥ 0`; an item of
|
|
19
|
+
* weight `0` is never picked.
|
|
20
|
+
* @param random - A source in `[0, 1)`; not called when no weight is positive.
|
|
21
|
+
* @returns An element, or `undefined` when `items` is empty or every weight is
|
|
22
|
+
* `0` (so an array that holds `undefined` cannot tell the two apart: use
|
|
23
|
+
* {@link weightedChoiceIndex}).
|
|
24
|
+
* @throws {RangeError} When a weight is negative, `NaN` or infinite, or the
|
|
25
|
+
* total weight overflows to `Infinity`.
|
|
26
|
+
*/
|
|
27
|
+
export function weightedChoice(items, weightOf, random) {
|
|
28
|
+
const index = weightedChoiceIndex(items, weightOf, random);
|
|
29
|
+
return index < 0 ? undefined : items[index];
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=weighted-choice.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"weighted-choice.js","sourceRoot":"","sources":["../../src/sample/weighted-choice.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAEjE;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,cAAc,CAC7B,KAAmB,EACnB,QAA4C,EAC5C,MAAgB;IAEhB,MAAM,KAAK,GAAG,mBAAmB,CAAC,KAAK,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;IAC3D,OAAO,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AAC7C,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Callable, Comparator } from "@ac-kit/core";
|
|
2
|
+
/** The smallest and the largest item of a collection. */
|
|
3
|
+
export type MinMaxItems<T> = {
|
|
4
|
+
min: T;
|
|
5
|
+
max: T;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* The smallest and the largest item under `compare`, in one pass — the
|
|
9
|
+
* comparator form of {@link minmaxBy}, named after `@ac-kit/core`'s `minmax`
|
|
10
|
+
* over numbers (d3's `extent`).
|
|
11
|
+
*
|
|
12
|
+
* Takes the items in pairs, ordering each pair first and then testing only its
|
|
13
|
+
* smaller item against the minimum and its larger against the maximum: three
|
|
14
|
+
* comparisons per two items rather than the four of two separate scans. Ties
|
|
15
|
+
* resolve to the first item seen, for the minimum and the maximum alike.
|
|
16
|
+
*
|
|
17
|
+
* Time complexity: O(n), about `3n / 2` calls to `compare`. Memory: O(1).
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* minmax(["pear", "fig", "apple"], compareNaturalAscending);
|
|
22
|
+
* // { min: "apple", max: "pear" }
|
|
23
|
+
* ```;
|
|
24
|
+
*
|
|
25
|
+
* @param items - The items, read once.
|
|
26
|
+
* @param compare - A strict weak ordering of the items.
|
|
27
|
+
* @returns The minimum and maximum, or `null` for an empty input.
|
|
28
|
+
*/
|
|
29
|
+
export declare function minmax<T>(items: Iterable<T>, compare: Comparator<T>): MinMaxItems<T> | null;
|
|
30
|
+
/**
|
|
31
|
+
* The items with the smallest and the largest `valueOf(item)`, in one pass —
|
|
32
|
+
* the accessor form of {@link minmax}, as `minBy` and `maxBy` are of their
|
|
33
|
+
* comparator forms.
|
|
34
|
+
*
|
|
35
|
+
* `valueOf` is called once per item. An item whose value is `NaN` has no place
|
|
36
|
+
* in the order and is skipped, so it is never returned. Ties resolve to the
|
|
37
|
+
* first item seen; `-0` and `0` tie.
|
|
38
|
+
*
|
|
39
|
+
* Time complexity: O(n). Memory: O(1).
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* minmaxBy(orders, (order) => order.total); // cheapest and dearest order
|
|
44
|
+
* ```;
|
|
45
|
+
*
|
|
46
|
+
* @param items - The items, read once.
|
|
47
|
+
* @param valueOf - The numeric key of an item.
|
|
48
|
+
* @returns The items with the minimum and maximum key, or `null` when no item
|
|
49
|
+
* has a key other than `NaN`.
|
|
50
|
+
*/
|
|
51
|
+
export declare function minmaxBy<T>(items: Iterable<T>, valueOf: Callable<[T], number>): MinMaxItems<T> | null;
|
|
52
|
+
//# sourceMappingURL=minmax.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"minmax.d.ts","sourceRoot":"","sources":["../../src/select/minmax.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAEzD,yDAAyD;AACzD,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI;IAC5B,GAAG,EAAE,CAAC,CAAC;IACP,GAAG,EAAE,CAAC,CAAC;CACP,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,MAAM,CAAC,CAAC,EACvB,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,EAClB,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,GACpB,WAAW,CAAC,CAAC,CAAC,GAAG,IAAI,CAiCvB;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EACzB,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,EAClB,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,GAC5B,WAAW,CAAC,CAAC,CAAC,GAAG,IAAI,CA4BvB"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The smallest and the largest item under `compare`, in one pass — the
|
|
3
|
+
* comparator form of {@link minmaxBy}, named after `@ac-kit/core`'s `minmax`
|
|
4
|
+
* over numbers (d3's `extent`).
|
|
5
|
+
*
|
|
6
|
+
* Takes the items in pairs, ordering each pair first and then testing only its
|
|
7
|
+
* smaller item against the minimum and its larger against the maximum: three
|
|
8
|
+
* comparisons per two items rather than the four of two separate scans. Ties
|
|
9
|
+
* resolve to the first item seen, for the minimum and the maximum alike.
|
|
10
|
+
*
|
|
11
|
+
* Time complexity: O(n), about `3n / 2` calls to `compare`. Memory: O(1).
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* minmax(["pear", "fig", "apple"], compareNaturalAscending);
|
|
16
|
+
* // { min: "apple", max: "pear" }
|
|
17
|
+
* ```;
|
|
18
|
+
*
|
|
19
|
+
* @param items - The items, read once.
|
|
20
|
+
* @param compare - A strict weak ordering of the items.
|
|
21
|
+
* @returns The minimum and maximum, or `null` for an empty input.
|
|
22
|
+
*/
|
|
23
|
+
export function minmax(items, compare) {
|
|
24
|
+
const iterator = items[Symbol.iterator]();
|
|
25
|
+
const first = iterator.next();
|
|
26
|
+
if (first.done === true) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
let min = first.value;
|
|
30
|
+
let max = first.value;
|
|
31
|
+
for (;;) {
|
|
32
|
+
const one = iterator.next();
|
|
33
|
+
if (one.done === true) {
|
|
34
|
+
return { min, max };
|
|
35
|
+
}
|
|
36
|
+
const other = iterator.next();
|
|
37
|
+
if (other.done === true) {
|
|
38
|
+
if (compare(one.value, min) < 0) {
|
|
39
|
+
min = one.value;
|
|
40
|
+
}
|
|
41
|
+
else if (compare(one.value, max) > 0) {
|
|
42
|
+
max = one.value;
|
|
43
|
+
}
|
|
44
|
+
return { min, max };
|
|
45
|
+
}
|
|
46
|
+
const order = compare(one.value, other.value);
|
|
47
|
+
// On a tie both candidates are the earlier item, `one`.
|
|
48
|
+
const smaller = order > 0 ? other.value : one.value;
|
|
49
|
+
const larger = order < 0 ? other.value : one.value;
|
|
50
|
+
if (compare(smaller, min) < 0) {
|
|
51
|
+
min = smaller;
|
|
52
|
+
}
|
|
53
|
+
if (compare(larger, max) > 0) {
|
|
54
|
+
max = larger;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The items with the smallest and the largest `valueOf(item)`, in one pass —
|
|
60
|
+
* the accessor form of {@link minmax}, as `minBy` and `maxBy` are of their
|
|
61
|
+
* comparator forms.
|
|
62
|
+
*
|
|
63
|
+
* `valueOf` is called once per item. An item whose value is `NaN` has no place
|
|
64
|
+
* in the order and is skipped, so it is never returned. Ties resolve to the
|
|
65
|
+
* first item seen; `-0` and `0` tie.
|
|
66
|
+
*
|
|
67
|
+
* Time complexity: O(n). Memory: O(1).
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* minmaxBy(orders, (order) => order.total); // cheapest and dearest order
|
|
72
|
+
* ```;
|
|
73
|
+
*
|
|
74
|
+
* @param items - The items, read once.
|
|
75
|
+
* @param valueOf - The numeric key of an item.
|
|
76
|
+
* @returns The items with the minimum and maximum key, or `null` when no item
|
|
77
|
+
* has a key other than `NaN`.
|
|
78
|
+
*/
|
|
79
|
+
export function minmaxBy(items, valueOf) {
|
|
80
|
+
let min;
|
|
81
|
+
let max;
|
|
82
|
+
let minValue = Infinity;
|
|
83
|
+
let maxValue = -Infinity;
|
|
84
|
+
let found = false;
|
|
85
|
+
for (const item of items) {
|
|
86
|
+
const value = valueOf(item);
|
|
87
|
+
if (value !== value) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (!found) {
|
|
91
|
+
min = item;
|
|
92
|
+
max = item;
|
|
93
|
+
minValue = value;
|
|
94
|
+
maxValue = value;
|
|
95
|
+
found = true;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (value < minValue) {
|
|
99
|
+
min = item;
|
|
100
|
+
minValue = value;
|
|
101
|
+
}
|
|
102
|
+
else if (value > maxValue) {
|
|
103
|
+
max = item;
|
|
104
|
+
maxValue = value;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return found ? { min: min, max: max } : null;
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=minmax.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"minmax.js","sourceRoot":"","sources":["../../src/select/minmax.ts"],"names":[],"mappings":"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"}
|
|
@@ -11,5 +11,5 @@ import type { Comparator } from "@ac-kit/core";
|
|
|
11
11
|
* @param compare Total order to select by.
|
|
12
12
|
* @returns The value that belongs at index `k`.
|
|
13
13
|
*/
|
|
14
|
-
export declare function
|
|
15
|
-
//# sourceMappingURL=quickselect.d.ts.map
|
|
14
|
+
export declare function quickselectInPlace<T>(items: T[], k: number, compare: Comparator<T>): T;
|
|
15
|
+
//# sourceMappingURL=quickselect-in-place.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quickselect-in-place.d.ts","sourceRoot":"","sources":["../../src/select/quickselect-in-place.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EACnC,KAAK,EAAE,CAAC,EAAE,EACV,CAAC,EAAE,MAAM,EACT,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,GACpB,CAAC,CAmCH"}
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* @param compare Total order to select by.
|
|
11
11
|
* @returns The value that belongs at index `k`.
|
|
12
12
|
*/
|
|
13
|
-
export function
|
|
13
|
+
export function quickselectInPlace(items, k, compare) {
|
|
14
14
|
let left = 0;
|
|
15
15
|
let right = items.length - 1;
|
|
16
16
|
while (left < right) {
|
|
@@ -44,4 +44,4 @@ export function quickselect(items, k, compare) {
|
|
|
44
44
|
}
|
|
45
45
|
return items[k];
|
|
46
46
|
}
|
|
47
|
-
//# sourceMappingURL=quickselect.js.map
|
|
47
|
+
//# sourceMappingURL=quickselect-in-place.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quickselect-in-place.js","sourceRoot":"","sources":["../../src/select/quickselect-in-place.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,kBAAkB,CACjC,KAAU,EACV,CAAS,EACT,OAAsB;IAEtB,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,IAAI,KAAK,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;IAE7B,OAAO,IAAI,GAAG,KAAK,EAAE,CAAC;QACrB,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAE,CAAC;QAC5D,IAAI,KAAK,GAAG,IAAI,CAAC;QACjB,IAAI,KAAK,GAAG,KAAK,CAAC;QAElB,OAAO,KAAK,IAAI,KAAK,EAAE,CAAC;YACvB,OAAO,OAAO,CAAC,KAAK,CAAC,KAAK,CAAE,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC1C,KAAK,EAAE,CAAC;YACT,CAAC;YACD,OAAO,OAAO,CAAC,KAAK,CAAC,KAAK,CAAE,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC1C,KAAK,EAAE,CAAC;YACT,CAAC;YACD,IAAI,KAAK,IAAI,KAAK,EAAE,CAAC;gBACpB,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAE,CAAC;gBAC3B,KAAK,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,KAAK,CAAE,CAAC;gBAC7B,KAAK,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;gBACpB,KAAK,EAAE,CAAC;gBACR,KAAK,EAAE,CAAC;YACT,CAAC;QACF,CAAC;QAED,IAAI,CAAC,IAAI,KAAK,EAAE,CAAC;YAChB,KAAK,GAAG,KAAK,CAAC;QACf,CAAC;aAAM,IAAI,CAAC,IAAI,KAAK,EAAE,CAAC;YACvB,IAAI,GAAG,KAAK,CAAC;QACd,CAAC;aAAM,CAAC;YACP,MAAM;QACP,CAAC;IACF,CAAC;IAED,OAAO,KAAK,CAAC,CAAC,CAAE,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { EqualityComparator } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* The runs `a[aStart … aStart + length)` equal to `b[bStart … bStart + length)`
|
|
4
|
+
* of a shortest edit script turning `a` into `b`, as flat `[aStart, bStart,
|
|
5
|
+
* length, …]` triples in order and merged where contiguous; their items form a
|
|
6
|
+
* longest common subsequence.
|
|
7
|
+
*
|
|
8
|
+
* Myers' O(ND) algorithm in its linear-space form (Myers, 1986, §4b): the
|
|
9
|
+
* forward and backward searches meet on a "middle snake", which splits the
|
|
10
|
+
* problem in two; each part is trimmed of its common prefix and suffix and
|
|
11
|
+
* split again. The parts wait on an explicit stack, so there is no recursion,
|
|
12
|
+
* and the two diagonal arrays are allocated once for the largest part. O((n +
|
|
13
|
+
* m) · D) time for `D` the edit distance, O(n + m) memory.
|
|
14
|
+
*/
|
|
15
|
+
export declare function matchingRuns_<T>(a: ArrayLike<T>, b: ArrayLike<T>, equals: EqualityComparator<T, T>): number[];
|
|
16
|
+
//# sourceMappingURL=_matching-runs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_matching-runs.d.ts","sourceRoot":"","sources":["../../src/sequence/_matching-runs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAEvD;;;;;;;;;;;;GAYG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAC9B,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC,EACf,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC,EACf,MAAM,EAAE,kBAAkB,CAAC,CAAC,EAAE,CAAC,CAAC,GAC9B,MAAM,EAAE,CA6EV"}
|