1 /* Copyright (c) 2007-2014 Massachusetts Institute of Technology
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
11 * The above copyright notice and this permission notice shall be
12 * included in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #include "nlopt-util.h"
26 #define inline __inline
29 typedef int cmp_t(void *, const void *, const void *);
31 static inline char *med3(char *, char *, char *, cmp_t *, void *);
33 #define MIN(a, b) ((a) < (b) ? a : b)
36 * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
40 swapfunc(char *a, char *b, size_t es)
51 #define vecswap(a, b, n) \
52 if ((n) > 0) swapfunc(a, b, n)
54 #define CMP(t, x, y) (cmp((t), (x), (y)))
57 med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk)
59 return CMP(thunk, a, b) < 0 ?
60 (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a ))
61 :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c ));
65 void qsort_r_fallback(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp)
67 char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
75 for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
77 pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
79 swapfunc(pl, pl - es, es);
82 pm = (char *)a + (n / 2) * es;
85 pn = (char *)a + (n - 1) * es;
87 size_t d = (n / 8) * es;
89 pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk);
90 pm = med3(pm - d, pm, pm + d, cmp, thunk);
91 pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk);
93 pm = med3(pl, pm, pn, cmp, thunk);
96 pa = pb = (char *)a + es;
98 pc = pd = (char *)a + (n - 1) * es;
100 while (pb <= pc && (cmp_result = CMP(thunk, pb, a)) <= 0) {
101 if (cmp_result == 0) {
103 swapfunc(pa, pb, es);
108 while (pb <= pc && (cmp_result = CMP(thunk, pc, a)) >= 0) {
109 if (cmp_result == 0) {
111 swapfunc(pc, pd, es);
118 swapfunc(pb, pc, es);
123 if (swap_cnt == 0) { /* Switch to insertion sort */
124 for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
126 pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
128 swapfunc(pl, pl - es, es);
132 pn = (char *)a + n * es;
133 d1 = MIN(pa - (char *)a, pb - pa);
134 vecswap(a, pb - d1, d1);
135 d1 = MIN(pd - pc, pn - pd - es);
136 vecswap(pb, pn - d1, d1);
141 /* Recurse on left partition, then iterate on right partition */
143 qsort_r_fallback(a, d1 / es, es, thunk, cmp);
146 /* Iterate rather than recurse to save stack space */
147 /* qsort(pn - d2, d2 / es, es, cmp); */
153 /* Recurse on right partition, then iterate on left partition */
155 qsort_r_fallback(pn - d2, d2 / es, es, thunk, cmp);
158 /* Iterate rather than recurse to save stack space */
159 /* qsort(a, d1 / es, es, cmp); */
166 /* these are required for GNU api compatibility as nlopt uses the BSD arguments ordering */
172 static int qsort_cmp_wrap(const void *a, const void *b, void *thunk)
174 qsort_wrapper *wrap = (qsort_wrapper *) thunk;
175 return (*wrap->compar)(wrap->thunk, a, b);
178 void nlopt_qsort_r(void *base_, size_t nmemb, size_t size, void *thunk, cmp_t* compar)
180 #if defined(HAVE_QSORT_R) && (defined(__APPLE__) || defined(__FreeBSD__))
181 qsort_r(base_, nmemb, size, thunk, compar);
182 #elif defined(HAVE_QSORT_R) && defined(__linux__)
183 qsort_wrapper wrapper;
184 wrapper.compar = compar;
185 wrapper.thunk = thunk;
186 qsort_r(base_, nmemb, size, qsort_cmp_wrap, &wrapper);
187 /*#elif defined(_WIN32)
188 qsort_s(base_, nmemb, size, compar, thunk);*/
190 qsort_r_fallback(base_, nmemb, size, thunk, compar);