Merge branch 'macosx-remove-fixme' into 'master'
[glib.git] / glib / gqsort.c
blob90e65aefe19ab9de5436341acf7e051daa50279b
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1991, 1992, 1996, 1997,1999,2004 Free Software Foundation, Inc.
3 * Copyright (C) 2000 Eazel, Inc.
4 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "config.h"
22 #include <limits.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "galloca.h"
26 #include "gmem.h"
28 #include "gqsort.h"
30 #include "gtestutils.h"
32 /* This file was originally from stdlib/msort.c in gnu libc, just changed
33 to build inside glib and to not fall back to an unstable quicksort
34 for large arrays. */
36 /* An alternative to qsort, with an identical interface.
37 This file is part of the GNU C Library.
38 Copyright (C) 1992,95-97,99,2000,01,02,04,07 Free Software Foundation, Inc.
39 Written by Mike Haertel, September 1988.
41 The GNU C Library is free software; you can redistribute it and/or
42 modify it under the terms of the GNU Lesser General Public
43 License as published by the Free Software Foundation; either
44 version 2.1 of the License, or (at your option) any later version.
46 The GNU C Library is distributed in the hope that it will be useful,
47 but WITHOUT ANY WARRANTY; without even the implied warranty of
48 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
49 Lesser General Public License for more details.
51 You should have received a copy of the GNU Lesser General Public
52 License along with the GNU C Library; if not, see
53 <http://www.gnu.org/licenses/>. */
56 struct msort_param
58 size_t s;
59 size_t var;
60 GCompareDataFunc cmp;
61 void *arg;
62 char *t;
65 static void msort_with_tmp (const struct msort_param *p, void *b, size_t n);
67 static void
68 msort_with_tmp (const struct msort_param *p, void *b, size_t n)
70 char *b1, *b2;
71 size_t n1, n2;
72 char *tmp = p->t;
73 const size_t s = p->s;
74 GCompareDataFunc cmp = p->cmp;
75 void *arg = p->arg;
77 if (n <= 1)
78 return;
80 n1 = n / 2;
81 n2 = n - n1;
82 b1 = b;
83 b2 = (char *) b + (n1 * p->s);
85 msort_with_tmp (p, b1, n1);
86 msort_with_tmp (p, b2, n2);
88 switch (p->var)
90 case 0:
91 while (n1 > 0 && n2 > 0)
93 if ((*cmp) (b1, b2, arg) <= 0)
95 *(guint32 *) tmp = *(guint32 *) b1;
96 b1 += sizeof (guint32);
97 --n1;
99 else
101 *(guint32 *) tmp = *(guint32 *) b2;
102 b2 += sizeof (guint32);
103 --n2;
105 tmp += sizeof (guint32);
107 break;
108 case 1:
109 while (n1 > 0 && n2 > 0)
111 if ((*cmp) (b1, b2, arg) <= 0)
113 *(guint64 *) tmp = *(guint64 *) b1;
114 b1 += sizeof (guint64);
115 --n1;
117 else
119 *(guint64 *) tmp = *(guint64 *) b2;
120 b2 += sizeof (guint64);
121 --n2;
123 tmp += sizeof (guint64);
125 break;
126 case 2:
127 while (n1 > 0 && n2 > 0)
129 unsigned long *tmpl = (unsigned long *) tmp;
130 unsigned long *bl;
132 tmp += s;
133 if ((*cmp) (b1, b2, arg) <= 0)
135 bl = (unsigned long *) b1;
136 b1 += s;
137 --n1;
139 else
141 bl = (unsigned long *) b2;
142 b2 += s;
143 --n2;
145 while (tmpl < (unsigned long *) tmp)
146 *tmpl++ = *bl++;
148 break;
149 case 3:
150 while (n1 > 0 && n2 > 0)
152 if ((*cmp) (*(const void **) b1, *(const void **) b2, arg) <= 0)
154 *(void **) tmp = *(void **) b1;
155 b1 += sizeof (void *);
156 --n1;
158 else
160 *(void **) tmp = *(void **) b2;
161 b2 += sizeof (void *);
162 --n2;
164 tmp += sizeof (void *);
166 break;
167 default:
168 while (n1 > 0 && n2 > 0)
170 if ((*cmp) (b1, b2, arg) <= 0)
172 memcpy (tmp, b1, s);
173 tmp += s;
174 b1 += s;
175 --n1;
177 else
179 memcpy (tmp, b2, s);
180 tmp += s;
181 b2 += s;
182 --n2;
185 break;
188 if (n1 > 0)
189 memcpy (tmp, b1, n1 * s);
190 memcpy (b, p->t, (n - n2) * s);
194 static void
195 msort_r (void *b, size_t n, size_t s, GCompareDataFunc cmp, void *arg)
197 size_t size = n * s;
198 char *tmp = NULL;
199 struct msort_param p;
201 /* For large object sizes use indirect sorting. */
202 if (s > 32)
203 size = 2 * n * sizeof (void *) + s;
205 if (size < 1024)
206 /* The temporary array is small, so put it on the stack. */
207 p.t = g_alloca (size);
208 else
210 /* It's large, so malloc it. */
211 tmp = g_malloc (size);
212 p.t = tmp;
215 p.s = s;
216 p.var = 4;
217 p.cmp = cmp;
218 p.arg = arg;
220 if (s > 32)
222 /* Indirect sorting. */
223 char *ip = (char *) b;
224 void **tp = (void **) (p.t + n * sizeof (void *));
225 void **t = tp;
226 void *tmp_storage = (void *) (tp + n);
227 char *kp;
228 size_t i;
230 while ((void *) t < tmp_storage)
232 *t++ = ip;
233 ip += s;
235 p.s = sizeof (void *);
236 p.var = 3;
237 msort_with_tmp (&p, p.t + n * sizeof (void *), n);
239 /* tp[0] .. tp[n - 1] is now sorted, copy around entries of
240 the original array. Knuth vol. 3 (2nd ed.) exercise 5.2-10. */
241 for (i = 0, ip = (char *) b; i < n; i++, ip += s)
242 if ((kp = tp[i]) != ip)
244 size_t j = i;
245 char *jp = ip;
246 memcpy (tmp_storage, ip, s);
250 size_t k = (kp - (char *) b) / s;
251 tp[j] = jp;
252 memcpy (jp, kp, s);
253 j = k;
254 jp = kp;
255 kp = tp[k];
257 while (kp != ip);
259 tp[j] = jp;
260 memcpy (jp, tmp_storage, s);
263 else
265 if ((s & (sizeof (guint32) - 1)) == 0
266 && ((char *) b - (char *) 0) % ALIGNOF_GUINT32 == 0)
268 if (s == sizeof (guint32))
269 p.var = 0;
270 else if (s == sizeof (guint64)
271 && ((char *) b - (char *) 0) % ALIGNOF_GUINT64 == 0)
272 p.var = 1;
273 else if ((s & (sizeof (unsigned long) - 1)) == 0
274 && ((char *) b - (char *) 0)
275 % ALIGNOF_UNSIGNED_LONG == 0)
276 p.var = 2;
278 msort_with_tmp (&p, b, n);
280 g_free (tmp);
284 * g_qsort_with_data:
285 * @pbase: (not nullable): start of array to sort
286 * @total_elems: elements in the array
287 * @size: size of each element
288 * @compare_func: function to compare elements
289 * @user_data: data to pass to @compare_func
291 * This is just like the standard C qsort() function, but
292 * the comparison routine accepts a user data argument.
294 * This is guaranteed to be a stable sort since version 2.32.
296 void
297 g_qsort_with_data (gconstpointer pbase,
298 gint total_elems,
299 gsize size,
300 GCompareDataFunc compare_func,
301 gpointer user_data)
303 msort_r ((gpointer)pbase, total_elems, size, compare_func, user_data);