chiark / gitweb /
untabify source files, make indenting more uniform with GNU indent -kr --no-tabs...
[nlopt.git] / src / util / qsort_r.c
1 /* Copyright (c) 2007-2014 Massachusetts Institute of Technology
2  *
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:
10  * 
11  * The above copyright notice and this permission notice shall be
12  * included in all copies or substantial portions of the Software.
13  * 
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. 
21  */
22
23 #include "nlopt-util.h"
24
25 /* Simple replacement for the BSD qsort_r function (re-entrant sorting),
26    if it is not available.
27
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.)
33
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.)
36 */
37
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)
41 {
42     if (a_ == b_)
43         return;
44     {
45         size_t i, nlong = size / sizeof(long);
46         long *a = (long *) a_, *b = (long *) b_;
47         for (i = 0; i < nlong; ++i) {
48             long c = a[i];
49             a[i] = b[i];
50             b[i] = c;
51         }
52         a_ = (void *) (a + nlong);
53         b_ = (void *) (b + nlong);
54     }
55     {
56         size_t i;
57         char *a = (char *) a_, *b = (char *) b_;
58         size = size % sizeof(long);
59         for (i = 0; i < size; ++i) {
60             char c = a[i];
61             a[i] = b[i];
62             b[i] = c;
63         }
64     }
65 }
66 #endif                          /* HAVE_QSORT_R */
67
68 void nlopt_qsort_r(void *base_, size_t nmemb, size_t size, void *thunk, int (*compar) (void *, const void *, const void *))
69 {
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
74        this. */
75     qsort_r(base_, nmemb, size, thunk, compar);
76 #else
77     char *base = (char *) base_;
78     if (nmemb < 10) {           /* use O(nmemb^2) algorithm for small enough nmemb */
79         size_t i, j;
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);
84     } else {
85         size_t i, pivot, npart;
86         /* pick median of first/middle/last elements as pivot */
87         {
88             const char *a = base, *b = base + (nmemb / 2) * size, *c = base + (nmemb - 1) * size;
89             pivot = compar(thunk, a, b) < 0 ? (compar(thunk, b, c) < 0 ? nmemb / 2 : (compar(thunk, a, c) < 0 ? nmemb - 1 : 0))
90                 : (compar(thunk, a, c) < 0 ? 0 : (compar(thunk, b, c) < 0 ? nmemb - 1 : nmemb / 2));
91         }
92         /* partition array */
93         swap(base + pivot * size, base + (nmemb - 1) * size, size);
94         pivot = (nmemb - 1) * size;
95         for (i = npart = 0; i < nmemb - 1; ++i)
96             if (compar(thunk, base + i * size, base + pivot) <= 0)
97                 swap(base + i * size, base + (npart++) * size, size);
98         swap(base + npart * size, base + pivot, size);
99         /* recursive sort of two partitions */
100         nlopt_qsort_r(base, npart, size, thunk, compar);
101         npart++;                /* don't need to sort pivot */
102         nlopt_qsort_r(base + npart * size, nmemb - npart, size, thunk, compar);
103     }
104 #endif                          /* !HAVE_QSORT_R */
105 }