chiark / gitweb /
work around broken solaris gcc
[nlopt.git] / util / qsort_r.c
1 /* Copyright (c) 2007-2008 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.  (It looks like glibc 2.8 will include qsort_r
27    as well.) 
28
29    (Actually, with glibc 2.3.6 on my Intel Core Duo, my implementation
30    below seems to be significantly faster than qsort.  Go figure.
31    Even so, I'd rather use the libc version of qsort_r if it is available,
32    on general principles, and to reduce code bloat.) */
33
34 #ifndef HAVE_QSORT_R
35 /* swap size bytes between a_ and b_ */
36 static void swap(void *a_, void *b_, size_t size)
37 {
38      if (a_ == b_) return;
39      {
40           size_t i, nlong = size / sizeof(long);
41           long *a = (long *) a_, *b = (long *) b_;
42           for (i = 0; i < nlong; ++i) {
43                long c = a[i];
44                a[i] = b[i];
45                b[i] = c;
46           }
47           a_ = (void*) (a + nlong);
48           b_ = (void*) (b + nlong);
49      }
50      {
51           size_t i;
52           char *a = (char *) a_, *b = (char *) b_;
53           size = size % sizeof(long);
54           for (i = 0; i < size; ++i) {
55                char c = a[i];
56                a[i] = b[i];
57                b[i] = c;
58           }
59      }
60 }
61 #endif /* HAVE_QSORT_R */
62
63 void nlopt_qsort_r(void *base_, size_t nmemb, size_t size, void *thunk,
64                    int (*compar)(void *, const void *, const void *))
65 {
66 #ifdef HAVE_QSORT_R
67      qsort_r(base_, nmemb, size, thunk, compar);
68 #else
69      char *base = (char *) base_;
70      if (nmemb < 10) { /* use O(nmemb^2) algorithm for small enough nmemb */
71           size_t i, j;
72           for (i = 0; i+1 < nmemb; ++i)
73                for (j = i+1; j < nmemb; ++j)
74                     if (compar(thunk, base+i*size, base+j*size) > 0)
75                          swap(base+i*size, base+j*size, size);
76      }
77      else {
78           size_t i, pivot, npart;
79           /* pick median of first/middle/last elements as pivot */
80           {
81                const char *a = base, *b = base + (nmemb/2)*size, 
82                     *c = base + (nmemb-1)*size;
83                pivot = compar(thunk,a,b) < 0
84                     ? (compar(thunk,b,c) < 0 ? nmemb/2 :
85                        (compar(thunk,a,c) < 0 ? nmemb-1 : 0))
86                     : (compar(thunk,a,c) < 0 ? 0 :
87                        (compar(thunk,b,c) < 0 ? nmemb-1 : nmemb/2));
88           }
89           /* partition array */
90           swap(base + pivot*size, base + (nmemb-1) * size, size);
91           pivot = (nmemb - 1) * size;
92           for (i = npart = 0; i < nmemb-1; ++i)
93                if (compar(thunk, base+i*size, base+pivot) <= 0)
94                     swap(base+i*size, base+(npart++)*size, size);
95           swap(base+npart*size, base+pivot, size);
96           /* recursive sort of two partitions */
97           nlopt_qsort_r(base, npart, size, thunk, compar);
98           npart++; /* don't need to sort pivot */
99           nlopt_qsort_r(base+npart*size, nmemb-npart, size, thunk, compar);
100      }
101 #endif /* !HAVE_QSORT_R */
102 }