~ubuntu-branches/ubuntu/trusty/scotch/trusty

« back to all changes in this revision

Viewing changes to src/libscotch/common_sort.c

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Prud'homme
  • Date: 2008-01-25 09:13:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080125091353-zghdao60dfsyc2bt
Tags: upstream-5.0.1.dfsg
ImportĀ upstreamĀ versionĀ 5.0.1.dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the Scotch distribution. It does
 
2
** not have the stardard Scotch header with the INRIA
 
3
** copyright notice because it is a very slight adaptation
 
4
** of the qsort routine of glibc 2.4, taylored to match
 
5
** Scotch needs. As Scotch is distributed according to the
 
6
** CeCILL-C license, which is LGPL-compatible, no further
 
7
** notices are required.
 
8
*/
 
9
 
 
10
/* Copyright (C) 1991,1992,1996,1997,1999,2004 Free Software Foundation, Inc.
 
11
   This file is part of the GNU C Library.
 
12
   Written by Douglas C. Schmidt (schmidt@ics.uci.edu).
 
13
 
 
14
   The GNU C Library is free software; you can redistribute it and/or
 
15
   modify it under the terms of the GNU Lesser General Public
 
16
   License as published by the Free Software Foundation; either
 
17
   version 2.1 of the License, or (at your option) any later version.
 
18
 
 
19
   The GNU C Library is distributed in the hope that it will be useful,
 
20
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
22
   Lesser General Public License for more details.
 
23
 
 
24
   You should have received a copy of the GNU Lesser General Public
 
25
   License along with the GNU C Library; if not, write to the Free
 
26
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 
27
   02111-1307 USA.  */
 
28
 
 
29
/* If you consider tuning this algorithm, you should consult first:
 
30
   Engineering a sort function; Jon Bentley and M. Douglas McIlroy;
 
31
   Software - Practice and Experience; Vol. 23 (11), 1249-1265, 1993.  */
 
32
 
 
33
#ifndef MAX_THRESH
 
34
 
 
35
#define MAX_THRESH 6
 
36
 
 
37
#define max_thresh                  (MAX_THRESH * INTSORTSIZE) /* Variable turned into constant */
 
38
 
 
39
/* Stack node declarations used to store unfulfilled partition obligations. */
 
40
typedef struct
 
41
  {
 
42
    char *lo;
 
43
    char *hi;
 
44
  } stack_node;
 
45
 
 
46
/* The next 4 #defines implement a very fast in-line stack abstraction. */
 
47
/* The stack needs log (total_elements) entries (we could even subtract
 
48
   log(MAX_THRESH)).  Since total_elements has type size_t, we get as
 
49
   upper bound for log (total_elements):
 
50
   bits per byte (CHAR_BIT) * sizeof(size_t).  */
 
51
#define STACK_SIZE      (CHAR_BIT * sizeof (INT))
 
52
#define PUSH(low, high) ((void) ((top->lo = (low)), (top->hi = (high)), ++top))
 
53
#define POP(low, high)  ((void) (--top, (low = top->lo), (high = top->hi)))
 
54
#define STACK_NOT_EMPTY (stack < top)
 
55
 
 
56
#endif /* MAX_THRESH */
 
57
 
 
58
/* Order size using quicksort.  This implementation incorporates
 
59
   four optimizations discussed in Sedgewick:
 
60
 
 
61
   1. Non-recursive, using an explicit stack of pointer that store the
 
62
      next array partition to sort.  To save time, this maximum amount
 
63
      of space required to store an array of SIZE_MAX is allocated on the
 
64
      stack.  Assuming a 32-bit (64 bit) integer for size_t, this needs
 
65
      only 32 * sizeof(stack_node) == 256 bytes (for 64 bit: 1024 bytes).
 
66
      Pretty cheap, actually.
 
67
 
 
68
   2. Chose the pivot element using a median-of-three decision tree.
 
69
      This reduces the probability of selecting a bad pivot value and
 
70
      eliminates certain extraneous comparisons.
 
71
 
 
72
   3. Only quicksorts TOTAL_ELEMS / MAX_THRESH partitions, leaving
 
73
      insertion sort to order the MAX_THRESH items within each partition.
 
74
      This is a big win, since insertion sort is faster for small, mostly
 
75
      sorted array segments.
 
76
 
 
77
   4. The larger of the two sub-partitions is always pushed onto the
 
78
      stack first, with the algorithm then concentrating on the
 
79
      smaller partition.  This *guarantees* no more than log (total_elems)
 
80
      stack size is needed (actually O(1) in this case)!  */
 
81
 
 
82
/* To be defined :
 
83
** INTSORTNAME : Name of function
 
84
** INTSORTSIZE : Size of elements to sort
 
85
** INTSORTSWAP : Swapping macro
 
86
** INTSORTCMP  : Comparison function
 
87
*/
 
88
 
 
89
void
 
90
INTSORTNAME (
 
91
void * const                pbase,                /*+ Array to sort             +*/
 
92
const INT                   total_elems)          /*+ Number of entries to sort +*/
 
93
{
 
94
  register char *base_ptr = (char *) pbase;
 
95
 
 
96
  if (total_elems == 0)
 
97
    /* Avoid lossage with unsigned arithmetic below.  */
 
98
    return;
 
99
 
 
100
  if (total_elems > MAX_THRESH)
 
101
    {
 
102
      char *lo = base_ptr;
 
103
      char *hi = &lo[INTSORTSIZE * (total_elems - 1)];
 
104
      stack_node stack[STACK_SIZE];
 
105
      stack_node *top = stack;
 
106
 
 
107
      PUSH (NULL, NULL);
 
108
 
 
109
      while (STACK_NOT_EMPTY)
 
110
        {
 
111
          char *left_ptr;
 
112
          char *right_ptr;
 
113
 
 
114
          /* Select median value from among LO, MID, and HI. Rearrange
 
115
             LO and HI so the three values are sorted. This lowers the
 
116
             probability of picking a pathological pivot value and
 
117
             skips a comparison for both the LEFT_PTR and RIGHT_PTR in
 
118
             the while loops. */
 
119
 
 
120
          char *mid = lo + INTSORTSIZE * ((hi - lo) / INTSORTSIZE >> 1);
 
121
 
 
122
          if (INTSORTCMP ((void *) mid, (void *) lo))
 
123
            INTSORTSWAP (mid, lo);
 
124
          if (INTSORTCMP ((void *) hi, (void *) mid))
 
125
            INTSORTSWAP (mid, hi);
 
126
          else
 
127
            goto jump_over;
 
128
          if (INTSORTCMP ((void *) mid, (void *) lo))
 
129
            INTSORTSWAP (mid, lo);
 
130
        jump_over:;
 
131
 
 
132
          left_ptr  = lo + INTSORTSIZE;
 
133
          right_ptr = hi - INTSORTSIZE;
 
134
 
 
135
          /* Here's the famous ``collapse the walls'' section of quicksort.
 
136
             Gotta like those tight inner loops!  They are the main reason
 
137
             that this algorithm runs much faster than others. */
 
138
          do
 
139
            {
 
140
              while (INTSORTCMP ((void *) left_ptr, (void *) mid))
 
141
                left_ptr += INTSORTSIZE;
 
142
 
 
143
              while (INTSORTCMP ((void *) mid, (void *) right_ptr))
 
144
                right_ptr -= INTSORTSIZE;
 
145
 
 
146
              if (left_ptr < right_ptr)
 
147
                {
 
148
                  INTSORTSWAP (left_ptr, right_ptr);
 
149
                  if (mid == left_ptr)
 
150
                    mid = right_ptr;
 
151
                  else if (mid == right_ptr)
 
152
                    mid = left_ptr;
 
153
                  left_ptr += INTSORTSIZE;
 
154
                  right_ptr -= INTSORTSIZE;
 
155
                }
 
156
              else if (left_ptr == right_ptr)
 
157
                {
 
158
                  left_ptr += INTSORTSIZE;
 
159
                  right_ptr -= INTSORTSIZE;
 
160
                  break;
 
161
                }
 
162
            }
 
163
          while (left_ptr <= right_ptr);
 
164
 
 
165
          /* Set up pointers for next iteration.  First determine whether
 
166
             left and right partitions are below the threshold size.  If so,
 
167
             ignore one or both.  Otherwise, push the larger partition's
 
168
             bounds on the stack and continue sorting the smaller one. */
 
169
 
 
170
          if ((size_t) (right_ptr - lo) <= max_thresh)
 
171
            {
 
172
              if ((size_t) (hi - left_ptr) <= max_thresh)
 
173
                /* Ignore both small partitions. */
 
174
                POP (lo, hi);
 
175
              else
 
176
                /* Ignore small left partition. */
 
177
                lo = left_ptr;
 
178
            }
 
179
          else if ((size_t) (hi - left_ptr) <= max_thresh)
 
180
            /* Ignore small right partition. */
 
181
            hi = right_ptr;
 
182
          else if ((right_ptr - lo) > (hi - left_ptr))
 
183
            {
 
184
              /* Push larger left partition indices. */
 
185
              PUSH (lo, right_ptr);
 
186
              lo = left_ptr;
 
187
            }
 
188
          else
 
189
            {
 
190
              /* Push larger right partition indices. */
 
191
              PUSH (left_ptr, hi);
 
192
              hi = right_ptr;
 
193
            }
 
194
        }
 
195
    }
 
196
 
 
197
  /* Once the BASE_PTR array is partially sorted by quicksort the rest
 
198
     is completely sorted using insertion sort, since this is efficient
 
199
     for partitions below MAX_THRESH size. BASE_PTR points to the beginning
 
200
     of the array to sort, and END_PTR points at the very last element in
 
201
     the array (*not* one beyond it!). */
 
202
 
 
203
#define min(x, y) ((x) < (y) ? (x) : (y))
 
204
 
 
205
  {
 
206
    char *const end_ptr = &base_ptr[INTSORTSIZE * (total_elems - 1)];
 
207
    char *tmp_ptr = base_ptr;
 
208
    char *thresh = min(end_ptr, base_ptr + max_thresh);
 
209
    register char *run_ptr;
 
210
 
 
211
    /* Find smallest element in first threshold and place it at the
 
212
       array's beginning.  This is the smallest array element,
 
213
       and the operation speeds up insertion sort's inner loop. */
 
214
 
 
215
    for (run_ptr = tmp_ptr + INTSORTSIZE; run_ptr <= thresh; run_ptr += INTSORTSIZE)
 
216
      if (INTSORTCMP ((void *) run_ptr, (void *) tmp_ptr))
 
217
        tmp_ptr = run_ptr;
 
218
 
 
219
    if (tmp_ptr != base_ptr)
 
220
      INTSORTSWAP (tmp_ptr, base_ptr);
 
221
 
 
222
    /* Insertion sort, running from left-hand-side up to right-hand-side.  */
 
223
 
 
224
    run_ptr = base_ptr + INTSORTSIZE;
 
225
    while ((run_ptr += INTSORTSIZE) <= end_ptr)
 
226
      {
 
227
        tmp_ptr = run_ptr - INTSORTSIZE;
 
228
        while (INTSORTCMP ((void *) run_ptr, (void *) tmp_ptr))
 
229
          tmp_ptr -= INTSORTSIZE;
 
230
 
 
231
        tmp_ptr += INTSORTSIZE;
 
232
        if (tmp_ptr != run_ptr)
 
233
          {
 
234
            char *trav;
 
235
 
 
236
            trav = run_ptr + INTSORTSIZE;
 
237
            while (--trav >= run_ptr)
 
238
              {
 
239
                char c = *trav;
 
240
                char *hi, *lo;
 
241
 
 
242
                for (hi = lo = trav; (lo -= INTSORTSIZE) >= tmp_ptr; hi = lo)
 
243
                  *hi = *lo;
 
244
                *hi = c;
 
245
              }
 
246
          }
 
247
      }
 
248
  }
 
249
}