2 * libsortnetwork - 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));
49 } /* sn_comparator_t *sn_comparator_create */
51 void sn_comparator_destroy (sn_comparator_t *c)
53 if (c->free_func != NULL)
54 c->free_func (c->user_data);
58 } /* void sn_comparator_destroy */
60 void sn_comparator_invert (sn_comparator_t *c)
67 } /* void sn_comparator_invert */
69 void sn_comparator_shift (sn_comparator_t *c, int sw, int inputs_num)
71 c->min = (c->min + sw) % inputs_num;
72 c->max = (c->max + sw) % inputs_num;
73 } /* void sn_comparator_shift */
75 void sn_comparator_swap (sn_comparator_t *c, int con0, int con1)
81 else if (c->min == con1)
90 else if (c->max == con1)
94 } /* void sn_comparator_swap */
96 int sn_comparator_compare (const sn_comparator_t *c0,
97 const sn_comparator_t *c1)
99 if (SN_COMP_LEFT (c0) < SN_COMP_LEFT (c1))
101 else if (SN_COMP_LEFT (c0) > SN_COMP_LEFT (c1))
103 else if (SN_COMP_RIGHT (c0) < SN_COMP_RIGHT (c1))
105 else if (SN_COMP_RIGHT (c0) > SN_COMP_RIGHT (c1))
109 } /* int sn_comparator_compare */
111 /* vim: set shiftwidth=2 softtabstop=2 : */