]> git.ozlabs.org Git - ccan/blob - ccan/aga/test/grid.c
crypto/shachain/tools: update to new rbuf API.
[ccan] / ccan / aga / test / grid.c
1 #include "config.h"
2
3 #include <assert.h>
4
5 #include <ccan/container_of/container_of.h>
6 #include <ccan/ptrint/ptrint.h>
7
8 #include <ccan/aga/aga.h>
9
10 #include "simple-graph.h"
11
12 static ptrint_t *grid_first_edge(const struct aga_graph *g,
13                                  const struct aga_node *n)
14 {
15         return int2ptr(1);
16 }
17
18 static ptrint_t *grid_next_edge(const struct aga_graph *g,
19                                 const struct aga_node *n,
20                                 ptrint_t *e)
21 {
22         int index = ptr2int(e);
23
24         if (index < 4)
25                 return int2ptr(index + 1);
26         else
27                 return NULL;           
28 }
29
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)
33 {
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;
38         int i = ptr2int(e);
39
40         assert((x >= 1) && (x <= gg->nx));
41         assert((y >= 1) && (y <= gg->ny));
42
43         switch (i) {
44         case 1: /* right */
45                 if (gg->right && (x != gg->nx))
46                         ei->to = &gg->sg.nodes[ni + 1];
47                 break;
48
49         case 2: /* down */
50                 if (gg->down && (y != gg->ny))
51                         ei->to = &gg->sg.nodes[ni + gg->nx];
52                 break;
53                 
54         case 3: /* left */
55                 if (gg->left && (x != 1))
56                         ei->to = &gg->sg.nodes[ni - 1];
57                 break;
58
59         case 4: /* up */
60                 if (gg->up && (y != 1))
61                         ei->to = &gg->sg.nodes[ni - gg->nx];
62                 break;
63
64         default:
65                 assert(0);
66         }
67         return 0;
68 }
69
70 void grid_graph_init(struct grid_graph *gg, int nx, int ny,
71                      bool right, bool down, bool left, bool up)
72 {
73         assert((nx * ny) < MAX_NODES);
74
75         gg->nx = nx;
76         gg->ny = ny;
77         gg->right = right;
78         gg->down = down;
79         gg->left = left;
80         gg->up = up;
81
82         simple_graph_init(&gg->sg, grid_first_edge, grid_next_edge,
83                           grid_edge_info);
84 }