Major change in API for creating sources to handle multiple main loops
[platform/upstream/glib.git] / gqsort.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1991, 1992, 1996, 1997 Free Software Foundation, Inc.
3  * Copyright (C) 2000 Eazel, Inc.
4  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
5  *
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 of the License, or (at your option) any later version.
10  *
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.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /*
23  * This file was originally part of the GNU C Library, and was modified to allow
24  * user data to be passed in to the sorting function.
25  *
26  * Written by Douglas C. Schmidt (schmidt@ics.uci.edu).
27  * Modified by Maciej Stachowiak (mjs@eazel.com)
28  *
29  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
30  * file for a list of people on the GLib Team.  See the ChangeLog
31  * files for a list of changes.  These files are distributed with
32  * GLib at ftp://ftp.gtk.org/pub/gtk/.  */
33
34 #include <string.h>
35
36 #include "glib.h"
37
38 /* Byte-wise swap two items of size SIZE. */
39 #define SWAP(a, b, size)                                                      \
40   do                                                                          \
41     {                                                                         \
42       register size_t __size = (size);                                        \
43       register char *__a = (a), *__b = (b);                                   \
44       do                                                                      \
45         {                                                                     \
46           char __tmp = *__a;                                                  \
47           *__a++ = *__b;                                                      \
48           *__b++ = __tmp;                                                     \
49         } while (--__size > 0);                                               \
50     } while (0)
51
52 /* Discontinue quicksort algorithm when partition gets below this size.
53    This particular magic number was chosen to work best on a Sun 4/260. */
54 #define MAX_THRESH 4
55
56 /* Stack node declarations used to store unfulfilled partition obligations. */
57 typedef struct
58 {
59   char *lo;
60   char *hi;
61 }
62 stack_node;
63
64 /* The next 4 #defines implement a very fast in-line stack abstraction. */
65 #define STACK_SIZE      (8 * sizeof(unsigned long int))
66 #define PUSH(low, high) ((void) ((top->lo = (low)), (top->hi = (high)), ++top))
67 #define POP(low, high)  ((void) (--top, (low = top->lo), (high = top->hi)))
68 #define STACK_NOT_EMPTY (stack < top)
69
70
71 /* Order size using quicksort.  This implementation incorporates
72  * four optimizations discussed in Sedgewick:
73  *
74  * 1. Non-recursive, using an explicit stack of pointer that store the next
75  *    array partition to sort.  To save time, this maximum amount of space
76  *    required to store an array of MAX_INT is allocated on the stack.  Assuming
77  *    a 32-bit integer, this needs only 32 * sizeof(stack_node) == 136 bits.
78  *    Pretty cheap, actually.
79  *
80  * 2. Chose the pivot element using a median-of-three decision tree.  This
81  *    reduces the probability of selecting a bad pivot value and eliminates
82  *    certain * extraneous comparisons.
83  *
84  * 3. Only quicksorts TOTAL_ELEMS / MAX_THRESH partitions, leaving insertion
85  *    sort to order the MAX_THRESH items within each partition.  This is a big
86  *    win, since insertion sort is faster for small, mostly sorted array
87  *    segments.
88  *
89  * 4. The larger of the two sub-partitions is always pushed onto the stack
90  *    first, with the algorithm then concentrating on the smaller partition.
91  *    This *guarantees* no more than log (n) stack size is needed (actually O(1)
92  *    in this case)!
93  */
94
95 void
96 g_qsort_with_data (gconstpointer    pbase,
97                    gint             total_elems,
98                    size_t           size,
99                    GCompareFuncData compare_func,
100                    gpointer         user_data)
101 {
102   register char *base_ptr = (char *) pbase;
103
104   /* Allocating SIZE bytes for a pivot buffer facilitates a better
105    * algorithm below since we can do comparisons directly on the pivot.
106    */
107   char *pivot_buffer = (char *) alloca (size);
108   const size_t max_thresh = MAX_THRESH * size;
109
110   g_return_if_fail (total_elems > 0);
111   g_return_if_fail (pbase != NULL);
112   g_return_if_fail (compare_func != NULL);
113
114   if (total_elems > MAX_THRESH)
115     {
116       char *lo = base_ptr;
117       char *hi = &lo[size * (total_elems - 1)];
118       /* Largest size needed for 32-bit int!!! */
119       stack_node stack[STACK_SIZE];
120       stack_node *top = stack + 1;
121
122       while (STACK_NOT_EMPTY)
123         {
124           char *left_ptr;
125           char *right_ptr;
126
127           char *pivot = pivot_buffer;
128
129           /* Select median value from among LO, MID, and HI. Rearrange
130            * LO and HI so the three values are sorted. This lowers the
131            * probability of picking a pathological pivot value and
132            * skips a comparison for both the LEFT_PTR and RIGHT_PTR. */
133
134           char *mid = lo + size * ((hi - lo) / size >> 1);
135
136           if ((*compare_func) ((void *) mid, (void *) lo, user_data) < 0)
137             SWAP (mid, lo, size);
138           if ((*compare_func) ((void *) hi, (void *) mid, user_data) < 0)
139             SWAP (mid, hi, size);
140           else
141             goto jump_over;
142           if ((*compare_func) ((void *) mid, (void *) lo, user_data) < 0)
143             SWAP (mid, lo, size);
144         jump_over:;
145           memcpy (pivot, mid, size);
146           pivot = pivot_buffer;
147
148           left_ptr = lo + size;
149           right_ptr = hi - size;
150
151           /* Here's the famous ``collapse the walls'' section of quicksort.
152            * Gotta like those tight inner loops!  They are the main reason
153            * that this algorithm runs much faster than others. */
154           do
155             {
156               while ((*compare_func)
157                      ((void *) left_ptr, (void *) pivot,
158                       user_data) < 0)
159                 left_ptr += size;
160
161               while ((*compare_func)
162                      ((void *) pivot, (void *) right_ptr,
163                       user_data) < 0)
164                 right_ptr -= size;
165
166               if (left_ptr < right_ptr)
167                 {
168                   SWAP (left_ptr, right_ptr, size);
169                   left_ptr += size;
170                   right_ptr -= size;
171                 }
172               else if (left_ptr == right_ptr)
173                 {
174                   left_ptr += size;
175                   right_ptr -= size;
176                   break;
177                 }
178             }
179           while (left_ptr <= right_ptr);
180
181           /* Set up pointers for next iteration.  First determine whether
182            * left and right partitions are below the threshold size.  If so,
183            * ignore one or both.  Otherwise, push the larger partition's
184            * bounds on the stack and continue sorting the smaller one. */
185
186           if ((size_t) (right_ptr - lo) <= max_thresh)
187             {
188               if ((size_t) (hi - left_ptr) <= max_thresh)
189                 /* Ignore both small partitions. */
190                 POP (lo, hi);
191               else
192                 /* Ignore small left partition. */
193                 lo = left_ptr;
194             }
195           else if ((size_t) (hi - left_ptr) <= max_thresh)
196                                 /* Ignore small right partition. */
197             hi = right_ptr;
198           else if ((right_ptr - lo) > (hi - left_ptr))
199             {
200                                 /* Push larger left partition indices. */
201               PUSH (lo, right_ptr);
202               lo = left_ptr;
203
204             }
205           else
206             {
207                                 /* Push larger right partition indices. */
208               PUSH (left_ptr, hi);
209               hi = right_ptr;
210             }
211         }
212     }
213
214   /* Once the BASE_PTR array is partially sorted by quicksort the rest
215    * is completely sorted using insertion sort, since this is efficient
216    * for partitions below MAX_THRESH size. BASE_PTR points to the beginning
217    * of the array to sort, and END_PTR points at the very last element in
218    * the array (*not* one beyond it!). */
219
220   {
221     char *const end_ptr = &base_ptr[size * (total_elems - 1)];
222     char *tmp_ptr = base_ptr;
223     char *thresh = MIN (end_ptr, base_ptr + max_thresh);
224     register char *run_ptr;
225
226     /* Find smallest element in first threshold and place it at the
227      * array's beginning.  This is the smallest array element,
228      * and the operation speeds up insertion sort's inner loop. */
229
230     for (run_ptr = tmp_ptr + size; run_ptr <= thresh;
231          run_ptr +=
232            size) if ((*compare_func) ((void *) run_ptr, (void *) tmp_ptr,
233                                       user_data) < 0)
234              tmp_ptr = run_ptr;
235
236     if (tmp_ptr != base_ptr)
237       SWAP (tmp_ptr, base_ptr, size);
238
239     /* Insertion sort, running from left-hand-side up to right-hand-side.  */
240
241     run_ptr = base_ptr + size;
242     while ((run_ptr += size) <= end_ptr)
243       {
244         tmp_ptr = run_ptr - size;
245         while ((*compare_func)
246                ((void *) run_ptr, (void *) tmp_ptr,
247                 user_data) < 0)
248           tmp_ptr -= size;
249
250         tmp_ptr += size;
251         if (tmp_ptr != run_ptr)
252           {
253             char *trav;
254
255             trav = run_ptr + size;
256             while (--trav >= run_ptr)
257               {
258                 char c = *trav;
259                 char *hi, *lo;
260
261                 for (hi = lo = trav;
262                      (lo -= size) >= tmp_ptr; hi = lo)
263                   *hi = *lo;
264                 *hi = c;
265               }
266           }
267       }
268   }
269 }