Import darkstat 3.0.712
[darkstat] / hosts_sort.c
1 /* darkstat 3
2 * copyright (c) 2001-2006 Emil Mikulic.
3 *
4 * hosts_sort.c: quicksort a table of buckets.
5 *
6 * You may use, modify and redistribute this file under the terms of the
7 * GNU General Public License version 2. (see COPYING.GPL)
8 */
9
10 #include "darkstat.h"
11 #include "hosts_db.h"
12 #include "err.h"
13
14 /* ---------------------------------------------------------------------------
15 * comparator for sorting (biggest first)
16 */
17 static int
18 cmp(const struct bucket * const *x, const struct bucket * const *y,
19 const enum sort_dir dir)
20 {
21 uint64_t a, b;
22
23 switch (dir) {
24 case IN:
25 a = (*x)->in;
26 b = (*y)->in;
27 break;
28 case OUT:
29 a = (*x)->out;
30 b = (*y)->out;
31 break;
32 case TOTAL:
33 a = (*x)->total;
34 b = (*y)->total;
35 break;
36 default:
37 errx(1, "cmp: unknown direction: %d", dir);
38 }
39
40 if (a < b) return (1);
41 else if (a > b) return (-1);
42 else return (0);
43 }
44
45 /*
46 * The quicksort code is derived from FreeBSD's
47 * src/lib/libc/stdlib/qsort.c v1.12
48 */
49
50 /*-
51 * Copyright (c) 1992, 1993
52 * The Regents of the University of California. All rights reserved.
53 *
54 * Redistribution and use in source and binary forms, with or without
55 * modification, are permitted provided that the following conditions
56 * are met:
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.
69 *
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
80 * SUCH DAMAGE.
81 */
82
83 static void
84 vecswap(const struct bucket **pi, const struct bucket **pj, int n)
85 {
86 if (n <= 0)
87 return;
88
89 do {
90 const struct bucket *t = *pi;
91 *pi++ = *pj;
92 *pj++ = t;
93 } while (--n > 0);
94 }
95
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; \
100 }
101
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)
107 {
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 ));
111 }
112
113 /* Partial sort - only sort elements in the range [left:right] */
114 void
115 qsort_buckets(const struct bucket **a, size_t n,
116 size_t left, size_t right,
117 const enum sort_dir dir)
118 {
119 const struct bucket **pa, **pb, **pc, **pd, **pl, **pm, **pn;
120 int d, r, swap_cnt;
121
122 loop:
123 swap_cnt = 0;
124 if (n < 7) {
125 for (pm = a+1; pm < a+n; pm++)
126 for (pl = pm;
127 (pl > a) && (cmp(pl-1, pl, dir) > 0);
128 pl--)
129 swap(pl, pl-1);
130 return;
131 }
132 pm = a + (n / 2);
133 if (n > 7) {
134 pl = a;
135 pn = a + (n - 1);
136 if (n > 40) {
137 d = (n / 8);
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);
141 }
142 pm = med3(pl, pm, pn, dir);
143 }
144 swap(a, pm);
145 pa = pb = a + 1;
146
147 pc = pd = a + (n - 1);
148 for (;;) {
149 while (pb <= pc && (r = cmp(pb, a, dir)) <= 0) {
150 if (r == 0) {
151 swap_cnt = 1;
152 swap(pa, pb);
153 pa++;
154 }
155 pb++;
156 }
157 while (pb <= pc && (r = cmp(pc, a, dir)) >= 0) {
158 if (r == 0) {
159 swap_cnt = 1;
160 swap(pc, pd);
161 pd--;
162 }
163 pc--;
164 }
165 if (pb > pc)
166 break;
167 swap(pb, pc);
168 swap_cnt = 1;
169 pb++;
170 pc--;
171 }
172 if (swap_cnt == 0) { /* Switch to insertion sort */
173 for (pm = a + 1; pm < a+n; pm++)
174 for (pl = pm;
175 (pl > a) && (cmp(pl-1, pl, dir) > 0);
176 pl--)
177 swap(pl, pl-1);
178 return;
179 }
180
181 pn = a + n;
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 */
190 if (n - r > left)
191 left = 0;
192 else
193 left -= n - r;
194 right -= n - r;
195 a += n - r;
196 n = r;
197 goto loop;
198 }
199 /* qsort(pn - r, r, cmp);*/
200 }
201
202 /* vim:set ts=3 sw=3 tw=78 expandtab: */