+++ /dev/null
-/** @file\r
- Quick Sort utility function.\r
-\r
- This utility makes use of a comparison function to search arrays of\r
- unspecified type. Where an argument declared as size_t nmemb specifies the\r
- length of the array for a function, nmemb can have the value zero on a call\r
- to that function; the comparison function is not called, a search finds no\r
- matching element. Pointer arguments on such a call shall still have valid\r
- values.\r
-\r
- The implementation shall ensure that both arguments of the comparison\r
- function are pointers to elements of the array.\r
-\r
- The comparison function shall not alter the contents of the array. The\r
- implementation may reorder elements of the array between calls to the\r
- comparison function, but shall not alter the contents of any individual\r
- element.\r
-\r
- When the same objects (consisting of size bytes, irrespective of their\r
- current positions in the array) are passed more than once to the comparison\r
- function, the results shall be consistent with one another. That is, they\r
- define a total ordering on the array.\r
-\r
- A sequence point occurs immediately before and immediately after each call to\r
- the comparison function, and also between any call to the comparison function\r
- and any movement of the objects passed as arguments to that call.\r
-\r
- Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
- * Copyright (c) 1992, 1993\r
- * The Regents of the University of California. All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * 4. Neither the name of the University nor the names of its contributors\r
- * may be used to endorse or promote products derived from this software\r
- * without specific prior written permission.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
- * SUCH DAMAGE.\r
-\r
- ("$FreeBSD: src/lib/libc/stdlib/qsort.c,v 1.15.2.1.2.1 2009/10/25 01:10:29 kensmith Exp $");\r
- */\r
-#include <LibConfig.h>\r
-\r
-#include <stdlib.h>\r
-\r
-typedef int cmp_t(const void *, const void *);\r
-\r
-static __inline char *med3(char *, char *, char *, cmp_t *);\r
-static __inline void swapfunc(char *, char *, size_t, int);\r
-\r
-/*\r
- * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".\r
- */\r
-#define swapcode(TYPE, parmi, parmj, n) { \\r
- size_t i = (n) / sizeof (TYPE); \\r
- TYPE *pi = (TYPE *) (parmi); \\r
- TYPE *pj = (TYPE *) (parmj); \\r
- do { \\r
- TYPE t = *pi; \\r
- *pi++ = *pj; \\r
- *pj++ = t; \\r
- } while (--i > 0); \\r
-}\r
-\r
-#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \\r
- es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;\r
-\r
-static __inline void\r
-swapfunc(char *a, char *b, size_t n, int swaptype)\r
-{\r
- if(swaptype <= 1)\r
- swapcode(long, a, b, n)\r
- else\r
- swapcode(char, a, b, n)\r
-}\r
-\r
-#define swap(a, b) \\r
- if (swaptype == 0) { \\r
- long t = *(long *)(a); \\r
- *(long *)(a) = *(long *)(b); \\r
- *(long *)(b) = t; \\r
- } else \\r
- swapfunc(a, b, es, swaptype)\r
-\r
-#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)\r
-\r
-static __inline char *\r
-med3(char *a, char *b, char *c, cmp_t *cmp )\r
-{\r
- return cmp(a, b) < 0 ?\r
- (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))\r
- :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));\r
-}\r
-\r
-/* The qsort function sorts an array of nmemb objects, the initial element of\r
- which is pointed to by base. The size of each object is specified by size.\r
-\r
- The contents of the array are sorted into ascending order according to a\r
- comparison function pointed to by compar, which is called with two\r
- arguments that point to the objects being compared. The function shall\r
- return an integer less than, equal to, or greater than zero if the first\r
- argument is considered to be respectively less than, equal to, or greater\r
- than the second.\r
-\r
- If two elements compare as equal, their order in the resulting sorted array\r
- is unspecified.\r
-*/\r
-void\r
-qsort(void *a, size_t n, size_t es, cmp_t *cmp)\r
-{\r
- char *pa, *pb, *pc, *pd, *pl, *pm, *pn;\r
- size_t d, r;\r
- int cmp_result;\r
- int swaptype, swap_cnt;\r
-\r
-loop: SWAPINIT(a, es);\r
- swap_cnt = 0;\r
- if (n < 7) {\r
- for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)\r
- for (pl = pm;\r
- pl > (char *)a && cmp(pl - es, pl) > 0;\r
- pl -= es)\r
- swap(pl, pl - es);\r
- return;\r
- }\r
- pm = (char *)a + (n / 2) * es;\r
- if (n > 7) {\r
- pl = a;\r
- pn = (char *)a + (n - 1) * es;\r
- if (n > 40) {\r
- d = (n / 8) * es;\r
- pl = med3(pl, pl + d, pl + 2 * d, cmp);\r
- pm = med3(pm - d, pm, pm + d, cmp);\r
- pn = med3(pn - 2 * d, pn - d, pn, cmp);\r
- }\r
- pm = med3(pl, pm, pn, cmp);\r
- }\r
- swap(a, pm);\r
- pa = pb = (char *)a + es;\r
-\r
- pc = pd = (char *)a + (n - 1) * es;\r
- for (;;) {\r
- while (pb <= pc && (cmp_result = cmp(pb, a)) <= 0) {\r
- if (cmp_result == 0) {\r
- swap_cnt = 1;\r
- swap(pa, pb);\r
- pa += es;\r
- }\r
- pb += es;\r
- }\r
- while (pb <= pc && (cmp_result = cmp(pc, a)) >= 0) {\r
- if (cmp_result == 0) {\r
- swap_cnt = 1;\r
- swap(pc, pd);\r
- pd -= es;\r
- }\r
- pc -= es;\r
- }\r
- if (pb > pc)\r
- break;\r
- swap(pb, pc);\r
- swap_cnt = 1;\r
- pb += es;\r
- pc -= es;\r
- }\r
- if (swap_cnt == 0) { /* Switch to insertion sort */\r
- for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)\r
- for (pl = pm;\r
- pl > (char *)a && cmp(pl - es, pl) > 0;\r
- pl -= es)\r
- swap(pl, pl - es);\r
- return;\r
- }\r
-\r
- pn = (char *)a + n * es;\r
- r = MIN(pa - (char *)a, pb - pa);\r
- vecswap(a, pb - r, r);\r
- r = MIN((size_t)(pd - pc), ((size_t)(pn - pd)) - es);\r
- vecswap(pb, pn - r, r);\r
- if ((size_t)(r = pb - pa) > es)\r
- qsort(a, r / es, es, cmp);\r
- if ((size_t)(r = pd - pc) > es) {\r
- /* Iterate rather than recurse to save stack space */\r
- a = pn - r;\r
- n = r / es;\r
- goto loop;\r
- }\r
-/* qsort(pn - r, r / es, es, cmp);*/\r
-}\r