This commit is contained in:
2025-09-04 23:14:40 +02:00
parent 2b05f07c1f
commit 337f069d5d
12 changed files with 907 additions and 213 deletions

63
slowgraph/sgraph-adj.c Normal file
View File

@@ -0,0 +1,63 @@
#define SLOWGRAPH_IMPL
#include "../slowgraph.h"
static void write_csv_field(char const *s) {
char const *y;
int quote = 0;
for (y = s; *y; y++) {
if (*y == '\n' || *y == '"') {
quote = 1;
break;
}
}
if (quote)
putchar('"');
for (; *s; s++) {
if (*s == '"')
putchar('"');
putchar(*s);
}
if (quote)
putchar('"');
}
static void dump_adj_matrix_row(SlowGraph *g, SlowGraphNode *row) {
SlowGraphNode *col;
write_csv_field(row->name);
printf(",");
for (col = g->first; col; col = col->next) {
if (SlowGraphNode_findConnection(row, col)) {
printf("1,");
} else {
printf("0,");
}
}
printf("\n");
}
static void dump_adj_matrix(SlowGraph *g) {
SlowGraphNode *n;
printf(",");
for (n = g->first; n; n = n->next) {
if (n != g->first)
printf(",");
write_csv_field(n->name);
}
printf("\n");
for (n = g->first; n; n = n->next) {
dump_adj_matrix_row(g, n);
}
}
int main(int argc, char **argv) {
SlowGraph graph = {0};
if (SlowGraph_readDGTXT(&graph, stdin))
return 1;
dump_adj_matrix(&graph);
return 0;
}