4176c7c9fed502f7f1c7ba37561b945576224451
[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
46   return (c);
47 } /* sn_comparator_t *sn_comparator_create */
48
49 void sn_comparator_destroy (sn_comparator_t *c)
50 {
51   if (c != NULL)
52     free (c);
53 } /* void sn_comparator_destroy */
54
55 void sn_comparator_invert (sn_comparator_t *c)
56 {
57   int max = c->min;
58   int min = c->max;
59
60   c->min = min;
61   c->max = max;
62 } /* void sn_comparator_invert */
63
64 void sn_comparator_shift (sn_comparator_t *c, int sw, int inputs_num)
65 {
66   c->min = (c->min + sw) % inputs_num;
67   c->max = (c->max + sw) % inputs_num;
68 } /* void sn_comparator_shift */
69
70 void sn_comparator_swap (sn_comparator_t *c, int con0, int con1)
71 {
72   if (c->min == con0)
73   {
74     c->min = con1;
75   }
76   else if (c->min == con1)
77   {
78     c->min = con0;
79   }
80
81   if (c->max == con0)
82   {
83     c->max = con1;
84   }
85   else if (c->max == con1)
86   {
87     c->max = con0;
88   }
89 } /* void sn_comparator_swap */
90
91 int sn_comparator_compare (const void *v0, const void *v1)
92 {
93   sn_comparator_t *c0 = (sn_comparator_t *) v0;
94   sn_comparator_t *c1 = (sn_comparator_t *) v1;
95
96   if (SN_COMP_LEFT (c0) < SN_COMP_LEFT (c1))
97     return (-1);
98   else if (SN_COMP_LEFT (c0) > SN_COMP_LEFT (c1))
99     return (1);
100   else if (SN_COMP_RIGHT (c0) < SN_COMP_RIGHT (c1))
101     return (-1);
102   else if (SN_COMP_RIGHT (c0) > SN_COMP_RIGHT (c1))
103     return (1);
104   else
105     return (0);
106 } /* int sn_comparator_compare */
107
108 /* vim: set shiftwidth=2 softtabstop=2 : */