2 * This file is part of the GROMACS molecular simulation package.
4 * This file is part of Gromacs Copyright (c) 1991-2010
5 * David van der Spoel, Erik Lindahl, Berk Hess, University of Groningen.
6 * Copyright (c) 2012, by the GROMACS development team, led by
7 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
8 * others, as listed in the AUTHORS file in the top-level source
9 * directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
47 qsort_swapfunc(void * a
,
64 for( ; n
> 0; ia
+= 1, ib
+= 1, n
-= sizeof(int))
75 for( ; n
> 0; ca
+= 1, cb
+= 1, n
-= 1)
89 int (*compar
) (const void *a
, const void *b
))
95 else if(compar(a
,c
) < 0)
104 else if(compar(a
,c
) > 0)
113 gmx_qsort(void * base
,
116 int (*compar
)(const void *, const void *))
118 #define QSORT_EXCH(a, b, t) (t = a, a = b, b = t)
119 #define QSORT_SWAP(a, b) swaptype != 0 ? qsort_swapfunc(a, b, size, swaptype) : \
120 (void)QSORT_EXCH(*(int *)(a), *(int *)(b), t)
122 char *pa
, *pb
, *pc
, *pd
, *pl
, *pm
, *pn
, *pv
, *cbase
;
127 cbase
= (char *)base
;
129 swaptype
= (size_t)(cbase
- (char *)0) % sizeof(int) || size
% sizeof(int) ? 2 : size
== sizeof(int)? 0 : 1;
133 /* Insertion sort on smallest arrays */
134 for (pm
= cbase
+ size
; pm
< cbase
+ nmemb
*size
; pm
+= size
)
136 for (pl
= pm
; (pl
> cbase
) && compar((void *)(pl
-size
),(void *) pl
) > 0; pl
-= size
)
138 QSORT_SWAP(pl
, pl
-size
);
144 /* Small arrays, middle element */
145 pm
= cbase
+ (nmemb
/2)*size
;
150 pn
= cbase
+ (nmemb
-1)*size
;
153 /* Big arrays, pseudomedian of 9 */
155 pl
= (char *)qsort_med3((void *)pl
, (void *)((size_t)pl
+s
), (void *)((size_t)pl
+2*s
), compar
);
156 pm
= (char *)qsort_med3((void *)((size_t)pm
-s
), (void *)pm
, (void *)((size_t)pm
+s
), compar
);
157 pn
= (char *)qsort_med3((void *)((size_t)pn
-2*s
), (void *)((size_t)pn
-s
), (void *)pn
, compar
);
159 /* Mid-size, med of 3 */
160 pm
= (char *)qsort_med3((void *)pl
, (void *)pm
, (void *)pn
, compar
);
163 /* pv points to partition value */
171 pv
= (char*)(void*)&v
;
176 pc
= pd
= cbase
+ (nmemb
-1)*size
;
180 while (pb
<= pc
&& (r
= compar((void *)pb
,(void *) pv
)) <= 0)
189 while (pc
>= pb
&& (r
= compar((void *)pc
,(void *) pv
)) >= 0)
206 pn
= cbase
+ nmemb
*size
;
217 qsort_swapfunc(cbase
, pb
-s
, s
, swaptype
);
229 qsort_swapfunc(pb
, pn
-s
, s
, swaptype
);
232 if ((s
= pb
-pa
) > size
)
234 gmx_qsort(cbase
, s
/size
, size
, compar
);
237 if ((s
= pd
-pc
) > size
)
239 gmx_qsort(pn
-s
, s
/size
, size
, compar
);