Merge remote branch 'origin/master'
[sort-networks.git] / src / sn_stage.h
1 /**
2  * \file sn_stage.h
3  * \brief The sn_stage_t class and associated methods.
4  *
5  * \verbatim
6  * libsortnetwork - src/sn_stage.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_STAGE_H
28 #define SN_STAGE_H 1
29
30 #include <stdio.h>
31
32 #include "sn_comparator.h"
33
34 /**
35  * Struct representing a stage of the comparator network. Don't modify this
36  * struct yourself, use the sn_stage_* methods instead.
37  */
38 struct sn_stage_s
39 {
40   int depth;                    /**< Depth of this stage, where zero means closest to the input. */
41   sn_comparator_t *comparators; /**< Pointer to a list of comparators contained in this stage. */
42   int comparators_num;          /**< Number of comparators in this stage. */
43 };
44 typedef struct sn_stage_s sn_stage_t;
45
46 /**
47  * Direction into which to cut.
48  *
49  * \see sn_network_cut_at, sn_stage_cut_at
50  */
51 enum sn_network_cut_dir_e
52 {
53   DIR_MIN, /**< Assume negative infinity. */
54   DIR_MAX  /**< Assume positive infinity. */
55 };
56
57 #define SN_STAGE_DEPTH(s) (s)->depth
58 #define SN_STAGE_COMP_NUM(s) (s)->comparators_num
59 #define SN_STAGE_COMP_GET(s,n) ((s)->comparators + (n))
60
61 /**
62  * Creates an empty stage and returns a pointer to it. The stage must be freed
63  * using sn_stage_destroy().
64  *
65  * \param depth Depth of the stage within the comparator network. This will be
66  *   re-set by sn_network_stage_add().
67  * \return Pointer to the comparator network or \c NULL on error.
68  */
69 sn_stage_t *sn_stage_create (int depth);
70
71 /**
72  * Clones an existing stage.
73  *
74  * \param s Stage to clone.
75  * \return Copied stage or \c NULL on error. The returned stage must be freed
76  *   using sn_stage_destroy().
77  */
78 sn_stage_t *sn_stage_clone (const sn_stage_t *s);
79
80 /**
81  * Destroys a stage allocated with sn_stage_create() or one of the other
82  * methods returning a \c sn_stage_t. This frees all allocated space.
83  *
84  * \param s The stage to destroy. May be \c NULL.
85  */
86 void sn_stage_destroy (sn_stage_t *s);
87
88 /**
89  * Applies a stage to an array of integers.
90  *
91  * \param s Pointer to the stage.
92  * \param[in,out] values Pointer to integer values to sort. The number of
93  *   integer values pointed to must be at least the number of inputs of the
94  *   comparator network. Otherwise, segmentation faults or memory corruption
95  *   will occur.
96  * \return Zero on success, non-zero on failure.
97  * \see sn_network_sort
98  */
99 int sn_stage_sort (sn_stage_t *s, int *values);
100
101 /**
102  * Adds a comparator to a stage. If this would return in a conflict (a
103  * comparator using on of the line already exists in this stage) an error is
104  * returned.
105  *
106  * \param s Pointer to the stage.
107  * \param c Pointer to a comparator to add. The given comparator is copied. It
108  *   is the caller's responsibility to free c.
109  * \return Zero on success, non-zero on failure.
110  * \see sn_stage_comparator_remove
111  */
112 int sn_stage_comparator_add (sn_stage_t *s, const sn_comparator_t *c);
113
114
115 /**
116  * Removed a comparator from a stage.
117  *
118  * \param s The stage from which to remove the comparator.
119  * \param index The index of the comparator to remove.
120  * \return Zero on success, non-zero on failure.
121  * \see sn_stage_comparator_add
122  */
123 int sn_stage_comparator_remove (sn_stage_t *s, int index);
124
125 /**
126  * Checks whether the given comparator can be added to a stage, i.e. if neither
127  * line if used by another comparator.
128  *
129  * \param s Pointer to the stage.
130  * \param c Pointer to the comparator.
131  * \return Zero if there is no conflict, one if there is a conflict and two if
132  *   the comparator is already present in the stage.
133  */
134 int sn_stage_comparator_check_conflict (sn_stage_t *s, const sn_comparator_t *c);
135
136 /**
137  * Prints the stage to \c STDOUT using a human readable representation.
138  *
139  * \param s The comparator network to display.
140  * \return Zero on success, non-zero on failure.
141  * \see sn_network_show
142  */
143 int sn_stage_show (sn_stage_t *s);
144
145 /**
146  * Inverts a stage by switching the direction of all comparators.
147  *
148  * \param s The stage to invert.
149  * \return Zero on success, non-zero on failure.
150  * \see sn_network_invert
151  */
152 int sn_stage_invert (sn_stage_t *s);
153
154 /**
155  * Shifts a stage (permutes the inputs). Each input is shifted \c sw positions,
156  * higher inputs are "wrapped around".
157  *
158  * \param s The stage to shift.
159  * \param sw The number of positions to shift.
160  * \param inputs_num The number of inputs of the comparator network. This value
161  *   is used to "wrap around" inputs.
162  * \return Zero on success, non-zero on failure.
163  */
164 int sn_stage_shift (sn_stage_t *s, int sw, int inputs_num);
165
166 /**
167  * Swaps two lines. This is used by the algorithm used in
168  * sn_network_normalize() to transform non-standard sort networks to standard
169  * sort networks.
170  *
171  * \param s The stage on which to operate.
172  * \param con0 Index of the first line.
173  * \param con1 Index of the second line.
174  * \return Zero on success, non-zero on failure.
175  * \see sn_network_normalize(), sn_comparator_swap()
176  */
177 int sn_stage_swap (sn_stage_t *s, int con0, int con1);
178
179 /**
180  * Removes an input / line by assuming positive or negative infinity is applied
181  * to one line.
182  *
183  * \param s The stage to work with.
184  * \param input The input / line on which is assumed to be positive or negative
185  *   infinity.
186  * \param dir Direction in which to cut; whether positive or negative infinity
187  *   is assumed.
188  * \return The new position of the infinite value on success or less than zero
189  *   on failure.
190  * \see sn_network_cut_at
191  */
192 int sn_stage_cut_at (sn_stage_t *s, int input, enum sn_network_cut_dir_e dir);
193
194 /* FIXME: Documentation */
195 int sn_stage_cut (sn_stage_t *s, int *mask, sn_stage_t **prev);
196
197 /**
198  * Remove an input from a stage, remove all touching comparators and adapt the
199  * input indexes of all remaining comparators.
200  *
201  * \param s The stage from which to remove the input.
202  * \param input The index of the line which to remove.
203  * \return Zero on success, non-zero on failure.
204  */
205 int sn_stage_remove_input (sn_stage_t *s, int input);
206
207 /**
208  * Reads a stage from a \c FILE pointer and return the newly allocated stage.
209  *
210  * \param fh The file handle.
211  * \return Pointer to a newly allocated stage or \c NULL on error.
212  * \see sn_stage_write
213  */
214 sn_stage_t *sn_stage_read (FILE *fh);
215
216 /**
217  * Writes a stage to a \c FILE pointer.
218  *
219  * \param s The stage to write.
220  * \param fh The file handle to write to.
221  * \return Zero on success, non-zero on failure.
222  * \see sn_stage_read
223  */
224 int sn_stage_write (sn_stage_t *s, FILE *fh);
225
226 /**
227  * Creates a serialized form of the given stage. The serialized form is
228  * byte-order independent and can easily be sent over a computer network.
229  *
230  * \param s The stage to serialize.
231  * \param[out] ret_buffer Pointer to a pointer into which the location of the
232  *   allocated memory will be written. It is the caller's responsibility to
233  *   free this memory.
234  * \param[out] ret_buffer_size Pointer to a size_t into which the size of the
235  *   allocated memory will be written.
236  * \return Zero on success, non-zero on failure.
237  * \see sn_stage_unserialize(), sn_network_serialize()
238  */
239 int sn_stage_serialize (sn_stage_t *s,
240     char **ret_buffer, size_t *ret_buffer_size);
241
242 /**
243  * Creates a stage from its serialized form.
244  *
245  * \param buffer Pointer to a buffer containing the stage in serialized form.
246  * \param buffer_size Size of the buffer (in bytes).
247  * \return Pointer to the newly allocated stage or \c NULL on error.
248  * \see sn_stage_serialize(), sn_network_unserialize()
249  */
250 sn_stage_t *sn_stage_unserialize (char **buffer, size_t *buffer_size);
251
252 #endif /* SN_STAGE_H */
253
254 /* vim: set shiftwidth=2 softtabstop=2 : */