libbubulle/edges.c

176 lines
3.9 KiB
C
Raw Normal View History

2023-03-22 06:31:50 +11:00
/*
* edges.c
2023-03-28 23:50:40 +11:00
* a part of libbubulle from tTh
2023-03-22 06:31:50 +11:00
*/
#include <stdio.h>
2023-03-26 21:44:34 +11:00
#include <stdlib.h>
#include <string.h>
2023-03-22 06:31:50 +11:00
2023-03-25 01:32:13 +11:00
#include "bubulles.h"
2023-03-22 06:31:50 +11:00
#include "edges.h"
2023-03-26 21:44:34 +11:00
/* --------------------------------------------------------------------- */
EdgeList * alloc_edgelist(char *name, int sz, int flags)
{
EdgeList *elptr;
AnEdge *array;
2023-03-30 14:05:32 +11:00
#if DEBUG_LEVEL
2023-03-26 21:44:34 +11:00
fprintf(stderr, ">>> %s ( '%s' %d 0x%X )\n", __func__, name, sz, flags);
2023-03-30 14:05:32 +11:00
#endif
2023-03-26 21:44:34 +11:00
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)
{
2023-03-30 14:05:32 +11:00
#if DEBUG_LEVEL
2023-03-26 21:44:34 +11:00
fprintf(stderr, ">>> %s ( %p 0x%X )\n", __func__, list, k);
2023-03-30 14:05:32 +11:00
#endif
2023-03-26 21:44:34 +11:00
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);
2023-03-27 09:03:54 +11:00
return 0;
2023-03-26 21:44:34 +11:00
}
/* --------------------------------------------------------------------- */
2023-03-27 09:03:54 +11:00
/*
* we have two functions for adding an edge to a list
* the first one add unconditionnaly the edge to the
* (non full) list...
*/
2023-03-26 21:44:34 +11:00
int push_an_edge(EdgeList *list, int p0, int p1)
{
2023-03-27 09:03:54 +11:00
#if DEBUG_LEVEL
2023-03-26 21:44:34 +11:00
fprintf(stderr, ">>> %s ( %p %d %d )\n", __func__, list, p0, p1);
2023-03-27 09:03:54 +11:00
#endif
2023-03-26 21:44:34 +11:00
if (list->fidx >= list->size) {
2023-03-27 09:03:54 +11:00
fprintf(stderr, "%s: w're doomed and overflowed\n", __func__);
2023-03-26 21:44:34 +11:00
#if MUST_ABORT
abort();
#endif
return -1;
}
list->edges[list->fidx].A = p0;
list->edges[list->fidx].B = p1;
list->fidx ++;
2023-03-27 09:03:54 +11:00
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;
}
2023-03-26 21:44:34 +11:00
2023-03-27 09:03:54 +11:00
if ( ! is_edge_in_list(list, p0, p1) ) {
list->edges[list->fidx].A = p0;
list->edges[list->fidx].B = p1;
list->fidx ++;
}
2023-03-26 21:44:34 +11:00
return 0;
}
2023-03-22 06:31:50 +11:00
/* --------------------------------------------------------------------- */
2023-03-27 09:03:54 +11:00
/*
* 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 */
}
/* --------------------------------------------------------------------- */
2023-03-25 01:32:13 +11:00
int print_edgelist_desc(EdgeList *list, int k)
{
2023-03-22 06:31:50 +11:00
2023-03-30 14:05:32 +11:00
fprintf(stderr, "------- edgelist '%s' at %p\n", list->name, list);
2023-03-22 06:31:50 +11:00
2023-03-25 01:32:13 +11:00
if (k) {
fprintf(stderr, "%s: k must be 0, was %d\n", __func__, k);
return k;
}
2023-03-30 14:05:32 +11:00
fprintf(stderr, "\tarray @ %p\n", list->edges);
fprintf(stderr, "\tsize %8d\n", list->size);
fprintf(stderr, "\tnext free %8d\n", list->fidx);
fprintf(stderr, "\tmagic 0x%08lX\n", list->magic);
2023-03-26 21:44:34 +11:00
2023-03-25 01:32:13 +11:00
return 0;
}
/* --------------------------------------------------------------------- */
2023-03-30 14:05:32 +11:00
int print_the_edges(FILE *fp, EdgeList *list, int k)
2023-03-26 21:44:34 +11:00
{
int foo;
2023-03-30 14:05:32 +11:00
#if DEBUG_LEVEL
2023-03-28 23:50:40 +11:00
fprintf(stderr, ">>> %s ( %p %d )\n", __func__, list, k);
2023-03-30 14:05:32 +11:00
#endif
2023-03-28 23:50:40 +11:00
2023-03-26 21:44:34 +11:00
if (k) {
fprintf(stderr, "In %s, k must be 0, was %d\n", __func__, k);
return k;
}
2023-03-28 23:50:40 +11:00
fprintf(stderr, " list.fidx = %d\n", list->fidx);
2023-03-26 21:44:34 +11:00
for (foo=0; foo<list->fidx; foo++) {
2023-03-30 14:05:32 +11:00
fprintf(fp, "%6d\t\t%5d %5d\n", foo,
list->edges[foo].A, list->edges[foo].B);
2023-03-26 21:44:34 +11:00
}
return -1;
}
/* --------------------------------------------------------------------- */