@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.
Files changed (156) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/README.md +44 -15
  3. package/dist/combinatorics/cartesian-product.d.ts +46 -0
  4. package/dist/combinatorics/cartesian-product.d.ts.map +1 -0
  5. package/dist/combinatorics/cartesian-product.js +79 -0
  6. package/dist/combinatorics/cartesian-product.js.map +1 -0
  7. package/dist/combinatorics/permutations.d.ts +41 -0
  8. package/dist/combinatorics/permutations.d.ts.map +1 -0
  9. package/dist/combinatorics/permutations.js +70 -0
  10. package/dist/combinatorics/permutations.js.map +1 -0
  11. package/dist/combinatorics/power-set.d.ts +40 -0
  12. package/dist/combinatorics/power-set.d.ts.map +1 -0
  13. package/dist/combinatorics/power-set.js +67 -0
  14. package/dist/combinatorics/power-set.js.map +1 -0
  15. package/dist/graph/bfs.d.ts +66 -0
  16. package/dist/graph/bfs.d.ts.map +1 -0
  17. package/dist/graph/bfs.js +89 -0
  18. package/dist/graph/bfs.js.map +1 -0
  19. package/dist/graph/connected-components.d.ts +35 -0
  20. package/dist/graph/connected-components.d.ts.map +1 -0
  21. package/dist/graph/connected-components.js +75 -0
  22. package/dist/graph/connected-components.js.map +1 -0
  23. package/dist/graph/floyd-warshall.d.ts +67 -0
  24. package/dist/graph/floyd-warshall.d.ts.map +1 -0
  25. package/dist/graph/floyd-warshall.js +106 -0
  26. package/dist/graph/floyd-warshall.js.map +1 -0
  27. package/dist/graph/graph-cycle-error.d.ts +16 -0
  28. package/dist/graph/graph-cycle-error.d.ts.map +1 -0
  29. package/dist/graph/graph-cycle-error.js +20 -0
  30. package/dist/graph/graph-cycle-error.js.map +1 -0
  31. package/dist/graph/lowest-common-ancestor.d.ts +32 -0
  32. package/dist/graph/lowest-common-ancestor.d.ts.map +1 -0
  33. package/dist/graph/lowest-common-ancestor.js +56 -0
  34. package/dist/graph/lowest-common-ancestor.js.map +1 -0
  35. package/dist/graph/topological-sort.d.ts +51 -0
  36. package/dist/graph/topological-sort.d.ts.map +1 -0
  37. package/dist/graph/topological-sort.js +124 -0
  38. package/dist/graph/topological-sort.js.map +1 -0
  39. package/dist/group/_nest-by.d.ts +19 -0
  40. package/dist/group/_nest-by.d.ts.map +1 -0
  41. package/dist/group/_nest-by.js +85 -0
  42. package/dist/group/_nest-by.js.map +1 -0
  43. package/dist/group/count-by.d.ts +31 -0
  44. package/dist/group/count-by.d.ts.map +1 -0
  45. package/dist/group/count-by.js +35 -0
  46. package/dist/group/count-by.js.map +1 -0
  47. package/dist/group/flat-group-by.d.ts +28 -0
  48. package/dist/group/flat-group-by.d.ts.map +1 -0
  49. package/dist/group/flat-group-by.js +30 -0
  50. package/dist/group/flat-group-by.js.map +1 -0
  51. package/dist/group/group-adjacent-by.d.ts +31 -0
  52. package/dist/group/group-adjacent-by.d.ts.map +1 -0
  53. package/dist/group/group-adjacent-by.js +50 -0
  54. package/dist/group/group-adjacent-by.js.map +1 -0
  55. package/dist/group/group-sort.d.ts +38 -0
  56. package/dist/group/group-sort.d.ts.map +1 -0
  57. package/dist/group/group-sort.js +49 -0
  58. package/dist/group/group-sort.js.map +1 -0
  59. package/dist/group/index-by.d.ts +33 -0
  60. package/dist/group/index-by.d.ts.map +1 -0
  61. package/dist/group/index-by.js +42 -0
  62. package/dist/group/index-by.js.map +1 -0
  63. package/dist/group/nested-map.d.ts +12 -0
  64. package/dist/group/nested-map.d.ts.map +1 -0
  65. package/dist/group/nested-map.js +2 -0
  66. package/dist/group/nested-map.js.map +1 -0
  67. package/dist/group/rollup-by.d.ts +62 -0
  68. package/dist/group/rollup-by.d.ts.map +1 -0
  69. package/dist/group/rollup-by.js +72 -0
  70. package/dist/group/rollup-by.js.map +1 -0
  71. package/dist/group/run-length-encode.d.ts +41 -0
  72. package/dist/group/run-length-encode.d.ts.map +1 -0
  73. package/dist/group/run-length-encode.js +66 -0
  74. package/dist/group/run-length-encode.js.map +1 -0
  75. package/dist/index.d.ts +32 -1
  76. package/dist/index.d.ts.map +1 -1
  77. package/dist/index.js +32 -1
  78. package/dist/index.js.map +1 -1
  79. package/dist/ordered/is-sorted.d.ts +33 -0
  80. package/dist/ordered/is-sorted.d.ts.map +1 -0
  81. package/dist/ordered/is-sorted.js +39 -0
  82. package/dist/ordered/is-sorted.js.map +1 -0
  83. package/dist/sample/sample-without-replacement.d.ts +26 -0
  84. package/dist/sample/sample-without-replacement.d.ts.map +1 -0
  85. package/dist/sample/sample-without-replacement.js +41 -0
  86. package/dist/sample/sample-without-replacement.js.map +1 -0
  87. package/dist/sample/shuffle.d.ts +2 -2
  88. package/dist/sample/shuffle.d.ts.map +1 -1
  89. package/dist/sample/shuffle.js +3 -3
  90. package/dist/sample/shuffle.js.map +1 -1
  91. package/dist/sample/weighted-choice-index.d.ts +29 -0
  92. package/dist/sample/weighted-choice-index.d.ts.map +1 -0
  93. package/dist/sample/weighted-choice-index.js +62 -0
  94. package/dist/sample/weighted-choice-index.js.map +1 -0
  95. package/dist/sample/weighted-choice.d.ts +28 -0
  96. package/dist/sample/weighted-choice.d.ts.map +1 -0
  97. package/dist/sample/weighted-choice.js +31 -0
  98. package/dist/sample/weighted-choice.js.map +1 -0
  99. package/dist/select/minmax.d.ts +52 -0
  100. package/dist/select/minmax.d.ts.map +1 -0
  101. package/dist/select/minmax.js +109 -0
  102. package/dist/select/minmax.js.map +1 -0
  103. package/dist/select/{quickselect.d.ts → quickselect-in-place.d.ts} +2 -2
  104. package/dist/select/quickselect-in-place.d.ts.map +1 -0
  105. package/dist/select/{quickselect.js → quickselect-in-place.js} +2 -2
  106. package/dist/select/quickselect-in-place.js.map +1 -0
  107. package/dist/sequence/_matching-runs.d.ts +16 -0
  108. package/dist/sequence/_matching-runs.d.ts.map +1 -0
  109. package/dist/sequence/_matching-runs.js +178 -0
  110. package/dist/sequence/_matching-runs.js.map +1 -0
  111. package/dist/sequence/common-prefix-length.d.ts +26 -0
  112. package/dist/sequence/common-prefix-length.d.ts.map +1 -0
  113. package/dist/sequence/common-prefix-length.js +34 -0
  114. package/dist/sequence/common-prefix-length.js.map +1 -0
  115. package/dist/sequence/diff-sequences.d.ts +59 -0
  116. package/dist/sequence/diff-sequences.d.ts.map +1 -0
  117. package/dist/sequence/diff-sequences.js +67 -0
  118. package/dist/sequence/diff-sequences.js.map +1 -0
  119. package/dist/sequence/levenshtein-distance.d.ts +43 -0
  120. package/dist/sequence/levenshtein-distance.d.ts.map +1 -0
  121. package/dist/sequence/levenshtein-distance.js +102 -0
  122. package/dist/sequence/levenshtein-distance.js.map +1 -0
  123. package/dist/sequence/longest-common-subsequence.d.ts +28 -0
  124. package/dist/sequence/longest-common-subsequence.d.ts.map +1 -0
  125. package/dist/sequence/longest-common-subsequence.js +39 -0
  126. package/dist/sequence/longest-common-subsequence.js.map +1 -0
  127. package/dist/sequence/longest-increasing-subsequence.d.ts +28 -0
  128. package/dist/sequence/longest-increasing-subsequence.d.ts.map +1 -0
  129. package/dist/sequence/longest-increasing-subsequence.js +47 -0
  130. package/dist/sequence/longest-increasing-subsequence.js.map +1 -0
  131. package/dist/sort/_sort-primitives.d.ts +6 -0
  132. package/dist/sort/_sort-primitives.d.ts.map +1 -0
  133. package/dist/sort/_sort-primitives.js +18 -0
  134. package/dist/sort/_sort-primitives.js.map +1 -0
  135. package/dist/sort/counting-sort.d.ts +40 -0
  136. package/dist/sort/counting-sort.d.ts.map +1 -0
  137. package/dist/sort/counting-sort.js +51 -0
  138. package/dist/sort/counting-sort.js.map +1 -0
  139. package/dist/sort/merge-sort.d.ts +26 -0
  140. package/dist/sort/merge-sort.d.ts.map +1 -0
  141. package/dist/sort/merge-sort.js +73 -0
  142. package/dist/sort/merge-sort.js.map +1 -0
  143. package/dist/sort/quick-sort-in-place.d.ts +35 -0
  144. package/dist/sort/quick-sort-in-place.d.ts.map +1 -0
  145. package/dist/sort/quick-sort-in-place.js +98 -0
  146. package/dist/sort/quick-sort-in-place.js.map +1 -0
  147. package/dist/sort/rank.d.ts +64 -0
  148. package/dist/sort/rank.d.ts.map +1 -0
  149. package/dist/sort/rank.js +107 -0
  150. package/dist/sort/rank.js.map +1 -0
  151. package/dist/sort/sort-range.d.ts.map +1 -1
  152. package/dist/sort/sort-range.js +1 -16
  153. package/dist/sort/sort-range.js.map +1 -1
  154. package/package.json +6 -6
  155. package/dist/select/quickselect.d.ts.map +0 -1
  156. package/dist/select/quickselect.js.map +0 -1
@@ -0,0 +1,124 @@
1
+ import { findCycle } from "./find-cycle.js";
2
+ import { GraphCycleError } from "./graph-cycle-error.js";
3
+ /**
4
+ * Orders every node reachable from `roots` so that each node comes before all
5
+ * of its successors: the dependency order of a directed acyclic graph, for a
6
+ * build order, a task scheduler or module initialisation. With `childrenOf`
7
+ * returning a node's _dependencies_ rather than its dependents, reverse the
8
+ * result to list dependencies first.
9
+ *
10
+ * Kahn's algorithm: nodes with no remaining predecessor are emitted in the
11
+ * order they were first reached, then each emitted node releases its
12
+ * successors. So the order is deterministic, and nodes with no edge between
13
+ * them keep the order `roots` and `childrenOf` gave them. Iterative throughout,
14
+ * so a hostile deep graph cannot overflow the stack.
15
+ *
16
+ * Nodes are compared by identity (`Map` semantics, so `NaN` equals `NaN`).
17
+ * `childrenOf` is called once per node, and the iterable it returns is read
18
+ * once; parallel edges are allowed. A cycle is never answered with a partial
19
+ * order: a {@link GraphCycleError} names one.
20
+ *
21
+ * Time complexity: O(V + E) for the nodes and edges reached. Memory: O(V + E).
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * const dependents = new Map([
26
+ * ["core", ["data", "algo"]],
27
+ * ["data", ["algo"]],
28
+ * ]);
29
+ * topologicalSort(["core"], (name) => dependents.get(name));
30
+ * // ["core", "data", "algo"]
31
+ * ```;
32
+ *
33
+ * @param roots - Where to start; a root reached from an earlier one is not
34
+ * reached again. Pass every node to order a whole graph.
35
+ * @param childrenOf - The successors of a node, or `undefined` for none.
36
+ * @param options - A work budget.
37
+ * @returns Every reachable node once, each before its successors.
38
+ * @throws {GraphCycleError} When a cycle is reachable; its `cycle` lists one,
39
+ * in edge order.
40
+ * @throws Whatever `options.budget.spend`, `childrenOf` or an iterator throws.
41
+ */
42
+ export function topologicalSort(roots, childrenOf, options = {}) {
43
+ const { budget } = options;
44
+ // Reached nodes by index, with their successors' indices in CSR form: the
45
+ // successors of node i are targets[offsets[i] … offsets[i + 1]).
46
+ const indices = new Map();
47
+ const nodes = [];
48
+ const offsets = [0];
49
+ const targets = [];
50
+ const indexOf = (node) => {
51
+ let index = indices.get(node);
52
+ if (index === undefined) {
53
+ budget?.spend(1);
54
+ index = nodes.length;
55
+ indices.set(node, index);
56
+ nodes.push(node);
57
+ }
58
+ return index;
59
+ };
60
+ // Nodes are expanded in the order they were reached, so each one's edges
61
+ // land contiguously in `targets`.
62
+ let expanded = 0;
63
+ for (const root of roots) {
64
+ budget?.spend(1);
65
+ indexOf(root);
66
+ while (expanded < nodes.length) {
67
+ const children = childrenOf(nodes[expanded]);
68
+ if (children !== undefined) {
69
+ for (const child of children) {
70
+ budget?.spend(1);
71
+ targets.push(indexOf(child));
72
+ }
73
+ }
74
+ offsets.push(targets.length);
75
+ expanded++;
76
+ }
77
+ }
78
+ const nodeCount = nodes.length;
79
+ const inDegrees = new Uint32Array(nodeCount);
80
+ for (const target of targets) {
81
+ inDegrees[target]++;
82
+ }
83
+ // The output doubles as Kahn's queue: `read` chases `written`.
84
+ const order = new Uint32Array(nodeCount);
85
+ let written = 0;
86
+ for (let index = 0; index < nodeCount; index++) {
87
+ if (inDegrees[index] === 0) {
88
+ order[written++] = index;
89
+ }
90
+ }
91
+ for (let read = 0; read < written; read++) {
92
+ const index = order[read];
93
+ const end = offsets[index + 1];
94
+ for (let edge = offsets[index]; edge < end; edge++) {
95
+ const target = targets[edge];
96
+ if (--inDegrees[target] === 0) {
97
+ order[written++] = target;
98
+ }
99
+ }
100
+ }
101
+ if (written < nodeCount) {
102
+ throw new GraphCycleError("topologicalSort", remainingCycle_(nodes, offsets, targets, inDegrees));
103
+ }
104
+ const sorted = Array.from({ length: nodeCount });
105
+ for (let position = 0; position < nodeCount; position++) {
106
+ sorted[position] = nodes[order[position]];
107
+ }
108
+ return sorted;
109
+ }
110
+ // Kahn's leftovers are the nodes still holding a predecessor among themselves,
111
+ // so the subgraph they induce has no source and therefore a cycle.
112
+ function remainingCycle_(nodes, offsets, targets, inDegrees) {
113
+ const remaining = [];
114
+ for (let index = 0; index < nodes.length; index++) {
115
+ if (inDegrees[index] > 0) {
116
+ remaining.push(index);
117
+ }
118
+ }
119
+ const cycle = findCycle(remaining, (index) => targets
120
+ .slice(offsets[index], offsets[index + 1])
121
+ .filter((target) => inDegrees[target] > 0));
122
+ return cycle.map((index) => nodes[index]);
123
+ }
124
+ //# sourceMappingURL=topological-sort.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"topological-sort.js","sourceRoot":"","sources":["../../src/graph/topological-sort.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAYzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,MAAM,UAAU,eAAe,CAC9B,KAAsB,EACtB,UAAwD,EACxD,OAAO,GAA2B,EAAE;IAEpC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAE3B,0EAA0E;IAC1E,iEAAiE;IACjE,MAAM,OAAO,GAAG,IAAI,GAAG,EAAiB,CAAC;IACzC,MAAM,KAAK,GAAY,EAAE,CAAC;IAC1B,MAAM,OAAO,GAAa,CAAC,CAAC,CAAC,CAAC;IAC9B,MAAM,OAAO,GAAa,EAAE,CAAC;IAE7B,MAAM,OAAO,GAAG,CAAC,IAAW,EAAU,EAAE;QACvC,IAAI,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAC9B,IAAI,KAAK,KAAK,SAAS,EAAE,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CAAC;IACD,OAAO,MAAM,CAAC;AACf,CAAC;AAED,+EAA+E;AAC/E,mEAAmE;AACnE,SAAS,eAAe,CACvB,KAAuB,EACvB,OAA0B,EAC1B,OAA0B,EAC1B,SAAsB;IAEtB,MAAM,SAAS,GAAa,EAAE,CAAC;IAC/B,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QACnD,IAAI,SAAS,CAAC,KAAK,CAAE,GAAG,CAAC,EAAE,CAAC;YAC3B,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACvB,CAAC;IACF,CAAC;IACD,MAAM,KAAK,GAAG,SAAS,CAAC,SAAS,EAAE,CAAC,KAAK,EAAE,EAAE,CAC5C,OAAO;SACL,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;SACzC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,SAAS,CAAC,MAAM,CAAE,GAAG,CAAC,CAAC,CAC3C,CAAC;IACH,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,CAAE,CAAC,CAAC;AAC5C,CAAC"}
@@ -0,0 +1,19 @@
1
+ import { type Callable, type EqualityComparisonStrategy } from "@ac-kit/core";
2
+ export declare function assertSameValueZero_(comparisonStrategy: EqualityComparisonStrategy<never, never>): void;
3
+ /**
4
+ * Walks each item down one `Map` level per accessor but the last, creating
5
+ * levels in first-occurrence order, and lets `addToLeaf` update the innermost
6
+ * map under the last key. O(n · k) for `k` accessors.
7
+ */
8
+ export declare function nestBy_<T, TLeaf>(iterable: Iterable<T>, keysOf: readonly Callable<[T], unknown>[], addToLeaf: (leaves: Map<unknown, TLeaf>, key: unknown, item: T) => void): Map<unknown, unknown>;
9
+ /** The `addToLeaf` of `nestBy_` that keeps each group's items, in order. */
10
+ export declare function groupInto_<T>(groups: Map<unknown, T[]>, key: unknown, item: T): void;
11
+ /** Replaces every leaf of a `depth`-level nesting by `map(leaf)`, in place. */
12
+ export declare function mapLeaves_<TLeaf, TResult>(nested: Map<unknown, unknown>, depth: number, map: (leaf: TLeaf) => TResult): void;
13
+ /**
14
+ * The leaves of a `depth`-level nesting as `[key₁, …, key_depth, leaf]` rows,
15
+ * depth first in insertion order: the first-occurrence order of the key
16
+ * tuples.
17
+ */
18
+ export declare function flattenNested_(nested: Map<unknown, unknown>, depth: number): unknown[][];
19
+ //# sourceMappingURL=_nest-by.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_nest-by.d.ts","sourceRoot":"","sources":["../../src/group/_nest-by.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,QAAQ,EACb,KAAK,0BAA0B,EAC/B,MAAM,cAAc,CAAC;AAEtB,wBAAgB,oBAAoB,CACnC,kBAAkB,EAAE,0BAA0B,CAAC,KAAK,EAAE,KAAK,CAAC,GAC1D,IAAI,CAMN;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,KAAK,EAC/B,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrB,MAAM,EAAE,SAAS,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,EACzC,SAAS,EAAE,CAAC,MAAM,EAAE,GAAG,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,KAAK,IAAI,GACrE,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAiBvB;AAED,4EAA4E;AAC5E,wBAAgB,UAAU,CAAC,CAAC,EAC3B,MAAM,EAAE,GAAG,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC,EACzB,GAAG,EAAE,OAAO,EACZ,IAAI,EAAE,CAAC,GACL,IAAI,CAON;AAED,+EAA+E;AAC/E,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,EACxC,MAAM,EAAE,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,EAC7B,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,OAAO,GAC3B,IAAI,CAeN;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAC7B,MAAM,EAAE,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,EAC7B,KAAK,EAAE,MAAM,GACX,OAAO,EAAE,EAAE,CAoBb"}
@@ -0,0 +1,85 @@
1
+ import { UnimplementedError, } from "@ac-kit/core";
2
+ export function assertSameValueZero_(comparisonStrategy) {
3
+ if (comparisonStrategy !== "sameValueZero") {
4
+ throw new UnimplementedError(`comparison strategy: ${comparisonStrategy.toString()}`);
5
+ }
6
+ }
7
+ /**
8
+ * Walks each item down one `Map` level per accessor but the last, creating
9
+ * levels in first-occurrence order, and lets `addToLeaf` update the innermost
10
+ * map under the last key. O(n · k) for `k` accessors.
11
+ */
12
+ export function nestBy_(iterable, keysOf, addToLeaf) {
13
+ const root = new Map();
14
+ const last = keysOf.length - 1;
15
+ for (const item of iterable) {
16
+ let level = root;
17
+ for (let depth = 0; depth < last; depth++) {
18
+ const key = keysOf[depth](item);
19
+ let next = level.get(key);
20
+ if (next === undefined) {
21
+ next = new Map();
22
+ level.set(key, next);
23
+ }
24
+ level = next;
25
+ }
26
+ addToLeaf(level, keysOf[last](item), item);
27
+ }
28
+ return root;
29
+ }
30
+ /** The `addToLeaf` of `nestBy_` that keeps each group's items, in order. */
31
+ export function groupInto_(groups, key, item) {
32
+ const group = groups.get(key);
33
+ if (group === undefined) {
34
+ groups.set(key, [item]);
35
+ }
36
+ else {
37
+ group.push(item);
38
+ }
39
+ }
40
+ /** Replaces every leaf of a `depth`-level nesting by `map(leaf)`, in place. */
41
+ export function mapLeaves_(nested, depth, map) {
42
+ const pending = [
43
+ [nested, depth],
44
+ ];
45
+ while (pending.length > 0) {
46
+ const [level, remaining] = pending.pop();
47
+ for (const [key, value] of level) {
48
+ if (remaining === 1) {
49
+ // Overwriting a present key keeps its place in the iteration.
50
+ level.set(key, map(value));
51
+ }
52
+ else {
53
+ pending.push([value, remaining - 1]);
54
+ }
55
+ }
56
+ }
57
+ }
58
+ /**
59
+ * The leaves of a `depth`-level nesting as `[key₁, …, key_depth, leaf]` rows,
60
+ * depth first in insertion order: the first-occurrence order of the key
61
+ * tuples.
62
+ */
63
+ export function flattenNested_(nested, depth) {
64
+ const rows = [];
65
+ const path = [];
66
+ const pending = [nested.entries()];
67
+ while (pending.length > 0) {
68
+ const next = pending[pending.length - 1].next();
69
+ if (next.done === true) {
70
+ pending.pop();
71
+ path.pop();
72
+ continue;
73
+ }
74
+ const [key, value] = next.value;
75
+ if (pending.length === depth) {
76
+ rows.push([...path, key, value]);
77
+ }
78
+ else {
79
+ path.push(key);
80
+ pending.push(value.entries());
81
+ }
82
+ }
83
+ return rows;
84
+ }
85
+ //# sourceMappingURL=_nest-by.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_nest-by.js","sourceRoot":"","sources":["../../src/group/_nest-by.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,kBAAkB,GAGlB,MAAM,cAAc,CAAC;AAEtB,MAAM,UAAU,oBAAoB,CACnC,kBAA4D;IAE5D,IAAI,kBAAkB,KAAK,eAAe,EAAE,CAAC;QAC5C,MAAM,IAAI,kBAAkB,CAC3B,wBAAwB,kBAAkB,CAAC,QAAQ,EAAE,EAAE,CACvD,CAAC;IACH,CAAC;AACF,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,OAAO,CACtB,QAAqB,EACrB,MAAyC,EACzC,SAAuE;IAEvE,MAAM,IAAI,GAAG,IAAI,GAAG,EAAoB,CAAC;IACzC,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;IAC/B,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;QAC7B,IAAI,KAAK,GAAG,IAAI,CAAC;QACjB,KAAK,I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AAC;AACb,CAAC"}
@@ -0,0 +1,31 @@
1
+ import type { EqualityComparisonStrategy } from "@ac-kit/core";
2
+ import type { KeyAccessors, NestedMap } from "./nested-map.js";
3
+ /**
4
+ * Counts the items of an iterable per key, one nested `Map` level per accessor,
5
+ * without materialising the groups — `groupBy` with each group replaced by its
6
+ * size, at none of the memory the groups would take.
7
+ *
8
+ * Keys appear in the order they first occur. For small non-negative integer
9
+ * keys, `groupIndices` gives the counts as differences of its offsets, in flat
10
+ * arrays.
11
+ *
12
+ * Time complexity: O(n · k) for `k` accessors under the default
13
+ * `"sameValueZero"`. Memory: O(distinct key tuples).
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * countBy(["a", "b", "a"], [(letter) => letter]); // Map { "a" → 2, "b" → 1 }
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 counts are kept in a `Map`.
26
+ * @returns Nested maps from each key to the number of items with it.
27
+ * @throws {UnimplementedError} If `comparisonStrategy` is not
28
+ * `"sameValueZero"`.
29
+ */
30
+ export declare function countBy<T, const Keys extends readonly [unknown, ...unknown[]]>(iterable: Iterable<T>, keysOf: KeyAccessors<T, Keys>, comparisonStrategy?: EqualityComparisonStrategy<Keys[number], Keys[number]>): NestedMap<Keys, number>;
31
+ //# sourceMappingURL=count-by.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"count-by.d.ts","sourceRoot":"","sources":["../../src/group/count-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;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;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,MAAM,CAAC,CAKzB"}
@@ -0,0 +1,35 @@
1
+ import { assertSameValueZero_, nestBy_ } from "./_nest-by.js";
2
+ /**
3
+ * Counts the items of an iterable per key, one nested `Map` level per accessor,
4
+ * without materialising the groups — `groupBy` with each group replaced by its
5
+ * size, at none of the memory the groups would take.
6
+ *
7
+ * Keys appear in the order they first occur. For small non-negative integer
8
+ * keys, `groupIndices` gives the counts as differences of its offsets, in flat
9
+ * arrays.
10
+ *
11
+ * Time complexity: O(n · k) for `k` accessors under the default
12
+ * `"sameValueZero"`. Memory: O(distinct key tuples).
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * countBy(["a", "b", "a"], [(letter) => letter]); // Map { "a" → 2, "b" → 1 }
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 counts are kept in a `Map`.
25
+ * @returns Nested maps from each key to the number of items with it.
26
+ * @throws {UnimplementedError} If `comparisonStrategy` is not
27
+ * `"sameValueZero"`.
28
+ */
29
+ export function countBy(iterable, keysOf, comparisonStrategy = "sameValueZero") {
30
+ assertSameValueZero_(comparisonStrategy);
31
+ return nestBy_(iterable, keysOf, (counts, key) => {
32
+ counts.set(key, (counts.get(key) ?? 0) + 1);
33
+ });
34
+ }
35
+ //# sourceMappingURL=count-by.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"count-by.js","sourceRoot":"","sources":["../../src/group/count-by.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAG9D;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,UAAU,OAAO,CACtB,QAAqB,EACrB,MAA6B,EAC7B,kBAAkB,GAGd,eAAe;IAEnB,oBAAoB,CAAC,kBAAkB,CAAC,CAAC;IACzC,OAAO,OAAO,CAAY,QAAQ,EAAE,MAAM,EAAE,CAAC,MAAM,EAAE,GAAG,EAAE,EAAE;QAC3D,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IAC7C,CAAC,CAA4B,CAAC;AAC/B,CAAC"}
@@ -0,0 +1,28 @@
1
+ import type { EqualityComparisonStrategy } from "@ac-kit/core";
2
+ import type { KeyAccessors } from "./nested-map.js";
3
+ /**
4
+ * The groups of `groupBy` flattened into one row per key tuple, `[key₁, …,
5
+ * keyₖ, items]`, in the order the key tuples first occur — d3's `flatGroup`,
6
+ * the shape a table or a chart's data layer takes.
7
+ *
8
+ * Time complexity: O(n · k) for `k` accessors under the default
9
+ * `"sameValueZero"`. Memory: O(n).
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * flatGroupBy(tasks, [(task) => task.owner]);
14
+ * // [["ada", [task1, task3]], ["grace", [task2]]]
15
+ * ```;
16
+ *
17
+ * @param iterable - The input iterable.
18
+ * @param keysOf - One or more functions taking an item and returning a key,
19
+ * outermost first; an array, so a trailing `comparisonStrategy` is not
20
+ * mistaken for one more accessor.
21
+ * @param comparisonStrategy - How keys are compared. Only `"sameValueZero"` is
22
+ * supported, since the groups are kept in a `Map`.
23
+ * @returns One row per key tuple: its keys, then its items in input order.
24
+ * @throws {UnimplementedError} If `comparisonStrategy` is not
25
+ * `"sameValueZero"`.
26
+ */
27
+ export declare function flatGroupBy<T, const Keys extends readonly [unknown, ...unknown[]]>(iterable: Iterable<T>, keysOf: KeyAccessors<T, Keys>, comparisonStrategy?: EqualityComparisonStrategy<Keys[number], Keys[number]>): [...Keys, T[]][];
28
+ //# sourceMappingURL=flat-group-by.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flat-group-by.d.ts","sourceRoot":"","sources":["../../src/group/flat-group-by.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAQ/D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAEpD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,WAAW,CAC1B,CAAC,EACD,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,kBAAkB,GAAE,0BAA0B,CAC7C,IAAI,CAAC,MAAM,CAAC,EACZ,IAAI,CAAC,MAAM,CAAC,CACM,GACjB,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE,CAMlB"}
@@ -0,0 +1,30 @@
1
+ import { assertSameValueZero_, flattenNested_, groupInto_, nestBy_, } from "./_nest-by.js";
2
+ /**
3
+ * The groups of `groupBy` flattened into one row per key tuple, `[key₁, …,
4
+ * keyₖ, items]`, in the order the key tuples first occur — d3's `flatGroup`,
5
+ * the shape a table or a chart's data layer takes.
6
+ *
7
+ * Time complexity: O(n · k) for `k` accessors under the default
8
+ * `"sameValueZero"`. Memory: O(n).
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * flatGroupBy(tasks, [(task) => task.owner]);
13
+ * // [["ada", [task1, task3]], ["grace", [task2]]]
14
+ * ```;
15
+ *
16
+ * @param iterable - The input iterable.
17
+ * @param keysOf - One or more functions taking an item and returning a key,
18
+ * outermost first; an array, so a trailing `comparisonStrategy` is not
19
+ * mistaken for one more accessor.
20
+ * @param comparisonStrategy - How keys are compared. Only `"sameValueZero"` is
21
+ * supported, since the groups are kept in a `Map`.
22
+ * @returns One row per key tuple: its keys, then its items in input order.
23
+ * @throws {UnimplementedError} If `comparisonStrategy` is not
24
+ * `"sameValueZero"`.
25
+ */
26
+ export function flatGroupBy(iterable, keysOf, comparisonStrategy = "sameValueZero") {
27
+ assertSameValueZero_(comparisonStrategy);
28
+ return flattenNested_(nestBy_(iterable, keysOf, groupInto_), keysOf.length);
29
+ }
30
+ //# sourceMappingURL=flat-group-by.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flat-group-by.js","sourceRoot":"","sources":["../../src/group/flat-group-by.ts"],"names":[],"mappings":"AAEA,OAAO,EACN,oBAAoB,EACpB,cAAc,EACd,UAAU,EACV,OAAO,GACP,MAAM,eAAe,CAAC;AAGvB;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,WAAW,CAI1B,QAAqB,EACrB,MAA6B,EAC7B,kBAAkB,GAGd,eAAe;IAEnB,oBAAoB,CAAC,kBAAkB,CAAC,CAAC;IACzC,OAAO,cAAc,CACpB,OAAO,CAAS,QAAQ,EAAE,MAAM,EAAE,UAAU,CAAC,EAC7C,MAAM,CAAC,MAAM,CACO,CAAC;AACvB,CAAC"}
@@ -0,0 +1,31 @@
1
+ import { type Callable, type EqualityComparisonStrategy } from "@ac-kit/core";
2
+ /**
3
+ * Groups **consecutive runs** of items sharing a key, lazily — Python's
4
+ * `itertools.groupby`, Rust's `chunk_by`, C++'s `views::chunk_by` (known as
5
+ * `chunkBy` too). Unlike `groupBy`, which gathers every occurrence of a key
6
+ * into one group wherever it appears, a key that comes back after a different
7
+ * one starts a new run: `a a b a` gives three runs, not two groups. So it
8
+ * streams in O(1) memory beyond the current run, and any equality works, not
9
+ * only one a `Map` can hash.
10
+ *
11
+ * Each run is yielded once the first item of the next has been read (or the
12
+ * input ends), as a fresh array owned by the caller, with the key of its first
13
+ * item. `keyOf` is called once per item.
14
+ *
15
+ * Time complexity: O(n), one key comparison per item after the first. Memory:
16
+ * O(longest run).
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * [...groupAdjacentBy([1, 3, 2, 4, 5], (value) => value % 2)];
21
+ * // [[1, [1, 3]], [0, [2, 4]], [1, [5]]]
22
+ * ```;
23
+ *
24
+ * @param iterable - The input iterable, read once as the runs are pulled.
25
+ * @param keyOf - The key of an item.
26
+ * @param comparisonStrategy - How the key of each item is compared with the key
27
+ * of the current run's first item. Default `"sameValueZero"`, as `groupBy`.
28
+ * @returns The runs, in order, each as `[key, items]`.
29
+ */
30
+ export declare function groupAdjacentBy<T, TKey>(iterable: Iterable<T>, keyOf: Callable<[T], TKey>, comparisonStrategy?: EqualityComparisonStrategy<TKey, TKey>): Generator<[key: TKey, items: T[]], void, undefined>;
31
+ //# sourceMappingURL=group-adjacent-by.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"group-adjacent-by.d.ts","sourceRoot":"","sources":["../../src/group/group-adjacent-by.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,QAAQ,EACb,KAAK,0BAA0B,EAC/B,MAAM,cAAc,CAAC;AAEtB;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAiB,eAAe,CAAC,CAAC,EAAE,IAAI,EACvC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrB,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,EAC1B,kBAAkB,GAAE,0BAA0B,CAAC,IAAI,EAAE,IAAI,CAAmB,GAC1E,SAAS,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,EAAE,IAAI,EAAE,SAAS,CAAC,CAmBrD"}
@@ -0,0 +1,50 @@
1
+ import { resolveEqualityComparator, } from "@ac-kit/core";
2
+ /**
3
+ * Groups **consecutive runs** of items sharing a key, lazily — Python's
4
+ * `itertools.groupby`, Rust's `chunk_by`, C++'s `views::chunk_by` (known as
5
+ * `chunkBy` too). Unlike `groupBy`, which gathers every occurrence of a key
6
+ * into one group wherever it appears, a key that comes back after a different
7
+ * one starts a new run: `a a b a` gives three runs, not two groups. So it
8
+ * streams in O(1) memory beyond the current run, and any equality works, not
9
+ * only one a `Map` can hash.
10
+ *
11
+ * Each run is yielded once the first item of the next has been read (or the
12
+ * input ends), as a fresh array owned by the caller, with the key of its first
13
+ * item. `keyOf` is called once per item.
14
+ *
15
+ * Time complexity: O(n), one key comparison per item after the first. Memory:
16
+ * O(longest run).
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * [...groupAdjacentBy([1, 3, 2, 4, 5], (value) => value % 2)];
21
+ * // [[1, [1, 3]], [0, [2, 4]], [1, [5]]]
22
+ * ```;
23
+ *
24
+ * @param iterable - The input iterable, read once as the runs are pulled.
25
+ * @param keyOf - The key of an item.
26
+ * @param comparisonStrategy - How the key of each item is compared with the key
27
+ * of the current run's first item. Default `"sameValueZero"`, as `groupBy`.
28
+ * @returns The runs, in order, each as `[key, items]`.
29
+ */
30
+ export function* groupAdjacentBy(iterable, keyOf, comparisonStrategy = "sameValueZero") {
31
+ const equals = resolveEqualityComparator(comparisonStrategy);
32
+ let runKey;
33
+ let run;
34
+ for (const item of iterable) {
35
+ const key = keyOf(item);
36
+ if (run !== undefined && equals(runKey, key)) {
37
+ run.push(item);
38
+ continue;
39
+ }
40
+ if (run !== undefined) {
41
+ yield [runKey, run];
42
+ }
43
+ runKey = key;
44
+ run = [item];
45
+ }
46
+ if (run !== undefined) {
47
+ yield [runKey, run];
48
+ }
49
+ }
50
+ //# sourceMappingURL=group-adjacent-by.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"group-adjacent-by.js","sourceRoot":"","sources":["../../src/group/group-adjacent-by.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,yBAAyB,GAGzB,MAAM,cAAc,CAAC;AAEtB;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,SAAS,CAAC,CAAC,eAAe,CAC/B,QAAqB,EACrB,KAA0B,EAC1B,kBAAkB,GAA2C,eAAe;IAE5E,MAAM,MAAM,GAAG,yBAAyB,CAAC,kBAAkB,CAAC,CAAC;IAC7D,IAAI,MAAY,CAAC;IACjB,IAAI,GAAoB,CAAC;IACzB,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;QAC7B,MAAM,GAAG,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC;QACxB,IAAI,GAAG,KAAK,SAAS,IAAI,MAAM,CAAC,MAAO,EAAE,GAAG,CAAC,EAAE,CAAC;YAC/C,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACf,SAAS;QACV,CAAC;QACD,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;YACvB,MAAM,CAAC,MAAO,EAAE,GAAG,CAAC,CAAC;QACtB,CAAC;QACD,MAAM,GAAG,GAAG,CAAC;QACb,GAAG,GAAG,CAAC,IAAI,CAAC,CAAC;IACd,CAAC;IACD,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACvB,MAAM,CAAC,MAAO,EAAE,GAAG,CAAC,CAAC;IACtB,CAAC;AACF,CAAC"}
@@ -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"}