@ac-kit/algo 0.5.0 → 0.7.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 +128 -0
- package/README.md +60 -18
- package/dist/combinatorics/cartesian-product.d.ts +46 -0
- package/dist/combinatorics/cartesian-product.d.ts.map +1 -0
- package/dist/combinatorics/cartesian-product.js +79 -0
- package/dist/combinatorics/cartesian-product.js.map +1 -0
- package/dist/combinatorics/permutations.d.ts +41 -0
- package/dist/combinatorics/permutations.d.ts.map +1 -0
- package/dist/combinatorics/permutations.js +70 -0
- package/dist/combinatorics/permutations.js.map +1 -0
- package/dist/combinatorics/power-set.d.ts +40 -0
- package/dist/combinatorics/power-set.d.ts.map +1 -0
- package/dist/combinatorics/power-set.js +67 -0
- package/dist/combinatorics/power-set.js.map +1 -0
- package/dist/graph/bfs.d.ts +66 -0
- package/dist/graph/bfs.d.ts.map +1 -0
- package/dist/graph/bfs.js +89 -0
- package/dist/graph/bfs.js.map +1 -0
- package/dist/graph/connected-components.d.ts +35 -0
- package/dist/graph/connected-components.d.ts.map +1 -0
- package/dist/graph/connected-components.js +75 -0
- package/dist/graph/connected-components.js.map +1 -0
- package/dist/graph/find-cycle.d.ts +5 -3
- package/dist/graph/find-cycle.d.ts.map +1 -1
- package/dist/graph/find-cycle.js +23 -3
- package/dist/graph/find-cycle.js.map +1 -1
- package/dist/graph/floyd-warshall.d.ts +67 -0
- package/dist/graph/floyd-warshall.d.ts.map +1 -0
- package/dist/graph/floyd-warshall.js +106 -0
- package/dist/graph/floyd-warshall.js.map +1 -0
- package/dist/graph/graph-cycle-error.d.ts +16 -0
- package/dist/graph/graph-cycle-error.d.ts.map +1 -0
- package/dist/graph/graph-cycle-error.js +20 -0
- package/dist/graph/graph-cycle-error.js.map +1 -0
- package/dist/graph/hierarchy-from-parents.js.map +1 -1
- package/dist/graph/lowest-common-ancestor.d.ts +32 -0
- package/dist/graph/lowest-common-ancestor.d.ts.map +1 -0
- package/dist/graph/lowest-common-ancestor.js +56 -0
- package/dist/graph/lowest-common-ancestor.js.map +1 -0
- package/dist/graph/topological-sort.d.ts +51 -0
- package/dist/graph/topological-sort.d.ts.map +1 -0
- package/dist/graph/topological-sort.js +124 -0
- package/dist/graph/topological-sort.js.map +1 -0
- package/dist/group/_nest-by.d.ts +19 -0
- package/dist/group/_nest-by.d.ts.map +1 -0
- package/dist/group/_nest-by.js +85 -0
- package/dist/group/_nest-by.js.map +1 -0
- package/dist/group/count-by.d.ts +31 -0
- package/dist/group/count-by.d.ts.map +1 -0
- package/dist/group/count-by.js +35 -0
- package/dist/group/count-by.js.map +1 -0
- package/dist/group/flat-group-by.d.ts +28 -0
- package/dist/group/flat-group-by.d.ts.map +1 -0
- package/dist/group/flat-group-by.js +30 -0
- package/dist/group/flat-group-by.js.map +1 -0
- package/dist/group/group-adjacent-by.d.ts +31 -0
- package/dist/group/group-adjacent-by.d.ts.map +1 -0
- package/dist/group/group-adjacent-by.js +50 -0
- package/dist/group/group-adjacent-by.js.map +1 -0
- package/dist/group/group-sort.d.ts +38 -0
- package/dist/group/group-sort.d.ts.map +1 -0
- package/dist/group/group-sort.js +49 -0
- package/dist/group/group-sort.js.map +1 -0
- package/dist/group/index-by.d.ts +33 -0
- package/dist/group/index-by.d.ts.map +1 -0
- package/dist/group/index-by.js +42 -0
- package/dist/group/index-by.js.map +1 -0
- package/dist/group/nested-map.d.ts +12 -0
- package/dist/group/nested-map.d.ts.map +1 -0
- package/dist/group/nested-map.js +2 -0
- package/dist/group/nested-map.js.map +1 -0
- package/dist/group/rollup-by.d.ts +62 -0
- package/dist/group/rollup-by.d.ts.map +1 -0
- package/dist/group/rollup-by.js +72 -0
- package/dist/group/rollup-by.js.map +1 -0
- package/dist/group/run-length-encode.d.ts +41 -0
- package/dist/group/run-length-encode.d.ts.map +1 -0
- package/dist/group/run-length-encode.js +66 -0
- package/dist/group/run-length-encode.js.map +1 -0
- package/dist/index.d.ts +39 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +39 -1
- package/dist/index.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/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/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/ordered/is-sorted.d.ts +33 -0
- package/dist/ordered/is-sorted.d.ts.map +1 -0
- package/dist/ordered/is-sorted.js +39 -0
- package/dist/ordered/is-sorted.js.map +1 -0
- package/dist/sample/sample-without-replacement.d.ts +26 -0
- package/dist/sample/sample-without-replacement.d.ts.map +1 -0
- package/dist/sample/sample-without-replacement.js +41 -0
- package/dist/sample/sample-without-replacement.js.map +1 -0
- package/dist/sample/shuffle.d.ts +2 -2
- package/dist/sample/shuffle.d.ts.map +1 -1
- package/dist/sample/shuffle.js +3 -3
- package/dist/sample/shuffle.js.map +1 -1
- package/dist/sample/weighted-choice-index.d.ts +29 -0
- package/dist/sample/weighted-choice-index.d.ts.map +1 -0
- package/dist/sample/weighted-choice-index.js +62 -0
- package/dist/sample/weighted-choice-index.js.map +1 -0
- package/dist/sample/weighted-choice.d.ts +28 -0
- package/dist/sample/weighted-choice.d.ts.map +1 -0
- package/dist/sample/weighted-choice.js +31 -0
- package/dist/sample/weighted-choice.js.map +1 -0
- package/dist/select/minmax.d.ts +52 -0
- package/dist/select/minmax.d.ts.map +1 -0
- package/dist/select/minmax.js +109 -0
- package/dist/select/minmax.js.map +1 -0
- package/dist/select/{quickselect.d.ts → quickselect-in-place.d.ts} +2 -2
- package/dist/select/quickselect-in-place.d.ts.map +1 -0
- package/dist/select/{quickselect.js → quickselect-in-place.js} +3 -3
- package/dist/select/quickselect-in-place.js.map +1 -0
- package/dist/sequence/_matching-runs.d.ts +16 -0
- package/dist/sequence/_matching-runs.d.ts.map +1 -0
- package/dist/sequence/_matching-runs.js +178 -0
- package/dist/sequence/_matching-runs.js.map +1 -0
- package/dist/sequence/common-prefix-length.d.ts +26 -0
- package/dist/sequence/common-prefix-length.d.ts.map +1 -0
- package/dist/sequence/common-prefix-length.js +34 -0
- package/dist/sequence/common-prefix-length.js.map +1 -0
- package/dist/sequence/diff-sequences.d.ts +59 -0
- package/dist/sequence/diff-sequences.d.ts.map +1 -0
- package/dist/sequence/diff-sequences.js +67 -0
- package/dist/sequence/diff-sequences.js.map +1 -0
- package/dist/sequence/levenshtein-distance.d.ts +43 -0
- package/dist/sequence/levenshtein-distance.d.ts.map +1 -0
- package/dist/sequence/levenshtein-distance.js +102 -0
- package/dist/sequence/levenshtein-distance.js.map +1 -0
- package/dist/sequence/longest-common-subsequence.d.ts +28 -0
- package/dist/sequence/longest-common-subsequence.d.ts.map +1 -0
- package/dist/sequence/longest-common-subsequence.js +39 -0
- package/dist/sequence/longest-common-subsequence.js.map +1 -0
- package/dist/sequence/longest-increasing-subsequence.d.ts +28 -0
- package/dist/sequence/longest-increasing-subsequence.d.ts.map +1 -0
- package/dist/sequence/longest-increasing-subsequence.js +47 -0
- package/dist/sequence/longest-increasing-subsequence.js.map +1 -0
- package/dist/sort/_sort-primitives.d.ts +6 -0
- package/dist/sort/_sort-primitives.d.ts.map +1 -0
- package/dist/sort/_sort-primitives.js +18 -0
- package/dist/sort/_sort-primitives.js.map +1 -0
- package/dist/sort/counting-sort.d.ts +40 -0
- package/dist/sort/counting-sort.d.ts.map +1 -0
- package/dist/sort/counting-sort.js +51 -0
- package/dist/sort/counting-sort.js.map +1 -0
- package/dist/sort/merge-sort.d.ts +26 -0
- package/dist/sort/merge-sort.d.ts.map +1 -0
- package/dist/sort/merge-sort.js +73 -0
- package/dist/sort/merge-sort.js.map +1 -0
- package/dist/sort/quick-sort-in-place.d.ts +35 -0
- package/dist/sort/quick-sort-in-place.d.ts.map +1 -0
- package/dist/sort/quick-sort-in-place.js +98 -0
- package/dist/sort/quick-sort-in-place.js.map +1 -0
- 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/rank.d.ts +64 -0
- package/dist/sort/rank.d.ts.map +1 -0
- package/dist/sort/rank.js +107 -0
- package/dist/sort/rank.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 +267 -0
- package/dist/sort/sort-range.js.map +1 -0
- package/package.json +6 -5
- package/dist/select/quickselect.d.ts.map +0 -1
- package/dist/select/quickselect.js.map +0 -1
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Comparator } from "@ac-kit/core";
|
|
2
|
+
import type { WritableArrayLike } from "./sort-range.js";
|
|
3
|
+
/**
|
|
4
|
+
* Sorts `items` in place by `compare` — quicksort, unstable, with O(log n)
|
|
5
|
+
* auxiliary space and no allocation beyond a small stack of ranges.
|
|
6
|
+
*
|
|
7
|
+
* Partitions three ways around the median of the first, middle and last items
|
|
8
|
+
* (Dijkstra's Dutch national flag): items equivalent to the pivot are settled
|
|
9
|
+
* in the pass that finds them, so many duplicates cost less, not more, and an
|
|
10
|
+
* all-equal input takes one pass. Ranges below 16 items are finished by
|
|
11
|
+
* insertion sort. No recursion: the larger side of each partition waits on an
|
|
12
|
+
* explicit stack while the smaller one is sorted, which bounds the stack to
|
|
13
|
+
* ⌊log₂ n⌋ ranges.
|
|
14
|
+
*
|
|
15
|
+
* The worst case stays O(n²) comparisons, on an input crafted against the
|
|
16
|
+
* median of three; `sortRange` (introsort) is the in-place sort that guarantees
|
|
17
|
+
* O(n log n).
|
|
18
|
+
*
|
|
19
|
+
* A comparator that is not a strict weak order leaves `items` in some
|
|
20
|
+
* permutation of its elements.
|
|
21
|
+
*
|
|
22
|
+
* Time complexity: O(n log n) comparisons on average, O(n · d) for `d` distinct
|
|
23
|
+
* keys, O(n²) in the worst case. Memory: O(log n).
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const scores = [3, 1, 2];
|
|
28
|
+
* quickSortInPlace(scores, compareNaturalAscending); // scores is [1, 2, 3]
|
|
29
|
+
* ```;
|
|
30
|
+
*
|
|
31
|
+
* @param items - Sorted in place: an array or a typed array.
|
|
32
|
+
* @param compare - A strict weak ordering of the items.
|
|
33
|
+
*/
|
|
34
|
+
export declare function quickSortInPlace<T>(items: WritableArrayLike<T>, compare: Comparator<T>): void;
|
|
35
|
+
//# sourceMappingURL=quick-sort-in-place.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quick-sort-in-place.d.ts","sourceRoot":"","sources":["../../src/sort/quick-sort-in-place.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAG/C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AA2BzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EACjC,KAAK,EAAE,iBAAiB,CAAC,CAAC,CAAC,EAC3B,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,GACpB,IAAI,CA2CN"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { insertionSort_, swap_ } from "./_sort-primitives.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 medianOfThree_(items, compare, from, to) {
|
|
6
|
+
const first = items[from];
|
|
7
|
+
const middle = items[from + ((to - from) >> 1)];
|
|
8
|
+
const last = items[to - 1];
|
|
9
|
+
if (compare(first, middle) < 0) {
|
|
10
|
+
if (compare(middle, last) < 0) {
|
|
11
|
+
return middle;
|
|
12
|
+
}
|
|
13
|
+
return compare(first, last) < 0 ? last : first;
|
|
14
|
+
}
|
|
15
|
+
if (compare(first, last) < 0) {
|
|
16
|
+
return first;
|
|
17
|
+
}
|
|
18
|
+
return compare(middle, last) < 0 ? last : middle;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Sorts `items` in place by `compare` — quicksort, unstable, with O(log n)
|
|
22
|
+
* auxiliary space and no allocation beyond a small stack of ranges.
|
|
23
|
+
*
|
|
24
|
+
* Partitions three ways around the median of the first, middle and last items
|
|
25
|
+
* (Dijkstra's Dutch national flag): items equivalent to the pivot are settled
|
|
26
|
+
* in the pass that finds them, so many duplicates cost less, not more, and an
|
|
27
|
+
* all-equal input takes one pass. Ranges below 16 items are finished by
|
|
28
|
+
* insertion sort. No recursion: the larger side of each partition waits on an
|
|
29
|
+
* explicit stack while the smaller one is sorted, which bounds the stack to
|
|
30
|
+
* ⌊log₂ n⌋ ranges.
|
|
31
|
+
*
|
|
32
|
+
* The worst case stays O(n²) comparisons, on an input crafted against the
|
|
33
|
+
* median of three; `sortRange` (introsort) is the in-place sort that guarantees
|
|
34
|
+
* O(n log n).
|
|
35
|
+
*
|
|
36
|
+
* A comparator that is not a strict weak order leaves `items` in some
|
|
37
|
+
* permutation of its elements.
|
|
38
|
+
*
|
|
39
|
+
* Time complexity: O(n log n) comparisons on average, O(n · d) for `d` distinct
|
|
40
|
+
* keys, O(n²) in the worst case. Memory: O(log n).
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const scores = [3, 1, 2];
|
|
45
|
+
* quickSortInPlace(scores, compareNaturalAscending); // scores is [1, 2, 3]
|
|
46
|
+
* ```;
|
|
47
|
+
*
|
|
48
|
+
* @param items - Sorted in place: an array or a typed array.
|
|
49
|
+
* @param compare - A strict weak ordering of the items.
|
|
50
|
+
*/
|
|
51
|
+
export function quickSortInPlace(items, compare) {
|
|
52
|
+
const pending = [];
|
|
53
|
+
let from = 0;
|
|
54
|
+
let to = items.length;
|
|
55
|
+
for (;;) {
|
|
56
|
+
while (to - from > INSERTION_LENGTH_) {
|
|
57
|
+
const pivot = medianOfThree_(items, compare, from, to);
|
|
58
|
+
// [from, less) < pivot, [less, index) ≡ pivot, (greater, to) > pivot.
|
|
59
|
+
let less = from;
|
|
60
|
+
let index = from;
|
|
61
|
+
let greater = to - 1;
|
|
62
|
+
while (index <= greater) {
|
|
63
|
+
const order = compare(items[index], pivot);
|
|
64
|
+
if (order < 0) {
|
|
65
|
+
swap_(items, less++, index++);
|
|
66
|
+
}
|
|
67
|
+
else if (order > 0) {
|
|
68
|
+
swap_(items, index, greater--);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
index++;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const lowerLength = less - from;
|
|
75
|
+
const upperLength = to - greater - 1;
|
|
76
|
+
// Only a comparator that is not a strict weak order can leave the
|
|
77
|
+
// pivot's class empty with one side the whole range.
|
|
78
|
+
if (lowerLength === to - from || upperLength === to - from) {
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
if (lowerLength < upperLength) {
|
|
82
|
+
pending.push(greater + 1, to);
|
|
83
|
+
to = less;
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
pending.push(from, less);
|
|
87
|
+
from = greater + 1;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
insertionSort_(items, compare, from, to);
|
|
91
|
+
if (pending.length === 0) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
to = pending.pop();
|
|
95
|
+
from = pending.pop();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=quick-sort-in-place.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quick-sort-in-place.js","sourceRoot":"","sources":["../../src/sort/quick-sort-in-place.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAG9D,2EAA2E;AAC3E,kCAAkC;AAClC,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B,SAAS,cAAc,CACtB,KAA2B,EAC3B,OAAsB,EACtB,IAAY,EACZ,EAAU;IAEV,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAE,CAAC;IAC3B,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAE,CAAC;IACjD,MAAM,IAAI,GAAG,KAAK,CAAC,EAAE,GAAG,CAAC,CAAE,CAAC;IAC5B,IAAI,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;QAChC,IAAI,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;YAC/B,OAAO,MAAM,CAAC;QACf,CAAC;QACD,OAAO,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,GAA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AK,EAAE,CAAC;gBACT,CAAC;YACF,CAAC;YACD,MAAM,WAAW,GAAG,IAAI,GAAG,IAAI,CAAC;YAChC,MAAM,WAAW,GAAG,EAAE,GAAG,OAAO,GAAG,CAAC,CAAC;YACrC,kEAAkE;YAClE,qDAAqD;YACrD,IAAI,WAAW,KAAK,EAAE,GAAG,IAAI,IAAI,WAAW,KAAK,EAAE,GAAG,IAAI,EAAE,CAAC;gBAC5D,MAAM;YACP,CAAC;YACD,IAAI,WAAW,GAAG,WAAW,EAAE,CAAC;gBAC/B,OAAO,CAAC,IAAI,CAAC,OAAO,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC;gBAC9B,EAAE,GAAG,IAAI,CAAC;YACX,CAAC;iBAAM,CAAC;gBACP,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;gBACzB,IAAI,GAAG,OAAO,GAAG,CAAC,CAAC;YACpB,CAAC;QACF,CAAC;QACD,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;QACzC,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC1B,OAAO;QACR,CAAC;QACD,EAAE,GAAG,OAAO,CAAC,GAAG,EAAG,CAAC;QACpB,IAAI,GAAG,OAAO,CAAC,GAAG,EAAG,CAAC;IACvB,CAAC;AACF,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 @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AE,EAAE,CAAC;gBAC5C,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAE,CAAC;gBACzB,KAAK,CAAC,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,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,64 @@
|
|
|
1
|
+
import type { Callable, Comparator } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* How tied values share ranks. With values `[10, 20, 20, 30]`:
|
|
4
|
+
*
|
|
5
|
+
* - `"average"` — the mean of the positions the ties span: `[1, 2.5, 2.5, 4]`
|
|
6
|
+
* (fractional ranking; what Spearman's and Kendall's coefficients and the
|
|
7
|
+
* rank tests need, since it keeps the rank sum `n(n + 1)/2`).
|
|
8
|
+
* - `"min"` — the lowest of those positions: `[1, 2, 2, 4]` (standard competition
|
|
9
|
+
* ranking, "1224").
|
|
10
|
+
* - `"max"` — the highest: `[1, 3, 3, 4]` (modified competition ranking, "1334").
|
|
11
|
+
* - `"dense"` — consecutive ranks for distinct values: `[1, 2, 2, 3]` ("1223").
|
|
12
|
+
* - `"ordinal"` — every value its own rank, ties in input order: `[1, 2, 3, 4]`
|
|
13
|
+
* ("1234").
|
|
14
|
+
*
|
|
15
|
+
* The names are SciPy `rankdata`'s and pandas `rank`'s methods (pandas calls
|
|
16
|
+
* `"ordinal"` `"first"`).
|
|
17
|
+
*/
|
|
18
|
+
export type RankTies = "average" | "min" | "max" | "dense" | "ordinal";
|
|
19
|
+
/** Options of {@link rank} and {@link rankBy}. */
|
|
20
|
+
export type RankOptions = {
|
|
21
|
+
/** How tied values share ranks. Defaults to `"average"`. */
|
|
22
|
+
readonly ties?: RankTies;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* The 1-based rank of each value in the order `compare` sorts them, written at
|
|
26
|
+
* the value's own position: `result[i]` is the rank of `values[i]`.
|
|
27
|
+
*
|
|
28
|
+
* Values `compare` finds equivalent are tied and share a rank by `options.ties`
|
|
29
|
+
* ({@link RankTies}, `"average"` by default).
|
|
30
|
+
*
|
|
31
|
+
* O(n log n) comparisons; one array of positions and the result.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* rank([30, 10, 20, 10], compareNaturalAscending); // [4, 1.5, 3, 1.5]
|
|
36
|
+
* rank([30, 10, 20, 10], compareNaturalAscending, { ties: "dense" }); // [3, 1, 2, 1]
|
|
37
|
+
* ```;
|
|
38
|
+
*
|
|
39
|
+
* @param values The values to rank. Not modified.
|
|
40
|
+
* @param compare A strict weak ordering of the values.
|
|
41
|
+
* @param options How ties share ranks.
|
|
42
|
+
* @returns A new array of `values.length` ranks in `[1, n]`.
|
|
43
|
+
*/
|
|
44
|
+
export declare function rank<T>(values: ArrayLike<T>, compare: Comparator<T>, options?: RankOptions): Float64Array;
|
|
45
|
+
/**
|
|
46
|
+
* The 1-based rank of each item by the number `valueOf` gives it, ascending,
|
|
47
|
+
* written at the item's own position: `result[i]` is the rank of `items[i]`.
|
|
48
|
+
*
|
|
49
|
+
* Equal keys are tied (`-0` ties `0`) and share a rank by `options.ties`
|
|
50
|
+
* ({@link RankTies}, `"average"` by default). An item whose key is `NaN` has no
|
|
51
|
+
* place in the order: its rank is `NaN`, and the other items are ranked among
|
|
52
|
+
* themselves (pandas' `na_option="keep"`).
|
|
53
|
+
*
|
|
54
|
+
* O(n log n) comparisons and one `valueOf` call per item; one key, one position
|
|
55
|
+
* and one result array.
|
|
56
|
+
*
|
|
57
|
+
* @param items The items to rank. Not modified.
|
|
58
|
+
* @param valueOf The numeric key of an item.
|
|
59
|
+
* @param options How ties share ranks.
|
|
60
|
+
* @returns A new array of `items.length` ranks, each in `[1, m]` for the `m`
|
|
61
|
+
* items with a key, or `NaN`.
|
|
62
|
+
*/
|
|
63
|
+
export declare function rankBy<T>(items: ArrayLike<T>, valueOf: Callable<[T], number>, options?: RankOptions): Float64Array;
|
|
64
|
+
//# sourceMappingURL=rank.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rank.d.ts","sourceRoot":"","sources":["../../src/sort/rank.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAIzD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,MAAM,QAAQ,GAAG,SAAS,GAAG,KAAK,GAAG,KAAK,GAAG,OAAO,GAAG,SAAS,CAAC;AAEvE,kDAAkD;AAClD,MAAM,MAAM,WAAW,GAAG;IACzB,4DAA4D;IAC5D,QAAQ,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAC;CACzB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,IAAI,CAAC,CAAC,EACrB,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,EACpB,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,EACtB,OAAO,CAAC,EAAE,WAAW,GACnB,YAAY,CAWd;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,MAAM,CAAC,CAAC,EACvB,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,EACnB,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,EAC9B,OAAO,CAAC,EAAE,WAAW,GACnB,YAAY,CA+Bd"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { sortedIndices } from "./sorted-indices.js";
|
|
2
|
+
/**
|
|
3
|
+
* The 1-based rank of each value in the order `compare` sorts them, written at
|
|
4
|
+
* the value's own position: `result[i]` is the rank of `values[i]`.
|
|
5
|
+
*
|
|
6
|
+
* Values `compare` finds equivalent are tied and share a rank by `options.ties`
|
|
7
|
+
* ({@link RankTies}, `"average"` by default).
|
|
8
|
+
*
|
|
9
|
+
* O(n log n) comparisons; one array of positions and the result.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* rank([30, 10, 20, 10], compareNaturalAscending); // [4, 1.5, 3, 1.5]
|
|
14
|
+
* rank([30, 10, 20, 10], compareNaturalAscending, { ties: "dense" }); // [3, 1, 2, 1]
|
|
15
|
+
* ```;
|
|
16
|
+
*
|
|
17
|
+
* @param values The values to rank. Not modified.
|
|
18
|
+
* @param compare A strict weak ordering of the values.
|
|
19
|
+
* @param options How ties share ranks.
|
|
20
|
+
* @returns A new array of `values.length` ranks in `[1, n]`.
|
|
21
|
+
*/
|
|
22
|
+
export function rank(values, compare, options) {
|
|
23
|
+
const order = sortedIndices(values, compare);
|
|
24
|
+
const ranks = new Float64Array(values.length);
|
|
25
|
+
assignRanks_(ranks, order, (left, right) => compare(values[left], values[right]) === 0, options?.ties ?? "average");
|
|
26
|
+
return ranks;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The 1-based rank of each item by the number `valueOf` gives it, ascending,
|
|
30
|
+
* written at the item's own position: `result[i]` is the rank of `items[i]`.
|
|
31
|
+
*
|
|
32
|
+
* Equal keys are tied (`-0` ties `0`) and share a rank by `options.ties`
|
|
33
|
+
* ({@link RankTies}, `"average"` by default). An item whose key is `NaN` has no
|
|
34
|
+
* place in the order: its rank is `NaN`, and the other items are ranked among
|
|
35
|
+
* themselves (pandas' `na_option="keep"`).
|
|
36
|
+
*
|
|
37
|
+
* O(n log n) comparisons and one `valueOf` call per item; one key, one position
|
|
38
|
+
* and one result array.
|
|
39
|
+
*
|
|
40
|
+
* @param items The items to rank. Not modified.
|
|
41
|
+
* @param valueOf The numeric key of an item.
|
|
42
|
+
* @param options How ties share ranks.
|
|
43
|
+
* @returns A new array of `items.length` ranks, each in `[1, m]` for the `m`
|
|
44
|
+
* items with a key, or `NaN`.
|
|
45
|
+
*/
|
|
46
|
+
export function rankBy(items, valueOf, options) {
|
|
47
|
+
const count = items.length;
|
|
48
|
+
const keys = new Float64Array(count);
|
|
49
|
+
const order = [];
|
|
50
|
+
const ranks = new Float64Array(count);
|
|
51
|
+
for (let index = 0; index < count; index++) {
|
|
52
|
+
const key = valueOf(items[index]);
|
|
53
|
+
keys[index] = key;
|
|
54
|
+
if (key === key) {
|
|
55
|
+
order.push(index);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
ranks[index] = Number.NaN;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
order.sort((left, right) => {
|
|
62
|
+
const leftKey = keys[left];
|
|
63
|
+
const rightKey = keys[right];
|
|
64
|
+
if (leftKey < rightKey) {
|
|
65
|
+
return -1;
|
|
66
|
+
}
|
|
67
|
+
return leftKey > rightKey ? 1 : left - right;
|
|
68
|
+
});
|
|
69
|
+
assignRanks_(ranks, order, (left, right) => keys[left] === keys[right], options?.ties ?? "average");
|
|
70
|
+
return ranks;
|
|
71
|
+
}
|
|
72
|
+
function assignRanks_(ranks, order, tied, ties) {
|
|
73
|
+
const count = order.length;
|
|
74
|
+
let distinct = 0;
|
|
75
|
+
let start = 0;
|
|
76
|
+
while (start < count) {
|
|
77
|
+
let end = start + 1;
|
|
78
|
+
while (end < count && tied(order[start], order[end])) {
|
|
79
|
+
end++;
|
|
80
|
+
}
|
|
81
|
+
distinct++;
|
|
82
|
+
for (let position = start; position < end; position++) {
|
|
83
|
+
ranks[order[position]] = groupRank_(ties, start, end, position, distinct);
|
|
84
|
+
}
|
|
85
|
+
start = end;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function groupRank_(ties, start, end, position, distinct) {
|
|
89
|
+
switch (ties) {
|
|
90
|
+
case "average": {
|
|
91
|
+
return (start + 1 + end) / 2;
|
|
92
|
+
}
|
|
93
|
+
case "min": {
|
|
94
|
+
return start + 1;
|
|
95
|
+
}
|
|
96
|
+
case "max": {
|
|
97
|
+
return end;
|
|
98
|
+
}
|
|
99
|
+
case "dense": {
|
|
100
|
+
return distinct;
|
|
101
|
+
}
|
|
102
|
+
case "ordinal": {
|
|
103
|
+
return position + 1;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=rank.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rank.js","sourceRoot":"","sources":["../../src/sort/rank.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AA0BpD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,IAAI,CACnB,MAAoB,EACpB,OAAsB,EACtB,OAAqB;IAErB,MAAM,KAAK,GAAG,aAAa,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC7C,MAAM,KAAK,GAAG,IAAI,YAAY,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC9C,YAAY,CACX,KAAK,EACL,KAAK,EACL,CAAC,IAAI,EA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;QAC9B,CAAC;QACD,KAAK,KAAK,EAAE,CAAC;YACZ,OAAO,KAAK,GAAG,CAAC,CAAC;QAClB,CAAC;QACD,KAAK,KAAK,EAAE,CAAC;YACZ,OAAO,GAAG,CAAC;QACZ,CAAC;QACD,KAAK,OAAO,EAAE,CAAC;YACd,OAAO,QAAQ,CAAC;QACjB,CAAC;QACD,KAAK,SAAS,EAAE,CAAC;YAChB,OAAO,QAAQ,GAAG,CAAC,CAAC;QACrB,CAAC;IACF,CAAC;AACF,CAAC"}
|
|
@@ -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;AAK/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;AA6IF;;;;;;;;;;;;;;;;;;;;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"}
|