Debian Codegraph @

texk/makeindexk/qsort.c

raw
1 /*
2 *
3 * This file is part of
4 * MakeIndex - A formatter and format independent index processor
5 *
6 * This file is public domain software donated by
7 * Nelson Beebe ([email protected]).
8 *
9 */
10
11 /*
12 * qsort.c: Our own version of the system qsort routine which is faster by an
13 * average of 25%, with lows and highs of 10% and 50%. The THRESHold below is
14 * the insertion sort threshold, and has been adjusted for records of size 48
15 * bytes. The MTHREShold is where we stop finding a better median.
16 */
17 #include "qsort.h" /* qq_compar_fn_t type and qqsort() prototype */
18
19 #define THRESH 4 /* threshold for insertion */
20 #define MTHRESH 6 /* threshold for median */
21
22 static int qsz; /* size of each record */
23 static int thresh; /* THRESHold in chars */
24 static int mthresh; /* MTHRESHold in chars */
25
26 static qq_compar_fn_t qcmp; /* the comparison routine */
27 static void qst(char *base, char *max);
28 /*
29 * qqsort: First, set up some global parameters for qst to share. Then,
30 * quicksort with qst(), and then a cleanup insertion sort ourselves. Sound
31 * simple? It's not...
32 */
33
34 void
35 qqsort(void *base, size_t n, size_t size, qq_compar_fn_t compar)
36 {
37 register char *i;
38 register char *j;
39 register char *lo;
40 register char *hi;
41 register char *min;
42 register char c;
43 char *max;
44
45 if (n <= 1)
46 return;
47 qsz = size;
48 qcmp = compar;
49 thresh = qsz * THRESH;
50 mthresh = qsz * MTHRESH;
51 max = (char *)base + n * qsz;
52 if (n >= THRESH) {
53 qst(base, max);
54 hi = (char *)base + thresh;
55 } else {
56 hi = max;
57 }
58 /*
59 * First put smallest element, which must be in the first THRESH, in the
60 * first position as a sentinel. This is done just by searching the
61 * first THRESH elements (or the first n if n < THRESH), finding the min,
62 * and swapping it into the first position.
63 */
64 for (j = lo = base; (lo += qsz) < hi;) {
65 if ((*qcmp) (j, lo) > 0)
66 j = lo;
67 }
68 if (j != base) { /* swap j into place */
69 for (i = base, hi = i + qsz; i < hi;) {
70 c = *j;
71 *j++ = *i;
72 *i++ = c;
73 }
74 }
75 /*
76 * With our sentinel in place, we now run the following hyper-fast
77 * insertion sort. For each remaining element, min, from [1] to [n-1],
78 * set hi to the index of the element AFTER which this one goes. Then, do
79 * the standard insertion sort shift on a character at a time basis for
80 * each element in the frob.
81 */
82 for (min = base; (hi = min += qsz) < max;) {
83 while ((*qcmp) (hi -= qsz, min) > 0);
84 if ((hi += qsz) != min) {
85 for (lo = min + qsz; --lo >= min;) {
86 c = *lo;
87 for (i = j = lo; (j -= qsz) >= hi; i = j)
88 *i = *j;
89 *i = c;
90 }
91 }
92 }
93 }
94
95
96
97 /*
98 * qst: Do a quicksort. First, find the median element, and put that one in
99 * the first place as the discriminator. (This "median" is just the median
100 * of the first, last and middle elements). (Using this median instead of
101 * the first element is a big win). Then, the usual partitioning/swapping,
102 * followed by moving the discriminator into the right place. Then, figure
103 * out the sizes of the two partions, do the smaller one recursively and the
104 * larger one via a repeat of this code. Stopping when there are less than
105 * THRESH elements in a partition and cleaning up with an insertion sort (in
106 * our caller) is a huge win. All data swaps are done in-line, which is
107 * space-losing but time-saving. (And there are only three places where this
108 * is done).
109 */
110
111 static void
112 qst(char *base, char *max)
113 {
114 register char *i;
115 register char *j;
116 register char *jj;
117 register char *mid;
118 register int ii;
119 register char c;
120 void *tmp;
121 int lo;
122 int hi;
123
124 lo = max - base; /* number of elements as chars */
125 do {
126 /*
127 * At the top here, lo is the number of characters of elements in the
128 * current partition. (Which should be max - base). Find the median
129 * of the first, last, and middle element and make that the middle
130 * element. Set j to largest of first and middle. If max is larger
131 * than that guy, then it's that guy, else compare max with loser of
132 * first and take larger. Things are set up to prefer the middle,
133 * then the first in case of ties.
134 */
135 mid = i = base + qsz * ((unsigned) (lo / qsz) >> 1);
136 if (lo >= mthresh) {
137 j = ((*qcmp) ((jj = base), i) > 0 ? jj : i);
138 if ((*qcmp) (j, (tmp = max - qsz)) > 0) {
139 /* switch to first loser */
140 j = (j == jj ? i : jj);
141 if ((*qcmp) (j, tmp) < 0)
142 j = tmp;
143 }
144 if (j != i) {
145 ii = qsz;
146 do {
147 c = *i;
148 *i++ = *j;
149 *j++ = c;
150 } while (--ii);
151 }
152 }
153 /* Semi-standard quicksort partitioning/swapping */
154 for (i = base, j = max - qsz;;) {
155 while (i < mid && (*qcmp) (i, mid) <= 0)
156 i += qsz;
157 while (j > mid) {
158 if ((*qcmp) (mid, j) <= 0) {
159 j -= qsz;
160 continue;
161 }
162 tmp = i + qsz; /* value of i after swap */
163 if (i == mid) { /* j <-> mid, new mid is j */
164 mid = jj = j;
165 } else { /* i <-> j */
166 jj = j;
167 j -= qsz;
168 }
169 goto swap;
170 }
171 if (i == mid) {
172 break;
173 } else { /* i <-> mid, new mid is i */
174 jj = mid;
175 tmp = mid = i; /* value of i after swap */
176 j -= qsz;
177 }
178 swap:
179 ii = qsz;
180 do {
181 c = *i;
182 *i++ = *jj;
183 *jj++ = c;
184 } while (--ii);
185 i = tmp;
186 }
187 /*
188 * Look at sizes of the two partitions, do the smaller one first by
189 * recursion, then do the larger one by making sure lo is its size,
190 * base and max are updated correctly, and branching back. But only
191 * repeat (recursively or by branching) if the partition is of at
192 * least size THRESH.
193 */
194 i = (j = mid) + qsz;
195 if ((lo = j - base) <= (hi = max - i)) {
196 if (lo >= thresh)
197 qst(base, j);
198 base = i;
199 lo = hi;
200 } else {
201 if (hi >= thresh)
202 qst(i, max);
203 max = j;
204 }
205 } while (lo >= thresh);
206 }