edgelists : second milestone reached

This commit is contained in:
tTh 2023-03-27 00:03:54 +02:00
parent 83085d1435
commit 6f254b6ff4
4 changed files with 113 additions and 15 deletions

View File

@ -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 $@ $?

60
edges.c
View File

@ -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)
{

View File

@ -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);

63
tbb.c
View File

@ -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<k; idx++) {
e0 = idx*7; e1 = 5-idx;
foo = push_an_edge(list, e0, e1);
if (foo) {
fprintf(stderr, "push (%d, %d) -> %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 -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<k; idx++) {
e0 = rand() % 666;
e1 = rand() % 666;
foo = push_a_missing_edge(list, e0, e1);
}
print_edgelist_desc(list, 0);
foo = print_the_edges(list, 0);
foo = free_edgelist(list, 0);
fprintf(stderr, " %s: free list -> %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;
}