5 #include <ccan/container_of/container_of.h>
6 #include <ccan/ptrint/ptrint.h>
8 #include <ccan/aga/aga.h>
10 #include "simple-graph.h"
12 static ptrint_t *grid_first_edge(const struct aga_graph *g,
13 const struct aga_node *n)
18 static ptrint_t *grid_next_edge(const struct aga_graph *g,
19 const struct aga_node *n,
22 int index = ptr2int(e);
25 return int2ptr(index + 1);
30 static int grid_edge_info(const struct aga_graph *g,
31 const struct aga_node *n,
32 ptrint_t *e, struct aga_edge_info *ei)
34 struct grid_graph *gg = container_of(g, struct grid_graph, sg.g);
35 int ni = n - gg->sg.nodes;
36 int x = ((ni - 1) % gg->nx) + 1;
37 int y = ((ni - 1) / gg->nx) + 1;
40 assert((x >= 1) && (x <= gg->nx));
41 assert((y >= 1) && (y <= gg->ny));
45 if (gg->right && (x != gg->nx))
46 ei->to = &gg->sg.nodes[ni + 1];
50 if (gg->down && (y != gg->ny))
51 ei->to = &gg->sg.nodes[ni + gg->nx];
55 if (gg->left && (x != 1))
56 ei->to = &gg->sg.nodes[ni - 1];
60 if (gg->up && (y != 1))
61 ei->to = &gg->sg.nodes[ni - gg->nx];
70 void grid_graph_init(struct grid_graph *gg, int nx, int ny,
71 bool right, bool down, bool left, bool up)
73 assert((nx * ny) < MAX_NODES);
82 simple_graph_init(&gg->sg, grid_first_edge, grid_next_edge,