From 6f254b6ff4245ab47df801d17e1c567b1b1bfeaa Mon Sep 17 00:00:00 2001 From: tTh Date: Mon, 27 Mar 2023 00:03:54 +0200 Subject: [PATCH] edgelists : second milestone reached --- Makefile | 2 +- edges.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++------ edges.h | 3 +++ tbb.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 113 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index c7d1c69..7f1a4b1 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ CC = gcc -OPT = -Wall -g -O3 -DDEBUG_LEVEL=0 -DMUST_ABORT=0 +OPT = -Wall -g -pg -O3 -DDEBUG_LEVEL=0 -DMUST_ABORT=0 libbubulles.a: bubulles.o edges.o ar r $@ $? diff --git a/edges.c b/edges.c index a2a9f31..dc1d63f 100644 --- a/edges.c +++ b/edges.c @@ -57,29 +57,77 @@ free(list->edges); memset(list, 0, sizeof(EdgeList)); free(list); -return -1; +return 0; } /* --------------------------------------------------------------------- */ +/* + * we have two functions for adding an edge to a list + * the first one add unconditionnaly the edge to the + * (non full) list... + */ int push_an_edge(EdgeList *list, int p0, int p1) { +#if DEBUG_LEVEL fprintf(stderr, ">>> %s ( %p %d %d )\n", __func__, list, p0, p1); +#endif if (list->fidx >= list->size) { - fprintf(stderr, "%s: w're doomed, overflow\n", __func__); + fprintf(stderr, "%s: w're doomed and overflowed\n", __func__); +#if MUST_ABORT + abort(); +#endif + return -1; + } +list->edges[list->fidx].A = p0; +list->edges[list->fidx].B = p1; +list->fidx ++; +return 0; +} +/* + * and the second only insert an edge if it was missing + * from the currently know list. + */ +int push_a_missing_edge(EdgeList *list, int p0, int p1) +{ +#if DEBUG_LEVEL +fprintf(stderr, ">>> %s ( %p %d %d )\n", __func__, list, p0, p1); +#endif + +if (list->fidx >= list->size) { + fprintf(stderr, "%s: w're doomed and overflowed\n", __func__); #if MUST_ABORT abort(); #endif return -1; } -list->edges[list->fidx].A = p0; -list->edges[list->fidx].B = p1; -list->fidx ++; - +if ( ! is_edge_in_list(list, p0, p1) ) { + list->edges[list->fidx].A = p0; + list->edges[list->fidx].B = p1; + list->fidx ++; + } return 0; } /* --------------------------------------------------------------------- */ +/* + * with this func, you can search for duplicates + */ +int is_edge_in_list(EdgeList *list, int p0, int p1) +{ +int idx; + +for (idx=0; idx < list->fidx; idx++) { + if ( (list->edges[idx].A == p0) && + (list->edges[idx].B == p1) ) return 1; + + if ( (list->edges[idx].A == p1) && + (list->edges[idx].B == p0) ) return 2; + } + +return 0; /* NOT FOUND */ +} +/* --------------------------------------------------------------------- */ int print_edgelist_desc(EdgeList *list, int k) { diff --git a/edges.h b/edges.h index 36bcc8f..54de80d 100644 --- a/edges.h +++ b/edges.h @@ -22,6 +22,9 @@ typedef struct { EdgeList * alloc_edgelist(char *name, int sz, int flags); int free_edgelist(EdgeList *list, int k); int push_an_edge(EdgeList *list, int p0, int p1); +int push_a_missing_edge(EdgeList *list, int p0, int p1); + +int is_edge_in_list(EdgeList *list, int p0, int p1); int print_edgelist_desc(EdgeList *list, int k); int print_the_edges(EdgeList *list, int k); diff --git a/tbb.c b/tbb.c index 8dea085..f6b8aec 100644 --- a/tbb.c +++ b/tbb.c @@ -11,10 +11,13 @@ #include "edges.h" /* --------------------------------------------------------------------- */ -int essai_des_edges(int k) +int essai_des_edges_A(int k) { EdgeList * list; int foo, idx; +int e0, e1; + +fprintf(stderr, "============== %s %7d ===========\n", __func__, k); list = alloc_edgelist("oups?", k, 0); fprintf(stderr, " alloc edge list -> %p\n", list); @@ -27,10 +30,13 @@ foo = push_an_edge(list, 24, 36); foo = print_the_edges(list, 0); -for (idx=0; idx<10; idx++) { - foo = push_an_edge(list, idx*7, -idx); - - +for (idx=0; idx %d\n", e0, e1, foo); + break; + } } foo = print_the_edges(list, 0); @@ -38,9 +44,48 @@ foo = print_the_edges(list, 0); foo = free_edgelist(list, 0); fprintf(stderr, " free list -> %d\n", foo); -return -1; +return 0; } /* --------------------------------------------------------------------- */ +/* + * ici on va faire des essais sur la gestion + * de la dé-duplication des aretes. + */ +int essai_des_edges_B(int k) +{ +EdgeList * list; +int foo, idx; +int e0, e1; + +fprintf(stderr, "============== %s %7d ===========\n", __func__, k); + +list = alloc_edgelist("BIG!", k, 0); +if (NULL == list) { + fprintf(stderr, "%s: epic fail\n", __func__); + abort(); + } + +print_edgelist_desc(list, 0); + +for (idx=0; idx %d\n", __func__, foo); + +return -1; +} + + +/* --------------------------------------------------------------------- */ + void test_alloc_free(int nbelm) { BBList *bublist; @@ -152,8 +197,10 @@ foo = test_peek_poke(5000); fprintf(stderr, "test peek/poke -> %d\n", foo); #endif -foo = essai_des_edges(10); -fprintf(stderr, "test des edges -> %d\n", foo); +foo = essai_des_edges_A(25); +fprintf(stderr, "test A des edges -> %d\n", foo); +foo = essai_des_edges_B(10000); +fprintf(stderr, "test B des edges -> %d\n", foo); return 0; }