]>
Commit | Line | Data |
---|---|---|
2aa62f2b | 1 | /** @file\r |
2 | Quick Sort utility function.\r | |
3 | \r | |
4 | This utility makes use of a comparison function to search arrays of\r | |
5 | unspecified type. Where an argument declared as size_t nmemb specifies the\r | |
6 | length of the array for a function, nmemb can have the value zero on a call\r | |
7 | to that function; the comparison function is not called, a search finds no\r | |
8 | matching element. Pointer arguments on such a call shall still have valid\r | |
9 | values.\r | |
10 | \r | |
11 | The implementation shall ensure that both arguments of the comparison\r | |
12 | function are pointers to elements of the array.\r | |
13 | \r | |
14 | The comparison function shall not alter the contents of the array. The\r | |
15 | implementation may reorder elements of the array between calls to the\r | |
16 | comparison function, but shall not alter the contents of any individual\r | |
17 | element.\r | |
18 | \r | |
19 | When the same objects (consisting of size bytes, irrespective of their\r | |
20 | current positions in the array) are passed more than once to the comparison\r | |
21 | function, the results shall be consistent with one another. That is, they\r | |
22 | define a total ordering on the array.\r | |
23 | \r | |
24 | A sequence point occurs immediately before and immediately after each call to\r | |
25 | the comparison function, and also between any call to the comparison function\r | |
26 | and any movement of the objects passed as arguments to that call.\r | |
27 | \r | |
28 | Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r | |
29 | * Copyright (c) 1992, 1993\r | |
30 | * The Regents of the University of California. All rights reserved.\r | |
31 | *\r | |
32 | * Redistribution and use in source and binary forms, with or without\r | |
33 | * modification, are permitted provided that the following conditions\r | |
34 | * are met:\r | |
35 | * 1. Redistributions of source code must retain the above copyright\r | |
36 | * notice, this list of conditions and the following disclaimer.\r | |
37 | * 2. Redistributions in binary form must reproduce the above copyright\r | |
38 | * notice, this list of conditions and the following disclaimer in the\r | |
39 | * documentation and/or other materials provided with the distribution.\r | |
40 | * 4. Neither the name of the University nor the names of its contributors\r | |
41 | * may be used to endorse or promote products derived from this software\r | |
42 | * without specific prior written permission.\r | |
43 | *\r | |
44 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r | |
45 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r | |
46 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r | |
47 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r | |
48 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r | |
49 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r | |
50 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r | |
51 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r | |
52 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r | |
53 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r | |
54 | * SUCH DAMAGE.\r | |
55 | \r | |
56 | ("$FreeBSD: src/lib/libc/stdlib/qsort.c,v 1.15.2.1.2.1 2009/10/25 01:10:29 kensmith Exp $");\r | |
57 | */\r | |
58 | #include <LibConfig.h>\r | |
59 | \r | |
60 | #include <stdlib.h>\r | |
61 | \r | |
62 | typedef int cmp_t(const void *, const void *);\r | |
63 | \r | |
64 | static __inline char *med3(char *, char *, char *, cmp_t *);\r | |
65 | static __inline void swapfunc(char *, char *, size_t, int);\r | |
66 | \r | |
67 | /*\r | |
68 | * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".\r | |
69 | */\r | |
70 | #define swapcode(TYPE, parmi, parmj, n) { \\r | |
71 | size_t i = (n) / sizeof (TYPE); \\r | |
72 | TYPE *pi = (TYPE *) (parmi); \\r | |
73 | TYPE *pj = (TYPE *) (parmj); \\r | |
74 | do { \\r | |
75 | TYPE t = *pi; \\r | |
76 | *pi++ = *pj; \\r | |
77 | *pj++ = t; \\r | |
78 | } while (--i > 0); \\r | |
79 | }\r | |
80 | \r | |
81 | #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \\r | |
82 | es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;\r | |
83 | \r | |
84 | static __inline void\r | |
85 | swapfunc(char *a, char *b, size_t n, int swaptype)\r | |
86 | {\r | |
87 | if(swaptype <= 1)\r | |
88 | swapcode(long, a, b, n)\r | |
89 | else\r | |
90 | swapcode(char, a, b, n)\r | |
91 | }\r | |
92 | \r | |
93 | #define swap(a, b) \\r | |
94 | if (swaptype == 0) { \\r | |
95 | long t = *(long *)(a); \\r | |
96 | *(long *)(a) = *(long *)(b); \\r | |
97 | *(long *)(b) = t; \\r | |
98 | } else \\r | |
99 | swapfunc(a, b, es, swaptype)\r | |
100 | \r | |
101 | #define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)\r | |
102 | \r | |
103 | static __inline char *\r | |
104 | med3(char *a, char *b, char *c, cmp_t *cmp )\r | |
105 | {\r | |
106 | return cmp(a, b) < 0 ?\r | |
107 | (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))\r | |
108 | :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));\r | |
109 | }\r | |
110 | \r | |
111 | /* The qsort function sorts an array of nmemb objects, the initial element of\r | |
112 | which is pointed to by base. The size of each object is specified by size.\r | |
113 | \r | |
114 | The contents of the array are sorted into ascending order according to a\r | |
115 | comparison function pointed to by compar, which is called with two\r | |
116 | arguments that point to the objects being compared. The function shall\r | |
117 | return an integer less than, equal to, or greater than zero if the first\r | |
118 | argument is considered to be respectively less than, equal to, or greater\r | |
119 | than the second.\r | |
120 | \r | |
121 | If two elements compare as equal, their order in the resulting sorted array\r | |
122 | is unspecified.\r | |
123 | */\r | |
124 | void\r | |
125 | qsort(void *a, size_t n, size_t es, cmp_t *cmp)\r | |
126 | {\r | |
127 | char *pa, *pb, *pc, *pd, *pl, *pm, *pn;\r | |
128 | size_t d, r;\r | |
129 | int cmp_result;\r | |
130 | int swaptype, swap_cnt;\r | |
131 | \r | |
132 | loop: SWAPINIT(a, es);\r | |
133 | swap_cnt = 0;\r | |
134 | if (n < 7) {\r | |
135 | for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)\r | |
136 | for (pl = pm;\r | |
137 | pl > (char *)a && cmp(pl - es, pl) > 0;\r | |
138 | pl -= es)\r | |
139 | swap(pl, pl - es);\r | |
140 | return;\r | |
141 | }\r | |
142 | pm = (char *)a + (n / 2) * es;\r | |
143 | if (n > 7) {\r | |
144 | pl = a;\r | |
145 | pn = (char *)a + (n - 1) * es;\r | |
146 | if (n > 40) {\r | |
147 | d = (n / 8) * es;\r | |
148 | pl = med3(pl, pl + d, pl + 2 * d, cmp);\r | |
149 | pm = med3(pm - d, pm, pm + d, cmp);\r | |
150 | pn = med3(pn - 2 * d, pn - d, pn, cmp);\r | |
151 | }\r | |
152 | pm = med3(pl, pm, pn, cmp);\r | |
153 | }\r | |
154 | swap(a, pm);\r | |
155 | pa = pb = (char *)a + es;\r | |
156 | \r | |
157 | pc = pd = (char *)a + (n - 1) * es;\r | |
158 | for (;;) {\r | |
159 | while (pb <= pc && (cmp_result = cmp(pb, a)) <= 0) {\r | |
160 | if (cmp_result == 0) {\r | |
161 | swap_cnt = 1;\r | |
162 | swap(pa, pb);\r | |
163 | pa += es;\r | |
164 | }\r | |
165 | pb += es;\r | |
166 | }\r | |
167 | while (pb <= pc && (cmp_result = cmp(pc, a)) >= 0) {\r | |
168 | if (cmp_result == 0) {\r | |
169 | swap_cnt = 1;\r | |
170 | swap(pc, pd);\r | |
171 | pd -= es;\r | |
172 | }\r | |
173 | pc -= es;\r | |
174 | }\r | |
175 | if (pb > pc)\r | |
176 | break;\r | |
177 | swap(pb, pc);\r | |
178 | swap_cnt = 1;\r | |
179 | pb += es;\r | |
180 | pc -= es;\r | |
181 | }\r | |
182 | if (swap_cnt == 0) { /* Switch to insertion sort */\r | |
183 | for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)\r | |
184 | for (pl = pm;\r | |
185 | pl > (char *)a && cmp(pl - es, pl) > 0;\r | |
186 | pl -= es)\r | |
187 | swap(pl, pl - es);\r | |
188 | return;\r | |
189 | }\r | |
190 | \r | |
191 | pn = (char *)a + n * es;\r | |
192 | r = MIN(pa - (char *)a, pb - pa);\r | |
193 | vecswap(a, pb - r, r);\r | |
194 | r = MIN((size_t)(pd - pc), ((size_t)(pn - pd)) - es);\r | |
195 | vecswap(pb, pn - r, r);\r | |
196 | if ((size_t)(r = pb - pa) > es)\r | |
197 | qsort(a, r / es, es, cmp);\r | |
198 | if ((size_t)(r = pd - pc) > es) {\r | |
199 | /* Iterate rather than recurse to save stack space */\r | |
200 | a = pn - r;\r | |
201 | n = r / es;\r | |
202 | goto loop;\r | |
203 | }\r | |
204 | /* qsort(pn - r, r / es, es, cmp);*/\r | |
205 | }\r |