@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 @@
|
|
|
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"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* {@link bisectRightNumbers} of every value: `out[i]` is how many elements of
|
|
3
|
+
* `items` are `≤ values[i]`, the index after the last of them. The searches run
|
|
4
|
+
* inside one loop, so no number crosses a call per value and nothing is
|
|
5
|
+
* allocated, wherever the call is made from.
|
|
6
|
+
*
|
|
7
|
+
* O(m log n) for m values and n items. `items` must be sorted ascending. A NaN
|
|
8
|
+
* value compares false with everything and gives 0, as
|
|
9
|
+
* {@link bisectRightNumbers} gives `low`; handle it before or after calling.
|
|
10
|
+
*
|
|
11
|
+
* @param out One index per value; its element type must hold `items.length` (an
|
|
12
|
+
* `Int32Array` holds up to 2³¹ − 1).
|
|
13
|
+
* @param items Numbers sorted ascending; a typed array reads without boxing.
|
|
14
|
+
* @param values The numbers to find insertion points for.
|
|
15
|
+
* @returns `out`.
|
|
16
|
+
* @throws {RangeError} When `out` and `values` differ in length.
|
|
17
|
+
* @see {@link bisectRightNumbers} for one value, or a window of `items`.
|
|
18
|
+
*/
|
|
19
|
+
export declare function bisectRightNumbersArray<T extends Int32Array | Uint32Array | Float64Array>(out: T, items: ArrayLike<number>, values: ArrayLike<number>): T;
|
|
20
|
+
//# sourceMappingURL=bisect-right-numbers-array.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bisect-right-numbers-array.d.ts","sourceRoot":"","sources":["../../src/ordered/bisect-right-numbers-array.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,uBAAuB,CACtC,CAAC,SAAS,UAAU,GAAG,WAAW,GAAG,YAAY,EAChD,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,MAAM,CAAC,GAAG,CAAC,CAwBhE"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* {@link bisectRightNumbers} of every value: `out[i]` is how many elements of
|
|
3
|
+
* `items` are `≤ values[i]`, the index after the last of them. The searches run
|
|
4
|
+
* inside one loop, so no number crosses a call per value and nothing is
|
|
5
|
+
* allocated, wherever the call is made from.
|
|
6
|
+
*
|
|
7
|
+
* O(m log n) for m values and n items. `items` must be sorted ascending. A NaN
|
|
8
|
+
* value compares false with everything and gives 0, as
|
|
9
|
+
* {@link bisectRightNumbers} gives `low`; handle it before or after calling.
|
|
10
|
+
*
|
|
11
|
+
* @param out One index per value; its element type must hold `items.length` (an
|
|
12
|
+
* `Int32Array` holds up to 2³¹ − 1).
|
|
13
|
+
* @param items Numbers sorted ascending; a typed array reads without boxing.
|
|
14
|
+
* @param values The numbers to find insertion points for.
|
|
15
|
+
* @returns `out`.
|
|
16
|
+
* @throws {RangeError} When `out` and `values` differ in length.
|
|
17
|
+
* @see {@link bisectRightNumbers} for one value, or a window of `items`.
|
|
18
|
+
*/
|
|
19
|
+
export function bisectRightNumbersArray(out, items, values) {
|
|
20
|
+
if (out.length !== values.length) {
|
|
21
|
+
throw new RangeError(`bisectRightNumbersArray: the output has ${out.length} elements and the values ${values.length}; they must agree.`);
|
|
22
|
+
}
|
|
23
|
+
const length = values.length;
|
|
24
|
+
const count = items.length;
|
|
25
|
+
for (let index = 0; index < length; index++) {
|
|
26
|
+
const value = values[index];
|
|
27
|
+
let start = 0;
|
|
28
|
+
let end = count;
|
|
29
|
+
while (start < end) {
|
|
30
|
+
const middle = start + Math.floor((end - start) / 2);
|
|
31
|
+
if (items[middle] <= value) {
|
|
32
|
+
start = middle + 1;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
end = middle;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
out[index] = start;
|
|
39
|
+
}
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=bisect-right-numbers-array.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bisect-right-numbers-array.js","sourceRoot":"","sources":["../../src/ordered/bisect-right-numbers-array.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,uBAAuB,CAErC,GAAM,EAAE,KAAwB,EAAE,MAAyB;IAC5D,IAAI,GAAG,CAAC,MAAM,KAAK,MAAM,CAAC,MAAM,EAAE,CAAC;QAClC,MAAM,IAAI,UAAU,CACnB,2CAA2C,GAAG,CAAC,MAAM,4BAA4B,MAAM,CAAC,MAAM,oBAAoB,CAClH,CAAC;IACH,CAAC;IACD,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;IAC7B,MAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC;IAC3B,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QAC7C,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAE,CAAC;QAC7B,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,IAAI,GAAG,GAAG,KAAK,CAAC;QAChB,OAAO,KAAK,GAAG,GAAG,EAAE,CAAC;YACpB,MAAM,MAAM,GAAG,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;YACrD,IAAI,KAAK,CAAC,MAAM,CAAE,IAAI,KAAK,EAAE,CAAC;gBAC7B,KAAK,GAAG,MAAM,GAAG,CAAC,CAAC;YACpB,CAAC;iBAAM,CAAC;gBACP,GAAG,GAAG,MAAM,CAAC;YACd,CAAC;QACF,CAAC;QACD,GAAG,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC;IACpB,CAAC;IAED,OAAO,GAAG,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finds the index after the last element of a sorted numeric array that is `≤
|
|
3
|
+
* value`, which is also how many elements are `≤ value`. It is
|
|
4
|
+
* {@link bisectRight} for numbers, with no comparator call per probe and no
|
|
5
|
+
* closure, so it allocates nothing.
|
|
6
|
+
*
|
|
7
|
+
* O(log n). `items` must be sorted ascending within `[low, high)`. A NaN
|
|
8
|
+
* `value` compares false with everything and returns `low`; handle it before
|
|
9
|
+
* calling.
|
|
10
|
+
*
|
|
11
|
+
* @param items Numbers sorted ascending; a typed array reads without boxing.
|
|
12
|
+
* @param value The number to find an insertion point for.
|
|
13
|
+
* @param low First index of the searched window.
|
|
14
|
+
* @param high End (exclusive) of the searched window.
|
|
15
|
+
* @returns The rightmost valid insertion index, in `[low, high]`.
|
|
16
|
+
* @see {@link bisectRight} for any item type.
|
|
17
|
+
*/
|
|
18
|
+
export declare function bisectRightNumbers(items: ArrayLike<number>, value: number, low?: number, high?: number): number;
|
|
19
|
+
//# sourceMappingURL=bisect-right-numbers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bisect-right-numbers.d.ts","sourceRoot":"","sources":["../../src/ordered/bisect-right-numbers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,kBAAkB,CACjC,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,EACxB,KAAK,EAAE,MAAM,EACb,GAAG,SAAI,EACP,IAAI,SAAe,GACjB,MAAM,CAaR"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finds the index after the last element of a sorted numeric array that is `≤
|
|
3
|
+
* value`, which is also how many elements are `≤ value`. It is
|
|
4
|
+
* {@link bisectRight} for numbers, with no comparator call per probe and no
|
|
5
|
+
* closure, so it allocates nothing.
|
|
6
|
+
*
|
|
7
|
+
* O(log n). `items` must be sorted ascending within `[low, high)`. A NaN
|
|
8
|
+
* `value` compares false with everything and returns `low`; handle it before
|
|
9
|
+
* calling.
|
|
10
|
+
*
|
|
11
|
+
* @param items Numbers sorted ascending; a typed array reads without boxing.
|
|
12
|
+
* @param value The number to find an insertion point for.
|
|
13
|
+
* @param low First index of the searched window.
|
|
14
|
+
* @param high End (exclusive) of the searched window.
|
|
15
|
+
* @returns The rightmost valid insertion index, in `[low, high]`.
|
|
16
|
+
* @see {@link bisectRight} for any item type.
|
|
17
|
+
*/
|
|
18
|
+
export function bisectRightNumbers(items, value, low = 0, high = items.length) {
|
|
19
|
+
let start = low;
|
|
20
|
+
let end = high;
|
|
21
|
+
while (start < end) {
|
|
22
|
+
const middle = start + Math.floor((end - start) / 2);
|
|
23
|
+
if (items[middle] <= value) {
|
|
24
|
+
start = middle + 1;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
end = middle;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return start;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=bisect-right-numbers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bisect-right-numbers.js","sourceRoot":"","sources":["../../src/ordered/bisect-right-numbers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,kBAAkB,CACjC,KAAwB,EACxB,KAAa,EACb,GAAG,GAAG,CAAC,EACP,IAAI,GAAG,KAAK,CAAC,MAAM;IAEnB,IAAI,KAAK,GAAG,GAAG,CAAC;IAChB,IAAI,GAAG,GAAG,IAAI,CAAC;IACf,OAAO,KAAK,GAAG,GAAG,EAAE,CAAC;QACpB,MAAM,MAAM,GAAG,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;QACrD,IAAI,KAAK,CAAC,MAAM,CAAE,IAAI,KAAK,EAAE,CAAC;YAC7B,KAAK,GAAG,MAAM,GAAG,CAAC,CAAC;QACpB,CAAC;aAAM,CAAC;YACP,GAAG,GAAG,MAAM,CAAC;QACd,CAAC;IACF,CAAC;IAED,OAAO,KAAK,CAAC;AACd,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"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-bisector.js","sourceRoot":"","sources":["../../src/ordered/create-bisector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAEtE,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAahD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,cAAc,CAC7B,OAAyB;IAEzB,MAAM,OAAO,GAAG,CAAC,IAAO,EAAE,KAAQ,EAAE,EAAE,CACrC,uBAAuB,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;IAE/C,2EAA2E;IAC3E,4CAA4C;IAC5C,OAAO;QACN,IAAI,EAAE,CAAC,KAAmB,EAAE,KAAQ,EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC;QAC1E,KAAK,EAAE,CAAC,KAAmB,EAAE,KAAQ,EAAE,EAAE,CACxC,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC;QACnC,MAAM,EAAE,CAAC,KAAmB,EAAE,KAAa,EAAE,EAAE,CAC9C,YAAY,CAAC,KAAK,EAAE,KAAK,EAAE,OAAgC,CAAC;
|
|
1
|
+
{"version":3,"file":"create-bisector.js","sourceRoot":"","sources":["../../src/ordered/create-bisector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAEtE,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAahD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,cAAc,CAC7B,OAAyB;IAEzB,MAAM,OAAO,GAAG,CAAC,IAAO,EAAE,KAAQ,EAAE,EAAE,CACrC,uBAAuB,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;IAE/C,2EAA2E;IAC3E,4CAA4C;IAC5C,OAAO;QACN,IAAI,EAAE,CAAC,KAAmB,EAAE,KAAQ,EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC;QAC1E,KAAK,EAAE,CAAC,KAAmB,EAAE,KAAQ,EAAE,EAAE,CACxC,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC;QACnC,MAAM,EAAE,CAAC,KAAmB,EAAE,KAAa,EAAE,EAAE,CAC9C,YAAY,CAAC,KAAK,EAAE,KAAK,EAAE,OAAgC,CAAC;KAC7D,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ordered digits fractional keys are written in.
|
|
3
|
+
*
|
|
4
|
+
* Digits are ASCII graphic characters (`!` to `~`) in strictly increasing
|
|
5
|
+
* code-unit order, so a key's order is the same under JavaScript's `<`, UTF-8
|
|
6
|
+
* or Latin-1 byte order and a database's binary collation (`COLLATE "C"`,
|
|
7
|
+
* `BINARY`). A case-insensitive collation sorts base 62 differently: give such
|
|
8
|
+
* a store a single-case alphabet (`"0123456789abcdefghijklmnopqrstuvwxyz"`).
|
|
9
|
+
*
|
|
10
|
+
* The lower half of the digits (`⌊base / 2⌋` of them) are the heads of negative
|
|
11
|
+
* integer parts, the upper half those of non-negative ones; a larger alphabet
|
|
12
|
+
* gives shorter keys and more integer heads.
|
|
13
|
+
*/
|
|
14
|
+
export declare class FractionalKeyAlphabet {
|
|
15
|
+
/** The digits, smallest first. */
|
|
16
|
+
readonly digits: string;
|
|
17
|
+
/** How many digits there are, at least 2 and at most 94. */
|
|
18
|
+
readonly base: number;
|
|
19
|
+
private readonly values;
|
|
20
|
+
/**
|
|
21
|
+
* @param digits At least two ASCII graphic characters in strictly increasing
|
|
22
|
+
* code-unit order.
|
|
23
|
+
* @throws {RangeError} If `digits` is shorter than two, holds a character
|
|
24
|
+
* outside `!`–`~`, or is not strictly increasing.
|
|
25
|
+
*/
|
|
26
|
+
constructor(digits: string);
|
|
27
|
+
/**
|
|
28
|
+
* The value of the digit written as code unit `code`.
|
|
29
|
+
*
|
|
30
|
+
* Complexity: `O(1)`.
|
|
31
|
+
*
|
|
32
|
+
* @returns From `0` to `base - 1`, or `-1` if `code` is not a digit.
|
|
33
|
+
*/
|
|
34
|
+
digitValue(code: number): number;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The default alphabet: `0`–`9`, `A`–`Z`, `a`–`z`, in byte order. Keys start at
|
|
38
|
+
* `"V0"` (the integer zero).
|
|
39
|
+
*/
|
|
40
|
+
export declare const BASE62_FRACTIONAL_KEY_ALPHABET: FractionalKeyAlphabet;
|
|
41
|
+
//# sourceMappingURL=fractional-key-alphabet.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fractional-key-alphabet.d.ts","sourceRoot":"","sources":["../../src/ordered/fractional-key-alphabet.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;GAYG;AACH,qBAAa,qBAAqB;IACjC,kCAAkC;IAClC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAExB,4DAA4D;IAC5D,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAY;IAEnC;;;;;OAKG;IACH,YAAY,MAAM,EAAE,MAAM,EA4BzB;IAED;;;;;;OAMG;IACH,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAI/B;CACD;AAED;;;GAGG;AACH,eAAO,MAAM,8BAA8B,EAAE,qBAG3C,CAAC"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { isAsciiGraphic } from "@ac-kit/core";
|
|
2
|
+
const ASCII_CODES_ = 0x80;
|
|
3
|
+
/**
|
|
4
|
+
* The ordered digits fractional keys are written in.
|
|
5
|
+
*
|
|
6
|
+
* Digits are ASCII graphic characters (`!` to `~`) in strictly increasing
|
|
7
|
+
* code-unit order, so a key's order is the same under JavaScript's `<`, UTF-8
|
|
8
|
+
* or Latin-1 byte order and a database's binary collation (`COLLATE "C"`,
|
|
9
|
+
* `BINARY`). A case-insensitive collation sorts base 62 differently: give such
|
|
10
|
+
* a store a single-case alphabet (`"0123456789abcdefghijklmnopqrstuvwxyz"`).
|
|
11
|
+
*
|
|
12
|
+
* The lower half of the digits (`⌊base / 2⌋` of them) are the heads of negative
|
|
13
|
+
* integer parts, the upper half those of non-negative ones; a larger alphabet
|
|
14
|
+
* gives shorter keys and more integer heads.
|
|
15
|
+
*/
|
|
16
|
+
export class FractionalKeyAlphabet {
|
|
17
|
+
/** The digits, smallest first. */
|
|
18
|
+
digits;
|
|
19
|
+
/** How many digits there are, at least 2 and at most 94. */
|
|
20
|
+
base;
|
|
21
|
+
values;
|
|
22
|
+
/**
|
|
23
|
+
* @param digits At least two ASCII graphic characters in strictly increasing
|
|
24
|
+
* code-unit order.
|
|
25
|
+
* @throws {RangeError} If `digits` is shorter than two, holds a character
|
|
26
|
+
* outside `!`–`~`, or is not strictly increasing.
|
|
27
|
+
*/
|
|
28
|
+
constructor(digits) {
|
|
29
|
+
if (digits.length < 2) {
|
|
30
|
+
throw new RangeError(`A fractional key alphabet needs at least 2 digits, got ${digits.length}`);
|
|
31
|
+
}
|
|
32
|
+
const values = new Int8Array(ASCII_CODES_).fill(-1);
|
|
33
|
+
let previous = -1;
|
|
34
|
+
for (let index = 0; index < digits.length; index++) {
|
|
35
|
+
const code = digits.charCodeAt(index);
|
|
36
|
+
if (!isAsciiGraphic(code)) {
|
|
37
|
+
throw new RangeError(`Fractional key digit ${index} must be an ASCII graphic character, got U+${code.toString(16).toUpperCase().padStart(4, "0")}`);
|
|
38
|
+
}
|
|
39
|
+
if (code <= previous) {
|
|
40
|
+
throw new RangeError(`Fractional key digits must be in strictly increasing code-unit order, digit ${index} is not`);
|
|
41
|
+
}
|
|
42
|
+
values[code] = index;
|
|
43
|
+
previous = code;
|
|
44
|
+
}
|
|
45
|
+
this.digits = digits;
|
|
46
|
+
this.base = digits.length;
|
|
47
|
+
this.values = values;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The value of the digit written as code unit `code`.
|
|
51
|
+
*
|
|
52
|
+
* Complexity: `O(1)`.
|
|
53
|
+
*
|
|
54
|
+
* @returns From `0` to `base - 1`, or `-1` if `code` is not a digit.
|
|
55
|
+
*/
|
|
56
|
+
digitValue(code) {
|
|
57
|
+
return Number.isInteger(code) && code >= 0 && code < ASCII_CODES_
|
|
58
|
+
? this.values[code]
|
|
59
|
+
: -1;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The default alphabet: `0`–`9`, `A`–`Z`, `a`–`z`, in byte order. Keys start at
|
|
64
|
+
* `"V0"` (the integer zero).
|
|
65
|
+
*/
|
|
66
|
+
export const BASE62_FRACTIONAL_KEY_ALPHABET = new FractionalKeyAlphabet("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
|
|
67
|
+
//# sourceMappingURL=fractional-key-alphabet.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fractional-key-alphabet.js","sourceRoot":"","sources":["../../src/ordered/fractional-key-alphabet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C,MAAM,YAAY,GAAG,IAAI,CAAC;AAE1B;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,qBAAqB;IACjC,kCAAkC;IACzB,MAAM,CAAS;IAExB,4DAA4D;IACnD,IAAI,CAAS;IAEL,MAAM,CAAY;IAEnC;;;;;OAKG;IACH,YAAY,MAAc;QACzB,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACvB,MAAM,IAAI,UAAU,CACnB,0DAA0D,MAAM,CAAC,MAAM,EAAE,CACzE,CAAC;QACH,CAAC;QACD,MAAM,MAAM,GAAG,IAAI,SAAS,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QACpD,IAAI,QAAQ,GAAG,CAAC,CAAC,CAAC;QAElB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;YACpD,MAAM,IAAI,GAAG,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;YAEtC,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,EAAE,CAAC;gBAC3B,MAAM,IAAI,UAAU,CACnB,wBAAwB,KAAK,8CAA8C,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,CAC7H,CAAC;YACH,CAAC;YACD,IAAI,IAAI,IAAI,QAAQ,EAAE,CAAC;gBACtB,MAAM,IAAI,UAAU,CACnB,+EAA+E,KAAK,SAAS,CAC7F,CAAC;YACH,CAAC;YACD,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;YACrB,QAAQ,GAAG,IAAI,CAAC;QACjB,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC;QAC1B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACtB,CAAC;IAED;;;;;;OAMG;IACH,UAAU,CAAC,IAAY;QACtB,OAAO,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,IAAI,GAAG,YAAY;YAChE,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAE;YACpB,CAAC,CAAC,CAAC,CAAC,CAAC;IACP,CAAC;CACD;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,8BAA8B,GAC1C,IAAI,qBAAqB,CACxB,gEAAgE,CAChE,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error thrown for a string that is not a fractional key under the alphabet in
|
|
3
|
+
* use.
|
|
4
|
+
*/
|
|
5
|
+
export declare class FractionalKeyError extends SyntaxError {
|
|
6
|
+
/** The rejected string. */
|
|
7
|
+
readonly key: string;
|
|
8
|
+
/**
|
|
9
|
+
* The code-unit offset the check gave up at: the offending digit, or the
|
|
10
|
+
* key's length when the key stops too early.
|
|
11
|
+
*/
|
|
12
|
+
readonly index: number;
|
|
13
|
+
constructor(key: string, index: number, reason: string);
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=fractional-key-error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fractional-key-error.d.ts","sourceRoot":"","sources":["../../src/ordered/fractional-key-error.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,qBAAa,kBAAmB,SAAQ,WAAW;IAClD,2BAA2B;IAC3B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAErB;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB,YAAY,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAOrD;CACD"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error thrown for a string that is not a fractional key under the alphabet in
|
|
3
|
+
* use.
|
|
4
|
+
*/
|
|
5
|
+
export class FractionalKeyError extends SyntaxError {
|
|
6
|
+
/** The rejected string. */
|
|
7
|
+
key;
|
|
8
|
+
/**
|
|
9
|
+
* The code-unit offset the check gave up at: the offending digit, or the
|
|
10
|
+
* key's length when the key stops too early.
|
|
11
|
+
*/
|
|
12
|
+
index;
|
|
13
|
+
constructor(key, index, reason) {
|
|
14
|
+
super(`Invalid fractional key ${JSON.stringify(key)} at ${index}: ${reason}`);
|
|
15
|
+
this.name = "FractionalKeyError";
|
|
16
|
+
this.key = key;
|
|
17
|
+
this.index = index;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=fractional-key-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fractional-key-error.js","sourceRoot":"","sources":["../../src/ordered/fractional-key-error.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,OAAO,kBAAmB,SAAQ,WAAW;IAClD,2BAA2B;IAClB,GAAG,CAAS;IAErB;;;OAGG;IACM,KAAK,CAAS;IAEvB,YAAY,GAAW,EAAE,KAAa,EAAE,MAAc;QACrD,KAAK,CACJ,0BAA0B,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,KAAK,KAAK,MAAM,EAAE,CACtE,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;QACjC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACpB,CAAC;CACD"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { type FractionalKeyAlphabet } from "./fractional-key-alphabet.js";
|
|
2
|
+
/** Options of the fractional key functions. */
|
|
3
|
+
export type FractionalKeyOptions = {
|
|
4
|
+
/** Default {@link BASE62_FRACTIONAL_KEY_ALPHABET}. */
|
|
5
|
+
readonly alphabet?: FractionalKeyAlphabet;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Whether `key` is a well-formed fractional key under the alphabet.
|
|
9
|
+
*
|
|
10
|
+
* A key is a head digit, the integer digits its head calls for, then a fraction
|
|
11
|
+
* that never ends in the zero digit; the smallest integer part (head and digits
|
|
12
|
+
* all zero) needs a fraction. See {@link generateFractionalKeyBetween}.
|
|
13
|
+
*
|
|
14
|
+
* Complexity: `O(|key|)`.
|
|
15
|
+
*/
|
|
16
|
+
export declare function isFractionalKey(key: string, options?: FractionalKeyOptions): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Throws unless `key` is a well-formed fractional key under the alphabet: use
|
|
19
|
+
* it where keys come from storage or the network.
|
|
20
|
+
*
|
|
21
|
+
* Complexity: `O(|key|)`.
|
|
22
|
+
*
|
|
23
|
+
* @throws {FractionalKeyError} With the offset the check gave up at.
|
|
24
|
+
*/
|
|
25
|
+
export declare function assertFractionalKey(key: string, options?: FractionalKeyOptions): void;
|
|
26
|
+
/**
|
|
27
|
+
* A key that sorts strictly between `lower` and `upper` under plain string
|
|
28
|
+
* comparison — fractional indexing, so that a user-ordered list inserts or
|
|
29
|
+
* moves one item by writing one key, with nothing renumbered.
|
|
30
|
+
*
|
|
31
|
+
* `null` is an open end: `(null, null)` is the first key, `(last, null)`
|
|
32
|
+
* appends and `(null, first)` prepends. Keys have an integer part and a
|
|
33
|
+
* fraction, and:
|
|
34
|
+
*
|
|
35
|
+
* - Appending or prepending steps the integer part, so `n` appends from one key
|
|
36
|
+
* grow it by `⌈log_base n⌉` digits at most (base 62: two digits hold 3 844
|
|
37
|
+
* appends, five hold 916 million). Past the last integer, which base 62
|
|
38
|
+
* reaches after `62^31` appends, keys keep growing in the fraction;
|
|
39
|
+
* - Between two keys, the result is the shortest key strictly between them — the
|
|
40
|
+
* next integer when there is room, otherwise the middle fraction digit — so
|
|
41
|
+
* it is never more than one code unit longer than the longer bound. Inserting
|
|
42
|
+
* again and again into the same gap halves it each time, adding a digit every
|
|
43
|
+
* `log₂ base` insertions (about 6 in base 62): spread a batch with
|
|
44
|
+
* {@link generateFractionalKeysBetween} instead.
|
|
45
|
+
*
|
|
46
|
+
* The result is deterministic: two replicas inserting into the same gap get the
|
|
47
|
+
* same key, which ties under string order. Break the tie by a second sort field
|
|
48
|
+
* (a record id), and generate a later key between the tied pair's neighbours.
|
|
49
|
+
*
|
|
50
|
+
* Complexity: `O(|lower| + |upper|)`.
|
|
51
|
+
*
|
|
52
|
+
* @param lower The key to sort after, or `null` for no lower bound.
|
|
53
|
+
* @param upper The key to sort before, or `null` for no upper bound.
|
|
54
|
+
* @throws {FractionalKeyError} If a bound is not a key under the alphabet.
|
|
55
|
+
* @throws {RangeError} If `lower` does not sort before `upper`.
|
|
56
|
+
*/
|
|
57
|
+
export declare function generateFractionalKeyBetween(lower: string | null, upper: string | null, options?: FractionalKeyOptions): string;
|
|
58
|
+
/**
|
|
59
|
+
* `count` ascending keys strictly between `lower` and `upper` (`null` for an
|
|
60
|
+
* open end), for inserting a batch at one place.
|
|
61
|
+
*
|
|
62
|
+
* With an open end, the keys are consecutive appends (or prepends), as repeated
|
|
63
|
+
* {@link generateFractionalKeyBetween} calls would make them. Between two keys,
|
|
64
|
+
* they take the free integers first; the rest are fractions that divide each
|
|
65
|
+
* remaining gap evenly, at the coarsest precision holding them, so every key is
|
|
66
|
+
* at most `⌈log_base(count + 1)⌉` code units longer than the longer bound.
|
|
67
|
+
* `count = 1` gives the key `generateFractionalKeyBetween` gives. Regenerating
|
|
68
|
+
* a run of keys between its neighbours this way also shortens a run that grew
|
|
69
|
+
* long from many insertions into one gap.
|
|
70
|
+
*
|
|
71
|
+
* Complexity: `O(count · L)` for keys of length `L`, plus `O(w²)` once for a
|
|
72
|
+
* window of `w` digits where the bounds differ.
|
|
73
|
+
*
|
|
74
|
+
* @param lower The key to sort after, or `null` for no lower bound.
|
|
75
|
+
* @param upper The key to sort before, or `null` for no upper bound.
|
|
76
|
+
* @param count How many keys, a non-negative safe integer.
|
|
77
|
+
* @throws {FractionalKeyError} If a bound is not a key under the alphabet.
|
|
78
|
+
* @throws {RangeError} If `lower` does not sort before `upper`, or `count` is
|
|
79
|
+
* not a non-negative safe integer.
|
|
80
|
+
*/
|
|
81
|
+
export declare function generateFractionalKeysBetween(lower: string | null, upper: string | null, count: number, options?: FractionalKeyOptions): string[];
|
|
82
|
+
//# 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,EAEN,KAAK,qBAAqB,EAC1B,MAAM,8BAA8B,CAAC;AAEtC,+CAA+C;AAC/C,MAAM,MAAM,oBAAoB,GAAG;IAClC,sDAAsD;IACtD,QAAQ,CAAC,QAAQ,CAAC,EAAE,qBAAqB,CAAC;CAC1C,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAC9B,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,oBAAoB,GAC5B,OAAO,CAKT;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAClC,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,oBAAoB,GAC5B,IAAI,CAEN;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,4BAA4B,CAC3C,KAAK,EAAE,MAAM,GAAG,IAAI,EACpB,KAAK,EAAE,MAAM,GAAG,IAAI,EACpB,OAAO,CAAC,EAAE,oBAAoB,GAC5B,MAAM,CA4BR;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,6BAA6B,CAC5C,KAAK,EAAE,MAAM,GAAG,IAAI,EACpB,KAAK,EAAE,MAAM,GAAG,IAAI,EACpB,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,oBAAoB,GAC5B,MAAM,EAAE,CAoEV"}
|