2 * collectd - src/sn_comparator.c
3 * Copyright (C) 2008-2010 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <ff at octo.it>
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
25 #ifndef _POSIX_C_SOURCE
26 # define _POSIX_C_SOURCE 200112L
32 #include "sn_comparator.h"
34 sn_comparator_t *sn_comparator_create (int min, int max)
38 c = (sn_comparator_t *) malloc (sizeof (sn_comparator_t));
41 memset (c, '\0', sizeof (sn_comparator_t));
47 } /* sn_comparator_t *sn_comparator_create */
49 void sn_comparator_destroy (sn_comparator_t *c)
53 } /* void sn_comparator_destroy */
55 void sn_comparator_invert (sn_comparator_t *c)
62 } /* void sn_comparator_invert */
64 void sn_comparator_shift (sn_comparator_t *c, int sw, int inputs_num)
66 c->min = (c->min + sw) % inputs_num;
67 c->max = (c->max + sw) % inputs_num;
68 } /* void sn_comparator_shift */
70 void sn_comparator_swap (sn_comparator_t *c, int con0, int con1)
76 else if (c->min == con1)
85 else if (c->max == con1)
89 } /* void sn_comparator_swap */
91 int sn_comparator_compare (const void *v0, const void *v1)
93 sn_comparator_t *c0 = (sn_comparator_t *) v0;
94 sn_comparator_t *c1 = (sn_comparator_t *) v1;
96 if (SN_COMP_LEFT (c0) < SN_COMP_LEFT (c1))
98 else if (SN_COMP_LEFT (c0) > SN_COMP_LEFT (c1))
100 else if (SN_COMP_RIGHT (c0) < SN_COMP_RIGHT (c1))
102 else if (SN_COMP_RIGHT (c0) > SN_COMP_RIGHT (c1))
106 } /* int sn_comparator_compare */
108 /* vim: set shiftwidth=2 softtabstop=2 : */