core-lightning/plugins/askrene/graph.c
Lagrang3 b1cd26373b askrene: small fixes suggested by Rusty Russell
- use graph_max_num_arcs/nodes instead of tal_count in bound checks,
- don't use ccan/lqueue, use instead a minimalistic queue
  implementation with an array,
- add missing const qualifiers to temporary tal allocators,
- check preconditions with assert,
- remove inline specifier for static functions,

Changelog-None

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
2024-11-21 16:17:52 +10:30

75 lines
2.2 KiB
C

#include "config.h"
#include <plugins/askrene/graph.h>
/* in the background add the actual arc or dual arc */
static void graph_push_outbound_arc(struct graph *graph, const struct arc arc,
const struct node node)
{
assert(arc.idx < graph_max_num_arcs(graph));
assert(node.idx < graph_max_num_nodes(graph));
/* arc is already added, skip */
if (graph->arc_tail[arc.idx].idx != INVALID_INDEX)
return;
graph->arc_tail[arc.idx] = node;
const struct arc first_arc = graph->node_adjacency_first[node.idx];
graph->node_adjacency_next[arc.idx] = first_arc;
graph->node_adjacency_first[node.idx] = arc;
}
bool graph_add_arc(struct graph *graph, const struct arc arc,
const struct node from, const struct node to)
{
assert(from.idx < graph->max_num_nodes);
assert(to.idx < graph->max_num_nodes);
const struct arc dual = arc_dual(graph, arc);
if (arc.idx >= graph->max_num_arcs || dual.idx >= graph->max_num_arcs)
return false;
graph_push_outbound_arc(graph, arc, from);
graph_push_outbound_arc(graph, dual, to);
return true;
}
struct graph *graph_new(const tal_t *ctx, const size_t max_num_nodes,
const size_t max_num_arcs, const size_t arc_dual_bit)
{
struct graph *graph;
graph = tal(ctx, struct graph);
/* bad allocation of graph */
if (!graph)
return graph;
graph->max_num_arcs = max_num_arcs;
graph->max_num_nodes = max_num_nodes;
graph->arc_dual_bit = arc_dual_bit;
graph->arc_tail = tal_arr(graph, struct node, graph->max_num_arcs);
graph->node_adjacency_first =
tal_arr(graph, struct arc, graph->max_num_nodes);
graph->node_adjacency_next =
tal_arr(graph, struct arc, graph->max_num_arcs);
/* bad allocation of graph components */
if (!graph->arc_tail || !graph->node_adjacency_first ||
!graph->node_adjacency_next) {
return tal_free(graph);
}
/* initialize with invalid indexes so that we know these slots have
* never been used, eg. arc/node is newly created */
for (size_t i = 0; i < graph->max_num_arcs; i++)
graph->arc_tail[i] = node_obj(INVALID_INDEX);
for (size_t i = 0; i < graph->max_num_nodes; i++)
graph->node_adjacency_first[i] = arc_obj(INVALID_INDEX);
for (size_t i = 0; i < graph->max_num_nodes; i++)
graph->node_adjacency_next[i] = arc_obj(INVALID_INDEX);
return graph;
}