2 * libsortnetwork - src/sn_comparator.c
3 * Copyright (C) 2008-2010 Florian octo Forster
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.
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
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
20 * Florian octo Forster <ff at octo.it>
23 #ifndef _ISOC99_SOURCE
24 # define _ISOC99_SOURCE
26 #ifndef _POSIX_C_SOURCE
27 # define _POSIX_C_SOURCE 200112L
33 #include "sn_comparator.h"
35 sn_comparator_t *sn_comparator_create (int min, int max)
39 c = (sn_comparator_t *) malloc (sizeof (sn_comparator_t));
42 memset (c, '\0', sizeof (sn_comparator_t));
50 } /* sn_comparator_t *sn_comparator_create */
52 void sn_comparator_destroy (sn_comparator_t *c)
54 if (c->free_func != NULL)
55 c->free_func (c->user_data);
59 } /* void sn_comparator_destroy */
61 void sn_comparator_invert (sn_comparator_t *c)
68 } /* void sn_comparator_invert */
70 void sn_comparator_shift (sn_comparator_t *c, int sw, int inputs_num)
72 c->min = (c->min + sw) % inputs_num;
73 c->max = (c->max + sw) % inputs_num;
74 } /* void sn_comparator_shift */
76 void sn_comparator_swap (sn_comparator_t *c, int con0, int con1)
82 else if (c->min == con1)
91 else if (c->max == con1)
95 } /* void sn_comparator_swap */
97 int sn_comparator_compare (const sn_comparator_t *c0,
98 const sn_comparator_t *c1)
100 if (SN_COMP_LEFT (c0) < SN_COMP_LEFT (c1))
102 else if (SN_COMP_LEFT (c0) > SN_COMP_LEFT (c1))
104 else if (SN_COMP_RIGHT (c0) < SN_COMP_RIGHT (c1))
106 else if (SN_COMP_RIGHT (c0) > SN_COMP_RIGHT (c1))
110 } /* int sn_comparator_compare */
112 uint64_t sn_comparator_get_hashval (const sn_comparator_t *c) /* {{{ */
117 /* 100937 and 103319 are some random prime numbers */
118 return ((((uint64_t) c->min) * 100937)
119 + (((uint64_t) c->max) * 103319));
120 } /* }}} uint32_t sn_comparator_get_hashval */
122 /* vim: set shiftwidth=2 softtabstop=2 : */