@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.
Files changed (191) hide show
  1. package/CHANGELOG.md +128 -0
  2. package/README.md +60 -18
  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/find-cycle.d.ts +5 -3
  24. package/dist/graph/find-cycle.d.ts.map +1 -1
  25. package/dist/graph/find-cycle.js +23 -3
  26. package/dist/graph/find-cycle.js.map +1 -1
  27. package/dist/graph/floyd-warshall.d.ts +67 -0
  28. package/dist/graph/floyd-warshall.d.ts.map +1 -0
  29. package/dist/graph/floyd-warshall.js +106 -0
  30. package/dist/graph/floyd-warshall.js.map +1 -0
  31. package/dist/graph/graph-cycle-error.d.ts +16 -0
  32. package/dist/graph/graph-cycle-error.d.ts.map +1 -0
  33. package/dist/graph/graph-cycle-error.js +20 -0
  34. package/dist/graph/graph-cycle-error.js.map +1 -0
  35. package/dist/graph/hierarchy-from-parents.js.map +1 -1
  36. package/dist/graph/lowest-common-ancestor.d.ts +32 -0
  37. package/dist/graph/lowest-common-ancestor.d.ts.map +1 -0
  38. package/dist/graph/lowest-common-ancestor.js +56 -0
  39. package/dist/graph/lowest-common-ancestor.js.map +1 -0
  40. package/dist/graph/topological-sort.d.ts +51 -0
  41. package/dist/graph/topological-sort.d.ts.map +1 -0
  42. package/dist/graph/topological-sort.js +124 -0
  43. package/dist/graph/topological-sort.js.map +1 -0
  44. package/dist/group/_nest-by.d.ts +19 -0
  45. package/dist/group/_nest-by.d.ts.map +1 -0
  46. package/dist/group/_nest-by.js +85 -0
  47. package/dist/group/_nest-by.js.map +1 -0
  48. package/dist/group/count-by.d.ts +31 -0
  49. package/dist/group/count-by.d.ts.map +1 -0
  50. package/dist/group/count-by.js +35 -0
  51. package/dist/group/count-by.js.map +1 -0
  52. package/dist/group/flat-group-by.d.ts +28 -0
  53. package/dist/group/flat-group-by.d.ts.map +1 -0
  54. package/dist/group/flat-group-by.js +30 -0
  55. package/dist/group/flat-group-by.js.map +1 -0
  56. package/dist/group/group-adjacent-by.d.ts +31 -0
  57. package/dist/group/group-adjacent-by.d.ts.map +1 -0
  58. package/dist/group/group-adjacent-by.js +50 -0
  59. package/dist/group/group-adjacent-by.js.map +1 -0
  60. package/dist/group/group-sort.d.ts +38 -0
  61. package/dist/group/group-sort.d.ts.map +1 -0
  62. package/dist/group/group-sort.js +49 -0
  63. package/dist/group/group-sort.js.map +1 -0
  64. package/dist/group/index-by.d.ts +33 -0
  65. package/dist/group/index-by.d.ts.map +1 -0
  66. package/dist/group/index-by.js +42 -0
  67. package/dist/group/index-by.js.map +1 -0
  68. package/dist/group/nested-map.d.ts +12 -0
  69. package/dist/group/nested-map.d.ts.map +1 -0
  70. package/dist/group/nested-map.js +2 -0
  71. package/dist/group/nested-map.js.map +1 -0
  72. package/dist/group/rollup-by.d.ts +62 -0
  73. package/dist/group/rollup-by.d.ts.map +1 -0
  74. package/dist/group/rollup-by.js +72 -0
  75. package/dist/group/rollup-by.js.map +1 -0
  76. package/dist/group/run-length-encode.d.ts +41 -0
  77. package/dist/group/run-length-encode.d.ts.map +1 -0
  78. package/dist/group/run-length-encode.js +66 -0
  79. package/dist/group/run-length-encode.js.map +1 -0
  80. package/dist/index.d.ts +39 -1
  81. package/dist/index.d.ts.map +1 -1
  82. package/dist/index.js +39 -1
  83. package/dist/index.js.map +1 -1
  84. package/dist/ordered/_fractional-key.d.ts +38 -0
  85. package/dist/ordered/_fractional-key.d.ts.map +1 -0
  86. package/dist/ordered/_fractional-key.js +251 -0
  87. package/dist/ordered/_fractional-key.js.map +1 -0
  88. package/dist/ordered/bisect-right-numbers-array.d.ts +20 -0
  89. package/dist/ordered/bisect-right-numbers-array.d.ts.map +1 -0
  90. package/dist/ordered/bisect-right-numbers-array.js +42 -0
  91. package/dist/ordered/bisect-right-numbers-array.js.map +1 -0
  92. package/dist/ordered/bisect-right-numbers.d.ts +19 -0
  93. package/dist/ordered/bisect-right-numbers.d.ts.map +1 -0
  94. package/dist/ordered/bisect-right-numbers.js +32 -0
  95. package/dist/ordered/bisect-right-numbers.js.map +1 -0
  96. package/dist/ordered/create-bisector.js.map +1 -1
  97. package/dist/ordered/fractional-key-alphabet.d.ts +41 -0
  98. package/dist/ordered/fractional-key-alphabet.d.ts.map +1 -0
  99. package/dist/ordered/fractional-key-alphabet.js +67 -0
  100. package/dist/ordered/fractional-key-alphabet.js.map +1 -0
  101. package/dist/ordered/fractional-key-error.d.ts +15 -0
  102. package/dist/ordered/fractional-key-error.d.ts.map +1 -0
  103. package/dist/ordered/fractional-key-error.js +20 -0
  104. package/dist/ordered/fractional-key-error.js.map +1 -0
  105. package/dist/ordered/fractional-key.d.ts +82 -0
  106. package/dist/ordered/fractional-key.d.ts.map +1 -0
  107. package/dist/ordered/fractional-key.js +199 -0
  108. package/dist/ordered/fractional-key.js.map +1 -0
  109. package/dist/ordered/is-sorted.d.ts +33 -0
  110. package/dist/ordered/is-sorted.d.ts.map +1 -0
  111. package/dist/ordered/is-sorted.js +39 -0
  112. package/dist/ordered/is-sorted.js.map +1 -0
  113. package/dist/sample/sample-without-replacement.d.ts +26 -0
  114. package/dist/sample/sample-without-replacement.d.ts.map +1 -0
  115. package/dist/sample/sample-without-replacement.js +41 -0
  116. package/dist/sample/sample-without-replacement.js.map +1 -0
  117. package/dist/sample/shuffle.d.ts +2 -2
  118. package/dist/sample/shuffle.d.ts.map +1 -1
  119. package/dist/sample/shuffle.js +3 -3
  120. package/dist/sample/shuffle.js.map +1 -1
  121. package/dist/sample/weighted-choice-index.d.ts +29 -0
  122. package/dist/sample/weighted-choice-index.d.ts.map +1 -0
  123. package/dist/sample/weighted-choice-index.js +62 -0
  124. package/dist/sample/weighted-choice-index.js.map +1 -0
  125. package/dist/sample/weighted-choice.d.ts +28 -0
  126. package/dist/sample/weighted-choice.d.ts.map +1 -0
  127. package/dist/sample/weighted-choice.js +31 -0
  128. package/dist/sample/weighted-choice.js.map +1 -0
  129. package/dist/select/minmax.d.ts +52 -0
  130. package/dist/select/minmax.d.ts.map +1 -0
  131. package/dist/select/minmax.js +109 -0
  132. package/dist/select/minmax.js.map +1 -0
  133. package/dist/select/{quickselect.d.ts → quickselect-in-place.d.ts} +2 -2
  134. package/dist/select/quickselect-in-place.d.ts.map +1 -0
  135. package/dist/select/{quickselect.js → quickselect-in-place.js} +3 -3
  136. package/dist/select/quickselect-in-place.js.map +1 -0
  137. package/dist/sequence/_matching-runs.d.ts +16 -0
  138. package/dist/sequence/_matching-runs.d.ts.map +1 -0
  139. package/dist/sequence/_matching-runs.js +178 -0
  140. package/dist/sequence/_matching-runs.js.map +1 -0
  141. package/dist/sequence/common-prefix-length.d.ts +26 -0
  142. package/dist/sequence/common-prefix-length.d.ts.map +1 -0
  143. package/dist/sequence/common-prefix-length.js +34 -0
  144. package/dist/sequence/common-prefix-length.js.map +1 -0
  145. package/dist/sequence/diff-sequences.d.ts +59 -0
  146. package/dist/sequence/diff-sequences.d.ts.map +1 -0
  147. package/dist/sequence/diff-sequences.js +67 -0
  148. package/dist/sequence/diff-sequences.js.map +1 -0
  149. package/dist/sequence/levenshtein-distance.d.ts +43 -0
  150. package/dist/sequence/levenshtein-distance.d.ts.map +1 -0
  151. package/dist/sequence/levenshtein-distance.js +102 -0
  152. package/dist/sequence/levenshtein-distance.js.map +1 -0
  153. package/dist/sequence/longest-common-subsequence.d.ts +28 -0
  154. package/dist/sequence/longest-common-subsequence.d.ts.map +1 -0
  155. package/dist/sequence/longest-common-subsequence.js +39 -0
  156. package/dist/sequence/longest-common-subsequence.js.map +1 -0
  157. package/dist/sequence/longest-increasing-subsequence.d.ts +28 -0
  158. package/dist/sequence/longest-increasing-subsequence.d.ts.map +1 -0
  159. package/dist/sequence/longest-increasing-subsequence.js +47 -0
  160. package/dist/sequence/longest-increasing-subsequence.js.map +1 -0
  161. package/dist/sort/_sort-primitives.d.ts +6 -0
  162. package/dist/sort/_sort-primitives.d.ts.map +1 -0
  163. package/dist/sort/_sort-primitives.js +18 -0
  164. package/dist/sort/_sort-primitives.js.map +1 -0
  165. package/dist/sort/counting-sort.d.ts +40 -0
  166. package/dist/sort/counting-sort.d.ts.map +1 -0
  167. package/dist/sort/counting-sort.js +51 -0
  168. package/dist/sort/counting-sort.js.map +1 -0
  169. package/dist/sort/merge-sort.d.ts +26 -0
  170. package/dist/sort/merge-sort.d.ts.map +1 -0
  171. package/dist/sort/merge-sort.js +73 -0
  172. package/dist/sort/merge-sort.js.map +1 -0
  173. package/dist/sort/quick-sort-in-place.d.ts +35 -0
  174. package/dist/sort/quick-sort-in-place.d.ts.map +1 -0
  175. package/dist/sort/quick-sort-in-place.js +98 -0
  176. package/dist/sort/quick-sort-in-place.js.map +1 -0
  177. package/dist/sort/radix-index-sorter.d.ts +57 -0
  178. package/dist/sort/radix-index-sorter.d.ts.map +1 -0
  179. package/dist/sort/radix-index-sorter.js +204 -0
  180. package/dist/sort/radix-index-sorter.js.map +1 -0
  181. package/dist/sort/rank.d.ts +64 -0
  182. package/dist/sort/rank.d.ts.map +1 -0
  183. package/dist/sort/rank.js +107 -0
  184. package/dist/sort/rank.js.map +1 -0
  185. package/dist/sort/sort-range.d.ts +48 -0
  186. package/dist/sort/sort-range.d.ts.map +1 -0
  187. package/dist/sort/sort-range.js +267 -0
  188. package/dist/sort/sort-range.js.map +1 -0
  189. package/package.json +6 -5
  190. package/dist/select/quickselect.d.ts.map +0 -1
  191. package/dist/select/quickselect.js.map +0 -1
@@ -0,0 +1,32 @@
1
+ import type { DefinedValue } from "@ac-kit/core";
2
+ /**
3
+ * The deepest node that is an ancestor of both `a` and `b` — where two paths
4
+ * through a tree diverged: the closest shared directory of two files, the merge
5
+ * base of two commits in a linear history, the innermost scope holding two
6
+ * declarations. A node counts as its own ancestor, so when `a` is an ancestor
7
+ * of `b`, the answer is `a`.
8
+ *
9
+ * Walks up from `a` marking every ancestor, then up from `b` until a marked
10
+ * one, so it needs only `parentOf` and no preprocessing; for many queries over
11
+ * one fixed tree, an Euler-tour or binary-lifting index answers each in O(1) or
12
+ * O(log h) instead. Iterative, so a deep tree cannot overflow the stack. Nodes
13
+ * are compared by identity (`Set` semantics, so `NaN` equals `NaN`).
14
+ *
15
+ * Time complexity: O(dₐ + d_b), the depths of the two nodes. Memory: the same.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * const parents = new Map([["b", "a"], ["c", "a"], ["d", "b"]]);
20
+ * lowestCommonAncestor("d", "c", (node) => parents.get(node)); // "a"
21
+ * ```;
22
+ *
23
+ * @param a - One node.
24
+ * @param b - The other node.
25
+ * @param parentOf - The parent of a node, or `undefined` for a root.
26
+ * @returns The lowest common ancestor, or `undefined` when the two nodes have
27
+ * none (they sit in different trees).
28
+ * @throws {RangeError} When following `parentOf` from `a` or from `b` comes
29
+ * back to a node already passed: the parents form a cycle, not a tree.
30
+ */
31
+ export declare function lowestCommonAncestor<TNode extends DefinedValue>(a: TNode, b: TNode, parentOf: (node: TNode) => TNode | undefined): TNode | undefined;
32
+ //# sourceMappingURL=lowest-common-ancestor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lowest-common-ancestor.d.ts","sourceRoot":"","sources":["../../src/graph/lowest-common-ancestor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,SAAS,YAAY,EAC9D,CAAC,EAAE,KAAK,EACR,CAAC,EAAE,KAAK,EACR,QAAQ,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,KAAK,GAAG,SAAS,GAC1C,KAAK,GAAG,SAAS,CA+BnB"}
@@ -0,0 +1,56 @@
1
+ /**
2
+ * The deepest node that is an ancestor of both `a` and `b` — where two paths
3
+ * through a tree diverged: the closest shared directory of two files, the merge
4
+ * base of two commits in a linear history, the innermost scope holding two
5
+ * declarations. A node counts as its own ancestor, so when `a` is an ancestor
6
+ * of `b`, the answer is `a`.
7
+ *
8
+ * Walks up from `a` marking every ancestor, then up from `b` until a marked
9
+ * one, so it needs only `parentOf` and no preprocessing; for many queries over
10
+ * one fixed tree, an Euler-tour or binary-lifting index answers each in O(1) or
11
+ * O(log h) instead. Iterative, so a deep tree cannot overflow the stack. Nodes
12
+ * are compared by identity (`Set` semantics, so `NaN` equals `NaN`).
13
+ *
14
+ * Time complexity: O(dₐ + d_b), the depths of the two nodes. Memory: the same.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * const parents = new Map([["b", "a"], ["c", "a"], ["d", "b"]]);
19
+ * lowestCommonAncestor("d", "c", (node) => parents.get(node)); // "a"
20
+ * ```;
21
+ *
22
+ * @param a - One node.
23
+ * @param b - The other node.
24
+ * @param parentOf - The parent of a node, or `undefined` for a root.
25
+ * @returns The lowest common ancestor, or `undefined` when the two nodes have
26
+ * none (they sit in different trees).
27
+ * @throws {RangeError} When following `parentOf` from `a` or from `b` comes
28
+ * back to a node already passed: the parents form a cycle, not a tree.
29
+ */
30
+ export function lowestCommonAncestor(a, b, parentOf) {
31
+ const ancestorsOfA = new Set();
32
+ for (let node = a; node !== undefined; node = parentOf(node)) {
33
+ if (ancestorsOfA.has(node)) {
34
+ throw cycleError_("a");
35
+ }
36
+ ancestorsOfA.add(node);
37
+ }
38
+ // Created on the first step off `a`'s line, so a query whose `b` lies on it
39
+ // allocates one set only.
40
+ let ancestorsOfB;
41
+ for (let node = b; node !== undefined; node = parentOf(node)) {
42
+ if (ancestorsOfA.has(node)) {
43
+ return node;
44
+ }
45
+ ancestorsOfB ??= new Set();
46
+ if (ancestorsOfB.has(node)) {
47
+ throw cycleError_("b");
48
+ }
49
+ ancestorsOfB.add(node);
50
+ }
51
+ return undefined;
52
+ }
53
+ function cycleError_(from) {
54
+ return new RangeError(`lowestCommonAncestor: the parents of ${from} form a cycle`);
55
+ }
56
+ //# sourceMappingURL=lowest-common-ancestor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lowest-common-ancestor.js","sourceRoot":"","sources":["../../src/graph/lowest-common-ancestor.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,UAAU,oBAAoB,CACnC,CAAQ,EACR,CAAQ,EACR,QAA4C;IAE5C,MAAM,YAAY,GAAG,IAAI,GAAG,EAAS,CAAC;IACtC,KACC,IAAI,IAAI,GAAsB,CAAC,EAC/B,IAAI,KAAK,SAAS,EAClB,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,EACpB,CAAC;QACF,IAAI,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YAC5B,MAAM,WAAW,CAAC,GAAG,CAAC,CAAC;QACxB,CAAC;QACD,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC;IAED,4EAA4E;IAC5E,0BAA0B;IAC1B,IAAI,YAAoC,CAAC;IACzC,KACC,IAAI,IAAI,GAAsB,CAAC,EAC/B,IAAI,KAAK,SAAS,EAClB,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,EACpB,CAAC;QACF,IAAI,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YAC5B,OAAO,IAAI,CAAC;QACb,CAAC;QACD,YAAY,KAAK,IAAI,GAAG,EAAE,CAAC;QAC3B,IAAI,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YAC5B,MAAM,WAAW,CAAC,GAAG,CAAC,CAAC;QACxB,CAAC;QACD,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC;IACD,OAAO,SAAS,CAAC;AAClB,CAAC;AAED,SAAS,WAAW,CAAC,IAAY;IAChC,OAAO,IAAI,UAAU,CACpB,wCAAwC,IAAI,eAAe,CAC3D,CAAC;AACH,CAAC"}
@@ -0,0 +1,51 @@
1
+ import type { WorkBudget } from "@ac-kit/core";
2
+ export type TopologicalSortOptions = {
3
+ /**
4
+ * Charged 1 for each root read (a repeated one included), 1 for each node
5
+ * reached and 1 for each successor read (each edge, repeated ones included),
6
+ * so a sort over an untrusted graph or an endless roots iterable can be
7
+ * capped. Whatever `spend` throws propagates unchanged.
8
+ */
9
+ budget?: WorkBudget;
10
+ };
11
+ /**
12
+ * Orders every node reachable from `roots` so that each node comes before all
13
+ * of its successors: the dependency order of a directed acyclic graph, for a
14
+ * build order, a task scheduler or module initialisation. With `childrenOf`
15
+ * returning a node's _dependencies_ rather than its dependents, reverse the
16
+ * result to list dependencies first.
17
+ *
18
+ * Kahn's algorithm: nodes with no remaining predecessor are emitted in the
19
+ * order they were first reached, then each emitted node releases its
20
+ * successors. So the order is deterministic, and nodes with no edge between
21
+ * them keep the order `roots` and `childrenOf` gave them. Iterative throughout,
22
+ * so a hostile deep graph cannot overflow the stack.
23
+ *
24
+ * Nodes are compared by identity (`Map` semantics, so `NaN` equals `NaN`).
25
+ * `childrenOf` is called once per node, and the iterable it returns is read
26
+ * once; parallel edges are allowed. A cycle is never answered with a partial
27
+ * order: a {@link GraphCycleError} names one.
28
+ *
29
+ * Time complexity: O(V + E) for the nodes and edges reached. Memory: O(V + E).
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * const dependents = new Map([
34
+ * ["core", ["data", "algo"]],
35
+ * ["data", ["algo"]],
36
+ * ]);
37
+ * topologicalSort(["core"], (name) => dependents.get(name));
38
+ * // ["core", "data", "algo"]
39
+ * ```;
40
+ *
41
+ * @param roots - Where to start; a root reached from an earlier one is not
42
+ * reached again. Pass every node to order a whole graph.
43
+ * @param childrenOf - The successors of a node, or `undefined` for none.
44
+ * @param options - A work budget.
45
+ * @returns Every reachable node once, each before its successors.
46
+ * @throws {GraphCycleError} When a cycle is reachable; its `cycle` lists one,
47
+ * in edge order.
48
+ * @throws Whatever `options.budget.spend`, `childrenOf` or an iterator throws.
49
+ */
50
+ export declare function topologicalSort<TNode>(roots: Iterable<TNode>, childrenOf: (node: TNode) => Iterable<TNode> | undefined, options?: TopologicalSortOptions): TNode[];
51
+ //# sourceMappingURL=topological-sort.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"topological-sort.d.ts","sourceRoot":"","sources":["../../src/graph/topological-sort.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAK/C,MAAM,MAAM,sBAAsB,GAAG;IACpC;;;;;OAKG;IACH,MAAM,CAAC,EAAE,UAAU,CAAC;CACpB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,wBAAgB,eAAe,CAAC,KAAK,EACpC,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC,EACtB,UAAU,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,QAAQ,CAAC,KAAK,CAAC,GAAG,SAAS,EACxD,OAAO,GAAE,sBAA2B,GAClC,KAAK,EAAE,CA6ET"}
@@ -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,CAAC;YACzB,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;YACjB,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC;YACrB,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YACzB,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAClB,CAAC;QACD,OAAO,KAAK,CAAC;IACd,CAAC,CAAC;IAEF,yEAAyE;IACzE,kCAAkC;IAClC,IAAI,QAAQ,GAAG,CAAC,CAAC;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G,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,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;YAC3C,MAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAE,CAAC,IAAI,CAAC,CAAC;YACjC,IAAI,IAAI,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAsC,CAAC;YAC/D,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;gBACxB,IAAI,GAAG,IAAI,GAAG,EAAE,CAAC;gBACjB,KAAK,CAAC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,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAC7B,MAA6B,EAC7B,KAAa;IAEb,MAAM,IAAI,GAAgB,EAAE,CAAC;IAC7B,MAAM,IAAI,GAAc,EAAE,CAAC;IAC3B,MAAM,OAAO,GAAmC,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;IACnE,OAAO,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC3B,MAAM,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAE,CAAC,IAAI,EAAE,CAAC;QACjD,IAAI,IAAI,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;YACxB,OAAO,CAAC,GAAG,EAAE,CAAC;YACd,IAAI,CAAC,GAAG,EAAE,CAAC;YACX,SAAS;QACV,CAAC;QACD,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC;QAChC,IAAI,OAAO,CAAC,MAAM,KAAK,KAAK,EAAE,CAAC;YAC9B,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC;QAClC,CAAC;aAAM,CAAC;YACP,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YACf,OAAO,CAAC,IAAI,CAAE,KAA+B,CAAC,OAAO,EAAE,CAAC,CAAC;QAC1D,CAAC;IACF,CAAC;IACD,OAAO,IAAI,CAAC;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"}