5 #include <ccan/container_of/container_of.h>
6 #include <ccan/ptrint/ptrint.h>
8 #include <ccan/agar/agar.h>
10 #include "simple-graphr.h"
12 static const void *grid_first_edge_r(const struct agar_graph *gr,
18 static const void *grid_next_edge_r(const struct agar_graph *gr,
19 const void *nr, const void *e)
21 int index = ptr2int(e);
24 return int2ptr(index + 1);
29 static int grid_edge_info_r(const struct agar_graph *gr,
30 const void *nr, const void *e,
31 struct agar_edge_info *eir)
33 struct grid_graphr *ggr = container_of(gr, struct grid_graphr, gr);
35 int x = ((ni - 1) % ggr->nx) + 1;
36 int y = ((ni - 1) / ggr->nx) + 1;
39 assert((x >= 1) && (x <= ggr->nx));
40 assert((y >= 1) && (y <= ggr->ny));
44 if (ggr->right && (x != ggr->nx))
45 eir->to = int2ptr(ni + 1);
49 if (ggr->down && (y != ggr->ny))
50 eir->to = int2ptr(ni + ggr->nx);
54 if (ggr->left && (x != 1))
55 eir->to = int2ptr(ni - 1);
59 if (ggr->up && (y != 1))
60 eir->to = int2ptr(ni - ggr->nx);
69 void grid_graphr_init(struct grid_graphr *ggr, int nx, int ny,
70 bool right, bool down, bool left, bool up)
79 agar_init_graph(&ggr->gr, grid_first_edge_r, grid_next_edge_r,