@ac-kit/algo 0.5.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 +128 -0
- package/README.md +60 -18
- 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/find-cycle.d.ts +5 -3
- package/dist/graph/find-cycle.d.ts.map +1 -1
- package/dist/graph/find-cycle.js +23 -3
- package/dist/graph/find-cycle.js.map +1 -1
- 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/hierarchy-from-parents.js.map +1 -1
- 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 +39 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +39 -1
- package/dist/index.js.map +1 -1
- package/dist/ordered/_fractional-key.d.ts +38 -0
- package/dist/ordered/_fractional-key.d.ts.map +1 -0
- package/dist/ordered/_fractional-key.js +251 -0
- package/dist/ordered/_fractional-key.js.map +1 -0
- package/dist/ordered/bisect-right-numbers-array.d.ts +20 -0
- package/dist/ordered/bisect-right-numbers-array.d.ts.map +1 -0
- package/dist/ordered/bisect-right-numbers-array.js +42 -0
- package/dist/ordered/bisect-right-numbers-array.js.map +1 -0
- package/dist/ordered/bisect-right-numbers.d.ts +19 -0
- package/dist/ordered/bisect-right-numbers.d.ts.map +1 -0
- package/dist/ordered/bisect-right-numbers.js +32 -0
- package/dist/ordered/bisect-right-numbers.js.map +1 -0
- package/dist/ordered/create-bisector.js.map +1 -1
- package/dist/ordered/fractional-key-alphabet.d.ts +41 -0
- package/dist/ordered/fractional-key-alphabet.d.ts.map +1 -0
- package/dist/ordered/fractional-key-alphabet.js +67 -0
- package/dist/ordered/fractional-key-alphabet.js.map +1 -0
- package/dist/ordered/fractional-key-error.d.ts +15 -0
- package/dist/ordered/fractional-key-error.d.ts.map +1 -0
- package/dist/ordered/fractional-key-error.js +20 -0
- package/dist/ordered/fractional-key-error.js.map +1 -0
- package/dist/ordered/fractional-key.d.ts +82 -0
- package/dist/ordered/fractional-key.d.ts.map +1 -0
- package/dist/ordered/fractional-key.js +199 -0
- package/dist/ordered/fractional-key.js.map +1 -0
- 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} +3 -3
- 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/radix-index-sorter.d.ts +57 -0
- package/dist/sort/radix-index-sorter.d.ts.map +1 -0
- package/dist/sort/radix-index-sorter.js +204 -0
- package/dist/sort/radix-index-sorter.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 +48 -0
- package/dist/sort/sort-range.d.ts.map +1 -0
- package/dist/sort/sort-range.js +267 -0
- package/dist/sort/sort-range.js.map +1 -0
- package/package.json +6 -5
- package/dist/select/quickselect.d.ts.map +0 -1
- package/dist/select/quickselect.js.map +0 -1
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Callable, Comparator, EqualityComparisonStrategy } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* The distinct keys of an iterable, ordered by a value computed from each key's
|
|
4
|
+
* group — d3's `groupSort`: the categories of a bar chart by their median, the
|
|
5
|
+
* owners by how many open tasks they hold.
|
|
6
|
+
*
|
|
7
|
+
* Groups the items by `keyOf`, reduces each group once with `valueOf`, and
|
|
8
|
+
* sorts the keys by those values, so `valueOf` runs once per group rather than
|
|
9
|
+
* once per comparison. Stable: keys whose values are equivalent stay in the
|
|
10
|
+
* order they first occur. To order by the groups themselves, return the group
|
|
11
|
+
* from `valueOf` and compare groups.
|
|
12
|
+
*
|
|
13
|
+
* Time complexity: O(n + g log g) for `g` distinct keys under the default
|
|
14
|
+
* `"sameValueZero"`, plus the reductions. Memory: O(n) for the groups.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* groupSort(
|
|
19
|
+
* tasks,
|
|
20
|
+
* (task) => task.owner,
|
|
21
|
+
* (group) => group.length,
|
|
22
|
+
* compareNaturalDescending,
|
|
23
|
+
* ); // the busiest owner first
|
|
24
|
+
* ```;
|
|
25
|
+
*
|
|
26
|
+
* @param iterable - The input iterable.
|
|
27
|
+
* @param keyOf - The key of an item.
|
|
28
|
+
* @param valueOf - Reduces one group, its items in input order, to the value
|
|
29
|
+
* its key sorts by.
|
|
30
|
+
* @param compare - A strict weak ordering of the reduced values.
|
|
31
|
+
* @param comparisonStrategy - How keys are compared. Only `"sameValueZero"` is
|
|
32
|
+
* supported, since the groups are kept in a `Map`.
|
|
33
|
+
* @returns Every distinct key once, in sorted order.
|
|
34
|
+
* @throws {UnimplementedError} If `comparisonStrategy` is not
|
|
35
|
+
* `"sameValueZero"`.
|
|
36
|
+
*/
|
|
37
|
+
export declare function groupSort<T, TKey, TValue>(iterable: Iterable<T>, keyOf: Callable<[T], TKey>, valueOf: Callable<[group: T[]], TValue>, compare: Comparator<TValue>, comparisonStrategy?: EqualityComparisonStrategy<TKey, TKey>): TKey[];
|
|
38
|
+
//# sourceMappingURL=group-sort.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"group-sort.d.ts","sourceRoot":"","sources":["../../src/group/group-sort.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,QAAQ,EACR,UAAU,EACV,0BAA0B,EAC1B,MAAM,cAAc,CAAC;AAKtB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EACxC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrB,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,EAC1B,OAAO,EAAE,QAAQ,CAAC,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC,EACvC,OAAO,EAAE,UAAU,CAAC,MAAM,CAAC,EAC3B,kBAAkB,GAAE,0BAA0B,CAAC,IAAI,EAAE,IAAI,CAAmB,GAC1E,IAAI,EAAE,CAaR"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { sortedIndices } from "../sort/sorted-indices.js";
|
|
2
|
+
import { assertSameValueZero_, groupInto_, nestBy_ } from "./_nest-by.js";
|
|
3
|
+
/**
|
|
4
|
+
* The distinct keys of an iterable, ordered by a value computed from each key's
|
|
5
|
+
* group — d3's `groupSort`: the categories of a bar chart by their median, the
|
|
6
|
+
* owners by how many open tasks they hold.
|
|
7
|
+
*
|
|
8
|
+
* Groups the items by `keyOf`, reduces each group once with `valueOf`, and
|
|
9
|
+
* sorts the keys by those values, so `valueOf` runs once per group rather than
|
|
10
|
+
* once per comparison. Stable: keys whose values are equivalent stay in the
|
|
11
|
+
* order they first occur. To order by the groups themselves, return the group
|
|
12
|
+
* from `valueOf` and compare groups.
|
|
13
|
+
*
|
|
14
|
+
* Time complexity: O(n + g log g) for `g` distinct keys under the default
|
|
15
|
+
* `"sameValueZero"`, plus the reductions. Memory: O(n) for the groups.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* groupSort(
|
|
20
|
+
* tasks,
|
|
21
|
+
* (task) => task.owner,
|
|
22
|
+
* (group) => group.length,
|
|
23
|
+
* compareNaturalDescending,
|
|
24
|
+
* ); // the busiest owner first
|
|
25
|
+
* ```;
|
|
26
|
+
*
|
|
27
|
+
* @param iterable - The input iterable.
|
|
28
|
+
* @param keyOf - The key of an item.
|
|
29
|
+
* @param valueOf - Reduces one group, its items in input order, to the value
|
|
30
|
+
* its key sorts by.
|
|
31
|
+
* @param compare - A strict weak ordering of the reduced values.
|
|
32
|
+
* @param comparisonStrategy - How keys are compared. Only `"sameValueZero"` is
|
|
33
|
+
* supported, since the groups are kept in a `Map`.
|
|
34
|
+
* @returns Every distinct key once, in sorted order.
|
|
35
|
+
* @throws {UnimplementedError} If `comparisonStrategy` is not
|
|
36
|
+
* `"sameValueZero"`.
|
|
37
|
+
*/
|
|
38
|
+
export function groupSort(iterable, keyOf, valueOf, compare, comparisonStrategy = "sameValueZero") {
|
|
39
|
+
assertSameValueZero_(comparisonStrategy);
|
|
40
|
+
const groups = nestBy_(iterable, [keyOf], groupInto_);
|
|
41
|
+
const keys = [];
|
|
42
|
+
const values = [];
|
|
43
|
+
for (const [key, group] of groups) {
|
|
44
|
+
keys.push(key);
|
|
45
|
+
values.push(valueOf(group));
|
|
46
|
+
}
|
|
47
|
+
return sortedIndices(values, compare).map((index) => keys[index]);
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=group-sort.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"group-sort.js","sourceRoot":"","sources":["../../src/group/group-sort.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,EAAE,oBAAoB,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAE1E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,UAAU,SAAS,CACxB,QAAqB,EACrB,KAA0B,EAC1B,OAAuC,EACvC,OAA2B,EAC3B,kBAAkB,GAA2C,eAAe;IAE5E,oBAAoB,CAAC,kBAAkB,CAAC,CAAC;IACzC,MAAM,MAAM,GAAG,OAAO,CAAS,QAAQ,EAAE,CAAC,KAAK,CAAC,EAAE,UAAU,CAG3D,CAAC;IACF,MAAM,IAAI,GAAW,EAAE,CAAC;IACxB,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,EAAE,CAAC;QACnC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACf,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;IAC7B,CAAC;IACD,OAAO,aAAa,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAE,CAAC,CAAC;AACpE,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { EqualityComparisonStrategy } from "@ac-kit/core";
|
|
2
|
+
import type { KeyAccessors, NestedMap } from "./nested-map.js";
|
|
3
|
+
/**
|
|
4
|
+
* Indexes the items of an iterable by their keys, one nested `Map` level per
|
|
5
|
+
* accessor, with the single item holding each key tuple at the innermost level
|
|
6
|
+
* — `groupBy` for keys that are unique, such as ids, which throws instead of
|
|
7
|
+
* silently keeping one of two items that share a key (d3's `index`).
|
|
8
|
+
*
|
|
9
|
+
* Keys appear in the order they first occur.
|
|
10
|
+
*
|
|
11
|
+
* Time complexity: O(n · k) for `k` accessors under the default
|
|
12
|
+
* `"sameValueZero"`. Memory: O(n).
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const byId = indexBy(users, [(user) => user.id]);
|
|
17
|
+
* byId.get(42); // the user with id 42
|
|
18
|
+
* ```;
|
|
19
|
+
*
|
|
20
|
+
* @param iterable - The input iterable.
|
|
21
|
+
* @param keysOf - One or more functions taking an item and returning a key,
|
|
22
|
+
* outermost level first; an array, so a trailing `comparisonStrategy` is not
|
|
23
|
+
* mistaken for one more accessor.
|
|
24
|
+
* @param comparisonStrategy - How keys are compared. Only `"sameValueZero"` is
|
|
25
|
+
* supported, since the index is kept in a `Map`.
|
|
26
|
+
* @returns Nested maps from each key to its item.
|
|
27
|
+
* @throws {RangeError} When two items have the same keys; the message names the
|
|
28
|
+
* position of the second.
|
|
29
|
+
* @throws {UnimplementedError} If `comparisonStrategy` is not
|
|
30
|
+
* `"sameValueZero"`.
|
|
31
|
+
*/
|
|
32
|
+
export declare function indexBy<T, const Keys extends readonly [unknown, ...unknown[]]>(iterable: Iterable<T>, keysOf: KeyAccessors<T, Keys>, comparisonStrategy?: EqualityComparisonStrategy<Keys[number], Keys[number]>): NestedMap<Keys, T>;
|
|
33
|
+
//# sourceMappingURL=index-by.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-by.d.ts","sourceRoot":"","sources":["../../src/group/index-by.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAG/D,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,KAAK,CAAC,IAAI,SAAS,SAAS,CAAC,OAAO,EAAE,GAAG,OAAO,EAAE,CAAC,EAC7E,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrB,MAAM,EAAE,YAAY,CAAC,CAAC,EAAE,IAAI,CAAC,EAC7B,kBAAkB,GAAE,0BAA0B,CAC7C,IAAI,CAAC,MAAM,CAAC,EACZ,IAAI,CAAC,MAAM,CAAC,CACM,GACjB,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC,CAYpB"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { assertSameValueZero_, nestBy_ } from "./_nest-by.js";
|
|
2
|
+
/**
|
|
3
|
+
* Indexes the items of an iterable by their keys, one nested `Map` level per
|
|
4
|
+
* accessor, with the single item holding each key tuple at the innermost level
|
|
5
|
+
* — `groupBy` for keys that are unique, such as ids, which throws instead of
|
|
6
|
+
* silently keeping one of two items that share a key (d3's `index`).
|
|
7
|
+
*
|
|
8
|
+
* Keys appear in the order they first occur.
|
|
9
|
+
*
|
|
10
|
+
* Time complexity: O(n · k) for `k` accessors under the default
|
|
11
|
+
* `"sameValueZero"`. Memory: O(n).
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const byId = indexBy(users, [(user) => user.id]);
|
|
16
|
+
* byId.get(42); // the user with id 42
|
|
17
|
+
* ```;
|
|
18
|
+
*
|
|
19
|
+
* @param iterable - The input iterable.
|
|
20
|
+
* @param keysOf - One or more functions taking an item and returning a key,
|
|
21
|
+
* outermost level first; an array, so a trailing `comparisonStrategy` is not
|
|
22
|
+
* mistaken for one more accessor.
|
|
23
|
+
* @param comparisonStrategy - How keys are compared. Only `"sameValueZero"` is
|
|
24
|
+
* supported, since the index is kept in a `Map`.
|
|
25
|
+
* @returns Nested maps from each key to its item.
|
|
26
|
+
* @throws {RangeError} When two items have the same keys; the message names the
|
|
27
|
+
* position of the second.
|
|
28
|
+
* @throws {UnimplementedError} If `comparisonStrategy` is not
|
|
29
|
+
* `"sameValueZero"`.
|
|
30
|
+
*/
|
|
31
|
+
export function indexBy(iterable, keysOf, comparisonStrategy = "sameValueZero") {
|
|
32
|
+
assertSameValueZero_(comparisonStrategy);
|
|
33
|
+
let position = 0;
|
|
34
|
+
return nestBy_(iterable, keysOf, (items, key, item) => {
|
|
35
|
+
if (items.has(key)) {
|
|
36
|
+
throw new RangeError(`indexBy: the item at position ${position} has the keys of an earlier one`);
|
|
37
|
+
}
|
|
38
|
+
items.set(key, item);
|
|
39
|
+
position++;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=index-by.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-by.js","sourceRoot":"","sources":["../../src/group/index-by.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAG9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,UAAU,OAAO,CACtB,QAAqB,EACrB,MAA6B,EAC7B,kBAAkB,GAGd,eAAe;IAEnB,oBAAoB,CAAC,kBAAkB,CAAC,CAAC;IACzC,IAAI,QAAQ,GAAG,CAAC,CAAC;IACjB,OAAO,OAAO,CAAO,QAAQ,EAAE,MAAM,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QAC3D,IAAI,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACpB,MAAM,IAAI,UAAU,CACnB,iCAAiC,QAAQ,iCAAiC,CAC1E,CAAC;QACH,CAAC;QACD,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;QACrB,QAAQ,EAAE,CAAC;IACZ,CAAC,CAAuB,CAAC;AAC1B,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Callable } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Nested `Map`s, one level per key in `Keys` (outermost first), with `TLeaf`
|
|
4
|
+
* values at the innermost level: what `countBy`, `rollupBy` and `indexBy`
|
|
5
|
+
* return, as `groupBy` returns `GroupedBy`.
|
|
6
|
+
*/
|
|
7
|
+
export type NestedMap<Keys extends readonly unknown[], TLeaf> = Keys extends readonly [infer Head, ...infer Tail] ? Tail extends readonly [] ? Map<Head, TLeaf> : Map<Head, NestedMap<Tail, TLeaf>> : never;
|
|
8
|
+
/** One key accessor per nesting level, typed by the tuple of keys returned. */
|
|
9
|
+
export type KeyAccessors<T, Keys extends readonly unknown[]> = {
|
|
10
|
+
[I in keyof Keys]: Callable<[T], Keys[I]>;
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=nested-map.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nested-map.d.ts","sourceRoot":"","sources":["../../src/group/nested-map.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;GAIG;AACH,MAAM,MAAM,SAAS,CACpB,IAAI,SAAS,SAAS,OAAO,EAAE,EAC/B,KAAK,IACF,IAAI,SAAS,SAAS,CAAC,MAAM,IAAI,EAAE,GAAG,MAAM,IAAI,CAAC,GAClD,IAAI,SAAS,SAAS,EAAE,GACvB,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,GAChB,GAAG,CAAC,IAAI,EAAE,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,GAClC,KAAK,CAAC;AAET,+EAA+E;AAC/E,MAAM,MAAM,YAAY,CAAC,CAAC,EAAE,IAAI,SAAS,SAAS,OAAO,EAAE,IAAI;KAC7D,CAAC,IAAI,MAAM,IAAI,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;CACzC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nested-map.js","sourceRoot":"","sources":["../../src/group/nested-map.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { Callable, EqualityComparisonStrategy } from "@ac-kit/core";
|
|
2
|
+
import type { KeyAccessors, NestedMap } from "./nested-map.js";
|
|
3
|
+
/**
|
|
4
|
+
* Groups the items of an iterable by their keys, one nested `Map` level per
|
|
5
|
+
* accessor, and reduces each group to one value — SQL's `GROUP BY` with an
|
|
6
|
+
* aggregate, d3's `rollup`. `groupBy` is `rollupBy` with the identity
|
|
7
|
+
* reduction.
|
|
8
|
+
*
|
|
9
|
+
* Keys appear in the order they first occur; `reduce` is called once per group,
|
|
10
|
+
* with its items in input order, once every item has been read.
|
|
11
|
+
*
|
|
12
|
+
* Time complexity: O(n · k) for `k` accessors under the default
|
|
13
|
+
* `"sameValueZero"`, plus the reductions. Memory: O(n) for the groups.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* rollupBy(sales, [(sale) => sale.region], (group) => group.length);
|
|
18
|
+
* // Map { "north" → 12, "south" → 7 }
|
|
19
|
+
* ```;
|
|
20
|
+
*
|
|
21
|
+
* @param iterable - The input iterable.
|
|
22
|
+
* @param keysOf - One or more functions taking an item and returning a key,
|
|
23
|
+
* outermost level first; an array, so a trailing `comparisonStrategy` is not
|
|
24
|
+
* mistaken for one more accessor.
|
|
25
|
+
* @param reduce - Reduces one group to its value.
|
|
26
|
+
* @param comparisonStrategy - How keys are compared. Only `"sameValueZero"` is
|
|
27
|
+
* supported, since the groups are kept in a `Map`.
|
|
28
|
+
* @returns Nested maps from each key to its group's reduced value.
|
|
29
|
+
* @throws {UnimplementedError} If `comparisonStrategy` is not
|
|
30
|
+
* `"sameValueZero"`.
|
|
31
|
+
*/
|
|
32
|
+
export declare function rollupBy<T, TResult, const Keys extends readonly [unknown, ...unknown[]]>(iterable: Iterable<T>, keysOf: KeyAccessors<T, Keys>, reduce: Callable<[group: T[]], TResult>, comparisonStrategy?: EqualityComparisonStrategy<Keys[number], Keys[number]>): NestedMap<Keys, TResult>;
|
|
33
|
+
/**
|
|
34
|
+
* {@link rollupBy} flattened into one row per key tuple, `[key₁, …, keyₖ,
|
|
35
|
+
* value]`, in the order the key tuples first occur — d3's `flatRollup`, the
|
|
36
|
+
* shape a table or a chart's data layer takes.
|
|
37
|
+
*
|
|
38
|
+
* Time complexity: O(n · k) for `k` accessors under the default
|
|
39
|
+
* `"sameValueZero"`, plus the reductions. Memory: O(n).
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* flatRollupBy(
|
|
44
|
+
* sales,
|
|
45
|
+
* [(sale) => sale.region, (sale) => sale.year],
|
|
46
|
+
* (group) => group.length,
|
|
47
|
+
* );
|
|
48
|
+
* // [["north", 2025, 5], ["north", 2026, 7], ["south", 2026, 7]]
|
|
49
|
+
* ```;
|
|
50
|
+
*
|
|
51
|
+
* @param iterable - The input iterable.
|
|
52
|
+
* @param keysOf - One or more functions taking an item and returning a key,
|
|
53
|
+
* outermost first.
|
|
54
|
+
* @param reduce - Reduces one group to its value.
|
|
55
|
+
* @param comparisonStrategy - How keys are compared. Only `"sameValueZero"` is
|
|
56
|
+
* supported.
|
|
57
|
+
* @returns One row per key tuple: its keys, then its group's reduced value.
|
|
58
|
+
* @throws {UnimplementedError} If `comparisonStrategy` is not
|
|
59
|
+
* `"sameValueZero"`.
|
|
60
|
+
*/
|
|
61
|
+
export declare function flatRollupBy<T, TResult, const Keys extends readonly [unknown, ...unknown[]]>(iterable: Iterable<T>, keysOf: KeyAccessors<T, Keys>, reduce: Callable<[group: T[]], TResult>, comparisonStrategy?: EqualityComparisonStrategy<Keys[number], Keys[number]>): [...Keys, TResult][];
|
|
62
|
+
//# sourceMappingURL=rollup-by.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rollup-by.d.ts","sourceRoot":"","sources":["../../src/group/rollup-by.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC;AASzE,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAY/D;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,QAAQ,CACvB,CAAC,EACD,OAAO,EACP,KAAK,CAAC,IAAI,SAAS,SAAS,CAAC,OAAO,EAAE,GAAG,OAAO,EAAE,CAAC,EAEnD,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrB,MAAM,EAAE,YAAY,CAAC,CAAC,EAAE,IAAI,CAAC,EAC7B,MAAM,EAAE,QAAQ,CAAC,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC,EAAE,OAAO,CAAC,EACvC,kBAAkB,GAAE,0BAA0B,CAC7C,IAAI,CAAC,MAAM,CAAC,EACZ,IAAI,CAAC,MAAM,CAAC,CACM,GACjB,SAAS,CAAC,IAAI,EAAE,OAAO,CAAC,CAG1B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,YAAY,CAC3B,CAAC,EACD,OAAO,EACP,KAAK,CAAC,IAAI,SAAS,SAAS,CAAC,OAAO,EAAE,GAAG,OAAO,EAAE,CAAC,EAEnD,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrB,MAAM,EAAE,YAAY,CAAC,CAAC,EAAE,IAAI,CAAC,EAC7B,MAAM,EAAE,QAAQ,CAAC,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC,EAAE,OAAO,CAAC,EACvC,kBAAkB,GAAE,0BAA0B,CAC7C,IAAI,CAAC,MAAM,CAAC,EACZ,IAAI,CAAC,MAAM,CAAC,CACM,GACjB,CAAC,GAAG,IAAI,EAAE,OAAO,CAAC,EAAE,CAMtB"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { assertSameValueZero_, flattenNested_, groupInto_, mapLeaves_, nestBy_, } from "./_nest-by.js";
|
|
2
|
+
function rollUp_(iterable, keysOf, reduce) {
|
|
3
|
+
const nested = nestBy_(iterable, keysOf, groupInto_);
|
|
4
|
+
mapLeaves_(nested, keysOf.length, reduce);
|
|
5
|
+
return nested;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Groups the items of an iterable by their keys, one nested `Map` level per
|
|
9
|
+
* accessor, and reduces each group to one value — SQL's `GROUP BY` with an
|
|
10
|
+
* aggregate, d3's `rollup`. `groupBy` is `rollupBy` with the identity
|
|
11
|
+
* reduction.
|
|
12
|
+
*
|
|
13
|
+
* Keys appear in the order they first occur; `reduce` is called once per group,
|
|
14
|
+
* with its items in input order, once every item has been read.
|
|
15
|
+
*
|
|
16
|
+
* Time complexity: O(n · k) for `k` accessors under the default
|
|
17
|
+
* `"sameValueZero"`, plus the reductions. Memory: O(n) for the groups.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* rollupBy(sales, [(sale) => sale.region], (group) => group.length);
|
|
22
|
+
* // Map { "north" → 12, "south" → 7 }
|
|
23
|
+
* ```;
|
|
24
|
+
*
|
|
25
|
+
* @param iterable - The input iterable.
|
|
26
|
+
* @param keysOf - One or more functions taking an item and returning a key,
|
|
27
|
+
* outermost level first; an array, so a trailing `comparisonStrategy` is not
|
|
28
|
+
* mistaken for one more accessor.
|
|
29
|
+
* @param reduce - Reduces one group to its value.
|
|
30
|
+
* @param comparisonStrategy - How keys are compared. Only `"sameValueZero"` is
|
|
31
|
+
* supported, since the groups are kept in a `Map`.
|
|
32
|
+
* @returns Nested maps from each key to its group's reduced value.
|
|
33
|
+
* @throws {UnimplementedError} If `comparisonStrategy` is not
|
|
34
|
+
* `"sameValueZero"`.
|
|
35
|
+
*/
|
|
36
|
+
export function rollupBy(iterable, keysOf, reduce, comparisonStrategy = "sameValueZero") {
|
|
37
|
+
assertSameValueZero_(comparisonStrategy);
|
|
38
|
+
return rollUp_(iterable, keysOf, reduce);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* {@link rollupBy} flattened into one row per key tuple, `[key₁, …, keyₖ,
|
|
42
|
+
* value]`, in the order the key tuples first occur — d3's `flatRollup`, the
|
|
43
|
+
* shape a table or a chart's data layer takes.
|
|
44
|
+
*
|
|
45
|
+
* Time complexity: O(n · k) for `k` accessors under the default
|
|
46
|
+
* `"sameValueZero"`, plus the reductions. Memory: O(n).
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* flatRollupBy(
|
|
51
|
+
* sales,
|
|
52
|
+
* [(sale) => sale.region, (sale) => sale.year],
|
|
53
|
+
* (group) => group.length,
|
|
54
|
+
* );
|
|
55
|
+
* // [["north", 2025, 5], ["north", 2026, 7], ["south", 2026, 7]]
|
|
56
|
+
* ```;
|
|
57
|
+
*
|
|
58
|
+
* @param iterable - The input iterable.
|
|
59
|
+
* @param keysOf - One or more functions taking an item and returning a key,
|
|
60
|
+
* outermost first.
|
|
61
|
+
* @param reduce - Reduces one group to its value.
|
|
62
|
+
* @param comparisonStrategy - How keys are compared. Only `"sameValueZero"` is
|
|
63
|
+
* supported.
|
|
64
|
+
* @returns One row per key tuple: its keys, then its group's reduced value.
|
|
65
|
+
* @throws {UnimplementedError} If `comparisonStrategy` is not
|
|
66
|
+
* `"sameValueZero"`.
|
|
67
|
+
*/
|
|
68
|
+
export function flatRollupBy(iterable, keysOf, reduce, comparisonStrategy = "sameValueZero") {
|
|
69
|
+
assertSameValueZero_(comparisonStrategy);
|
|
70
|
+
return flattenNested_(rollUp_(iterable, keysOf, reduce), keysOf.length);
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=rollup-by.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rollup-by.js","sourceRoot":"","sources":["../../src/group/rollup-by.ts"],"names":[],"mappings":"AAEA,OAAO,EACN,oBAAoB,EACpB,cAAc,EACd,UAAU,EACV,UAAU,EACV,OAAO,GACP,MAAM,eAAe,CAAC;AAGvB,SAAS,OAAO,CACf,QAAqB,EACrB,MAAyC,EACzC,MAAuC;IAEvC,MAAM,MAAM,GAAG,OAAO,CAAS,QAAQ,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC;IAC7D,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC1C,OAAO,MAAM,CAAC;AACf,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,UAAU,QAAQ,CAKvB,QAAqB,EACrB,MAA6B,EAC7B,MAAuC,EACvC,kBAAkB,GAGd,eAAe;IAEnB,oBAAoB,CAAC,kBAAkB,CAAC,CAAC;IACzC,OAAO,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,CAA6B,CAAC;AACtE,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,YAAY,CAK3B,QAAqB,EACrB,MAA6B,EAC7B,MAAuC,EACvC,kBAAkB,GAGd,eAAe;IAEnB,oBAAoB,CAAC,kBAAkB,CAAC,CAAC;IACzC,OAAO,cAAc,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,CAGnE,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type EqualityComparisonStrategy } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Collapses consecutive equal values into `[value, count]` runs, lazily —
|
|
4
|
+
* run-length encoding, the counting form of `groupAdjacentBy`: `a a a b a`
|
|
5
|
+
* gives `[a, 3], [b, 1], [a, 1]`. The inverse is {@link runLengthDecode}.
|
|
6
|
+
*
|
|
7
|
+
* Each run is yielded once the first value of the next has been read (or the
|
|
8
|
+
* input ends), with the first value of the run.
|
|
9
|
+
*
|
|
10
|
+
* Time complexity: O(n), one comparison per value after the first. Memory:
|
|
11
|
+
* O(1).
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* [...runLengthEncode("aaab")]; // [["a", 3], ["b", 1]]
|
|
16
|
+
* ```;
|
|
17
|
+
*
|
|
18
|
+
* @param iterable - The values, read once as the runs are pulled.
|
|
19
|
+
* @param comparisonStrategy - How each value is compared with the first value
|
|
20
|
+
* of the current run. Default `"sameValueZero"`, so `NaN` runs collapse.
|
|
21
|
+
* @returns The runs, in order, each as `[value, count]` with `count ≥ 1`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function runLengthEncode<T>(iterable: Iterable<T>, comparisonStrategy?: EqualityComparisonStrategy<T, T>): Generator<[value: T, count: number], void, undefined>;
|
|
24
|
+
/**
|
|
25
|
+
* Expands `[value, count]` runs back into the values they stand for, lazily —
|
|
26
|
+
* the inverse of {@link runLengthEncode}. A run of count `0` yields nothing.
|
|
27
|
+
*
|
|
28
|
+
* Time complexity: O(n + r) for `n` values out and `r` runs in. Memory: O(1).
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* [...runLengthDecode([["a", 3], ["b", 1]])]; // ["a", "a", "a", "b"]
|
|
33
|
+
* ```;
|
|
34
|
+
*
|
|
35
|
+
* @param runs - The runs, read once as the values are pulled.
|
|
36
|
+
* @returns Each run's value, `count` times, run after run.
|
|
37
|
+
* @throws {RangeError} When a count is not a non-negative safe integer, as the
|
|
38
|
+
* run is reached.
|
|
39
|
+
*/
|
|
40
|
+
export declare function runLengthDecode<T>(runs: Iterable<readonly [value: T, count: number]>): Generator<T, void, undefined>;
|
|
41
|
+
//# sourceMappingURL=run-length-encode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-length-encode.d.ts","sourceRoot":"","sources":["../../src/group/run-length-encode.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,KAAK,0BAA0B,EAC/B,MAAM,cAAc,CAAC;AAEtB;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAiB,eAAe,CAAC,CAAC,EACjC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrB,kBAAkB,GAAE,0BAA0B,CAAC,CAAC,EAAE,CAAC,CAAmB,GACpE,SAAS,CAAC,CAAC,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,EAAE,IAAI,EAAE,SAAS,CAAC,CAkBvD;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAiB,eAAe,CAAC,CAAC,EACjC,IAAI,EAAE,QAAQ,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC,GAChD,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,CAAC,CAO/B"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { assertInteger, resolveEqualityComparator, } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Collapses consecutive equal values into `[value, count]` runs, lazily —
|
|
4
|
+
* run-length encoding, the counting form of `groupAdjacentBy`: `a a a b a`
|
|
5
|
+
* gives `[a, 3], [b, 1], [a, 1]`. The inverse is {@link runLengthDecode}.
|
|
6
|
+
*
|
|
7
|
+
* Each run is yielded once the first value of the next has been read (or the
|
|
8
|
+
* input ends), with the first value of the run.
|
|
9
|
+
*
|
|
10
|
+
* Time complexity: O(n), one comparison per value after the first. Memory:
|
|
11
|
+
* O(1).
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* [...runLengthEncode("aaab")]; // [["a", 3], ["b", 1]]
|
|
16
|
+
* ```;
|
|
17
|
+
*
|
|
18
|
+
* @param iterable - The values, read once as the runs are pulled.
|
|
19
|
+
* @param comparisonStrategy - How each value is compared with the first value
|
|
20
|
+
* of the current run. Default `"sameValueZero"`, so `NaN` runs collapse.
|
|
21
|
+
* @returns The runs, in order, each as `[value, count]` with `count ≥ 1`.
|
|
22
|
+
*/
|
|
23
|
+
export function* runLengthEncode(iterable, comparisonStrategy = "sameValueZero") {
|
|
24
|
+
const equals = resolveEqualityComparator(comparisonStrategy);
|
|
25
|
+
let value;
|
|
26
|
+
let count = 0;
|
|
27
|
+
for (const item of iterable) {
|
|
28
|
+
if (count > 0 && equals(value, item)) {
|
|
29
|
+
count++;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (count > 0) {
|
|
33
|
+
yield [value, count];
|
|
34
|
+
}
|
|
35
|
+
value = item;
|
|
36
|
+
count = 1;
|
|
37
|
+
}
|
|
38
|
+
if (count > 0) {
|
|
39
|
+
yield [value, count];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Expands `[value, count]` runs back into the values they stand for, lazily —
|
|
44
|
+
* the inverse of {@link runLengthEncode}. A run of count `0` yields nothing.
|
|
45
|
+
*
|
|
46
|
+
* Time complexity: O(n + r) for `n` values out and `r` runs in. Memory: O(1).
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* [...runLengthDecode([["a", 3], ["b", 1]])]; // ["a", "a", "a", "b"]
|
|
51
|
+
* ```;
|
|
52
|
+
*
|
|
53
|
+
* @param runs - The runs, read once as the values are pulled.
|
|
54
|
+
* @returns Each run's value, `count` times, run after run.
|
|
55
|
+
* @throws {RangeError} When a count is not a non-negative safe integer, as the
|
|
56
|
+
* run is reached.
|
|
57
|
+
*/
|
|
58
|
+
export function* runLengthDecode(runs) {
|
|
59
|
+
for (const [value, count] of runs) {
|
|
60
|
+
assertInteger(count, "runLengthDecode: a run's count", 0);
|
|
61
|
+
for (let index = 0; index < count; index++) {
|
|
62
|
+
yield value;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=run-length-encode.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-length-encode.js","sourceRoot":"","sources":["../../src/group/run-length-encode.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,aAAa,EACb,yBAAyB,GAEzB,MAAM,cAAc,CAAC;AAEtB;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,SAAS,CAAC,CAAC,eAAe,CAC/B,QAAqB,EACrB,kBAAkB,GAAqC,eAAe;IAEtE,MAAM,MAAM,GAAG,yBAAyB,CAAC,kBAAkB,CAAC,CAAC;IAC7D,IAAI,KAAQ,CAAC;IACb,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;QAC7B,IAAI,KAAK,GAAG,CAAC,IAAI,MAAM,CAAC,KAAM,EAAE,IAAI,CAAC,EAAE,CAAC;YACvC,KAAK,EAAE,CAAC;YACR,SAAS;QACV,CAAC;QACD,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;YACf,MAAM,CAAC,KAAM,EAAE,KAAK,CAAC,CAAC;QACvB,CAAC;QACD,KAAK,GAAG,IAAI,CAAC;QACb,KAAK,GAAG,CAAC,CAAC;IACX,CAAC;IACD,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QACf,MAAM,CAAC,KAAM,EAAE,KAAK,CAAC,CAAC;IACvB,CAAC;AACF,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,SAAS,CAAC,CAAC,eAAe,CAC/B,IAAkD;IAElD,KAAK,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,IAAI,IAAI,EAAE,CAAC;QACnC,aAAa,CAAC,KAAK,EAAE,gCAAgC,EAAE,CAAC,CAAC,CAAC;QAC1D,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC;YAC5C,MAAM,KAAK,CAAC;QACb,CAAC;IACF,CAAC;AACF,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,33 @@
|
|
|
1
|
+
export * from "./combinatorics/cartesian-product.js";
|
|
1
2
|
export * from "./combinatorics/combinations.js";
|
|
3
|
+
export * from "./combinatorics/permutations.js";
|
|
4
|
+
export * from "./combinatorics/power-set.js";
|
|
2
5
|
export * from "./distribute/spread-evenly.js";
|
|
3
6
|
export * from "./graph/a-star.js";
|
|
4
7
|
export * from "./graph/best-first-search.js";
|
|
8
|
+
export * from "./graph/bfs.js";
|
|
5
9
|
export * from "./graph/children-of.js";
|
|
10
|
+
export * from "./graph/connected-components.js";
|
|
6
11
|
export * from "./graph/dfs.js";
|
|
7
12
|
export * from "./graph/find-cycle.js";
|
|
13
|
+
export * from "./graph/floyd-warshall.js";
|
|
14
|
+
export * from "./graph/graph-cycle-error.js";
|
|
8
15
|
export * from "./graph/hierarchy-from-parents.js";
|
|
16
|
+
export * from "./graph/lowest-common-ancestor.js";
|
|
17
|
+
export * from "./graph/topological-sort.js";
|
|
9
18
|
export * from "./graph/walk-tree.js";
|
|
19
|
+
export * from "./group/count-by.js";
|
|
20
|
+
export * from "./group/flat-group-by.js";
|
|
21
|
+
export * from "./group/group-adjacent-by.js";
|
|
10
22
|
export * from "./group/group-by.js";
|
|
11
23
|
export * from "./group/group-indices.js";
|
|
24
|
+
export * from "./group/group-sort.js";
|
|
25
|
+
export * from "./group/index-by.js";
|
|
26
|
+
export * from "./group/nested-map.js";
|
|
12
27
|
export * from "./group/partition-indices.js";
|
|
13
28
|
export * from "./group/partition.js";
|
|
29
|
+
export * from "./group/rollup-by.js";
|
|
30
|
+
export * from "./group/run-length-encode.js";
|
|
14
31
|
export * from "./interval/complement-intervals.js";
|
|
15
32
|
export * from "./interval/intersect-intervals.js";
|
|
16
33
|
export * from "./interval/interval-step.js";
|
|
@@ -23,17 +40,32 @@ export * from "./optimize/viterbi.js";
|
|
|
23
40
|
export * from "./ordered/binary-search.js";
|
|
24
41
|
export * from "./ordered/bisect-center.js";
|
|
25
42
|
export * from "./ordered/bisect-left.js";
|
|
43
|
+
export * from "./ordered/bisect-right-numbers-array.js";
|
|
44
|
+
export * from "./ordered/bisect-right-numbers.js";
|
|
26
45
|
export * from "./ordered/bisect-right.js";
|
|
27
46
|
export * from "./ordered/bisect.js";
|
|
28
47
|
export * from "./ordered/create-bisector.js";
|
|
48
|
+
export * from "./ordered/fractional-key-alphabet.js";
|
|
49
|
+
export * from "./ordered/fractional-key-error.js";
|
|
50
|
+
export * from "./ordered/fractional-key.js";
|
|
51
|
+
export * from "./ordered/is-sorted.js";
|
|
29
52
|
export * from "./recursion/run-trampoline.js";
|
|
30
53
|
export * from "./sample/choice.js";
|
|
54
|
+
export * from "./sample/sample-without-replacement.js";
|
|
31
55
|
export * from "./sample/shuffle.js";
|
|
56
|
+
export * from "./sample/weighted-choice-index.js";
|
|
57
|
+
export * from "./sample/weighted-choice.js";
|
|
32
58
|
export * from "./select/max-by.js";
|
|
33
59
|
export * from "./select/min-by.js";
|
|
34
|
-
export * from "./select/
|
|
60
|
+
export * from "./select/minmax.js";
|
|
61
|
+
export * from "./select/quickselect-in-place.js";
|
|
35
62
|
export * from "./select/select-at.js";
|
|
36
63
|
export * from "./select/unique-min-by.js";
|
|
64
|
+
export * from "./sequence/common-prefix-length.js";
|
|
65
|
+
export * from "./sequence/diff-sequences.js";
|
|
66
|
+
export * from "./sequence/levenshtein-distance.js";
|
|
67
|
+
export * from "./sequence/longest-common-subsequence.js";
|
|
68
|
+
export * from "./sequence/longest-increasing-subsequence.js";
|
|
37
69
|
export * from "./set/difference.js";
|
|
38
70
|
export * from "./set/duplicates-by.js";
|
|
39
71
|
export * from "./set/intersection.js";
|
|
@@ -45,6 +77,12 @@ export * from "./set/symmetric-difference.js";
|
|
|
45
77
|
export * from "./set/union.js";
|
|
46
78
|
export * from "./set/uniq-by.js";
|
|
47
79
|
export * from "./set/uniq.js";
|
|
80
|
+
export * from "./sort/counting-sort.js";
|
|
81
|
+
export * from "./sort/merge-sort.js";
|
|
82
|
+
export * from "./sort/quick-sort-in-place.js";
|
|
83
|
+
export * from "./sort/radix-index-sorter.js";
|
|
48
84
|
export * from "./sort/radix-sort.js";
|
|
85
|
+
export * from "./sort/rank.js";
|
|
86
|
+
export * from "./sort/sort-range.js";
|
|
49
87
|
export * from "./sort/sorted-indices.js";
|
|
50
88
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iCAAiC,CAAC;AAChD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,mBAAmB,CAAC;AAClC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,wBAAwB,CAAC;AACvC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,mCAAmC,CAAC;AAClD,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,0BAA0B,CAAC;AACzC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sBAAsB,CAAC;AACrC,cAAc,oCAAoC,CAAC;AACnD,cAAc,mCAAmC,CAAC;AAClD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,sCAAsC,CAAC;AACrD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,kCAAkC,CAAC;AACjD,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sCAAsC,CAAC;AACrD,cAAc,iCAAiC,CAAC;AAChD,cAAc,iCAAiC,CAAC;AAChD,cAAc,8BAA8B,CAAC;AAC7C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,mBAAmB,CAAC;AAClC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,wBAAwB,CAAC;AACvC,cAAc,iCAAiC,CAAC;AAChD,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,mCAAmC,CAAC;AAClD,cAAc,mCAAmC,CAAC;AAClD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,0BAA0B,CAAC;AACzC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,qBAAqB,CAAC;AACpC,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,oCAAoC,CAAC;AACnD,cAAc,mCAAmC,CAAC;AAClD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,sCAAsC,CAAC;AACrD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,kCAAkC,CAAC;AACjD,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,yCAAyC,CAAC;AACxD,cAAc,mCAAmC,CAAC;AAClD,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sCAAsC,CAAC;AACrD,cAAc,mCAAmC,CAAC;AAClD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,wBAAwB,CAAC;AACvC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,oBAAoB,CAAC;AACnC,cAAc,wCAAwC,CAAC;AACvD,cAAc,qBAAqB,CAAC;AACpC,cAAc,mCAAmC,CAAC;AAClD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kCAAkC,CAAC;AACjD,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,oCAAoC,CAAC;AACnD,cAAc,8BAA8B,CAAC;AAC7C,cAAc,oCAAoC,CAAC;AACnD,cAAc,0CAA0C,CAAC;AACzD,cAAc,8CAA8C,CAAC;AAC7D,cAAc,qBAAqB,CAAC;AACpC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,sBAAsB,CAAC;AACrC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,yBAAyB,CAAC;AACxC,cAAc,sBAAsB,CAAC;AACrC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sBAAsB,CAAC;AACrC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,0BAA0B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,33 @@
|
|
|
1
|
+
export * from "./combinatorics/cartesian-product.js";
|
|
1
2
|
export * from "./combinatorics/combinations.js";
|
|
3
|
+
export * from "./combinatorics/permutations.js";
|
|
4
|
+
export * from "./combinatorics/power-set.js";
|
|
2
5
|
export * from "./distribute/spread-evenly.js";
|
|
3
6
|
export * from "./graph/a-star.js";
|
|
4
7
|
export * from "./graph/best-first-search.js";
|
|
8
|
+
export * from "./graph/bfs.js";
|
|
5
9
|
export * from "./graph/children-of.js";
|
|
10
|
+
export * from "./graph/connected-components.js";
|
|
6
11
|
export * from "./graph/dfs.js";
|
|
7
12
|
export * from "./graph/find-cycle.js";
|
|
13
|
+
export * from "./graph/floyd-warshall.js";
|
|
14
|
+
export * from "./graph/graph-cycle-error.js";
|
|
8
15
|
export * from "./graph/hierarchy-from-parents.js";
|
|
16
|
+
export * from "./graph/lowest-common-ancestor.js";
|
|
17
|
+
export * from "./graph/topological-sort.js";
|
|
9
18
|
export * from "./graph/walk-tree.js";
|
|
19
|
+
export * from "./group/count-by.js";
|
|
20
|
+
export * from "./group/flat-group-by.js";
|
|
21
|
+
export * from "./group/group-adjacent-by.js";
|
|
10
22
|
export * from "./group/group-by.js";
|
|
11
23
|
export * from "./group/group-indices.js";
|
|
24
|
+
export * from "./group/group-sort.js";
|
|
25
|
+
export * from "./group/index-by.js";
|
|
26
|
+
export * from "./group/nested-map.js";
|
|
12
27
|
export * from "./group/partition-indices.js";
|
|
13
28
|
export * from "./group/partition.js";
|
|
29
|
+
export * from "./group/rollup-by.js";
|
|
30
|
+
export * from "./group/run-length-encode.js";
|
|
14
31
|
export * from "./interval/complement-intervals.js";
|
|
15
32
|
export * from "./interval/intersect-intervals.js";
|
|
16
33
|
export * from "./interval/interval-step.js";
|
|
@@ -23,17 +40,32 @@ export * from "./optimize/viterbi.js";
|
|
|
23
40
|
export * from "./ordered/binary-search.js";
|
|
24
41
|
export * from "./ordered/bisect-center.js";
|
|
25
42
|
export * from "./ordered/bisect-left.js";
|
|
43
|
+
export * from "./ordered/bisect-right-numbers-array.js";
|
|
44
|
+
export * from "./ordered/bisect-right-numbers.js";
|
|
26
45
|
export * from "./ordered/bisect-right.js";
|
|
27
46
|
export * from "./ordered/bisect.js";
|
|
28
47
|
export * from "./ordered/create-bisector.js";
|
|
48
|
+
export * from "./ordered/fractional-key-alphabet.js";
|
|
49
|
+
export * from "./ordered/fractional-key-error.js";
|
|
50
|
+
export * from "./ordered/fractional-key.js";
|
|
51
|
+
export * from "./ordered/is-sorted.js";
|
|
29
52
|
export * from "./recursion/run-trampoline.js";
|
|
30
53
|
export * from "./sample/choice.js";
|
|
54
|
+
export * from "./sample/sample-without-replacement.js";
|
|
31
55
|
export * from "./sample/shuffle.js";
|
|
56
|
+
export * from "./sample/weighted-choice-index.js";
|
|
57
|
+
export * from "./sample/weighted-choice.js";
|
|
32
58
|
export * from "./select/max-by.js";
|
|
33
59
|
export * from "./select/min-by.js";
|
|
34
|
-
export * from "./select/
|
|
60
|
+
export * from "./select/minmax.js";
|
|
61
|
+
export * from "./select/quickselect-in-place.js";
|
|
35
62
|
export * from "./select/select-at.js";
|
|
36
63
|
export * from "./select/unique-min-by.js";
|
|
64
|
+
export * from "./sequence/common-prefix-length.js";
|
|
65
|
+
export * from "./sequence/diff-sequences.js";
|
|
66
|
+
export * from "./sequence/levenshtein-distance.js";
|
|
67
|
+
export * from "./sequence/longest-common-subsequence.js";
|
|
68
|
+
export * from "./sequence/longest-increasing-subsequence.js";
|
|
37
69
|
export * from "./set/difference.js";
|
|
38
70
|
export * from "./set/duplicates-by.js";
|
|
39
71
|
export * from "./set/intersection.js";
|
|
@@ -45,6 +77,12 @@ export * from "./set/symmetric-difference.js";
|
|
|
45
77
|
export * from "./set/union.js";
|
|
46
78
|
export * from "./set/uniq-by.js";
|
|
47
79
|
export * from "./set/uniq.js";
|
|
80
|
+
export * from "./sort/counting-sort.js";
|
|
81
|
+
export * from "./sort/merge-sort.js";
|
|
82
|
+
export * from "./sort/quick-sort-in-place.js";
|
|
83
|
+
export * from "./sort/radix-index-sorter.js";
|
|
48
84
|
export * from "./sort/radix-sort.js";
|
|
85
|
+
export * from "./sort/rank.js";
|
|
86
|
+
export * from "./sort/sort-range.js";
|
|
49
87
|
export * from "./sort/sorted-indices.js";
|
|
50
88
|
//# sourceMappingURL=index.js.map
|