2 * copyright (c) 2001-2006 Emil Mikulic.
4 * hosts_sort.c: quicksort a table of buckets.
6 * You may use, modify and redistribute this file under the terms of the
7 * GNU General Public License version 2. (see COPYING.GPL)
14 /* ---------------------------------------------------------------------------
15 * comparator for sorting (biggest first)
18 cmp(const struct bucket
* const *x
, const struct bucket
* const *y
,
19 const enum sort_dir dir
)
37 errx(1, "cmp: unknown direction: %d", dir
);
40 if (a
< b
) return (1);
41 else if (a
> b
) return (-1);
46 * The quicksort code is derived from FreeBSD's
47 * src/lib/libc/stdlib/qsort.c v1.12
51 * Copyright (c) 1992, 1993
52 * The Regents of the University of California. All rights reserved.
54 * Redistribution and use in source and binary forms, with or without
55 * modification, are permitted provided that the following conditions
57 * 1. Redistributions of source code must retain the above copyright
58 * notice, this list of conditions and the following disclaimer.
59 * 2. Redistributions in binary form must reproduce the above copyright
60 * notice, this list of conditions and the following disclaimer in the
61 * documentation and/or other materials provided with the distribution.
62 * 3. All advertising materials mentioning features or use of this software
63 * must display the following acknowledgement:
64 * This product includes software developed by the University of
65 * California, Berkeley and its contributors.
66 * 4. Neither the name of the University nor the names of its contributors
67 * may be used to endorse or promote products derived from this software
68 * without specific prior written permission.
70 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
71 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
72 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
73 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
74 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
75 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
76 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
77 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
78 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
79 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
84 vecswap(const struct bucket
**pi
, const struct bucket
**pj
, int n
)
90 const struct bucket
*t
= *pi
;
96 #define swap(a, b) { \
97 const struct bucket *t = *(const struct bucket **)(a); \
98 *(const struct bucket **)(a) = *(const struct bucket **)(b); \
99 *(const struct bucket **)(b) = t; \
102 static const struct bucket
**
103 med3(const struct bucket
**a
,
104 const struct bucket
**b
,
105 const struct bucket
**c
,
106 const enum sort_dir dir
)
108 return (cmp(a
, b
, dir
) < 0)
109 ? (cmp(b
, c
, dir
) < 0 ? b
: (cmp(a
, c
, dir
) < 0 ? c
: a
))
110 : (cmp(b
, c
, dir
) > 0 ? b
: (cmp(a
, c
, dir
) < 0 ? a
: c
));
113 /* Partial sort - only sort elements in the range [left:right] */
115 qsort_buckets(const struct bucket
**a
, size_t n
,
116 size_t left
, size_t right
,
117 const enum sort_dir dir
)
119 const struct bucket
**pa
, **pb
, **pc
, **pd
, **pl
, **pm
, **pn
;
125 for (pm
= a
+1; pm
< a
+n
; pm
++)
127 (pl
> a
) && (cmp(pl
-1, pl
, dir
) > 0);
138 pl
= med3(pl
, pl
+ d
, pl
+ 2 * d
, dir
);
139 pm
= med3(pm
- d
, pm
, pm
+ d
, dir
);
140 pn
= med3(pn
- 2 * d
, pn
- d
, pn
, dir
);
142 pm
= med3(pl
, pm
, pn
, dir
);
147 pc
= pd
= a
+ (n
- 1);
149 while (pb
<= pc
&& (r
= cmp(pb
, a
, dir
)) <= 0) {
157 while (pb
<= pc
&& (r
= cmp(pc
, a
, dir
)) >= 0) {
172 if (swap_cnt
== 0) { /* Switch to insertion sort */
173 for (pm
= a
+ 1; pm
< a
+n
; pm
++)
175 (pl
> a
) && (cmp(pl
-1, pl
, dir
) > 0);
182 r
= min(pa
- a
, pb
- pa
);
183 vecswap(a
, pb
- r
, r
);
184 r
= min(pd
- pc
, pn
- pd
- 1);
185 vecswap(pb
, pn
- r
, r
);
186 if (((r
= pb
- pa
) > 1) && ((unsigned)r
>= left
))
187 qsort_buckets(a
, r
, left
, right
, dir
);
188 if (((r
= pd
- pc
) > 1) && (n
- r
<= right
)) {
189 /* Iterate rather than recurse to save stack space */
199 /* qsort(pn - r, r, cmp);*/
202 /* vim:set ts=3 sw=3 tw=78 expandtab: */