Compare commits

...

2 Commits

Author SHA1 Message Date
tTh
6f254b6ff4 edgelists : second milestone reached 2023-03-27 00:03:54 +02:00
tTh
83085d1435 first work on edgelists 2023-03-26 12:44:34 +02:00
5 changed files with 242 additions and 7 deletions

View File

@ -10,7 +10,7 @@
CC = gcc 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 libbubulles.a: bubulles.o edges.o
ar r $@ $? ar r $@ $?

View File

@ -4,7 +4,7 @@
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
#define LIBBB_VERSION 60 #define LIBBB_VERSION 61
#define SZ_BUBULLE_TEXT 81 /* arbitrary value */ #define SZ_BUBULLE_TEXT 81 /* arbitrary value */

144
edges.c
View File

@ -3,21 +3,161 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bubulles.h" #include "bubulles.h"
#include "edges.h" #include "edges.h"
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
int print_edgelist_desc(EdgeList *list, int k) EdgeList * alloc_edgelist(char *name, int sz, int flags)
{
EdgeList *elptr;
AnEdge *array;
fprintf(stderr, ">>> %s ( '%s' %d 0x%X )\n", __func__, name, sz, flags);
if (NULL==(elptr = calloc(1, sizeof(EdgeList)))) {
fprintf(stderr, "no mem available in %s\n", __func__);
return NULL;
}
memset(elptr, 0, sizeof(EdgeList));
if (NULL==(array = calloc(sz, sizeof(Bubulle)))) {
fprintf(stderr, "no mem available in %s\n", __func__);
free(elptr);
return NULL;
}
elptr->edges = array;
if ( (NULL != name) && (strlen(name) < SZ_BUBULLE_TEXT) )
strcpy(elptr->name, name);
else
strcpy(elptr->name, "noname");
elptr->magic = 0x55555555;
elptr->size = sz;
elptr->flags = flags;
return elptr;
}
/* --------------------------------------------------------------------- */
int free_edgelist(EdgeList *list, int k)
{ {
printf("edgelist addr: %p\n", list); fprintf(stderr, ">>> %s ( %p 0x%X )\n", __func__, list, k);
if (k) { if (k) {
fprintf(stderr, "%s: k must be 0, was %d\n", __func__, k); fprintf(stderr, "%s: k must be 0, was %d\n", __func__, k);
return k; return k;
} }
free(list->edges);
memset(list, 0, sizeof(EdgeList));
free(list);
return 0; 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 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;
}
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)
{
fprintf(stderr, "--- edgelist '%s' at %p\n", list->name, list);
if (k) {
fprintf(stderr, "%s: k must be 0, was %d\n", __func__, k);
return k;
}
fprintf(stderr, " array @ %p\n", list->edges);
fprintf(stderr, " size %8d\n", list->size);
fprintf(stderr, " next free %8d\n", list->fidx);
// fprintf(stderr, " magic 0x%08X\n", list->magic);
return 0;
}
/* --------------------------------------------------------------------- */
int print_the_edges(EdgeList *list, int k)
{
int foo;
if (k) {
fprintf(stderr, "In %s, k must be 0, was %d\n", __func__, k);
return k;
}
for (foo=0; foo<list->fidx; foo++) {
printf("%d\t\t%5d %5d\n", foo, list->edges[foo].A, list->edges[foo].B);
}
return -1;
}
/* --------------------------------------------------------------------- */

13
edges.h
View File

@ -9,6 +9,7 @@ typedef struct {
} AnEdge; } AnEdge;
typedef struct { typedef struct {
unsigned long magic;
char name[SZ_BUBULLE_TEXT+1]; char name[SZ_BUBULLE_TEXT+1];
int size; /* max number of edges */ int size; /* max number of edges */
int fidx; /* next free slot */ int fidx; /* next free slot */
@ -17,3 +18,15 @@ typedef struct {
} EdgeList; } EdgeList;
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
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);
/* --------------------------------------------------------------------- */

88
tbb.c
View File

@ -8,8 +8,84 @@
#include <stdlib.h> #include <stdlib.h>
#include "bubulles.h" #include "bubulles.h"
#include "edges.h"
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
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);
print_edgelist_desc(list, 0);
foo = push_an_edge(list, 13, 37);
fprintf(stderr, " push edge -> %d\n", foo);
foo = push_an_edge(list, 24, 36);
foo = print_the_edges(list, 0);
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);
foo = free_edgelist(list, 0);
fprintf(stderr, " free list -> %d\n", foo);
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) void test_alloc_free(int nbelm)
{ {
BBList *bublist; BBList *bublist;
@ -109,16 +185,22 @@ int main(int argc, char *argv[])
{ {
int foo; int foo;
printf("*** Bubulles Testing -- %s %s\n\n", __DATE__, __TIME__); fprintf(stderr, "*** Bubulles Testing %s %s\n\n", __DATE__, __TIME__);
bubulles_version(1); bubulles_version(0);
#if (0)
test_alloc_free(5); test_alloc_free(5);
test_push_pop(20000); test_push_pop(20000);
test_cleanfill_my_bublist(999); test_cleanfill_my_bublist(999);
foo = test_peek_poke(5000); foo = test_peek_poke(5000);
fprintf(stderr, "test peek/poke -> %d\n", foo); fprintf(stderr, "test peek/poke -> %d\n", foo);
#endif
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; return 0;
} }