Address dag_man issues

This commit is contained in:
Abdelrahman Said
2026-07-04 18:55:08 +01:00
parent 25249b5e1e
commit ba7f2bedf1
+46 -44
View File
@@ -1,6 +1,7 @@
// vim:fileencoding=utf-8:foldmethod=marker // vim:fileencoding=utf-8:foldmethod=marker
#include "../src/wapp/wapp.h" #include "../src/wapp/wapp.h"
#include <_inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -46,7 +47,7 @@ wp_intern void prPoolFree(PrPool *pool, void *slot) {
* Graph * Graph
* -------------------------------------------------------------------------*/ * -------------------------------------------------------------------------*/
#define INVALID_NODE_INDEX (u32)-1 #define INVALID_NODE_INDEX (u64)-1
#define INVALID_NODE_ID ((PrNodeId){ .index = INVALID_NODE_INDEX, .generation = INVALID_NODE_INDEX }) #define INVALID_NODE_ID ((PrNodeId){ .index = INVALID_NODE_INDEX, .generation = INVALID_NODE_INDEX })
typedef enum { typedef enum {
@@ -59,8 +60,8 @@ typedef enum {
} PrNodeType; } PrNodeType;
typedef struct { typedef struct {
u32 index; u64 index;
u32 generation; u64 generation;
} PrNodeId; } PrNodeId;
typedef struct { typedef struct {
@@ -70,8 +71,8 @@ typedef struct {
f32 gain; f32 gain;
} params; } params;
PrNodeType type; PrNodeType type;
u32 generation; u64 generation;
u32 next_free; u64 next_free;
} PrNode; } PrNode;
typedef PrNode *PrNodeArray; typedef PrNode *PrNodeArray;
@@ -84,8 +85,8 @@ typedef PrNode *PrNodeArray;
* ---------------------------------------------------------------------------*/ * ---------------------------------------------------------------------------*/
typedef struct PrEdgeVertex PrEdgeVertex; typedef struct PrEdgeVertex PrEdgeVertex;
struct PrEdgeVertex { struct PrEdgeVertex {
PrEdgeVertex *next; PrEdgeVertex *next_forward;
PrEdgeVertex *prev; PrEdgeVertex *next_backward;
PrNodeId source; PrNodeId source;
PrNodeId target; PrNodeId target;
}; };
@@ -98,20 +99,20 @@ typedef struct {
u64 capacity; u64 capacity;
u64 max_count_ever; u64 max_count_ever;
u64 count; u64 count;
u32 free_head; u64 free_head;
} PrGraph; } PrGraph;
wp_intern b8 prGraphIsStaleId(const PrGraph *graph, PrNodeId id) { wp_intern b8 prGraphIsStaleId(const PrGraph *graph, PrNodeId id) {
u32 generation = graph->nodes[id.index].generation; u64 generation = graph->nodes[id.index].generation;
return id.generation != generation; return id.generation != generation;
} }
wp_intern b8 prGraphIsActiveNode(const PrGraph *graph, PrNodeId id) { wp_intern b8 prGraphIsActiveNode(const PrGraph *graph, PrNodeId id) {
u32 next_free = graph->nodes[id.index].next_free; u64 next_free = graph->nodes[id.index].next_free;
return !prGraphIsStaleId(graph, id) && next_free == INVALID_NODE_INDEX; return !prGraphIsStaleId(graph, id) && next_free == INVALID_NODE_INDEX;
} }
wp_intern PrNodeId prGraphGetNode(const PrGraph *graph, u32 index) { wp_intern PrNodeId prGraphGetNode(const PrGraph *graph, u64 index) {
return (PrNodeId){ .index = index, .generation = graph->nodes[index].generation }; return (PrNodeId){ .index = index, .generation = graph->nodes[index].generation };
} }
@@ -135,22 +136,23 @@ wp_intern void prGraphInit(PrGraph *graph, WpAllocator *allocator, u64 capacity)
wp_intern void prGraphAddEdge(PrGraph *graph, PrNodeId from, PrNodeId to) { wp_intern void prGraphAddEdge(PrGraph *graph, PrNodeId from, PrNodeId to) {
if (!prGraphIsActiveNode(graph, from) || !prGraphIsActiveNode(graph, to)) { return; } if (!prGraphIsActiveNode(graph, from) || !prGraphIsActiveNode(graph, to)) { return; }
if (from.index == to.index) { return; }
PrEdgeVertex *edge = (PrEdgeVertex *)prPoolAlloc(&graph->vertex_pool); PrEdgeVertex *edge = (PrEdgeVertex *)prPoolAlloc(&graph->vertex_pool);
PrEdgeVertex *src = &graph->vertices[from.index]; PrEdgeVertex *src = &graph->vertices[from.index];
PrEdgeVertex *dst = &graph->vertices[to.index]; PrEdgeVertex *dst = &graph->vertices[to.index];
edge->source = from; edge->source = from;
edge->target = to; edge->target = to;
edge->next = src->next; edge->next_forward = src->next_forward;
edge->prev = dst->prev; edge->next_backward = dst->next_backward;
src->next = edge; src->next_forward = edge;
dst->prev = edge; dst->next_backward = edge;
} }
wp_intern PrNodeId prGraphAddNode(PrGraph *graph, PrNodeType type) { wp_intern PrNodeId prGraphAddNode(PrGraph *graph, PrNodeType type) {
u32 idx = graph->free_head; u64 idx = graph->free_head;
if (idx == INVALID_NODE_INDEX) { return INVALID_NODE_ID; } if (idx == INVALID_NODE_INDEX) { return INVALID_NODE_ID; }
PrNode *node = &graph->nodes[idx]; PrNode *node = &graph->nodes[idx];
@@ -171,20 +173,20 @@ wp_intern void _unlinkForward(PrGraph *graph, PrNodeId from, PrEdgeVertex *edge)
if (!prGraphIsActiveNode(graph, from)) { return; } if (!prGraphIsActiveNode(graph, from)) { return; }
PrEdgeVertex *src = &graph->vertices[from.index]; PrEdgeVertex *src = &graph->vertices[from.index];
PrEdgeVertex *curr = src->next; PrEdgeVertex *curr = src->next_forward;
PrEdgeVertex *prev = NULL; PrEdgeVertex *prev = NULL;
while (curr) { while (curr) {
if (curr == edge) { if (curr == edge) {
if (prev) { if (prev) {
prev->next = curr->next; prev->next_forward = curr->next_forward;
} else { } else {
src->next = curr->next; src->next_forward = curr->next_forward;
} }
return; return;
} }
prev = curr; prev = curr;
curr = curr->next; curr = curr->next_forward;
} }
} }
@@ -192,20 +194,20 @@ wp_intern void _unlinkBackward(PrGraph *graph, PrNodeId to, PrEdgeVertex *edge)
if (!prGraphIsActiveNode(graph, to)) { return; } if (!prGraphIsActiveNode(graph, to)) { return; }
PrEdgeVertex *dst = &graph->vertices[to.index]; PrEdgeVertex *dst = &graph->vertices[to.index];
PrEdgeVertex *curr = dst->prev; PrEdgeVertex *curr = dst->next_backward;
PrEdgeVertex *prev = NULL; PrEdgeVertex *prev = NULL;
while (curr) { while (curr) {
if (curr == edge) { if (curr == edge) {
if (prev) { if (prev) {
prev->prev = curr->prev; prev->next_backward = curr->next_backward;
} else { } else {
dst->prev = curr->prev; dst->next_backward = curr->next_backward;
} }
return; return;
} }
prev = curr; prev = curr;
curr = curr->prev; curr = curr->next_backward;
} }
} }
@@ -213,7 +215,7 @@ wp_intern void prGraphRemoveNode(PrGraph *graph, PrNodeId id) {
if (!prGraphIsActiveNode(graph, id)) { return; } if (!prGraphIsActiveNode(graph, id)) { return; }
PrNode *node = &graph->nodes[id.index]; PrNode *node = &graph->nodes[id.index];
u32 next_gen = node->generation + 1; u64 next_gen = node->generation + 1;
memset(node, 0, sizeof(PrNode)); memset(node, 0, sizeof(PrNode));
node->generation = next_gen; node->generation = next_gen;
@@ -221,28 +223,28 @@ wp_intern void prGraphRemoveNode(PrGraph *graph, PrNodeId id) {
graph->free_head = id.index; graph->free_head = id.index;
PrEdgeVertex *vertex = &graph->vertices[id.index]; PrEdgeVertex *vertex = &graph->vertices[id.index];
if (!(vertex->next) && !(vertex->prev)) { if (!(vertex->next_forward) && !(vertex->next_backward)) {
goto REMOVE_NODE_UNSET_POINTERS; goto REMOVE_NODE_UNSET_POINTERS;
} }
PrEdgeVertex *curr = vertex->next; PrEdgeVertex *curr = vertex->next_forward;
while (curr) { while (curr) {
PrEdgeVertex *next = curr->next; PrEdgeVertex *next = curr->next_forward;
_unlinkBackward(graph, curr->target, curr); _unlinkBackward(graph, curr->target, curr);
prPoolFree(&graph->vertex_pool, curr); prPoolFree(&graph->vertex_pool, curr);
curr = next; curr = next;
} }
curr = vertex->prev; curr = vertex->next_backward;
while (curr) { while (curr) {
PrEdgeVertex *prev = curr->prev; PrEdgeVertex *trailing = curr->next_backward;
_unlinkForward(graph, curr->source, curr); _unlinkForward(graph, curr->source, curr);
prPoolFree(&graph->vertex_pool, curr); prPoolFree(&graph->vertex_pool, curr);
curr = prev; curr = trailing;
} }
REMOVE_NODE_UNSET_POINTERS: REMOVE_NODE_UNSET_POINTERS:
vertex->next = vertex->prev = NULL; vertex->next_forward = vertex->next_backward = NULL;
graph->count--; graph->count--;
} }
@@ -255,15 +257,15 @@ wp_intern void prGraphDump(const PrGraph *graph) {
PrEdgeVertex *vertex = &graph->vertices[i]; PrEdgeVertex *vertex = &graph->vertices[i];
printf("%u:", id.index + 1); printf("%" PRIu64 ":", id.index + 1);
if (!(vertex->prev)) { if (!(vertex->next_backward)) {
printf(" (none)"); printf(" (none)");
} else { } else {
PrEdgeVertex *curr = vertex->prev; PrEdgeVertex *curr = vertex->next_backward;
while (curr) { while (curr) {
printf(" %u", curr->source.index + 1); printf(" %" PRIu64, curr->source.index + 1);
curr = curr->prev; curr = curr->next_backward;
} }
} }
@@ -277,15 +279,15 @@ wp_intern void prGraphDump(const PrGraph *graph) {
PrEdgeVertex *vertex = &graph->vertices[i]; PrEdgeVertex *vertex = &graph->vertices[i];
printf("%u:", id.index + 1); printf("%" PRIu64 ":", id.index + 1);
if (!(vertex->next)) { if (!(vertex->next_forward)) {
printf(" (none)"); printf(" (none)");
} else { } else {
PrEdgeVertex *curr = vertex->next; PrEdgeVertex *curr = vertex->next_forward;
while (curr) { while (curr) {
printf(" %u", curr->target.index + 1); printf(" %" PRIu64, curr->target.index + 1);
curr = curr->next; curr = curr->next_forward;
} }
} }