From: Florian Forster Date: Mon, 20 Dec 2010 22:11:09 +0000 (+0100) Subject: src/sn_network.[ch]: Implement sn_network_network_add(). X-Git-Tag: v1.0.0~9 X-Git-Url: https://git.octo.it/?p=sort-networks.git;a=commitdiff_plain;h=0c45212d2c6f5533aa4a3006cbb8347f2b238296 src/sn_network.[ch]: Implement sn_network_network_add(). --- diff --git a/src/sn_network.c b/src/sn_network.c index 6908cf8..15269c9 100644 --- a/src/sn_network.c +++ b/src/sn_network.c @@ -207,6 +207,33 @@ sn_network_t *sn_network_create_pairwise (int inputs_num) /* {{{ */ return (n); } /* }}} sn_network_t *sn_network_create_pairwise */ +int sn_network_network_add (sn_network_t *n, sn_network_t *other) /* {{{ */ +{ + int stages_num; + sn_stage_t **tmp; + + if ((n == NULL) || (other == NULL)) + return (EINVAL); + + stages_num = n->stages_num + other->stages_num; + if (stages_num <= n->stages_num) + return (EINVAL); + + tmp = realloc (n->stages, sizeof (*n->stages) * stages_num); + if (tmp == NULL) + return (ENOMEM); + n->stages = tmp; + + memcpy (n->stages + n->stages_num, other->stages, + sizeof (*other->stages) * other->stages_num); + n->stages_num = stages_num; + + free (other->stages); + free (other); + + return (0); +} /* }}} int sn_network_network_add */ + int sn_network_stage_add (sn_network_t *n, sn_stage_t *s) /* {{{ */ { sn_stage_t **temp; diff --git a/src/sn_network.h b/src/sn_network.h index 8e6d492..716c2a3 100644 --- a/src/sn_network.h +++ b/src/sn_network.h @@ -95,6 +95,18 @@ sn_network_t *sn_network_create_odd_even_mergesort (int inputs_num); sn_network_t *sn_network_create_pairwise (int inputs_num); /** + * Append another network to a given network. + * + * \param n The comparator network to which the other network is added. This + * network is modified. + * \param other The network to be added to the first network. This network is + * consumed by this function and the memory pointed to is freed. You cannot + * use that network after this call, so use sn_network_clone() if required. + * \return Zero on success, non-zero on failure. + */ +int sn_network_network_add (sn_network_t *n, sn_network_t *other); + +/** * Append a new stage to a comparator network. * * \param n The comparator network to which to add the stage.