@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,199 @@
|
|
|
1
|
+
import { assertInteger } from "@ac-kit/core";
|
|
2
|
+
import { checkKey_, findKeyError_, fractionBetween_, fractionsBetween_, incrementInteger_, integerEnd_, keyAfter_, keyBefore_, zeroInteger_, } from "./_fractional-key.js";
|
|
3
|
+
import { BASE62_FRACTIONAL_KEY_ALPHABET, } from "./fractional-key-alphabet.js";
|
|
4
|
+
/**
|
|
5
|
+
* Whether `key` is a well-formed fractional key under the alphabet.
|
|
6
|
+
*
|
|
7
|
+
* A key is a head digit, the integer digits its head calls for, then a fraction
|
|
8
|
+
* that never ends in the zero digit; the smallest integer part (head and digits
|
|
9
|
+
* all zero) needs a fraction. See {@link generateFractionalKeyBetween}.
|
|
10
|
+
*
|
|
11
|
+
* Complexity: `O(|key|)`.
|
|
12
|
+
*/
|
|
13
|
+
export function isFractionalKey(key, options) {
|
|
14
|
+
return (findKeyError_(key, options?.alphabet ?? BASE62_FRACTIONAL_KEY_ALPHABET) ===
|
|
15
|
+
null);
|
|
16
|
+
}
|
|
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 function assertFractionalKey(key, options) {
|
|
26
|
+
checkKey_(key, options?.alphabet ?? BASE62_FRACTIONAL_KEY_ALPHABET);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A key that sorts strictly between `lower` and `upper` under plain string
|
|
30
|
+
* comparison — fractional indexing, so that a user-ordered list inserts or
|
|
31
|
+
* moves one item by writing one key, with nothing renumbered.
|
|
32
|
+
*
|
|
33
|
+
* `null` is an open end: `(null, null)` is the first key, `(last, null)`
|
|
34
|
+
* appends and `(null, first)` prepends. Keys have an integer part and a
|
|
35
|
+
* fraction, and:
|
|
36
|
+
*
|
|
37
|
+
* - Appending or prepending steps the integer part, so `n` appends from one key
|
|
38
|
+
* grow it by `⌈log_base n⌉` digits at most (base 62: two digits hold 3 844
|
|
39
|
+
* appends, five hold 916 million). Past the last integer, which base 62
|
|
40
|
+
* reaches after `62^31` appends, keys keep growing in the fraction;
|
|
41
|
+
* - Between two keys, the result is the shortest key strictly between them — the
|
|
42
|
+
* next integer when there is room, otherwise the middle fraction digit — so
|
|
43
|
+
* it is never more than one code unit longer than the longer bound. Inserting
|
|
44
|
+
* again and again into the same gap halves it each time, adding a digit every
|
|
45
|
+
* `log₂ base` insertions (about 6 in base 62): spread a batch with
|
|
46
|
+
* {@link generateFractionalKeysBetween} instead.
|
|
47
|
+
*
|
|
48
|
+
* The result is deterministic: two replicas inserting into the same gap get the
|
|
49
|
+
* same key, which ties under string order. Break the tie by a second sort field
|
|
50
|
+
* (a record id), and generate a later key between the tied pair's neighbours.
|
|
51
|
+
*
|
|
52
|
+
* Complexity: `O(|lower| + |upper|)`.
|
|
53
|
+
*
|
|
54
|
+
* @param lower The key to sort after, or `null` for no lower bound.
|
|
55
|
+
* @param upper The key to sort before, or `null` for no upper bound.
|
|
56
|
+
* @throws {FractionalKeyError} If a bound is not a key under the alphabet.
|
|
57
|
+
* @throws {RangeError} If `lower` does not sort before `upper`.
|
|
58
|
+
*/
|
|
59
|
+
export function generateFractionalKeyBetween(lower, upper, options) {
|
|
60
|
+
const alphabet = options?.alphabet ?? BASE62_FRACTIONAL_KEY_ALPHABET;
|
|
61
|
+
const lowerEnd = lower === null ? 0 : checkKey_(lower, alphabet);
|
|
62
|
+
const upperEnd = upper === null ? 0 : checkKey_(upper, alphabet);
|
|
63
|
+
if (lower === null) {
|
|
64
|
+
return upper === null
|
|
65
|
+
? zeroInteger_(alphabet)
|
|
66
|
+
: keyBefore_(upper, upperEnd, alphabet);
|
|
67
|
+
}
|
|
68
|
+
if (upper === null) {
|
|
69
|
+
return keyAfter_(lower, lowerEnd, alphabet);
|
|
70
|
+
}
|
|
71
|
+
checkOrder_(lower, upper);
|
|
72
|
+
const lowerInteger = lower.slice(0, lowerEnd);
|
|
73
|
+
if (lowerInteger === upper.slice(0, upperEnd)) {
|
|
74
|
+
return (lowerInteger +
|
|
75
|
+
fractionBetween_(lower.slice(lowerEnd), upper.slice(upperEnd), alphabet));
|
|
76
|
+
}
|
|
77
|
+
// The integers differ, so `lowerInteger` has a successor.
|
|
78
|
+
const next = incrementInteger_(lowerInteger, alphabet);
|
|
79
|
+
return next < upper
|
|
80
|
+
? next
|
|
81
|
+
: lowerInteger + fractionBetween_(lower.slice(lowerEnd), null, alphabet);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* `count` ascending keys strictly between `lower` and `upper` (`null` for an
|
|
85
|
+
* open end), for inserting a batch at one place.
|
|
86
|
+
*
|
|
87
|
+
* With an open end, the keys are consecutive appends (or prepends), as repeated
|
|
88
|
+
* {@link generateFractionalKeyBetween} calls would make them. Between two keys,
|
|
89
|
+
* they take the free integers first; the rest are fractions that divide each
|
|
90
|
+
* remaining gap evenly, at the coarsest precision holding them, so every key is
|
|
91
|
+
* at most `⌈log_base(count + 1)⌉` code units longer than the longer bound.
|
|
92
|
+
* `count = 1` gives the key `generateFractionalKeyBetween` gives. Regenerating
|
|
93
|
+
* a run of keys between its neighbours this way also shortens a run that grew
|
|
94
|
+
* long from many insertions into one gap.
|
|
95
|
+
*
|
|
96
|
+
* Complexity: `O(count · L)` for keys of length `L`, plus `O(w²)` once for a
|
|
97
|
+
* window of `w` digits where the bounds differ.
|
|
98
|
+
*
|
|
99
|
+
* @param lower The key to sort after, or `null` for no lower bound.
|
|
100
|
+
* @param upper The key to sort before, or `null` for no upper bound.
|
|
101
|
+
* @param count How many keys, a non-negative safe integer.
|
|
102
|
+
* @throws {FractionalKeyError} If a bound is not a key under the alphabet.
|
|
103
|
+
* @throws {RangeError} If `lower` does not sort before `upper`, or `count` is
|
|
104
|
+
* not a non-negative safe integer.
|
|
105
|
+
*/
|
|
106
|
+
export function generateFractionalKeysBetween(lower, upper, count, options) {
|
|
107
|
+
const alphabet = options?.alphabet ?? BASE62_FRACTIONAL_KEY_ALPHABET;
|
|
108
|
+
assertInteger(count, "count", 0);
|
|
109
|
+
const lowerEnd = lower === null ? 0 : checkKey_(lower, alphabet);
|
|
110
|
+
const upperEnd = upper === null ? 0 : checkKey_(upper, alphabet);
|
|
111
|
+
if (count === 0) {
|
|
112
|
+
if (lower !== null && upper !== null) {
|
|
113
|
+
checkOrder_(lower, upper);
|
|
114
|
+
}
|
|
115
|
+
return [];
|
|
116
|
+
}
|
|
117
|
+
if (lower === null || upper === null) {
|
|
118
|
+
return openEndedKeys_(lower, lowerEnd, upper, upperEnd, count, alphabet);
|
|
119
|
+
}
|
|
120
|
+
checkOrder_(lower, upper);
|
|
121
|
+
const lowerInteger = lower.slice(0, lowerEnd);
|
|
122
|
+
const upperInteger = upper.slice(0, upperEnd);
|
|
123
|
+
const lowerFraction = lower.slice(lowerEnd);
|
|
124
|
+
if (lowerInteger === upperInteger) {
|
|
125
|
+
return prefixAll_(lowerInteger, fractionsBetween_(lowerFraction, upper.slice(upperEnd), count, alphabet));
|
|
126
|
+
}
|
|
127
|
+
// The integers strictly between the bounds, as many as are wanted; the last
|
|
128
|
+
// integer has no successor, but can be `upper`'s own.
|
|
129
|
+
const integers = [];
|
|
130
|
+
let next = incrementInteger_(lowerInteger, alphabet);
|
|
131
|
+
while (integers.length < count && next !== null && next < upper) {
|
|
132
|
+
integers.push(next);
|
|
133
|
+
next = incrementInteger_(next, alphabet);
|
|
134
|
+
}
|
|
135
|
+
if (integers.length === count) {
|
|
136
|
+
return integers;
|
|
137
|
+
}
|
|
138
|
+
// Fewer integers than keys: share the rest evenly between the gaps around
|
|
139
|
+
// them. The last integer may be `upper`'s own, below `upper`'s fraction.
|
|
140
|
+
const gapCount = integers.length + 1;
|
|
141
|
+
const share = Math.floor((count - integers.length) / gapCount);
|
|
142
|
+
const extra = (count - integers.length) % gapCount;
|
|
143
|
+
const keys = [];
|
|
144
|
+
for (let gap = 0; gap < gapCount; gap++) {
|
|
145
|
+
if (gap > 0) {
|
|
146
|
+
keys.push(integers[gap - 1]);
|
|
147
|
+
}
|
|
148
|
+
const keysInGap = share +
|
|
149
|
+
Math.floor(((gap + 1) * extra) / gapCount) -
|
|
150
|
+
Math.floor((gap * extra) / gapCount);
|
|
151
|
+
if (keysInGap === 0) {
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
const integer = gap === 0 ? lowerInteger : integers[gap - 1];
|
|
155
|
+
const fractions = fractionsBetween_(gap === 0 ? lowerFraction : "", integer === upperInteger ? upper.slice(upperEnd) : null, keysInGap, alphabet);
|
|
156
|
+
for (const fraction of fractions) {
|
|
157
|
+
keys.push(integer + fraction);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return keys;
|
|
161
|
+
}
|
|
162
|
+
function checkOrder_(lower, upper) {
|
|
163
|
+
if (!(lower < upper)) {
|
|
164
|
+
throw new RangeError(`The lower key must sort before the upper key, got ${JSON.stringify(lower)} and ${JSON.stringify(upper)}`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
function openEndedKeys_(lower, lowerEnd, upper, upperEnd, count, alphabet) {
|
|
168
|
+
const keys = new Array(count);
|
|
169
|
+
if (upper === null) {
|
|
170
|
+
let key = lower ?? zeroInteger_(alphabet);
|
|
171
|
+
let first = 0;
|
|
172
|
+
if (lower === null) {
|
|
173
|
+
keys[0] = key;
|
|
174
|
+
first = 1;
|
|
175
|
+
}
|
|
176
|
+
let end = lower === null ? integerEnd_(key, alphabet) : lowerEnd;
|
|
177
|
+
for (let index = first; index < count; index++) {
|
|
178
|
+
key = keyAfter_(key, end, alphabet);
|
|
179
|
+
end = integerEnd_(key, alphabet);
|
|
180
|
+
keys[index] = key;
|
|
181
|
+
}
|
|
182
|
+
return keys;
|
|
183
|
+
}
|
|
184
|
+
let key = upper;
|
|
185
|
+
let end = upperEnd;
|
|
186
|
+
for (let index = count - 1; index >= 0; index--) {
|
|
187
|
+
key = keyBefore_(key, end, alphabet);
|
|
188
|
+
end = integerEnd_(key, alphabet);
|
|
189
|
+
keys[index] = key;
|
|
190
|
+
}
|
|
191
|
+
return keys;
|
|
192
|
+
}
|
|
193
|
+
function prefixAll_(prefix, fractions) {
|
|
194
|
+
for (let index = 0; index < fractions.length; index++) {
|
|
195
|
+
fractions[index] = prefix + fractions[index];
|
|
196
|
+
}
|
|
197
|
+
return fractions;
|
|
198
|
+
}
|
|
199
|
+
//# sourceMappingURL=fractional-key.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fractional-key.js","sourceRoot":"","sources":["../../src/ordered/fractional-key.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,OAAO,EACN,SAAS,EACT,aAAa,EACb,gBAAgB,EAChB,iBAAiB,EACjB,iBAAiB,EACjB,WAAW,EACX,SAAS,EACT,UAAU,EACV,YAAY,GACZ,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACN,8BAA8B,GAE9B,MAAM,8BAA8B,CAAC;AAQtC;;;;;;;;GAQG;AACH,MAAM,UAAU,eAAe,CAC9B,GAAW,E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,KAAK,IAAI,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC;QACjD,GAAG,GAAG,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;QACrC,GAAG,GAAG,WAAW,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;QACjC,IAAI,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC;IACnB,CAAC;IACD,OAAO,IAAI,CAAC;AACb,CAAC;AAED,SAAS,UAAU,CAAC,MAAc,EAAE,SAAmB;IACtD,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,SAAS,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QACvD,SAAS,CAAC,KAAK,CAAC,GAAG,MAAM,GAAG,SAAS,CAAC,KAAK,CAAE,CAAC;IAC/C,CAAC;IACD,OAAO,SAAS,CAAC;AAClB,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"}
|
|
@@ -14,7 +14,7 @@ export function quickselect(items, k, compare) {
|
|
|
14
14
|
let left = 0;
|
|
15
15
|
let right = items.length - 1;
|
|
16
16
|
while (left < right) {
|
|
17
|
-
const pivot = items[
|
|
17
|
+
const pivot = items[left + Math.floor((right - left) / 2)];
|
|
18
18
|
let lower = left;
|
|
19
19
|
let upper = right;
|
|
20
20
|
while (lower <= upper) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"quickselect.js","sourceRoot":"","sources":["../../src/select/quickselect.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,WAAW,CAC1B,KAAU,EACV,CAAS,EACT,OAAsB;IAEtB,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,IAAI,KAAK,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;IAE7B,OAAO,IAAI,GAAG,KAAK,EAAE,CAAC;QACrB,MAAM,KAAK,GAAG,KAAK,CAAC,
|
|
1
|
+
{"version":3,"file":"quickselect.js","sourceRoot":"","sources":["../../src/select/quickselect.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,WAAW,CAC1B,KAAU,EACV,CAAS,EACT,OAAsB;IAEtB,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,IAAI,KAAK,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;IAE7B,OAAO,IAAI,GAAG,KAAK,EAAE,CAAC;QACrB,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAE,CAAC;QAC5D,IAAI,KAAK,GAAG,IAAI,CAAC;QACjB,IAAI,KAAK,GAAG,KAAK,CAAC;QAElB,OAAO,KAAK,IAAI,KAAK,EAAE,CAAC;YACvB,OAAO,OAAO,CAAC,KAAK,CAAC,KAAK,CAAE,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC1C,KAAK,EAAE,CAAC;YACT,CAAC;YACD,OAAO,OAAO,CAAC,KAAK,CAAC,KAAK,CAAE,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC1C,KAAK,EAAE,CAAC;YACT,CAAC;YACD,IAAI,KAAK,IAAI,KAAK,EAAE,CAAC;gBACpB,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAE,CAAC;gBAC3B,KAAK,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,KAAK,CAAE,CAAC;gBAC7B,KAAK,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;gBACpB,KAAK,EAAE,CAAC;gBACR,KAAK,EAAE,CAAC;YACT,CAAC;QACF,CAAC;QAED,IAAI,CAAC,IAAI,KAAK,EAAE,CAAC;YAChB,KAAK,GAAG,KAAK,CAAC;QACf,CAAC;aAAM,IAAI,CAAC,IAAI,KAAK,EAAE,CAAC;YACvB,IAAI,GAAG,KAAK,CAAC;QACd,CAAC;aAAM,CAAC;YACP,MAAM;QACP,CAAC;IACF,CAAC;IAED,OAAO,KAAK,CAAC,CAAC,CAAE,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/** The key arrays `RadixIndexSorter` orders by. */
|
|
2
|
+
export type RadixSortKeys = Float32Array | Float64Array | Int32Array | Uint32Array;
|
|
3
|
+
/**
|
|
4
|
+
* Sorts the positions of typed-array keys — an argsort, `keys[out[0]]` the
|
|
5
|
+
* first key — with a least-significant-digit radix sort over the keys' bits:
|
|
6
|
+
* 11-bit digits, three passes for a 32-bit key and six for a `Float64Array`,
|
|
7
|
+
* every histogram counted in one read of the keys, and a pass whose digit is
|
|
8
|
+
* the same for every key skipped (Terdiman, "Radix Sort Revisited", 2000). O(n)
|
|
9
|
+
* per pass whatever the keys, never a comparator: the sort of the depth of each
|
|
10
|
+
* triangle or splat that a renderer redoes on every camera move.
|
|
11
|
+
*
|
|
12
|
+
* Stable in both orders: keys that compare equal keep the order of their
|
|
13
|
+
* positions. Floating-point keys are ordered as numbers, `-0` equal to `0`, and
|
|
14
|
+
* every `NaN` comes last in either order.
|
|
15
|
+
*
|
|
16
|
+
* The sorter owns its scratch: a sort allocates only when `count` exceeds every
|
|
17
|
+
* count it sorted before (about 12 bytes a key, plus 48 KiB of histograms at
|
|
18
|
+
* the first sort), so a sorter kept across frames sorts with no allocation. Not
|
|
19
|
+
* re-entrant: one sort at a time per sorter.
|
|
20
|
+
*/
|
|
21
|
+
export declare class RadixIndexSorter {
|
|
22
|
+
private capacity_;
|
|
23
|
+
private low_;
|
|
24
|
+
private high_;
|
|
25
|
+
private work_;
|
|
26
|
+
private histograms_;
|
|
27
|
+
/** Holds one key at a time, to read its bits without a view per array. */
|
|
28
|
+
private readonly f64_;
|
|
29
|
+
private readonly f64Words_;
|
|
30
|
+
private readonly f32_;
|
|
31
|
+
private readonly f32Word_;
|
|
32
|
+
/**
|
|
33
|
+
* Writes into `out[0 … count)` the positions `0 … count − 1` ordered by
|
|
34
|
+
* `keys[position]`, ascending or descending.
|
|
35
|
+
*
|
|
36
|
+
* @param out Receives the positions; only its first `count` entries are
|
|
37
|
+
* written.
|
|
38
|
+
* @param keys Read, not modified; must not share memory with `out`.
|
|
39
|
+
* @param order Default `"ascending"`.
|
|
40
|
+
* @param count The keys to sort, from the first. Default `keys.length`.
|
|
41
|
+
* @returns `out`.
|
|
42
|
+
* @throws {TypeError} When `keys` is not one of the four typed arrays, from
|
|
43
|
+
* any realm.
|
|
44
|
+
* @throws {RangeError} Unless `count` is an integer in `[0, keys.length]`,
|
|
45
|
+
* `out` holds at least `count` entries and shares no memory with `keys`,
|
|
46
|
+
* and `order` is one of the two.
|
|
47
|
+
*/
|
|
48
|
+
sort(out: Uint32Array, keys: RadixSortKeys, order?: "ascending" | "descending", count?: number): Uint32Array;
|
|
49
|
+
private reserve_;
|
|
50
|
+
/**
|
|
51
|
+
* The keys' bits, mapped so that unsigned order is the keys' order: the sign
|
|
52
|
+
* flipped for integers and positive floats, every bit for negative floats,
|
|
53
|
+
* complemented again for a descending sort, `NaN` last.
|
|
54
|
+
*/
|
|
55
|
+
private loadKeys_;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=radix-index-sorter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"radix-index-sorter.d.ts","sourceRoot":"","sources":["../../src/sort/radix-index-sorter.ts"],"names":[],"mappings":"AAEA,mDAAmD;AACnD,MAAM,MAAM,aAAa,GACtB,YAAY,GACZ,YAAY,GACZ,UAAU,GACV,WAAW,CAAC;AAef;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,gBAAgB;IAC5B,OAAO,CAAC,SAAS,CAAK;IACtB,OAAO,CAAC,IAAI,CAAsB;IAClC,OAAO,CAAC,KAAK,CAAsB;IACnC,OAAO,CAAC,KAAK,CAAsB;IACnC,OAAO,CAAC,WAAW,CAAsB;IACzC,0EAA0E;IAC1E,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAuB;IAC5C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAqC;IAC/D,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAuB;IAC5C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAqC;IAE9D;;;;;;;;;;;;;;;OAeG;IACH,IAAI,CACH,GAAG,EAAE,WAAW,EAChB,IAAI,EAAE,aAAa,EACnB,KAAK,GAAE,WAAW,GAAG,YAA0B,EAC/C,KAAK,GAAE,MAAoB,GACzB,WAAW,CAwFb;IAED,OAAO,CAAC,QAAQ;IAahB;;;;OAIG;IACH,OAAO,CAAC,SAAS;CA0DjB"}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { assertInteger, typedArrayName, viewsMayOverlap } from "@ac-kit/core";
|
|
2
|
+
const DIGIT_BITS_ = 11;
|
|
3
|
+
const BUCKETS_ = 1 << DIGIT_BITS_;
|
|
4
|
+
/** Two 32-bit words of a 64-bit key, three digits each: 11, 11 and 10 bits. */
|
|
5
|
+
const MAX_PASSES_ = 6;
|
|
6
|
+
const SIGN_ = 0x80000000;
|
|
7
|
+
const LITTLE_ENDIAN_ = new Uint8Array(Uint32Array.of(1).buffer)[0] === 1;
|
|
8
|
+
/**
|
|
9
|
+
* Sorts the positions of typed-array keys — an argsort, `keys[out[0]]` the
|
|
10
|
+
* first key — with a least-significant-digit radix sort over the keys' bits:
|
|
11
|
+
* 11-bit digits, three passes for a 32-bit key and six for a `Float64Array`,
|
|
12
|
+
* every histogram counted in one read of the keys, and a pass whose digit is
|
|
13
|
+
* the same for every key skipped (Terdiman, "Radix Sort Revisited", 2000). O(n)
|
|
14
|
+
* per pass whatever the keys, never a comparator: the sort of the depth of each
|
|
15
|
+
* triangle or splat that a renderer redoes on every camera move.
|
|
16
|
+
*
|
|
17
|
+
* Stable in both orders: keys that compare equal keep the order of their
|
|
18
|
+
* positions. Floating-point keys are ordered as numbers, `-0` equal to `0`, and
|
|
19
|
+
* every `NaN` comes last in either order.
|
|
20
|
+
*
|
|
21
|
+
* The sorter owns its scratch: a sort allocates only when `count` exceeds every
|
|
22
|
+
* count it sorted before (about 12 bytes a key, plus 48 KiB of histograms at
|
|
23
|
+
* the first sort), so a sorter kept across frames sorts with no allocation. Not
|
|
24
|
+
* re-entrant: one sort at a time per sorter.
|
|
25
|
+
*/
|
|
26
|
+
export class RadixIndexSorter {
|
|
27
|
+
capacity_ = 0;
|
|
28
|
+
low_ = new Uint32Array(0);
|
|
29
|
+
high_ = new Uint32Array(0);
|
|
30
|
+
work_ = new Uint32Array(0);
|
|
31
|
+
histograms_ = new Uint32Array(0);
|
|
32
|
+
/** Holds one key at a time, to read its bits without a view per array. */
|
|
33
|
+
f64_ = new Float64Array(1);
|
|
34
|
+
f64Words_ = new Uint32Array(this.f64_.buffer);
|
|
35
|
+
f32_ = new Float32Array(1);
|
|
36
|
+
f32Word_ = new Uint32Array(this.f32_.buffer);
|
|
37
|
+
/**
|
|
38
|
+
* Writes into `out[0 … count)` the positions `0 … count − 1` ordered by
|
|
39
|
+
* `keys[position]`, ascending or descending.
|
|
40
|
+
*
|
|
41
|
+
* @param out Receives the positions; only its first `count` entries are
|
|
42
|
+
* written.
|
|
43
|
+
* @param keys Read, not modified; must not share memory with `out`.
|
|
44
|
+
* @param order Default `"ascending"`.
|
|
45
|
+
* @param count The keys to sort, from the first. Default `keys.length`.
|
|
46
|
+
* @returns `out`.
|
|
47
|
+
* @throws {TypeError} When `keys` is not one of the four typed arrays, from
|
|
48
|
+
* any realm.
|
|
49
|
+
* @throws {RangeError} Unless `count` is an integer in `[0, keys.length]`,
|
|
50
|
+
* `out` holds at least `count` entries and shares no memory with `keys`,
|
|
51
|
+
* and `order` is one of the two.
|
|
52
|
+
*/
|
|
53
|
+
sort(out, keys, order = "ascending", count = keys.length) {
|
|
54
|
+
assertInteger(count, "RadixIndexSorter.sort: count", 0, keys.length);
|
|
55
|
+
if (out.length < count) {
|
|
56
|
+
throw new RangeError(`RadixIndexSorter.sort: out holds ${out.length} entries, ${count} needed`);
|
|
57
|
+
}
|
|
58
|
+
// the internal slot, not `instanceof`: keys from another realm too
|
|
59
|
+
const kind = typedArrayName(keys);
|
|
60
|
+
if (kind !== "Float64Array" &&
|
|
61
|
+
kind !== "Float32Array" &&
|
|
62
|
+
kind !== "Int32Array" &&
|
|
63
|
+
kind !== "Uint32Array") {
|
|
64
|
+
throw new TypeError(`RadixIndexSorter.sort: keys is a Float32Array, Float64Array, Int32Array or Uint32Array, got ${kind ?? typeof keys}`);
|
|
65
|
+
}
|
|
66
|
+
if (order !== "ascending" && order !== "descending") {
|
|
67
|
+
throw new RangeError(`RadixIndexSorter.sort: order is "ascending" or "descending", got ${String(order)}`);
|
|
68
|
+
}
|
|
69
|
+
if (count > 0 && viewsMayOverlap(out, keys)) {
|
|
70
|
+
throw new RangeError("RadixIndexSorter.sort: out and keys must not share memory");
|
|
71
|
+
}
|
|
72
|
+
this.reserve_(count);
|
|
73
|
+
const wide = kind === "Float64Array";
|
|
74
|
+
this.loadKeys_(keys, kind, count, order === "descending");
|
|
75
|
+
const passes = wide ? MAX_PASSES_ : 3;
|
|
76
|
+
const histograms = this.histograms_;
|
|
77
|
+
histograms.fill(0, 0, passes * BUCKETS_);
|
|
78
|
+
const low = this.low_;
|
|
79
|
+
const high = this.high_;
|
|
80
|
+
for (let index = 0; index < count; index++) {
|
|
81
|
+
const word = low[index];
|
|
82
|
+
histograms[word & 0x7ff]++;
|
|
83
|
+
histograms[BUCKETS_ + ((word >>> 11) & 0x7ff)]++;
|
|
84
|
+
histograms[2 * BUCKETS_ + (word >>> 22)]++;
|
|
85
|
+
if (wide) {
|
|
86
|
+
const upper = high[index];
|
|
87
|
+
histograms[3 * BUCKETS_ + (upper & 0x7ff)]++;
|
|
88
|
+
histograms[4 * BUCKETS_ + ((upper >>> 11) & 0x7ff)]++;
|
|
89
|
+
histograms[5 * BUCKETS_ + (upper >>> 22)]++;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
for (let index = 0; index < count; index++) {
|
|
93
|
+
out[index] = index;
|
|
94
|
+
}
|
|
95
|
+
let source = out;
|
|
96
|
+
let target = this.work_;
|
|
97
|
+
for (let pass = 0; pass < passes; pass++) {
|
|
98
|
+
const base = pass * BUCKETS_;
|
|
99
|
+
const words = pass < 3 ? low : high;
|
|
100
|
+
const shift = (pass % 3) * DIGIT_BITS_;
|
|
101
|
+
const mask = pass % 3 === 2 ? 0x3ff : 0x7ff;
|
|
102
|
+
// one digit for every key: the pass would move nothing
|
|
103
|
+
if (count === 0 ||
|
|
104
|
+
histograms[base + ((words[0] >>> shift) & mask)] === count) {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
let total = 0;
|
|
108
|
+
for (let bucket = 0; bucket < BUCKETS_; bucket++) {
|
|
109
|
+
const size = histograms[base + bucket];
|
|
110
|
+
histograms[base + bucket] = total;
|
|
111
|
+
total += size;
|
|
112
|
+
}
|
|
113
|
+
for (let index = 0; index < count; index++) {
|
|
114
|
+
const position = source[index];
|
|
115
|
+
const bucket = base + ((words[position] >>> shift) & mask);
|
|
116
|
+
target[histograms[bucket]++] = position;
|
|
117
|
+
}
|
|
118
|
+
const swap = source;
|
|
119
|
+
source = target;
|
|
120
|
+
target = swap;
|
|
121
|
+
}
|
|
122
|
+
if (source !== out) {
|
|
123
|
+
// a loop, not `set(subarray)`: a view per sort would allocate
|
|
124
|
+
for (let index = 0; index < count; index++) {
|
|
125
|
+
out[index] = source[index];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return out;
|
|
129
|
+
}
|
|
130
|
+
reserve_(count) {
|
|
131
|
+
if (this.histograms_.length === 0) {
|
|
132
|
+
this.histograms_ = new Uint32Array(MAX_PASSES_ * BUCKETS_);
|
|
133
|
+
}
|
|
134
|
+
if (count <= this.capacity_) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
this.capacity_ = count;
|
|
138
|
+
this.low_ = new Uint32Array(count);
|
|
139
|
+
this.high_ = new Uint32Array(count);
|
|
140
|
+
this.work_ = new Uint32Array(count);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* The keys' bits, mapped so that unsigned order is the keys' order: the sign
|
|
144
|
+
* flipped for integers and positive floats, every bit for negative floats,
|
|
145
|
+
* complemented again for a descending sort, `NaN` last.
|
|
146
|
+
*/
|
|
147
|
+
loadKeys_(keys, kind, count, descending) {
|
|
148
|
+
const low = this.low_;
|
|
149
|
+
const high = this.high_;
|
|
150
|
+
const flip = descending ? 0xffffffff : 0;
|
|
151
|
+
if (kind === "Float64Array") {
|
|
152
|
+
const value = this.f64_;
|
|
153
|
+
const words = this.f64Words_;
|
|
154
|
+
const lowWord = LITTLE_ENDIAN_ ? 0 : 1;
|
|
155
|
+
for (let index = 0; index < count; index++) {
|
|
156
|
+
const key = keys[index];
|
|
157
|
+
// -0 as 0: equal keys keep their order
|
|
158
|
+
value[0] = key === 0 ? 0 : key;
|
|
159
|
+
let upper = words[1 - lowWord];
|
|
160
|
+
let lower = words[lowWord];
|
|
161
|
+
if (key !== key) {
|
|
162
|
+
upper = 0xffffffff;
|
|
163
|
+
lower = 0xffffffff;
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
if ((upper & SIGN_) !== 0) {
|
|
167
|
+
upper = ~upper;
|
|
168
|
+
lower = ~lower;
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
upper |= SIGN_;
|
|
172
|
+
}
|
|
173
|
+
upper ^= flip;
|
|
174
|
+
lower ^= flip;
|
|
175
|
+
}
|
|
176
|
+
high[index] = upper >>> 0;
|
|
177
|
+
low[index] = lower >>> 0;
|
|
178
|
+
}
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (kind === "Float32Array") {
|
|
182
|
+
const value = this.f32_;
|
|
183
|
+
const word = this.f32Word_;
|
|
184
|
+
for (let index = 0; index < count; index++) {
|
|
185
|
+
const key = keys[index];
|
|
186
|
+
value[0] = key === 0 ? 0 : key;
|
|
187
|
+
let bits = word[0];
|
|
188
|
+
if (key !== key) {
|
|
189
|
+
bits = 0xffffffff;
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
bits = ((bits & SIGN_) !== 0 ? ~bits : bits | SIGN_) ^ flip;
|
|
193
|
+
}
|
|
194
|
+
low[index] = bits >>> 0;
|
|
195
|
+
}
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const signed = kind === "Int32Array" ? SIGN_ : 0;
|
|
199
|
+
for (let index = 0; index < count; index++) {
|
|
200
|
+
low[index] = (keys[index] ^ signed ^ flip) >>> 0;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
//# sourceMappingURL=radix-index-sorter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"radix-index-sorter.js","sourceRoot":"","sources":["../../src/sort/radix-index-sorter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,c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,CAAC,CAAC,GAAG,CAAC;gBAC/B,IAAI,IAAI,GAAG,IAAI,CAAC,CAAC,CAAE,CAAC;gBACpB,IAAI,GAAG,KAAK,GAAG,EAAE,CAAC;oBACjB,IAAI,GAAG,UAAU,CAAC;gBACnB,CAAC;qBAAM,CAAC;oBACP,IAAI,GAAG,CAAC,CAAC,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,GAAG,KAAK,CAAC,GAAG,IAAI,CAAC;gBAC7D,CAAC;gBACD,GAAG,CAAC,KAAK,CAAC,GAAG,IAAI,KAAK,CAAC,CAAC;YACzB,CAAC;YACD,OAAO;QACR,CAAC;QACD,MAAM,MAAM,GAAG,IAAI,KAAK,YAAY,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QACjD,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC;YAC5C,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAE,GAAG,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC;QACnD,CAAC;IACF,CAAC;CACD"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Comparator } from "@ac-kit/core";
|
|
2
|
+
/** An array or typed array whose elements can be written. */
|
|
3
|
+
export type WritableArrayLike<T> = {
|
|
4
|
+
[index: number]: T;
|
|
5
|
+
readonly length: number;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Sorts `items[from … to)` in place by `compare`, leaving the rest of `items`
|
|
9
|
+
* untouched: a typed array of row indices ordered by the cells they point at,
|
|
10
|
+
* one group's slice of a scratch buffer, without the view `subarray` would
|
|
11
|
+
* allocate or the work arrays `TypedArray.prototype.sort(compare)` allocates.
|
|
12
|
+
*
|
|
13
|
+
* Introsort (Musser, 1997): quicksort on the median of three, heapsort past 2 ·
|
|
14
|
+
* ⌊log₂ n⌋ levels, insertion sort below 16 elements. Not stable. O(n log n)
|
|
15
|
+
* comparisons in the worst case, O(log n) stack, no allocation.
|
|
16
|
+
*
|
|
17
|
+
* A comparator that is not a strict weak order leaves the range in some
|
|
18
|
+
* permutation of its elements.
|
|
19
|
+
*
|
|
20
|
+
* @param items Sorted in place within the range.
|
|
21
|
+
* @param compare A strict weak ordering of the items.
|
|
22
|
+
* @param from First position. Default `0`.
|
|
23
|
+
* @param to End position, excluded. Default `items.length`.
|
|
24
|
+
* @throws {RangeError} When `from` and `to` are not integers with `0 ≤ from ≤
|
|
25
|
+
* to ≤ items.length`.
|
|
26
|
+
* @see {@link sortNumbersRange} for numbers, without a comparator.
|
|
27
|
+
*/
|
|
28
|
+
export declare function sortRange<T>(items: WritableArrayLike<T>, compare: Comparator<T>, from?: number, to?: number): void;
|
|
29
|
+
/**
|
|
30
|
+
* Sorts the numbers `items[from … to)` in place, in the order
|
|
31
|
+
* `Float64Array.prototype.sort()` gives — ascending, `−0` before `+0`, `NaN`
|
|
32
|
+
* last — leaving the rest of `items` untouched and allocating nothing: one
|
|
33
|
+
* group's slice of a scratch buffer, without the view `subarray` allocates.
|
|
34
|
+
*
|
|
35
|
+
* Introsort over `<` (see {@link sortRange}), after one pass that moves the
|
|
36
|
+
* `NaN`s to the end; the zeros, which `<` cannot tell apart, are then signed in
|
|
37
|
+
* O(log n). Not stable, which only shows in `NaN` payloads. O(n log n), O(log
|
|
38
|
+
* n) stack.
|
|
39
|
+
*
|
|
40
|
+
* @param items Sorted in place within the range; a typed array reads without
|
|
41
|
+
* boxing.
|
|
42
|
+
* @param from First position. Default `0`.
|
|
43
|
+
* @param to End position, excluded. Default `items.length`.
|
|
44
|
+
* @throws {RangeError} When `from` and `to` are not integers with `0 ≤ from ≤
|
|
45
|
+
* to ≤ items.length`.
|
|
46
|
+
*/
|
|
47
|
+
export declare function sortNumbersRange(items: WritableArrayLike<number>, from?: number, to?: number): void;
|
|
48
|
+
//# sourceMappingURL=sort-range.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sort-range.d.ts","sourceRoot":"","sources":["../../src/sort/sort-range.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAI/C,6DAA6D;AAC7D,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI;IAClC,CAAC,KAAK,EAAE,MAAM,GAAG,CAAC,CAAC;IACnB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACxB,CAAC;AAoKF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAC1B,KAAK,EAAE,iBAAiB,CAAC,CAAC,CAAC,EAC3B,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,EACtB,IAAI,SAAI,EACR,EAAE,GAAE,MAAqB,GACvB,IAAI,CAGN;AAuHD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,gBAAgB,CAC/B,KAAK,EAAE,iBAAiB,CAAC,MAAM,CAAC,EAChC,IAAI,SAAI,EACR,EAAE,GAAE,MAAqB,GACvB,IAAI,CA8BN"}
|