src/sn_comparator.h: Add Doxygen documentation.
[sort-networks.git] / src / sn_comparator.h
1 /**
2  * \file sn_comparator.h
3  * \brief The sn_comparator_t class and associated methods.
4  *
5  * \verbatim
6  * libsortnetwork - src/sn_comparator.h
7  * Copyright (C) 2008-2010  Florian octo Forster
8  *
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.
12  *
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.
17  *
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
21  *
22  * Authors:
23  *   Florian octo Forster <ff at octo.it>
24  * \endverbatim
25  **/
26
27 #ifndef SN_COMPARATOR_H
28 #define SN_COMPARATOR_H 1
29
30 /**
31  * Struct representing a comparator. Don't access the members of this struct
32  * directly, use the macros below instead.
33  */
34 struct sn_comparator_s
35 {
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. */
38 };
39 typedef struct sn_comparator_s sn_comparator_t;
40
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
49
50 /**
51  * Allocates, initializes and returns a new comparator object. The returned
52  * pointer must be freed by sn_comparator_destroy().
53  *
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.
57  */
58 sn_comparator_t *sn_comparator_create (int min, int max);
59
60 /**
61  * Destroys a comparator object, freeing all allocated space.
62  *
63  * \param c Pointer to the comparator object. This pointer must be allocated by
64  *   sn_comparator_create().
65  */
66 void sn_comparator_destroy (sn_comparator_t *c);
67
68 /**
69  * Inverts a comparator by switching the minimum and maximum indexes stored in
70  * the comparator.
71  *
72  * \param c Pointer to the comparator.
73  */
74 void sn_comparator_invert (sn_comparator_t *c);
75
76 /**
77  * Shifts the indexes stored in the comparator by a constant offset. If the
78  * index becomes too large, it will "wrap around".
79  *
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.
84  */
85 void sn_comparator_shift (sn_comparator_t *c, int sw, int inputs_num);
86
87 /**
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,
90  * this is a no-op.
91  *
92  * \param c The comparator to modify.
93  * \param con0 Index of the first line.
94  * \param con1 Index of the second line.
95  */
96 void sn_comparator_swap (sn_comparator_t *c, int con0, int con1);
97
98 /**
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.
102  *
103  * \param c0 Pointer to the first comparator.
104  * \param c1 Pointer to the second comparator.
105  */
106 int sn_comparator_compare (const sn_comparator_t *c0,
107     const sn_comparator_t *c1);
108
109 #endif /* SN_COMPARATOR_H */
110
111 /* vim: set shiftwidth=2 softtabstop=2 : */