2 * copyright (c) 2001-2012 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 a
= (*x
)->u
.host
.last_seen_mono
;
38 b
= (*y
)->u
.host
.last_seen_mono
;
41 errx(1, "cmp: unknown direction: %d", dir
);
44 if (a
< b
) return (1);
45 else if (a
> b
) return (-1);
50 * The quicksort code is derived from FreeBSD's
51 * src/lib/libc/stdlib/qsort.c v1.12
55 * Copyright (c) 1992, 1993
56 * The Regents of the University of California. All rights reserved.
58 * Redistribution and use in source and binary forms, with or without
59 * modification, are permitted provided that the following conditions
61 * 1. Redistributions of source code must retain the above copyright
62 * notice, this list of conditions and the following disclaimer.
63 * 2. Redistributions in binary form must reproduce the above copyright
64 * notice, this list of conditions and the following disclaimer in the
65 * documentation and/or other materials provided with the distribution.
66 * 3. All advertising materials mentioning features or use of this software
67 * must display the following acknowledgement:
68 * This product includes software developed by the University of
69 * California, Berkeley and its contributors.
70 * 4. Neither the name of the University nor the names of its contributors
71 * may be used to endorse or promote products derived from this software
72 * without specific prior written permission.
74 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
75 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
76 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
77 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
78 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
79 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
80 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
81 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
82 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
83 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88 vecswap(const struct bucket
**pi
, const struct bucket
**pj
, int n
)
94 const struct bucket
*t
= *pi
;
100 #define swap(a, b) { \
101 const struct bucket *t = *(const struct bucket **)(a); \
102 *(const struct bucket **)(a) = *(const struct bucket **)(b); \
103 *(const struct bucket **)(b) = t; \
106 static const struct bucket
**
107 med3(const struct bucket
**a
,
108 const struct bucket
**b
,
109 const struct bucket
**c
,
110 const enum sort_dir dir
)
112 return (cmp(a
, b
, dir
) < 0)
113 ? (cmp(b
, c
, dir
) < 0 ? b
: (cmp(a
, c
, dir
) < 0 ? c
: a
))
114 : (cmp(b
, c
, dir
) > 0 ? b
: (cmp(a
, c
, dir
) < 0 ? a
: c
));
117 /* Partial sort - only sort elements in the range [left:right] */
119 qsort_buckets(const struct bucket
**a
, size_t n
,
120 size_t left
, size_t right
,
121 const enum sort_dir dir
)
123 const struct bucket
**pa
, **pb
, **pc
, **pd
, **pl
, **pm
, **pn
;
129 for (pm
= a
+1; pm
< a
+n
; pm
++)
131 (pl
> a
) && (cmp(pl
-1, pl
, dir
) > 0);
142 pl
= med3(pl
, pl
+ d
, pl
+ 2 * d
, dir
);
143 pm
= med3(pm
- d
, pm
, pm
+ d
, dir
);
144 pn
= med3(pn
- 2 * d
, pn
- d
, pn
, dir
);
146 pm
= med3(pl
, pm
, pn
, dir
);
151 pc
= pd
= a
+ (n
- 1);
153 while (pb
<= pc
&& (r
= cmp(pb
, a
, dir
)) <= 0) {
161 while (pb
<= pc
&& (r
= cmp(pc
, a
, dir
)) >= 0) {
176 if (swap_cnt
== 0) { /* Switch to insertion sort */
177 for (pm
= a
+ 1; pm
< a
+n
; pm
++)
179 (pl
> a
) && (cmp(pl
-1, pl
, dir
) > 0);
186 r
= MIN(pa
- a
, pb
- pa
);
187 vecswap(a
, pb
- r
, r
);
188 r
= MIN(pd
- pc
, pn
- pd
- 1);
189 vecswap(pb
, pn
- r
, r
);
190 if (((r
= pb
- pa
) > 1) && ((unsigned)r
>= left
))
191 qsort_buckets(a
, r
, left
, right
, dir
);
192 if (((r
= pd
- pc
) > 1) && (n
- r
<= right
)) {
193 /* Iterate rather than recurse to save stack space */
203 /* qsort(pn - r, r, cmp);*/
206 /* vim:set ts=3 sw=3 tw=78 expandtab: */