From: Florian Forster Date: Thu, 13 Jan 2011 10:03:14 +0000 (+0100) Subject: src/sn_{network,stage,comparator}.[ch]: Implement sn_network_get_hashval() and friends. X-Git-Tag: v1.1.0~24 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=518eb323bcc57e22984f75134a6c6b44876d8dd2;p=sort-networks.git src/sn_{network,stage,comparator}.[ch]: Implement sn_network_get_hashval() and friends. --- diff --git a/src/sn-info.c b/src/sn-info.c index cf85f89..e2bc9c9 100644 --- a/src/sn-info.c +++ b/src/sn-info.c @@ -112,11 +112,13 @@ int main (int argc, char **argv) " Normalized: %4s\n" " Sorts: %7s\n" " Rating: %4i\n" + " Hash: 0x%08"PRIx32"\n" "\n", comparators_num, (normalized ? "yes" : "no"), ((inputs_num > 16) ? "unknown" : (sorts ? "yes" : "no")), - (stages_num_compressed * inputs_num) + comparators_num); + (stages_num_compressed * inputs_num) + comparators_num, + sn_network_get_hashval (n)); exit (EXIT_SUCCESS); } /* int main */ diff --git a/src/sn_comparator.c b/src/sn_comparator.c index e404ded..0726504 100644 --- a/src/sn_comparator.c +++ b/src/sn_comparator.c @@ -109,4 +109,14 @@ int sn_comparator_compare (const sn_comparator_t *c0, return (0); } /* int sn_comparator_compare */ +uint32_t sn_comparator_get_hashval (const sn_comparator_t *c) /* {{{ */ +{ + if (c == NULL) + return (0); + + /* 100937 and 103319 are some random prime numbers */ + return ((((uint32_t) c->min) * 100937) + + (((uint32_t) c->max) * 103319)); +} /* }}} uint32_t sn_comparator_get_hashval */ + /* vim: set shiftwidth=2 softtabstop=2 : */ diff --git a/src/sn_comparator.h b/src/sn_comparator.h index ba82dcb..db9bc07 100644 --- a/src/sn_comparator.h +++ b/src/sn_comparator.h @@ -28,6 +28,9 @@ #ifndef SN_COMPARATOR_H #define SN_COMPARATOR_H 1 +#include +#include + /** * Struct representing a comparator. Don't access the members of this struct * directly, use the macros below instead. @@ -114,6 +117,8 @@ void sn_comparator_swap (sn_comparator_t *c, int con0, int con1); int sn_comparator_compare (const sn_comparator_t *c0, const sn_comparator_t *c1); +uint32_t sn_comparator_get_hashval (const sn_comparator_t *c); + #endif /* SN_COMPARATOR_H */ /* vim: set shiftwidth=2 softtabstop=2 : */ diff --git a/src/sn_network.c b/src/sn_network.c index 0fdaaa3..2071510 100644 --- a/src/sn_network.c +++ b/src/sn_network.c @@ -1171,4 +1171,20 @@ sn_network_t *sn_network_unserialize (char *buffer, /* {{{ */ return (n); } /* }}} sn_network_t *sn_network_unserialize */ +uint32_t sn_network_get_hashval (const sn_network_t *n) /* {{{ */ +{ + uint32_t hash; + int i; + + if (n == NULL) + return (0); + + hash = (uint32_t) n->inputs_num; + + for (i = 0; i < n->stages_num; i++) + hash = (hash * 104207) + sn_stage_get_hashval (n->stages[i]); + + return (hash); +} /* }}} uint32_t sn_network_get_hashval */ + /* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/src/sn_network.h b/src/sn_network.h index eb5b2e9..4626d9d 100644 --- a/src/sn_network.h +++ b/src/sn_network.h @@ -31,6 +31,8 @@ #define SN_NETWORK_H 1 #include +#include +#include #include "sn_comparator.h" #include "sn_stage.h" @@ -351,6 +353,9 @@ int sn_network_serialize (sn_network_t *n, char **ret_buffer, * \see sn_network_serialize */ sn_network_t *sn_network_unserialize (char *buffer, size_t buffer_size); + +uint32_t sn_network_get_hashval (const sn_network_t *n); + #endif /* SN_NETWORK_H */ /* vim: set shiftwidth=2 softtabstop=2 : */ diff --git a/src/sn_stage.c b/src/sn_stage.c index d0d54d7..1b564cc 100644 --- a/src/sn_stage.c +++ b/src/sn_stage.c @@ -608,4 +608,20 @@ sn_stage_t *sn_stage_unserialize (char **ret_buffer, size_t *ret_buffer_size) return (s); } /* sn_stage_t *sn_stage_unserialize */ -/* vim: set shiftwidth=2 softtabstop=2 expandtab : */ +uint32_t sn_stage_get_hashval (const sn_stage_t *s) /* {{{ */ +{ + uint32_t hash; + int i; + + if (s == NULL) + return (0); + + hash = (uint32_t) s->depth; + + for (i = 0; i < s->comparators_num; i++) + hash = (hash * 99991) + sn_comparator_get_hashval (s->comparators + i); + + return (hash); +} /* }}} uint32_t sn_stage_get_hashval */ + +/* vim: set shiftwidth=2 softtabstop=2 expandtab fdm=marker : */ diff --git a/src/sn_stage.h b/src/sn_stage.h index 0e3f3bd..ee51b9f 100644 --- a/src/sn_stage.h +++ b/src/sn_stage.h @@ -29,6 +29,8 @@ #define SN_STAGE_H 1 #include +#include +#include #include "sn_comparator.h" @@ -250,6 +252,8 @@ int sn_stage_serialize (sn_stage_t *s, */ sn_stage_t *sn_stage_unserialize (char **buffer, size_t *buffer_size); +uint32_t sn_stage_get_hashval (const sn_stage_t *s); + #endif /* SN_STAGE_H */ /* vim: set shiftwidth=2 softtabstop=2 : */