- Implement RtlPrefectMemoryNonTemporal. Patch by Patrick Baggett <baggett.patrick...
[reactos.git] / reactos / lib / rtl / qsort.c
1 /*
2 * COPYRIGHT:
3 *-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * PROJECT: ReactOS system libraries
36 * PURPOSE: Unicode Conversion Routines
37 * FILE: lib/rtl/qsort.c
38 * PROGRAMMER: Adapted from CygWin newlib 2000-03-12.
39 */
40 #include <rtl.h>
41
42 /* FIXME: these types should be from the default includes */
43
44 typedef int (* _pfunccmp_t) (const void *, const void *);
45
46 #define min(a,b) ((a)<(b)?(a):(b))
47
48 /*
49 * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
50 */
51 #define swapcode(TYPE, parmi, parmj, n) { \
52 long i = (n) / sizeof (TYPE); \
53 register TYPE *pi = (TYPE *) (parmi); \
54 register TYPE *pj = (TYPE *) (parmj); \
55 do { \
56 register TYPE t = *pi; \
57 *pi++ = *pj; \
58 *pj++ = t; \
59 } while (--i > 0); \
60 }
61
62 #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
63 es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
64
65 static __inline void
66 swapfunc (
67 char * a,
68 char * b,
69 int n,
70 int swaptype
71 )
72 {
73 if(swaptype <= 1)
74 swapcode(long, a, b, n)
75 else
76 swapcode(char, a, b, n)
77 }
78
79 #define swap(a, b) \
80 if (swaptype == 0) { \
81 long t = *(long *)(a); \
82 *(long *)(a) = *(long *)(b); \
83 *(long *)(b) = t; \
84 } else \
85 swapfunc(a, b, es, swaptype)
86
87 #define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
88
89 static __inline char *
90 med3 (
91 char * a,
92 char * b,
93 char * c,
94 _pfunccmp_t cmp
95 )
96 {
97 return cmp(a, b) < 0 ?
98 (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
99 :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
100 }
101
102
103 /* EXPORTED */
104 void
105 qsort (
106 void * a,
107 size_t n,
108 size_t es,
109 _pfunccmp_t cmp
110 )
111 {
112 char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
113 int d, r, swaptype, swap_cnt;
114
115 loop: SWAPINIT(a, es);
116 swap_cnt = 0;
117 if (n < 7)
118 {
119 for ( pm = (char *) a + es;
120 pm < (char *) a + n * es;
121 pm += es
122 )
123 {
124 for ( pl = pm;
125 pl > (char *) a && cmp(pl - es, pl) > 0;
126 pl -= es
127 )
128 {
129 swap(pl, pl - es);
130 }
131 }
132 return;
133 }
134 pm = (char *) a + (n / 2) * es;
135 if (n > 7)
136 {
137 pl = (char *) a;
138 pn = (char *) a + (n - 1) * es;
139 if (n > 40)
140 {
141 d = (n / 8) * es;
142 pl = med3(pl, pl + d, pl + 2 * d, cmp);
143 pm = med3(pm - d, pm, pm + d, cmp);
144 pn = med3(pn - 2 * d, pn - d, pn, cmp);
145 }
146 pm = med3(pl, pm, pn, cmp);
147 }
148 swap(a, pm);
149 pa = pb = (char *) a + es;
150
151 pc = pd = (char *) a + (n - 1) * es;
152 for (;;)
153 {
154 while (pb <= pc && (r = cmp(pb, a)) <= 0)
155 {
156 if (r == 0)
157 {
158 swap_cnt = 1;
159 swap(pa, pb);
160 pa += es;
161 }
162 pb += es;
163 }
164 while (pb <= pc && (r = cmp(pc, a)) >= 0)
165 {
166 if (r == 0)
167 {
168 swap_cnt = 1;
169 swap(pc, pd);
170 pd -= es;
171 }
172 pc -= es;
173 }
174 if (pb > pc)
175 {
176 break;
177 }
178 swap(pb, pc);
179 swap_cnt = 1;
180 pb += es;
181 pc -= es;
182 }
183 if (swap_cnt == 0) /* Switch to insertion sort */
184 {
185 for ( pm = (char *) a + es;
186 pm < (char *) a + n * es;
187 pm += es
188 )
189 {
190 for ( pl = pm;
191 pl > (char *) a && cmp(pl - es, pl) > 0;
192 pl -= es
193 )
194 {
195 swap(pl, pl - es);
196 }
197 }
198 return;
199 }
200
201 pn = (char *) a + n * es;
202 r = min(pa - (char *)a, pb - pa);
203 vecswap(a, pb - r, r);
204 r = min(pd - pc, pn - pd - es);
205 vecswap(pb, pn - r, r);
206 if ((r = pb - pa) > es)
207 {
208 qsort(a, r / es, es, cmp);
209 }
210 if ((r = pd - pc) > es)
211 {
212 /* Iterate rather than recurse to save stack space */
213 a = pn - r;
214 n = r / es;
215 goto loop;
216 }
217 /* qsort(pn - r, r / es, es, cmp);*/
218 }
219
220
221 /* EOF */