annotate PsiCLASS-1.0.2/samtools-0.1.19/ksort.h @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 /* The MIT License
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 Copyright (c) 2008 Genome Research Ltd (GRL).
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 Permission is hereby granted, free of charge, to any person obtaining
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 a copy of this software and associated documentation files (the
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 "Software"), to deal in the Software without restriction, including
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 without limitation the rights to use, copy, modify, merge, publish,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 distribute, sublicense, and/or sell copies of the Software, and to
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 permit persons to whom the Software is furnished to do so, subject to
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 the following conditions:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 The above copyright notice and this permission notice shall be
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 included in all copies or substantial portions of the Software.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 SOFTWARE.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 /* Contact: Heng Li <lh3@sanger.ac.uk> */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 /*
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 2012-12-11 (0.1.4):
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 * Defined __ks_insertsort_##name as static to compile with C99.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 2008-11-16 (0.1.4):
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 * Fixed a bug in introsort() that happens in rare cases.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 2008-11-05 (0.1.3):
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 * Fixed a bug in introsort() for complex comparisons.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 * Fixed a bug in mergesort(). The previous version is not stable.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 2008-09-15 (0.1.2):
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 * Accelerated introsort. On my Mac (not on another Linux machine),
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 my implementation is as fast as std::sort on random input.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 * Added combsort and in introsort, switch to combsort if the
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 recursion is too deep.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 2008-09-13 (0.1.1):
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 * Added k-small algorithm
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 2008-09-05 (0.1.0):
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 * Initial version
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 #ifndef AC_KSORT_H
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 #define AC_KSORT_H
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 #include <stdlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 #include <string.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 typedef struct {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 void *left, *right;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 int depth;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 } ks_isort_stack_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 #define KSORT_SWAP(type_t, a, b) { register type_t t=(a); (a)=(b); (b)=t; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 #define KSORT_INIT(name, type_t, __sort_lt) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 void ks_mergesort_##name(size_t n, type_t array[], type_t temp[]) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 type_t *a2[2], *a, *b; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 int curr, shift; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 a2[0] = array; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 a2[1] = temp? temp : (type_t*)malloc(sizeof(type_t) * n); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 for (curr = 0, shift = 0; (1ul<<shift) < n; ++shift) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 a = a2[curr]; b = a2[1-curr]; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 if (shift == 0) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 type_t *p = b, *i, *eb = a + n; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 for (i = a; i < eb; i += 2) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 if (i == eb - 1) *p++ = *i; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 else { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 if (__sort_lt(*(i+1), *i)) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 *p++ = *(i+1); *p++ = *i; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 } else { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 *p++ = *i; *p++ = *(i+1); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 } else { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 size_t i, step = 1ul<<shift; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 for (i = 0; i < n; i += step<<1) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 type_t *p, *j, *k, *ea, *eb; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 if (n < i + step) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 ea = a + n; eb = a; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 } else { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 ea = a + i + step; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 eb = a + (n < i + (step<<1)? n : i + (step<<1)); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 j = a + i; k = a + i + step; p = b + i; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 while (j < ea && k < eb) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 if (__sort_lt(*k, *j)) *p++ = *k++; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 else *p++ = *j++; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 while (j < ea) *p++ = *j++; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 while (k < eb) *p++ = *k++; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 curr = 1 - curr; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 if (curr == 1) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 type_t *p = a2[0], *i = a2[1], *eb = array + n; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 for (; p < eb; ++i) *p++ = *i; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 if (temp == 0) free(a2[1]); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 void ks_heapadjust_##name(size_t i, size_t n, type_t l[]) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 size_t k = i; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 type_t tmp = l[i]; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 while ((k = (k << 1) + 1) < n) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 if (k != n - 1 && __sort_lt(l[k], l[k+1])) ++k; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 if (__sort_lt(l[k], tmp)) break; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 l[i] = l[k]; i = k; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 l[i] = tmp; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 void ks_heapmake_##name(size_t lsize, type_t l[]) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 size_t i; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 for (i = (lsize >> 1) - 1; i != (size_t)(-1); --i) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 ks_heapadjust_##name(i, lsize, l); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 void ks_heapsort_##name(size_t lsize, type_t l[]) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 size_t i; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 for (i = lsize - 1; i > 0; --i) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 type_t tmp; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 tmp = *l; *l = l[i]; l[i] = tmp; ks_heapadjust_##name(0, i, l); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 static inline void __ks_insertsort_##name(type_t *s, type_t *t) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 type_t *i, *j, swap_tmp; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 for (i = s + 1; i < t; ++i) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 for (j = i; j > s && __sort_lt(*j, *(j-1)); --j) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 swap_tmp = *j; *j = *(j-1); *(j-1) = swap_tmp; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 void ks_combsort_##name(size_t n, type_t a[]) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 const double shrink_factor = 1.2473309501039786540366528676643; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 int do_swap; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 size_t gap = n; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 type_t tmp, *i, *j; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 do { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 if (gap > 2) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 gap = (size_t)(gap / shrink_factor); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 if (gap == 9 || gap == 10) gap = 11; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 do_swap = 0; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 for (i = a; i < a + n - gap; ++i) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 j = i + gap; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 if (__sort_lt(*j, *i)) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 tmp = *i; *i = *j; *j = tmp; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 do_swap = 1; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 } while (do_swap || gap > 2); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 if (gap != 1) __ks_insertsort_##name(a, a + n); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 void ks_introsort_##name(size_t n, type_t a[]) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 int d; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 ks_isort_stack_t *top, *stack; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 type_t rp, swap_tmp; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 type_t *s, *t, *i, *j, *k; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 if (n < 1) return; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 else if (n == 2) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 if (__sort_lt(a[1], a[0])) { swap_tmp = a[0]; a[0] = a[1]; a[1] = swap_tmp; } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 return; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 for (d = 2; 1ul<<d < n; ++d); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 stack = (ks_isort_stack_t*)malloc(sizeof(ks_isort_stack_t) * ((sizeof(size_t)*d)+2)); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 top = stack; s = a; t = a + (n-1); d <<= 1; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 while (1) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 if (s < t) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 if (--d == 0) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 ks_combsort_##name(t - s + 1, s); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 t = s; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 continue; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 i = s; j = t; k = i + ((j-i)>>1) + 1; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 if (__sort_lt(*k, *i)) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 if (__sort_lt(*k, *j)) k = j; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 } else k = __sort_lt(*j, *i)? i : j; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 rp = *k; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 if (k != t) { swap_tmp = *k; *k = *t; *t = swap_tmp; } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 for (;;) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 do ++i; while (__sort_lt(*i, rp)); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 do --j; while (i <= j && __sort_lt(rp, *j)); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 if (j <= i) break; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 swap_tmp = *i; *i = *j; *j = swap_tmp; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 swap_tmp = *i; *i = *t; *t = swap_tmp; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213 if (i-s > t-i) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 if (i-s > 16) { top->left = s; top->right = i-1; top->depth = d; ++top; } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215 s = t-i > 16? i+1 : t; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216 } else { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217 if (t-i > 16) { top->left = i+1; top->right = t; top->depth = d; ++top; } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218 t = i-s > 16? i-1 : s; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220 } else { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 if (top == stack) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222 free(stack); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 __ks_insertsort_##name(a, a+n); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224 return; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 } else { --top; s = (type_t*)top->left; t = (type_t*)top->right; d = top->depth; } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229 /* This function is adapted from: http://ndevilla.free.fr/median/ */ \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 /* 0 <= kk < n */ \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231 type_t ks_ksmall_##name(size_t n, type_t arr[], size_t kk) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233 type_t *low, *high, *k, *ll, *hh, *mid; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 low = arr; high = arr + n - 1; k = arr + kk; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235 for (;;) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236 if (high <= low) return *k; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 if (high == low + 1) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 if (__sort_lt(*high, *low)) KSORT_SWAP(type_t, *low, *high); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239 return *k; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 mid = low + (high - low) / 2; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 if (__sort_lt(*high, *mid)) KSORT_SWAP(type_t, *mid, *high); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243 if (__sort_lt(*high, *low)) KSORT_SWAP(type_t, *low, *high); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 if (__sort_lt(*low, *mid)) KSORT_SWAP(type_t, *mid, *low); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245 KSORT_SWAP(type_t, *mid, *(low+1)); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 ll = low + 1; hh = high; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 for (;;) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248 do ++ll; while (__sort_lt(*ll, *low)); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 do --hh; while (__sort_lt(*low, *hh)); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250 if (hh < ll) break; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251 KSORT_SWAP(type_t, *ll, *hh); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253 KSORT_SWAP(type_t, *low, *hh); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254 if (hh <= k) low = ll; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255 if (hh >= k) high = hh - 1; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 void ks_shuffle_##name(size_t n, type_t a[]) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 int i, j; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 for (i = n; i > 1; --i) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 type_t tmp; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263 j = (int)(drand48() * i); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 tmp = a[j]; a[j] = a[i-1]; a[i-1] = tmp; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268 #define ks_mergesort(name, n, a, t) ks_mergesort_##name(n, a, t)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 #define ks_introsort(name, n, a) ks_introsort_##name(n, a)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 #define ks_combsort(name, n, a) ks_combsort_##name(n, a)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271 #define ks_heapsort(name, n, a) ks_heapsort_##name(n, a)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272 #define ks_heapmake(name, n, a) ks_heapmake_##name(n, a)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 #define ks_heapadjust(name, i, n, a) ks_heapadjust_##name(i, n, a)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 #define ks_ksmall(name, n, a, k) ks_ksmall_##name(n, a, k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275 #define ks_shuffle(name, n, a) ks_shuffle_##name(n, a)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277 #define ks_lt_generic(a, b) ((a) < (b))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278 #define ks_lt_str(a, b) (strcmp((a), (b)) < 0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280 typedef const char *ksstr_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 #define KSORT_INIT_GENERIC(type_t) KSORT_INIT(type_t, type_t, ks_lt_generic)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283 #define KSORT_INIT_STR KSORT_INIT(str, ksstr_t, ks_lt_str)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 #endif