Bump copyright years.
[darkstat] / hosts_sort.c
1 /* darkstat 3
2 * copyright (c) 2001-2012 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 "cdefs.h"
11 #include "err.h"
12 #include "hosts_db.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 case LASTSEEN:
37 a = (*x)->u.host.last_seen_mono;
38 b = (*y)->u.host.last_seen_mono;
39 break;
40 default:
41 errx(1, "cmp: unknown direction: %d", dir);
42 }
43
44 if (a < b) return (1);
45 else if (a > b) return (-1);
46 else return (0);
47 }
48
49 /*
50 * The quicksort code is derived from FreeBSD's
51 * src/lib/libc/stdlib/qsort.c v1.12
52 */
53
54 /*-
55 * Copyright (c) 1992, 1993
56 * The Regents of the University of California. All rights reserved.
57 *
58 * Redistribution and use in source and binary forms, with or without
59 * modification, are permitted provided that the following conditions
60 * are met:
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.
73 *
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
84 * SUCH DAMAGE.
85 */
86
87 static void
88 vecswap(const struct bucket **pi, const struct bucket **pj, int n)
89 {
90 if (n <= 0)
91 return;
92
93 do {
94 const struct bucket *t = *pi;
95 *pi++ = *pj;
96 *pj++ = t;
97 } while (--n > 0);
98 }
99
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; \
104 }
105
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)
111 {
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 ));
115 }
116
117 /* Partial sort - only sort elements in the range [left:right] */
118 void
119 qsort_buckets(const struct bucket **a, size_t n,
120 size_t left, size_t right,
121 const enum sort_dir dir)
122 {
123 const struct bucket **pa, **pb, **pc, **pd, **pl, **pm, **pn;
124 int d, r, swap_cnt;
125
126 loop:
127 swap_cnt = 0;
128 if (n < 7) {
129 for (pm = a+1; pm < a+n; pm++)
130 for (pl = pm;
131 (pl > a) && (cmp(pl-1, pl, dir) > 0);
132 pl--)
133 swap(pl, pl-1);
134 return;
135 }
136 pm = a + (n / 2);
137 if (n > 7) {
138 pl = a;
139 pn = a + (n - 1);
140 if (n > 40) {
141 d = (n / 8);
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);
145 }
146 pm = med3(pl, pm, pn, dir);
147 }
148 swap(a, pm);
149 pa = pb = a + 1;
150
151 pc = pd = a + (n - 1);
152 for (;;) {
153 while (pb <= pc && (r = cmp(pb, a, dir)) <= 0) {
154 if (r == 0) {
155 swap_cnt = 1;
156 swap(pa, pb);
157 pa++;
158 }
159 pb++;
160 }
161 while (pb <= pc && (r = cmp(pc, a, dir)) >= 0) {
162 if (r == 0) {
163 swap_cnt = 1;
164 swap(pc, pd);
165 pd--;
166 }
167 pc--;
168 }
169 if (pb > pc)
170 break;
171 swap(pb, pc);
172 swap_cnt = 1;
173 pb++;
174 pc--;
175 }
176 if (swap_cnt == 0) { /* Switch to insertion sort */
177 for (pm = a + 1; pm < a+n; pm++)
178 for (pl = pm;
179 (pl > a) && (cmp(pl-1, pl, dir) > 0);
180 pl--)
181 swap(pl, pl-1);
182 return;
183 }
184
185 pn = a + n;
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 */
194 if (n - r > left)
195 left = 0;
196 else
197 left -= n - r;
198 right -= n - r;
199 a += n - r;
200 n = r;
201 goto loop;
202 }
203 /* qsort(pn - r, r, cmp);*/
204 }
205
206 /* vim:set ts=3 sw=3 tw=78 expandtab: */