Debian Codegraph @

extern/metis/GKlib/gk_mksort.h

raw
1 /*!
2 \file gk_mksort.h
3 \brief Templates for the qsort routine
4
5 \date Started 3/28/07
6 \author George
7 \version\verbatim $Id: gk_mksort.h 21051 2017-05-25 04:36:14Z karypis $ \endverbatim
8 */
9
10
11 #ifndef _GK_MKSORT_H_
12 #define _GK_MKSORT_H_
13
14 /* Adopted from GNU glibc by Mjt.
15 * See stdlib/qsort.c in glibc */
16
17 /* Copyright (C) 1991, 1992, 1996, 1997, 1999 Free Software Foundation, Inc.
18 This file is part of the GNU C Library.
19 Written by Douglas C. Schmidt ([email protected]).
20
21 The GNU C Library is free software; you can redistribute it and/or
22 modify it under the terms of the GNU Lesser General Public
23 License as published by the Free Software Foundation; either
24 version 2.1 of the License, or (at your option) any later version.
25
26 The GNU C Library is distributed in the hope that it will be useful,
27 but WITHOUT ANY WARRANTY; without even the implied warranty of
28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 Lesser General Public License for more details.
30
31 You should have received a copy of the GNU Lesser General Public
32 License along with the GNU C Library; if not, write to the Free
33 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
34 02111-1307 USA. */
35
36 /* in-line qsort implementation. Differs from traditional qsort() routine
37 * in that it is a macro, not a function, and instead of passing an address
38 * of a comparision routine to the function, it is possible to inline
39 * comparision routine, thus speed up sorting alot.
40 *
41 * Usage:
42 * #include "iqsort.h"
43 * #define islt(a,b) (strcmp((*a),(*b))<0)
44 * char *arr[];
45 * int n;
46 * GKQSORT(char*, arr, n, islt);
47 *
48 * The "prototype" and 4 arguments are:
49 * GKQSORT(TYPE,BASE,NELT,ISLT)
50 * 1) type of each element, TYPE,
51 * 2) address of the beginning of the array, of type TYPE*,
52 * 3) number of elements in the array, and
53 * 4) comparision routine.
54 * Array pointer and number of elements are referenced only once.
55 * This is similar to a call
56 * qsort(BASE,NELT,sizeof(TYPE),ISLT)
57 * with the difference in last parameter.
58 * Note the islt macro/routine (it receives pointers to two elements):
59 * the only condition of interest is whenever one element is less than
60 * another, no other conditions (greather than, equal to etc) are tested.
61 * So, for example, to define integer sort, use:
62 * #define islt(a,b) ((*a)<(*b))
63 * GKQSORT(int, arr, n, islt)
64 *
65 * The macro could be used to implement a sorting function (see examples
66 * below), or to implement the sorting algorithm inline. That is, either
67 * create a sorting function and use it whenever you want to sort something,
68 * or use GKQSORT() macro directly instead a call to such routine. Note that
69 * the macro expands to quite some code (compiled size of int qsort on x86
70 * is about 700..800 bytes).
71 *
72 * Using this macro directly it isn't possible to implement traditional
73 * qsort() routine, because the macro assumes sizeof(element) == sizeof(TYPE),
74 * while qsort() allows element size to be different.
75 *
76 * Several ready-to-use examples:
77 *
78 * Sorting array of integers:
79 * void int_qsort(int *arr, unsigned n) {
80 * #define int_lt(a,b) ((*a)<(*b))
81 * GKQSORT(int, arr, n, int_lt);
82 * }
83 *
84 * Sorting array of string pointers:
85 * void str_qsort(char *arr[], unsigned n) {
86 * #define str_lt(a,b) (strcmp((*a),(*b)) < 0)
87 * GKQSORT(char*, arr, n, str_lt);
88 * }
89 *
90 * Sorting array of structures:
91 *
92 * struct elt {
93 * int key;
94 * ...
95 * };
96 * void elt_qsort(struct elt *arr, unsigned n) {
97 * #define elt_lt(a,b) ((a)->key < (b)->key)
98 * GKQSORT(struct elt, arr, n, elt_lt);
99 * }
100 *
101 * And so on.
102 */
103
104 /* Swap two items pointed to by A and B using temporary buffer t. */
105 #define _GKQSORT_SWAP(a, b, t) ((void)((t = *a), (*a = *b), (*b = t)))
106
107 /* Discontinue quicksort algorithm when partition gets below this size. */
108 #define _GKQSORT_MAX_THRESH 8
109
110 /* The next 4 #defines implement a very fast in-line stack abstraction. */
111 #define _GKQSORT_STACK_SIZE (8 * sizeof(size_t))
112 #define _GKQSORT_PUSH(top, low, high) (((top->_lo = (low)), (top->_hi = (high)), ++top))
113 #define _GKQSORT_POP(low, high, top) ((--top, (low = top->_lo), (high = top->_hi)))
114 #define _GKQSORT_STACK_NOT_EMPTY (_stack < _top)
115
116
117 /* The main code starts here... */
118 #define GK_MKQSORT(GKQSORT_TYPE,GKQSORT_BASE,GKQSORT_NELT,GKQSORT_LT) \
119 { \
120 GKQSORT_TYPE *const _base = (GKQSORT_BASE); \
121 const size_t _elems = (GKQSORT_NELT); \
122 GKQSORT_TYPE _hold; \
123 \
124 if (_elems < 1) \
125 return; \
126 \
127 /* Don't declare two variables of type GKQSORT_TYPE in a single \
128 * statement: eg `TYPE a, b;', in case if TYPE is a pointer, \
129 * expands to `type* a, b;' wich isn't what we want. \
130 */ \
131 \
132 if (_elems > _GKQSORT_MAX_THRESH) { \
133 GKQSORT_TYPE *_lo = _base; \
134 GKQSORT_TYPE *_hi = _lo + _elems - 1; \
135 struct { \
136 GKQSORT_TYPE *_hi; GKQSORT_TYPE *_lo; \
137 } _stack[_GKQSORT_STACK_SIZE], *_top = _stack + 1; \
138 \
139 while (_GKQSORT_STACK_NOT_EMPTY) { \
140 GKQSORT_TYPE *_left_ptr; GKQSORT_TYPE *_right_ptr; \
141 \
142 /* Select median value from among LO, MID, and HI. Rearrange \
143 LO and HI so the three values are sorted. This lowers the \
144 probability of picking a pathological pivot value and \
145 skips a comparison for both the LEFT_PTR and RIGHT_PTR in \
146 the while loops. */ \
147 \
148 GKQSORT_TYPE *_mid = _lo + ((_hi - _lo) >> 1); \
149 \
150 if (GKQSORT_LT (_mid, _lo)) \
151 _GKQSORT_SWAP (_mid, _lo, _hold); \
152 if (GKQSORT_LT (_hi, _mid)) \
153 _GKQSORT_SWAP (_mid, _hi, _hold); \
154 else \
155 goto _jump_over; \
156 if (GKQSORT_LT (_mid, _lo)) \
157 _GKQSORT_SWAP (_mid, _lo, _hold); \
158 _jump_over:; \
159 \
160 _left_ptr = _lo + 1; \
161 _right_ptr = _hi - 1; \
162 \
163 /* Here's the famous ``collapse the walls'' section of quicksort. \
164 Gotta like those tight inner loops! They are the main reason \
165 that this algorithm runs much faster than others. */ \
166 do { \
167 while (GKQSORT_LT (_left_ptr, _mid)) \
168 ++_left_ptr; \
169 \
170 while (GKQSORT_LT (_mid, _right_ptr)) \
171 --_right_ptr; \
172 \
173 if (_left_ptr < _right_ptr) { \
174 _GKQSORT_SWAP (_left_ptr, _right_ptr, _hold); \
175 if (_mid == _left_ptr) \
176 _mid = _right_ptr; \
177 else if (_mid == _right_ptr) \
178 _mid = _left_ptr; \
179 ++_left_ptr; \
180 --_right_ptr; \
181 } \
182 else if (_left_ptr == _right_ptr) { \
183 ++_left_ptr; \
184 --_right_ptr; \
185 break; \
186 } \
187 } while (_left_ptr <= _right_ptr); \
188 \
189 /* Set up pointers for next iteration. First determine whether \
190 left and right partitions are below the threshold size. If so, \
191 ignore one or both. Otherwise, push the larger partition's \
192 bounds on the stack and continue sorting the smaller one. */ \
193 \
194 if (_right_ptr - _lo <= _GKQSORT_MAX_THRESH) { \
195 if (_hi - _left_ptr <= _GKQSORT_MAX_THRESH) \
196 /* Ignore both small partitions. */ \
197 _GKQSORT_POP (_lo, _hi, _top); \
198 else \
199 /* Ignore small left partition. */ \
200 _lo = _left_ptr; \
201 } \
202 else if (_hi - _left_ptr <= _GKQSORT_MAX_THRESH) \
203 /* Ignore small right partition. */ \
204 _hi = _right_ptr; \
205 else if (_right_ptr - _lo > _hi - _left_ptr) { \
206 /* Push larger left partition indices. */ \
207 _GKQSORT_PUSH (_top, _lo, _right_ptr); \
208 _lo = _left_ptr; \
209 } \
210 else { \
211 /* Push larger right partition indices. */ \
212 _GKQSORT_PUSH (_top, _left_ptr, _hi); \
213 _hi = _right_ptr; \
214 } \
215 } \
216 } \
217 \
218 /* Once the BASE array is partially sorted by quicksort the rest \
219 is completely sorted using insertion sort, since this is efficient \
220 for partitions below MAX_THRESH size. BASE points to the \
221 beginning of the array to sort, and END_PTR points at the very \
222 last element in the array (*not* one beyond it!). */ \
223 \
224 { \
225 GKQSORT_TYPE *const _end_ptr = _base + _elems - 1; \
226 GKQSORT_TYPE *_tmp_ptr = _base; \
227 register GKQSORT_TYPE *_run_ptr; \
228 GKQSORT_TYPE *_thresh; \
229 \
230 _thresh = _base + _GKQSORT_MAX_THRESH; \
231 if (_thresh > _end_ptr) \
232 _thresh = _end_ptr; \
233 \
234 /* Find smallest element in first threshold and place it at the \
235 array's beginning. This is the smallest array element, \
236 and the operation speeds up insertion sort's inner loop. */ \
237 \
238 for (_run_ptr = _tmp_ptr + 1; _run_ptr <= _thresh; ++_run_ptr) \
239 if (GKQSORT_LT (_run_ptr, _tmp_ptr)) \
240 _tmp_ptr = _run_ptr; \
241 \
242 if (_tmp_ptr != _base) \
243 _GKQSORT_SWAP (_tmp_ptr, _base, _hold); \
244 \
245 /* Insertion sort, running from left-hand-side \
246 * up to right-hand-side. */ \
247 \
248 _run_ptr = _base + 1; \
249 while (++_run_ptr <= _end_ptr) { \
250 _tmp_ptr = _run_ptr - 1; \
251 while (GKQSORT_LT (_run_ptr, _tmp_ptr)) \
252 --_tmp_ptr; \
253 \
254 ++_tmp_ptr; \
255 if (_tmp_ptr != _run_ptr) { \
256 GKQSORT_TYPE *_trav = _run_ptr + 1; \
257 while (--_trav >= _run_ptr) { \
258 GKQSORT_TYPE *_hi; GKQSORT_TYPE *_lo; \
259 _hold = *_trav; \
260 \
261 for (_hi = _lo = _trav; --_lo >= _tmp_ptr; _hi = _lo) \
262 *_hi = *_lo; \
263 *_hi = _hold; \
264 } \
265 } \
266 } \
267 } \
268 \
269 }
270
271 #endif