@ac-kit/algo 0.4.1 → 0.5.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 +72 -0
- package/README.md +18 -8
- package/dist/graph/children-of.d.ts +9 -0
- package/dist/graph/children-of.d.ts.map +1 -0
- package/dist/graph/children-of.js +2 -0
- package/dist/graph/children-of.js.map +1 -0
- package/dist/graph/find-cycle.d.ts +31 -0
- package/dist/graph/find-cycle.d.ts.map +1 -0
- package/dist/graph/find-cycle.js +84 -0
- package/dist/graph/find-cycle.js.map +1 -0
- package/dist/graph/hierarchy-from-parents.d.ts +41 -0
- package/dist/graph/hierarchy-from-parents.d.ts.map +1 -0
- package/dist/graph/hierarchy-from-parents.js +75 -0
- package/dist/graph/hierarchy-from-parents.js.map +1 -0
- package/dist/group/group-indices.d.ts +56 -0
- package/dist/group/group-indices.d.ts.map +1 -0
- package/dist/group/group-indices.js +112 -0
- package/dist/group/group-indices.js.map +1 -0
- package/dist/index.d.ts +6 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/interval/is-in-sorted-intervals.js +2 -2
- package/dist/interval/is-in-sorted-intervals.js.map +1 -1
- package/dist/ordered/_binary-search.d.ts +6 -3
- package/dist/ordered/_binary-search.d.ts.map +1 -1
- package/dist/ordered/_binary-search.js +8 -5
- package/dist/ordered/_binary-search.js.map +1 -1
- package/dist/ordered/binary-search.d.ts +26 -0
- package/dist/ordered/binary-search.d.ts.map +1 -0
- package/dist/ordered/binary-search.js +34 -0
- package/dist/ordered/binary-search.js.map +1 -0
- package/dist/ordered/bisect-center.js +2 -2
- package/dist/ordered/bisect-center.js.map +1 -1
- package/dist/ordered/bisect-left.js +2 -2
- package/dist/ordered/bisect-left.js.map +1 -1
- package/dist/ordered/bisect-right.js +2 -2
- package/dist/ordered/bisect-right.js.map +1 -1
- package/dist/sample/_random-index.d.ts +4 -0
- package/dist/sample/_random-index.d.ts.map +1 -0
- package/dist/sample/_random-index.js +5 -0
- package/dist/sample/_random-index.js.map +1 -0
- package/dist/sample/choice.js +2 -2
- package/dist/sample/choice.js.map +1 -1
- package/dist/sample/shuffle.js +3 -3
- package/dist/sample/shuffle.js.map +1 -1
- package/dist/sort/sorted-indices.d.ts +18 -0
- package/dist/sort/sorted-indices.d.ts.map +1 -0
- package/dist/sort/sorted-indices.js +22 -0
- package/dist/sort/sorted-indices.js.map +1 -0
- package/package.json +5 -5
- package/dist/sample/random-boolean.d.ts +0 -23
- package/dist/sample/random-boolean.d.ts.map +0 -1
- package/dist/sample/random-boolean.js +0 -27
- package/dist/sample/random-boolean.js.map +0 -1
- package/dist/sample/random-integer.d.ts +0 -27
- package/dist/sample/random-integer.d.ts.map +0 -1
- package/dist/sample/random-integer.js +0 -40
- package/dist/sample/random-integer.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,77 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.5.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 34e8cfa: `findCycle(roots, childrenOf, options?)` returns one directed cycle reachable
|
|
8
|
+
from `roots` as its nodes in edge order (each has an edge to the next, the last
|
|
9
|
+
to the first; a self-loop is a single node), or `undefined` when there is none.
|
|
10
|
+
Nodes are compared by identity, the search is iterative so a deep graph cannot
|
|
11
|
+
overflow the stack, and `options.budget` is charged 1 per node entered and 1 per
|
|
12
|
+
successor scanned (a repeated successor included). O(V + E) time.
|
|
13
|
+
- 34e8cfa: `groupIndices(items, groupOf, groupCount)` groups the positions of `items` by
|
|
14
|
+
the group number `groupOf(item, index)` gives each one and returns them as two
|
|
15
|
+
`Uint32Array`s, `offsets` (`groupCount + 1` entries) and `indices`, so group
|
|
16
|
+
`g` is `indices[offsets[g] … offsets[g + 1])` in ascending order: the
|
|
17
|
+
compressed sparse row layout of an adjacency list or a parent → children
|
|
18
|
+
table, in O(n + groupCount). Group numbers already in an array take the
|
|
19
|
+
identity callback (`groupIndices(parentOf, (parent) => parent, n)`). A group
|
|
20
|
+
of `-1` leaves the position out; any other result that is not an integer in
|
|
21
|
+
`[0, groupCount)` throws a `RangeError` naming the position and the value.
|
|
22
|
+
`groupOf` is called twice per item and must be deterministic: if it answers
|
|
23
|
+
differently the second time, the result groups the second answers when the
|
|
24
|
+
group sizes still match, and a `RangeError` is thrown otherwise.
|
|
25
|
+
- 34e8cfa: New `hierarchyFromParents(items, { keyOf, parentKeyOf })`: children lists from
|
|
26
|
+
rows that carry a key and a parent key (d3 `stratify`), as flat `Uint32Array`
|
|
27
|
+
offsets and indices with a ready-made `childrenOf`. One root is used as it is,
|
|
28
|
+
zero or several get a synthetic root; keys compare as `Map` keys and children
|
|
29
|
+
keep input order. Throws `RangeError` on a duplicate key or an unknown parent
|
|
30
|
+
key, naming the rows and the key (with `@ac-kit/core`'s `describeValue`), or on
|
|
31
|
+
a cycle. New type `ChildrenOf<TNode>`, the callback `walkTree` and the
|
|
32
|
+
hierarchy layouts take.
|
|
33
|
+
|
|
34
|
+
`@ac-kit/algo` no longer depends on `@ac-kit/math-geometry`,
|
|
35
|
+
`@ac-kit/math-linear` or `@ac-kit/math-scalar`.
|
|
36
|
+
- 34e8cfa: `randomInteger` and `randomBoolean` move from `@ac-kit/algo` to
|
|
37
|
+
`@ac-kit/math-random`, with unchanged behaviour. **Breaking for
|
|
38
|
+
`@ac-kit/algo`:** import them from `@ac-kit/math-random` instead
|
|
39
|
+
(`import { randomBoolean, randomInteger } from "@ac-kit/math-random"`);
|
|
40
|
+
`choice` and `shuffle` stay in `@ac-kit/algo`. `@ac-kit/math-stats` and
|
|
41
|
+
`@ac-kit/model-markup` now depend on `@ac-kit/math-random`.
|
|
42
|
+
- 34e8cfa: `sortedIndices(values, compare)` returns the positions of `values` in the order
|
|
43
|
+
a comparator sorts them (an argsort), stable on ties, without modifying the
|
|
44
|
+
values. It accepts any array-like, typed arrays included, and returns a plain
|
|
45
|
+
`number[]`.
|
|
46
|
+
- 34e8cfa: `binarySearch(low, high, predicate)` finds the first integer in `[low, high)`
|
|
47
|
+
where a monotonic predicate turns false — what C++ and Rust call
|
|
48
|
+
`partition_point`. It searches an answer rather than an array, the smallest
|
|
49
|
+
capacity that fits or the first millisecond a condition holds, over any range of
|
|
50
|
+
safe integers.
|
|
51
|
+
|
|
52
|
+
### Patch Changes
|
|
53
|
+
|
|
54
|
+
- Updated dependencies [34e8cfa]
|
|
55
|
+
- Updated dependencies [34e8cfa]
|
|
56
|
+
- Updated dependencies [34e8cfa]
|
|
57
|
+
- Updated dependencies [34e8cfa]
|
|
58
|
+
- Updated dependencies [34e8cfa]
|
|
59
|
+
- Updated dependencies [34e8cfa]
|
|
60
|
+
- Updated dependencies [34e8cfa]
|
|
61
|
+
- Updated dependencies [34e8cfa]
|
|
62
|
+
- Updated dependencies [34e8cfa]
|
|
63
|
+
- Updated dependencies [34e8cfa]
|
|
64
|
+
- Updated dependencies [34e8cfa]
|
|
65
|
+
- Updated dependencies [34e8cfa]
|
|
66
|
+
- Updated dependencies [34e8cfa]
|
|
67
|
+
- Updated dependencies [34e8cfa]
|
|
68
|
+
- Updated dependencies [34e8cfa]
|
|
69
|
+
- Updated dependencies [34e8cfa]
|
|
70
|
+
- Updated dependencies [34e8cfa]
|
|
71
|
+
- Updated dependencies [34e8cfa]
|
|
72
|
+
- @ac-kit/core@0.12.0
|
|
73
|
+
- @ac-kit/data@0.4.0
|
|
74
|
+
|
|
3
75
|
## 0.4.1
|
|
4
76
|
|
|
5
77
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -31,7 +31,10 @@ const distinct = [...uniqBy(events, (event) => event.correlationId)];
|
|
|
31
31
|
optional work budget, safe on hostile deep trees), `aStar`
|
|
32
32
|
(which is Dijkstra's algorithm when you give it no heuristic) and greedy
|
|
33
33
|
`bestFirstSearch`, all over a `childrenOf`/`neighboursOf` callback rather than
|
|
34
|
-
any particular graph type.
|
|
34
|
+
any particular graph type. `findCycle` returns one concrete directed cycle
|
|
35
|
+
(or nothing), for an error message that names it. `hierarchyFromParents` turns rows with a key and a
|
|
36
|
+
parent key (a chart's frame) into the `childrenOf` those take, as flat
|
|
37
|
+
`Uint32Array` offsets and indices (d3 `stratify`).
|
|
35
38
|
- **Recursion without a stack** — `runTrampoline` runs a recursive algorithm
|
|
36
39
|
written as native synchronous generators (`const value = yield child`) on
|
|
37
40
|
an explicit stack, so a hostile nesting depth costs heap, not a stack
|
|
@@ -42,16 +45,23 @@ const distinct = [...uniqBy(events, (event) => event.correlationId)];
|
|
|
42
45
|
`backtrack` for constraint search that undoes its own moves on the way back
|
|
43
46
|
out of a dead end.
|
|
44
47
|
- **Binary search** — `bisect`, `bisectLeft`, `bisectRight`, `bisectCenter` and
|
|
45
|
-
a reusable `createBisector` bound to one accessor,
|
|
46
|
-
|
|
48
|
+
a reusable `createBisector` bound to one accessor, `binarySearch` over any
|
|
49
|
+
integer range driven by a predicate alone, plus membership testing against
|
|
50
|
+
sorted, disjoint intervals.
|
|
47
51
|
- **Selection** — `quickselect` for order statistics, `minBy`/`maxBy` for the
|
|
48
52
|
extreme item by a derived key, `uniqueMinBy` for the smallest item under a
|
|
49
53
|
comparator only when no other ties with it, `selectAt` for picking positions.
|
|
50
|
-
- **Sorting** — `radixSort`, a stable, comparator-free sort over an integer key
|
|
54
|
+
- **Sorting** — `radixSort`, a stable, comparator-free sort over an integer key,
|
|
55
|
+
and `sortedIndices`, the stable permutation a comparator sorts values into
|
|
56
|
+
(an argsort), to reorder parallel arrays or write results back in place.
|
|
51
57
|
- **Combinatorics** — `combinations` and `combinationsLazy`, every `k`-subset
|
|
52
58
|
of your items.
|
|
53
59
|
- **Grouping** — `groupBy` (nesting one `Map` level per accessor), `partition`,
|
|
54
|
-
`partitionIndices`, `uniq` and `uniqBy
|
|
60
|
+
`partitionIndices`, `uniq` and `uniqBy`; `groupIndices(items, groupOf,
|
|
61
|
+
groupCount)` groups positions by an integer group number into two flat
|
|
62
|
+
`Uint32Array`s (offsets and indices, the CSR layout of an adjacency list or a
|
|
63
|
+
parent → children table) by counting sort. Group numbers already in an array
|
|
64
|
+
take the identity callback: `groupIndices(parentOf, (parent) => parent, n)`.
|
|
55
65
|
- **Set algebra** — `union`, `intersection`, `difference`,
|
|
56
66
|
`symmetricDifference`, and the `isSubsetOf` / `isSupersetOf` /
|
|
57
67
|
`isDisjointFrom` predicates — over any iterables; `duplicatesBy` yields each
|
|
@@ -59,9 +69,9 @@ const distinct = [...uniqBy(events, (event) => event.correlationId)];
|
|
|
59
69
|
over another.
|
|
60
70
|
- **Shuffling** — `shuffle` (Fisher–Yates or Sattolo) with the random source
|
|
61
71
|
injectable, so results can be made reproducible, and `spreadEvenly` to
|
|
62
|
-
interleave two collections at evenly spaced positions; `
|
|
63
|
-
|
|
64
|
-
|
|
72
|
+
interleave two collections at evenly spaced positions; `choice` draws one
|
|
73
|
+
element from an injectable random source. (`randomInteger` and
|
|
74
|
+
`randomBoolean` live in `@ac-kit/math-random`.)
|
|
65
75
|
|
|
66
76
|
## What's not included
|
|
67
77
|
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Callable } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Children of a node, or `undefined`/empty for a leaf. Called once per node;
|
|
4
|
+
* the result must not change during the call. The same shape `walkTree` takes.
|
|
5
|
+
*/
|
|
6
|
+
export type ChildrenOf<TNode> = Callable<[
|
|
7
|
+
node: TNode
|
|
8
|
+
], ArrayLike<TNode> | undefined>;
|
|
9
|
+
//# sourceMappingURL=children-of.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"children-of.d.ts","sourceRoot":"","sources":["../../src/graph/children-of.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;GAGG;AACH,MAAM,MAAM,UAAU,CAAC,KAAK,IAAI,QAAQ,CACvC;IAAC,IAAI,EAAE,KAAK;CAAC,EACb,SAAS,CAAC,KAAK,CAAC,GAAG,SAAS,CAC5B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"children-of.js","sourceRoot":"","sources":["../../src/graph/children-of.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { WorkBudget } from "@ac-kit/core";
|
|
2
|
+
export type FindCycleOptions = {
|
|
3
|
+
/**
|
|
4
|
+
* Charged 1 before each node is entered and 1 for each successor read (each
|
|
5
|
+
* edge scanned, repeated ones included), so a search over an untrusted graph
|
|
6
|
+
* can be capped. Whatever `spend` throws propagates unchanged.
|
|
7
|
+
*/
|
|
8
|
+
budget?: WorkBudget;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Finds one directed cycle reachable from `roots`, for an error message or a
|
|
12
|
+
* "cannot order these" report. Iterative depth-first search, so a hostile deep
|
|
13
|
+
* graph cannot overflow the stack.
|
|
14
|
+
*
|
|
15
|
+
* Nodes are compared by identity (`Map` semantics, so `NaN` equals `NaN`).
|
|
16
|
+
* `childrenOf` is called once per node, and the iterable it returns is read
|
|
17
|
+
* once. Whichever way the search ends, the iterators still open are closed with
|
|
18
|
+
* `return()`; an error from that is dropped in favour of the one being thrown.
|
|
19
|
+
*
|
|
20
|
+
* Time complexity: O(V + E). Memory: O(V).
|
|
21
|
+
*
|
|
22
|
+
* @param roots - Where to start; nodes already reached from an earlier root are
|
|
23
|
+
* not entered again. Pass every node to check a whole graph.
|
|
24
|
+
* @param childrenOf - The successors of a node, or `undefined` for none.
|
|
25
|
+
* @returns The nodes of one cycle in edge order: each one has an edge to the
|
|
26
|
+
* next and the last one to the first (a self-loop is a single node); the
|
|
27
|
+
* search order decides which cycle. `undefined` when no cycle is reachable.
|
|
28
|
+
* @throws Whatever `options.budget.spend`, `childrenOf` or an iterator throws.
|
|
29
|
+
*/
|
|
30
|
+
export declare function findCycle<TNode>(roots: Iterable<TNode>, childrenOf: (node: TNode) => Iterable<TNode> | undefined, options?: FindCycleOptions): TNode[] | undefined;
|
|
31
|
+
//# sourceMappingURL=find-cycle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"find-cycle.d.ts","sourceRoot":"","sources":["../../src/graph/find-cycle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C,MAAM,MAAM,gBAAgB,GAAG;IAC9B;;;;OAIG;IACH,MAAM,CAAC,EAAE,UAAU,CAAC;CACpB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAC9B,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC,EACtB,UAAU,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,QAAQ,CAAC,KAAK,CAAC,GAAG,SAAS,EACxD,OAAO,GAAE,gBAAqB,GAC5B,KAAK,EAAE,GAAG,SAAS,CAWrB"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finds one directed cycle reachable from `roots`, for an error message or a
|
|
3
|
+
* "cannot order these" report. Iterative depth-first search, so a hostile deep
|
|
4
|
+
* graph cannot overflow the stack.
|
|
5
|
+
*
|
|
6
|
+
* Nodes are compared by identity (`Map` semantics, so `NaN` equals `NaN`).
|
|
7
|
+
* `childrenOf` is called once per node, and the iterable it returns is read
|
|
8
|
+
* once. Whichever way the search ends, the iterators still open are closed with
|
|
9
|
+
* `return()`; an error from that is dropped in favour of the one being thrown.
|
|
10
|
+
*
|
|
11
|
+
* Time complexity: O(V + E). Memory: O(V).
|
|
12
|
+
*
|
|
13
|
+
* @param roots - Where to start; nodes already reached from an earlier root are
|
|
14
|
+
* not entered again. Pass every node to check a whole graph.
|
|
15
|
+
* @param childrenOf - The successors of a node, or `undefined` for none.
|
|
16
|
+
* @returns The nodes of one cycle in edge order: each one has an edge to the
|
|
17
|
+
* next and the last one to the first (a self-loop is a single node); the
|
|
18
|
+
* search order decides which cycle. `undefined` when no cycle is reachable.
|
|
19
|
+
* @throws Whatever `options.budget.spend`, `childrenOf` or an iterator throws.
|
|
20
|
+
*/
|
|
21
|
+
export function findCycle(roots, childrenOf, options = {}) {
|
|
22
|
+
const pending = [];
|
|
23
|
+
let result;
|
|
24
|
+
try {
|
|
25
|
+
result = search_(roots, childrenOf, options.budget, pending);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
closeAll_(pending, false);
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
closeAll_(pending, true);
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
function search_(roots, childrenOf, budget, pending) {
|
|
35
|
+
// Index on the current path while a node is open, -1 once it is finished.
|
|
36
|
+
const pathIndices = new Map();
|
|
37
|
+
const path = [];
|
|
38
|
+
const enter = (node) => {
|
|
39
|
+
budget?.spend(1);
|
|
40
|
+
pathIndices.set(node, path.length);
|
|
41
|
+
path.push(node);
|
|
42
|
+
pending.push((childrenOf(node) ?? [])[Symbol.iterator]());
|
|
43
|
+
};
|
|
44
|
+
for (const root of roots) {
|
|
45
|
+
if (pathIndices.has(root)) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
enter(root);
|
|
49
|
+
while (path.length > 0) {
|
|
50
|
+
const next = pending[pending.length - 1].next();
|
|
51
|
+
if (next.done) {
|
|
52
|
+
pathIndices.set(path.pop(), -1);
|
|
53
|
+
pending.pop();
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
budget?.spend(1);
|
|
57
|
+
const index = pathIndices.get(next.value);
|
|
58
|
+
if (index === undefined) {
|
|
59
|
+
enter(next.value);
|
|
60
|
+
}
|
|
61
|
+
else if (index >= 0) {
|
|
62
|
+
return path.slice(index);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
// Closes innermost first; every iterator gets its `return()`, and only when
|
|
69
|
+
// `report` is set does the first error from one propagate.
|
|
70
|
+
function closeAll_(pending, report) {
|
|
71
|
+
let failure;
|
|
72
|
+
while (pending.length > 0) {
|
|
73
|
+
try {
|
|
74
|
+
pending.pop().return?.();
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
failure ??= { error };
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (report && failure !== undefined) {
|
|
81
|
+
throw failure.error;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=find-cycle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"find-cycle.js","sourceRoot":"","sources":["../../src/graph/find-cycle.ts"],"names":[],"mappings":"AAWA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,SAAS,CACxB,KAAsB,EACtB,UAAwD,EACxD,OAAO,GAAqB,EAAE;IAE9B,MAAM,OAAO,GAAsB,EAAE,CAAC;IACtC,IAAI,MAA2B,CAAC;IAChC,IAAI,CAAC;QACJ,MAAM,GAAG,OAAO,CAAC,KAAK,EAAE,UAAU,EAAE,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,SAAS,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QAC1B,MAAM,KAAK,CAAC;IACb,CAAC;IACD,SAAS,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IACzB,OAAO,MAAM,CAAC;AACf,CAAC;AAED,SAAS,OAAO,CACf,KAAsB,EACtB,UAAwD,EACxD,MAA8B,EAC9B,OAA0B;IAE1B,0EAA0E;IAC1E,MAAM,WAAW,GAAG,IAAI,GAAG,EAAiB,CAAC;IAC7C,MAAM,IAAI,GAAY,EAAE,CAAC;IAEzB,MAAM,KAAK,GAAG,CAAC,IAAW,EAAQ,EAAE;QACnC,MAAM,EAAE,KAAK,CAA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,SAAS,CAAC,OAA4B,EAAE,MAAe;IAC/D,IAAI,OAAgD,CAAC;IACrD,OAAO,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC3B,IAAI,CAAC;YACJ,OAAO,CAAC,GAAG,EAAG,CAAC,MAAM,EAAE,EAAE,CAAC;QAC3B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,OAAO,KAAK,EAAE,KAAK,EAAE,CAAC;QACvB,CAAC;IACF,CAAC;IACD,IAAI,MAAM,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QACrC,MAAM,OAAO,CAAC,KAAK,CAAC;IACrB,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type Callable } from "@ac-kit/core";
|
|
2
|
+
export type ParentTraits<TItem, TKey> = {
|
|
3
|
+
readonly keyOf: Callable<[item: TItem, index: number], TKey>;
|
|
4
|
+
/** `null` or `undefined` for a root. */
|
|
5
|
+
readonly parentKeyOf: Callable<[
|
|
6
|
+
item: TItem,
|
|
7
|
+
index: number
|
|
8
|
+
], TKey | null | undefined>;
|
|
9
|
+
};
|
|
10
|
+
export type IndexedHierarchy = {
|
|
11
|
+
/**
|
|
12
|
+
* The node to lay out from: the single root's index, or `itemCount` — a
|
|
13
|
+
* synthetic node whose children are the roots — when there are zero or
|
|
14
|
+
* several roots.
|
|
15
|
+
*/
|
|
16
|
+
readonly root: number;
|
|
17
|
+
/**
|
|
18
|
+
* `itemCount + 2` offsets: node i's children are `children[childStarts[i] …
|
|
19
|
+
* childStarts[i + 1])`, node `itemCount` being the synthetic root.
|
|
20
|
+
*/
|
|
21
|
+
readonly childStarts: Uint32Array;
|
|
22
|
+
readonly children: Uint32Array;
|
|
23
|
+
/**
|
|
24
|
+
* Ready for the layouts' `childrenOf`: a `subarray` view, no copy of the
|
|
25
|
+
* data.
|
|
26
|
+
*/
|
|
27
|
+
readonly childrenOf: Callable<[node: number], Uint32Array>;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Children lists from a parent relation (d3 `stratify`): rows with a key and a
|
|
31
|
+
* parent key, as a chart's frame has. Keys compare with SameValueZero. Children
|
|
32
|
+
* keep input order.
|
|
33
|
+
*
|
|
34
|
+
* Complexity: O(n) expected (one `Map`).
|
|
35
|
+
*
|
|
36
|
+
* @throws {RangeError} A duplicate key (naming both rows and the key), a parent
|
|
37
|
+
* key no row has (naming the row and the key), or a cycle (rows unreachable
|
|
38
|
+
* from every root; names the lowest). Keys are named with `describeValue`.
|
|
39
|
+
*/
|
|
40
|
+
export declare function hierarchyFromParents<TItem, TKey>(items: readonly TItem[], traits: ParentTraits<TItem, TKey>): IndexedHierarchy;
|
|
41
|
+
//# sourceMappingURL=hierarchy-from-parents.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hierarchy-from-parents.d.ts","sourceRoot":"","sources":["../../src/graph/hierarchy-from-parents.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAK5D,MAAM,MAAM,YAAY,CAAC,KAAK,EAAE,IAAI,IAAI;IACvC,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,EAAE,IAAI,CAAC,CAAC;IAC7D,wCAAwC;IACxC,QAAQ,CAAC,WAAW,EAAE,QAAQ,CAC7B;QAAC,IAAI,EAAE,KAAK;QAAE,KAAK,EAAE,MAAM;KAAC,EAC5B,IAAI,GAAG,IAAI,GAAG,SAAS,CACvB,CAAC;CACF,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC9B;;;;OAIG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC;IAClC,QAAQ,CAAC,QAAQ,EAAE,WAAW,CAAC;IAC/B;;;OAGG;IACH,QAAQ,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE,WAAW,CAAC,CAAC;CAC3D,CAAC;AAEF;;;;;;;;;;GAUG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,IAAI,EAC/C,KAAK,EAAE,SAAS,KAAK,EAAE,EACvB,MAAM,EAAE,YAAY,CAAC,KAAK,EAAE,IAAI,CAAC,GAC/B,gBAAgB,CAwDlB"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { describeValue } from "@ac-kit/core";
|
|
2
|
+
import { groupIndices } from "../group/group-indices.js";
|
|
3
|
+
import { walkTree } from "./walk-tree.js";
|
|
4
|
+
/**
|
|
5
|
+
* Children lists from a parent relation (d3 `stratify`): rows with a key and a
|
|
6
|
+
* parent key, as a chart's frame has. Keys compare with SameValueZero. Children
|
|
7
|
+
* keep input order.
|
|
8
|
+
*
|
|
9
|
+
* Complexity: O(n) expected (one `Map`).
|
|
10
|
+
*
|
|
11
|
+
* @throws {RangeError} A duplicate key (naming both rows and the key), a parent
|
|
12
|
+
* key no row has (naming the row and the key), or a cycle (rows unreachable
|
|
13
|
+
* from every root; names the lowest). Keys are named with `describeValue`.
|
|
14
|
+
*/
|
|
15
|
+
export function hierarchyFromParents(items, traits) {
|
|
16
|
+
const itemCount = items.length;
|
|
17
|
+
const rowOfKey = new Map();
|
|
18
|
+
const parentKeys = [];
|
|
19
|
+
// Keys are named with `describeValue`, which never throws or runs their code.
|
|
20
|
+
for (let row = 0; row < itemCount; row++) {
|
|
21
|
+
const key = traits.keyOf(items[row], row);
|
|
22
|
+
const earlier = rowOfKey.get(key);
|
|
23
|
+
if (earlier !== undefined) {
|
|
24
|
+
throw new RangeError(`hierarchyFromParents: rows ${earlier} and ${row} have the same key ${describeValue(key)}`);
|
|
25
|
+
}
|
|
26
|
+
rowOfKey.set(key, row);
|
|
27
|
+
parentKeys.push(traits.parentKeyOf(items[row], row));
|
|
28
|
+
}
|
|
29
|
+
// Parent row per item; itemCount (the synthetic node) marks a root.
|
|
30
|
+
const parents = new Int32Array(itemCount);
|
|
31
|
+
let rootCount = 0;
|
|
32
|
+
let onlyRoot = itemCount;
|
|
33
|
+
for (let row = 0; row < itemCount; row++) {
|
|
34
|
+
const parentKey = parentKeys[row];
|
|
35
|
+
if (parentKey === null || parentKey === undefined) {
|
|
36
|
+
parents[row] = itemCount;
|
|
37
|
+
rootCount++;
|
|
38
|
+
onlyRoot = row;
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const parent = rowOfKey.get(parentKey);
|
|
42
|
+
if (parent === undefined) {
|
|
43
|
+
throw new RangeError(`hierarchyFromParents: row ${row} has the parent key ${describeValue(parentKey)}, which no row has`);
|
|
44
|
+
}
|
|
45
|
+
parents[row] = parent;
|
|
46
|
+
}
|
|
47
|
+
let root = itemCount;
|
|
48
|
+
if (rootCount === 1) {
|
|
49
|
+
root = onlyRoot;
|
|
50
|
+
parents[root] = -1;
|
|
51
|
+
}
|
|
52
|
+
const { offsets: childStarts, indices: children } = groupIndices(parents, (parent) => parent, itemCount + 1);
|
|
53
|
+
const childrenOf = (node) => children.subarray(childStarts[node], childStarts[node + 1]);
|
|
54
|
+
assertReachable_(itemCount, root, childrenOf);
|
|
55
|
+
return { root, childStarts, children, childrenOf };
|
|
56
|
+
}
|
|
57
|
+
function assertReachable_(itemCount, root, childrenOf) {
|
|
58
|
+
const reached = new Uint8Array(itemCount + 1);
|
|
59
|
+
let reachedCount = 0;
|
|
60
|
+
walkTree(root, childrenOf, (node) => {
|
|
61
|
+
reached[node] = 1;
|
|
62
|
+
reachedCount++;
|
|
63
|
+
return undefined;
|
|
64
|
+
});
|
|
65
|
+
// The synthetic root counts when it is the start.
|
|
66
|
+
const expected = root === itemCount ? itemCount + 1 : itemCount;
|
|
67
|
+
if (reachedCount < expected) {
|
|
68
|
+
let lowest = 0;
|
|
69
|
+
while (reached[lowest] === 1) {
|
|
70
|
+
lowest++;
|
|
71
|
+
}
|
|
72
|
+
throw new RangeError(`hierarchyFromParents: row ${lowest} is in a cycle or below one: it cannot be reached from a root`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=hierarchy-from-parents.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hierarchy-from-parents.js","sourceRoot":"","sources":["../../src/graph/hierarchy-from-parents.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAiB,MAAM,cAAc,CAAC;AAE5D,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AA+B1C;;;;;;;;;;GAUG;AACH,MAAM,UAAU,oBAAoB,CACnC,KAAuB,EACvB,MAAiC;IAEjC,MAAM,SAAS,GAAG,KAAK,CAAC,MAAM,CAAC;IAC/B,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAgB,CAAC;IACzC,MAAM,UAAU,GAAgC,EAAE,CAAC;IAEnD,8EAA8E;IAC9E,KAAK,IAAI,GAAG,GAAG,CAAC,EAAE,GAAG,GAAG,SAAS,EAAE,GAAG,EAAE,EAAE,CAAC;QAC1C,MAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAE,EAAE,GAAG,CAAC,CAAC;QAC3C,MAAM,OAAO,GAAG,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAClC,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC3B,MAAM,IAAI,UAAU,CACnB,8BAA8B,OAAO,QAAQ,GAAG,sBAAsB,aAAa,CAAC,GAAG,CAAC,EAAE,CAC1F,CAAC;QACH,CAAC;QACD,QAAQ,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QACvB,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,KAAK,CAAC,GAAG,CAAE,EAAE,GAAG,CAAC,CAAC,CAAC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CAAC,IAAI,GAAG,CAAC,CAAE,CAAC,CAAC;IAE/D,gBAAgB,CAAC,SAAS,EAAE,IAAI,EAAE,UAAU,CAAC,CAAC;IAE9C,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,UAAU,EAAE,CAAC;AACpD,CAAC;AAED,SAAS,gBAAgB,CACxB,SAAiB,EACjB,IAAY,EACZ,UAAyC;IAEzC,MAAM,OAAO,GAAG,IAAI,UAAU,CAAC,SAAS,GAAG,CAAC,CAAC,CAAC;IAC9C,IAAI,YAAY,GAAG,CAAC,CAAC;IACrB,QAAQ,CAAC,IAAI,EAAE,UAAU,EAAE,CAAC,IAAI,EAAE,EAAE;QACnC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,YAAY,EAAE,CAAC;QACf,OAAO,SAAS,CAAC;IAClB,CAAC,CAAC,CAAC;IAEH,kDAAkD;IAClD,MAAM,QAAQ,GAAG,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAChE,IAAI,YAAY,GAAG,QAAQ,EAAE,CAAC;QAC7B,IAAI,MAAM,GAAG,CAAC,CAAC;QACf,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YAC9B,MAAM,EAAE,CAAC;QACV,CAAC;QACD,MAAM,IAAI,UAAU,CACnB,6BAA6B,MAAM,+DAA+D,CAClG,CAAC;IACH,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Positions grouped by group number: group `g` holds `indices[offsets[g] …
|
|
3
|
+
* offsets[g + 1])`, ascending.
|
|
4
|
+
*/
|
|
5
|
+
export type IndexGroups = {
|
|
6
|
+
/** `groupCount + 1` ascending offsets into `indices`, the first `0`. */
|
|
7
|
+
readonly offsets: Uint32Array;
|
|
8
|
+
/**
|
|
9
|
+
* The positions of `groups`, group after group, each group in ascending
|
|
10
|
+
* order; positions whose group is `-1` are absent.
|
|
11
|
+
*/
|
|
12
|
+
readonly indices: Uint32Array;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Groups the positions of `items` by the group number `groupOf` gives each one
|
|
16
|
+
* — the compressed sparse row (CSR) layout an adjacency list, a parent →
|
|
17
|
+
* children table or a bucket index is built on, in two flat arrays rather than
|
|
18
|
+
* an array of arrays.
|
|
19
|
+
*
|
|
20
|
+
* A counting sort: count, prefix-sum, stable fill, in O(n + groupCount) time
|
|
21
|
+
* and no memory beyond the result. A group of `-1` means "no group" and leaves
|
|
22
|
+
* the position out; every other group must be an integer in `[0, groupCount)`.
|
|
23
|
+
*
|
|
24
|
+
* `groupOf` is called twice per item, once per pass, and its results are never
|
|
25
|
+
* stored, so it must be deterministic. Every result is checked in both passes:
|
|
26
|
+
* when the second call answers differently, the result groups the second
|
|
27
|
+
* answers if the group sizes still match, and a `RangeError` is thrown
|
|
28
|
+
* otherwise; no inconsistent result is ever returned.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* const tasks = [{ owner: 1 }, { owner: 0 }, { owner: -1 }, { owner: 1 }];
|
|
32
|
+
* const { offsets, indices } = groupIndices(tasks, (task) => task.owner, 2);
|
|
33
|
+
* // offsets: [0, 1, 3], indices: [1, 0, 3]
|
|
34
|
+
* // group 0 holds indices[0 … 1) = [1]; group 1 holds indices[1 … 3) = [0, 3]
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* // Group numbers already in an array: the identity callback.
|
|
38
|
+
* const parentOf = Int32Array.of(-1, 0, 0, 1);
|
|
39
|
+
* const children = groupIndices(
|
|
40
|
+
* parentOf,
|
|
41
|
+
* (parent) => parent,
|
|
42
|
+
* parentOf.length,
|
|
43
|
+
* );
|
|
44
|
+
*
|
|
45
|
+
* @param items - The items to group.
|
|
46
|
+
* @param groupOf - The group of an item, or `-1` for none; deterministic.
|
|
47
|
+
* @param groupCount - How many groups there are.
|
|
48
|
+
* @returns The offsets and the grouped positions.
|
|
49
|
+
* @throws {RangeError} When `groupCount` or `items.length` is not an integer in
|
|
50
|
+
* `[0, 2³² − 1]`, or `groupOf` returns neither `-1` nor an integer in `[0,
|
|
51
|
+
* groupCount)`; the message names the position and the result. Also when
|
|
52
|
+
* `groupOf` answers differently in the second pass and the group sizes no
|
|
53
|
+
* longer match.
|
|
54
|
+
*/
|
|
55
|
+
export declare function groupIndices<T>(items: ArrayLike<T>, groupOf: (item: T, index: number) => number, groupCount: number): IndexGroups;
|
|
56
|
+
//# sourceMappingURL=group-indices.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"group-indices.d.ts","sourceRoot":"","sources":["../../src/group/group-indices.ts"],"names":[],"mappings":"AAKA;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG;IACzB,wEAAwE;IACxE,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC;IAC9B;;;OAGG;IACH,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC;CAC9B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAC7B,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,EACnB,OAAO,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,EAC3C,UAAU,EAAE,MAAM,GAChB,WAAW,CAsEb"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { assertInteger, describeValue, isIntegerInRange } from "@ac-kit/core";
|
|
2
|
+
const MAX_UINT32_ = 0xffffffff;
|
|
3
|
+
const UNWRITTEN_ = 0xffffffff;
|
|
4
|
+
/**
|
|
5
|
+
* Groups the positions of `items` by the group number `groupOf` gives each one
|
|
6
|
+
* — the compressed sparse row (CSR) layout an adjacency list, a parent →
|
|
7
|
+
* children table or a bucket index is built on, in two flat arrays rather than
|
|
8
|
+
* an array of arrays.
|
|
9
|
+
*
|
|
10
|
+
* A counting sort: count, prefix-sum, stable fill, in O(n + groupCount) time
|
|
11
|
+
* and no memory beyond the result. A group of `-1` means "no group" and leaves
|
|
12
|
+
* the position out; every other group must be an integer in `[0, groupCount)`.
|
|
13
|
+
*
|
|
14
|
+
* `groupOf` is called twice per item, once per pass, and its results are never
|
|
15
|
+
* stored, so it must be deterministic. Every result is checked in both passes:
|
|
16
|
+
* when the second call answers differently, the result groups the second
|
|
17
|
+
* answers if the group sizes still match, and a `RangeError` is thrown
|
|
18
|
+
* otherwise; no inconsistent result is ever returned.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const tasks = [{ owner: 1 }, { owner: 0 }, { owner: -1 }, { owner: 1 }];
|
|
22
|
+
* const { offsets, indices } = groupIndices(tasks, (task) => task.owner, 2);
|
|
23
|
+
* // offsets: [0, 1, 3], indices: [1, 0, 3]
|
|
24
|
+
* // group 0 holds indices[0 … 1) = [1]; group 1 holds indices[1 … 3) = [0, 3]
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // Group numbers already in an array: the identity callback.
|
|
28
|
+
* const parentOf = Int32Array.of(-1, 0, 0, 1);
|
|
29
|
+
* const children = groupIndices(
|
|
30
|
+
* parentOf,
|
|
31
|
+
* (parent) => parent,
|
|
32
|
+
* parentOf.length,
|
|
33
|
+
* );
|
|
34
|
+
*
|
|
35
|
+
* @param items - The items to group.
|
|
36
|
+
* @param groupOf - The group of an item, or `-1` for none; deterministic.
|
|
37
|
+
* @param groupCount - How many groups there are.
|
|
38
|
+
* @returns The offsets and the grouped positions.
|
|
39
|
+
* @throws {RangeError} When `groupCount` or `items.length` is not an integer in
|
|
40
|
+
* `[0, 2³² − 1]`, or `groupOf` returns neither `-1` nor an integer in `[0,
|
|
41
|
+
* groupCount)`; the message names the position and the result. Also when
|
|
42
|
+
* `groupOf` answers differently in the second pass and the group sizes no
|
|
43
|
+
* longer match.
|
|
44
|
+
*/
|
|
45
|
+
export function groupIndices(items, groupOf, groupCount) {
|
|
46
|
+
assertInteger(groupCount, "groupCount", 0, MAX_UINT32_);
|
|
47
|
+
const length = items.length;
|
|
48
|
+
assertInteger(length, "items.length", 0, MAX_UINT32_);
|
|
49
|
+
// Counted one slot up, so the prefix sum leaves each group's start in the
|
|
50
|
+
// slot of its own number and the total in the last.
|
|
51
|
+
const offsets = new Uint32Array(groupCount + 1);
|
|
52
|
+
let kept = 0;
|
|
53
|
+
for (let position = 0; position < length; position++) {
|
|
54
|
+
const group = groupOf(items[position], position);
|
|
55
|
+
if (group === -1) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (!isIntegerInRange(group, 0, groupCount)) {
|
|
59
|
+
throw new RangeError(`groupOf(items[${position}], ${position}) must be -1 or an integer in [0, ${groupCount}), got ${describeValue(group)}`);
|
|
60
|
+
}
|
|
61
|
+
offsets[group + 1]++;
|
|
62
|
+
kept++;
|
|
63
|
+
}
|
|
64
|
+
for (let group = 0; group < groupCount; group++) {
|
|
65
|
+
offsets[group + 1] += offsets[group];
|
|
66
|
+
}
|
|
67
|
+
// Each group's start serves as its write cursor, which leaves it at the
|
|
68
|
+
// group's end, the next group's start: shifting by one slot restores them.
|
|
69
|
+
//
|
|
70
|
+
// `groupOf` is called twice, and may answer differently the
|
|
71
|
+
// second time. Every slot starts as a mark no position can equal (positions
|
|
72
|
+
// stay below 2³² − 1), and a write must land, inside the array, on a marked
|
|
73
|
+
// slot. With as many writes as counted entries, a group that took fewer than
|
|
74
|
+
// its share would leave a slot no group can reach, so the first group to
|
|
75
|
+
// stray took more and ran onto the next group's first slot: a collision, a
|
|
76
|
+
// write past the end, or, when no later group writes, cursors out of order.
|
|
77
|
+
const indices = new Uint32Array(kept).fill(UNWRITTEN_);
|
|
78
|
+
let written = 0;
|
|
79
|
+
for (let position = 0; position < length; position++) {
|
|
80
|
+
const group = groupOf(items[position], position);
|
|
81
|
+
if (group === -1) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (!isIntegerInRange(group, 0, groupCount)) {
|
|
85
|
+
throw changed_(position);
|
|
86
|
+
}
|
|
87
|
+
const slot = offsets[group]++;
|
|
88
|
+
if (slot >= kept || indices[slot] !== UNWRITTEN_) {
|
|
89
|
+
throw changed_(position);
|
|
90
|
+
}
|
|
91
|
+
indices[slot] = position;
|
|
92
|
+
written++;
|
|
93
|
+
}
|
|
94
|
+
if (written !== kept) {
|
|
95
|
+
throw changed_();
|
|
96
|
+
}
|
|
97
|
+
for (let group = 1; group < groupCount; group++) {
|
|
98
|
+
if (offsets[group - 1] > offsets[group]) {
|
|
99
|
+
throw changed_();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
offsets.copyWithin(1, 0, groupCount);
|
|
103
|
+
offsets[0] = 0;
|
|
104
|
+
return { offsets, indices };
|
|
105
|
+
}
|
|
106
|
+
function changed_(position) {
|
|
107
|
+
const where = position === undefined
|
|
108
|
+
? "groupOf's results"
|
|
109
|
+
: `groupOf(items[${position}], ${position})`;
|
|
110
|
+
return new RangeError(`${where} changed between groupIndices' counting and filling passes; groupOf must be deterministic`);
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=group-indices.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"group-indices.js","sourceRoot":"","sources":["../../src/group/group-indices.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAE9E,MAAM,WAAW,GAAG,UAAU,CAAC;AAC/B,MAAM,UAAU,GAAG,UAAU,CAAC;AAgB9B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,MAAM,UAAU,YAAY,CAC3B,KAAmB,EACnB,OAA2C,EAC3C,UAAkB;IAElB,aAAa,CAAC,UAAU,EAAE,YAAY,EAAE,CAAC,EAAE,WAAW,CAAC,CAAC;IACxD,MAAM,MAAM,GAAG,KAAK,CAAC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AQ,KAAK,SAAS;QACrB,CAAC,CAAC,mBAAmB;QACrB,CAAC,CAAC,iBAAiB,QAAQ,MAAM,QAAQ,GAAG,CAAC;IAE/C,OAAO,IAAI,UAAU,CACpB,GAAG,KAAK,2FAA2F,CACnG,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -2,9 +2,13 @@ export * from "./combinatorics/combinations.js";
|
|
|
2
2
|
export * from "./distribute/spread-evenly.js";
|
|
3
3
|
export * from "./graph/a-star.js";
|
|
4
4
|
export * from "./graph/best-first-search.js";
|
|
5
|
+
export * from "./graph/children-of.js";
|
|
5
6
|
export * from "./graph/dfs.js";
|
|
7
|
+
export * from "./graph/find-cycle.js";
|
|
8
|
+
export * from "./graph/hierarchy-from-parents.js";
|
|
6
9
|
export * from "./graph/walk-tree.js";
|
|
7
10
|
export * from "./group/group-by.js";
|
|
11
|
+
export * from "./group/group-indices.js";
|
|
8
12
|
export * from "./group/partition-indices.js";
|
|
9
13
|
export * from "./group/partition.js";
|
|
10
14
|
export * from "./interval/complement-intervals.js";
|
|
@@ -16,6 +20,7 @@ export * from "./interval/subtract-intervals.js";
|
|
|
16
20
|
export * from "./optimize/backtrack.js";
|
|
17
21
|
export * from "./optimize/beam-search.js";
|
|
18
22
|
export * from "./optimize/viterbi.js";
|
|
23
|
+
export * from "./ordered/binary-search.js";
|
|
19
24
|
export * from "./ordered/bisect-center.js";
|
|
20
25
|
export * from "./ordered/bisect-left.js";
|
|
21
26
|
export * from "./ordered/bisect-right.js";
|
|
@@ -23,8 +28,6 @@ export * from "./ordered/bisect.js";
|
|
|
23
28
|
export * from "./ordered/create-bisector.js";
|
|
24
29
|
export * from "./recursion/run-trampoline.js";
|
|
25
30
|
export * from "./sample/choice.js";
|
|
26
|
-
export * from "./sample/random-boolean.js";
|
|
27
|
-
export * from "./sample/random-integer.js";
|
|
28
31
|
export * from "./sample/shuffle.js";
|
|
29
32
|
export * from "./select/max-by.js";
|
|
30
33
|
export * from "./select/min-by.js";
|
|
@@ -43,4 +46,5 @@ export * from "./set/union.js";
|
|
|
43
46
|
export * from "./set/uniq-by.js";
|
|
44
47
|
export * from "./set/uniq.js";
|
|
45
48
|
export * from "./sort/radix-sort.js";
|
|
49
|
+
export * from "./sort/sorted-indices.js";
|
|
46
50
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iCAAiC,CAAC;AAChD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,mBAAmB,CAAC;AAClC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,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,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iCAAiC,CAAC;AAChD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,mBAAmB,CAAC;AAClC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,wBAAwB,CAAC;AACvC,cAA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.js";
|
|
|
2
2
|
export * from "./distribute/spread-evenly.js";
|
|
3
3
|
export * from "./graph/a-star.js";
|
|
4
4
|
export * from "./graph/best-first-search.js";
|
|
5
|
+
export * from "./graph/children-of.js";
|
|
5
6
|
export * from "./graph/dfs.js";
|
|
7
|
+
export * from "./graph/find-cycle.js";
|
|
8
|
+
export * from "./graph/hierarchy-from-parents.js";
|
|
6
9
|
export * from "./graph/walk-tree.js";
|
|
7
10
|
export * from "./group/group-by.js";
|
|
11
|
+
export * from "./group/group-indices.js";
|
|
8
12
|
export * from "./group/partition-indices.js";
|
|
9
13
|
export * from "./group/partition.js";
|
|
10
14
|
export * from "./interval/complement-intervals.js";
|
|
@@ -16,6 +20,7 @@ export * from "./interval/subtract-intervals.js";
|
|
|
16
20
|
export * from "./optimize/backtrack.js";
|
|
17
21
|
export * from "./optimize/beam-search.js";
|
|
18
22
|
export * from "./optimize/viterbi.js";
|
|
23
|
+
export * from "./ordered/binary-search.js";
|
|
19
24
|
export * from "./ordered/bisect-center.js";
|
|
20
25
|
export * from "./ordered/bisect-left.js";
|
|
21
26
|
export * from "./ordered/bisect-right.js";
|
|
@@ -23,8 +28,6 @@ export * from "./ordered/bisect.js";
|
|
|
23
28
|
export * from "./ordered/create-bisector.js";
|
|
24
29
|
export * from "./recursion/run-trampoline.js";
|
|
25
30
|
export * from "./sample/choice.js";
|
|
26
|
-
export * from "./sample/random-boolean.js";
|
|
27
|
-
export * from "./sample/random-integer.js";
|
|
28
31
|
export * from "./sample/shuffle.js";
|
|
29
32
|
export * from "./select/max-by.js";
|
|
30
33
|
export * from "./select/min-by.js";
|
|
@@ -43,4 +46,5 @@ export * from "./set/union.js";
|
|
|
43
46
|
export * from "./set/uniq-by.js";
|
|
44
47
|
export * from "./set/uniq.js";
|
|
45
48
|
export * from "./sort/radix-sort.js";
|
|
49
|
+
export * from "./sort/sorted-indices.js";
|
|
46
50
|
//# sourceMappingURL=index.js.map
|
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,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,
|
|
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,wBAAwB,CAAC;AACvC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,mCAAmC,CAAC;AAClD,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,0BAA0B,CAAC;AACzC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sBAAsB,CAAC;AACrC,cAAc,oCAAoC,CAAC;AACnD,cAAc,mCAAmC,CAAC;AAClD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,sCAAsC,CAAC;AACrD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,kCAAkC,CAAC;AACjD,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,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;AACrC,cAAc,0BAA0B,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { binarySearch_ } from "../ordered/_binary-search.js";
|
|
2
2
|
/**
|
|
3
3
|
* Tests whether `value` falls within any of the given sorted, disjoint ranges,
|
|
4
4
|
* each represented as a pair of inclusive bounds in a flat array.
|
|
@@ -15,7 +15,7 @@ import { binarySearch } from "../ordered/_binary-search.js";
|
|
|
15
15
|
*/
|
|
16
16
|
export function isInSortedIntervals(ranges, value, compare) {
|
|
17
17
|
const rangeCount = ranges.length / 2;
|
|
18
|
-
const candidate =
|
|
18
|
+
const candidate = binarySearch_(0, rangeCount, (index) => compare(ranges[index * 2], value) <= 0) - 1;
|
|
19
19
|
return candidate >= 0 && compare(value, ranges[candidate * 2 + 1]) <= 0;
|
|
20
20
|
}
|
|
21
21
|
//# sourceMappingURL=is-in-sorted-intervals.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-in-sorted-intervals.js","sourceRoot":"","sources":["../../src/interval/is-in-sorted-intervals.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"is-in-sorted-intervals.js","sourceRoot":"","sources":["../../src/interval/is-in-sorted-intervals.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAE7D;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,mBAAmB,CAClC,MAAoB,EACpB,KAAQ,EACR,OAAsB;IAEtB,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;IAErC,MAAM,SAAS,GACd,aAAa,CACZ,CAAC,EACD,UAAU,EACV,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC,CAAE,EAAE,KAAK,CAAC,IAAI,CAAC,CAClD,GAAG,CAAC,CAAC;IAEP,OAAO,SAAS,IAAI,CAAC,IAAI,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,SAAS,GAAG,CAAC,GAAG,CAAC,CAAE,CAAC,IAAI,CAAC,CAAC;AAC1E,CAAC"}
|
|
@@ -6,8 +6,11 @@
|
|
|
6
6
|
* `isInSortedIntervals`'s flat, stride-2 range array) without materializing one
|
|
7
7
|
* element per searched item.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
9
|
+
* Unchecked: the public, validating form is `binarySearch`.
|
|
10
|
+
*
|
|
11
|
+
* @param predicate - `true` if the answer lies strictly to the right of
|
|
12
|
+
* `index`.
|
|
13
|
+
* @returns The leftmost index in `[low, high]` where `predicate` is false.
|
|
11
14
|
*/
|
|
12
|
-
export declare function
|
|
15
|
+
export declare function binarySearch_(low: number, high: number, predicate: (index: number) => boolean): number;
|
|
13
16
|
//# sourceMappingURL=_binary-search.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_binary-search.d.ts","sourceRoot":"","sources":["../../src/ordered/_binary-search.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"_binary-search.d.ts","sourceRoot":"","sources":["../../src/ordered/_binary-search.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,aAAa,CAC5B,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,GACnC,MAAM,CAYR"}
|
|
@@ -6,13 +6,16 @@
|
|
|
6
6
|
* `isInSortedIntervals`'s flat, stride-2 range array) without materializing one
|
|
7
7
|
* element per searched item.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
9
|
+
* Unchecked: the public, validating form is `binarySearch`.
|
|
10
|
+
*
|
|
11
|
+
* @param predicate - `true` if the answer lies strictly to the right of
|
|
12
|
+
* `index`.
|
|
13
|
+
* @returns The leftmost index in `[low, high]` where `predicate` is false.
|
|
11
14
|
*/
|
|
12
|
-
export function
|
|
15
|
+
export function binarySearch_(low, high, predicate) {
|
|
13
16
|
while (low < high) {
|
|
14
|
-
const mid =
|
|
15
|
-
if (
|
|
17
|
+
const mid = low + Math.floor((high - low) / 2);
|
|
18
|
+
if (predicate(mid)) {
|
|
16
19
|
low = mid + 1;
|
|
17
20
|
}
|
|
18
21
|
else {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_binary-search.js","sourceRoot":"","sources":["../../src/ordered/_binary-search.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"_binary-search.js","sourceRoot":"","sources":["../../src/ordered/_binary-search.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,aAAa,CAC5B,GAAW,EACX,IAAY,EACZ,SAAqC;IAErC,OAAO,GAAG,GAAG,IAAI,EAAE,CAAC;QACnB,MAAM,GAAG,GAAG,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;QAE/C,IAAI,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC;YACpB,GAAG,GAAG,GAAG,GAAG,CAAC,CAAC;QACf,CAAC;aAAM,CAAC;YACP,IAAI,GAAG,GAAG,CAAC;QACZ,CAAC;IACF,CAAC;IAED,OAAO,GAAG,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Predicate } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* The first integer in `[low, high)` at which `predicate` is `false`, or `high`
|
|
4
|
+
* when it is `true` for all of them: the partition point of a predicate that is
|
|
5
|
+
* `true` up to some integer and `false` from it on — `std::partition_point` in
|
|
6
|
+
* C++, `partition_point` in Rust. It searches an answer rather than an array:
|
|
7
|
+
* the smallest capacity that fits, the first millisecond at which a condition
|
|
8
|
+
* holds.
|
|
9
|
+
*
|
|
10
|
+
* Monotonicity is a precondition, not checked. For any predicate the result `i`
|
|
11
|
+
* is still a boundary: `i === low` or `predicate(i - 1)` was `true`, and `i ===
|
|
12
|
+
* high` or `predicate(i)` was `false`.
|
|
13
|
+
*
|
|
14
|
+
* `predicate` is called only with integers in `[low, high)`, never when `low
|
|
15
|
+
* === high`, and at most ⌈log₂(high − low + 1)⌉ times. What it throws
|
|
16
|
+
* propagates.
|
|
17
|
+
*
|
|
18
|
+
* @param low The first candidate, a safe integer.
|
|
19
|
+
* @param high One past the last candidate, a safe integer `>= low`.
|
|
20
|
+
* @param predicate `true` while the answer lies further right.
|
|
21
|
+
* @returns An integer in `[low, high]`.
|
|
22
|
+
* @throws {RangeError} When `low` or `high` is not a safe integer, or `low >
|
|
23
|
+
* high`.
|
|
24
|
+
*/
|
|
25
|
+
export declare function binarySearch(low: number, high: number, predicate: Predicate<[candidate: number]>): number;
|
|
26
|
+
//# sourceMappingURL=binary-search.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binary-search.d.ts","sourceRoot":"","sources":["../../src/ordered/binary-search.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAI9C;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,YAAY,CAC3B,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,SAAS,CAAC,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,GACvC,MAAM,CASR"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { binarySearch_ } from "./_binary-search.js";
|
|
2
|
+
/**
|
|
3
|
+
* The first integer in `[low, high)` at which `predicate` is `false`, or `high`
|
|
4
|
+
* when it is `true` for all of them: the partition point of a predicate that is
|
|
5
|
+
* `true` up to some integer and `false` from it on — `std::partition_point` in
|
|
6
|
+
* C++, `partition_point` in Rust. It searches an answer rather than an array:
|
|
7
|
+
* the smallest capacity that fits, the first millisecond at which a condition
|
|
8
|
+
* holds.
|
|
9
|
+
*
|
|
10
|
+
* Monotonicity is a precondition, not checked. For any predicate the result `i`
|
|
11
|
+
* is still a boundary: `i === low` or `predicate(i - 1)` was `true`, and `i ===
|
|
12
|
+
* high` or `predicate(i)` was `false`.
|
|
13
|
+
*
|
|
14
|
+
* `predicate` is called only with integers in `[low, high)`, never when `low
|
|
15
|
+
* === high`, and at most ⌈log₂(high − low + 1)⌉ times. What it throws
|
|
16
|
+
* propagates.
|
|
17
|
+
*
|
|
18
|
+
* @param low The first candidate, a safe integer.
|
|
19
|
+
* @param high One past the last candidate, a safe integer `>= low`.
|
|
20
|
+
* @param predicate `true` while the answer lies further right.
|
|
21
|
+
* @returns An integer in `[low, high]`.
|
|
22
|
+
* @throws {RangeError} When `low` or `high` is not a safe integer, or `low >
|
|
23
|
+
* high`.
|
|
24
|
+
*/
|
|
25
|
+
export function binarySearch(low, high, predicate) {
|
|
26
|
+
if (!Number.isSafeInteger(low) || !Number.isSafeInteger(high)) {
|
|
27
|
+
throw new RangeError("low and high must be safe integers.");
|
|
28
|
+
}
|
|
29
|
+
if (low > high) {
|
|
30
|
+
throw new RangeError("low must not be greater than high.");
|
|
31
|
+
}
|
|
32
|
+
return binarySearch_(low, high, predicate);
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=binary-search.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binary-search.js","sourceRoot":"","sources":["../../src/ordered/binary-search.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,YAAY,CAC3B,GAAW,EACX,IAAY,EACZ,SAAyC;IAEzC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,EAAE,CAAC;QAC/D,MAAM,IAAI,UAAU,CAAC,qCAAqC,CAAC,CAAC;IAC7D,CAAC;IACD,IAAI,GAAG,GAAG,IAAI,EAAE,CAAC;QAChB,MAAM,IAAI,UAAU,CAAC,oCAAoC,CAAC,CAAC;IAC5D,CAAC;IAED,OAAO,aAAa,CAAC,GAAG,EAAE,IAAI,EAAE,SAAS,CAAC,CAAC;AAC5C,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { binarySearch_ } from "./_binary-search.js";
|
|
2
2
|
/**
|
|
3
3
|
* Finds the index of the element in `items` whose key is closest to `value`.
|
|
4
4
|
*
|
|
@@ -15,7 +15,7 @@ import { binarySearch } from "./_binary-search.js";
|
|
|
15
15
|
* @returns The index of the closest element.
|
|
16
16
|
*/
|
|
17
17
|
export function bisectCenter(items, value, valueOf) {
|
|
18
|
-
const index =
|
|
18
|
+
const index = binarySearch_(0, items.length, (probe) => valueOf(items[probe]) < value);
|
|
19
19
|
if (index === 0) {
|
|
20
20
|
return 0;
|
|
21
21
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bisect-center.js","sourceRoot":"","sources":["../../src/ordered/bisect-center.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"bisect-center.js","sourceRoot":"","sources":["../../src/ordered/bisect-center.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,YAAY,CAC3B,KAAmB,EACnB,KAAa,EACb,OAA8B;IAE9B,MAAM,KAAK,GAAG,aAAa,CAC1B,CAAC,EACD,KAAK,CAAC,MAAM,EACZ,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAE,CAAC,GAAG,KAAK,CACzC,CAAC;IAEF,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;QACjB,OAAO,CAAC,CAAC;IACV,CAAC;IACD,IAAI,KAAK,KAAK,KAAK,CAAC,MAAM,EAAE,CAAC;QAC5B,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;IACzB,CAAC;IAED,MAAM,UAAU,GAAG,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAE,CAAC,CAAC;IACtD,MAAM,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,CAAE,CAAC,GAAG,KAAK,CAAC;IAE9C,OAAO,UAAU,GAAG,MAAM,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;AAChD,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { binarySearch_ } from "./_binary-search.js";
|
|
2
2
|
/**
|
|
3
3
|
* Finds the leftmost index in `items` where `value` could be inserted while
|
|
4
4
|
* keeping `items` sorted — before any existing element considered equivalent to
|
|
@@ -13,6 +13,6 @@ import { binarySearch } from "./_binary-search.js";
|
|
|
13
13
|
* @returns The leftmost valid insertion index, in `[0, items.length]`.
|
|
14
14
|
*/
|
|
15
15
|
export function bisectLeft(items, value, compare) {
|
|
16
|
-
return
|
|
16
|
+
return binarySearch_(0, items.length, (index) => compare(items[index], value) < 0);
|
|
17
17
|
}
|
|
18
18
|
//# sourceMappingURL=bisect-left.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bisect-left.js","sourceRoot":"","sources":["../../src/ordered/bisect-left.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"bisect-left.js","sourceRoot":"","sources":["../../src/ordered/bisect-left.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,UAAU,CACzB,KAAmB,EACnB,KAAQ,EACR,OAAsC;IAEtC,OAAO,aAAa,CACnB,CAAC,EACD,KAAK,CAAC,MAAM,EACZ,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAE,EAAE,KAAK,CAAC,GAAG,CAAC,CAC5C,CAAC;AACH,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { binarySearch_ } from "./_binary-search.js";
|
|
2
2
|
/**
|
|
3
3
|
* Finds the rightmost index in `items` where `value` could be inserted while
|
|
4
4
|
* keeping `items` sorted — after any existing element considered equivalent to
|
|
@@ -13,6 +13,6 @@ import { binarySearch } from "./_binary-search.js";
|
|
|
13
13
|
* @returns The rightmost valid insertion index, in `[0, items.length]`.
|
|
14
14
|
*/
|
|
15
15
|
export function bisectRight(items, value, compare) {
|
|
16
|
-
return
|
|
16
|
+
return binarySearch_(0, items.length, (index) => compare(items[index], value) <= 0);
|
|
17
17
|
}
|
|
18
18
|
//# sourceMappingURL=bisect-right.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bisect-right.js","sourceRoot":"","sources":["../../src/ordered/bisect-right.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"bisect-right.js","sourceRoot":"","sources":["../../src/ordered/bisect-right.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,WAAW,CAC1B,KAAmB,EACnB,KAAQ,EACR,OAAsC;IAEtC,OAAO,aAAa,CACnB,CAAC,EACD,KAAK,CAAC,MAAM,EACZ,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAE,EAAE,KAAK,CAAC,IAAI,CAAC,CAC7C,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_random-index.d.ts","sourceRoot":"","sources":["../../src/sample/_random-index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C,2EAA2E;AAC3E,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,GAAG,MAAM,CAEpE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_random-index.js","sourceRoot":"","sources":["../../src/sample/_random-index.ts"],"names":[],"mappings":"AAEA,2EAA2E;AAC3E,MAAM,UAAU,WAAW,CAAC,MAAc,EAAE,MAAgB;IAC3D,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,MAAM,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,CAAC;AAC5D,CAAC"}
|
package/dist/sample/choice.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomIndex } from "./_random-index.js";
|
|
2
2
|
/**
|
|
3
3
|
* Picks one element of `items` uniformly.
|
|
4
4
|
*
|
|
@@ -20,6 +20,6 @@ export function choice(items, random) {
|
|
|
20
20
|
if (items.length === 0) {
|
|
21
21
|
return undefined;
|
|
22
22
|
}
|
|
23
|
-
return items[
|
|
23
|
+
return items[randomIndex(items.length, random)];
|
|
24
24
|
}
|
|
25
25
|
//# sourceMappingURL=choice.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"choice.js","sourceRoot":"","sources":["../../src/sample/choice.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"choice.js","sourceRoot":"","sources":["../../src/sample/choice.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD;;;;;;;;;;;;;;;;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,WAAW,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AACjD,CAAC"}
|
package/dist/sample/shuffle.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomIndex } from "./_random-index.js";
|
|
2
2
|
/**
|
|
3
3
|
* Shuffles `items` using the Fisher-Yates algorithm and returns a new shuffled
|
|
4
4
|
* array.
|
|
@@ -16,10 +16,10 @@ export function shuffle(items, options) {
|
|
|
16
16
|
const algorithm = options?.algorithm ?? "fisherYates";
|
|
17
17
|
const randomize = (i) => {
|
|
18
18
|
if (algorithm === "sattolo") {
|
|
19
|
-
return
|
|
19
|
+
return randomIndex(i, rand);
|
|
20
20
|
}
|
|
21
21
|
else {
|
|
22
|
-
return
|
|
22
|
+
return randomIndex(i + 1, rand);
|
|
23
23
|
}
|
|
24
24
|
};
|
|
25
25
|
const out = items.slice();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shuffle.js","sourceRoot":"","sources":["../../src/sample/shuffle.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"shuffle.js","sourceRoot":"","sources":["../../src/sample/shuffle.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAqBjD;;;;;;;;;;;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,WAAW,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;QAC7B,CAAC;aAAM,CAAC;YACP,OAAO,WAAW,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC;QACjC,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,18 @@
|
|
|
1
|
+
import type { Comparator } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* The positions of `values` in the order `compare` sorts them — an argsort:
|
|
4
|
+
* `values[result[0]]` is the first value, `values[result[1]]` the second, and
|
|
5
|
+
* so on. Stable: positions of equivalent values keep their input order.
|
|
6
|
+
*
|
|
7
|
+
* Reach for it to sort one array and carry the permutation over to others, or
|
|
8
|
+
* to write a result back at each value's original position (ranks, adjusted
|
|
9
|
+
* p-values) without sorting `[value, index]` pairs.
|
|
10
|
+
*
|
|
11
|
+
* O(n log n) comparisons; one array of `n` positions.
|
|
12
|
+
*
|
|
13
|
+
* @param values The values to order. Not modified.
|
|
14
|
+
* @param compare A strict weak ordering of the values.
|
|
15
|
+
* @returns A new array of the positions `0 … n − 1`, in sorted order.
|
|
16
|
+
*/
|
|
17
|
+
export declare function sortedIndices<T>(values: ArrayLike<T>, compare: Comparator<T>): number[];
|
|
18
|
+
//# sourceMappingURL=sorted-indices.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sorted-indices.d.ts","sourceRoot":"","sources":["../../src/sort/sorted-indices.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAC9B,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,EACpB,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,GACpB,MAAM,EAAE,CAMV"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The positions of `values` in the order `compare` sorts them — an argsort:
|
|
3
|
+
* `values[result[0]]` is the first value, `values[result[1]]` the second, and
|
|
4
|
+
* so on. Stable: positions of equivalent values keep their input order.
|
|
5
|
+
*
|
|
6
|
+
* Reach for it to sort one array and carry the permutation over to others, or
|
|
7
|
+
* to write a result back at each value's original position (ranks, adjusted
|
|
8
|
+
* p-values) without sorting `[value, index]` pairs.
|
|
9
|
+
*
|
|
10
|
+
* O(n log n) comparisons; one array of `n` positions.
|
|
11
|
+
*
|
|
12
|
+
* @param values The values to order. Not modified.
|
|
13
|
+
* @param compare A strict weak ordering of the values.
|
|
14
|
+
* @returns A new array of the positions `0 … n − 1`, in sorted order.
|
|
15
|
+
*/
|
|
16
|
+
export function sortedIndices(values, compare) {
|
|
17
|
+
const indices = Array.from({ length: values.length }, (_, index) => index);
|
|
18
|
+
// Array.prototype.sort is stable (ES2019), and the input positions start in
|
|
19
|
+
// ascending order, so equivalent values keep their order.
|
|
20
|
+
return indices.sort((left, right) => compare(values[left], values[right]));
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=sorted-indices.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sorted-indices.js","sourceRoot":"","sources":["../../src/sort/sorted-indices.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,aAAa,CAC5B,MAAoB,EACpB,OAAsB;IAEtB,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC;IAE3E,4EAA4E;IAC5E,0DAA0D;IAC1D,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAE,EAAE,MAAM,CAAC,KAAK,CAAE,CAAC,CAAC,CAAC;AAC9E,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ac-kit/algo",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.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,15 +45,15 @@
|
|
|
45
45
|
"prepack": "yarn build"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@ac-kit/core": "^0.
|
|
49
|
-
"@ac-kit/data": "^0.
|
|
48
|
+
"@ac-kit/core": "^0.12.0",
|
|
49
|
+
"@ac-kit/data": "^0.4.0",
|
|
50
50
|
"@types/web": "^0.0.367"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@ac-kit/math-random": "0.
|
|
53
|
+
"@ac-kit/math-random": "0.4.0",
|
|
54
54
|
"@ac-kit/tsconfig": "0.1.0",
|
|
55
55
|
"@ac-kit/typedoc-config": "0.1.0",
|
|
56
|
-
"@ac-kit/vitest-config": "0.
|
|
56
|
+
"@ac-kit/vitest-config": "0.2.0",
|
|
57
57
|
"@typescript/native": "typescript@7.0.2",
|
|
58
58
|
"rimraf": "6.1.4",
|
|
59
59
|
"typedoc": "0.28.20",
|
|
@@ -1,23 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1,27 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1,27 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1,40 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|