src/sn_{network,stage,comparator}.[ch]: Implement sn_network_get_hashval() and friends.
[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 library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation; either version 2.1 of the License, or (at
8  * your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors:
20  *   Florian octo Forster <ff at octo.it>
21  **/
22
23 #ifndef _ISOC99_SOURCE
24 # define _ISOC99_SOURCE
25 #endif
26 #ifndef _POSIX_C_SOURCE
27 # define _POSIX_C_SOURCE 200112L
28 #endif
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "sn_comparator.h"
34
35 sn_comparator_t *sn_comparator_create (int min, int max)
36 {
37   sn_comparator_t *c;
38
39   c = (sn_comparator_t *) malloc (sizeof (sn_comparator_t));
40   if (c == NULL)
41     return (NULL);
42   memset (c, '\0', sizeof (sn_comparator_t));
43
44   c->min = min;
45   c->max = max;
46   c->user_data = NULL;
47   c->free_func = NULL;
48
49   return (c);
50 } /* sn_comparator_t *sn_comparator_create */
51
52 void sn_comparator_destroy (sn_comparator_t *c)
53 {
54   if (c->free_func != NULL)
55     c->free_func (c->user_data);
56
57   if (c != NULL)
58     free (c);
59 } /* void sn_comparator_destroy */
60
61 void sn_comparator_invert (sn_comparator_t *c)
62 {
63   int max = c->min;
64   int min = c->max;
65
66   c->min = min;
67   c->max = max;
68 } /* void sn_comparator_invert */
69
70 void sn_comparator_shift (sn_comparator_t *c, int sw, int inputs_num)
71 {
72   c->min = (c->min + sw) % inputs_num;
73   c->max = (c->max + sw) % inputs_num;
74 } /* void sn_comparator_shift */
75
76 void sn_comparator_swap (sn_comparator_t *c, int con0, int con1)
77 {
78   if (c->min == con0)
79   {
80     c->min = con1;
81   }
82   else if (c->min == con1)
83   {
84     c->min = con0;
85   }
86
87   if (c->max == con0)
88   {
89     c->max = con1;
90   }
91   else if (c->max == con1)
92   {
93     c->max = con0;
94   }
95 } /* void sn_comparator_swap */
96
97 int sn_comparator_compare (const sn_comparator_t *c0,
98     const sn_comparator_t *c1)
99 {
100   if (SN_COMP_LEFT (c0) < SN_COMP_LEFT (c1))
101     return (-1);
102   else if (SN_COMP_LEFT (c0) > SN_COMP_LEFT (c1))
103     return (1);
104   else if (SN_COMP_RIGHT (c0) < SN_COMP_RIGHT (c1))
105     return (-1);
106   else if (SN_COMP_RIGHT (c0) > SN_COMP_RIGHT (c1))
107     return (1);
108   else
109     return (0);
110 } /* int sn_comparator_compare */
111
112 uint32_t sn_comparator_get_hashval (const sn_comparator_t *c) /* {{{ */
113 {
114   if (c == NULL)
115     return (0);
116
117   /* 100937 and 103319 are some random prime numbers */
118   return ((((uint32_t) c->min) * 100937)
119       + (((uint32_t) c->max) * 103319));
120 } /* }}} uint32_t sn_comparator_get_hashval */
121
122 /* vim: set shiftwidth=2 softtabstop=2 : */