src/sn_{network,stage}.[ch]: Implement sn_{network,stage}_compare.
[sort-networks.git] / src / sn_stage.h
index e2b57a6..2f5f729 100644 (file)
@@ -6,18 +6,19 @@
  * libsortnetwork - src/sn_stage.h
  * Copyright (C) 2008-2010  Florian octo Forster
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; only version 2 of the License is applicable.
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or (at
+ * your option) any later version.
  *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
  *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  * Authors:
  *   Florian octo Forster <ff at octo.it>
@@ -28,6 +29,8 @@
 #define SN_STAGE_H 1
 
 #include <stdio.h>
+#include <stdint.h>
+#include <inttypes.h>
 
 #include "sn_comparator.h"
 
@@ -133,9 +136,49 @@ int sn_stage_comparator_remove (sn_stage_t *s, int index);
  */
 int sn_stage_comparator_check_conflict (sn_stage_t *s, const sn_comparator_t *c);
 
+/**
+ * Prints the stage to \c STDOUT using a human readable representation.
+ *
+ * \param s The comparator network to display.
+ * \return Zero on success, non-zero on failure.
+ * \see sn_network_show
+ */
 int sn_stage_show (sn_stage_t *s);
+
+/**
+ * Inverts a stage by switching the direction of all comparators.
+ *
+ * \param s The stage to invert.
+ * \return Zero on success, non-zero on failure.
+ * \see sn_network_invert
+ */
 int sn_stage_invert (sn_stage_t *s);
+
+/**
+ * Shifts a stage (permutes the inputs). Each input is shifted \c sw positions,
+ * higher inputs are "wrapped around".
+ *
+ * \param s The stage to shift.
+ * \param sw The number of positions to shift.
+ * \param inputs_num The number of inputs of the comparator network. This value
+ *   is used to "wrap around" inputs.
+ * \return Zero on success, non-zero on failure.
+ */
 int sn_stage_shift (sn_stage_t *s, int sw, int inputs_num);
+
+int sn_stage_unify (sn_stage_t *s);
+
+/**
+ * Swaps two lines. This is used by the algorithm used in
+ * sn_network_normalize() to transform non-standard sort networks to standard
+ * sort networks.
+ *
+ * \param s The stage on which to operate.
+ * \param con0 Index of the first line.
+ * \param con1 Index of the second line.
+ * \return Zero on success, non-zero on failure.
+ * \see sn_network_normalize(), sn_comparator_swap()
+ */
 int sn_stage_swap (sn_stage_t *s, int con0, int con1);
 
 /**
@@ -153,15 +196,68 @@ int sn_stage_swap (sn_stage_t *s, int con0, int con1);
  */
 int sn_stage_cut_at (sn_stage_t *s, int input, enum sn_network_cut_dir_e dir);
 
+/* FIXME: Documentation */
+int sn_stage_cut (sn_stage_t *s, int *mask, sn_stage_t **prev);
+
+/**
+ * Remove an input from a stage, remove all touching comparators and adapt the
+ * input indexes of all remaining comparators.
+ *
+ * \param s The stage from which to remove the input.
+ * \param input The index of the line which to remove.
+ * \return Zero on success, non-zero on failure.
+ */
 int sn_stage_remove_input (sn_stage_t *s, int input);
 
+/**
+ * Reads a stage from a \c FILE pointer and return the newly allocated stage.
+ *
+ * \param fh The file handle.
+ * \return Pointer to a newly allocated stage or \c NULL on error.
+ * \see sn_stage_write
+ */
 sn_stage_t *sn_stage_read (FILE *fh);
+
+/**
+ * Writes a stage to a \c FILE pointer.
+ *
+ * \param s The stage to write.
+ * \param fh The file handle to write to.
+ * \return Zero on success, non-zero on failure.
+ * \see sn_stage_read
+ */
 int sn_stage_write (sn_stage_t *s, FILE *fh);
 
+/**
+ * Creates a serialized form of the given stage. The serialized form is
+ * byte-order independent and can easily be sent over a computer network.
+ *
+ * \param s The stage to serialize.
+ * \param[out] ret_buffer Pointer to a pointer into which the location of the
+ *   allocated memory will be written. It is the caller's responsibility to
+ *   free this memory.
+ * \param[out] ret_buffer_size Pointer to a size_t into which the size of the
+ *   allocated memory will be written.
+ * \return Zero on success, non-zero on failure.
+ * \see sn_stage_unserialize(), sn_network_serialize()
+ */
 int sn_stage_serialize (sn_stage_t *s,
     char **ret_buffer, size_t *ret_buffer_size);
+
+/**
+ * Creates a stage from its serialized form.
+ *
+ * \param buffer Pointer to a buffer containing the stage in serialized form.
+ * \param buffer_size Size of the buffer (in bytes).
+ * \return Pointer to the newly allocated stage or \c NULL on error.
+ * \see sn_stage_serialize(), sn_network_unserialize()
+ */
 sn_stage_t *sn_stage_unserialize (char **buffer, size_t *buffer_size);
 
+int sn_stage_compare (const sn_stage_t *s0, const sn_stage_t *s1);
+
+uint64_t sn_stage_get_hashval (const sn_stage_t *s);
+
 #endif /* SN_STAGE_H */
 
 /* vim: set shiftwidth=2 softtabstop=2 : */