@ac-kit/algo 0.5.0 → 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 +61 -0
- package/README.md +18 -5
- 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/hierarchy-from-parents.js.map +1 -1
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- 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/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/package.json +5 -4
|
@@ -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,cAAc,CAAC;AAe9E,MAAM,WAAW,GAAG,EAAE,CAAC;AACvB,MAAM,QAAQ,GAAG,CAAC,IAAI,WAAW,CAAC;AAClC,+EAA+E;AAC/E,MAAM,WAAW,GAAG,CAAC,CAAC;AACtB,MAAM,KAAK,GAAG,UAAU,CAAC;AACzB,MAAM,cAAc,GAAG,IAAI,UAAU,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;AAEzE;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,OAAO,gBAAgB;IACpB,SAAS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G,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"}
|
|
@@ -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,SAAS,WAAW,CAAC,MAAc;IAClC,OAAO,CAAC,GAAG,CAAC,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC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I,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"}
|
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,12 +45,13 @@
|
|
|
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.4.
|
|
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
57
|
"@ac-kit/vitest-config": "0.2.0",
|