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/>.
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
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/>. */
65 static void msort_with_tmp (const struct msort_param
*p
, void *b
, size_t n
);
68 msort_with_tmp (const struct msort_param
*p
, void *b
, size_t n
)
73 const size_t s
= p
->s
;
74 GCompareDataFunc cmp
= p
->cmp
;
83 b2
= (char *) b
+ (n1
* p
->s
);
85 msort_with_tmp (p
, b1
, n1
);
86 msort_with_tmp (p
, b2
, n2
);
91 while (n1
> 0 && n2
> 0)
93 if ((*cmp
) (b1
, b2
, arg
) <= 0)
95 *(guint32
*) tmp
= *(guint32
*) b1
;
96 b1
+= sizeof (guint32
);
101 *(guint32
*) tmp
= *(guint32
*) b2
;
102 b2
+= sizeof (guint32
);
105 tmp
+= sizeof (guint32
);
109 while (n1
> 0 && n2
> 0)
111 if ((*cmp
) (b1
, b2
, arg
) <= 0)
113 *(guint64
*) tmp
= *(guint64
*) b1
;
114 b1
+= sizeof (guint64
);
119 *(guint64
*) tmp
= *(guint64
*) b2
;
120 b2
+= sizeof (guint64
);
123 tmp
+= sizeof (guint64
);
127 while (n1
> 0 && n2
> 0)
129 unsigned long *tmpl
= (unsigned long *) tmp
;
133 if ((*cmp
) (b1
, b2
, arg
) <= 0)
135 bl
= (unsigned long *) b1
;
141 bl
= (unsigned long *) b2
;
145 while (tmpl
< (unsigned long *) tmp
)
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 *);
160 *(void **) tmp
= *(void **) b2
;
161 b2
+= sizeof (void *);
164 tmp
+= sizeof (void *);
168 while (n1
> 0 && n2
> 0)
170 if ((*cmp
) (b1
, b2
, arg
) <= 0)
189 memcpy (tmp
, b1
, n1
* s
);
190 memcpy (b
, p
->t
, (n
- n2
) * s
);
195 msort_r (void *b
, size_t n
, size_t s
, GCompareDataFunc cmp
, void *arg
)
199 struct msort_param p
;
201 /* For large object sizes use indirect sorting. */
203 size
= 2 * n
* sizeof (void *) + s
;
206 /* The temporary array is small, so put it on the stack. */
207 p
.t
= g_alloca (size
);
210 /* It's large, so malloc it. */
211 tmp
= g_malloc (size
);
222 /* Indirect sorting. */
223 char *ip
= (char *) b
;
224 void **tp
= (void **) (p
.t
+ n
* sizeof (void *));
226 void *tmp_storage
= (void *) (tp
+ n
);
230 while ((void *) t
< tmp_storage
)
235 p
.s
= sizeof (void *);
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
)
246 memcpy (tmp_storage
, ip
, s
);
250 size_t k
= (kp
- (char *) b
) / s
;
260 memcpy (jp
, tmp_storage
, s
);
265 if ((s
& (sizeof (guint32
) - 1)) == 0
266 && ((char *) b
- (char *) 0) % ALIGNOF_GUINT32
== 0)
268 if (s
== sizeof (guint32
))
270 else if (s
== sizeof (guint64
)
271 && ((char *) b
- (char *) 0) % ALIGNOF_GUINT64
== 0)
273 else if ((s
& (sizeof (unsigned long) - 1)) == 0
274 && ((char *) b
- (char *) 0)
275 % ALIGNOF_UNSIGNED_LONG
== 0)
278 msort_with_tmp (&p
, b
, n
);
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.
297 g_qsort_with_data (gconstpointer pbase
,
300 GCompareDataFunc compare_func
,
303 msort_r ((gpointer
)pbase
, total_elems
, size
, compare_func
, user_data
);