2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2012,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 #include "qsort_threadsafe.h"
42 qsort_swapfunc(void * a,
59 for (; n > 0; ia += 1, ib += 1, n -= sizeof(int))
70 for (; n > 0; ca += 1, cb += 1, n -= 1)
84 int (*compar) (const void *a, const void *b))
92 else if (compar(a, c) < 0)
103 if (compar(b, c) > 0)
107 else if (compar(a, c) > 0)
120 gmx_qsort(void * base,
123 int (*compar)(const void *, const void *))
125 #define QSORT_EXCH(a, b, t) (t = a, a = b, b = t)
126 #define QSORT_SWAP(a, b) swaptype != 0 ? qsort_swapfunc(a, b, size, swaptype) : \
127 (void)QSORT_EXCH(*(int *)(a), *(int *)(b), t)
129 char *pa, *pb, *pc, *pd, *pl, *pm, *pn, *pv, *cbase;
134 cbase = (char *)base;
136 swaptype = (size_t)(cbase - (char *)0) % sizeof(int) || size % sizeof(int) ? 2 : size == sizeof(int) ? 0 : 1;
140 /* Insertion sort on smallest arrays */
141 for (pm = cbase + size; pm < cbase + nmemb*size; pm += size)
143 for (pl = pm; (pl > cbase) && compar((void *)(pl-size), (void *) pl) > 0; pl -= size)
145 QSORT_SWAP(pl, pl-size);
151 /* Small arrays, middle element */
152 pm = cbase + (nmemb/2)*size;
157 pn = cbase + (nmemb-1)*size;
160 /* Big arrays, pseudomedian of 9 */
162 pl = (char *)qsort_med3((void *)pl, (void *)((size_t)pl+s), (void *)((size_t)pl+2*s), compar);
163 pm = (char *)qsort_med3((void *)((size_t)pm-s), (void *)pm, (void *)((size_t)pm+s), compar);
164 pn = (char *)qsort_med3((void *)((size_t)pn-2*s), (void *)((size_t)pn-s), (void *)pn, compar);
166 /* Mid-size, med of 3 */
167 pm = (char *)qsort_med3((void *)pl, (void *)pm, (void *)pn, compar);
170 /* pv points to partition value */
178 pv = (char*)(void*)&v;
183 pc = pd = cbase + (nmemb-1)*size;
187 while (pb <= pc && (r = compar((void *)pb, (void *) pv)) <= 0)
196 while (pc >= pb && (r = compar((void *)pc, (void *) pv)) >= 0)
213 pn = cbase + nmemb*size;
224 qsort_swapfunc(cbase, pb-s, s, swaptype);
236 qsort_swapfunc(pb, pn-s, s, swaptype);
239 if ((s = pb-pa) > size)
241 gmx_qsort(cbase, s/size, size, compar);
244 if ((s = pd-pc) > size)
246 gmx_qsort(pn-s, s/size, size, compar);