Back to index

lightning-sunbird  0.9+nobinonly
nsQuickSort.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 
00003 /*-
00004  * Copyright (c) 1992, 1993
00005  *     The Regents of the University of California.  All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  * 3. Neither the name of the University nor the names of its contributors
00016  *    may be used to endorse or promote products derived from this software
00017  *    without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00020  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00023  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00024  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00025  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00026  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00027  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00028  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00029  * SUCH DAMAGE.
00030  */
00031 
00032 /* We need this because Solaris' version of qsort is broken and
00033  * causes array bounds reads.
00034  */
00035 
00036 #include <stdlib.h>
00037 #include "prtypes.h"
00038 #include "nsQuickSort.h"
00039 
00040 PR_BEGIN_EXTERN_C
00041 
00042 #if !defined(DEBUG) && (defined(__cplusplus) || defined(__gcc))
00043 # ifndef INLINE
00044 #  define INLINE inline
00045 # endif
00046 #else
00047 # define INLINE
00048 #endif
00049 
00050 typedef int           cmp_t(const void *, const void *, void *);
00051 static INLINE char   *med3(char *, char *, char *, cmp_t *, void *);
00052 static INLINE void   swapfunc(char *, char *, int, int);
00053 
00054 /*
00055  * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
00056  */
00057 #define swapcode(TYPE, parmi, parmj, n) {               \
00058        long i = (n) / sizeof (TYPE);                    \
00059        register TYPE *pi = (TYPE *) (parmi);            \
00060        register TYPE *pj = (TYPE *) (parmj);            \
00061        do {                                      \
00062               register TYPE t = *pi;             \
00063               *pi++ = *pj;                       \
00064               *pj++ = t;                         \
00065         } while (--i > 0);                       \
00066 }
00067 
00068 #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
00069        es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
00070 
00071 static INLINE void
00072 swapfunc(char *a, char *b, int n, int swaptype)
00073 {
00074        if(swaptype <= 1)
00075               swapcode(long, a, b, n)
00076        else
00077               swapcode(char, a, b, n)
00078 }
00079 
00080 #define swap(a, b)                               \
00081        if (swaptype == 0) {                      \
00082               long t = *(long *)(a);                    \
00083               *(long *)(a) = *(long *)(b);              \
00084               *(long *)(b) = t;                  \
00085        } else                                    \
00086               swapfunc((char *)a, (char*)b, (int)es, swaptype)
00087 
00088 #define vecswap(a, b, n)    if ((n) > 0) swapfunc((char *)a, (char *)b, (int)n, swaptype)
00089 
00090 static INLINE char *
00091 med3(char *a, char *b, char *c, cmp_t* cmp, void *data)
00092 {
00093        return cmp(a, b, data) < 0 ?
00094               (cmp(b, c, data) < 0 ? b : (cmp(a, c, data) < 0 ? c : a ))
00095               :(cmp(b, c, data) > 0 ? b : (cmp(a, c, data) < 0 ? a : c ));
00096 }
00097 
00098 void NS_QuickSort (
00099        void *a,
00100        unsigned int n,
00101     unsigned int es,
00102        cmp_t *cmp,
00103        void *data
00104     )
00105 {
00106        char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
00107        int d, r, swaptype, swap_cnt;
00108 
00109 loop:  SWAPINIT(a, es);
00110        swap_cnt = 0;
00111        if (n < 7) {
00112               for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
00113                      for (pl = pm; pl > (char *)a && cmp(pl - es, pl, data) > 0;
00114                           pl -= es)
00115                             swap(pl, pl - es);
00116               return;
00117        }
00118        pm = (char *)a + (n / 2) * es;
00119        if (n > 7) {
00120               pl = (char *)a;
00121               pn = (char *)a + (n - 1) * es;
00122               if (n > 40) {
00123                      d = (n / 8) * es;
00124                      pl = med3(pl, pl + d, pl + 2 * d, cmp, data);
00125                      pm = med3(pm - d, pm, pm + d, cmp, data);
00126                      pn = med3(pn - 2 * d, pn - d, pn, cmp, data);
00127               }
00128               pm = med3(pl, pm, pn, cmp, data);
00129        }
00130        swap(a, pm);
00131        pa = pb = (char *)a + es;
00132 
00133        pc = pd = (char *)a + (n - 1) * es;
00134        for (;;) {
00135               while (pb <= pc && (r = cmp(pb, a, data)) <= 0) {
00136                      if (r == 0) {
00137                             swap_cnt = 1;
00138                             swap(pa, pb);
00139                             pa += es;
00140                      }
00141                      pb += es;
00142               }
00143               while (pb <= pc && (r = cmp(pc, a, data)) >= 0) {
00144                      if (r == 0) {
00145                             swap_cnt = 1;
00146                             swap(pc, pd);
00147                             pd -= es;
00148                      }
00149                      pc -= es;
00150               }
00151               if (pb > pc)
00152                      break;
00153               swap(pb, pc);
00154               swap_cnt = 1;
00155               pb += es;
00156               pc -= es;
00157        }
00158        if (swap_cnt == 0) {  /* Switch to insertion sort */
00159               for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
00160                      for (pl = pm; pl > (char *)a && cmp(pl - es, pl, data) > 0;
00161                           pl -= es)
00162                             swap(pl, pl - es);
00163               return;
00164        }
00165 
00166        pn = (char *)a + n * es;
00167        r = PR_MIN(pa - (char *)a, pb - pa);
00168        vecswap(a, pb - r, r);
00169        r = PR_MIN(pd - pc, (int)(pn - pd - es));
00170        vecswap(pb, pn - r, r);
00171        if ((r = pb - pa) > (int)es)
00172         NS_QuickSort(a, r / es, es, cmp, data);
00173        if ((r = pd - pc) > (int)es) {
00174               /* Iterate rather than recurse to save stack space */
00175               a = pn - r;
00176               n = r / es;
00177               goto loop;
00178        }
00179 /*            NS_QuickSort(pn - r, r / es, es, cmp, data);*/
00180 }
00181 
00182 PR_END_EXTERN_C