src/util/qsort.c File Reference

#include "util.h"
Include dependency graph for qsort.c:

Go to the source code of this file.

Defines

#define THRESH   4
#define MTHRESH   6

Functions

static void qst ARGS ((char *base, char *max))
void qsort (void *vbase, size_t n, size_t size, int(*compar)(const void *, const void *))
static void qst (char *base, char *max)

Variables

static char rcsid[] UNUSED = "$Id: qsort.c,v 1.5 2002/08/25 05:30:13 fabio Exp $"
static int(* qcmp )(const void *, const void *)
static int qsz
static int thresh
static int mthresh

Define Documentation

#define MTHRESH   6

Definition at line 39 of file qsort.c.

#define THRESH   4

Definition at line 38 of file qsort.c.


Function Documentation

static void qst ARGS ( (char *base, char *max  )  [static]
void qsort ( void *  vbase,
size_t  n,
size_t  size,
int(*)(const void *, const void *)  compar 
)

Function********************************************************************

Synopsis [Own version of the system qsort routine.]

Description [The THRESHold below is the insertion sort threshold, and has been adjusted for records of size 48 bytes. The MTHREShold is where we stop finding a better median. First, set up some global parameters for qst to share. Then, quicksort with qst(), and then a cleanup insertion sort ourselves. Sound simple? It's not...]

SideEffects []

SeeAlso [qst]

Definition at line 70 of file qsort.c.

00075 {
00076         register char c, *i, *j, *lo, *hi;
00077         char *min, *max, *base;
00078 
00079         if (n <= 1)
00080                 return;
00081         base = (char *) vbase;
00082         qsz = size;
00083         qcmp = compar;
00084         thresh = qsz * THRESH;
00085         mthresh = qsz * MTHRESH;
00086         max = base + n * qsz;
00087         if (n >= THRESH) {
00088                 qst(base, max);
00089                 hi = base + thresh;
00090         } else {
00091                 hi = max;
00092         }
00093         /*
00094          * First put smallest element, which must be in the first THRESH, in
00095          * the first position as a sentinel.  This is done just by searching
00096          * the first THRESH elements (or the first n if n < THRESH), finding
00097          * the min, and swapping it into the first position.
00098          */
00099         for (j = lo = base; (lo += qsz) < hi; )
00100                 if ((*qcmp)(j, lo) > 0)
00101                         j = lo;
00102         if (j != base) {
00103                 /* swap j into place */
00104                 for (i = base, hi = base + qsz; i < hi; ) {
00105                         c = *j;
00106                         *j++ = *i;
00107                         *i++ = c;
00108                 }
00109         }
00110         /*
00111          * With our sentinel in place, we now run the following hyper-fast
00112          * insertion sort. For each remaining element, min, from [1] to [n-1],
00113          * set hi to the index of the element AFTER which this one goes.
00114          * Then, do the standard insertion sort shift on a character at a time
00115          * basis for each element in the frob.
00116          */
00117         for (min = base; (hi = min += qsz) < max; ) {
00118                 while ((*qcmp)(hi -= qsz, min) > 0)
00119                         /* void */;
00120                 if ((hi += qsz) != min) {
00121                         for (lo = min + qsz; --lo >= min; ) {
00122                                 c = *lo;
00123                                 for (i = j = lo; (j -= qsz) >= hi; i = j)
00124                                         *i = *j;
00125                                 *i = c;
00126                         }
00127                 }
00128         }
00129 }

static void qst ( char *  base,
char *  max 
) [static]

Function********************************************************************

Synopsis [Effectively perform qsort]

Description [First, find the median element, and put that one in the first place as the discriminator. (This "median" is just the median of the first, last and middle elements). (Using this median instead of the first element is a big win). Then, the usual partitioning/swapping, followed by moving the discriminator into the right place. Then, figure out the sizes of the two partions, do the smaller one recursively and the larger one via a repeat of this code. Stopping when there are less than THRESH elements in a partition and cleaning up with an insertion sort (in our caller) is a huge win. All data swaps are done in-line, which is space-losing but time-saving. (And there are only three places where this is done).]

SideEffects []

SeeAlso [qsort]

Definition at line 156 of file qsort.c.

00157 {
00158         register char c, *i, *j, *jj;
00159         register int ii;
00160         char *mid, *tmp;
00161         int lo, hi;
00162 
00163         /*
00164          * At the top here, lo is the number of characters of elements in the
00165          * current partition.  (Which should be max - base).
00166          * Find the median of the first, last, and middle element and make
00167          * that the middle element.  Set j to largest of first and middle.
00168          * If max is larger than that guy, then it's that guy, else compare
00169          * max with loser of first and take larger.  Things are set up to
00170          * prefer the middle, then the first in case of ties.
00171          */
00172         lo = max - base;                /* number of elements as chars */
00173         do      {
00174                 mid = i = base + qsz * ((lo / qsz) >> 1);
00175                 if (lo >= mthresh) {
00176                         j = ((*qcmp)((jj = base), i) > 0 ? jj : i);
00177                         if ((*qcmp)(j, (tmp = max - qsz)) > 0) {
00178                                 /* switch to first loser */
00179                                 j = (j == jj ? i : jj);
00180                                 if ((*qcmp)(j, tmp) < 0)
00181                                         j = tmp;
00182                         }
00183                         if (j != i) {
00184                                 ii = qsz;
00185                                 do      {
00186                                         c = *i;
00187                                         *i++ = *j;
00188                                         *j++ = c;
00189                                 } while (--ii);
00190                         }
00191                 }
00192                 /*
00193                  * Semi-standard quicksort partitioning/swapping
00194                  */
00195                 for (i = base, j = max - qsz; ; ) {
00196                         while (i < mid && (*qcmp)(i, mid) <= 0)
00197                                 i += qsz;
00198                         while (j > mid) {
00199                                 if ((*qcmp)(mid, j) <= 0) {
00200                                         j -= qsz;
00201                                         continue;
00202                                 }
00203                                 tmp = i + qsz;  /* value of i after swap */
00204                                 if (i == mid) {
00205                                         /* j <-> mid, new mid is j */
00206                                         mid = jj = j;
00207                                 } else {
00208                                         /* i <-> j */
00209                                         jj = j;
00210                                         j -= qsz;
00211                                 }
00212                                 goto swap;
00213                         }
00214                         if (i == mid) {
00215                                 break;
00216                         } else {
00217                                 /* i <-> mid, new mid is i */
00218                                 jj = mid;
00219                                 tmp = mid = i;  /* value of i after swap */
00220                                 j -= qsz;
00221                         }
00222                 swap:
00223                         ii = qsz;
00224                         do      {
00225                                 c = *i;
00226                                 *i++ = *jj;
00227                                 *jj++ = c;
00228                         } while (--ii);
00229                         i = tmp;
00230                 }
00231                 /*
00232                  * Look at sizes of the two partitions, do the smaller
00233                  * one first by recursion, then do the larger one by
00234                  * making sure lo is its size, base and max are update
00235                  * correctly, and branching back.  But only repeat
00236                  * (recursively or by branching) if the partition is
00237                  * of at least size THRESH.
00238                  */
00239                 i = (j = mid) + qsz;
00240                 if ((lo = j - base) <= (hi = max - i)) {
00241                         if (lo >= thresh)
00242                                 qst(base, j);
00243                         base = i;
00244                         lo = hi;
00245                 } else {
00246                         if (hi >= thresh)
00247                                 qst(i, max);
00248                         max = j;
00249                 }
00250         } while (lo >= thresh);
00251 }


Variable Documentation

int mthresh [static]

Definition at line 45 of file qsort.c.

int(* qcmp)(const void *, const void *) [static]
int qsz [static]

Definition at line 43 of file qsort.c.

int thresh [static]

Definition at line 44 of file qsort.c.

char rcsid [] UNUSED = "$Id: qsort.c,v 1.5 2002/08/25 05:30:13 fabio Exp $" [static]

CFile***********************************************************************

FileName [qsort.c]

PackageName [util]

Synopsis [Our own qsort routine.]

Author []

Copyright [Copyright (c) 1994-1996 The Regents of the Univ. of California. All rights reserved.

Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy, modify, and distribute this software and its documentation for any purpose, provided that the above copyright notice and the following two paragraphs appear in all copies of this software.

IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.]

Definition at line 35 of file qsort.c.


Generated on Tue Jan 12 13:57:29 2010 for glu-2.2 by  doxygen 1.6.1