home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / stdlib / qsort.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  6KB  |  217 lines

  1. /*-
  2.  * Copyright (c) 1980, 1983 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that: (1) source distributions retain this entire copyright
  7.  * notice and comment, and (2) distributions including binaries display
  8.  * the following acknowledgement:  ``This product includes software
  9.  * developed by the University of California, Berkeley and its contributors''
  10.  * in the documentation or other materials provided with the distribution
  11.  * and in all advertising materials mentioning features or use of this
  12.  * software. Neither the name of the University nor the names of its
  13.  * contributors may be used to endorse or promote products derived
  14.  * from this software without specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. /* Modified by PerOla Valfridsson 910215 */
  21.  
  22. #if defined(LIBC_SCCS) && !defined(lint)
  23. static char sccsid[] = "@(#)qsort.c    5.7 (Berkeley) 5/17/90";
  24. #endif /* LIBC_SCCS and not lint */
  25.  
  26. #define _KERNEL
  27. #include "ixemul.h"
  28.  
  29. #include <stdlib.h>
  30.  
  31. static void qst(char *base, char *max, int (*qcmp)(), int qsz);
  32.  
  33. /*
  34.  * qsort.c:
  35.  * Our own version of the system qsort routine which is faster by an average
  36.  * of 25%, with lows and highs of 10% and 50%.
  37.  * The THRESHold below is the insertion sort threshold, and has been adjusted
  38.  * for records of size 48 bytes.
  39.  * The MTHREShold is where we stop finding a better median.
  40.  */
  41.  
  42. #define        THRESH        4        /* threshold for insertion */
  43. #define        MTHRESH        6        /* threshold for median */
  44.  
  45. /*
  46.  * qsort:
  47.  * First, set up some global parameters for qst to share.  Then, quicksort
  48.  * with qst(), and then a cleanup insertion sort ourselves.  Sound simple?
  49.  * It's not...
  50.  */
  51.  
  52. void
  53. qsort(void *base, size_t n, size_t qsz, int (*qcmp)())
  54. {
  55.     register char c, *i, *j, *lo, *hi;
  56.     char *min, *max;
  57.  
  58.     if (n <= 1)
  59.         return;
  60.  
  61.     max = base + n * qsz;
  62.     if (n >= THRESH) {
  63.         qst(base, max, qcmp, qsz);
  64.         hi = base + qsz * THRESH;
  65.     } else {
  66.         hi = max;
  67.     }
  68.     /*
  69.      * First put smallest element, which must be in the first THRESH, in
  70.      * the first position as a sentinel.  This is done just by searching
  71.      * the first THRESH elements (or the first n if n < THRESH), finding
  72.      * the min, and swapping it into the first position.
  73.      */
  74.     for (j = lo = base; (lo += qsz) < hi; )
  75.         if (qcmp(j, lo) > 0)
  76.             j = lo;
  77.     if (j != base) {
  78.         /* swap j into place */
  79.         for (i = base, hi = base + qsz; i < hi; ) {
  80.             c = *j;
  81.             *j++ = *i;
  82.             *i++ = c;
  83.         }
  84.     }
  85.     /*
  86.      * With our sentinel in place, we now run the following hyper-fast
  87.      * insertion sort.  For each remaining element, min, from [1] to [n-1],
  88.      * set hi to the index of the element AFTER which this one goes.
  89.      * Then, do the standard insertion sort shift on a character at a time
  90.      * basis for each element in the frob.
  91.      */
  92.     for (min = base; (hi = (min += qsz)) < max; ) {
  93.         while (qcmp((hi -= qsz), min) > 0)
  94.             /* void */;
  95.         if ((hi += qsz) != min) {
  96.             for (lo = (min + qsz); --lo >= min; ) {
  97.                 c = *lo;
  98.                 for (i = j = lo; (j -= qsz) >= hi; i = j)
  99.                     *i = *j;
  100.                 *i = c;
  101.             }
  102.         }
  103.     }
  104. }
  105.  
  106. /*
  107.  * qst:
  108.  * Do a quicksort
  109.  * First, find the median element, and put that one in the first place as the
  110.  * discriminator.  (This "median" is just the median of the first, last and
  111.  * middle elements).  (Using this median instead of the first element is a big
  112.  * win).  Then, the usual partitioning/swapping, followed by moving the
  113.  * discriminator into the right place.  Then, figure out the sizes of the two
  114.  * partions, do the smaller one recursively and the larger one via a repeat of
  115.  * this code.  Stopping when there are less than THRESH elements in a partition
  116.  * and cleaning up with an insertion sort (in our caller) is a huge win.
  117.  * All data swaps are done in-line, which is space-losing but time-saving.
  118.  * (And there are only three places where this is done).
  119.  */
  120.  
  121. static void qst(char *base, char *max, int (*qcmp)(), int qsz)
  122. {
  123.     register char c, *i, *j, *jj;
  124.     register int ii;
  125.     char *mid, *tmp;
  126.     int lo, hi;
  127.  
  128.     /*
  129.      * At the top here, lo is the number of characters of elements in the
  130.      * current partition.  (Which should be max - base).
  131.      * Find the median of the first, last, and middle element and make
  132.      * that the middle element.  Set j to largest of first and middle.
  133.      * If max is larger than that guy, then it's that guy, else compare
  134.      * max with loser of first and take larger.  Things are set up to
  135.      * prefer the middle, then the first in case of ties.
  136.      */
  137.     lo = max - base;        /* number of elements as chars */
  138.     do    {
  139.         mid = i = base + qsz * ((lo / qsz) >> 1);
  140.         if (lo >= qsz * MTHRESH) {
  141.             j = (qcmp((jj = base), i) > 0 ? jj : i);
  142.             if (qcmp(j, (tmp = max - qsz)) > 0) {
  143.                 /* switch to first loser */
  144.                 j = (j == jj ? i : jj);
  145.                 if (qcmp(j, tmp) < 0)
  146.                     j = tmp;
  147.             }
  148.             if (j != i) {
  149.                 ii = qsz;
  150.                 do    {
  151.                     c = *i;
  152.                     *i++ = *j;
  153.                     *j++ = c;
  154.                 } while (--ii);
  155.             }
  156.         }
  157.         /*
  158.          * Semi-standard quicksort partitioning/swapping
  159.          */
  160.         for (i = base, j = max - qsz; ; ) {
  161.             while (i < mid && qcmp(i, mid) <= 0)
  162.                 i += qsz;
  163.             while (j > mid) {
  164.                 if (qcmp(mid, j) <= 0) {
  165.                     j -= qsz;
  166.                     continue;
  167.                 }
  168.                 tmp = i + qsz;    /* value of i after swap */
  169.                 if (i == mid) {
  170.                     /* j <-> mid, new mid is j */
  171.                     mid = jj = j;
  172.                 } else {
  173.                     /* i <-> j */
  174.                     jj = j;
  175.                     j -= qsz;
  176.                 }
  177.                 goto swap;
  178.             }
  179.             if (i == mid) {
  180.                 break;
  181.             } else {
  182.                 /* i <-> mid, new mid is i */
  183.                 jj = mid;
  184.                 tmp = mid = i;    /* value of i after swap */
  185.                 j -= qsz;
  186.             }
  187.         swap:
  188.             ii = qsz;
  189.             do    {
  190.                 c = *i;
  191.                 *i++ = *jj;
  192.                 *jj++ = c;
  193.             } while (--ii);
  194.             i = tmp;
  195.         }
  196.         /*
  197.          * Look at sizes of the two partitions, do the smaller
  198.          * one first by recursion, then do the larger one by
  199.          * making sure lo is its size, base and max are update
  200.          * correctly, and branching back.  But only repeat
  201.          * (recursively or by branching) if the partition is
  202.          * of at least size THRESH.
  203.          */
  204.         i = (j = mid) + qsz;
  205.         if ((lo = j - base) <= (hi = max - i)) {
  206.             if (lo >= qsz * THRESH)
  207.                 qst(base, j, qcmp, qsz);
  208.             base = i;
  209.             lo = hi;
  210.         } else {
  211.             if (hi >= qsz * THRESH)
  212.                 qst(i, max, qcmp, qsz);
  213.             max = j;
  214.         }
  215.     } while (lo >= qsz * THRESH);
  216. }
  217.