michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: michael@0: /*- michael@0: * Copyright (c) 1992, 1993 michael@0: * The Regents of the University of California. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. Neither the name of the University nor the names of its contributors michael@0: * may be used to endorse or promote products derived from this software michael@0: * without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND michael@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE michael@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL michael@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS michael@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) michael@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT michael@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY michael@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF michael@0: * SUCH DAMAGE. michael@0: */ michael@0: michael@0: /* We need this because Solaris' version of qsort is broken and michael@0: * causes array bounds reads. michael@0: */ michael@0: michael@0: #include michael@0: #include "nsAlgorithm.h" michael@0: #include "nsQuickSort.h" michael@0: michael@0: extern "C" { michael@0: michael@0: #if !defined(DEBUG) && (defined(__cplusplus) || defined(__gcc)) michael@0: # ifndef INLINE michael@0: # define INLINE inline michael@0: # endif michael@0: #else michael@0: # define INLINE michael@0: #endif michael@0: michael@0: typedef int cmp_t(const void *, const void *, void *); michael@0: static INLINE char *med3(char *, char *, char *, cmp_t *, void *); michael@0: static INLINE void swapfunc(char *, char *, int, int); michael@0: michael@0: /* michael@0: * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". michael@0: */ michael@0: #define swapcode(TYPE, parmi, parmj, n) { \ michael@0: long i = (n) / sizeof (TYPE); \ michael@0: TYPE *pi = (TYPE *) (parmi); \ michael@0: TYPE *pj = (TYPE *) (parmj); \ michael@0: do { \ michael@0: TYPE t = *pi; \ michael@0: *pi++ = *pj; \ michael@0: *pj++ = t; \ michael@0: } while (--i > 0); \ michael@0: } michael@0: michael@0: #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ michael@0: es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; michael@0: michael@0: static INLINE void michael@0: swapfunc(char *a, char *b, int n, int swaptype) michael@0: { michael@0: if(swaptype <= 1) michael@0: swapcode(long, a, b, n) michael@0: else michael@0: swapcode(char, a, b, n) michael@0: } michael@0: michael@0: #define swap(a, b) \ michael@0: if (swaptype == 0) { \ michael@0: long t = *(long *)(a); \ michael@0: *(long *)(a) = *(long *)(b); \ michael@0: *(long *)(b) = t; \ michael@0: } else \ michael@0: swapfunc((char *)a, (char*)b, (int)es, swaptype) michael@0: michael@0: #define vecswap(a, b, n) if ((n) > 0) swapfunc((char *)a, (char *)b, (int)n, swaptype) michael@0: michael@0: static INLINE char * michael@0: med3(char *a, char *b, char *c, cmp_t* cmp, void *data) michael@0: { michael@0: return cmp(a, b, data) < 0 ? michael@0: (cmp(b, c, data) < 0 ? b : (cmp(a, c, data) < 0 ? c : a )) michael@0: :(cmp(b, c, data) > 0 ? b : (cmp(a, c, data) < 0 ? a : c )); michael@0: } michael@0: michael@0: void NS_QuickSort ( michael@0: void *a, michael@0: unsigned int n, michael@0: unsigned int es, michael@0: cmp_t *cmp, michael@0: void *data michael@0: ) michael@0: { michael@0: char *pa, *pb, *pc, *pd, *pl, *pm, *pn; michael@0: int d, r, swaptype; michael@0: michael@0: loop: SWAPINIT(a, es); michael@0: /* Use insertion sort when input is small */ michael@0: if (n < 7) { michael@0: for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) michael@0: for (pl = pm; pl > (char *)a && cmp(pl - es, pl, data) > 0; michael@0: pl -= es) michael@0: swap(pl, pl - es); michael@0: return; michael@0: } michael@0: /* Choose pivot */ michael@0: pm = (char *)a + (n / 2) * es; michael@0: if (n > 7) { michael@0: pl = (char *)a; michael@0: pn = (char *)a + (n - 1) * es; michael@0: if (n > 40) { michael@0: d = (n / 8) * es; michael@0: pl = med3(pl, pl + d, pl + 2 * d, cmp, data); michael@0: pm = med3(pm - d, pm, pm + d, cmp, data); michael@0: pn = med3(pn - 2 * d, pn - d, pn, cmp, data); michael@0: } michael@0: pm = med3(pl, pm, pn, cmp, data); michael@0: } michael@0: swap(a, pm); michael@0: pa = pb = (char *)a + es; michael@0: michael@0: pc = pd = (char *)a + (n - 1) * es; michael@0: /* loop invariants: michael@0: * [a, pa) = pivot michael@0: * [pa, pb) < pivot michael@0: * [pb, pc + es) unprocessed michael@0: * [pc + es, pd + es) > pivot michael@0: * [pd + es, pn) = pivot michael@0: */ michael@0: for (;;) { michael@0: while (pb <= pc && (r = cmp(pb, a, data)) <= 0) { michael@0: if (r == 0) { michael@0: swap(pa, pb); michael@0: pa += es; michael@0: } michael@0: pb += es; michael@0: } michael@0: while (pb <= pc && (r = cmp(pc, a, data)) >= 0) { michael@0: if (r == 0) { michael@0: swap(pc, pd); michael@0: pd -= es; michael@0: } michael@0: pc -= es; michael@0: } michael@0: if (pb > pc) michael@0: break; michael@0: swap(pb, pc); michael@0: pb += es; michael@0: pc -= es; michael@0: } michael@0: /* Move pivot values */ michael@0: pn = (char *)a + n * es; michael@0: r = XPCOM_MIN(pa - (char *)a, pb - pa); michael@0: vecswap(a, pb - r, r); michael@0: r = XPCOM_MIN(pd - pc, pn - pd - es); michael@0: vecswap(pb, pn - r, r); michael@0: /* Recursively process partitioned items */ michael@0: if ((r = pb - pa) > (int)es) michael@0: NS_QuickSort(a, r / es, es, cmp, data); michael@0: if ((r = pd - pc) > (int)es) { michael@0: /* Iterate rather than recurse to save stack space */ michael@0: a = pn - r; michael@0: n = r / es; michael@0: goto loop; michael@0: } michael@0: /* NS_QuickSort(pn - r, r / es, es, cmp, data);*/ michael@0: } michael@0: michael@0: } michael@0: michael@0: #undef INLINE michael@0: #undef swapcode michael@0: #undef SWAPINIT michael@0: #undef swap michael@0: #undef vecswap