@ac-kit/algo 0.4.1 → 0.6.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 +133 -0
- package/README.md +31 -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 +33 -0
- package/dist/graph/find-cycle.d.ts.map +1 -0
- package/dist/graph/find-cycle.js +104 -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 +13 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -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/_fractional-key.d.ts +38 -0
- package/dist/ordered/_fractional-key.d.ts.map +1 -0
- package/dist/ordered/_fractional-key.js +251 -0
- package/dist/ordered/_fractional-key.js.map +1 -0
- 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-numbers-array.d.ts +20 -0
- package/dist/ordered/bisect-right-numbers-array.d.ts.map +1 -0
- package/dist/ordered/bisect-right-numbers-array.js +42 -0
- package/dist/ordered/bisect-right-numbers-array.js.map +1 -0
- package/dist/ordered/bisect-right-numbers.d.ts +19 -0
- package/dist/ordered/bisect-right-numbers.d.ts.map +1 -0
- package/dist/ordered/bisect-right-numbers.js +32 -0
- package/dist/ordered/bisect-right-numbers.js.map +1 -0
- package/dist/ordered/bisect-right.js +2 -2
- package/dist/ordered/bisect-right.js.map +1 -1
- package/dist/ordered/create-bisector.js.map +1 -1
- package/dist/ordered/fractional-key-alphabet.d.ts +41 -0
- package/dist/ordered/fractional-key-alphabet.d.ts.map +1 -0
- package/dist/ordered/fractional-key-alphabet.js +67 -0
- package/dist/ordered/fractional-key-alphabet.js.map +1 -0
- package/dist/ordered/fractional-key-error.d.ts +15 -0
- package/dist/ordered/fractional-key-error.d.ts.map +1 -0
- package/dist/ordered/fractional-key-error.js +20 -0
- package/dist/ordered/fractional-key-error.js.map +1 -0
- package/dist/ordered/fractional-key.d.ts +82 -0
- package/dist/ordered/fractional-key.d.ts.map +1 -0
- package/dist/ordered/fractional-key.js +199 -0
- package/dist/ordered/fractional-key.js.map +1 -0
- 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/select/quickselect.js +1 -1
- package/dist/select/quickselect.js.map +1 -1
- package/dist/sort/radix-index-sorter.d.ts +57 -0
- package/dist/sort/radix-index-sorter.d.ts.map +1 -0
- package/dist/sort/radix-index-sorter.js +204 -0
- package/dist/sort/radix-index-sorter.js.map +1 -0
- package/dist/sort/sort-range.d.ts +48 -0
- package/dist/sort/sort-range.d.ts.map +1 -0
- package/dist/sort/sort-range.js +282 -0
- package/dist/sort/sort-range.js.map +1 -0
- 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 +6 -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
|
@@ -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,MAAM,CAAC;IAC5B,aAAa,CAAC,MAAM,EAAE,cAAc,EAAE,CAAC,EAAE,WAAW,CAAC,CAAC;IAEtD,0EAA0E;IAC1E,oDAAoD;IACpD,MAAM,OAAO,GAAG,IAAI,WAAW,CAAC,UAAU,GAAG,CAAC,CAAC,CAAC;IAChD,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,KAAK,IAAI,QAAQ,GAAG,CAAC,EAAE,QAAQ,GAAG,MAAM,EAAE,QAAQ,EAAE,EAAE,CAAC;QACtD,MAAM,KAAK,GAAY,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAE,EAAE,QAAQ,CAAC,CAAC;QAE3D,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,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;QAC/B,IAAI,IAAI,IAAI,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,KAAK,UAAU,EAAE,CAAC;YAClD,MAAM,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAC1B,CAAC;QAED,OAAO,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC;QACzB,OAAO,EAAE,CAAC;IACX,CAAC;IACD,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;QACtB,MAAM,QAAQ,EAAE,CAAC;IAClB,CAAC;IACD,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,UAAU,EAAE,KAAK,EAAE,EAAE,CAAC;QACjD,IAAI,OAAO,CAAC,KAAK,GAAG,CAAC,CAAE,GAAG,OAAO,CAAC,KAAK,CAAE,EAAE,CAAC;YAC3C,MAAM,QAAQ,EAAE,CAAC;QAClB,CAAC;IACF,CAAC;IACD,OAAO,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC,EAAE,UAAU,CAAC,CAAC;IACrC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IAEf,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC;AAC7B,CAAC;AAED,SAAS,QAAQ,CAAC,QAAiB;IAClC,MAAM,KAAK,GACV,QAAQ,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,15 +20,19 @@ 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";
|
|
26
|
+
export * from "./ordered/bisect-right-numbers-array.js";
|
|
27
|
+
export * from "./ordered/bisect-right-numbers.js";
|
|
21
28
|
export * from "./ordered/bisect-right.js";
|
|
22
29
|
export * from "./ordered/bisect.js";
|
|
23
30
|
export * from "./ordered/create-bisector.js";
|
|
31
|
+
export * from "./ordered/fractional-key-alphabet.js";
|
|
32
|
+
export * from "./ordered/fractional-key-error.js";
|
|
33
|
+
export * from "./ordered/fractional-key.js";
|
|
24
34
|
export * from "./recursion/run-trampoline.js";
|
|
25
35
|
export * from "./sample/choice.js";
|
|
26
|
-
export * from "./sample/random-boolean.js";
|
|
27
|
-
export * from "./sample/random-integer.js";
|
|
28
36
|
export * from "./sample/shuffle.js";
|
|
29
37
|
export * from "./select/max-by.js";
|
|
30
38
|
export * from "./select/min-by.js";
|
|
@@ -42,5 +50,8 @@ export * from "./set/symmetric-difference.js";
|
|
|
42
50
|
export * from "./set/union.js";
|
|
43
51
|
export * from "./set/uniq-by.js";
|
|
44
52
|
export * from "./set/uniq.js";
|
|
53
|
+
export * from "./sort/radix-index-sorter.js";
|
|
45
54
|
export * from "./sort/radix-sort.js";
|
|
55
|
+
export * from "./sort/sort-range.js";
|
|
56
|
+
export * from "./sort/sorted-indices.js";
|
|
46
57
|
//# 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
|
|
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,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,CAA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
|
|
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,15 +20,19 @@ 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";
|
|
26
|
+
export * from "./ordered/bisect-right-numbers-array.js";
|
|
27
|
+
export * from "./ordered/bisect-right-numbers.js";
|
|
21
28
|
export * from "./ordered/bisect-right.js";
|
|
22
29
|
export * from "./ordered/bisect.js";
|
|
23
30
|
export * from "./ordered/create-bisector.js";
|
|
31
|
+
export * from "./ordered/fractional-key-alphabet.js";
|
|
32
|
+
export * from "./ordered/fractional-key-error.js";
|
|
33
|
+
export * from "./ordered/fractional-key.js";
|
|
24
34
|
export * from "./recursion/run-trampoline.js";
|
|
25
35
|
export * from "./sample/choice.js";
|
|
26
|
-
export * from "./sample/random-boolean.js";
|
|
27
|
-
export * from "./sample/random-integer.js";
|
|
28
36
|
export * from "./sample/shuffle.js";
|
|
29
37
|
export * from "./select/max-by.js";
|
|
30
38
|
export * from "./select/min-by.js";
|
|
@@ -42,5 +50,8 @@ export * from "./set/symmetric-difference.js";
|
|
|
42
50
|
export * from "./set/union.js";
|
|
43
51
|
export * from "./set/uniq-by.js";
|
|
44
52
|
export * from "./set/uniq.js";
|
|
53
|
+
export * from "./sort/radix-index-sorter.js";
|
|
45
54
|
export * from "./sort/radix-sort.js";
|
|
55
|
+
export * from "./sort/sort-range.js";
|
|
56
|
+
export * from "./sort/sorted-indices.js";
|
|
46
57
|
//# 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
|
|
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,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C,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,38 @@
|
|
|
1
|
+
import type { FractionalKeyAlphabet } from "./fractional-key-alphabet.js";
|
|
2
|
+
import { FractionalKeyError } from "./fractional-key-error.js";
|
|
3
|
+
/** The integer part's length (head included) for a head of value `head`. */
|
|
4
|
+
export declare function integerPartLength_(head: number, alphabet: FractionalKeyAlphabet): number;
|
|
5
|
+
/** Why `key` is not a key, or `null` if it is one. */
|
|
6
|
+
export declare function findKeyError_(key: string, alphabet: FractionalKeyAlphabet): FractionalKeyError | null;
|
|
7
|
+
/** Validates `key` and answers where its integer part ends. */
|
|
8
|
+
export declare function checkKey_(key: string, alphabet: FractionalKeyAlphabet): number;
|
|
9
|
+
/** Where a key already known valid ends its integer part. */
|
|
10
|
+
export declare function integerEnd_(key: string, alphabet: FractionalKeyAlphabet): number;
|
|
11
|
+
export declare function zeroInteger_(alphabet: FractionalKeyAlphabet): string;
|
|
12
|
+
export declare function isSmallestInteger_(integer: string, alphabet: FractionalKeyAlphabet): boolean;
|
|
13
|
+
/** The next integer part, or `null` past the largest one. `O(length)`. */
|
|
14
|
+
export declare function incrementInteger_(integer: string, alphabet: FractionalKeyAlphabet): string | null;
|
|
15
|
+
/** The previous integer part, or `null` below the smallest one. `O(length)`. */
|
|
16
|
+
export declare function decrementInteger_(integer: string, alphabet: FractionalKeyAlphabet): string | null;
|
|
17
|
+
/**
|
|
18
|
+
* The fraction strictly between `lower` and `upper` (`null` for `1`) with the
|
|
19
|
+
* fewest digits, the middle one of those: the `n = 1` case of
|
|
20
|
+
* {@link fractionsBetween_}, without its arithmetic. `O(|lower| + |upper|)`.
|
|
21
|
+
*/
|
|
22
|
+
export declare function fractionBetween_(lower: string, upper: string | null, alphabet: FractionalKeyAlphabet): string;
|
|
23
|
+
/**
|
|
24
|
+
* `count ≥ 1` fractions strictly between `lower` and `upper` (`null` for `1`),
|
|
25
|
+
* ascending: the points dividing the interval into `count + 1` equal parts,
|
|
26
|
+
* rounded down at the coarsest precision that keeps them distinct, so each has
|
|
27
|
+
* at most `max(|lower|, |upper|) + ⌈log_base(count + 1)⌉` digits.
|
|
28
|
+
*
|
|
29
|
+
* Only the window of digits from the first difference on is computed, with a
|
|
30
|
+
* `bigint` while finding the precision and a digit array while stepping. `O(w² +
|
|
31
|
+
* count · (w + p))`, for a window of `w` digits after a shared prefix of `p`.
|
|
32
|
+
*/
|
|
33
|
+
export declare function fractionsBetween_(lower: string, upper: string | null, count: number, alphabet: FractionalKeyAlphabet): string[];
|
|
34
|
+
/** The key just above `key` (valid, its integer part ending at `end`). */
|
|
35
|
+
export declare function keyAfter_(key: string, end: number, alphabet: FractionalKeyAlphabet): string;
|
|
36
|
+
/** The key just below `key` (valid, its integer part ending at `end`). */
|
|
37
|
+
export declare function keyBefore_(key: string, end: number, alphabet: FractionalKeyAlphabet): string;
|
|
38
|
+
//# sourceMappingURL=_fractional-key.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_fractional-key.d.ts","sourceRoot":"","sources":["../../src/ordered/_fractional-key.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAC1E,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAiB/D,4EAA4E;AAC5E,wBAAgB,kBAAkB,CACjC,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,qBAAqB,GAC7B,MAAM,CAIR;AAED,sDAAsD;AACtD,wBAAgB,aAAa,CAC5B,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,qBAAqB,GAC7B,kBAAkB,GAAG,IAAI,CAqC3B;AAED,+DAA+D;AAC/D,wBAAgB,SAAS,CACxB,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,qBAAqB,GAC7B,MAAM,CAOR;AAED,6DAA6D;AAC7D,wBAAgB,WAAW,CAC1B,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,qBAAqB,GAC7B,MAAM,CAER;AAED,wBAAgB,YAAY,CAAC,QAAQ,EAAE,qBAAqB,GAAG,MAAM,CAIpE;AAED,wBAAgB,kBAAkB,CACjC,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,qBAAqB,GAC7B,OAAO,CAOT;AAED,0EAA0E;AAC1E,wBAAgB,iBAAiB,CAChC,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,qBAAqB,GAC7B,MAAM,GAAG,IAAI,CA4Bf;AAED,gFAAgF;AAChF,wBAAgB,iBAAiB,CAChC,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,qBAAqB,GAC7B,MAAM,GAAG,IAAI,CAyBf;AAmCD;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC/B,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,GAAG,IAAI,EACpB,QAAQ,EAAE,qBAAqB,GAC7B,MAAM,CA4BR;AAED;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAChC,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,GAAG,IAAI,EACpB,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,qBAAqB,GAC7B,MAAM,EAAE,CAmEV;AAED,0EAA0E;AAC1E,wBAAgB,SAAS,CACxB,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,qBAAqB,GAC7B,MAAM,CAKR;AAED,0EAA0E;AAC1E,wBAAgB,UAAU,CACzB,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,qBAAqB,GAC7B,MAAM,CAcR"}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { FractionalKeyError } from "./fractional-key-error.js";
|
|
2
|
+
/*
|
|
3
|
+
* A key is a head digit, the integer digits the head calls for, and a
|
|
4
|
+
* fraction:
|
|
5
|
+
*
|
|
6
|
+
* - With `N = ⌊base / 2⌋`, a head of value `h < N` starts a negative integer
|
|
7
|
+
* part of `N − h` digits, and `h ≥ N` a non-negative one of `h − N + 1`
|
|
8
|
+
* digits. Integer parts therefore sort by head first — longer negatives
|
|
9
|
+
* lower, longer non-negatives higher — then by their fixed-width digits.
|
|
10
|
+
* - The fraction is a base-`base` fraction `0.d₁d₂…`, possibly empty, never
|
|
11
|
+
* ending in the zero digit, so that string order on fractions is numeric
|
|
12
|
+
* order.
|
|
13
|
+
* - The smallest integer part (head 0, all zero digits) needs a fraction, so
|
|
14
|
+
* that every key has a key below it.
|
|
15
|
+
*/
|
|
16
|
+
/** The integer part's length (head included) for a head of value `head`. */
|
|
17
|
+
export function integerPartLength_(head, alphabet) {
|
|
18
|
+
const half = alphabet.base >> 1;
|
|
19
|
+
return 1 + (head < half ? half - head : head - half + 1);
|
|
20
|
+
}
|
|
21
|
+
/** Why `key` is not a key, or `null` if it is one. */
|
|
22
|
+
export function findKeyError_(key, alphabet) {
|
|
23
|
+
if (key.length === 0) {
|
|
24
|
+
return new FractionalKeyError(key, 0, "a key is never empty");
|
|
25
|
+
}
|
|
26
|
+
for (let index = 0; index < key.length; index++) {
|
|
27
|
+
if (alphabet.digitValue(key.charCodeAt(index)) < 0) {
|
|
28
|
+
return new FractionalKeyError(key, index, "not a digit of the alphabet");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const head = alphabet.digitValue(key.charCodeAt(0));
|
|
32
|
+
const end = integerPartLength_(head, alphabet);
|
|
33
|
+
if (key.length < end) {
|
|
34
|
+
return new FractionalKeyError(key, key.length, `the integer part needs ${end - 1} digits after its head`);
|
|
35
|
+
}
|
|
36
|
+
if (key.length > end &&
|
|
37
|
+
alphabet.digitValue(key.charCodeAt(key.length - 1)) === 0) {
|
|
38
|
+
return new FractionalKeyError(key, key.length - 1, "a fraction never ends in the zero digit");
|
|
39
|
+
}
|
|
40
|
+
if (key.length === end && isSmallestInteger_(key, alphabet)) {
|
|
41
|
+
return new FractionalKeyError(key, key.length, "the smallest integer part needs a fraction");
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
/** Validates `key` and answers where its integer part ends. */
|
|
46
|
+
export function checkKey_(key, alphabet) {
|
|
47
|
+
const error = findKeyError_(key, alphabet);
|
|
48
|
+
if (error !== null) {
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
return integerPartLength_(alphabet.digitValue(key.charCodeAt(0)), alphabet);
|
|
52
|
+
}
|
|
53
|
+
/** Where a key already known valid ends its integer part. */
|
|
54
|
+
export function integerEnd_(key, alphabet) {
|
|
55
|
+
return integerPartLength_(alphabet.digitValue(key.charCodeAt(0)), alphabet);
|
|
56
|
+
}
|
|
57
|
+
export function zeroInteger_(alphabet) {
|
|
58
|
+
const { digits } = alphabet;
|
|
59
|
+
return digits[alphabet.base >> 1] + digits[0];
|
|
60
|
+
}
|
|
61
|
+
export function isSmallestInteger_(integer, alphabet) {
|
|
62
|
+
for (let index = 0; index < integer.length; index++) {
|
|
63
|
+
if (alphabet.digitValue(integer.charCodeAt(index)) !== 0) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
/** The next integer part, or `null` past the largest one. `O(length)`. */
|
|
70
|
+
export function incrementInteger_(integer, alphabet) {
|
|
71
|
+
const { base, digits } = alphabet;
|
|
72
|
+
let index = integer.length - 1;
|
|
73
|
+
while (index >= 1 &&
|
|
74
|
+
alphabet.digitValue(integer.charCodeAt(index)) === base - 1) {
|
|
75
|
+
index--;
|
|
76
|
+
}
|
|
77
|
+
if (index >= 1) {
|
|
78
|
+
const digit = alphabet.digitValue(integer.charCodeAt(index));
|
|
79
|
+
return (integer.slice(0, index) +
|
|
80
|
+
digits[digit + 1] +
|
|
81
|
+
digits[0].repeat(integer.length - 1 - index));
|
|
82
|
+
}
|
|
83
|
+
const head = alphabet.digitValue(integer.charCodeAt(0));
|
|
84
|
+
if (head === base - 1) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
return (digits[head + 1] +
|
|
88
|
+
digits[0].repeat(integerPartLength_(head + 1, alphabet) - 1));
|
|
89
|
+
}
|
|
90
|
+
/** The previous integer part, or `null` below the smallest one. `O(length)`. */
|
|
91
|
+
export function decrementInteger_(integer, alphabet) {
|
|
92
|
+
const { base, digits } = alphabet;
|
|
93
|
+
let index = integer.length - 1;
|
|
94
|
+
while (index >= 1 && alphabet.digitValue(integer.charCodeAt(index)) === 0) {
|
|
95
|
+
index--;
|
|
96
|
+
}
|
|
97
|
+
if (index >= 1) {
|
|
98
|
+
const digit = alphabet.digitValue(integer.charCodeAt(index));
|
|
99
|
+
return (integer.slice(0, index) +
|
|
100
|
+
digits[digit - 1] +
|
|
101
|
+
digits[base - 1].repeat(integer.length - 1 - index));
|
|
102
|
+
}
|
|
103
|
+
const head = alphabet.digitValue(integer.charCodeAt(0));
|
|
104
|
+
if (head === 0) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
return (digits[head - 1] +
|
|
108
|
+
digits[base - 1].repeat(integerPartLength_(head - 1, alphabet) - 1));
|
|
109
|
+
}
|
|
110
|
+
function digitAt_(fraction, index, alphabet) {
|
|
111
|
+
return index < fraction.length
|
|
112
|
+
? alphabet.digitValue(fraction.charCodeAt(index))
|
|
113
|
+
: 0;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Where `lower` and `upper` first differ, `lower` read with trailing zeros; `0`
|
|
117
|
+
* when `upper` is the open end `1`.
|
|
118
|
+
*/
|
|
119
|
+
function firstDifference_(lower, upper, alphabet) {
|
|
120
|
+
if (upper === null) {
|
|
121
|
+
return 0;
|
|
122
|
+
}
|
|
123
|
+
let index = 0;
|
|
124
|
+
while (digitAt_(lower, index, alphabet) ===
|
|
125
|
+
alphabet.digitValue(upper.charCodeAt(index))) {
|
|
126
|
+
index++;
|
|
127
|
+
}
|
|
128
|
+
return index;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* The fraction strictly between `lower` and `upper` (`null` for `1`) with the
|
|
132
|
+
* fewest digits, the middle one of those: the `n = 1` case of
|
|
133
|
+
* {@link fractionsBetween_}, without its arithmetic. `O(|lower| + |upper|)`.
|
|
134
|
+
*/
|
|
135
|
+
export function fractionBetween_(lower, upper, alphabet) {
|
|
136
|
+
const { base, digits } = alphabet;
|
|
137
|
+
let index = firstDifference_(lower, upper, alphabet);
|
|
138
|
+
let result = upper === null ? "" : upper.slice(0, index);
|
|
139
|
+
if (upper !== null) {
|
|
140
|
+
const low = digitAt_(lower, index, alphabet);
|
|
141
|
+
const high = alphabet.digitValue(upper.charCodeAt(index));
|
|
142
|
+
const gap = high - low + (upper.length > index + 1 ? 1 : 0);
|
|
143
|
+
if (gap >= 2) {
|
|
144
|
+
return result + digits[low + (gap >> 1)];
|
|
145
|
+
}
|
|
146
|
+
// `upper` ends right after `low + 1`: continue above `lower` towards 1.
|
|
147
|
+
result += digits[low];
|
|
148
|
+
index++;
|
|
149
|
+
}
|
|
150
|
+
for (;;) {
|
|
151
|
+
const low = digitAt_(lower, index, alphabet);
|
|
152
|
+
const gap = base - low;
|
|
153
|
+
if (gap >= 2) {
|
|
154
|
+
return result + digits[low + (gap >> 1)];
|
|
155
|
+
}
|
|
156
|
+
result += digits[low];
|
|
157
|
+
index++;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* `count ≥ 1` fractions strictly between `lower` and `upper` (`null` for `1`),
|
|
162
|
+
* ascending: the points dividing the interval into `count + 1` equal parts,
|
|
163
|
+
* rounded down at the coarsest precision that keeps them distinct, so each has
|
|
164
|
+
* at most `max(|lower|, |upper|) + ⌈log_base(count + 1)⌉` digits.
|
|
165
|
+
*
|
|
166
|
+
* Only the window of digits from the first difference on is computed, with a
|
|
167
|
+
* `bigint` while finding the precision and a digit array while stepping. `O(w² +
|
|
168
|
+
* count · (w + p))`, for a window of `w` digits after a shared prefix of `p`.
|
|
169
|
+
*/
|
|
170
|
+
export function fractionsBetween_(lower, upper, count, alphabet) {
|
|
171
|
+
const { base, digits } = alphabet;
|
|
172
|
+
const bigBase = BigInt(base);
|
|
173
|
+
const parts = BigInt(count + 1);
|
|
174
|
+
const start = firstDifference_(lower, upper, alphabet);
|
|
175
|
+
const prefix = upper === null ? "" : upper.slice(0, start);
|
|
176
|
+
// The window [low, high) scaled by base^width; `1` is one unit at width 0.
|
|
177
|
+
let low = 0n;
|
|
178
|
+
let highFloor = upper === null ? 1n : 0n;
|
|
179
|
+
let gap = 0n;
|
|
180
|
+
let width = 0;
|
|
181
|
+
do {
|
|
182
|
+
const position = start + width;
|
|
183
|
+
low = low * bigBase + BigInt(digitAt_(lower, position, alphabet));
|
|
184
|
+
highFloor =
|
|
185
|
+
highFloor * bigBase +
|
|
186
|
+
BigInt(upper === null ? 0 : digitAt_(upper, position, alphabet));
|
|
187
|
+
width++;
|
|
188
|
+
const highCeiling = upper !== null && upper.length > position + 1
|
|
189
|
+
? highFloor + 1n
|
|
190
|
+
: highFloor;
|
|
191
|
+
gap = highCeiling - low;
|
|
192
|
+
} while (gap < parts);
|
|
193
|
+
const point = new Uint8Array(width);
|
|
194
|
+
const step = new Uint8Array(width);
|
|
195
|
+
let quotient = gap / parts;
|
|
196
|
+
for (let position = width - 1; position >= 0; position--) {
|
|
197
|
+
point[position] = digitAt_(lower, start + position, alphabet);
|
|
198
|
+
step[position] = Number(quotient % bigBase);
|
|
199
|
+
quotient /= bigBase;
|
|
200
|
+
}
|
|
201
|
+
// point_j = low + ⌊j · gap / parts⌋, stepped by ⌊gap / parts⌋ plus a carry
|
|
202
|
+
// from the remainder, Bresenham-style.
|
|
203
|
+
const remainder = Number(gap % parts);
|
|
204
|
+
const partCount = count + 1;
|
|
205
|
+
let accumulated = 0;
|
|
206
|
+
const fractions = new Array(count);
|
|
207
|
+
for (let index = 0; index < count; index++) {
|
|
208
|
+
accumulated += remainder;
|
|
209
|
+
let carry = 0;
|
|
210
|
+
if (accumulated >= partCount) {
|
|
211
|
+
accumulated -= partCount;
|
|
212
|
+
carry = 1;
|
|
213
|
+
}
|
|
214
|
+
for (let position = width - 1; position >= 0; position--) {
|
|
215
|
+
const sum = point[position] + step[position] + carry;
|
|
216
|
+
carry = sum >= base ? 1 : 0;
|
|
217
|
+
point[position] = sum - carry * base;
|
|
218
|
+
}
|
|
219
|
+
let last = width - 1;
|
|
220
|
+
while (point[last] === 0) {
|
|
221
|
+
last--;
|
|
222
|
+
}
|
|
223
|
+
let fraction = prefix;
|
|
224
|
+
for (let position = 0; position <= last; position++) {
|
|
225
|
+
fraction += digits[point[position]];
|
|
226
|
+
}
|
|
227
|
+
fractions[index] = fraction;
|
|
228
|
+
}
|
|
229
|
+
return fractions;
|
|
230
|
+
}
|
|
231
|
+
/** The key just above `key` (valid, its integer part ending at `end`). */
|
|
232
|
+
export function keyAfter_(key, end, alphabet) {
|
|
233
|
+
const integer = key.slice(0, end);
|
|
234
|
+
const next = incrementInteger_(integer, alphabet);
|
|
235
|
+
return next ?? integer + fractionBetween_(key.slice(end), null, alphabet);
|
|
236
|
+
}
|
|
237
|
+
/** The key just below `key` (valid, its integer part ending at `end`). */
|
|
238
|
+
export function keyBefore_(key, end, alphabet) {
|
|
239
|
+
const integer = key.slice(0, end);
|
|
240
|
+
if (key.length > end) {
|
|
241
|
+
return isSmallestInteger_(integer, alphabet)
|
|
242
|
+
? integer + fractionBetween_("", key.slice(end), alphabet)
|
|
243
|
+
: integer;
|
|
244
|
+
}
|
|
245
|
+
// Only the smallest integer has no predecessor, and it never stands alone.
|
|
246
|
+
const previous = decrementInteger_(integer, alphabet);
|
|
247
|
+
return isSmallestInteger_(previous, alphabet)
|
|
248
|
+
? previous + fractionBetween_("", null, alphabet)
|
|
249
|
+
: previous;
|
|
250
|
+
}
|
|
251
|
+
//# sourceMappingURL=_fractional-key.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_fractional-key.js","sourceRoot":"","sources":["../../src/ordered/_fractional-key.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAE/D;;;;;;;;;;;;;GAaG;AAEH,4EAA4E;AAC5E,MAAM,UAAU,kBAAkB,CACjC,IAAY,EACZ,QAA+B;IAE/B,MAAM,IAAI,GAAG,QAAQ,CAAC,IAAI,IAAI,CAAC,CAAC;IAEhC,OAAO,CAAC,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC;AAC1D,CAAC;AAED,sDAAsD;AACtD,MAAM,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,CACxB,GAAW,EACX,GAAW,EACX,QAA+B;IAE/B,MAAM,OAAO,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IAClC,MAAM,IAAI,GAAG,iBAAiB,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IAElD,OAAO,IAAI,IAAI,OAAO,GAAG,gBAAgB,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;AAC3E,CAAC;AAED,0EAA0E;AAC1E,MAAM,UAAU,UAAU,CACzB,GAAW,EACX,GAAW,EACX,QAA+B;IAE/B,MAAM,OAAO,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IAElC,IAAI,GAAG,CAAC,MAAM,GAAG,GAAG,EAAE,CAAC;QACtB,OAAO,kBAAkB,CAAC,OAAO,EAAE,QAAQ,CAAC;YAC3C,CAAC,CAAC,OAAO,GAAG,gBAAgB,CAAC,EAAE,EAAE,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC;YAC1D,CAAC,CAAC,OAAO,CAAC;IACZ,CAAC;IACD,2EAA2E;IAC3E,MAAM,QAAQ,GAAG,iBAAiB,CAAC,OAAO,EAAE,QAAQ,CAAE,CAAC;IAEvD,OAAO,kBAAkB,CAAC,QAAQ,EAAE,QAAQ,CAAC;QAC5C,CAAC,CAAC,QAAQ,GAAG,gBAAgB,CAAC,EAAE,EAAE,IAAI,EAAE,QAAQ,CAAC;QACjD,CAAC,CAAC,QAAQ,CAAC;AACb,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
|