]> git.ozlabs.org Git - ccan/blob - ccan/aga/test/parallel.c
tal: allow notifiers on NULL.
[ccan] / ccan / aga / test / parallel.c
1 #include "config.h"
2
3 #include <assert.h>
4
5 #include <ccan/aga/aga.h>
6 #include <ccan/container_of/container_of.h>
7 #include <ccan/ptrint/ptrint.h>
8
9 #include "simple-graph.h"
10
11 static ptrint_t *parallel_first_edge(const struct aga_graph *g,
12                                        const struct aga_node *n)
13 {
14         struct parallel_graph *pg = container_of(g, struct parallel_graph, sg.g);
15
16         if (n != &pg->sg.nodes[1]) {
17                 assert(n == &pg->sg.nodes[2]);
18                 return NULL;
19         }
20
21         if (pg->nlinks)
22                 return int2ptr(1);
23         else
24                 return NULL;
25 }
26
27 static ptrint_t *parallel_next_edge(const struct aga_graph *g,
28                                       const struct aga_node *n,
29                                       ptrint_t *edge)
30 {
31         struct parallel_graph *pg = container_of(g, struct parallel_graph, sg.g);
32         int index = ptr2int(edge);
33
34         if (n != &pg->sg.nodes[1]) {
35                 assert(n == &pg->sg.nodes[2]);
36                 return NULL;
37         }
38
39         if (index < pg->nlinks)
40                 return int2ptr(index + 1);
41         else
42                 return NULL;
43 }
44
45 static int parallel_edge_info(const struct aga_graph *g, const struct aga_node *n,
46                               ptrint_t *edge, struct aga_edge_info *ei)
47 {
48         struct parallel_graph *pg = container_of(g, struct parallel_graph, sg.g);
49
50         assert(n == &pg->sg.nodes[1]);
51
52         ei->to = &pg->sg.nodes[2];
53         if (ptr2int(edge) == pg->cheaplink)
54                 ei->icost = 1;
55         else
56                 ei->icost = 2;
57         return 0;
58 }
59
60 void parallel_graph_init(struct parallel_graph *pg, int nlinks, int cheaplink)
61 {
62         pg->nlinks = nlinks;
63         pg->cheaplink = cheaplink;
64
65         simple_graph_init(&pg->sg, parallel_first_edge, parallel_next_edge,
66                           parallel_edge_info);
67 }