Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2
* vim: set ts=8 sts=2 et sw=2 tw=80:
3
* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef ds_Sort_h
8
#define ds_Sort_h
9
10
#include "jstypes.h"
11
12
namespace js {
13
14
namespace detail {
15
16
template <typename T>
17
MOZ_ALWAYS_INLINE void CopyNonEmptyArray(T* dst, const T* src, size_t nelems) {
18
MOZ_ASSERT(nelems != 0);
19
const T* end = src + nelems;
20
do {
21
*dst++ = *src++;
22
} while (src != end);
23
}
24
25
/* Helper function for MergeSort. */
26
template <typename T, typename Comparator>
27
MOZ_ALWAYS_INLINE bool MergeArrayRuns(T* dst, const T* src, size_t run1,
28
size_t run2, Comparator c) {
29
MOZ_ASSERT(run1 >= 1);
30
MOZ_ASSERT(run2 >= 1);
31
32
/* Copy runs already in sorted order. */
33
const T* b = src + run1;
34
bool lessOrEqual;
35
if (!c(b[-1], b[0], &lessOrEqual)) {
36
return false;
37
}
38
39
if (!lessOrEqual) {
40
/* Runs are not already sorted, merge them. */
41
for (const T* a = src;;) {
42
if (!c(*a, *b, &lessOrEqual)) {
43
return false;
44
}
45
if (lessOrEqual) {
46
*dst++ = *a++;
47
if (!--run1) {
48
src = b;
49
break;
50
}
51
} else {
52
*dst++ = *b++;
53
if (!--run2) {
54
src = a;
55
break;
56
}
57
}
58
}
59
}
60
CopyNonEmptyArray(dst, src, run1 + run2);
61
return true;
62
}
63
64
} /* namespace detail */
65
66
/*
67
* Sort the array using the merge sort algorithm. The scratch should point to
68
* a temporary storage that can hold nelems elements.
69
*
70
* The comparator must provide the () operator with the following signature:
71
*
72
* bool operator()(const T& a, const T& a, bool* lessOrEqualp);
73
*
74
* It should return true on success and set *lessOrEqualp to the result of
75
* a <= b operation. If it returns false, the sort terminates immediately with
76
* the false result. In this case the content of the array and scratch is
77
* arbitrary.
78
*/
79
template <typename T, typename Comparator>
80
MOZ_MUST_USE bool MergeSort(T* array, size_t nelems, T* scratch, Comparator c) {
81
const size_t INS_SORT_LIMIT = 3;
82
83
if (nelems <= 1) {
84
return true;
85
}
86
87
/*
88
* Apply insertion sort to small chunks to reduce the number of merge
89
* passes needed.
90
*/
91
for (size_t lo = 0; lo < nelems; lo += INS_SORT_LIMIT) {
92
size_t hi = lo + INS_SORT_LIMIT;
93
if (hi >= nelems) {
94
hi = nelems;
95
}
96
for (size_t i = lo + 1; i != hi; i++) {
97
for (size_t j = i;;) {
98
bool lessOrEqual;
99
if (!c(array[j - 1], array[j], &lessOrEqual)) {
100
return false;
101
}
102
if (lessOrEqual) {
103
break;
104
}
105
T tmp = array[j - 1];
106
array[j - 1] = array[j];
107
array[j] = tmp;
108
if (--j == lo) {
109
break;
110
}
111
}
112
}
113
}
114
115
T* vec1 = array;
116
T* vec2 = scratch;
117
for (size_t run = INS_SORT_LIMIT; run < nelems; run *= 2) {
118
for (size_t lo = 0; lo < nelems; lo += 2 * run) {
119
size_t hi = lo + run;
120
if (hi >= nelems) {
121
detail::CopyNonEmptyArray(vec2 + lo, vec1 + lo, nelems - lo);
122
break;
123
}
124
size_t run2 = (run <= nelems - hi) ? run : nelems - hi;
125
if (!detail::MergeArrayRuns(vec2 + lo, vec1 + lo, run, run2, c)) {
126
return false;
127
}
128
}
129
T* swap = vec1;
130
vec1 = vec2;
131
vec2 = swap;
132
}
133
if (vec1 == scratch) {
134
detail::CopyNonEmptyArray(array, scratch, nelems);
135
}
136
return true;
137
}
138
139
} /* namespace js */
140
141
#endif /* ds_Sort_h */