]> git.ozlabs.org Git - ccan/blob - ccan/agar/test/parallel.c
agar: Re-entrant Abstract Graph Algorithms
[ccan] / ccan / agar / test / parallel.c
1 #include "config.h"
2
3 #include <assert.h>
4
5 #include <ccan/agar/agar.h>
6 #include <ccan/container_of/container_of.h>
7 #include <ccan/ptrint/ptrint.h>
8
9 #include "simple-graphr.h"
10
11 static const void *parallel_first_edge_r(const struct agar_graph *gr,
12                                          const void *nr)
13 {
14         const struct parallel_graphr *pgr
15                 = container_of(gr, struct parallel_graphr, gr);
16
17         if (ptr2int(nr) != 1) {
18                 assert(ptr2int(nr) == 2);
19                 return NULL;
20         }
21
22         if (pgr->nlinks)
23                 return int2ptr(1);
24         else
25                 return NULL;
26 }
27
28 static const void *parallel_next_edge_r(const struct agar_graph *gr,
29                                         const void *nr, const void *edge)
30 {
31         const struct parallel_graphr *pgr
32                 = container_of(gr, struct parallel_graphr, gr);
33         int index = ptr2int(edge);
34
35         if (ptr2int(nr) != 1) {
36                 assert(ptr2int(nr) == 2);
37                 return NULL;
38         }
39
40         if (index < pgr->nlinks)
41                 return int2ptr(index + 1);
42         else
43                 return NULL;
44 }
45
46 static int parallel_edge_info_r(const struct agar_graph *gr,
47                                 const void *nr, const void *edge,
48                                 struct agar_edge_info *eir)
49 {
50         assert(ptr2int(nr) == 1);
51
52         eir->to = int2ptr(2);
53         return 0;
54 }
55
56 void parallel_graphr_init(struct parallel_graphr *pgr, int nlinks)
57 {
58         pgr->nlinks = nlinks;
59
60         agar_init_graph(&pgr->gr, parallel_first_edge_r, parallel_next_edge_r,
61                         parallel_edge_info_r);
62 }