7
* Copyright (c) 2000, 2002 Michael J. Roberts. All Rights Reserved.
9
* Please see the accompanying license file, LICENSE.TXT, for information
10
* on using and copying this software.
14
vmsort.cpp - T3 VM quicksort implementation
16
Implements quicksort. We use our own implementation rather than the
17
standard C library's qsort() routine for two reasons. First, we might
18
want to throw an exception out of the comparison routine, and it is
19
not clear that it is safe to longjmp() past qsort() on every type of
20
machine and every C run-time implementation. Second, the standard C
21
library's qsort() routine doesn't provide any means to pass a context
22
to the comparison callback, and further insists that the data to be
23
sorted be arranged as an array; we provide a higher-level abstraction
24
for the comparison callback.
28
05/14/00 MJRoberts - Creation
37
/* ------------------------------------------------------------------------ */
41
void CVmQSortData::sort(VMG_ size_t l, size_t r)
43
/* proceed if we have a non-empty range */
49
/* start at the ends of the range */
53
/* partition the range */
56
/* find the leftmost element >= the right element */
60
} while (i != r && compare(vmg_ i, v_idx) < 0);
62
/* find the rightmost element <= the right element */
66
} while (j != l && compare(vmg_ j, v_idx) > 0);
68
/* exchange elements i and j */
71
/* if we moved the 'v' element, follow that in the index */
79
/* undo the last exchange */
82
/* exchange the right value into the pivot point */
85
/* recursively sort the subranges */