@ac-kit/algo 0.3.1 → 0.4.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 +61 -0
- package/README.md +16 -4
- package/dist/graph/walk-tree.d.ts +55 -0
- package/dist/graph/walk-tree.d.ts.map +1 -0
- package/dist/graph/walk-tree.js +78 -0
- package/dist/graph/walk-tree.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/dist/recursion/run-trampoline.d.ts +68 -0
- package/dist/recursion/run-trampoline.d.ts.map +1 -0
- package/dist/recursion/run-trampoline.js +103 -0
- package/dist/recursion/run-trampoline.js.map +1 -0
- package/dist/sample/choice.d.ts +20 -0
- package/dist/sample/choice.d.ts.map +1 -0
- package/dist/sample/choice.js +25 -0
- package/dist/sample/choice.js.map +1 -0
- package/dist/sample/random-boolean.d.ts +23 -0
- package/dist/sample/random-boolean.d.ts.map +1 -0
- package/dist/sample/random-boolean.js +27 -0
- package/dist/sample/random-boolean.js.map +1 -0
- package/dist/sample/random-integer.d.ts +27 -0
- package/dist/sample/random-integer.d.ts.map +1 -0
- package/dist/sample/random-integer.js +40 -0
- package/dist/sample/random-integer.js.map +1 -0
- package/dist/sample/shuffle.d.ts.map +1 -1
- package/dist/sample/shuffle.js +3 -2
- package/dist/sample/shuffle.js.map +1 -1
- package/dist/select/unique-min-by.d.ts +28 -0
- package/dist/select/unique-min-by.d.ts.map +1 -0
- package/dist/select/unique-min-by.js +47 -0
- package/dist/select/unique-min-by.js.map +1 -0
- package/dist/set/_key-set.d.ts +13 -0
- package/dist/set/_key-set.d.ts.map +1 -0
- package/dist/set/_key-set.js +13 -0
- package/dist/set/_key-set.js.map +1 -0
- package/dist/set/duplicates-by.d.ts +35 -0
- package/dist/set/duplicates-by.d.ts.map +1 -0
- package/dist/set/duplicates-by.js +48 -0
- package/dist/set/duplicates-by.js.map +1 -0
- package/dist/set/override-by.d.ts +34 -0
- package/dist/set/override-by.d.ts.map +1 -0
- package/dist/set/override-by.js +50 -0
- package/dist/set/override-by.js.map +1 -0
- package/dist/set/uniq-by.d.ts +1 -1
- package/dist/set/uniq-by.d.ts.map +1 -1
- package/dist/set/uniq-by.js +7 -21
- package/dist/set/uniq-by.js.map +1 -1
- package/package.json +4 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,66 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 2a0fe74: - New `duplicatesBy(iterable, keyOf, comparisonStrategy?)` yields `[item, index]` for every item whose key an earlier item already had, in iteration order, so the first item of each key is never yielded and a key seen three times yields twice. It is lazy, calls `keyOf` once per item and is O(n) under the default `"sameValueZero"`; any other strategy costs O(n·k) for k distinct keys. A key of `undefined` is a key like any other.
|
|
8
|
+
- New `overrideBy(base, override, keyOf, comparisonStrategy?)` returns a new array: the items of `base` whose key no item of `override` has, in order, then all of `override`. Duplicates inside either list are kept as they are, and both inputs may be one-shot iterables.
|
|
9
|
+
- New `uniqueMinBy(iterable, compare)` returns the item that comes first under a `Comparator`, or `undefined` when the iterable is empty or when two or more items tie for the first place. One pass, O(1) memory.
|
|
10
|
+
- New `randomBoolean(probability, random)` answers `true` with the given probability, from a `RandomFn`. It draws exactly once for any probability, `0` never answers `true` and `1` always does, and a probability that is `NaN` or outside `[0, 1]` throws `RangeError` before drawing.
|
|
11
|
+
- Fix: `uniqBy` under the default `"sameValueZero"` no longer treats keys of different types as one (`uniqBy([1, "1", NaN, "NaN"], (key) => key)` returned `[1, NaN]`); it now follows `SameValueZero` exactly, as `uniq` does.
|
|
12
|
+
- 5152230: New `randomInteger(minInclusive, maxExclusive, random)` draws an integer
|
|
13
|
+
uniformly from a half-open range in O(1) from a `RandomFn`, throwing
|
|
14
|
+
`RangeError` for non-safe-integer bounds, an empty range or a range wider than
|
|
15
|
+
2^53; its bias is bounded by `range / 2^k` for a `k`-bit generator. New
|
|
16
|
+
`choice(items, random)` picks one element uniformly, and returns `undefined` for
|
|
17
|
+
an empty array without calling `random`.
|
|
18
|
+
- 5152230: `walkTree(root, childrenOf, visit, options?)` walks a tree in pre-order without
|
|
19
|
+
recursion, so a deep hostile tree cannot overflow the stack. `childrenOf`
|
|
20
|
+
returns an `ArrayLike` (or `undefined` for a leaf); `visit(node, parent,
|
|
21
|
+
siblingIndex, depth)` is called per node, with the root's parent `undefined`,
|
|
22
|
+
and may return `DfsWalkSkipChildren` to prune or `DfsWalkStop` to end the walk,
|
|
23
|
+
which then returns `true`. An optional `budget` (a `WorkBudget` from
|
|
24
|
+
`@ac-kit/core`) is charged `costOf(node)` units (default `1`) before each visit;
|
|
25
|
+
whatever it throws propagates. Memory is O(depth); nothing is allocated per
|
|
26
|
+
node. A node reachable twice is visited twice.
|
|
27
|
+
- 5152230: New `runTrampoline(task)` and the `TrampolineTask<T>` type: a recursive
|
|
28
|
+
algorithm written as generators that `yield` their sub-tasks runs on an explicit
|
|
29
|
+
stack, one native frame at any depth. A sub-task's error is thrown into its
|
|
30
|
+
parent at the `yield`, so `try`/`catch`/`finally` behave as in the recursion it
|
|
31
|
+
replaces; uncaught, it leaves `runTrampoline` unchanged. Only native synchronous
|
|
32
|
+
generators are supported. Overrides of generator methods are ignored. Yielding
|
|
33
|
+
an invalid task (including an async generator or a proxy) or a task already on
|
|
34
|
+
the stack throws a `TypeError` into the parent; an invalid root throws one
|
|
35
|
+
immediately.
|
|
36
|
+
|
|
37
|
+
### Patch Changes
|
|
38
|
+
|
|
39
|
+
- Updated dependencies [2a0fe74]
|
|
40
|
+
- Updated dependencies [5152230]
|
|
41
|
+
- Updated dependencies [2a0fe74]
|
|
42
|
+
- Updated dependencies [5152230]
|
|
43
|
+
- Updated dependencies [5152230]
|
|
44
|
+
- Updated dependencies [2a0fe74]
|
|
45
|
+
- Updated dependencies [2a0fe74]
|
|
46
|
+
- Updated dependencies [5152230]
|
|
47
|
+
- Updated dependencies [5152230]
|
|
48
|
+
- Updated dependencies [5152230]
|
|
49
|
+
- Updated dependencies [5152230]
|
|
50
|
+
- Updated dependencies [5152230]
|
|
51
|
+
- Updated dependencies [5152230]
|
|
52
|
+
- Updated dependencies [5152230]
|
|
53
|
+
- @ac-kit/core@0.10.0
|
|
54
|
+
- @ac-kit/data@0.3.2
|
|
55
|
+
|
|
56
|
+
## 0.3.2
|
|
57
|
+
|
|
58
|
+
### Patch Changes
|
|
59
|
+
|
|
60
|
+
- Updated dependencies [20341eb]
|
|
61
|
+
- @ac-kit/core@0.9.0
|
|
62
|
+
- @ac-kit/data@0.3.1
|
|
63
|
+
|
|
3
64
|
## 0.3.1
|
|
4
65
|
|
|
5
66
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -26,10 +26,17 @@ const distinct = [...uniqBy(events, (event) => event.correlationId)];
|
|
|
26
26
|
|
|
27
27
|
## What's included
|
|
28
28
|
|
|
29
|
-
- **Graph search** — `dfs` with enter/exit hooks and skip/stop control,
|
|
29
|
+
- **Graph search** — `dfs` with enter/exit hooks and skip/stop control,
|
|
30
|
+
`walkTree` (iterative pre-order, with parent, sibling index, depth and an
|
|
31
|
+
optional work budget, safe on hostile deep trees), `aStar`
|
|
30
32
|
(which is Dijkstra's algorithm when you give it no heuristic) and greedy
|
|
31
33
|
`bestFirstSearch`, all over a `childrenOf`/`neighboursOf` callback rather than
|
|
32
34
|
any particular graph type.
|
|
35
|
+
- **Recursion without a stack** — `runTrampoline` runs a recursive algorithm
|
|
36
|
+
written as native synchronous generators (`const value = yield child`) on
|
|
37
|
+
an explicit stack, so a hostile nesting depth costs heap, not a stack
|
|
38
|
+
overflow; errors and `try`/`finally` behave as in the recursion it replaces.
|
|
39
|
+
The runner ignores overrides of generator methods.
|
|
33
40
|
- **State-space search** — `beamSearch` for a width-bounded exploration,
|
|
34
41
|
`viterbi` for the cheapest path through layers whose equal states merge, and
|
|
35
42
|
`backtrack` for constraint search that undoes its own moves on the way back
|
|
@@ -38,7 +45,8 @@ const distinct = [...uniqBy(events, (event) => event.correlationId)];
|
|
|
38
45
|
a reusable `createBisector` bound to one accessor, plus membership testing
|
|
39
46
|
against sorted, disjoint intervals.
|
|
40
47
|
- **Selection** — `quickselect` for order statistics, `minBy`/`maxBy` for the
|
|
41
|
-
extreme item by a derived key, `
|
|
48
|
+
extreme item by a derived key, `uniqueMinBy` for the smallest item under a
|
|
49
|
+
comparator only when no other ties with it, `selectAt` for picking positions.
|
|
42
50
|
- **Sorting** — `radixSort`, a stable, comparator-free sort over an integer key.
|
|
43
51
|
- **Combinatorics** — `combinations` and `combinationsLazy`, every `k`-subset
|
|
44
52
|
of your items.
|
|
@@ -46,10 +54,14 @@ const distinct = [...uniqBy(events, (event) => event.correlationId)];
|
|
|
46
54
|
`partitionIndices`, `uniq` and `uniqBy`.
|
|
47
55
|
- **Set algebra** — `union`, `intersection`, `difference`,
|
|
48
56
|
`symmetricDifference`, and the `isSubsetOf` / `isSupersetOf` /
|
|
49
|
-
`isDisjointFrom` predicates — over any iterables
|
|
57
|
+
`isDisjointFrom` predicates — over any iterables; `duplicatesBy` yields each
|
|
58
|
+
repeat of a key with its position, and `overrideBy` layers one keyed list
|
|
59
|
+
over another.
|
|
50
60
|
- **Shuffling** — `shuffle` (Fisher–Yates or Sattolo) with the random source
|
|
51
61
|
injectable, so results can be made reproducible, and `spreadEvenly` to
|
|
52
|
-
interleave two collections at evenly spaced positions
|
|
62
|
+
interleave two collections at evenly spaced positions; `randomInteger`,
|
|
63
|
+
`randomBoolean` and `choice` draw one integer, one boolean or one element
|
|
64
|
+
from an injectable random source.
|
|
53
65
|
|
|
54
66
|
## What's not included
|
|
55
67
|
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { WorkBudget } from "@ac-kit/core";
|
|
2
|
+
import type { DfsWalkControl } from "./dfs.js";
|
|
3
|
+
/**
|
|
4
|
+
* Called for each node, in pre-order, before its children.
|
|
5
|
+
*
|
|
6
|
+
* Returning `undefined` or `DfsWalkContinue` descends into the node's children,
|
|
7
|
+
* `DfsWalkSkipChildren` skips them, `DfsWalkStop` ends the walk.
|
|
8
|
+
*/
|
|
9
|
+
export type TreeWalkVisitor<TNode> = (node: TNode, parent: TNode | undefined, siblingIndex: number, depth: number) => DfsWalkControl | undefined;
|
|
10
|
+
export type TreeWalkOptions<TNode> = {
|
|
11
|
+
/**
|
|
12
|
+
* Charged before each node is visited, so a walk over an untrusted tree can
|
|
13
|
+
* be capped. Whatever `spend` throws propagates unchanged out of
|
|
14
|
+
* {@link walkTree}.
|
|
15
|
+
*/
|
|
16
|
+
budget?: WorkBudget;
|
|
17
|
+
/**
|
|
18
|
+
* Units charged to `budget` for a node; a non-negative integer, `0` charging
|
|
19
|
+
* nothing. Defaults to `1` per node. Ignored without `budget`.
|
|
20
|
+
*/
|
|
21
|
+
costOf?: (node: TNode) => number;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Walks a tree depth-first in pre-order without recursion, so a hostile deep
|
|
25
|
+
* tree cannot overflow the stack.
|
|
26
|
+
*
|
|
27
|
+
* Takes a shape, not a structure: `childrenOf` is the only thing that knows how
|
|
28
|
+
* a node exposes its children. It is called once per visited node whose
|
|
29
|
+
* children are not skipped, and the returned array-like must not change while
|
|
30
|
+
* the walk is inside it. A node reachable twice is visited twice; guard in the
|
|
31
|
+
* visitor if the input may not be a tree.
|
|
32
|
+
*
|
|
33
|
+
* A callback rather than a generator: a generator allocates one result record
|
|
34
|
+
* per node (measured 2 to 4 times slower for 200 000 nodes, 9 to 16 ms against
|
|
35
|
+
* 4 ms, and its pending entries grow with the width), whereas this allocates
|
|
36
|
+
* nothing per node. No post-order hook is offered; pre-order consumers that
|
|
37
|
+
* need document order, parents or sibling indices get them from the visitor
|
|
38
|
+
* arguments.
|
|
39
|
+
*
|
|
40
|
+
* Time complexity: O(V). Memory: O(depth) for the walk itself, never O(width).
|
|
41
|
+
*
|
|
42
|
+
* @param root - The node to start from; visited with sibling index `0`, depth
|
|
43
|
+
* `0` and no parent.
|
|
44
|
+
* @param childrenOf - Returns the children of a node, or `undefined` for a
|
|
45
|
+
* leaf.
|
|
46
|
+
* @param visit - Called per node; see {@link TreeWalkVisitor}.
|
|
47
|
+
* @param options - Optional work cap.
|
|
48
|
+
* @returns `true` if the visitor stopped the walk, `false` if it completed.
|
|
49
|
+
* @throws Whatever `options.budget.spend` throws, before the node that could
|
|
50
|
+
* not be paid for is visited, including what it throws for an invalid
|
|
51
|
+
* `costOf` result (`FixedWorkBudget` throws `RangeError`); and anything
|
|
52
|
+
* `childrenOf` or `visit` throws.
|
|
53
|
+
*/
|
|
54
|
+
export declare function walkTree<TNode>(root: TNode, childrenOf: (node: TNode) => ArrayLike<TNode> | undefined, visit: TreeWalkVisitor<TNode>, options?: TreeWalkOptions<TNode>): boolean;
|
|
55
|
+
//# sourceMappingURL=walk-tree.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"walk-tree.d.ts","sourceRoot":"","sources":["../../src/graph/walk-tree.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAG/C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE/C;;;;;GAKG;AACH,MAAM,MAAM,eAAe,CAAC,KAAK,IAAI,CACpC,IAAI,EAAE,KAAK,EACX,MAAM,EAAE,KAAK,GAAG,SAAS,EACzB,YAAY,EAAE,MAAM,EACpB,KAAK,EAAE,MAAM,KACT,cAAc,GAAG,SAAS,CAAC;AAEhC,MAAM,MAAM,eAAe,CAAC,KAAK,IAAI;IACpC;;;;OAIG;IACH,MAAM,CAAC,EAAE,UAAU,CAAC;IAEpB;;;OAGG;IACH,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,MAAM,CAAC;CACjC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAC7B,IAAI,EAAE,KAAK,EACX,UAAU,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,SAAS,CAAC,KAAK,CAAC,GAAG,SAAS,EACzD,KAAK,EAAE,eAAe,CAAC,KAAK,CAAC,EAC7B,OAAO,GAAE,eAAe,CAAC,KAAK,CAAM,GAClC,OAAO,CAuDT"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { DfsWalkSkipChildren, DfsWalkStop } from "./dfs.js";
|
|
2
|
+
/**
|
|
3
|
+
* Walks a tree depth-first in pre-order without recursion, so a hostile deep
|
|
4
|
+
* tree cannot overflow the stack.
|
|
5
|
+
*
|
|
6
|
+
* Takes a shape, not a structure: `childrenOf` is the only thing that knows how
|
|
7
|
+
* a node exposes its children. It is called once per visited node whose
|
|
8
|
+
* children are not skipped, and the returned array-like must not change while
|
|
9
|
+
* the walk is inside it. A node reachable twice is visited twice; guard in the
|
|
10
|
+
* visitor if the input may not be a tree.
|
|
11
|
+
*
|
|
12
|
+
* A callback rather than a generator: a generator allocates one result record
|
|
13
|
+
* per node (measured 2 to 4 times slower for 200 000 nodes, 9 to 16 ms against
|
|
14
|
+
* 4 ms, and its pending entries grow with the width), whereas this allocates
|
|
15
|
+
* nothing per node. No post-order hook is offered; pre-order consumers that
|
|
16
|
+
* need document order, parents or sibling indices get them from the visitor
|
|
17
|
+
* arguments.
|
|
18
|
+
*
|
|
19
|
+
* Time complexity: O(V). Memory: O(depth) for the walk itself, never O(width).
|
|
20
|
+
*
|
|
21
|
+
* @param root - The node to start from; visited with sibling index `0`, depth
|
|
22
|
+
* `0` and no parent.
|
|
23
|
+
* @param childrenOf - Returns the children of a node, or `undefined` for a
|
|
24
|
+
* leaf.
|
|
25
|
+
* @param visit - Called per node; see {@link TreeWalkVisitor}.
|
|
26
|
+
* @param options - Optional work cap.
|
|
27
|
+
* @returns `true` if the visitor stopped the walk, `false` if it completed.
|
|
28
|
+
* @throws Whatever `options.budget.spend` throws, before the node that could
|
|
29
|
+
* not be paid for is visited, including what it throws for an invalid
|
|
30
|
+
* `costOf` result (`FixedWorkBudget` throws `RangeError`); and anything
|
|
31
|
+
* `childrenOf` or `visit` throws.
|
|
32
|
+
*/
|
|
33
|
+
export function walkTree(root, childrenOf, visit, options = {}) {
|
|
34
|
+
const { budget, costOf } = options;
|
|
35
|
+
// One frame per ancestor: its node, its children and the next child to take.
|
|
36
|
+
const nodes = [];
|
|
37
|
+
const siblings = [];
|
|
38
|
+
const nextIndices = [];
|
|
39
|
+
let node = root;
|
|
40
|
+
let siblingIndex = 0;
|
|
41
|
+
for (;;) {
|
|
42
|
+
budget?.spend(costOf === undefined ? 1 : costOf(node));
|
|
43
|
+
const depth = nodes.length;
|
|
44
|
+
const control = visit(node, depth === 0 ? undefined : nodes[depth - 1], siblingIndex, depth);
|
|
45
|
+
if (control === DfsWalkStop) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
if (control !== DfsWalkSkipChildren) {
|
|
49
|
+
const children = childrenOf(node);
|
|
50
|
+
if (children !== undefined && children.length > 0) {
|
|
51
|
+
nodes.push(node);
|
|
52
|
+
siblings.push(children);
|
|
53
|
+
nextIndices.push(1);
|
|
54
|
+
node = children[0];
|
|
55
|
+
siblingIndex = 0;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
for (;;) {
|
|
60
|
+
const top = nodes.length - 1;
|
|
61
|
+
if (top < 0) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
const children = siblings[top];
|
|
65
|
+
const next = nextIndices[top];
|
|
66
|
+
if (next < children.length) {
|
|
67
|
+
nextIndices[top] = next + 1;
|
|
68
|
+
node = children[next];
|
|
69
|
+
siblingIndex = next;
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
nodes.pop();
|
|
73
|
+
siblings.pop();
|
|
74
|
+
nextIndices.pop();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=walk-tree.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"walk-tree.js","sourceRoot":"","sources":["../../src/graph/walk-tree.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,mBAAmB,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AA+B5D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,UAAU,QAAQ,CACvB,IAAW,EACX,UAAyD,EACzD,KAA6B,EAC7B,OAAO,GAA2B,EAAE;IAEpC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAEnC,6EAA6E;IAC7E,MAAM,KAAK,GAAY,EAAE,CAAC;IAC1B,MAAM,QAAQ,GAAuB,EAAE,CAAC;IACxC,MAAM,WAAW,GAAa,EAAE,CAAC;IAEjC,IAAI,IAAI,GAAG,IAAI,CAAC;IAChB,IAAI,YAAY,GAAG,CAAC,CAAC;IAErB,SAAS,CAAC;QACT,MAAM,EAAE,KAAK,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;QACvD,MAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC;QAC3B,MAAM,OAAO,GAAG,KAAK,CACpB,IAAI,EACJ,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,EAC1C,YAAY,EACZ,KAAK,CACL,CAAC;QAEF,IAAI,OAAO,KAAK,WAAW,EAAE,CAAC;YAC7B,OAAO,IAAI,CAAC;QACb,CAAC;QAED,IAAI,OAAO,KAAK,mBAAmB,EAAE,CAAC;YACrC,MAAM,QAAQ,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC;YAClC,IAAI,QAAQ,KAAK,SAAS,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACnD,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBACjB,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;gBACxB,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;gBACpB,IAAI,GAAG,QAAQ,CAAC,CAAC,CAAE,CAAC;gBACpB,YAAY,GAAG,CAAC,CAAC;gBACjB,SAAS;YACV,CAAC;QACF,CAAC;QAED,SAAS,CAAC;YACT,MAAM,GAAG,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;YAC7B,IAAI,GAAG,GAAG,CAAC,EAAE,CAAC;gBACb,OAAO,KAAK,CAAC;YACd,CAAC;YACD,MAAM,QAAQ,GAAG,QAAQ,CAAC,GAAG,CAAE,CAAC;YAChC,MAAM,IAAI,GAAG,WAAW,CAAC,GAAG,CAAE,CAAC;YAC/B,IAAI,IAAI,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC;gBAC5B,WAAW,CAAC,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC;gBAC5B,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAE,CAAC;gBACvB,YAAY,GAAG,IAAI,CAAC;gBACpB,MAAM;YACP,CAAC;YACD,KAAK,CAAC,GAAG,EAAE,CAAC;YACZ,QAAQ,CAAC,GAAG,EAAE,CAAC;YACf,WAAW,CAAC,GAAG,EAAE,CAAC;QACnB,CAAC;IACF,CAAC;AACF,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export * from "./distribute/spread-evenly.js";
|
|
|
3
3
|
export * from "./graph/a-star.js";
|
|
4
4
|
export * from "./graph/best-first-search.js";
|
|
5
5
|
export * from "./graph/dfs.js";
|
|
6
|
+
export * from "./graph/walk-tree.js";
|
|
6
7
|
export * from "./group/group-by.js";
|
|
7
8
|
export * from "./group/partition-indices.js";
|
|
8
9
|
export * from "./group/partition.js";
|
|
@@ -20,16 +21,23 @@ export * from "./ordered/bisect-left.js";
|
|
|
20
21
|
export * from "./ordered/bisect-right.js";
|
|
21
22
|
export * from "./ordered/bisect.js";
|
|
22
23
|
export * from "./ordered/create-bisector.js";
|
|
24
|
+
export * from "./recursion/run-trampoline.js";
|
|
25
|
+
export * from "./sample/choice.js";
|
|
26
|
+
export * from "./sample/random-boolean.js";
|
|
27
|
+
export * from "./sample/random-integer.js";
|
|
23
28
|
export * from "./sample/shuffle.js";
|
|
24
29
|
export * from "./select/max-by.js";
|
|
25
30
|
export * from "./select/min-by.js";
|
|
26
31
|
export * from "./select/quickselect.js";
|
|
27
32
|
export * from "./select/select-at.js";
|
|
33
|
+
export * from "./select/unique-min-by.js";
|
|
28
34
|
export * from "./set/difference.js";
|
|
35
|
+
export * from "./set/duplicates-by.js";
|
|
29
36
|
export * from "./set/intersection.js";
|
|
30
37
|
export * from "./set/is-disjoint-from.js";
|
|
31
38
|
export * from "./set/is-subset-of.js";
|
|
32
39
|
export * from "./set/is-superset-of.js";
|
|
40
|
+
export * from "./set/override-by.js";
|
|
33
41
|
export * from "./set/symmetric-difference.js";
|
|
34
42
|
export * from "./set/union.js";
|
|
35
43
|
export * from "./set/uniq-by.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iCAAiC,CAAC;AAChD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,mBAAmB,CAAC;AAClC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sBAAsB,CAAC;AACrC,cAAc,oCAAoC,CAAC;AACnD,cAAc,mCAAmC,CAAC;AAClD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,sCAAsC,CAAC;AACrD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,kCAAkC,CAAC;AACjD,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iCAAiC,CAAC;AAChD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,mBAAmB,CAAC;AAClC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sBAAsB,CAAC;AACrC,cAAc,oCAAoC,CAAC;AACnD,cAAc,mCAAmC,CAAC;AAClD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,sCAAsC,CAAC;AACrD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,kCAAkC,CAAC;AACjD,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,oBAAoB,CAAC;AACnC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,sBAAsB,CAAC;AACrC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,7 @@ export * from "./distribute/spread-evenly.js";
|
|
|
3
3
|
export * from "./graph/a-star.js";
|
|
4
4
|
export * from "./graph/best-first-search.js";
|
|
5
5
|
export * from "./graph/dfs.js";
|
|
6
|
+
export * from "./graph/walk-tree.js";
|
|
6
7
|
export * from "./group/group-by.js";
|
|
7
8
|
export * from "./group/partition-indices.js";
|
|
8
9
|
export * from "./group/partition.js";
|
|
@@ -20,16 +21,23 @@ export * from "./ordered/bisect-left.js";
|
|
|
20
21
|
export * from "./ordered/bisect-right.js";
|
|
21
22
|
export * from "./ordered/bisect.js";
|
|
22
23
|
export * from "./ordered/create-bisector.js";
|
|
24
|
+
export * from "./recursion/run-trampoline.js";
|
|
25
|
+
export * from "./sample/choice.js";
|
|
26
|
+
export * from "./sample/random-boolean.js";
|
|
27
|
+
export * from "./sample/random-integer.js";
|
|
23
28
|
export * from "./sample/shuffle.js";
|
|
24
29
|
export * from "./select/max-by.js";
|
|
25
30
|
export * from "./select/min-by.js";
|
|
26
31
|
export * from "./select/quickselect.js";
|
|
27
32
|
export * from "./select/select-at.js";
|
|
33
|
+
export * from "./select/unique-min-by.js";
|
|
28
34
|
export * from "./set/difference.js";
|
|
35
|
+
export * from "./set/duplicates-by.js";
|
|
29
36
|
export * from "./set/intersection.js";
|
|
30
37
|
export * from "./set/is-disjoint-from.js";
|
|
31
38
|
export * from "./set/is-subset-of.js";
|
|
32
39
|
export * from "./set/is-superset-of.js";
|
|
40
|
+
export * from "./set/override-by.js";
|
|
33
41
|
export * from "./set/symmetric-difference.js";
|
|
34
42
|
export * from "./set/union.js";
|
|
35
43
|
export * from "./set/uniq-by.js";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iCAAiC,CAAC;AAChD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,mBAAmB,CAAC;AAClC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sBAAsB,CAAC;AACrC,cAAc,oCAAoC,CAAC;AACnD,cAAc,mCAAmC,CAAC;AAClD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,sCAAsC,CAAC;AACrD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,kCAAkC,CAAC;AACjD,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iCAAiC,CAAC;AAChD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,mBAAmB,CAAC;AAClC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sBAAsB,CAAC;AACrC,cAAc,oCAAoC,CAAC;AACnD,cAAc,mCAAmC,CAAC;AAClD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,sCAAsC,CAAC;AACrD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,kCAAkC,CAAC;AACjD,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,oBAAoB,CAAC;AACnC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,sBAAsB,CAAC;AACrC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A computation written like a recursive function, run without native recursion
|
|
3
|
+
* by {@link runTrampoline}: instead of calling itself it yields a sub-task, and
|
|
4
|
+
* `const value = yield child` resumes it with the child's result.
|
|
5
|
+
*
|
|
6
|
+
* A task is a synchronous generator object, so a `function*` returning
|
|
7
|
+
* `TrampolineTask<T>` is the way to write one. `yield*` must not be used: it
|
|
8
|
+
* delegates on the native stack, which is what this type exists to avoid.
|
|
9
|
+
*
|
|
10
|
+
* @template T What the task returns.
|
|
11
|
+
*/
|
|
12
|
+
export type TrampolineTask<T> = Generator<TrampolineTask<unknown>, T, unknown>;
|
|
13
|
+
/**
|
|
14
|
+
* Runs `task` and every sub-task it yields on an explicit stack, so the native
|
|
15
|
+
* call stack keeps one frame however deep the tasks nest: no input can raise
|
|
16
|
+
* the engine's `RangeError: Maximum call stack size exceeded`.
|
|
17
|
+
*
|
|
18
|
+
* It behaves as the recursion it replaces:
|
|
19
|
+
*
|
|
20
|
+
* - A sub-task's result is sent to the task that yielded it.
|
|
21
|
+
* - An error thrown by a sub-task is thrown into the task that yielded it at its
|
|
22
|
+
* `yield`, so `try`/`catch`/`finally` around a `yield` work as around a call;
|
|
23
|
+
* uncaught, it unwinds every task below and leaves `runTrampoline`
|
|
24
|
+
* unchanged.
|
|
25
|
+
* - Only native synchronous generator objects are accepted. The runner calls the
|
|
26
|
+
* built-in generator methods directly; overrides and protocol getters are
|
|
27
|
+
* ignored. Async generators, proxies and lookalike objects are rejected.
|
|
28
|
+
* - Yielding an invalid task or a task already on the stack — the yielding task
|
|
29
|
+
* itself or one of its ancestors — throws a `TypeError` into the yielding
|
|
30
|
+
* task at its `yield`.
|
|
31
|
+
* - On error, the runner resumes each suspended parent with the exception,
|
|
32
|
+
* innermost first. It does not forcibly close tasks with `return()`: errors
|
|
33
|
+
* either get caught by a task or unwind the stack before leaving the runner.
|
|
34
|
+
*
|
|
35
|
+
* It charges no budget and checks no signal itself: a task that evaluates
|
|
36
|
+
* untrusted input charges its `WorkBudget` (from `@ac-kit/core`) or reads its
|
|
37
|
+
* `AbortSignal` where it does the work, and what those throw unwinds the tasks
|
|
38
|
+
* like any error.
|
|
39
|
+
*
|
|
40
|
+
* Complexity: O(1) native stack, O(nesting depth) heap for the pending tasks;
|
|
41
|
+
* O(1) time per resumption, excluding time spent in user code.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* type Tree = { readonly children: readonly Tree[] };
|
|
46
|
+
*
|
|
47
|
+
* function* depth(tree: Tree): TrampolineTask<number> {
|
|
48
|
+
* let deepest = 0;
|
|
49
|
+
* for (const child of tree.children) {
|
|
50
|
+
* deepest = Math.max(deepest, (yield depth(child)) as number);
|
|
51
|
+
* }
|
|
52
|
+
* return deepest + 1;
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* let chain: Tree = { children: [] };
|
|
56
|
+
* for (let level = 1; level < 1_000_000; level++) {
|
|
57
|
+
* chain = { children: [chain] };
|
|
58
|
+
* }
|
|
59
|
+
* runTrampoline(depth(chain)); // 1000000
|
|
60
|
+
* ```;
|
|
61
|
+
*
|
|
62
|
+
* @param task The root task, a synchronous generator object.
|
|
63
|
+
* @returns What the root task returns.
|
|
64
|
+
* @throws {TypeError} When the root is not a synchronous generator object.
|
|
65
|
+
* @throws Any exception not caught by a task, unchanged.
|
|
66
|
+
*/
|
|
67
|
+
export declare function runTrampoline<T>(task: TrampolineTask<T>): T;
|
|
68
|
+
//# sourceMappingURL=run-trampoline.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-trampoline.d.ts","sourceRoot":"","sources":["../../src/recursion/run-trampoline.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,MAAM,cAAc,CAAC,CAAC,IAAI,SAAS,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE,OAAO,CAAC,CAAC;AAE/E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC,GAAG,CAAC,CA8C3D"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs `task` and every sub-task it yields on an explicit stack, so the native
|
|
3
|
+
* call stack keeps one frame however deep the tasks nest: no input can raise
|
|
4
|
+
* the engine's `RangeError: Maximum call stack size exceeded`.
|
|
5
|
+
*
|
|
6
|
+
* It behaves as the recursion it replaces:
|
|
7
|
+
*
|
|
8
|
+
* - A sub-task's result is sent to the task that yielded it.
|
|
9
|
+
* - An error thrown by a sub-task is thrown into the task that yielded it at its
|
|
10
|
+
* `yield`, so `try`/`catch`/`finally` around a `yield` work as around a call;
|
|
11
|
+
* uncaught, it unwinds every task below and leaves `runTrampoline`
|
|
12
|
+
* unchanged.
|
|
13
|
+
* - Only native synchronous generator objects are accepted. The runner calls the
|
|
14
|
+
* built-in generator methods directly; overrides and protocol getters are
|
|
15
|
+
* ignored. Async generators, proxies and lookalike objects are rejected.
|
|
16
|
+
* - Yielding an invalid task or a task already on the stack — the yielding task
|
|
17
|
+
* itself or one of its ancestors — throws a `TypeError` into the yielding
|
|
18
|
+
* task at its `yield`.
|
|
19
|
+
* - On error, the runner resumes each suspended parent with the exception,
|
|
20
|
+
* innermost first. It does not forcibly close tasks with `return()`: errors
|
|
21
|
+
* either get caught by a task or unwind the stack before leaving the runner.
|
|
22
|
+
*
|
|
23
|
+
* It charges no budget and checks no signal itself: a task that evaluates
|
|
24
|
+
* untrusted input charges its `WorkBudget` (from `@ac-kit/core`) or reads its
|
|
25
|
+
* `AbortSignal` where it does the work, and what those throw unwinds the tasks
|
|
26
|
+
* like any error.
|
|
27
|
+
*
|
|
28
|
+
* Complexity: O(1) native stack, O(nesting depth) heap for the pending tasks;
|
|
29
|
+
* O(1) time per resumption, excluding time spent in user code.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* type Tree = { readonly children: readonly Tree[] };
|
|
34
|
+
*
|
|
35
|
+
* function* depth(tree: Tree): TrampolineTask<number> {
|
|
36
|
+
* let deepest = 0;
|
|
37
|
+
* for (const child of tree.children) {
|
|
38
|
+
* deepest = Math.max(deepest, (yield depth(child)) as number);
|
|
39
|
+
* }
|
|
40
|
+
* return deepest + 1;
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* let chain: Tree = { children: [] };
|
|
44
|
+
* for (let level = 1; level < 1_000_000; level++) {
|
|
45
|
+
* chain = { children: [chain] };
|
|
46
|
+
* }
|
|
47
|
+
* runTrampoline(depth(chain)); // 1000000
|
|
48
|
+
* ```;
|
|
49
|
+
*
|
|
50
|
+
* @param task The root task, a synchronous generator object.
|
|
51
|
+
* @returns What the root task returns.
|
|
52
|
+
* @throws {TypeError} When the root is not a synchronous generator object.
|
|
53
|
+
* @throws Any exception not caught by a task, unchanged.
|
|
54
|
+
*/
|
|
55
|
+
export function runTrampoline(task) {
|
|
56
|
+
const pending = [task];
|
|
57
|
+
const active = new Set(pending);
|
|
58
|
+
let input;
|
|
59
|
+
let throwing = false;
|
|
60
|
+
for (;;) {
|
|
61
|
+
const current = pending[pending.length - 1];
|
|
62
|
+
let step;
|
|
63
|
+
try {
|
|
64
|
+
step = Reflect.apply(throwing ? nativeGeneratorMethods.throw : nativeGeneratorMethods.next, current, [input]);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
pending.pop();
|
|
68
|
+
active.delete(current);
|
|
69
|
+
if (pending.length === 0) {
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
input = error;
|
|
73
|
+
throwing = true;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
throwing = false;
|
|
77
|
+
if (step.done === true) {
|
|
78
|
+
pending.pop();
|
|
79
|
+
active.delete(current);
|
|
80
|
+
if (pending.length === 0) {
|
|
81
|
+
return step.value;
|
|
82
|
+
}
|
|
83
|
+
input = step.value;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
const child = step.value;
|
|
87
|
+
if (active.has(child)) {
|
|
88
|
+
input = new TypeError("A trampoline task yielded a task that is already running");
|
|
89
|
+
throwing = true;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
pending.push(child);
|
|
93
|
+
active.add(child);
|
|
94
|
+
input = undefined;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const generatorPrototype = Object.getPrototypeOf(function* () { }.prototype);
|
|
98
|
+
const nativeGeneratorMethods = {
|
|
99
|
+
next: generatorPrototype.next,
|
|
100
|
+
throw: generatorPrototype.throw,
|
|
101
|
+
return: generatorPrototype.return,
|
|
102
|
+
};
|
|
103
|
+
//# sourceMappingURL=run-trampoline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-trampoline.js","sourceRoot":"","sources":["../../src/recursion/run-trampoline.ts"],"names":[],"mappings":"AAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AACH,MAAM,UAAU,aAAa,CAAI,IAAuB;IACvD,MAAM,OAAO,GAA8B,CAAC,IAAI,CAAC,CAAC;IAClD,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,CAAC;IAChC,IAAI,KAAc,CAAC;IACnB,IAAI,QAAQ,GAAG,KAAK,CAAC;IACrB,SAAS,CAAC;QACT,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAE,CAAC;QAC7C,IAAI,IAAsD,CAAC;QAC3D,IAAI,CAAC;YACJ,IAAI,GAAG,OAAO,CAAC,KAAK,CACnB,QAAQ,CAAC,CAAC,CAAC,sBAAsB,CAAC,KAAK,CAAC,CAAC,CAAC,sBAAsB,CAAC,IAAI,EACrE,OAAO,EACP,CAAC,KAAK,CAAC,CACP,CAAC;QACH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,OAAO,CAAC,GAAG,EAAE,CAAC;YACd,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YACvB,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBAC1B,MAAM,KAAK,CAAC;YACb,CAAC;YACD,KAAK,GAAG,KAAK,CAAC;YACd,QAAQ,GAAG,IAAI,CAAC;YAChB,SAAS;QACV,CAAC;QACD,QAAQ,GAAG,KAAK,CAAC;QACjB,IAAI,IAAI,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;YACxB,OAAO,CAAC,GAAG,EAAE,CAAC;YACd,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YACvB,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBAC1B,OAAO,IAAI,CAAC,KAAU,CAAC;YACxB,CAAC;YACD,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;YACnB,SAAS;QACV,CAAC;QACD,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;QACzB,IAAI,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;YACvB,KAAK,GAAG,IAAI,SAAS,CACpB,0DAA0D,CAC1D,CAAC;YACF,QAAQ,GAAG,IAAI,CAAC;YAChB,SAAS;QACV,CAAC;QACD,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACpB,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QAClB,KAAK,GAAG,SAAS,CAAC;IACnB,CAAC;AACF,CAAC;AAED,MAAM,kBAAkB,GAA4B,MAAM,CAAC,cAAc,CACxE,QAAQ,CAAC,MAAK,CAAC,CAAC,SAAS,CACzB,CAAC;AACF,MAAM,sBAAsB,GAAG;IAC9B,IAAI,EAAE,kBAAkB,CAAC,IAAI;IAC7B,KAAK,EAAE,kBAAkB,CAAC,KAAK;IAC/B,MAAM,EAAE,kBAAkB,CAAC,MAAM;CACjC,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { RandomFn } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Picks one element of `items` uniformly.
|
|
4
|
+
*
|
|
5
|
+
* Complexity: O(1), one call to `random`; `items` is not copied or modified.
|
|
6
|
+
* Takes an array because the pick indexes freely.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* choice(["a", "b", "c"], Math.random); // "a", "b" or "c"
|
|
11
|
+
* choice([], Math.random); // undefined
|
|
12
|
+
* ```;
|
|
13
|
+
*
|
|
14
|
+
* @param items The candidates.
|
|
15
|
+
* @param random A source in `[0, 1)`; not called when `items` is empty.
|
|
16
|
+
* @returns An element, or `undefined` when `items` is empty (so an array that
|
|
17
|
+
* holds `undefined` cannot tell the two apart: check `items.length`).
|
|
18
|
+
*/
|
|
19
|
+
export declare function choice<T>(items: readonly T[], random: RandomFn): T | undefined;
|
|
20
|
+
//# sourceMappingURL=choice.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"choice.d.ts","sourceRoot":"","sources":["../../src/sample/choice.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAI7C;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,MAAM,CAAC,CAAC,EACvB,KAAK,EAAE,SAAS,CAAC,EAAE,EACnB,MAAM,EAAE,QAAQ,GACd,CAAC,GAAG,SAAS,CAKf"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { randomInteger } from "./random-integer.js";
|
|
2
|
+
/**
|
|
3
|
+
* Picks one element of `items` uniformly.
|
|
4
|
+
*
|
|
5
|
+
* Complexity: O(1), one call to `random`; `items` is not copied or modified.
|
|
6
|
+
* Takes an array because the pick indexes freely.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* choice(["a", "b", "c"], Math.random); // "a", "b" or "c"
|
|
11
|
+
* choice([], Math.random); // undefined
|
|
12
|
+
* ```;
|
|
13
|
+
*
|
|
14
|
+
* @param items The candidates.
|
|
15
|
+
* @param random A source in `[0, 1)`; not called when `items` is empty.
|
|
16
|
+
* @returns An element, or `undefined` when `items` is empty (so an array that
|
|
17
|
+
* holds `undefined` cannot tell the two apart: check `items.length`).
|
|
18
|
+
*/
|
|
19
|
+
export function choice(items, random) {
|
|
20
|
+
if (items.length === 0) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
return items[randomInteger(0, items.length, random)];
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=choice.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"choice.js","sourceRoot":"","sources":["../../src/sample/choice.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,MAAM,CACrB,KAAmB,EACnB,MAAgB;IAEhB,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACxB,OAAO,SAAS,CAAC;IAClB,CAAC;IACD,OAAO,KAAK,CAAC,aAAa,CAAC,CAAC,EAAE,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AACtD,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { RandomFn } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Answers `true` with the given probability.
|
|
4
|
+
*
|
|
5
|
+
* Calls `random` exactly once, whatever `probability` is (also `0` and `1`), so
|
|
6
|
+
* a seeded source is consumed at the same rate whichever branch a test takes.
|
|
7
|
+
* `0` never answers `true` and `1` always does, since `random` is in `[0, 1)`.
|
|
8
|
+
*
|
|
9
|
+
* Complexity: O(1), one call to `random`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* if (randomBoolean(0.3, Math.random)) {
|
|
14
|
+
* // taken three times out of ten
|
|
15
|
+
* }
|
|
16
|
+
* ```;
|
|
17
|
+
*
|
|
18
|
+
* @param probability The chance of `true`, in `[0, 1]`.
|
|
19
|
+
* @param random A source in `[0, 1)`.
|
|
20
|
+
* @throws {RangeError} When `probability` is `NaN` or outside `[0, 1]`.
|
|
21
|
+
*/
|
|
22
|
+
export declare function randomBoolean(probability: number, random: RandomFn): boolean;
|
|
23
|
+
//# sourceMappingURL=random-boolean.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random-boolean.d.ts","sourceRoot":"","sources":["../../src/sample/random-boolean.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,aAAa,CAAC,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,GAAG,OAAO,CAM5E"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Answers `true` with the given probability.
|
|
3
|
+
*
|
|
4
|
+
* Calls `random` exactly once, whatever `probability` is (also `0` and `1`), so
|
|
5
|
+
* a seeded source is consumed at the same rate whichever branch a test takes.
|
|
6
|
+
* `0` never answers `true` and `1` always does, since `random` is in `[0, 1)`.
|
|
7
|
+
*
|
|
8
|
+
* Complexity: O(1), one call to `random`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* if (randomBoolean(0.3, Math.random)) {
|
|
13
|
+
* // taken three times out of ten
|
|
14
|
+
* }
|
|
15
|
+
* ```;
|
|
16
|
+
*
|
|
17
|
+
* @param probability The chance of `true`, in `[0, 1]`.
|
|
18
|
+
* @param random A source in `[0, 1)`.
|
|
19
|
+
* @throws {RangeError} When `probability` is `NaN` or outside `[0, 1]`.
|
|
20
|
+
*/
|
|
21
|
+
export function randomBoolean(probability, random) {
|
|
22
|
+
if (!(probability >= 0 && probability <= 1)) {
|
|
23
|
+
throw new RangeError("probability must be within [0, 1]");
|
|
24
|
+
}
|
|
25
|
+
return random() < probability;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=random-boolean.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random-boolean.js","sourceRoot":"","sources":["../../src/sample/random-boolean.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,aAAa,CAAC,WAAmB,EAAE,MAAgB;IAClE,IAAI,CAAC,CAAC,WAAW,IAAI,CAAC,IAAI,WAAW,IAAI,CAAC,CAAC,EAAE,CAAC;QAC7C,MAAM,IAAI,UAAU,CAAC,mCAAmC,CAAC,CAAC;IAC3D,CAAC;IAED,OAAO,MAAM,EAAE,GAAG,WAAW,CAAC;AAC/B,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { RandomFn } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Draws an integer uniformly from `[minInclusive, maxExclusive)`.
|
|
4
|
+
*
|
|
5
|
+
* Computed as `floor(random() * range)`, so the result is as uniform as
|
|
6
|
+
* `random` is: a generator with `k` bits of output gives each value a
|
|
7
|
+
* probability within `range / 2^k` (relative) of `1 / range`, about `2^-20` for
|
|
8
|
+
* `range = 2^32` over `Math.random`'s 52 to 53 bits, depending on the engine. A
|
|
9
|
+
* range wider than the generator's resolution cannot be covered uniformly by
|
|
10
|
+
* any method.
|
|
11
|
+
*
|
|
12
|
+
* Complexity: O(1), one call to `random`.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const roll = randomInteger(1, 7, Math.random); // 1..6
|
|
17
|
+
* ```;
|
|
18
|
+
*
|
|
19
|
+
* @param minInclusive The smallest result; a safe integer.
|
|
20
|
+
* @param maxExclusive The bound, never returned; a safe integer greater than
|
|
21
|
+
* `minInclusive`, with `maxExclusive - minInclusive` at most `2^53`.
|
|
22
|
+
* @param random A source in `[0, 1)`.
|
|
23
|
+
* @throws {RangeError} When a bound is not a safe integer, the range is empty
|
|
24
|
+
* or wider than `2^53`.
|
|
25
|
+
*/
|
|
26
|
+
export declare function randomInteger(minInclusive: number, maxExclusive: number, random: RandomFn): number;
|
|
27
|
+
//# sourceMappingURL=random-integer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random-integer.d.ts","sourceRoot":"","sources":["../../src/sample/random-integer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,aAAa,CAC5B,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,MAAM,EACpB,MAAM,EAAE,QAAQ,GACd,MAAM,CAkBR"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Draws an integer uniformly from `[minInclusive, maxExclusive)`.
|
|
3
|
+
*
|
|
4
|
+
* Computed as `floor(random() * range)`, so the result is as uniform as
|
|
5
|
+
* `random` is: a generator with `k` bits of output gives each value a
|
|
6
|
+
* probability within `range / 2^k` (relative) of `1 / range`, about `2^-20` for
|
|
7
|
+
* `range = 2^32` over `Math.random`'s 52 to 53 bits, depending on the engine. A
|
|
8
|
+
* range wider than the generator's resolution cannot be covered uniformly by
|
|
9
|
+
* any method.
|
|
10
|
+
*
|
|
11
|
+
* Complexity: O(1), one call to `random`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const roll = randomInteger(1, 7, Math.random); // 1..6
|
|
16
|
+
* ```;
|
|
17
|
+
*
|
|
18
|
+
* @param minInclusive The smallest result; a safe integer.
|
|
19
|
+
* @param maxExclusive The bound, never returned; a safe integer greater than
|
|
20
|
+
* `minInclusive`, with `maxExclusive - minInclusive` at most `2^53`.
|
|
21
|
+
* @param random A source in `[0, 1)`.
|
|
22
|
+
* @throws {RangeError} When a bound is not a safe integer, the range is empty
|
|
23
|
+
* or wider than `2^53`.
|
|
24
|
+
*/
|
|
25
|
+
export function randomInteger(minInclusive, maxExclusive, random) {
|
|
26
|
+
if (!Number.isSafeInteger(minInclusive) ||
|
|
27
|
+
!Number.isSafeInteger(maxExclusive)) {
|
|
28
|
+
throw new RangeError("bounds must be safe integers");
|
|
29
|
+
}
|
|
30
|
+
// Compared before subtracting: the difference of two safe integers can
|
|
31
|
+
// round to 2^53 when the true range is 2^53 + 1.
|
|
32
|
+
if (!(maxExclusive > minInclusive) || maxExclusive > minInclusive + 2 ** 53) {
|
|
33
|
+
throw new RangeError("maxExclusive must exceed minInclusive by at most 2^53");
|
|
34
|
+
}
|
|
35
|
+
const range = maxExclusive - minInclusive;
|
|
36
|
+
const offset = Math.floor(random() * range);
|
|
37
|
+
// random() * range can round up to range itself for a huge range.
|
|
38
|
+
return minInclusive + Math.min(offset, range - 1);
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=random-integer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random-integer.js","sourceRoot":"","sources":["../../src/sample/random-integer.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,aAAa,CAC5B,YAAoB,EACpB,YAAoB,EACpB,MAAgB;IAEhB,IACC,CAAC,MAAM,CAAC,aAAa,CAAC,YAAY,CAAC;QACnC,CAAC,MAAM,CAAC,aAAa,CAAC,YAAY,CAAC,EAClC,CAAC;QACF,MAAM,IAAI,UAAU,CAAC,8BAA8B,CAAC,CAAC;IACtD,CAAC;IACD,uEAAuE;IACvE,iDAAiD;IACjD,IAAI,CAAC,CAAC,YAAY,GAAG,YAAY,CAAC,IAAI,YAAY,GAAG,YAAY,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC;QAC7E,MAAM,IAAI,UAAU,CACnB,uDAAuD,CACvD,CAAC;IACH,CAAC;IACD,MAAM,KAAK,GAAG,YAAY,GAAG,YAAY,CAAC;IAC1C,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC;IAC5C,kEAAkE;IAClE,OAAO,YAAY,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;AACnD,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shuffle.d.ts","sourceRoot":"","sources":["../../src/sample/shuffle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"shuffle.d.ts","sourceRoot":"","sources":["../../src/sample/shuffle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAIxC,MAAM,MAAM,gBAAgB,GAAG,aAAa,GAAG,SAAS,CAAC;AAEzD,MAAM,MAAM,cAAc,GAAG;IAC5B;;;;;OAKG;IACH,IAAI,CAAC,EAAE,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;IAE5B;;;;OAIG;IACH,SAAS,CAAC,EAAE,gBAAgB,CAAC;CAC7B,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,CAAC,EAAE,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,CAAC,EAAE,CAuB7E"}
|
package/dist/sample/shuffle.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { randomInteger } from "./random-integer.js";
|
|
1
2
|
/**
|
|
2
3
|
* Shuffles `items` using the Fisher-Yates algorithm and returns a new shuffled
|
|
3
4
|
* array.
|
|
@@ -15,10 +16,10 @@ export function shuffle(items, options) {
|
|
|
15
16
|
const algorithm = options?.algorithm ?? "fisherYates";
|
|
16
17
|
const randomize = (i) => {
|
|
17
18
|
if (algorithm === "sattolo") {
|
|
18
|
-
return
|
|
19
|
+
return randomInteger(0, i, rand);
|
|
19
20
|
}
|
|
20
21
|
else {
|
|
21
|
-
return
|
|
22
|
+
return randomInteger(0, i + 1, rand);
|
|
22
23
|
}
|
|
23
24
|
};
|
|
24
25
|
const out = items.slice();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shuffle.js","sourceRoot":"","sources":["../../src/sample/shuffle.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"shuffle.js","sourceRoot":"","sources":["../../src/sample/shuffle.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAqBpD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,OAAO,CAAI,KAAmB,EAAE,OAAwB;IACvE,MAAM,IAAI,GAAG,OAAO,EAAE,IAAI,IAAI,IAAI,CAAC,MAAM,CAAC;IAC1C,MAAM,SAAS,GAAG,OAAO,EAAE,SAAS,IAAI,aAAa,CAAC;IAEtD,MAAM,SAAS,GAAG,CAAC,CAAS,EAAU,EAAE;QACvC,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;YAC7B,OAAO,aAAa,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC;QAClC,CAAC;aAAM,CAAC;YACP,OAAO,aAAa,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC;QACtC,CAAC;IACF,CAAC,CAAC;IAEF,MAAM,GAAG,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;IAE1B,KAAK,IAAI,CAAC,GAAG,GAAG,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QACzC,MAAM,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC;QAEvB,MAAM,GAAG,GAAG,GAAG,CAAC,CAAC,CAAE,CAAC;QACpB,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAE,CAAC;QACjB,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC;IACd,CAAC;IAED,OAAO,GAAG,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Comparator } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Returns the item that comes first under `compare`, provided it is the only
|
|
4
|
+
* one: `undefined` when the iterable is empty or when two or more items tie for
|
|
5
|
+
* the first place (the comparator answers `0` between the best item and
|
|
6
|
+
* another).
|
|
7
|
+
*
|
|
8
|
+
* Use it to pick a winner only when the order is decisive, and fall back to a
|
|
9
|
+
* default or a message when it is not. `minBy` always answers; this does not
|
|
10
|
+
* guess between equals. Unlike `minBy`'s `null`, `undefined` stands for both
|
|
11
|
+
* "empty" and "tied", neither having a single minimum.
|
|
12
|
+
*
|
|
13
|
+
* Time complexity: O(n), one pass and one comparison per item after the first.
|
|
14
|
+
* Memory: O(1). `compare` must be a strict weak ordering: a tie is `0`.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const byLength = (a: string, b: string) => Math.sign(a.length - b.length);
|
|
19
|
+
* uniqueMinBy(["ccc", "a", "bb"], byLength); // "a"
|
|
20
|
+
* uniqueMinBy(["a", "b", "cc"], byLength); // undefined: "a" and "b" tie
|
|
21
|
+
* ```;
|
|
22
|
+
*
|
|
23
|
+
* @param iterable The input iterable.
|
|
24
|
+
* @param compare Orders two items; `0` means they are equivalent.
|
|
25
|
+
* @returns The single smallest item, or `undefined`.
|
|
26
|
+
*/
|
|
27
|
+
export declare function uniqueMinBy<T>(iterable: Iterable<T>, compare: Comparator<T>): T | undefined;
|
|
28
|
+
//# sourceMappingURL=unique-min-by.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unique-min-by.d.ts","sourceRoot":"","sources":["../../src/select/unique-min-by.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAC5B,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrB,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,GACpB,CAAC,GAAG,SAAS,CAuBf"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the item that comes first under `compare`, provided it is the only
|
|
3
|
+
* one: `undefined` when the iterable is empty or when two or more items tie for
|
|
4
|
+
* the first place (the comparator answers `0` between the best item and
|
|
5
|
+
* another).
|
|
6
|
+
*
|
|
7
|
+
* Use it to pick a winner only when the order is decisive, and fall back to a
|
|
8
|
+
* default or a message when it is not. `minBy` always answers; this does not
|
|
9
|
+
* guess between equals. Unlike `minBy`'s `null`, `undefined` stands for both
|
|
10
|
+
* "empty" and "tied", neither having a single minimum.
|
|
11
|
+
*
|
|
12
|
+
* Time complexity: O(n), one pass and one comparison per item after the first.
|
|
13
|
+
* Memory: O(1). `compare` must be a strict weak ordering: a tie is `0`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const byLength = (a: string, b: string) => Math.sign(a.length - b.length);
|
|
18
|
+
* uniqueMinBy(["ccc", "a", "bb"], byLength); // "a"
|
|
19
|
+
* uniqueMinBy(["a", "b", "cc"], byLength); // undefined: "a" and "b" tie
|
|
20
|
+
* ```;
|
|
21
|
+
*
|
|
22
|
+
* @param iterable The input iterable.
|
|
23
|
+
* @param compare Orders two items; `0` means they are equivalent.
|
|
24
|
+
* @returns The single smallest item, or `undefined`.
|
|
25
|
+
*/
|
|
26
|
+
export function uniqueMinBy(iterable, compare) {
|
|
27
|
+
let best;
|
|
28
|
+
let hasBest = false;
|
|
29
|
+
let isTied = false;
|
|
30
|
+
for (const item of iterable) {
|
|
31
|
+
if (!hasBest) {
|
|
32
|
+
best = item;
|
|
33
|
+
hasBest = true;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
const order = compare(item, best);
|
|
37
|
+
if (order < 0) {
|
|
38
|
+
best = item;
|
|
39
|
+
isTied = false;
|
|
40
|
+
}
|
|
41
|
+
else if (order === 0) {
|
|
42
|
+
isTied = true;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return isTied ? undefined : best;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=unique-min-by.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unique-min-by.js","sourceRoot":"","sources":["../../src/select/unique-min-by.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,WAAW,CAC1B,QAAqB,EACrB,OAAsB;IAEtB,IAAI,IAAmB,CAAC;IACxB,IAAI,OAAO,GAAG,KAAK,CAAC;IACpB,IAAI,MAAM,GAAG,KAAK,CAAC;IAEnB,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;QAC7B,IAAI,CAAC,OAAO,EAAE,CAAC;YACd,IAAI,GAAG,IAAI,CAAC;YACZ,OAAO,GAAG,IAAI,CAAC;YACf,SAAS;QACV,CAAC;QAED,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,EAAE,IAAS,CAAC,CAAC;QAEvC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;YACf,IAAI,GAAG,IAAI,CAAC;YACZ,MAAM,GAAG,KAAK,CAAC;QAChB,CAAC;aAAM,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;YACxB,MAAM,GAAG,IAAI,CAAC;QACf,CAAC;IACF,CAAC;IAED,OAAO,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC;AAClC,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { EqualityComparisonStrategy } from "@ac-kit/core";
|
|
2
|
+
/** The two operations `duplicatesBy` and `overrideBy` need of a set of keys. */
|
|
3
|
+
export type KeySet_<K> = {
|
|
4
|
+
has(key: K): boolean;
|
|
5
|
+
add(key: K): unknown;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* A set of keys under `comparisonStrategy`: a native `Set` for the default
|
|
9
|
+
* `"sameValueZero"` (O(1) per operation), an `EnhancedSet` otherwise (O(n) per
|
|
10
|
+
* operation, since an arbitrary equality cannot be hashed).
|
|
11
|
+
*/
|
|
12
|
+
export declare function createKeySet_<K>(comparisonStrategy: EqualityComparisonStrategy<K, K>): KeySet_<K>;
|
|
13
|
+
//# sourceMappingURL=_key-set.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_key-set.d.ts","sourceRoot":"","sources":["../../src/set/_key-set.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAG/D,gFAAgF;AAChF,MAAM,MAAM,OAAO,CAAC,CAAC,IAAI;IACxB,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC;IACrB,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC;CACrB,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAC9B,kBAAkB,EAAE,0BAA0B,CAAC,CAAC,EAAE,CAAC,CAAC,GAClD,OAAO,CAAC,CAAC,CAAC,CAMZ"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { EnhancedSet } from "@ac-kit/data";
|
|
2
|
+
/**
|
|
3
|
+
* A set of keys under `comparisonStrategy`: a native `Set` for the default
|
|
4
|
+
* `"sameValueZero"` (O(1) per operation), an `EnhancedSet` otherwise (O(n) per
|
|
5
|
+
* operation, since an arbitrary equality cannot be hashed).
|
|
6
|
+
*/
|
|
7
|
+
export function createKeySet_(comparisonStrategy) {
|
|
8
|
+
if (comparisonStrategy === "sameValueZero") {
|
|
9
|
+
return new Set();
|
|
10
|
+
}
|
|
11
|
+
return new EnhancedSet(undefined, { comparisonStrategy });
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=_key-set.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_key-set.js","sourceRoot":"","sources":["../../src/set/_key-set.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAQ3C;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAC5B,kBAAoD;IAEpD,IAAI,kBAAkB,KAAK,eAAe,EAAE,CAAC;QAC5C,OAAO,IAAI,GAAG,EAAK,CAAC;IACrB,CAAC;IAED,OAAO,IAAI,WAAW,CAAI,SAAS,EAAE,EAAE,kBAAkB,EAAE,CAAC,CAAC;AAC9D,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Callable, EqualityComparisonStrategy } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Yields every item whose `keyOf(item)` was already produced by an earlier
|
|
4
|
+
* item, with its position, in iteration order. The first item of each key is
|
|
5
|
+
* never yielded, so the items left out are exactly those `uniqBy` yields.
|
|
6
|
+
*
|
|
7
|
+
* Use it to report each repeat (a validation issue per second use of an id). An
|
|
8
|
+
* item whose key occurs three times is yielded twice. `uniqBy` is the
|
|
9
|
+
* complement; to ask only whether a repeat exists, pull one value.
|
|
10
|
+
*
|
|
11
|
+
* Time complexity: O(n) under the default `"sameValueZero"`, O(n·k) under any
|
|
12
|
+
* other strategy, where k is the number of distinct keys, since a custom
|
|
13
|
+
* equality cannot be hashed. Memory: O(k). `keyOf` is called once per item, and
|
|
14
|
+
* nothing runs before the first value is pulled.
|
|
15
|
+
*
|
|
16
|
+
* A key of `undefined` is a key like any other: filter such items out first if
|
|
17
|
+
* they mean "no key". Under the default strategy two keys are the same
|
|
18
|
+
* according to `SameValueZero`, so a composite key must be a primitive
|
|
19
|
+
* (`JSON.stringify([a, b])`) or compared by a custom strategy.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const refs = ["a", "b", "a", "c", "a"];
|
|
24
|
+
* [...duplicatesBy(refs, (ref) => ref)];
|
|
25
|
+
* // [["a", 2], ["a", 4]]
|
|
26
|
+
* ```;
|
|
27
|
+
*
|
|
28
|
+
* @param iterable The input iterable.
|
|
29
|
+
* @param keyOf A function that takes an item and returns its key.
|
|
30
|
+
* @param comparisonStrategy The equality comparison strategy for keys.
|
|
31
|
+
* @returns An iterator over `[item, index]` for each repeat, `index` being the
|
|
32
|
+
* item's position in `iterable`.
|
|
33
|
+
*/
|
|
34
|
+
export declare function duplicatesBy<T, K>(iterable: Iterable<T>, keyOf: Callable<[T], K>, comparisonStrategy?: EqualityComparisonStrategy<K, K>): IterableIterator<[item: T, index: number]>;
|
|
35
|
+
//# sourceMappingURL=duplicates-by.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"duplicates-by.d.ts","sourceRoot":"","sources":["../../src/set/duplicates-by.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAIzE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAiB,YAAY,CAAC,CAAC,EAAE,CAAC,EACjC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrB,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EACvB,kBAAkB,GAAE,0BAA0B,CAAC,CAAC,EAAE,CAAC,CAAmB,GACpE,gBAAgB,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC,CAe5C"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { createKeySet_ } from "./_key-set.js";
|
|
2
|
+
/**
|
|
3
|
+
* Yields every item whose `keyOf(item)` was already produced by an earlier
|
|
4
|
+
* item, with its position, in iteration order. The first item of each key is
|
|
5
|
+
* never yielded, so the items left out are exactly those `uniqBy` yields.
|
|
6
|
+
*
|
|
7
|
+
* Use it to report each repeat (a validation issue per second use of an id). An
|
|
8
|
+
* item whose key occurs three times is yielded twice. `uniqBy` is the
|
|
9
|
+
* complement; to ask only whether a repeat exists, pull one value.
|
|
10
|
+
*
|
|
11
|
+
* Time complexity: O(n) under the default `"sameValueZero"`, O(n·k) under any
|
|
12
|
+
* other strategy, where k is the number of distinct keys, since a custom
|
|
13
|
+
* equality cannot be hashed. Memory: O(k). `keyOf` is called once per item, and
|
|
14
|
+
* nothing runs before the first value is pulled.
|
|
15
|
+
*
|
|
16
|
+
* A key of `undefined` is a key like any other: filter such items out first if
|
|
17
|
+
* they mean "no key". Under the default strategy two keys are the same
|
|
18
|
+
* according to `SameValueZero`, so a composite key must be a primitive
|
|
19
|
+
* (`JSON.stringify([a, b])`) or compared by a custom strategy.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const refs = ["a", "b", "a", "c", "a"];
|
|
24
|
+
* [...duplicatesBy(refs, (ref) => ref)];
|
|
25
|
+
* // [["a", 2], ["a", 4]]
|
|
26
|
+
* ```;
|
|
27
|
+
*
|
|
28
|
+
* @param iterable The input iterable.
|
|
29
|
+
* @param keyOf A function that takes an item and returns its key.
|
|
30
|
+
* @param comparisonStrategy The equality comparison strategy for keys.
|
|
31
|
+
* @returns An iterator over `[item, index]` for each repeat, `index` being the
|
|
32
|
+
* item's position in `iterable`.
|
|
33
|
+
*/
|
|
34
|
+
export function* duplicatesBy(iterable, keyOf, comparisonStrategy = "sameValueZero") {
|
|
35
|
+
const seen = createKeySet_(comparisonStrategy);
|
|
36
|
+
let index = 0;
|
|
37
|
+
for (const item of iterable) {
|
|
38
|
+
const key = keyOf(item);
|
|
39
|
+
if (seen.has(key)) {
|
|
40
|
+
yield [item, index];
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
seen.add(key);
|
|
44
|
+
}
|
|
45
|
+
index++;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=duplicates-by.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"duplicates-by.js","sourceRoot":"","sources":["../../src/set/duplicates-by.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAE9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,SAAS,CAAC,CAAC,YAAY,CAC5B,QAAqB,EACrB,KAAuB,EACvB,kBAAkB,GAAqC,eAAe;IAEtE,MAAM,IAAI,GAAG,aAAa,CAAI,kBAAkB,CAAC,CAAC;IAClD,IAAI,KAAK,GAAG,CAAC,CAAC;IAEd,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;QAC7B,MAAM,GAAG,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC;QAExB,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACnB,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QACrB,CAAC;aAAM,CAAC;YACP,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACf,CAAC;QAED,KAAK,EAAE,CAAC;IACT,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Callable, EqualityComparisonStrategy } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Layers `override` over `base`: the items of `base` whose key no item of
|
|
4
|
+
* `override` has, in their order, followed by all of `override`, in its order.
|
|
5
|
+
*
|
|
6
|
+
* This is the merge of two keyed lists where the later one wins (settings
|
|
7
|
+
* entries, headers, cookies). Items inside one list are not deduplicated: two
|
|
8
|
+
* base items with the same key both stay unless `override` has that key, and
|
|
9
|
+
* every item of `override` is kept even when two share a key. Neither input is
|
|
10
|
+
* modified.
|
|
11
|
+
*
|
|
12
|
+
* Time complexity: O(n + m) under the default `"sameValueZero"`; O(n·m) under
|
|
13
|
+
* any other strategy, since a custom equality cannot be hashed. `keyOf` is
|
|
14
|
+
* called once per item of `override` and once per item of `base`. Memory: O(m)
|
|
15
|
+
* for the keys, plus the result.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* overrideBy(
|
|
20
|
+
* [{ id: 1, v: "a" }, { id: 2, v: "b" }],
|
|
21
|
+
* [{ id: 2, v: "B" }, { id: 3, v: "C" }],
|
|
22
|
+
* (entry) => entry.id,
|
|
23
|
+
* );
|
|
24
|
+
* // [{ id: 1, v: "a" }, { id: 2, v: "B" }, { id: 3, v: "C" }]
|
|
25
|
+
* ```;
|
|
26
|
+
*
|
|
27
|
+
* @param base The items kept unless overridden.
|
|
28
|
+
* @param override The items that replace those of `base` with the same key.
|
|
29
|
+
* @param keyOf A function that takes an item and returns its key.
|
|
30
|
+
* @param comparisonStrategy The equality comparison strategy for keys.
|
|
31
|
+
* @returns A new array.
|
|
32
|
+
*/
|
|
33
|
+
export declare function overrideBy<T, K>(base: Iterable<T>, override: Iterable<T>, keyOf: Callable<[T], K>, comparisonStrategy?: EqualityComparisonStrategy<K, K>): T[];
|
|
34
|
+
//# sourceMappingURL=override-by.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"override-by.d.ts","sourceRoot":"","sources":["../../src/set/override-by.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAIzE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,CAAC,EAC9B,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,EACjB,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrB,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EACvB,kBAAkB,GAAE,0BAA0B,CAAC,CAAC,EAAE,CAAC,CAAmB,GACpE,CAAC,EAAE,CAqBL"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { createKeySet_ } from "./_key-set.js";
|
|
2
|
+
/**
|
|
3
|
+
* Layers `override` over `base`: the items of `base` whose key no item of
|
|
4
|
+
* `override` has, in their order, followed by all of `override`, in its order.
|
|
5
|
+
*
|
|
6
|
+
* This is the merge of two keyed lists where the later one wins (settings
|
|
7
|
+
* entries, headers, cookies). Items inside one list are not deduplicated: two
|
|
8
|
+
* base items with the same key both stay unless `override` has that key, and
|
|
9
|
+
* every item of `override` is kept even when two share a key. Neither input is
|
|
10
|
+
* modified.
|
|
11
|
+
*
|
|
12
|
+
* Time complexity: O(n + m) under the default `"sameValueZero"`; O(n·m) under
|
|
13
|
+
* any other strategy, since a custom equality cannot be hashed. `keyOf` is
|
|
14
|
+
* called once per item of `override` and once per item of `base`. Memory: O(m)
|
|
15
|
+
* for the keys, plus the result.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* overrideBy(
|
|
20
|
+
* [{ id: 1, v: "a" }, { id: 2, v: "b" }],
|
|
21
|
+
* [{ id: 2, v: "B" }, { id: 3, v: "C" }],
|
|
22
|
+
* (entry) => entry.id,
|
|
23
|
+
* );
|
|
24
|
+
* // [{ id: 1, v: "a" }, { id: 2, v: "B" }, { id: 3, v: "C" }]
|
|
25
|
+
* ```;
|
|
26
|
+
*
|
|
27
|
+
* @param base The items kept unless overridden.
|
|
28
|
+
* @param override The items that replace those of `base` with the same key.
|
|
29
|
+
* @param keyOf A function that takes an item and returns its key.
|
|
30
|
+
* @param comparisonStrategy The equality comparison strategy for keys.
|
|
31
|
+
* @returns A new array.
|
|
32
|
+
*/
|
|
33
|
+
export function overrideBy(base, override, keyOf, comparisonStrategy = "sameValueZero") {
|
|
34
|
+
const overridden = createKeySet_(comparisonStrategy);
|
|
35
|
+
const overrideItems = Array.from(override);
|
|
36
|
+
for (const item of overrideItems) {
|
|
37
|
+
overridden.add(keyOf(item));
|
|
38
|
+
}
|
|
39
|
+
const result = [];
|
|
40
|
+
for (const item of base) {
|
|
41
|
+
if (!overridden.has(keyOf(item))) {
|
|
42
|
+
result.push(item);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
for (const item of overrideItems) {
|
|
46
|
+
result.push(item);
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=override-by.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"override-by.js","sourceRoot":"","sources":["../../src/set/override-by.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAE9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,UAAU,UAAU,CACzB,IAAiB,EACjB,QAAqB,EACrB,KAAuB,EACvB,kBAAkB,GAAqC,eAAe;IAEtE,MAAM,UAAU,GAAG,aAAa,CAAI,kBAAkB,CAAC,CAAC;IACxD,MAAM,aAAa,GAAG,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAE3C,KAAK,MAAM,IAAI,IAAI,aAAa,EAAE,CAAC;QAClC,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;IAC7B,CAAC;IAED,MAAM,MAAM,GAAQ,EAAE,CAAC;IAEvB,KAAK,MAAM,IAAI,IAAI,IAAI,EAAE,CAAC;QACzB,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;YAClC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACnB,CAAC;IACF,CAAC;IAED,KAAK,MAAM,IAAI,IAAI,aAAa,EAAE,CAAC;QAClC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACnB,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC"}
|
package/dist/set/uniq-by.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uniq-by.d.ts","sourceRoot":"","sources":["../../src/set/uniq-by.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"uniq-by.d.ts","sourceRoot":"","sources":["../../src/set/uniq-by.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAIzE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAiB,MAAM,CAAC,CAAC,EAAE,CAAC,EAC3B,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrB,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EACvB,kBAAkB,GAAE,0BAA0B,CAAC,CAAC,EAAE,CAAC,CAAmB,GACpE,gBAAgB,CAAC,CAAC,CAAC,CAWrB"}
|
package/dist/set/uniq-by.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { EnhancedSet } from "@ac-kit/data";
|
|
1
|
+
import { createKeySet_ } from "./_key-set.js";
|
|
3
2
|
/**
|
|
4
3
|
* Yields the first item seen for each distinct `keyOf(item)`, in
|
|
5
4
|
* first-occurrence order.
|
|
@@ -23,25 +22,12 @@ import { EnhancedSet } from "@ac-kit/data";
|
|
|
23
22
|
* @returns An iterator over the first item seen for each distinct key.
|
|
24
23
|
*/
|
|
25
24
|
export function* uniqBy(iterable, keyOf, comparisonStrategy = "sameValueZero") {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
seen.add(key);
|
|
33
|
-
yield item;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
const seen = new EnhancedSet(undefined, { comparisonStrategy });
|
|
39
|
-
for (const item of iterable) {
|
|
40
|
-
const key = keyOf(item);
|
|
41
|
-
if (!seen.has(key)) {
|
|
42
|
-
seen.add(key);
|
|
43
|
-
yield item;
|
|
44
|
-
}
|
|
25
|
+
const seen = createKeySet_(comparisonStrategy);
|
|
26
|
+
for (const item of iterable) {
|
|
27
|
+
const key = keyOf(item);
|
|
28
|
+
if (!seen.has(key)) {
|
|
29
|
+
seen.add(key);
|
|
30
|
+
yield item;
|
|
45
31
|
}
|
|
46
32
|
}
|
|
47
33
|
}
|
package/dist/set/uniq-by.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uniq-by.js","sourceRoot":"","sources":["../../src/set/uniq-by.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"uniq-by.js","sourceRoot":"","sources":["../../src/set/uniq-by.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAE9C;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,SAAS,CAAC,CAAC,MAAM,CACtB,QAAqB,EACrB,KAAuB,EACvB,kBAAkB,GAAqC,eAAe;IAEtE,MAAM,IAAI,GAAG,aAAa,CAAI,kBAAkB,CAAC,CAAC;IAElD,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;QAC7B,MAAM,GAAG,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC;QAExB,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACpB,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YACd,MAAM,IAAI,CAAC;QACZ,CAAC;IACF,CAAC;AACF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ac-kit/algo",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Classic algorithms that work on your own types rather than on numbers: searching, sorting, grouping, graph traversal and string distance.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"algorithms",
|
|
@@ -45,11 +45,12 @@
|
|
|
45
45
|
"prepack": "yarn build"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@ac-kit/core": "^0.
|
|
49
|
-
"@ac-kit/data": "^0.3.
|
|
48
|
+
"@ac-kit/core": "^0.10.0",
|
|
49
|
+
"@ac-kit/data": "^0.3.2",
|
|
50
50
|
"@types/web": "^0.0.367"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
+
"@ac-kit/math-random": "0.2.0",
|
|
53
54
|
"@ac-kit/tsconfig": "0.1.0",
|
|
54
55
|
"@ac-kit/typedoc-config": "0.1.0",
|
|
55
56
|
"@ac-kit/vitest-config": "0.1.1",
|