Debian Codegraph @

src/sort.c

raw symbols SCIP
1 /*
2 * The dhcpd-pools has BSD 2-clause license which also known as "Simplified
3 * BSD License" or "FreeBSD License".
4 *
5 * Copyright 2006- Sami Kerola. All rights reserved.
6 * Copyright 2012 Cheer Xiao.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the
18 * distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * The views and conclusions contained in the software and documentation are
33 * those of the authors and should not be interpreted as representing
34 * official policies, either expressed or implied, of Sami Kerola.
35 */
36
37 /*! \file sort.c
38 * \brief Functions to sort output.
39 */
40
41 #include <config.h>
42
43 #include <math.h>
44 #include <stdint.h>
45 #include <stdlib.h>
46 #include <string.h>
47
48 #include "error.h"
49 #include "progname.h"
50 #include "quote.h"
51 #include "xalloc.h"
52
53 #include "dhcpd-pools.h"
54
55 /*! \brief Compare IP address, with IPv4/v6 determination.
56 * \param a Binary IP address.
57 * \param b Binary IP address.
58 * \return If a < b return -1, if a < b return 1, when they are equal return 0.
59 */
60 int ipcomp_init(const union ipaddr_t *restrict a __attribute__ ((unused)),
61 const union ipaddr_t *restrict b __attribute__ ((unused)))
62 {
63 return 0;
64 }
65
66 int ipcomp_v4(const union ipaddr_t *restrict a, const union ipaddr_t *restrict b)
67 {
68 if (a->v4 < b->v4)
69 return -1;
70 if (a->v4 > b->v4)
71 return 1;
72 return 0;
73 }
74
75 int ipcomp_v6(const union ipaddr_t *restrict a, const union ipaddr_t *restrict b)
76 {
77 return memcmp(&a->v6, &b->v6, sizeof(a->v6));
78 }
79
80 /*! \brief Compare IP address in leases_t structure, with IPv4/v6 determination.
81 * \param a Binary IP address.
82 * \param b Binary IP address.
83 * \return If a < b return -1, if a < b return 1, when they are equal return 0.
84 */
85 int leasecomp_init(const struct leases_t *restrict a __attribute__ ((unused)),
86 const struct leases_t *restrict b __attribute__ ((unused)))
87 {
88 return 0;
89 }
90
91 int leasecomp_v4(const struct leases_t *restrict a, const struct leases_t *restrict b)
92 {
93 if (a->ip.v4 < b->ip.v4)
94 return -1;
95 if (a->ip.v4 > b->ip.v4)
96 return 1;
97 return 0;
98 }
99
100 int leasecomp_v6(const struct leases_t *restrict a, const struct leases_t *restrict b)
101 {
102 return memcmp(&a->ip.v6, &b->ip.v6, sizeof(a->ip.v6));
103 }
104
105 /*! \brief Compare IP address in leases. Suitable for sorting range table.
106 * \param r1 A range structure.
107 * \param r2 A range structure.
108 * \return Return pas through from ipcomp.
109 */
110 int rangecomp(const void *restrict r1, const void *restrict r2)
111 {
112 return ipcomp(&((const struct range_t *)r1)->first_ip,
113 &((const struct range_t *)r2)->first_ip);
114 }
115
116 /*! \brief Compare two doubles.
117 * \param f1,f2 Data to compare.
118 * \return Like strcmp.
119 */
120 int comp_double(double f1, double f2)
121 {
122 if (isless(f1, f2))
123 return -1;
124 else if (isless(f2, f1))
125 return 1;
126 return 0;
127 }
128
129
130 /*! \brief Compare two range_t by their first_ip.
131 * \param r1,r2 Pointers to data to compare.
132 * \return Like strcmp.
133 */
134 int comp_ip(struct range_t *r1, struct range_t *r2)
135 {
136 return ipcomp(&r1->first_ip, &r2->first_ip);
137 }
138
139 /*! \brief Compare two range_t by their capacity.
140 * \param r1,r2 Pointers to data to compare.
141 * \return Like strcmp.
142 */
143 int comp_max(struct range_t *r1, struct range_t *r2)
144 {
145 return comp_double(get_range_size(r1), get_range_size(r2));
146 }
147
148 /*! \brief Compare two range_t by their current usage.
149 * \param r1,r2 Pointers to data to compare.
150 * \return Like strcmp.
151 */
152 int comp_cur(struct range_t *r1, struct range_t *r2)
153 {
154 return comp_double(r1->count, r2->count);
155 }
156
157 /*! \brief Compare two range_t by their current usage percentage.
158 * \param r1,r2 Pointers to data to compare.
159 * \return Like strcmp.
160 */
161 int comp_percent(struct range_t *r1, struct range_t *r2)
162 {
163 return comp_double(ret_percent(*r1), ret_percent(*r2));
164 }
165
166 /*! \brief Compare two range_t by their touched addresses.
167 * \param r1,r2 Pointers to data to compare.
168 * \return Like strcmp.
169 */
170 int comp_touched(struct range_t *r1, struct range_t *r2)
171 {
172 return comp_double(r1->touched, r2->touched);
173 }
174
175 /*! \brief Compare two range_t by their touched and in use addresses.
176 * \param r1,r2 Pointers to data to compare.
177 * \return Like strcmp.
178 */
179 int comp_tc(struct range_t *r1, struct range_t *r2)
180 {
181 return comp_double(ret_tc(*r1), ret_tc(*r2));
182 }
183
184 /*! \brief Compare two range_t by their touched and in use percentage.
185 * \param r1,r2 Pointers to data to compare.
186 * \return Like strcmp.
187 */
188 int comp_tcperc(struct range_t *r1, struct range_t *r2)
189 {
190 return comp_double(ret_tcperc(*r1), ret_tcperc(*r2));
191 }
192
193 /*! \brief Percentage in use in range.
194 * \param r A range structure.
195 * \return Usage percentage of the given range.
196 */
197 double ret_percent(struct range_t r)
198 {
199 return r.count / get_range_size(&r);
200 }
201
202 /*! \brief Touched and in use in range
203 * \param r A range structure.
204 * \return Number of touched or in use addresses in the given range.
205 */
206 double ret_tc(struct range_t r)
207 {
208 return (r.count + r.touched);
209 }
210
211 /*! \brief Return percentage of addresses touched and in use in range.
212 * \param r A range structure.
213 * \return Percentage of touched or in use addresses in the given range.
214 */
215 double ret_tcperc(struct range_t r)
216 {
217 return ret_tc(r) / get_range_size(&r);
218 }
219
220 /*! \brief Sort field selector.
221 * \param c Symbolic name of a sort by character.
222 * The sort algorithms are stabile, which means multiple sorts can be
223 * specified and they do not mess the result of previous sort. The sort
224 * algorithms are used via function pointer, that gets to be reassigned.
225 * \return Return the selected compare function.
226 */
227 comparer_t field_selector(char c)
228 {
229 switch (c) {
230 case 'n':
231 return NULL;
232 case 'i':
233 return comp_ip;
234 case 'm':
235 return comp_max;
236 case 'c':
237 return comp_cur;
238 case 'p':
239 return comp_percent;
240 case 't':
241 return comp_touched;
242 case 'T':
243 return comp_tc;
244 case 'e':
245 return comp_tcperc;
246 default:
247 {
248 char str[2] = { c, '\0' };
249 error(EXIT_FAILURE, 0, "field_selector: unknown sort order: %s",
250 quote(str));
251 }
252 }
253 return NULL;
254 }
255
256 /*! \brief Perform requested sorting.
257 * \param left The left side of the merge sort.
258 * \param right The right side of the merge sort.
259 * \return Relevant for merge sort decision.
260 */
261 static int merge(struct conf_t *state, struct range_t *restrict left, struct range_t *restrict right)
262 {
263 struct output_sort *p;
264 int ret;
265
266 for (p = state->sorts; p; p = p->next) {
267 if (p->func == NULL) {
268 /* String sorting is special. */
269 ret = strcmp(left->shared_net->name, right->shared_net->name);
270 } else {
271 /* Range sorts are common. */
272 ret = p->func(left, right);
273 }
274 if (0 < ret)
275 return (0);
276 if (ret < 0)
277 return (1);
278 }
279 /* this is reached if nothing was sorted */
280 return (0);
281 }
282
283 /*! \brief Mergesort for range table.
284 */
285 void mergesort_ranges(struct conf_t *state, struct range_t *restrict orig, unsigned int size,
286 struct range_t *restrict temp, const int root_call)
287 {
288 unsigned int left, i, u_right;
289 struct range_t hold;
290 /* Merge sort split size */
291 static const unsigned int MIN_MERGE_SIZE = 8;
292
293 if (temp == NULL)
294 temp = xmalloc(sizeof(struct range_t) * size);
295
296 if (size < MIN_MERGE_SIZE) {
297 for (left = 0; left < size; left++) {
298 int s_right;
299
300 hold = *(orig + left);
301 for (s_right = left - 1; 0 <= s_right; s_right--) {
302 if (merge(state, (orig + s_right), &hold))
303 break;
304 *(orig + s_right + 1) = *(orig + s_right);
305 }
306 *(orig + s_right + 1) = hold;
307 }
308 if (root_call)
309 free(temp);
310 return;
311 }
312 mergesort_ranges(state, orig, size / 2, temp, 0);
313 mergesort_ranges(state, orig + size / 2, size - size / 2, temp, 0);
314 left = 0;
315 u_right = size / 2;
316 i = 0;
317 while (left < size / 2 && u_right < size) {
318 if (merge(state, (orig + left), (orig + u_right))) {
319 *(temp + i) = *(orig + left);
320 left++;
321 } else {
322 *(temp + i) = *(orig + u_right);
323 u_right++;
324 }
325 i++;
326 }
327 while (left < size / 2) {
328 *(temp + i) = *(orig + left);
329 left++;
330 i++;
331 }
332 while (u_right < size) {
333 *(temp + i) = *(orig + u_right);
334 u_right++;
335 i++;
336 }
337 memcpy(orig, temp, size * sizeof(struct range_t));
338
339 if (root_call)
340 free(temp);
341 }