summaryrefslogtreecommitdiffstats
path: root/js/src/builtin/Sorting.js
blob: faa31349c01352183b7e8767333b78636e58045e (plain)
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
/* 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 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 (callContentFunction(comparefn, undefined, swap, item) <= 0) {
        break;
      }
      array[j + 1] = swap;
    }
    array[j + 1] = item;
  }
}

// A helper function for MergeSort.
//
// Merge comparefn-sorted slices list[start..<=mid] and list[mid+1..<=end],
// storing the merged sequence in out[start..<=end].
function Merge(list, out, start, mid, end, comparefn) {
  // Skip lopsided runs to avoid doing useless work.
  // Skip calling the comparator if the sub-list is already sorted.
  if (
    mid >= end ||
    callContentFunction(comparefn, undefined, list[mid], list[mid + 1]) <= 0
  ) {
    for (var i = start; i <= end; i++) {
      DefineDataProperty(out, i, list[i]);
    }
    return;
  }

  var i = start;
  var j = mid + 1;
  var k = start;
  while (i <= mid && j <= end) {
    var lvalue = list[i];
    var rvalue = list[j];
    if (callContentFunction(comparefn, undefined, lvalue, rvalue) <= 0) {
      DefineDataProperty(out, k++, lvalue);
      i++;
    } else {
      DefineDataProperty(out, k++, rvalue);
      j++;
    }
  }

  // Empty out any remaining elements.
  while (i <= mid) {
    DefineDataProperty(out, k++, list[i++]);
  }
  while (j <= end) {
    DefineDataProperty(out, k++, list[j++]);
  }
}

// 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) {
  assert(IsPackedArray(array), "array is packed");
  assert(array.length === len, "length mismatch");
  assert(len > 0, "array should be non-empty");

  // Insertion sort for small arrays, where "small" is defined by performance
  // testing.
  if (len < 24) {
    InsertionSort(array, 0, len - 1, comparefn);
    return array;
  }

  // We do all of our allocating up front
  var lBuffer = array;
  var rBuffer = [];

  // Use insertion sort for initial ranges.
  var windowSize = 4;
  for (var start = 0; start < len - 1; start += windowSize) {
    var end = std_Math_min(start + windowSize - 1, len - 1);
    InsertionSort(lBuffer, start, end, comparefn);
  }

  for (; windowSize < len; windowSize = 2 * windowSize) {
    for (var start = 0; start < len; start += 2 * windowSize) {
      // The midpoint between the two subarrays.
      var mid = start + windowSize - 1;

      // To keep from going over the edge.
      var end = std_Math_min(start + 2 * windowSize - 1, len - 1);

      Merge(lBuffer, rBuffer, start, mid, end, comparefn);
    }

    // Swap both lists.
    var swap = lBuffer;
    lBuffer = rBuffer;
    rBuffer = swap;
  }
  return lBuffer;
}

// A helper function for MergeSortTypedArray.
//
// Merge comparefn-sorted slices list[start..<=mid] and list[mid+1..<=end],
// storing the merged sequence in out[start..<=end].
function MergeTypedArray(list, out, start, mid, end, comparefn) {
  // Skip lopsided runs to avoid doing useless work.
  // Skip calling the comparator if the sub-list is already sorted.
  if (
    mid >= end ||
    callContentFunction(comparefn, undefined, list[mid], list[mid + 1]) <= 0
  ) {
    for (var i = start; i <= end; i++) {
      out[i] = list[i];
    }
    return;
  }

  var i = start;
  var j = mid + 1;
  var k = start;
  while (i <= mid && j <= end) {
    var lvalue = list[i];
    var rvalue = list[j];
    if (callContentFunction(comparefn, undefined, lvalue, rvalue) <= 0) {
      out[k++] = lvalue;
      i++;
    } else {
      out[k++] = rvalue;
      j++;
    }
  }

  // Empty out any remaining elements.
  while (i <= mid) {
    out[k++] = list[i++];
  }
  while (j <= end) {
    out[k++] = list[j++];
  }
}

// Iterative, bottom up, mergesort. Optimized version for TypedArrays.
function MergeSortTypedArray(array, len, comparefn) {
  assert(
    IsPossiblyWrappedTypedArray(array),
    "MergeSortTypedArray works only with typed arrays."
  );

  // Use the same TypedArray kind for the buffer.
  var C = ConstructorForTypedArray(array);

  var lBuffer = new C(len);

  // Copy all elements into a temporary buffer, so that any modifications
  // when calling |comparefn| are ignored.
  for (var i = 0; i < len; i++) {
    lBuffer[i] = array[i];
  }

  // Insertion sort for small arrays, where "small" is defined by performance
  // testing.
  if (len < 8) {
    InsertionSort(lBuffer, 0, len - 1, comparefn);

    return lBuffer;
  }

  // We do all of our allocating up front.
  var rBuffer = new C(len);

  // Use insertion sort for the initial ranges.
  var windowSize = 4;
  for (var start = 0; start < len - 1; start += windowSize) {
    var end = std_Math_min(start + windowSize - 1, len - 1);
    InsertionSort(lBuffer, start, end, comparefn);
  }

  for (; windowSize < len; windowSize = 2 * windowSize) {
    for (var start = 0; start < len; start += 2 * windowSize) {
      // The midpoint between the two subarrays.
      var mid = start + windowSize - 1;

      // To keep from going over the edge.
      var end = std_Math_min(start + 2 * windowSize - 1, len - 1);

      MergeTypedArray(lBuffer, rBuffer, start, mid, end, comparefn);
    }

    // Swap both lists.
    var swap = lBuffer;
    lBuffer = rBuffer;
    rBuffer = swap;
  }

  return lBuffer;
}