home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnuc / stdlib / rcs / qsort.c,v < prev    next >
Encoding:
Text File  |  1992-07-04  |  7.0 KB  |  270 lines

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