2 * \file sn_comparator.h
3 * \brief The sn_comparator_t class and associated methods.
6 * libsortnetwork - src/sn_comparator.h
7 * Copyright (C) 2008-2010 Florian octo Forster
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; only version 2 of the License is applicable.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 * Florian octo Forster <ff at octo.it>
27 #ifndef SN_COMPARATOR_H
28 #define SN_COMPARATOR_H 1
31 * Struct representing a comparator. Don't access the members of this struct
32 * directly, use the macros below instead.
34 struct sn_comparator_s
36 int min; /**< Index of the line onto which the smaller element will be put. */
37 int max; /**< Index of the line onto which the larger element will be put. */
39 typedef struct sn_comparator_s sn_comparator_t;
41 /** Returns the "left" line, i.e. the line with the smaller index. */
42 #define SN_COMP_LEFT(c) (((c)->min < (c)->max) ? (c)->min : (c)->max)
43 /** Returns the "right" line, i.e. the line with the larger index. */
44 #define SN_COMP_RIGHT(c) (((c)->min > (c)->max) ? (c)->min : (c)->max)
45 /** Returns the index of the line onto which the smaller element will be put. */
46 #define SN_COMP_MIN(c) (c)->min
47 /** Returns the index of the line onto which the larger element will be put. */
48 #define SN_COMP_MAX(c) (c)->max
51 * Allocates, initializes and returns a new comparator object. The returned
52 * pointer must be freed by sn_comparator_destroy().
54 * \param min Index of the line onto which the smaller element will be put.
55 * \param max Index of the line onto which the larger element will be put.
56 * \return The newly allocated object or \c NULL on failure.
58 sn_comparator_t *sn_comparator_create (int min, int max);
61 * Destroys a comparator object, freeing all allocated space.
63 * \param c Pointer to the comparator object. This pointer must be allocated by
64 * sn_comparator_create().
66 void sn_comparator_destroy (sn_comparator_t *c);
69 * Inverts a comparator by switching the minimum and maximum indexes stored in
72 * \param c Pointer to the comparator.
74 void sn_comparator_invert (sn_comparator_t *c);
77 * Shifts the indexes stored in the comparator by a constant offset. If the
78 * index becomes too large, it will "wrap around".
80 * \param c The comparator to modify.
81 * \param sw The offset by which to shift the indexes.
82 * \param inputs_num The number of lines / inputs of the comparator network.
83 * This number is used to wrap large indexes around.
85 void sn_comparator_shift (sn_comparator_t *c, int sw, int inputs_num);
88 * Swaps two line indexes by replacing all occurrences of one index with
89 * another index and vice versa. If the comparator does not touch either line,
92 * \param c The comparator to modify.
93 * \param con0 Index of the first line.
94 * \param con1 Index of the second line.
96 void sn_comparator_swap (sn_comparator_t *c, int con0, int con1);
99 * Compares two comparators and returns less than zero, zero, or greater than
100 * zero if the first comparator is smaller than, equal to or larger than the
101 * second comparator, respectively.
103 * \param c0 Pointer to the first comparator.
104 * \param c1 Pointer to the second comparator.
106 int sn_comparator_compare (const sn_comparator_t *c0,
107 const sn_comparator_t *c1);
109 #endif /* SN_COMPARATOR_H */
111 /* vim: set shiftwidth=2 softtabstop=2 : */