a81957696e5591ee7461aedc5693614c9d7f7b8a
[sort-networks.git] / src / sn_comparator.c
1 /**
2  * libsortnetwork - src/sn_comparator.c
3  * Copyright (C) 2008-2010  Florian octo Forster
4  *
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.
8  *
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.
13  *
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
17  *
18  * Authors:
19  *   Florian octo Forster <ff at octo.it>
20  **/
21
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
24 #endif
25 #ifndef _POSIX_C_SOURCE
26 # define _POSIX_C_SOURCE 200112L
27 #endif
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "sn_comparator.h"
33
34 sn_comparator_t *sn_comparator_create (int min, int max)
35 {
36   sn_comparator_t *c;
37
38   c = (sn_comparator_t *) malloc (sizeof (sn_comparator_t));
39   if (c == NULL)
40     return (NULL);
41   memset (c, '\0', sizeof (sn_comparator_t));
42
43   c->min = min;
44   c->max = max;
45   c->user_data = NULL;
46   c->free_func = NULL;
47
48   return (c);
49 } /* sn_comparator_t *sn_comparator_create */
50
51 void sn_comparator_destroy (sn_comparator_t *c)
52 {
53   if (c->free_func != NULL)
54     c->free_func (c->user_data);
55
56   if (c != NULL)
57     free (c);
58 } /* void sn_comparator_destroy */
59
60 void sn_comparator_invert (sn_comparator_t *c)
61 {
62   int max = c->min;
63   int min = c->max;
64
65   c->min = min;
66   c->max = max;
67 } /* void sn_comparator_invert */
68
69 void sn_comparator_shift (sn_comparator_t *c, int sw, int inputs_num)
70 {
71   c->min = (c->min + sw) % inputs_num;
72   c->max = (c->max + sw) % inputs_num;
73 } /* void sn_comparator_shift */
74
75 void sn_comparator_swap (sn_comparator_t *c, int con0, int con1)
76 {
77   if (c->min == con0)
78   {
79     c->min = con1;
80   }
81   else if (c->min == con1)
82   {
83     c->min = con0;
84   }
85
86   if (c->max == con0)
87   {
88     c->max = con1;
89   }
90   else if (c->max == con1)
91   {
92     c->max = con0;
93   }
94 } /* void sn_comparator_swap */
95
96 int sn_comparator_compare (const sn_comparator_t *c0,
97     const sn_comparator_t *c1)
98 {
99   if (SN_COMP_LEFT (c0) < SN_COMP_LEFT (c1))
100     return (-1);
101   else if (SN_COMP_LEFT (c0) > SN_COMP_LEFT (c1))
102     return (1);
103   else if (SN_COMP_RIGHT (c0) < SN_COMP_RIGHT (c1))
104     return (-1);
105   else if (SN_COMP_RIGHT (c0) > SN_COMP_RIGHT (c1))
106     return (1);
107   else
108     return (0);
109 } /* int sn_comparator_compare */
110
111 /* vim: set shiftwidth=2 softtabstop=2 : */