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.
23 #include "nlopt-util.h"
25 /* Simple replacement for the BSD qsort_r function (re-entrant sorting),
26 if it is not available.
28 (glibc 2.8 included a qsort_r function as well, but totally
29 *%&$#-ed things up by gratuitously changing the argument order, in
30 such a way as to allow code using the BSD ordering to compile but
31 die a flaming death at runtime. Damn them all to Hell, I'll just
32 use my own implementation.)
34 (Actually, with glibc 2.3.6 on my Intel Core Duo, my implementation
35 below seems to be significantly faster than qsort. Go figure.)
38 #ifndef HAVE_QSORT_R_damn_it_use_my_own
39 /* swap size bytes between a_ and b_ */
40 static void swap(void *a_, void *b_, size_t size)
44 size_t i, nlong = size / sizeof(long);
45 long *a = (long *) a_, *b = (long *) b_;
46 for (i = 0; i < nlong; ++i) {
51 a_ = (void*) (a + nlong);
52 b_ = (void*) (b + nlong);
56 char *a = (char *) a_, *b = (char *) b_;
57 size = size % sizeof(long);
58 for (i = 0; i < size; ++i) {
65 #endif /* HAVE_QSORT_R */
67 void nlopt_qsort_r(void *base_, size_t nmemb, size_t size, void *thunk,
68 int (*compar)(void *, const void *, const void *))
70 #ifdef HAVE_QSORT_R_damn_it_use_my_own
71 /* Even if we could detect glibc vs. BSD by appropriate
72 macrology, there is no way to make the calls compatible
73 without writing a wrapper for the compar function...screw
75 qsort_r(base_, nmemb, size, thunk, compar);
77 char *base = (char *) base_;
78 if (nmemb < 10) { /* use O(nmemb^2) algorithm for small enough nmemb */
80 for (i = 0; i+1 < nmemb; ++i)
81 for (j = i+1; j < nmemb; ++j)
82 if (compar(thunk, base+i*size, base+j*size) > 0)
83 swap(base+i*size, base+j*size, size);
86 size_t i, pivot, npart;
87 /* pick median of first/middle/last elements as pivot */
89 const char *a = base, *b = base + (nmemb/2)*size,
90 *c = base + (nmemb-1)*size;
91 pivot = compar(thunk,a,b) < 0
92 ? (compar(thunk,b,c) < 0 ? nmemb/2 :
93 (compar(thunk,a,c) < 0 ? nmemb-1 : 0))
94 : (compar(thunk,a,c) < 0 ? 0 :
95 (compar(thunk,b,c) < 0 ? nmemb-1 : nmemb/2));
98 swap(base + pivot*size, base + (nmemb-1) * size, size);
99 pivot = (nmemb - 1) * size;
100 for (i = npart = 0; i < nmemb-1; ++i)
101 if (compar(thunk, base+i*size, base+pivot) <= 0)
102 swap(base+i*size, base+(npart++)*size, size);
103 swap(base+npart*size, base+pivot, size);
104 /* recursive sort of two partitions */
105 nlopt_qsort_r(base, npart, size, thunk, compar);
106 npart++; /* don't need to sort pivot */
107 nlopt_qsort_r(base+npart*size, nmemb-npart, size, thunk, compar);
109 #endif /* !HAVE_QSORT_R */