3 * \brief The sn_network_t class and associated methods.
5 * This is more details about what this file does.
8 * libsortnetwork - src/sn_network.h
9 * Copyright (C) 2008-2010 Florian octo Forster
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.
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
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
26 * Florian octo Forster <ff at octo.it>
31 #define SN_NETWORK_H 1
35 #include "sn_comparator.h"
39 * The global struct representing a comparator or sort network.
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. */
47 typedef struct sn_network_s sn_network_t;
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
54 * Creates an empty comparator network and returns a pointer to it. The
55 * comparator network must be freed using sn_network_destroy().
57 * \param inputs_num Number of inputs the comparator network has.
58 * \return Pointer to the comparator network or \c NULL on error.
60 sn_network_t *sn_network_create (int inputs_num);
63 * Clones an existing comparator network.
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().
69 sn_network_t *sn_network_clone (const sn_network_t *n);
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
76 * \param n The comparator network to destroy. May be \c NULL.
78 void sn_network_destroy (sn_network_t *n);
81 * Creates a new sort network using Batcher's Odd-Even-Mergesort algorithm.
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.
87 sn_network_t *sn_network_create_odd_even_mergesort (int inputs_num);
90 * Creates a new sort network using Batcher's Bitonic-Mergesort algorithm.
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.
96 sn_network_t *sn_network_create_bitonic_mergesort (int inputs_num);
99 * Creates a new sorting network using the Pairwise sorting algorithm published
101 * \param inputs_num Number of inputs / outputs of the sorting network.
102 * \return A pointer to the newly allocated sorting network or \c NULL if an
103 * invalid number of inputs was given or allocation failed.
105 sn_network_t *sn_network_create_pairwise (int inputs_num);
108 * Append another network to a given network.
110 * \param n The comparator network to which the other network is added. This
111 * network is modified.
112 * \param other The network to be added to the first network. This network is
113 * consumed by this function and the memory pointed to is freed. You cannot
114 * use that network after this call, so use sn_network_clone() if required.
115 * \return Zero on success, non-zero on failure.
117 int sn_network_network_add (sn_network_t *n, sn_network_t *other);
120 * Append a new stage to a comparator network.
122 * \param n The comparator network to which to add the stage.
123 * \param s A pointer to a stage. The memory pointed to by this parameter is
124 * not copied and freed by sn_network_destroy(). It is the caller's
125 * responsibility to call sn_stage_clone() if appropriate.
126 * \return Zero on success, non-zero on failure.
128 int sn_network_stage_add (sn_network_t *n, sn_stage_t *s);
131 * Remove a stage from a comparator network.
133 * \param n A pointer to the comparator network to modify.
134 * \param s_num The depth of the comparator network to remove, with zero being
135 * meaning the stage closest to the inputs.
136 * \return Zero on success, non-zero on failure.
138 int sn_network_stage_remove (sn_network_t *n, int s_num);
141 * Adds a comparator to a comparator network. The code tries to add the
142 * comparator to the last stage, i.e. the stage closest to the outputs. If this
143 * is not possible, because one line is used by another comparator, a new stage
144 * is created and appended to the sorting network.
146 * \param n Pointer to the comparator netork.
147 * \param c Pointer to a comparator to add. The given comparator is copied. It
148 * is the caller's responsibility to free c.
149 * \return Zero on success, non-zero on failure.
151 int sn_network_comparator_add (sn_network_t *n, const sn_comparator_t *c);
154 * Returns the number of comparators contained in the comparator network. This
155 * will traverse all comparators in all stages, resulting in a running time of
156 * \f$ \mathcal{O}(n) \f$.
158 * \param n Comparator network to work with.
159 * \return The number of comparators contained in the network or less than zero
160 * on error (\c n is \c NULL).
162 int sn_network_get_comparator_num (const sn_network_t *n);
165 * Applies a comparator network to an array of integers. This is implemented by
166 * calling sn_stage_sort() with every stage of the network.
168 * \param n Pointer to the comparator netork.
169 * \param[in,out] values Pointer to integer values to sort. The number of
170 * integer values pointed to must be at least the number of inputs of the
171 * comparator network. Otherwise, segmentation faults or memory corruption
173 * \return Zero on success, non-zero on failure.
176 int sn_network_sort (sn_network_t *n, int *values);
179 * Checks whether a given comparator network is a sorting network by testing
180 * all \f$ 2^n \f$ 0-1-patterns. Since this function has exponential running
181 * time, using it with comparator networks with many inputs is not advisable.
183 * \param n The comparator network to test.
184 * \return Zero if the comparator network is a sort network, one if the
185 * comparator network is \em not a sort network, or something else on error.
187 int sn_network_brute_force_check (sn_network_t *n);
190 * Prints the comparator network to \c STDOUT using a human readable
193 * \param n The comparator network to display.
194 * \return Zero on success, non-zero on failure.
196 int sn_network_show (sn_network_t *n);
199 * Inverts a comparator network by switching the direction of all comparators.
201 * \param n The network to invert.
202 * \return Zero on success, non-zero on failure.
204 int sn_network_invert (sn_network_t *n);
207 * Shifts a comparator network (permutes the inputs). Each input is shifted
208 * \f$ s \f$ positions, higher inputs are "wrapped around".
210 * \param n The comparator network to shift.
211 * \param s The number of positions to shift.
212 * \return Zero on success, non-zero on failure.
214 int sn_network_shift (sn_network_t *n, int s);
217 * Compresses a comparator network by moving all comparators to the earliest
218 * possible stage and removing all remaining empty stages.
220 * \param n The network to compress.
221 * \return Zero on success, non-zero on failure.
223 int sn_network_compress (sn_network_t *n);
226 * Converts a non-standard comparator network to a standard comparator network,
227 * i.e. a network in which all comparators point in the same direction.
229 * \param n The network to normalize.
230 * \return Zero on success, non-zero on failure.
232 int sn_network_normalize (sn_network_t *n);
235 * Removes an input and all comparators touching that input from the comparator
238 * \param n The network to modify.
239 * \param input The zero-based index of the input to remove.
240 * \return Zero on success, non-zero on failure.
242 int sn_network_remove_input (sn_network_t *n, int input);
245 * Removes an inputs from a comparator network by assuming positive or negative
246 * infinity to be supplied to a given input. The value will take a
247 * deterministic way through the comparator network. After removing the path
248 * and all comparators it touched from the comparator network, a new comparator
249 * network with \f$ n-1 \f$ inputs remains. The remaining outputs behave
250 * identical to the original network with respect to one another.
252 * \param n The comparator network to modify.
253 * \param input The input to remove.
254 * \param dir The direction in which to cut, i.e. whether to assume positive or
256 * \return Zero on success, non-zero on failure.
258 int sn_network_cut_at (sn_network_t *n, int input, enum sn_network_cut_dir_e dir);
260 /* FIXME: Documentation */
261 int sn_network_cut (sn_network_t *n, int *mask);
264 * An alias for sn_network_combine_odd_even_merge().
266 sn_network_t *sn_network_combine (sn_network_t *n0, sn_network_t *n1);
269 * Combines two comparator networks using a bitonic merger. The number of
270 * inputs of both comparator networks must be identical and a power of two.
272 * \param n0 First network.
273 * \param n1 Second network.
274 * \return Newly allocated network with twice the number of inputs or \c NULL
277 sn_network_t *sn_network_combine_bitonic_merge (sn_network_t *n0, sn_network_t *n1);
280 * Combines two comparator networks using the odd-even-merger.
282 * \param n0 First network.
283 * \param n1 Second network.
284 * \return Newly allocated network or \c NULL on error.
286 sn_network_t *sn_network_combine_odd_even_merge (sn_network_t *n0, sn_network_t *n1);
289 * Reads a comparator network from a open file handle.
291 * \param fh The FILE pointer to read from.
292 * \return A newly allocated comparator network or \c NULL on error.
293 * \see sn_network_read_file
295 sn_network_t *sn_network_read (FILE *fh);
298 * Reads a comparator network from a file.
300 * \param file File name to read the network from.
301 * \return A newly allocated comparator network or \c NULL on error.
302 * \see sn_network_read
304 sn_network_t *sn_network_read_file (const char *file);
307 * Writes a comparator network to a file handle.
309 * \param n The comparator network to write.
310 * \param fh The file handle to write to. Must be opened in write mode.
311 * \return Zero on success, non-zero on failure.
312 * \see sn_network_write_file
314 int sn_network_write (sn_network_t *n, FILE *fh);
317 * Writes a comparator network to a file.
319 * \param n The comparator network to write.
320 * \param fh The file name to write the network to.
321 * \return Zero on success, non-zero on failure.
322 * \see sn_network_write
324 int sn_network_write_file (sn_network_t *n, const char *file);
327 * Creates a serialized form of the given comparator network. The serialized
328 * form is byte-order independent and can easily be sent over a computer
331 * \param n The comparator network to serialize.
332 * \param[out] ret_buffer Pointer to a pointer into which the location of the
333 * allocated memory will be written. It is the caller's responsibility to
335 * \param[out] ret_buffer_size Pointer to a size_t into which the size of the
336 * allocated memory will be written.
337 * \return Zero on success, non-zero on failure.
338 * \see sn_network_unserialize
340 int sn_network_serialize (sn_network_t *n, char **ret_buffer,
341 size_t *ret_buffer_size);
344 * Creates a comparator network from its serialized form.
346 * \param buffer Pointer to a buffer containing the comparator network in
348 * \param buffer_size Size of the buffer (in bytes).
349 * \return Pointer to the newly allocated comparator network or \c NULL on
351 * \see sn_network_serialize
353 sn_network_t *sn_network_unserialize (char *buffer, size_t buffer_size);
354 #endif /* SN_NETWORK_H */
356 /* vim: set shiftwidth=2 softtabstop=2 : */