@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,282 @@
|
|
|
1
|
+
import { bisectRightNumbers } from "../ordered/bisect-right-numbers.js";
|
|
2
|
+
// Below this length a range is finished by insertion sort, which beats any
|
|
3
|
+
// partitioning on a few elements.
|
|
4
|
+
const INSERTION_LENGTH_ = 16;
|
|
5
|
+
function assertRange_(name, length, from, to) {
|
|
6
|
+
if (!Number.isInteger(from) ||
|
|
7
|
+
!Number.isInteger(to) ||
|
|
8
|
+
from < 0 ||
|
|
9
|
+
from > to ||
|
|
10
|
+
to > length) {
|
|
11
|
+
throw new RangeError(`${name}: [${from}, ${to}) is not a range of [0, ${length})`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
// Introsort's budget: past 2 · ⌊log₂ n⌋ levels of partitioning the input is
|
|
15
|
+
// adversarial, and heapsort finishes the range in O(n log n).
|
|
16
|
+
function depthLimit_(length) {
|
|
17
|
+
return 2 * (31 - Math.clz32(length));
|
|
18
|
+
}
|
|
19
|
+
function swap_(items, a, b) {
|
|
20
|
+
const item = items[a];
|
|
21
|
+
items[a] = items[b];
|
|
22
|
+
items[b] = item;
|
|
23
|
+
}
|
|
24
|
+
function insertionSort_(items, compare, from, to) {
|
|
25
|
+
for (let position = from + 1; position < to; position++) {
|
|
26
|
+
const item = items[position];
|
|
27
|
+
let hole = position;
|
|
28
|
+
while (hole > from && compare(items[hole - 1], item) > 0) {
|
|
29
|
+
items[hole] = items[hole - 1];
|
|
30
|
+
hole--;
|
|
31
|
+
}
|
|
32
|
+
items[hole] = item;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function siftDown_(items, compare, from, root, size) {
|
|
36
|
+
let parent = root;
|
|
37
|
+
for (;;) {
|
|
38
|
+
let child = 2 * parent + 1;
|
|
39
|
+
if (child >= size) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (child + 1 < size &&
|
|
43
|
+
compare(items[from + child], items[from + child + 1]) < 0) {
|
|
44
|
+
child++;
|
|
45
|
+
}
|
|
46
|
+
if (compare(items[from + parent], items[from + child]) >= 0) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
swap_(items, from + parent, from + child);
|
|
50
|
+
parent = child;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function heapSort_(items, compare, from, to) {
|
|
54
|
+
const size = to - from;
|
|
55
|
+
for (let root = (size >> 1) - 1; root >= 0; root--) {
|
|
56
|
+
siftDown_(items, compare, from, root, size);
|
|
57
|
+
}
|
|
58
|
+
for (let end = size - 1; end > 0; end--) {
|
|
59
|
+
swap_(items, from, from + end);
|
|
60
|
+
siftDown_(items, compare, from, 0, end);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// Hoare's partition around the median of the first, middle and last elements,
|
|
64
|
+
// left at the middle: both sides come out non-empty, so every level shrinks the
|
|
65
|
+
// range. The bounds checks only matter to a comparator that is not a strict
|
|
66
|
+
// weak order, which would otherwise run the scans off the range.
|
|
67
|
+
function partition_(items, compare, from, to) {
|
|
68
|
+
const last = to - 1;
|
|
69
|
+
const middle = from + ((last - from) >> 1);
|
|
70
|
+
if (compare(items[middle], items[from]) < 0) {
|
|
71
|
+
swap_(items, middle, from);
|
|
72
|
+
}
|
|
73
|
+
if (compare(items[last], items[middle]) < 0) {
|
|
74
|
+
swap_(items, last, middle);
|
|
75
|
+
if (compare(items[middle], items[from]) < 0) {
|
|
76
|
+
swap_(items, middle, from);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const pivot = items[middle];
|
|
80
|
+
let lower = from - 1;
|
|
81
|
+
let upper = to;
|
|
82
|
+
for (;;) {
|
|
83
|
+
do {
|
|
84
|
+
lower++;
|
|
85
|
+
} while (lower < last && compare(items[lower], pivot) < 0);
|
|
86
|
+
do {
|
|
87
|
+
upper--;
|
|
88
|
+
} while (upper > from && compare(items[upper], pivot) > 0);
|
|
89
|
+
if (lower >= upper) {
|
|
90
|
+
return upper + 1;
|
|
91
|
+
}
|
|
92
|
+
swap_(items, lower, upper);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function introSort_(items, compare, from, to, depth) {
|
|
96
|
+
let start = from;
|
|
97
|
+
let end = to;
|
|
98
|
+
let budget = depth;
|
|
99
|
+
while (end - start > INSERTION_LENGTH_) {
|
|
100
|
+
if (budget === 0) {
|
|
101
|
+
heapSort_(items, compare, start, end);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
budget--;
|
|
105
|
+
const split = partition_(items, compare, start, end);
|
|
106
|
+
// Recursing into the smaller side bounds the stack to O(log n) frames.
|
|
107
|
+
if (split - start < end - split) {
|
|
108
|
+
introSort_(items, compare, start, split, budget);
|
|
109
|
+
start = split;
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
introSort_(items, compare, split, end, budget);
|
|
113
|
+
end = split;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
insertionSort_(items, compare, start, end);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Sorts `items[from … to)` in place by `compare`, leaving the rest of `items`
|
|
120
|
+
* untouched: a typed array of row indices ordered by the cells they point at,
|
|
121
|
+
* one group's slice of a scratch buffer, without the view `subarray` would
|
|
122
|
+
* allocate or the work arrays `TypedArray.prototype.sort(compare)` allocates.
|
|
123
|
+
*
|
|
124
|
+
* Introsort (Musser, 1997): quicksort on the median of three, heapsort past 2 ·
|
|
125
|
+
* ⌊log₂ n⌋ levels, insertion sort below 16 elements. Not stable. O(n log n)
|
|
126
|
+
* comparisons in the worst case, O(log n) stack, no allocation.
|
|
127
|
+
*
|
|
128
|
+
* A comparator that is not a strict weak order leaves the range in some
|
|
129
|
+
* permutation of its elements.
|
|
130
|
+
*
|
|
131
|
+
* @param items Sorted in place within the range.
|
|
132
|
+
* @param compare A strict weak ordering of the items.
|
|
133
|
+
* @param from First position. Default `0`.
|
|
134
|
+
* @param to End position, excluded. Default `items.length`.
|
|
135
|
+
* @throws {RangeError} When `from` and `to` are not integers with `0 ≤ from ≤
|
|
136
|
+
* to ≤ items.length`.
|
|
137
|
+
* @see {@link sortNumbersRange} for numbers, without a comparator.
|
|
138
|
+
*/
|
|
139
|
+
export function sortRange(items, compare, from = 0, to = items.length) {
|
|
140
|
+
assertRange_("sortRange", items.length, from, to);
|
|
141
|
+
introSort_(items, compare, from, to, depthLimit_(to - from));
|
|
142
|
+
}
|
|
143
|
+
// The same introsort over numbers compared with `<`, so no comparator is called
|
|
144
|
+
// and no double crosses a call boxed.
|
|
145
|
+
function insertionSortNumbers_(items, from, to) {
|
|
146
|
+
for (let position = from + 1; position < to; position++) {
|
|
147
|
+
const item = items[position];
|
|
148
|
+
let hole = position;
|
|
149
|
+
while (hole > from && items[hole - 1] > item) {
|
|
150
|
+
items[hole] = items[hole - 1];
|
|
151
|
+
hole--;
|
|
152
|
+
}
|
|
153
|
+
items[hole] = item;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function siftDownNumbers_(items, from, root, size) {
|
|
157
|
+
let parent = root;
|
|
158
|
+
for (;;) {
|
|
159
|
+
let child = 2 * parent + 1;
|
|
160
|
+
if (child >= size) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (child + 1 < size && items[from + child] < items[from + child + 1]) {
|
|
164
|
+
child++;
|
|
165
|
+
}
|
|
166
|
+
if (items[from + parent] >= items[from + child]) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
swap_(items, from + parent, from + child);
|
|
170
|
+
parent = child;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
function heapSortNumbers_(items, from, to) {
|
|
174
|
+
const size = to - from;
|
|
175
|
+
for (let root = (size >> 1) - 1; root >= 0; root--) {
|
|
176
|
+
siftDownNumbers_(items, from, root, size);
|
|
177
|
+
}
|
|
178
|
+
for (let end = size - 1; end > 0; end--) {
|
|
179
|
+
swap_(items, from, from + end);
|
|
180
|
+
siftDownNumbers_(items, from, 0, end);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
function partitionNumbers_(items, from, to) {
|
|
184
|
+
const last = to - 1;
|
|
185
|
+
const middle = from + ((last - from) >> 1);
|
|
186
|
+
if (items[middle] < items[from]) {
|
|
187
|
+
swap_(items, middle, from);
|
|
188
|
+
}
|
|
189
|
+
if (items[last] < items[middle]) {
|
|
190
|
+
swap_(items, last, middle);
|
|
191
|
+
if (items[middle] < items[from]) {
|
|
192
|
+
swap_(items, middle, from);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const pivot = items[middle];
|
|
196
|
+
let lower = from - 1;
|
|
197
|
+
let upper = to;
|
|
198
|
+
for (;;) {
|
|
199
|
+
do {
|
|
200
|
+
lower++;
|
|
201
|
+
} while (items[lower] < pivot);
|
|
202
|
+
do {
|
|
203
|
+
upper--;
|
|
204
|
+
} while (items[upper] > pivot);
|
|
205
|
+
if (lower >= upper) {
|
|
206
|
+
return upper + 1;
|
|
207
|
+
}
|
|
208
|
+
swap_(items, lower, upper);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function introSortNumbers_(items, from, to, depth) {
|
|
212
|
+
let start = from;
|
|
213
|
+
let end = to;
|
|
214
|
+
let budget = depth;
|
|
215
|
+
while (end - start > INSERTION_LENGTH_) {
|
|
216
|
+
if (budget === 0) {
|
|
217
|
+
heapSortNumbers_(items, start, end);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
budget--;
|
|
221
|
+
const split = partitionNumbers_(items, start, end);
|
|
222
|
+
if (split - start < end - split) {
|
|
223
|
+
introSortNumbers_(items, start, split, budget);
|
|
224
|
+
start = split;
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
introSortNumbers_(items, split, end, budget);
|
|
228
|
+
end = split;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
insertionSortNumbers_(items, start, end);
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Sorts the numbers `items[from … to)` in place, in the order
|
|
235
|
+
* `Float64Array.prototype.sort()` gives — ascending, `−0` before `+0`, `NaN`
|
|
236
|
+
* last — leaving the rest of `items` untouched and allocating nothing: one
|
|
237
|
+
* group's slice of a scratch buffer, without the view `subarray` allocates.
|
|
238
|
+
*
|
|
239
|
+
* Introsort over `<` (see {@link sortRange}), after one pass that moves the
|
|
240
|
+
* `NaN`s to the end; the zeros, which `<` cannot tell apart, are then signed in
|
|
241
|
+
* O(log n). Not stable, which only shows in `NaN` payloads. O(n log n), O(log
|
|
242
|
+
* n) stack.
|
|
243
|
+
*
|
|
244
|
+
* @param items Sorted in place within the range; a typed array reads without
|
|
245
|
+
* boxing.
|
|
246
|
+
* @param from First position. Default `0`.
|
|
247
|
+
* @param to End position, excluded. Default `items.length`.
|
|
248
|
+
* @throws {RangeError} When `from` and `to` are not integers with `0 ≤ from ≤
|
|
249
|
+
* to ≤ items.length`.
|
|
250
|
+
*/
|
|
251
|
+
export function sortNumbersRange(items, from = 0, to = items.length) {
|
|
252
|
+
assertRange_("sortNumbersRange", items.length, from, to);
|
|
253
|
+
let kept = from;
|
|
254
|
+
let zeros = 0;
|
|
255
|
+
let negativeZeros = 0;
|
|
256
|
+
for (let position = from; position < to; position++) {
|
|
257
|
+
const value = items[position];
|
|
258
|
+
if (value !== value) {
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
if (value === 0) {
|
|
262
|
+
zeros++;
|
|
263
|
+
if (1 / value < 0) {
|
|
264
|
+
negativeZeros++;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
items[kept++] = value;
|
|
268
|
+
}
|
|
269
|
+
for (let position = kept; position < to; position++) {
|
|
270
|
+
items[position] = Number.NaN;
|
|
271
|
+
}
|
|
272
|
+
introSortNumbers_(items, from, kept, depthLimit_(kept - from));
|
|
273
|
+
if (negativeZeros === 0 || negativeZeros === zeros) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
// The zeros sit together after the last negative number.
|
|
277
|
+
const first = bisectRightNumbers(items, -Number.MIN_VALUE, from, kept);
|
|
278
|
+
for (let position = first; position < first + zeros; position++) {
|
|
279
|
+
items[position] = position < first + negativeZeros ? -0 : 0;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
//# sourceMappingURL=sort-range.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sort-range.js","sourceRoot":"","sources":["../../src/sort/sort-range.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,EAAE,MAAM,oCAAoC,CAAC;AAQxE,2EAA2E;AAC3E,kCAAkC;AAClC,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B,SAAS,YAAY,CACpB,IAAY,EACZ,MAAc,EACd,IAAY,EACZ,EAAU;IAEV,IACC,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC;QACvB,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC;QACrB,IAAI,GAAG,CAAC;QACR,IAAI,GAAG,EAAE;QACT,EAAE,GAAG,MAAM,EACV,CAAC;QACF,MAAM,IAAI,UAAU,CACnB,GAAG,IAAI,MAAM,IAAI,KAAK,EAAE,2BAA2B,MAAM,GAAG,CAC5D,CAAC;IACH,CAAC;AACF,CAAC;AAED,4EAA4E;AAC5E,8DAA8D;AAC9D,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AQ,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC;IAC9B,CAAC;IACD,iBAAiB,CAAC,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,WAAW,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC;IAC/D,IAAI,aAAa,KAAK,CAAC,IAAI,aAAa,KAAK,KAAK,EAAE,CAAC;QACpD,OAAO;IACR,CAAC;IACD,yDAAyD;IACzD,MAAM,KAAK,GAAG,kBAAkB,CAAC,KAAK,EAAE,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IACvE,KAAK,IAAI,QAAQ,GAAG,KAAK,EAAE,QAAQ,GAAG,KAAK,GAAG,KAAK,EAAE,QAAQ,EAAE,EAAE,CAAC;QACjE,KAAK,CAAC,QAAQ,CAAC,GAAG,QAAQ,GAAG,KAAK,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC7D,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Comparator } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* The positions of `values` in the order `compare` sorts them — an argsort:
|
|
4
|
+
* `values[result[0]]` is the first value, `values[result[1]]` the second, and
|
|
5
|
+
* so on. Stable: positions of equivalent values keep their input order.
|
|
6
|
+
*
|
|
7
|
+
* Reach for it to sort one array and carry the permutation over to others, or
|
|
8
|
+
* to write a result back at each value's original position (ranks, adjusted
|
|
9
|
+
* p-values) without sorting `[value, index]` pairs.
|
|
10
|
+
*
|
|
11
|
+
* O(n log n) comparisons; one array of `n` positions.
|
|
12
|
+
*
|
|
13
|
+
* @param values The values to order. Not modified.
|
|
14
|
+
* @param compare A strict weak ordering of the values.
|
|
15
|
+
* @returns A new array of the positions `0 … n − 1`, in sorted order.
|
|
16
|
+
*/
|
|
17
|
+
export declare function sortedIndices<T>(values: ArrayLike<T>, compare: Comparator<T>): number[];
|
|
18
|
+
//# sourceMappingURL=sorted-indices.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sorted-indices.d.ts","sourceRoot":"","sources":["../../src/sort/sorted-indices.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAC9B,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,EACpB,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,GACpB,MAAM,EAAE,CAMV"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The positions of `values` in the order `compare` sorts them — an argsort:
|
|
3
|
+
* `values[result[0]]` is the first value, `values[result[1]]` the second, and
|
|
4
|
+
* so on. Stable: positions of equivalent values keep their input order.
|
|
5
|
+
*
|
|
6
|
+
* Reach for it to sort one array and carry the permutation over to others, or
|
|
7
|
+
* to write a result back at each value's original position (ranks, adjusted
|
|
8
|
+
* p-values) without sorting `[value, index]` pairs.
|
|
9
|
+
*
|
|
10
|
+
* O(n log n) comparisons; one array of `n` positions.
|
|
11
|
+
*
|
|
12
|
+
* @param values The values to order. Not modified.
|
|
13
|
+
* @param compare A strict weak ordering of the values.
|
|
14
|
+
* @returns A new array of the positions `0 … n − 1`, in sorted order.
|
|
15
|
+
*/
|
|
16
|
+
export function sortedIndices(values, compare) {
|
|
17
|
+
const indices = Array.from({ length: values.length }, (_, index) => index);
|
|
18
|
+
// Array.prototype.sort is stable (ES2019), and the input positions start in
|
|
19
|
+
// ascending order, so equivalent values keep their order.
|
|
20
|
+
return indices.sort((left, right) => compare(values[left], values[right]));
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=sorted-indices.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sorted-indices.js","sourceRoot":"","sources":["../../src/sort/sorted-indices.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,aAAa,CAC5B,MAAoB,EACpB,OAAsB;IAEtB,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC;IAE3E,4EAA4E;IAC5E,0DAA0D;IAC1D,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAE,EAAE,MAAM,CAAC,KAAK,CAAE,CAAC,CAAC,CAAC;AAC9E,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ac-kit/algo",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Classic algorithms that work on your own types rather than on numbers: searching, sorting, grouping, graph traversal and string distance.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"algorithms",
|
|
@@ -45,15 +45,16 @@
|
|
|
45
45
|
"prepack": "yarn build"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@ac-kit/core": "^0.
|
|
49
|
-
"@ac-kit/data": "^0.
|
|
48
|
+
"@ac-kit/core": "^0.13.0",
|
|
49
|
+
"@ac-kit/data": "^0.5.0",
|
|
50
50
|
"@types/web": "^0.0.367"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@ac-kit/math-random": "0.
|
|
53
|
+
"@ac-kit/math-random": "0.4.1",
|
|
54
|
+
"@ac-kit/test-util": "0.5.0",
|
|
54
55
|
"@ac-kit/tsconfig": "0.1.0",
|
|
55
56
|
"@ac-kit/typedoc-config": "0.1.0",
|
|
56
|
-
"@ac-kit/vitest-config": "0.
|
|
57
|
+
"@ac-kit/vitest-config": "0.2.0",
|
|
57
58
|
"@typescript/native": "typescript@7.0.2",
|
|
58
59
|
"rimraf": "6.1.4",
|
|
59
60
|
"typedoc": "0.28.20",
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { RandomFn } from "@ac-kit/core";
|
|
2
|
-
/**
|
|
3
|
-
* Answers `true` with the given probability.
|
|
4
|
-
*
|
|
5
|
-
* Calls `random` exactly once, whatever `probability` is (also `0` and `1`), so
|
|
6
|
-
* a seeded source is consumed at the same rate whichever branch a test takes.
|
|
7
|
-
* `0` never answers `true` and `1` always does, since `random` is in `[0, 1)`.
|
|
8
|
-
*
|
|
9
|
-
* Complexity: O(1), one call to `random`.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* if (randomBoolean(0.3, Math.random)) {
|
|
14
|
-
* // taken three times out of ten
|
|
15
|
-
* }
|
|
16
|
-
* ```;
|
|
17
|
-
*
|
|
18
|
-
* @param probability The chance of `true`, in `[0, 1]`.
|
|
19
|
-
* @param random A source in `[0, 1)`.
|
|
20
|
-
* @throws {RangeError} When `probability` is `NaN` or outside `[0, 1]`.
|
|
21
|
-
*/
|
|
22
|
-
export declare function randomBoolean(probability: number, random: RandomFn): boolean;
|
|
23
|
-
//# sourceMappingURL=random-boolean.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"random-boolean.d.ts","sourceRoot":"","sources":["../../src/sample/random-boolean.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,aAAa,CAAC,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,GAAG,OAAO,CAM5E"}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Answers `true` with the given probability.
|
|
3
|
-
*
|
|
4
|
-
* Calls `random` exactly once, whatever `probability` is (also `0` and `1`), so
|
|
5
|
-
* a seeded source is consumed at the same rate whichever branch a test takes.
|
|
6
|
-
* `0` never answers `true` and `1` always does, since `random` is in `[0, 1)`.
|
|
7
|
-
*
|
|
8
|
-
* Complexity: O(1), one call to `random`.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* ```ts
|
|
12
|
-
* if (randomBoolean(0.3, Math.random)) {
|
|
13
|
-
* // taken three times out of ten
|
|
14
|
-
* }
|
|
15
|
-
* ```;
|
|
16
|
-
*
|
|
17
|
-
* @param probability The chance of `true`, in `[0, 1]`.
|
|
18
|
-
* @param random A source in `[0, 1)`.
|
|
19
|
-
* @throws {RangeError} When `probability` is `NaN` or outside `[0, 1]`.
|
|
20
|
-
*/
|
|
21
|
-
export function randomBoolean(probability, random) {
|
|
22
|
-
if (!(probability >= 0 && probability <= 1)) {
|
|
23
|
-
throw new RangeError("probability must be within [0, 1]");
|
|
24
|
-
}
|
|
25
|
-
return random() < probability;
|
|
26
|
-
}
|
|
27
|
-
//# sourceMappingURL=random-boolean.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"random-boolean.js","sourceRoot":"","sources":["../../src/sample/random-boolean.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,aAAa,CAAC,WAAmB,EAAE,MAAgB;IAClE,IAAI,CAAC,CAAC,WAAW,IAAI,CAAC,IAAI,WAAW,IAAI,CAAC,CAAC,EAAE,CAAC;QAC7C,MAAM,IAAI,UAAU,CAAC,mCAAmC,CAAC,CAAC;IAC3D,CAAC;IAED,OAAO,MAAM,EAAE,GAAG,WAAW,CAAC;AAC/B,CAAC"}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { RandomFn } from "@ac-kit/core";
|
|
2
|
-
/**
|
|
3
|
-
* Draws an integer uniformly from `[minInclusive, maxExclusive)`.
|
|
4
|
-
*
|
|
5
|
-
* Computed as `floor(random() * range)`, so the result is as uniform as
|
|
6
|
-
* `random` is: a generator with `k` bits of output gives each value a
|
|
7
|
-
* probability within `range / 2^k` (relative) of `1 / range`, about `2^-20` for
|
|
8
|
-
* `range = 2^32` over `Math.random`'s 52 to 53 bits, depending on the engine. A
|
|
9
|
-
* range wider than the generator's resolution cannot be covered uniformly by
|
|
10
|
-
* any method.
|
|
11
|
-
*
|
|
12
|
-
* Complexity: O(1), one call to `random`.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```ts
|
|
16
|
-
* const roll = randomInteger(1, 7, Math.random); // 1..6
|
|
17
|
-
* ```;
|
|
18
|
-
*
|
|
19
|
-
* @param minInclusive The smallest result; a safe integer.
|
|
20
|
-
* @param maxExclusive The bound, never returned; a safe integer greater than
|
|
21
|
-
* `minInclusive`, with `maxExclusive - minInclusive` at most `2^53`.
|
|
22
|
-
* @param random A source in `[0, 1)`.
|
|
23
|
-
* @throws {RangeError} When a bound is not a safe integer, the range is empty
|
|
24
|
-
* or wider than `2^53`.
|
|
25
|
-
*/
|
|
26
|
-
export declare function randomInteger(minInclusive: number, maxExclusive: number, random: RandomFn): number;
|
|
27
|
-
//# sourceMappingURL=random-integer.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"random-integer.d.ts","sourceRoot":"","sources":["../../src/sample/random-integer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,aAAa,CAC5B,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,MAAM,EACpB,MAAM,EAAE,QAAQ,GACd,MAAM,CAkBR"}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Draws an integer uniformly from `[minInclusive, maxExclusive)`.
|
|
3
|
-
*
|
|
4
|
-
* Computed as `floor(random() * range)`, so the result is as uniform as
|
|
5
|
-
* `random` is: a generator with `k` bits of output gives each value a
|
|
6
|
-
* probability within `range / 2^k` (relative) of `1 / range`, about `2^-20` for
|
|
7
|
-
* `range = 2^32` over `Math.random`'s 52 to 53 bits, depending on the engine. A
|
|
8
|
-
* range wider than the generator's resolution cannot be covered uniformly by
|
|
9
|
-
* any method.
|
|
10
|
-
*
|
|
11
|
-
* Complexity: O(1), one call to `random`.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```ts
|
|
15
|
-
* const roll = randomInteger(1, 7, Math.random); // 1..6
|
|
16
|
-
* ```;
|
|
17
|
-
*
|
|
18
|
-
* @param minInclusive The smallest result; a safe integer.
|
|
19
|
-
* @param maxExclusive The bound, never returned; a safe integer greater than
|
|
20
|
-
* `minInclusive`, with `maxExclusive - minInclusive` at most `2^53`.
|
|
21
|
-
* @param random A source in `[0, 1)`.
|
|
22
|
-
* @throws {RangeError} When a bound is not a safe integer, the range is empty
|
|
23
|
-
* or wider than `2^53`.
|
|
24
|
-
*/
|
|
25
|
-
export function randomInteger(minInclusive, maxExclusive, random) {
|
|
26
|
-
if (!Number.isSafeInteger(minInclusive) ||
|
|
27
|
-
!Number.isSafeInteger(maxExclusive)) {
|
|
28
|
-
throw new RangeError("bounds must be safe integers");
|
|
29
|
-
}
|
|
30
|
-
// Compared before subtracting: the difference of two safe integers can
|
|
31
|
-
// round to 2^53 when the true range is 2^53 + 1.
|
|
32
|
-
if (!(maxExclusive > minInclusive) || maxExclusive > minInclusive + 2 ** 53) {
|
|
33
|
-
throw new RangeError("maxExclusive must exceed minInclusive by at most 2^53");
|
|
34
|
-
}
|
|
35
|
-
const range = maxExclusive - minInclusive;
|
|
36
|
-
const offset = Math.floor(random() * range);
|
|
37
|
-
// random() * range can round up to range itself for a huge range.
|
|
38
|
-
return minInclusive + Math.min(offset, range - 1);
|
|
39
|
-
}
|
|
40
|
-
//# sourceMappingURL=random-integer.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"random-integer.js","sourceRoot":"","sources":["../../src/sample/random-integer.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,aAAa,CAC5B,YAAoB,EACpB,YAAoB,EACpB,MAAgB;IAEhB,IACC,CAAC,MAAM,CAAC,aAAa,CAAC,YAAY,CAAC;QACnC,CAAC,MAAM,CAAC,aAAa,CAAC,YAAY,CAAC,EAClC,CAAC;QACF,MAAM,IAAI,UAAU,CAAC,8BAA8B,CAAC,CAAC;IACtD,CAAC;IACD,uEAAuE;IACvE,iDAAiD;IACjD,IAAI,CAAC,CAAC,YAAY,GAAG,YAAY,CAAC,IAAI,YAAY,GAAG,YAAY,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC;QAC7E,MAAM,IAAI,UAAU,CACnB,uDAAuD,CACvD,CAAC;IACH,CAAC;IACD,MAAM,KAAK,GAAG,YAAY,GAAG,YAAY,CAAC;IAC1C,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC;IAC5C,kEAAkE;IAClE,OAAO,YAAY,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;AACnD,CAAC"}
|