1 extern "C" {
2 /* Copyright (c) 2007-2014 Massachusetts Institute of Technology
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "nlopt-util.h"
25 
26 /* Simple replacement for the BSD qsort_r function (re-entrant sorting),
27    if it is not available.
28 
29    (glibc 2.8 included a qsort_r function as well, but totally
30    *%&$#-ed things up by gratuitously changing the argument order, in
31    such a way as to allow code using the BSD ordering to compile but
32    die a flaming death at runtime.  Damn them all to Hell, I'll just
33    use my own implementation.)
34 
35    (Actually, with glibc 2.3.6 on my Intel Core Duo, my implementation
36    below seems to be significantly faster than qsort.  Go figure.)
37 */
38 
39 #ifndef HAVE_QSORT_R_damn_it_use_my_own
40 /* swap size bytes between a_ and b_ */
swap(void * a_,void * b_,size_t size)41 static void swap(void *a_, void *b_, size_t size)
42 {
43      if (a_ == b_) 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 
nlopt_qsort_r(void * base_,size_t nmemb,size_t size,void * thunk,int (* compar)(void *,const void *,const void *))68 void nlopt_qsort_r(void *base_, size_t nmemb, size_t size, void *thunk,
69 		   int (*compar)(void *, const void *, const void *))
70 {
71 #ifdef HAVE_QSORT_R_damn_it_use_my_own
72      /* Even if we could detect glibc vs. BSD by appropriate
73 	macrology, there is no way to make the calls compatible
74 	without writing a wrapper for the compar function...screw
75 	this. */
76      qsort_r(base_, nmemb, size, thunk, compar);
77 #else
78      char *base = (char *) base_;
79      if (nmemb < 10) { /* use O(nmemb^2) algorithm for small enough nmemb */
80 	  size_t i, j;
81 	  for (i = 0; i+1 < nmemb; ++i)
82 	       for (j = i+1; j < nmemb; ++j)
83 		    if (compar(thunk, base+i*size, base+j*size) > 0)
84 			 swap(base+i*size, base+j*size, size);
85      }
86      else {
87 	  size_t i, pivot, npart;
88 	  /* pick median of first/middle/last elements as pivot */
89 	  {
90 	       const char *a = base, *b = base + (nmemb/2)*size,
91 		    *c = base + (nmemb-1)*size;
92 	       pivot = compar(thunk,a,b) < 0
93 		    ? (compar(thunk,b,c) < 0 ? nmemb/2 :
94 		       (compar(thunk,a,c) < 0 ? nmemb-1 : 0))
95 		    : (compar(thunk,a,c) < 0 ? 0 :
96 		       (compar(thunk,b,c) < 0 ? nmemb-1 : nmemb/2));
97 	  }
98 	  /* partition array */
99 	  swap(base + pivot*size, base + (nmemb-1) * size, size);
100 	  pivot = (nmemb - 1) * size;
101 	  for (i = npart = 0; i < nmemb-1; ++i)
102 	       if (compar(thunk, base+i*size, base+pivot) <= 0)
103 		    swap(base+i*size, base+(npart++)*size, size);
104 	  swap(base+npart*size, base+pivot, size);
105 	  /* recursive sort of two partitions */
106 	  nlopt_qsort_r(base, npart, size, thunk, compar);
107 	  npart++; /* don't need to sort pivot */
108 	  nlopt_qsort_r(base+npart*size, nmemb-npart, size, thunk, compar);
109      }
110 #endif /* !HAVE_QSORT_R */
111 }
112 }
113