src/sn_{comparator,network,random,stage}.[ch]: Change license to LGPLv2.1+.
[sort-networks.git] / src / sn_network.h
1 /**
2  * \file sn_network.h
3  * \brief The sn_network_t class and associated methods.
4  *
5  * This is more details about what this file does.
6  *
7  * \verbatim
8  * libsortnetwork - src/sn_network.h
9  * Copyright (C) 2008-2010  Florian octo Forster
10  *
11  * This library is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or (at
14  * your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
19  * for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this library; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
24  *
25  * Authors:
26  *   Florian octo Forster <ff at octo.it>
27  * \endverbatim
28  **/
29
30 #ifndef SN_NETWORK_H
31 #define SN_NETWORK_H 1
32
33 #include <stdio.h>
34
35 #include "sn_comparator.h"
36 #include "sn_stage.h"
37
38 /**
39  * The global struct representing a comparator or sort network.
40  */
41 struct sn_network_s
42 {
43   int inputs_num;       /**< Number of inputs of the comparator network. */
44   sn_stage_t **stages;  /**< Array of pointers to the stages of a comparator network. */
45   int stages_num;       /**< Number of stages in this comparator network. */
46 };
47 typedef struct sn_network_s sn_network_t;
48
49 #define SN_NETWORK_STAGE_NUM(n) (n)->stages_num
50 #define SN_NETWORK_STAGE_GET(n,i) ((n)->stages[i])
51 #define SN_NETWORK_INPUT_NUM(n) (n)->inputs_num
52
53 /**
54  * Creates an empty comparator network and returns a pointer to it. The
55  * comparator network must be freed using sn_network_destroy().
56  *
57  * \param inputs_num Number of inputs the comparator network has.
58  * \return Pointer to the comparator network or \c NULL on error.
59  */
60 sn_network_t *sn_network_create (int inputs_num);
61
62 /**
63  * Clones an existing comparator network.
64  *
65  * \param n Comparator network to clone.
66  * \return Copied sort network or \c NULL on error. The returned network must
67  *   be freed using sn_network_destroy().
68  */
69 sn_network_t *sn_network_clone (const sn_network_t *n);
70
71 /**
72  * Destroys a comparator network allocated with sn_network_create() or one of
73  * the other methods returning a \c sn_network_t. This frees all allocated
74  * space.
75  *
76  * \param n The comparator network to destroy. May be \c NULL.
77  */
78 void sn_network_destroy (sn_network_t *n);
79
80 /**
81  * Creates a new sort network using Batcher's Odd-Even-Mergesort algorithm.
82  *
83  * \param inputs_num Number of inputs / outputs of the sorting network.
84  * \return A pointer to the newly allocated sorting network or \c NULL if an
85  *   invalid number of inputs was given or allocation failed.
86  */
87 sn_network_t *sn_network_create_odd_even_mergesort (int inputs_num);
88
89 /**
90  * Creates a new sorting network using the Pairwise sorting algorithm published
91  * by Ian Parberry.
92  * \param inputs_num Number of inputs / outputs of the sorting network.
93  * \return A pointer to the newly allocated sorting network or \c NULL if an
94  *   invalid number of inputs was given or allocation failed.
95  */
96 sn_network_t *sn_network_create_pairwise (int inputs_num);
97
98 /**
99  * Append another network to a given network.
100  *
101  * \param n The comparator network to which the other network is added. This
102  *   network is modified.
103  * \param other The network to be added to the first network. This network is
104  *   consumed by this function and the memory pointed to is freed. You cannot
105  *   use that network after this call, so use sn_network_clone() if required.
106  * \return Zero on success, non-zero on failure.
107  */
108 int sn_network_network_add (sn_network_t *n, sn_network_t *other);
109
110 /**
111  * Append a new stage to a comparator network.
112  *
113  * \param n The comparator network to which to add the stage.
114  * \param s A pointer to a stage. The memory pointed to by this parameter is
115  *   not copied and freed by sn_network_destroy(). It is the caller's
116  *   responsibility to call sn_stage_clone() if appropriate.
117  * \return Zero on success, non-zero on failure.
118  */
119 int sn_network_stage_add (sn_network_t *n, sn_stage_t *s);
120
121 /**
122  * Remove a stage from a comparator network.
123  *
124  * \param n A pointer to the comparator network to modify.
125  * \param s_num The depth of the comparator network to remove, with zero being
126  *   meaning the stage closest to the inputs.
127  * \return Zero on success, non-zero on failure.
128  */
129 int sn_network_stage_remove (sn_network_t *n, int s_num);
130
131 /**
132  * Adds a comparator to a comparator network. The code tries to add the
133  * comparator to the last stage, i.e. the stage closest to the outputs. If this
134  * is not possible, because one line is used by another comparator, a new stage
135  * is created and appended to the sorting network.
136  *
137  * \param n Pointer to the comparator netork.
138  * \param c Pointer to a comparator to add. The given comparator is copied. It
139  *   is the caller's responsibility to free c.
140  * \return Zero on success, non-zero on failure.
141  */
142 int sn_network_comparator_add (sn_network_t *n, const sn_comparator_t *c);
143
144 /**
145  * Returns the number of comparators contained in the comparator network. This
146  * will traverse all comparators in all stages, resulting in a running time of
147  * \f$ \mathcal{O}(n) \f$.
148  *
149  * \param n Comparator network to work with.
150  * \return The number of comparators contained in the network or less than zero
151  *  on error (\c n is \c NULL).
152  */
153 int sn_network_get_comparator_num (const sn_network_t *n);
154
155 /**
156  * Applies a comparator network to an array of integers. This is implemented by
157  * calling sn_stage_sort() with every stage of the network.
158  *
159  * \param n Pointer to the comparator netork.
160  * \param[in,out] values Pointer to integer values to sort. The number of
161  *   integer values pointed to must be at least the number of inputs of the
162  *   comparator network. Otherwise, segmentation faults or memory corruption
163  *   will occur.
164  * \return Zero on success, non-zero on failure.
165  * \see sn_stage_sort
166  */
167 int sn_network_sort (sn_network_t *n, int *values);
168
169 /**
170  * Checks whether a given comparator network is a sorting network by testing
171  * all \f$ 2^n \f$ 0-1-patterns. Since this function has exponential running
172  * time, using it with comparator networks with many inputs is not advisable.
173  *
174  * \param n The comparator network to test.
175  * \return Zero if the comparator network is a sort network, one if the
176  *   comparator network is \em not a sort network, or something else on error.
177  */
178 int sn_network_brute_force_check (sn_network_t *n);
179
180 /**
181  * Prints the comparator network to \c STDOUT using a human readable
182  * representation.
183  *
184  * \param n The comparator network to display.
185  * \return Zero on success, non-zero on failure.
186  */
187 int sn_network_show (sn_network_t *n);
188
189 /**
190  * Inverts a comparator network by switching the direction of all comparators.
191  *
192  * \param n The network to invert.
193  * \return Zero on success, non-zero on failure.
194  */
195 int sn_network_invert (sn_network_t *n);
196
197 /**
198  * Shifts a comparator network (permutes the inputs). Each input is shifted
199  * \f$ s \f$ positions, higher inputs are "wrapped around".
200  *
201  * \param n The comparator network to shift.
202  * \param s The number of positions to shift.
203  * \return Zero on success, non-zero on failure.
204  */
205 int sn_network_shift (sn_network_t *n, int s);
206
207 /**
208  * Compresses a comparator network by moving all comparators to the earliest
209  * possible stage and removing all remaining empty stages.
210  *
211  * \param n The network to compress.
212  * \return Zero on success, non-zero on failure.
213  */
214 int sn_network_compress (sn_network_t *n);
215
216 /**
217  * Converts a non-standard comparator network to a standard comparator network,
218  * i.e. a network in which all comparators point in the same direction.
219  *
220  * \param n The network to normalize.
221  * \return Zero on success, non-zero on failure.
222  */
223 int sn_network_normalize (sn_network_t *n);
224
225 /**
226  * Removes an input and all comparators touching that input from the comparator
227  * network.
228  *
229  * \param n The network to modify.
230  * \param input The zero-based index of the input to remove.
231  * \return Zero on success, non-zero on failure.
232  */
233 int sn_network_remove_input (sn_network_t *n, int input);
234
235 /**
236  * Removes an inputs from a comparator network by assuming positive or negative
237  * infinity to be supplied to a given input. The value will take a
238  * deterministic way through the comparator network. After removing the path
239  * and all comparators it touched from the comparator network, a new comparator
240  * network with \f$ n-1 \f$ inputs remains. The remaining outputs behave
241  * identical to the original network with respect to one another.
242  *
243  * \param n The comparator network to modify.
244  * \param input The input to remove.
245  * \param dir The direction in which to cut, i.e. whether to assume positive or
246  *   negative infinity.
247  * \return Zero on success, non-zero on failure.
248  */
249 int sn_network_cut_at (sn_network_t *n, int input, enum sn_network_cut_dir_e dir);
250
251 /* FIXME: Documentation */
252 int sn_network_cut (sn_network_t *n, int *mask);
253
254 /**
255  * An alias for sn_network_combine_odd_even_merge().
256  */
257 sn_network_t *sn_network_combine (sn_network_t *n0, sn_network_t *n1);
258
259 /**
260  * Combines two comparator networks using a bitonic merger. The number of
261  * inputs of both comparator networks must be identical and a power of two.
262  *
263  * \param n0 First network.
264  * \param n1 Second network.
265  * \return Newly allocated network with twice the number of inputs or \c NULL
266  *   on error.
267  */
268 sn_network_t *sn_network_combine_bitonic_merge (sn_network_t *n0, sn_network_t *n1);
269
270 /**
271  * Combines two comparator networks using the odd-even-merger.
272  *
273  * \param n0 First network.
274  * \param n1 Second network.
275  * \return Newly allocated network or \c NULL on error.
276  */
277 sn_network_t *sn_network_combine_odd_even_merge (sn_network_t *n0, sn_network_t *n1);
278
279 /**
280  * Reads a comparator network from a open file handle.
281  *
282  * \param fh The FILE pointer to read from.
283  * \return A newly allocated comparator network or \c NULL on error.
284  * \see sn_network_read_file
285  */
286 sn_network_t *sn_network_read (FILE *fh);
287
288 /**
289  * Reads a comparator network from a file.
290  *
291  * \param file File name to read the network from.
292  * \return A newly allocated comparator network or \c NULL on error.
293  * \see sn_network_read
294  */
295 sn_network_t *sn_network_read_file (const char *file);
296
297 /**
298  * Writes a comparator network to a file handle.
299  *
300  * \param n The comparator network to write.
301  * \param fh The file handle to write to. Must be opened in write mode.
302  * \return Zero on success, non-zero on failure.
303  * \see sn_network_write_file
304  */
305 int sn_network_write (sn_network_t *n, FILE *fh);
306
307 /**
308  * Writes a comparator network to a file.
309  *
310  * \param n The comparator network to write.
311  * \param fh The file name to write the network to.
312  * \return Zero on success, non-zero on failure.
313  * \see sn_network_write
314  */
315 int sn_network_write_file (sn_network_t *n, const char *file);
316
317 /**
318  * Creates a serialized form of the given comparator network. The serialized
319  * form is byte-order independent and can easily be sent over a computer
320  * network.
321  *
322  * \param n The comparator network to serialize.
323  * \param[out] ret_buffer Pointer to a pointer into which the location of the
324  *   allocated memory will be written. It is the caller's responsibility to
325  *   free this memory.
326  * \param[out] ret_buffer_size Pointer to a size_t into which the size of the
327  *   allocated memory will be written.
328  * \return Zero on success, non-zero on failure.
329  * \see sn_network_unserialize
330  */
331 int sn_network_serialize (sn_network_t *n, char **ret_buffer,
332     size_t *ret_buffer_size);
333
334 /**
335  * Creates a comparator network from its serialized form.
336  *
337  * \param buffer Pointer to a buffer containing the comparator network in
338  *   serialized form.
339  * \param buffer_size Size of the buffer (in bytes).
340  * \return Pointer to the newly allocated comparator network or \c NULL on
341  *   error.
342  * \see sn_network_serialize
343  */
344 sn_network_t *sn_network_unserialize (char *buffer, size_t buffer_size);
345 #endif /* SN_NETWORK_H */
346
347 /* vim: set shiftwidth=2 softtabstop=2 : */