Address dag_man issues

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