From: Florian Forster Date: Mon, 24 Jan 2011 07:27:53 +0000 (+0100) Subject: src/sn_{network,stage}.[ch]: Implement sn_{network,stage}_compare. X-Git-Tag: v1.1.0~14 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=785cb745f68b61aa83820a4f85a8c91a0e015228;p=sort-networks.git src/sn_{network,stage}.[ch]: Implement sn_{network,stage}_compare. --- diff --git a/src/sn_network.c b/src/sn_network.c index 9dd2329..29c2c03 100644 --- a/src/sn_network.c +++ b/src/sn_network.c @@ -1187,6 +1187,38 @@ sn_network_t *sn_network_unserialize (char *buffer, /* {{{ */ return (n); } /* }}} sn_network_t *sn_network_unserialize */ +int sn_network_compare (const sn_network_t *n0, const sn_network_t *n1) /* {{{ */ +{ + int status; + int i; + + if (n0 == n1) + return (0); + else if (n0 == NULL) + return (-1); + else if (n1 == NULL) + return (1); + + if (n0->inputs_num < n1->inputs_num) + return (-1); + else if (n0->inputs_num > n1->inputs_num) + return (1); + + if (n0->stages_num < n1->stages_num) + return (-1); + else if (n0->stages_num > n1->stages_num) + return (1); + + for (i = 0; i < n0->stages_num; i++) + { + status = sn_stage_compare (n0->stages[i], n1->stages[i]); + if (status != 0) + return (status); + } + + return (0); +} /* }}} int sn_network_compare */ + uint64_t sn_network_get_hashval (const sn_network_t *n) /* {{{ */ { uint64_t hash; diff --git a/src/sn_network.h b/src/sn_network.h index 2e92898..f94a237 100644 --- a/src/sn_network.h +++ b/src/sn_network.h @@ -356,6 +356,20 @@ int sn_network_serialize (sn_network_t *n, char **ret_buffer, */ sn_network_t *sn_network_unserialize (char *buffer, size_t buffer_size); +/** + * Compares two networks and returns zero if they are equal. If they are not + * equal, a number greater than zero or less than zero is returned in a + * consistent matter, so this function can be used to sort networks and detect + * duplicates. It is strongly recommended that you call sn_network_unify() + * before comparing two networks, because they internal structure does matter + * for this function. + * + * \return Zero if the two networks are equal, non-zero otherwise. Return + * values are consistent so this function can be used to sort networks. + * \see sn_network_unify + */ +int sn_network_compare (const sn_network_t *n0, const sn_network_t *n1); + uint64_t sn_network_get_hashval (const sn_network_t *n); #endif /* SN_NETWORK_H */ diff --git a/src/sn_stage.c b/src/sn_stage.c index 1a7b2f9..1584469 100644 --- a/src/sn_stage.c +++ b/src/sn_stage.c @@ -626,6 +626,33 @@ sn_stage_t *sn_stage_unserialize (char **ret_buffer, size_t *ret_buffer_size) return (s); } /* sn_stage_t *sn_stage_unserialize */ +int sn_stage_compare (const sn_stage_t *s0, const sn_stage_t *s1) /* {{{ */ +{ + int status; + int i; + + if (s0 == s1) + return (0); + else if (s0 == NULL) + return (-1); + else if (s1 == NULL) + return (1); + + if (s0->comparators_num < s1->comparators_num) + return (-1); + else if (s0->comparators_num > s1->comparators_num) + return (1); + + for (i = 0; i < s0->comparators_num; i++) + { + status = sn_comparator_compare (s0->comparators + i, s1->comparators + i); + if (status != 0) + return (status); + } + + return (0); +} /* }}} int sn_stage_compare */ + uint64_t sn_stage_get_hashval (const sn_stage_t *s) /* {{{ */ { uint64_t hash; diff --git a/src/sn_stage.h b/src/sn_stage.h index cdba998..2f5f729 100644 --- a/src/sn_stage.h +++ b/src/sn_stage.h @@ -254,6 +254,8 @@ int sn_stage_serialize (sn_stage_t *s, */ 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 */