libbubulle/edges.c
2023-03-26 12:44:34 +02:00

116 lines
2.6 KiB
C

/*
* edges.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bubulles.h"
#include "edges.h"
/* --------------------------------------------------------------------- */
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)
{
fprintf(stderr, ">>> %s ( %p 0x%X )\n", __func__, list, k);
if (k) {
fprintf(stderr, "%s: k must be 0, was %d\n", __func__, k);
return k;
}
free(list->edges);
memset(list, 0, sizeof(EdgeList));
free(list);
return -1;
}
/* --------------------------------------------------------------------- */
int push_an_edge(EdgeList *list, int p0, int p1)
{
fprintf(stderr, ">>> %s ( %p %d %d )\n", __func__, list, p0, p1);
if (list->fidx >= list->size) {
fprintf(stderr, "%s: w're doomed, overflow\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;
}
/* --------------------------------------------------------------------- */
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;
}
/* --------------------------------------------------------------------- */