@ac-kit/algo 0.1.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 +7 -0
- package/LICENSE +28 -0
- package/README.md +70 -0
- package/dist/distribute/spread-evenly.d.ts +17 -0
- package/dist/distribute/spread-evenly.d.ts.map +1 -0
- package/dist/distribute/spread-evenly.js +41 -0
- package/dist/distribute/spread-evenly.js.map +1 -0
- package/dist/graph/_search-by-priority.d.ts +42 -0
- package/dist/graph/_search-by-priority.d.ts.map +1 -0
- package/dist/graph/_search-by-priority.js +56 -0
- package/dist/graph/_search-by-priority.js.map +1 -0
- package/dist/graph/a-star.d.ts +19 -0
- package/dist/graph/a-star.d.ts.map +1 -0
- package/dist/graph/a-star.js +20 -0
- package/dist/graph/a-star.js.map +1 -0
- package/dist/graph/best-first-search.d.ts +25 -0
- package/dist/graph/best-first-search.d.ts.map +1 -0
- package/dist/graph/best-first-search.js +26 -0
- package/dist/graph/best-first-search.js.map +1 -0
- package/dist/graph/dfs.d.ts +46 -0
- package/dist/graph/dfs.d.ts.map +1 -0
- package/dist/graph/dfs.js +49 -0
- package/dist/graph/dfs.js.map +1 -0
- package/dist/group/_partition-into.d.ts +3 -0
- package/dist/group/_partition-into.d.ts.map +1 -0
- package/dist/group/_partition-into.js +11 -0
- package/dist/group/_partition-into.js.map +1 -0
- package/dist/group/group-by.d.ts +33 -0
- package/dist/group/group-by.d.ts.map +1 -0
- package/dist/group/group-by.js +51 -0
- package/dist/group/group-by.js.map +1 -0
- package/dist/group/partition-indices.d.ts +11 -0
- package/dist/group/partition-indices.d.ts.map +1 -0
- package/dist/group/partition-indices.js +13 -0
- package/dist/group/partition-indices.js.map +1 -0
- package/dist/group/partition.d.ts +10 -0
- package/dist/group/partition.d.ts.map +1 -0
- package/dist/group/partition.js +12 -0
- package/dist/group/partition.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/interval/is-in-sorted-intervals.d.ts +17 -0
- package/dist/interval/is-in-sorted-intervals.d.ts.map +1 -0
- package/dist/interval/is-in-sorted-intervals.js +21 -0
- package/dist/interval/is-in-sorted-intervals.js.map +1 -0
- package/dist/optimize/backtrack.d.ts +38 -0
- package/dist/optimize/backtrack.d.ts.map +1 -0
- package/dist/optimize/backtrack.js +56 -0
- package/dist/optimize/backtrack.js.map +1 -0
- package/dist/optimize/beam-search.d.ts +34 -0
- package/dist/optimize/beam-search.d.ts.map +1 -0
- package/dist/optimize/beam-search.js +42 -0
- package/dist/optimize/beam-search.js.map +1 -0
- package/dist/ordered/_binary-search.d.ts +13 -0
- package/dist/ordered/_binary-search.d.ts.map +1 -0
- package/dist/ordered/_binary-search.js +24 -0
- package/dist/ordered/_binary-search.js.map +1 -0
- package/dist/ordered/bisect-center.d.ts +18 -0
- package/dist/ordered/bisect-center.d.ts.map +1 -0
- package/dist/ordered/bisect-center.js +29 -0
- package/dist/ordered/bisect-center.js.map +1 -0
- package/dist/ordered/bisect-left.d.ts +15 -0
- package/dist/ordered/bisect-left.d.ts.map +1 -0
- package/dist/ordered/bisect-left.js +18 -0
- package/dist/ordered/bisect-left.js.map +1 -0
- package/dist/ordered/bisect-right.d.ts +15 -0
- package/dist/ordered/bisect-right.d.ts.map +1 -0
- package/dist/ordered/bisect-right.js +18 -0
- package/dist/ordered/bisect-right.js.map +1 -0
- package/dist/ordered/bisect.d.ts +2 -0
- package/dist/ordered/bisect.d.ts.map +1 -0
- package/dist/ordered/bisect.js +2 -0
- package/dist/ordered/bisect.js.map +1 -0
- package/dist/ordered/create-bisector.d.ts +30 -0
- package/dist/ordered/create-bisector.d.ts.map +1 -0
- package/dist/ordered/create-bisector.js +32 -0
- package/dist/ordered/create-bisector.js.map +1 -0
- package/dist/sample/shuffle.d.ts +31 -0
- package/dist/sample/shuffle.d.ts.map +1 -0
- package/dist/sample/shuffle.js +33 -0
- package/dist/sample/shuffle.js.map +1 -0
- package/dist/select/_extreme-by.d.ts +11 -0
- package/dist/select/_extreme-by.d.ts.map +1 -0
- package/dist/select/_extreme-by.js +23 -0
- package/dist/select/_extreme-by.js.map +1 -0
- package/dist/select/max-by.d.ts +13 -0
- package/dist/select/max-by.d.ts.map +1 -0
- package/dist/select/max-by.js +15 -0
- package/dist/select/max-by.js.map +1 -0
- package/dist/select/min-by.d.ts +13 -0
- package/dist/select/min-by.d.ts.map +1 -0
- package/dist/select/min-by.js +15 -0
- package/dist/select/min-by.js.map +1 -0
- package/dist/select/quickselect.d.ts +15 -0
- package/dist/select/quickselect.d.ts.map +1 -0
- package/dist/select/quickselect.js +47 -0
- package/dist/select/quickselect.js.map +1 -0
- package/dist/select/select-at.d.ts +11 -0
- package/dist/select/select-at.d.ts.map +1 -0
- package/dist/select/select-at.js +13 -0
- package/dist/select/select-at.js.map +1 -0
- package/dist/set/difference.d.ts +13 -0
- package/dist/set/difference.d.ts.map +1 -0
- package/dist/set/difference.js +20 -0
- package/dist/set/difference.js.map +1 -0
- package/dist/set/intersection.d.ts +16 -0
- package/dist/set/intersection.d.ts.map +1 -0
- package/dist/set/intersection.js +43 -0
- package/dist/set/intersection.js.map +1 -0
- package/dist/set/is-disjoint-from.d.ts +13 -0
- package/dist/set/is-disjoint-from.d.ts.map +1 -0
- package/dist/set/is-disjoint-from.js +20 -0
- package/dist/set/is-disjoint-from.js.map +1 -0
- package/dist/set/is-subset-of.d.ts +13 -0
- package/dist/set/is-subset-of.d.ts.map +1 -0
- package/dist/set/is-subset-of.js +20 -0
- package/dist/set/is-subset-of.js.map +1 -0
- package/dist/set/is-superset-of.d.ts +14 -0
- package/dist/set/is-superset-of.d.ts.map +1 -0
- package/dist/set/is-superset-of.js +21 -0
- package/dist/set/is-superset-of.js.map +1 -0
- package/dist/set/symmetric-difference.d.ts +13 -0
- package/dist/set/symmetric-difference.d.ts.map +1 -0
- package/dist/set/symmetric-difference.js +20 -0
- package/dist/set/symmetric-difference.js.map +1 -0
- package/dist/set/union.d.ts +13 -0
- package/dist/set/union.d.ts.map +1 -0
- package/dist/set/union.js +28 -0
- package/dist/set/union.js.map +1 -0
- package/dist/set/uniq-by.d.ts +25 -0
- package/dist/set/uniq-by.d.ts.map +1 -0
- package/dist/set/uniq-by.js +48 -0
- package/dist/set/uniq-by.js.map +1 -0
- package/dist/set/uniq.d.ts +14 -0
- package/dist/set/uniq.d.ts.map +1 -0
- package/dist/set/uniq.js +34 -0
- package/dist/set/uniq.js.map +1 -0
- package/dist/sort/radix-sort.d.ts +14 -0
- package/dist/sort/radix-sort.d.ts.map +1 -0
- package/dist/sort/radix-sort.js +58 -0
- package/dist/sort/radix-sort.js.map +1 -0
- package/package.json +64 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2026 Anthony Champagne and contributors
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# @ac-kit/algo
|
|
2
|
+
|
|
3
|
+
Classic algorithms that work on **your** types, not on numbers.
|
|
4
|
+
|
|
5
|
+
Nothing here assumes what an element is. Ordering, equality, scoring and
|
|
6
|
+
neighbour expansion are all supplied as callbacks, so the same `quickselect`
|
|
7
|
+
sorts your invoices by due date and your pixels by luminance, and the same graph
|
|
8
|
+
search walks your dependency tree and your maze.
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { groupBy, quickselect, uniqBy } from "@ac-kit/algo";
|
|
12
|
+
|
|
13
|
+
// Order statistics without paying for a full sort — O(n) average.
|
|
14
|
+
const median = quickselect(
|
|
15
|
+
[...durations],
|
|
16
|
+
durations.length >> 1,
|
|
17
|
+
(a, b) => a - b,
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
// Group by any derived key; add more accessors to nest another level.
|
|
21
|
+
const byOwner = groupBy(tasks, [(task) => task.ownerId]);
|
|
22
|
+
|
|
23
|
+
// Deduplicate lazily — nothing is materialized until you pull.
|
|
24
|
+
const distinct = [...uniqBy(events, (event) => event.correlationId)];
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## What's included
|
|
28
|
+
|
|
29
|
+
- **Graph search** — `dfs` with enter/exit hooks and skip/stop control, `aStar`
|
|
30
|
+
(which is Dijkstra's algorithm when you give it no heuristic) and greedy
|
|
31
|
+
`bestFirstSearch`, all over a `childrenOf`/`neighboursOf` callback rather than
|
|
32
|
+
any particular graph type.
|
|
33
|
+
- **State-space search** — `beamSearch` for a width-bounded exploration, and
|
|
34
|
+
`backtrack` for constraint search that undoes its own moves on the way back
|
|
35
|
+
out of a dead end.
|
|
36
|
+
- **Binary search** — `bisect`, `bisectLeft`, `bisectRight`, `bisectCenter` and
|
|
37
|
+
a reusable `createBisector` bound to one accessor, plus membership testing
|
|
38
|
+
against sorted, disjoint intervals.
|
|
39
|
+
- **Selection** — `quickselect` for order statistics, `minBy`/`maxBy` for the
|
|
40
|
+
extreme item by a derived key, `selectAt` for picking positions.
|
|
41
|
+
- **Sorting** — `radixSort`, a stable, comparator-free sort over an integer key.
|
|
42
|
+
- **Grouping** — `groupBy` (nesting one `Map` level per accessor), `partition`,
|
|
43
|
+
`partitionIndices`, `uniq` and `uniqBy`.
|
|
44
|
+
- **Set algebra** — `union`, `intersection`, `difference`,
|
|
45
|
+
`symmetricDifference`, and the `isSubsetOf` / `isSupersetOf` /
|
|
46
|
+
`isDisjointFrom` predicates — over any iterables.
|
|
47
|
+
- **Shuffling** — `shuffle` (Fisher–Yates or Sattolo) with the random source
|
|
48
|
+
injectable, so results can be made reproducible, and `spreadEvenly` to
|
|
49
|
+
interleave two collections at evenly spaced positions.
|
|
50
|
+
|
|
51
|
+
## What's not included
|
|
52
|
+
|
|
53
|
+
This package deliberately stops at three boundaries, so if you are looking for
|
|
54
|
+
one of these you want a different one:
|
|
55
|
+
|
|
56
|
+
- **Anything with a security claim** — message digests, ciphers, signatures, key
|
|
57
|
+
derivation. Use the `@ac-kit/crypto-*` packages. Non-cryptographic hashes and
|
|
58
|
+
checksums, built for distribution and speed and unsafe against an adversary,
|
|
59
|
+
are `@ac-kit/noncrypto-hash`.
|
|
60
|
+
- **Anything whose subject is a number** — statistics, linear algebra, geometry,
|
|
61
|
+
numeric domains. Use the `@ac-kit/math-*` packages instead.
|
|
62
|
+
- **Anything you build once and query repeatedly** — heaps, tries, graphs, sets
|
|
63
|
+
with custom equality, multi-pattern matchers, alias tables. Everything here is
|
|
64
|
+
one-shot: give it an input, get a result. Use `@ac-kit/data` instead.
|
|
65
|
+
|
|
66
|
+
Portable: no Node.js built-ins, so it runs unchanged in browsers, Deno, Bun and
|
|
67
|
+
edge runtimes. It depends only on `@ac-kit/core` and `@ac-kit/data`.
|
|
68
|
+
|
|
69
|
+
See the [generated API docs](https://anthochamp.github.io/node-essentials/api/algo/)
|
|
70
|
+
for the full reference.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interleaves `special` items into `rest` at evenly-spaced positions.
|
|
3
|
+
*
|
|
4
|
+
* Each special item is placed at the center of an equal-length slot so that
|
|
5
|
+
* special items are spread as uniformly as possible through the combined
|
|
6
|
+
* sequence — the same reasoning as Bresenham's line algorithm, distributing `k`
|
|
7
|
+
* items across `n` positions without accumulating drift.
|
|
8
|
+
*
|
|
9
|
+
* Time complexity: O(n) in the combined length. Both inputs are read in full
|
|
10
|
+
* before the first item is yielded, since the spacing depends on both counts.
|
|
11
|
+
*
|
|
12
|
+
* @param special - Items to distribute evenly.
|
|
13
|
+
* @param rest - Items to fill the gaps.
|
|
14
|
+
* @returns An iterator over the combined sequence.
|
|
15
|
+
*/
|
|
16
|
+
export declare function spreadEvenly<T>(special: readonly T[], rest: readonly T[]): IterableIterator<T>;
|
|
17
|
+
//# sourceMappingURL=spread-evenly.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spread-evenly.d.ts","sourceRoot":"","sources":["../../src/distribute/spread-evenly.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;GAcG;AACH,wBAAiB,YAAY,CAAC,CAAC,EAC9B,OAAO,EAAE,SAAS,CAAC,EAAE,EACrB,IAAI,EAAE,SAAS,CAAC,EAAE,GAChB,gBAAgB,CAAC,CAAC,CAAC,CAyBrB"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { FastSet } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Interleaves `special` items into `rest` at evenly-spaced positions.
|
|
4
|
+
*
|
|
5
|
+
* Each special item is placed at the center of an equal-length slot so that
|
|
6
|
+
* special items are spread as uniformly as possible through the combined
|
|
7
|
+
* sequence — the same reasoning as Bresenham's line algorithm, distributing `k`
|
|
8
|
+
* items across `n` positions without accumulating drift.
|
|
9
|
+
*
|
|
10
|
+
* Time complexity: O(n) in the combined length. Both inputs are read in full
|
|
11
|
+
* before the first item is yielded, since the spacing depends on both counts.
|
|
12
|
+
*
|
|
13
|
+
* @param special - Items to distribute evenly.
|
|
14
|
+
* @param rest - Items to fill the gaps.
|
|
15
|
+
* @returns An iterator over the combined sequence.
|
|
16
|
+
*/
|
|
17
|
+
export function* spreadEvenly(special, rest) {
|
|
18
|
+
if (special.length === 0) {
|
|
19
|
+
yield* rest;
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const total = special.length + rest.length;
|
|
23
|
+
const slots = new FastSet();
|
|
24
|
+
for (let k = 0; k < special.length; k++) {
|
|
25
|
+
slots.add(Math.floor(((k + 0.5) * total) / special.length));
|
|
26
|
+
}
|
|
27
|
+
let si = 0;
|
|
28
|
+
let ri = 0;
|
|
29
|
+
for (let pos = 0; pos < total; pos++) {
|
|
30
|
+
if (slots.has(pos) && si < special.length) {
|
|
31
|
+
yield special[si++];
|
|
32
|
+
}
|
|
33
|
+
else if (ri < rest.length) {
|
|
34
|
+
yield rest[ri++];
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
yield special[si++];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
//# source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O,CAAC,EAAE,EAAE,CAAE,CAAC;QACtB,CAAC;IACF,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { DefinedValue } from "@ac-kit/core";
|
|
2
|
+
/** How a caller's graph is expanded, weighted and tested. */
|
|
3
|
+
export type GraphSearchTraits<TNode> = {
|
|
4
|
+
/** Returns the nodes reachable in one step from `node`. */
|
|
5
|
+
neighboursOf: (node: TNode) => Iterable<TNode>;
|
|
6
|
+
/** Whether `node` is the node being searched for. */
|
|
7
|
+
isGoal: (node: TNode) => boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Cost of the edge from `from` to `to`. Must be non-negative — a negative
|
|
10
|
+
* edge invalidates the "first settled is cheapest" property every algorithm
|
|
11
|
+
* here relies on. Defaults to `1`, which makes cost equal hop count.
|
|
12
|
+
*/
|
|
13
|
+
costOf?: (from: TNode, to: TNode) => number;
|
|
14
|
+
/**
|
|
15
|
+
* Estimated remaining cost from `node` to a goal. Must never overestimate, or
|
|
16
|
+
* the path found is not guaranteed to be the cheapest. Defaults to `0`, which
|
|
17
|
+
* reduces {@link aStar} to Dijkstra's algorithm exactly.
|
|
18
|
+
*/
|
|
19
|
+
heuristicOf?: (node: TNode) => number;
|
|
20
|
+
/**
|
|
21
|
+
* Identity for a node, so two structurally equal nodes are recognised as one.
|
|
22
|
+
* Defaults to the node itself, which is right when nodes are interned or
|
|
23
|
+
* primitive.
|
|
24
|
+
*/
|
|
25
|
+
keyOf?: (node: TNode) => unknown;
|
|
26
|
+
};
|
|
27
|
+
/** A route found by a graph search, and what it costs to walk it. */
|
|
28
|
+
export type GraphPath<TNode> = {
|
|
29
|
+
/** The nodes from the start to the goal, both included. */
|
|
30
|
+
path: readonly TNode[];
|
|
31
|
+
/** Total of `costOf` across the path's edges. */
|
|
32
|
+
cost: number;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Best-first graph search, ordered by whatever `priorityOf` makes of the cost
|
|
36
|
+
* so far and the estimate remaining. Lower priority is explored first.
|
|
37
|
+
*
|
|
38
|
+
* Time complexity: O((V + E) log V) — every edge can queue one entry, and a
|
|
39
|
+
* heap operation is logarithmic.
|
|
40
|
+
*/
|
|
41
|
+
export declare function searchByPriority<TNode extends DefinedValue>(start: TNode, traits: GraphSearchTraits<TNode>, priorityOf: (gScore: number, heuristic: number) => number): GraphPath<TNode> | null;
|
|
42
|
+
//# sourceMappingURL=_search-by-priority.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_search-by-priority.d.ts","sourceRoot":"","sources":["../../src/graph/_search-by-priority.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAG5C,6DAA6D;AAC7D,MAAM,MAAM,iBAAiB,CAAC,KAAK,IAAI;IACtC,2DAA2D;IAC3D,YAAY,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,QAAQ,CAAC,KAAK,CAAC,CAAC;IAE/C,qDAAqD;IACrD,MAAM,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,OAAO,CAAC;IAEjC;;;;OAIG;IACH,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,EAAE,KAAK,KAAK,MAAM,CAAC;IAE5C;;;;OAIG;IACH,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,MAAM,CAAC;IAEtC;;;;OAIG;IACH,KAAK,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,OAAO,CAAC;CACjC,CAAC;AAEF,qEAAqE;AACrE,MAAM,MAAM,SAAS,CAAC,KAAK,IAAI;IAC9B,2DAA2D;IAC3D,IAAI,EAAE,SAAS,KAAK,EAAE,CAAC;IAEvB,iDAAiD;IACjD,IAAI,EAAE,MAAM,CAAC;CACb,CAAC;AAQF;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,SAAS,YAAY,EAC1D,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,iBAAiB,CAAC,KAAK,CAAC,EAChC,UAAU,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,KAAK,MAAM,GACvD,SAAS,CAAC,KAAK,CAAC,GAAG,IAAI,CAoDzB"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { PriorityQueue } from "@ac-kit/data";
|
|
2
|
+
/**
|
|
3
|
+
* Best-first graph search, ordered by whatever `priorityOf` makes of the cost
|
|
4
|
+
* so far and the estimate remaining. Lower priority is explored first.
|
|
5
|
+
*
|
|
6
|
+
* Time complexity: O((V + E) log V) — every edge can queue one entry, and a
|
|
7
|
+
* heap operation is logarithmic.
|
|
8
|
+
*/
|
|
9
|
+
export function searchByPriority(start, traits, priorityOf) {
|
|
10
|
+
const { neighboursOf, isGoal, costOf = () => 1, heuristicOf = () => 0, keyOf = (node) => node, } = traits;
|
|
11
|
+
const bestCost = new Map([[keyOf(start), 0]]);
|
|
12
|
+
const cameFrom = new Map();
|
|
13
|
+
const frontier = new PriorityQueue();
|
|
14
|
+
frontier.insert(priorityOf(0, heuristicOf(start)), {
|
|
15
|
+
node: start,
|
|
16
|
+
gScore: 0,
|
|
17
|
+
});
|
|
18
|
+
while (frontier.count() > 0) {
|
|
19
|
+
const entry = frontier.extract();
|
|
20
|
+
const key = keyOf(entry.node);
|
|
21
|
+
// A cheaper route to this node was queued after this entry was.
|
|
22
|
+
if (entry.gScore > (bestCost.get(key) ?? Infinity)) {
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
if (isGoal(entry.node)) {
|
|
26
|
+
return {
|
|
27
|
+
path: reconstructPath_(cameFrom, keyOf, entry.node),
|
|
28
|
+
cost: entry.gScore,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
for (const next of neighboursOf(entry.node)) {
|
|
32
|
+
const nextKey = keyOf(next);
|
|
33
|
+
const gScore = entry.gScore + costOf(entry.node, next);
|
|
34
|
+
if (gScore >= (bestCost.get(nextKey) ?? Infinity)) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
bestCost.set(nextKey, gScore);
|
|
38
|
+
cameFrom.set(nextKey, entry.node);
|
|
39
|
+
frontier.insert(priorityOf(gScore, heuristicOf(next)), {
|
|
40
|
+
node: next,
|
|
41
|
+
gScore,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
function reconstructPath_(cameFrom, keyOf, goal) {
|
|
48
|
+
const path = [goal];
|
|
49
|
+
let previous = cameFrom.get(keyOf(goal));
|
|
50
|
+
while (previous !== undefined) {
|
|
51
|
+
path.push(previous);
|
|
52
|
+
previous = cameFrom.get(keyOf(previous));
|
|
53
|
+
}
|
|
54
|
+
return path.reverse();
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=_search-by-priority.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_search-by-priority.js","sourceRoot":"","sources":["../../src/graph/_search-by-priority.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AA+C7C;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAC/B,KAAY,EACZ,MAAgC,EAChC,UAAyD;IAEzD,MAAM,EACL,YAAY,EACZ,MAAM,EACN,MAAM,GAAG,GAAG,EAAE,CAAC,CAAC,EAChB,WAAW,GAAG,GAAG,EAAE,CAAC,CAAC,EACrB,KAAK,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,GACtB,GAAG,MAAM,CAAC;IAEX,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAkB,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAC/D,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAkB,CAAC;IAE3C,MAAM,QAAQ,GAAG,IAAI,aAAa,EAA4B,CAAC;IAC/D,QAAQ,CAAC,MA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AW,CAAC,IAAI,CAAC,CAAC,EAAE;gBACtD,IAAI,EAAE,IAAI;gBACV,MAAM;aACN,CAAC,CAAC;QACJ,CAAC;IACF,CAAC;IAED,OAAO,IAAI,CAAC;AACb,CAAC;AAED,SAAS,gBAAgB,CACxB,QAAqC,EACrC,KAA+B,EAC/B,IAAW;IAEX,MAAM,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC;IAEpB,IAAI,QAAQ,GAAG,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;IACzC,OAAO,QAAQ,KAAK,SAAS,EAAE,CAAC;QAC/B,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QACpB,QAAQ,GAAG,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC1C,CAAC;IAED,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;AACvB,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { DefinedValue } from "@ac-kit/core";
|
|
2
|
+
import { type GraphPath, type GraphSearchTraits } from "./_search-by-priority.js";
|
|
3
|
+
/**
|
|
4
|
+
* Finds the cheapest path from `start` to a goal, exploring by cost so far plus
|
|
5
|
+
* estimated cost remaining.
|
|
6
|
+
*
|
|
7
|
+
* With no `heuristicOf` this **is** Dijkstra's algorithm — the estimate is
|
|
8
|
+
* uniformly zero, so exploration is purely by cost so far. A heuristic that
|
|
9
|
+
* never overestimates keeps the result optimal while cutting how much of the
|
|
10
|
+
* graph gets visited; one that overestimates trades that guarantee for speed.
|
|
11
|
+
*
|
|
12
|
+
* Time complexity: O((V + E) log V), and `costOf` must be non-negative.
|
|
13
|
+
*
|
|
14
|
+
* @param start - The node to search from.
|
|
15
|
+
* @param traits - How to expand, weight and test a node.
|
|
16
|
+
* @returns The cheapest path and its cost, or `null` if no goal is reachable.
|
|
17
|
+
*/
|
|
18
|
+
export declare function aStar<TNode extends DefinedValue>(start: TNode, traits: GraphSearchTraits<TNode>): GraphPath<TNode> | null;
|
|
19
|
+
//# sourceMappingURL=a-star.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"a-star.d.ts","sourceRoot":"","sources":["../../src/graph/a-star.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEjD,OAAO,EACN,KAAK,SAAS,EACd,KAAK,iBAAiB,EAEtB,MAAM,0BAA0B,CAAC;AAElC;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,KAAK,CAAC,KAAK,SAAS,YAAY,EAC/C,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,iBAAiB,CAAC,KAAK,CAAC,GAC9B,SAAS,CAAC,KAAK,CAAC,GAAG,IAAI,CAMzB"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { searchByPriority, } from "./_search-by-priority.js";
|
|
2
|
+
/**
|
|
3
|
+
* Finds the cheapest path from `start` to a goal, exploring by cost so far plus
|
|
4
|
+
* estimated cost remaining.
|
|
5
|
+
*
|
|
6
|
+
* With no `heuristicOf` this **is** Dijkstra's algorithm — the estimate is
|
|
7
|
+
* uniformly zero, so exploration is purely by cost so far. A heuristic that
|
|
8
|
+
* never overestimates keeps the result optimal while cutting how much of the
|
|
9
|
+
* graph gets visited; one that overestimates trades that guarantee for speed.
|
|
10
|
+
*
|
|
11
|
+
* Time complexity: O((V + E) log V), and `costOf` must be non-negative.
|
|
12
|
+
*
|
|
13
|
+
* @param start - The node to search from.
|
|
14
|
+
* @param traits - How to expand, weight and test a node.
|
|
15
|
+
* @returns The cheapest path and its cost, or `null` if no goal is reachable.
|
|
16
|
+
*/
|
|
17
|
+
export function aStar(start, traits) {
|
|
18
|
+
return searchByPriority(start, traits, (gScore, heuristic) => gScore + heuristic);
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=a-star.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"a-star.js","sourceRoot":"","sources":["../../src/graph/a-star.ts"],"names":[],"mappings":"AAEA,OAAO,EAGN,gBAAgB,GAChB,MAAM,0BAA0B,CAAC;AAElC;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,KAAK,CACpB,KAAY,EACZ,MAAgC;IAEhC,OAAO,gBAAgB,CACtB,KAAK,EACL,MAAM,EACN,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE,CAAC,MAAM,GAAG,SAAS,CACzC,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { DefinedValue } from "@ac-kit/core";
|
|
2
|
+
import { type GraphPath, type GraphSearchTraits } from "./_search-by-priority.js";
|
|
3
|
+
/**
|
|
4
|
+
* Finds a path from `start` to a goal, always expanding whichever frontier node
|
|
5
|
+
* _looks_ closest to one — greedy best-first search.
|
|
6
|
+
*
|
|
7
|
+
* Ordering is by `heuristicOf` alone, ignoring the cost already paid. That
|
|
8
|
+
* reaches a goal far sooner than {@link aStar} on a graph where the heuristic
|
|
9
|
+
* points the right way, and returns a **worse than optimal** path when it does
|
|
10
|
+
* not. Use {@link aStar} whenever the cheapest path is what matters; use this
|
|
11
|
+
* when any path, found fast, is enough.
|
|
12
|
+
*
|
|
13
|
+
* The returned `cost` is still the true cost of the path walked, so a caller
|
|
14
|
+
* can measure how far off it landed.
|
|
15
|
+
*
|
|
16
|
+
* Time complexity: O((V + E) log V), and `costOf` must be non-negative.
|
|
17
|
+
*
|
|
18
|
+
* @param start - The node to search from.
|
|
19
|
+
* @param traits - How to expand, weight and test a node. `heuristicOf` is what
|
|
20
|
+
* drives this search; leaving it at its `0` default degenerates to an
|
|
21
|
+
* arbitrary-order traversal.
|
|
22
|
+
* @returns The path found and its cost, or `null` if no goal is reachable.
|
|
23
|
+
*/
|
|
24
|
+
export declare function bestFirstSearch<TNode extends DefinedValue>(start: TNode, traits: GraphSearchTraits<TNode>): GraphPath<TNode> | null;
|
|
25
|
+
//# sourceMappingURL=best-first-search.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"best-first-search.d.ts","sourceRoot":"","sources":["../../src/graph/best-first-search.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEjD,OAAO,EACN,KAAK,SAAS,EACd,KAAK,iBAAiB,EAEtB,MAAM,0BAA0B,CAAC;AAElC;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,eAAe,CAAC,KAAK,SAAS,YAAY,EACzD,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,iBAAiB,CAAC,KAAK,CAAC,GAC9B,SAAS,CAAC,KAAK,CAAC,GAAG,IAAI,CAEzB"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { searchByPriority, } from "./_search-by-priority.js";
|
|
2
|
+
/**
|
|
3
|
+
* Finds a path from `start` to a goal, always expanding whichever frontier node
|
|
4
|
+
* _looks_ closest to one — greedy best-first search.
|
|
5
|
+
*
|
|
6
|
+
* Ordering is by `heuristicOf` alone, ignoring the cost already paid. That
|
|
7
|
+
* reaches a goal far sooner than {@link aStar} on a graph where the heuristic
|
|
8
|
+
* points the right way, and returns a **worse than optimal** path when it does
|
|
9
|
+
* not. Use {@link aStar} whenever the cheapest path is what matters; use this
|
|
10
|
+
* when any path, found fast, is enough.
|
|
11
|
+
*
|
|
12
|
+
* The returned `cost` is still the true cost of the path walked, so a caller
|
|
13
|
+
* can measure how far off it landed.
|
|
14
|
+
*
|
|
15
|
+
* Time complexity: O((V + E) log V), and `costOf` must be non-negative.
|
|
16
|
+
*
|
|
17
|
+
* @param start - The node to search from.
|
|
18
|
+
* @param traits - How to expand, weight and test a node. `heuristicOf` is what
|
|
19
|
+
* drives this search; leaving it at its `0` default degenerates to an
|
|
20
|
+
* arbitrary-order traversal.
|
|
21
|
+
* @returns The path found and its cost, or `null` if no goal is reachable.
|
|
22
|
+
*/
|
|
23
|
+
export function bestFirstSearch(start, traits) {
|
|
24
|
+
return searchByPriority(start, traits, (_gScore, heuristic) => heuristic);
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=best-first-search.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"best-first-search.js","sourceRoot":"","sources":["../../src/graph/best-first-search.ts"],"names":[],"mappings":"AAEA,OAAO,EAGN,gBAAgB,GAChB,MAAM,0BAA0B,CAAC;AAElC;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,eAAe,CAC9B,KAAY,EACZ,MAAgC;IAEhC,OAAO,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC,OAAO,EAAE,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,CAAC;AAC3E,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/** Continue walking the graph, visiting children of this node. */
|
|
2
|
+
export declare const DfsWalkContinue: unique symbol;
|
|
3
|
+
/** Skip visiting children of this node, but continue walking the graph. */
|
|
4
|
+
export declare const DfsWalkSkipChildren: unique symbol;
|
|
5
|
+
/** Stop walking the graph immediately, returning `true` from {@link dfs}. */
|
|
6
|
+
export declare const DfsWalkStop: unique symbol;
|
|
7
|
+
/** The return value of {@link DfsTraits.onEnter} to control the walk. */
|
|
8
|
+
export type DfsWalkControl = typeof DfsWalkContinue | typeof DfsWalkSkipChildren | typeof DfsWalkStop;
|
|
9
|
+
export type DfsTraits<TNode> = {
|
|
10
|
+
/** Returns the successors of a node, or `undefined`/empty for a leaf. */
|
|
11
|
+
childrenOf: (node: TNode) => Iterable<TNode> | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* Called on entering a node, before its children. Returning `undefined` is
|
|
14
|
+
* treated as {@link DfsWalkContinue}.
|
|
15
|
+
*/
|
|
16
|
+
onEnter?: (node: TNode) => DfsWalkControl | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Called on leaving a node, after its children — including when the walk is
|
|
19
|
+
* stopping, so a caller undoing state applied in `onEnter` always gets the
|
|
20
|
+
* matching call.
|
|
21
|
+
*/
|
|
22
|
+
onExit?: (node: TNode) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Marks nodes already visited, for a cyclic graph. Omit for a tree, where no
|
|
25
|
+
* node is reachable twice and the bookkeeping would be wasted.
|
|
26
|
+
*/
|
|
27
|
+
seen?: Set<TNode>;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Walks a graph or tree of unknown shape depth-first, in pre-order.
|
|
31
|
+
*
|
|
32
|
+
* The structure is not required to conform to any shared node interface:
|
|
33
|
+
* `childrenOf` is the only thing that has to know how a given `TNode` exposes
|
|
34
|
+
* its successors, which lets one generic walker drive discriminated unions
|
|
35
|
+
* whose node kinds carry children under entirely different field names.
|
|
36
|
+
*
|
|
37
|
+
* Time complexity: O(V + E). Without {@link DfsTraits.seen}, a cyclic graph does
|
|
38
|
+
* not terminate — pass a `Set` whenever a cycle is possible.
|
|
39
|
+
*
|
|
40
|
+
* @param root - The node to start walking from.
|
|
41
|
+
* @param traits - How to traverse, and what to do at each node.
|
|
42
|
+
* @returns `true` if the walk was stopped early via {@link DfsWalkStop},
|
|
43
|
+
* `false` if it ran to completion.
|
|
44
|
+
*/
|
|
45
|
+
export declare function dfs<TNode>(root: TNode, traits: DfsTraits<TNode>): boolean;
|
|
46
|
+
//# sourceMappingURL=dfs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dfs.d.ts","sourceRoot":"","sources":["../../src/graph/dfs.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,eAAO,MAAM,eAAe,eAA4B,CAAC;AACzD,2EAA2E;AAC3E,eAAO,MAAM,mBAAmB,eAAgC,CAAC;AACjE,6EAA6E;AAC7E,eAAO,MAAM,WAAW,eAAwB,CAAC;AAEjD,yEAAyE;AACzE,MAAM,MAAM,cAAc,GACvB,OAAO,eAAe,GACtB,OAAO,mBAAmB,GAC1B,OAAO,WAAW,CAAC;AAEtB,MAAM,MAAM,SAAS,CAAC,KAAK,IAAI;IAC9B,yEAAyE;IACzE,UAAU,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,QAAQ,CAAC,KAAK,CAAC,GAAG,SAAS,CAAC;IAEzD;;;OAGG;IACH,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,cAAc,GAAG,SAAS,CAAC;IAEtD;;;;OAIG;IACH,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,IAAI,CAAC;IAE/B;;;OAGG;IACH,IAAI,CAAC,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC;CAClB,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,GAAG,CAAC,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,CAAC,KAAK,CAAC,GAAG,OAAO,CAiCzE"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** Continue walking the graph, visiting children of this node. */
|
|
2
|
+
export const DfsWalkContinue = Symbol("DfsWalkContinue");
|
|
3
|
+
/** Skip visiting children of this node, but continue walking the graph. */
|
|
4
|
+
export const DfsWalkSkipChildren = Symbol("DfsWalkSkipChildren");
|
|
5
|
+
/** Stop walking the graph immediately, returning `true` from {@link dfs}. */
|
|
6
|
+
export const DfsWalkStop = Symbol("DfsWalkStop");
|
|
7
|
+
/**
|
|
8
|
+
* Walks a graph or tree of unknown shape depth-first, in pre-order.
|
|
9
|
+
*
|
|
10
|
+
* The structure is not required to conform to any shared node interface:
|
|
11
|
+
* `childrenOf` is the only thing that has to know how a given `TNode` exposes
|
|
12
|
+
* its successors, which lets one generic walker drive discriminated unions
|
|
13
|
+
* whose node kinds carry children under entirely different field names.
|
|
14
|
+
*
|
|
15
|
+
* Time complexity: O(V + E). Without {@link DfsTraits.seen}, a cyclic graph does
|
|
16
|
+
* not terminate — pass a `Set` whenever a cycle is possible.
|
|
17
|
+
*
|
|
18
|
+
* @param root - The node to start walking from.
|
|
19
|
+
* @param traits - How to traverse, and what to do at each node.
|
|
20
|
+
* @returns `true` if the walk was stopped early via {@link DfsWalkStop},
|
|
21
|
+
* `false` if it ran to completion.
|
|
22
|
+
*/
|
|
23
|
+
export function dfs(root, traits) {
|
|
24
|
+
const { childrenOf, onEnter, onExit, seen } = traits;
|
|
25
|
+
if (seen?.has(root) === true) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
seen?.add(root);
|
|
29
|
+
const control = onEnter?.(root) ?? DfsWalkContinue;
|
|
30
|
+
if (control === DfsWalkStop) {
|
|
31
|
+
onExit?.(root);
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
let stopped = false;
|
|
35
|
+
if (control !== DfsWalkSkipChildren) {
|
|
36
|
+
const children = childrenOf(root);
|
|
37
|
+
if (children !== undefined) {
|
|
38
|
+
for (const child of children) {
|
|
39
|
+
if (dfs(child, traits)) {
|
|
40
|
+
stopped = true;
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
onExit?.(root);
|
|
47
|
+
return stopped;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=dfs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dfs.js","sourceRoot":"","sources":["../../src/graph/dfs.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CAAC,iBAAiB,CAAC,CAAC;AACzD,2EAA2E;AAC3E,MAAM,CAAC,MAAM,mBAAmB,GAAG,MAAM,CAAC,qBAAqB,CAAC,CAAC;AACjE,6EAA6E;AAC7E,MAAM,CAAC,MAAM,WAAW,GAAG,MAAM,CAAC,aAAa,CAAC,CAAC;AAgCjD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,GAAG,CAAQ,IAAW,EAAE,MAAwB;IAC/D,MAAM,EAAE,UAAU,EAAE,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AAK,CAAC,GACnC,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,CAWZ"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function partitionInto(iterable, predicate, select) {
|
|
2
|
+
const matching = [];
|
|
3
|
+
const nonMatching = [];
|
|
4
|
+
let index = 0;
|
|
5
|
+
for (const item of iterable) {
|
|
6
|
+
(predicate(item) ? matching : nonMatching).push(select(item, index));
|
|
7
|
+
index++;
|
|
8
|
+
}
|
|
9
|
+
return [matching, nonMatching];
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=_partition-into.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_partition-into.js","sourceRoot":"","sources":["../../src/group/_partition-into.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,aAAa,CAC5B,QAAqB,EACrB,SAAyB,EACzB,MAAqC;IAErC,MAAM,QAAQ,GAAQ,EAAE,CAAC;IACzB,MAAM,WAAW,GAAQ,EAAE,CAAC;IAE5B,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;QAC7B,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;QACrE,KAAK,EAAE,CAAC;IACT,CAAC;IAED,OAAO,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;AAChC,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type Callable, type EqualityComparisonStrategy } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* A nesting of {@link groupBy}'s result, one `Map` level per key accessor.
|
|
4
|
+
* `Depth` counts the accessors still to be applied.
|
|
5
|
+
*/
|
|
6
|
+
export type GroupedBy<T, Keys extends readonly unknown[]> = Keys extends readonly [infer Head, ...infer Tail] ? Tail extends readonly [] ? Map<Head, T[]> : Map<Head, GroupedBy<T, Tail>> : never;
|
|
7
|
+
/**
|
|
8
|
+
* Groups the items of an iterable by the key returned from each accessor, one
|
|
9
|
+
* nested `Map` level per accessor.
|
|
10
|
+
*
|
|
11
|
+
* Groups are created in the order their keys first occur. Time complexity: O(n
|
|
12
|
+
* · k) for `k` accessors, O(n) additional space, under the default
|
|
13
|
+
* `"sameValueZero"`.
|
|
14
|
+
*
|
|
15
|
+
* `keysOf` is an array rather than a rest parameter because a trailing
|
|
16
|
+
* `comparisonStrategy` is itself allowed to be a function, so it could not be
|
|
17
|
+
* told apart from one more accessor.
|
|
18
|
+
*
|
|
19
|
+
* @param iterable The input iterable.
|
|
20
|
+
* @param keysOf One or more functions taking an item and returning a group key,
|
|
21
|
+
* outermost level first.
|
|
22
|
+
* @param comparisonStrategy How keys are compared. Only `"sameValueZero"` is
|
|
23
|
+
* supported, because grouping is backed by a `Map`, whose key equality is
|
|
24
|
+
* fixed; any other strategy would need a different backing structure and a
|
|
25
|
+
* different complexity.
|
|
26
|
+
* @returns Nested maps from each group key to the items in that group.
|
|
27
|
+
* @throws {UnimplementedError} If `comparisonStrategy` is not
|
|
28
|
+
* `"sameValueZero"`.
|
|
29
|
+
*/
|
|
30
|
+
export declare function groupBy<T, const Keys extends readonly [unknown, ...unknown[]]>(iterable: Iterable<T>, keysOf: {
|
|
31
|
+
[I in keyof Keys]: Callable<[T], Keys[I]>;
|
|
32
|
+
}, comparisonStrategy?: EqualityComparisonStrategy<Keys[number], Keys[number]>): GroupedBy<T, Keys>;
|
|
33
|
+
//# sourceMappingURL=group-by.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"group-by.d.ts","sourceRoot":"","sources":["../../src/group/group-by.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,QAAQ,EACb,KAAK,0BAA0B,EAC/B,MAAM,cAAc,CAAC;AAEtB;;;GAGG;AACH,MAAM,MAAM,SAAS,CACpB,CAAC,EACD,IAAI,SAAS,SAAS,OAAO,EAAE,IAC5B,IAAI,SAAS,SAAS,CAAC,MAAM,IAAI,EAAE,GAAG,MAAM,IAAI,CAAC,GAClD,IAAI,SAAS,SAAS,EAAE,GACvB,GAAG,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,GACd,GAAG,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,GAC9B,KAAK,CAAC;AAET;;;;;;;;;;;;;;;;;;;;;;GAsBG;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;KAAG,CAAC,IAAI,MAAM,IAAI,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;CAAE,EACrD,kBAAkB,GAAE,0BAA0B,CAC7C,IAAI,CAAC,MAAM,CAAC,EACZ,IAAI,CAAC,MAAM,CAAC,CACM,GACjB,SAAS,CAAC,CAAC,EAAE,IAAI,CAAC,CAiCpB"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { UnimplementedError, } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Groups the items of an iterable by the key returned from each accessor, one
|
|
4
|
+
* nested `Map` level per accessor.
|
|
5
|
+
*
|
|
6
|
+
* Groups are created in the order their keys first occur. Time complexity: O(n
|
|
7
|
+
* · k) for `k` accessors, O(n) additional space, under the default
|
|
8
|
+
* `"sameValueZero"`.
|
|
9
|
+
*
|
|
10
|
+
* `keysOf` is an array rather than a rest parameter because a trailing
|
|
11
|
+
* `comparisonStrategy` is itself allowed to be a function, so it could not be
|
|
12
|
+
* told apart from one more accessor.
|
|
13
|
+
*
|
|
14
|
+
* @param iterable The input iterable.
|
|
15
|
+
* @param keysOf One or more functions taking an item and returning a group key,
|
|
16
|
+
* outermost level first.
|
|
17
|
+
* @param comparisonStrategy How keys are compared. Only `"sameValueZero"` is
|
|
18
|
+
* supported, because grouping is backed by a `Map`, whose key equality is
|
|
19
|
+
* fixed; any other strategy would need a different backing structure and a
|
|
20
|
+
* different complexity.
|
|
21
|
+
* @returns Nested maps from each group key to the items in that group.
|
|
22
|
+
* @throws {UnimplementedError} If `comparisonStrategy` is not
|
|
23
|
+
* `"sameValueZero"`.
|
|
24
|
+
*/
|
|
25
|
+
export function groupBy(iterable, keysOf, comparisonStrategy = "sameValueZero") {
|
|
26
|
+
if (comparisonStrategy !== "sameValueZero") {
|
|
27
|
+
throw new UnimplementedError(`comparison strategy: ${comparisonStrategy.toString()}`);
|
|
28
|
+
}
|
|
29
|
+
const [first, ...rest] = keysOf;
|
|
30
|
+
// Single-accessor fast path: no recursion, no intermediate grouping.
|
|
31
|
+
const groups = new Map();
|
|
32
|
+
for (const item of iterable) {
|
|
33
|
+
const key = first(item);
|
|
34
|
+
const group = groups.get(key);
|
|
35
|
+
if (group === undefined) {
|
|
36
|
+
groups.set(key, [item]);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
group.push(item);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (rest.length === 0) {
|
|
43
|
+
return groups;
|
|
44
|
+
}
|
|
45
|
+
const nested = new Map();
|
|
46
|
+
for (const [key, items] of groups) {
|
|
47
|
+
nested.set(key, groupBy(items, rest));
|
|
48
|
+
}
|
|
49
|
+
return nested;
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=group-by.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"group-by.js","sourceRoot":"","sources":["../../src/group/group-by.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,kBAAkB,GAGlB,MAAM,cAAc,CAAC;AAetB;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,OAAO,CACtB,QAAqB,EACrB,MAAqD,EACrD,kBAAkB,GAGd,eAAe;IAEnB,IAAI,kBAAkB,KAAK,eAAe,EAAE,CAAC;QAC5C,MAAM,IAAI,kBAAkB,CAC3B,wBAAwB,kBAAkB,CAAC,QAAQ,EAAE,EAAE,CACvD,CAAC;IACH,CAAC;IAED,MAAM,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,GAAG,MAAM,CAAC;IAEhC,qEAAqE;IACrE,MAAM,MAAM,GAAG,IAAI,GAAG,EAAgB,CAAC;IAEvC,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;QAC7B,MAAM,GAAG,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC;QACxB,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAE9B,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACzB,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;QACzB,CAAC;aAAM,CAAC;YACP,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAClB,CAAC;IACF,CAAC;IAED,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvB,OAAO,MAA4B,CAAC;IACrC,CAAC;IAED,MAAM,MAAM,GAAG,IAAI,GAAG,EAAoB,CAAC;IAC3C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,EAAE,CAAC;QACnC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,KAAK,EAAE,IAAgC,CAAC,CAAC,CAAC;IACnE,CAAC;IAED,OAAO,MAA4B,CAAC;AACrC,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Predicate } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* {@link partition} reporting positions instead of elements — the single-pass
|
|
4
|
+
* equivalent of the `map`/`filter`/`map` chain for index extraction.
|
|
5
|
+
*
|
|
6
|
+
* @param iterable - The input iterable.
|
|
7
|
+
* @param predicate - Returns true for indices going into the first group.
|
|
8
|
+
* @returns A tuple `[matchingIndices, nonMatchingIndices]`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function partitionIndices<T>(iterable: Iterable<T>, predicate: Predicate<[T]>): [number[], number[]];
|
|
11
|
+
//# sourceMappingURL=partition-indices.d.ts.map
|