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
|
#include "stdafx.h"
#include "Sort.h"
namespace storm {
/**
* SortData.
*/
static void init(SortData &d) {
if (d.compare)
d.compareFn = d.compare->rawCall();
assert(d.data->filled < d.data->count, L"Sorting requires at least one free element.");
}
SortData::SortData(GcArray<byte> *data, const Handle &type) :
data(data), type(type), compare(null), begin(0), end(data->filled) { init(*this); }
SortData::SortData(GcArray<byte> *data, const Handle &type, FnBase *compare) :
data(data), type(type), compare(compare), begin(0), end(data->filled) { init(*this); }
SortData::SortData(GcArray<byte> *data, const Handle &type, size_t begin, size_t end) :
data(data), type(type), compare(null), begin(begin), end(end) { init(*this); }
SortData::SortData(GcArray<byte> *data, const Handle &type, FnBase *compare, size_t begin, size_t end) :
data(data), type(type), compare(compare), begin(begin), end(end) { init(*this); }
SortData::SortData(const SortData &src, size_t begin, size_t end) :
data(src.data), type(src.type), compare(src.compare), compareFn(src.compareFn), begin(begin), end(end) {}
/**
* Convenience operations.
*/
// Compare two elements.
static inline bool compare(const SortData &d, const void *params[2]) {
if (d.compare) {
bool r = false;
d.compareFn.call(d.compare, &r, params);
return r;
} else {
return (*d.type.lessFn)(params[0], params[1]);
}
}
static inline bool compare(const SortData &d, size_t a, size_t b) {
size_t size = d.type.size;
const void *params[2] = {
d.data->v + a*size,
d.data->v + b*size,
};
return compare(d, params);
}
static inline bool compare(const SortData &d, size_t a, const void *b) {
size_t size = d.type.size;
const void *params[2] = {
d.data->v + a*size,
b,
};
return compare(d, params);
}
// Move 'from' to 'to'. Assuming 'to' is not initialized.
static inline void move(const SortData &d, size_t to, size_t from) {
if (to == from)
return;
// Note: Just using memcpy is safe. The GC moves memory around all the time.
size_t size = d.type.size;
memcpy(d.data->v + to*size, d.data->v + from*size, size);
}
// Move 'from' to 'to' while keeping 'preserve' intact. Assumes 'from' is not to be preserved.
static inline void move(const SortData &d, size_t &preserve, size_t to, size_t from) {
if (preserve == to) {
preserve = d.data->filled;
move(d, preserve, to);
}
move(d, to, from);
}
/**
* Heap operations.
*/
static inline size_t left(const SortData &sort, size_t item) {
return (item - sort.begin)*2 + 1 + sort.begin;
}
static inline size_t right(const SortData &sort, size_t item) {
return (item - sort.begin)*2 + 2 + sort.begin;
}
static inline size_t parent(const SortData &sort, size_t item) {
return (item - sort.begin - 1) / 2 + sort.begin;
}
// 'elem' is the location of the element that should be at location 'top'.
static inline void siftDown(const SortData &sort, size_t top, size_t elem) {
size_t at = top;
while (at < sort.end) {
size_t l = left(sort, at);
size_t r = right(sort, at);
// Done?
if ((l >= sort.end || !compare(sort, elem, l))
&& (r >= sort.end || !compare(sort, elem, r)))
break;
// Pick one and move.
if (r < sort.end && compare(sort, l, r)) {
move(sort, elem, at, r);
at = r;
} else {
move(sort, elem, at, l);
at = l;
}
}
// Move the original element to its final position if needed.
move(sort, at, elem);
}
void makeHeap(const SortData &sort) {
size_t top = parent(sort, sort.end) + 1;
while (top > sort.begin) {
top--;
// See if this element needs to move down the heap.
siftDown(sort, top, top);
}
}
void heapInsert(const void *elem, const SortData &sort) {
// Sift up until we're done.
size_t at = sort.end;
while (at != sort.begin) {
size_t p = parent(sort, at);
// Done?
if (!compare(sort, p, elem))
break;
// Move the parent one step down in the tree.
move(sort, at, p);
at = p;
}
// Insert the new element in the heap.
sort.type.safeCopy(sort.data->v + at*sort.type.size, elem);
}
void heapRemove(const SortData &sort) {
if (sort.begin + 1 >= sort.end)
return;
// Move the top element out of the way.
move(sort, sort.data->filled, sort.begin);
// Update the heap.
siftDown(sort, sort.begin, sort.end - 1);
// Move the extracted element to its proper location.
move(sort, sort.end - 1, sort.data->filled);
}
void heapSort(const SortData &sort) {
SortData heap = sort;
makeHeap(heap);
// Pop the elements one by one.
while (heap.begin + 1 < heap.end) {
heapRemove(heap);
heap.end--;
}
}
void insertionSort(const SortData &sort) {
size_t scratch = sort.data->filled;
for (size_t target = sort.begin + 1; target < sort.end; target++) {
// Do we need to do anything at all with this element?
if (!compare(sort, target, target - 1))
continue;
// Move it to our scratch space, move the element we already compared and keep comparing
// until we find a suitable spot.
move(sort, scratch, target);
size_t to = target;
do {
move(sort, to, to - 1);
to--;
} while (to > sort.begin && compare(sort, scratch, to - 1));
// Put the element we moved back where it belongs!
move(sort, to, scratch);
}
}
/**
* Quicksort.
*/
static inline size_t pickPivot(const SortData &now) {
// Pick the median of three elements to get better performance in quicksort.
size_t a = now.begin;
size_t b = now.end - 1;
size_t c = (b - a)/2 + a;
// Small partition? Just pick one of them.
if (a == c || b == c)
return c;
if (compare(now, a, b)) { // a < b
if (compare(now, b, c))
return b; // a < b < c
else if (compare(now, c, a))
return a; // c < a < b
else
return c; // a < c < b
} else { // b < a
if (compare(now, c, b))
return b; // c < b < a
else if (compare(now, a, c))
return a; // b < a < c
else
return c; // b < c < a
}
}
static inline size_t partition(const SortData &now, size_t pivot) {
// Move the pivot away into temporary storage.
size_t temp = now.data->filled;
move(now, temp, pivot);
// Make sure 'now.begin' is empty.
if (pivot != now.begin)
move(now, pivot, now.begin);
// Partition.
size_t l = now.begin, r = now.end - 1;
while (l < r) {
// Find an element from the right to put in the hole to the left.
while (l < r && !compare(now, r, temp))
r--;
if (l < r)
move(now, l++, r);
// Find an element from the left to put in the hole to the right.
while (l < r && !compare(now, temp, l))
l++;
if (l < r)
move(now, r--, l);
}
// Move the pivot back into its proper location and return its location.
move(now, l, temp);
return l;
}
void sort(const SortData &sort) {
SortData now = sort;
// Stack of remembered ranges.
const size_t STACK_SIZE = 30;
size_t begin[STACK_SIZE];
size_t end[STACK_SIZE];
size_t top = 0;
// Minimum number of elements actually handled by quicksort.
const size_t INSERTION_LIMIT = 16;
while (true) {
if (now.begin + 1 >= now.end) {
// Try to pop some work from the stack!
if (top > 0) {
top--;
now.begin = begin[top];
now.end = end[top];
} else {
// I guess we're done.
break;
}
}
// Small enough to fall back to insertion sort?
if (now.end - now.begin <= INSERTION_LIMIT) {
insertionSort(now);
now.begin = now.end;
continue;
}
// Out of stack space? Fall back to heap sort.
if (top >= STACK_SIZE) {
heapSort(now);
now.begin = now.end;
continue;
}
// Pick a pivot and use that.
size_t pivot = pickPivot(now);
pivot = partition(now, pivot);
// Put the smaller part on hold for later. Take the larger one now.
begin[top] = now.begin;
end[top] = now.end;
if (pivot - now.begin < now.end - pivot) {
end[top] = pivot;
now.begin = pivot + 1;
} else {
begin[top] = pivot + 1;
now.end = pivot;
}
// Only 'commit' the transaction if it was a nonzero number of elements.
if (begin[top] + 1 < end[top])
top++;
}
}
}
|