1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// We use varying sorts across the self-hosted codebase. All sorts are
// consolidated here to avoid confusion and re-implementation of existing
// algorithms.
// For sorting values with limited range; uint8 and int8.
function CountingSort(array, len, signed) {
var buffer = new List();
var min = 0;
// Map int8 values onto the uint8 range when storing in buffer.
if (signed) {
min = -128;
}
for (var i = 0; i < 256; i++) {
buffer[i] = 0;
}
// Populate the buffer
for (var i = 0; i < len; i++) {
var val = array[i];
buffer[val - min]++
}
// Traverse the buffer in order and write back elements to array
var val = 0;
for (var i = 0; i < len; i++) {
// Invariant: sum(buffer[val:]) == len-i
while (true) {
if (buffer[val] > 0) {
array[i] = val + min;
buffer[val]--;
break;
} else {
val++;
}
}
}
return array;
}
// Helper for RadixSort
function ByteAtCol(x, pos) {
return (x >> (pos * 8)) & 0xFF;
}
function SortByColumn(array, len, aux, col) {
const R = 256;
let counts = new List();
// |counts| is used to compute the starting index position for each key.
// Letting counts[0] always be 0, simplifies the transform step below.
// Example:
//
// Computing frequency counts for the input [1 2 1] gives:
// 0 1 2 3 ... (keys)
// 0 0 2 1 (frequencies)
//
// Transforming frequencies to indexes gives:
// 0 1 2 3 ... (keys)
// 0 0 2 3 (indexes)
for (let r = 0; r < R + 1; r++) {
counts[r] = 0;
}
// Compute frequency counts
for (let i = 0; i < len; i++) {
let val = array[i];
let b = ByteAtCol(val, col);
counts[b + 1]++;
}
// Transform counts to indices.
for (let r = 0; r < R; r++) {
counts[r+1] += counts[r];
}
// Distribute
for (let i = 0; i < len; i++) {
let val = array[i];
let b = ByteAtCol(val, col);
aux[counts[b]++] = val;
}
// Copy back
for (let i = 0; i < len; i++) {
array[i] = aux[i];
}
}
// Sorts integers and float32. |signed| is true for int16 and int32, |floating|
// is true for float32.
function RadixSort(array, len, buffer, nbytes, signed, floating, comparefn) {
// Determined by performance testing.
if (len < 128) {
QuickSort(array, len, comparefn);
return array;
}
let aux = new List();
for (let i = 0; i < len; i++) {
aux[i] = 0;
}
let view = array;
let signMask = 1 << nbytes * 8 - 1;
// Preprocess
if (floating) {
// This happens if the array object is constructed under JIT
if (buffer === null) {
buffer = callFunction(std_TypedArray_buffer, array);
}
// Verify that the buffer is non-null
assert(buffer !== null, "Attached data buffer should be reified when array length is >= 128.");
view = new Int32Array(buffer);
// Flip sign bit for positive numbers; flip all bits for negative
// numbers
for (let i = 0; i < len; i++) {
if (view[i] & signMask) {
view[i] ^= 0xFFFFFFFF;
} else {
view[i] ^= signMask
}
}
} else if (signed) {
// Flip sign bit
for (let i = 0; i < len; i++) {
view[i] ^= signMask
}
}
// Sort
for (let col = 0; col < nbytes; col++) {
SortByColumn(view, len, aux, col);
}
// Restore original bit representation
if (floating) {
for (let i = 0; i < len; i++) {
if (view[i] & signMask) {
view[i] ^= signMask;
} else {
view[i] ^= 0xFFFFFFFF;
}
}
} else if (signed) {
for (let i = 0; i < len; i++) {
view[i] ^= signMask
}
}
return array;
}
// For sorting small arrays.
function InsertionSort(array, from, to, comparefn) {
let item, swap, i, j;
for (i = from + 1; i <= to; i++) {
item = array[i];
for (j = i - 1; j >= from; j--) {
swap = array[j];
if (comparefn(swap, item) <= 0)
break;
array[j + 1] = swap;
}
array[j + 1] = item;
}
}
function SwapArrayElements(array, i, j) {
var swap = array[i];
array[i] = array[j];
array[j] = swap;
}
// A helper function for MergeSort.
function Merge(list, start, mid, end, lBuffer, rBuffer, comparefn) {
var i, j, k;
var sizeLeft = mid - start + 1;
var sizeRight = end - mid;
// Copy our virtual lists into separate buffers.
for (i = 0; i < sizeLeft; i++)
lBuffer[i] = list[start + i];
for (j = 0; j < sizeRight; j++)
rBuffer[j] = list[mid + 1 + j];
i = 0;
j = 0;
k = start;
while (i < sizeLeft && j < sizeRight) {
if (comparefn(lBuffer[i], rBuffer[j]) <= 0) {
list[k] = lBuffer[i];
i++;
} else {
list[k] = rBuffer[j];
j++;
}
k++;
}
// Empty out any remaining elements in the buffer.
while (i < sizeLeft) {
list[k] =lBuffer[i];
i++;
k++;
}
while (j < sizeRight) {
list[k] =rBuffer[j];
j++;
k++;
}
}
// Helper function for overwriting a sparse array with a
// dense array, filling remaining slots with holes.
function MoveHoles(sparse, sparseLen, dense, denseLen) {
for (var i = 0; i < denseLen; i++)
sparse[i] = dense[i];
for (var j = denseLen; j < sparseLen; j++)
delete sparse[j];
}
// Iterative, bottom up, mergesort.
function MergeSort(array, len, comparefn) {
// Until recently typed arrays had no sort method. To work around that
// many users passed them to Array.prototype.sort. Now that we have a
// typed array specific sorting method it makes sense to divert to it
// when possible.
if (IsPossiblyWrappedTypedArray(array)) {
return callFunction(TypedArraySort, array, comparefn);
}
// To save effort we will do all of our work on a dense list,
// then create holes at the end.
var denseList = new List();
var denseLen = 0;
for (var i = 0; i < len; i++) {
if (i in array)
denseList[denseLen++] = array[i];
}
if (denseLen < 1)
return array;
// Insertion sort for small arrays, where "small" is defined by performance
// testing.
if (denseLen < 24) {
InsertionSort(denseList, 0, denseLen - 1, comparefn);
MoveHoles(array, len, denseList, denseLen);
return array;
}
// We do all of our allocating up front
var lBuffer = new List();
var rBuffer = new List();
var mid, end, endOne, endTwo;
for (var windowSize = 1; windowSize < denseLen; windowSize = 2 * windowSize) {
for (var start = 0; start < denseLen - 1; start += 2 * windowSize) {
assert(windowSize < denseLen, "The window size is larger than the array denseLength!");
// The midpoint between the two subarrays.
mid = start + windowSize - 1;
// To keep from going over the edge.
end = start + 2 * windowSize - 1;
end = end < denseLen - 1 ? end : denseLen - 1;
// Skip lopsided runs to avoid doing useless work
if (mid > end)
continue;
Merge(denseList, start, mid, end, lBuffer, rBuffer, comparefn);
}
}
MoveHoles(array, len, denseList, denseLen);
return array;
}
// Rearranges the elements in array[from:to + 1] and returns an index j such that:
// - from < j < to
// - each element in array[from:j] is less than or equal to array[j]
// - each element in array[j + 1:to + 1] greater than or equal to array[j].
function Partition(array, from, to, comparefn) {
assert(to - from >= 3, "Partition will not work with less than three elements");
var medianIndex = (from + to) >> 1;
var i = from + 1;
var j = to;
SwapArrayElements(array, medianIndex, i);
// Median of three pivot selection.
if (comparefn(array[from], array[to]) > 0)
SwapArrayElements(array, from, to);
if (comparefn(array[i], array[to]) > 0)
SwapArrayElements(array, i, to);
if (comparefn(array[from], array[i]) > 0)
SwapArrayElements(array, from, i);
var pivotIndex = i;
// Hoare partition method.
for(;;) {
do i++; while (comparefn(array[i], array[pivotIndex]) < 0);
do j--; while (comparefn(array[j], array[pivotIndex]) > 0);
if (i > j)
break;
SwapArrayElements(array, i, j);
}
SwapArrayElements(array, pivotIndex, j);
return j;
}
// In-place QuickSort.
function QuickSort(array, len, comparefn) {
// Managing the stack ourselves seems to provide a small performance boost.
var stack = new List();
var top = 0;
var start = 0;
var end = len - 1;
var pivotIndex, i, j, leftLen, rightLen;
for (;;) {
// Insertion sort for the first N elements where N is some value
// determined by performance testing.
if (end - start <= 23) {
InsertionSort(array, start, end, comparefn);
if (top < 1)
break;
end = stack[--top];
start = stack[--top];
} else {
pivotIndex = Partition(array, start, end, comparefn);
// Calculate the left and right sub-array lengths and save
// stack space by directly modifying start/end so that
// we sort the longest of the two during the next iteration.
// This reduces the maximum stack size to log2(len).
leftLen = (pivotIndex - 1) - start;
rightLen = end - (pivotIndex + 1);
if (rightLen > leftLen) {
stack[top++] = start;
stack[top++] = pivotIndex - 1;
start = pivotIndex + 1;
} else {
stack[top++] = pivotIndex + 1;
stack[top++] = end;
end = pivotIndex - 1;
}
}
}
return array;
}
|