]> git.ozlabs.org Git - ccan/blobdiff - ccan/agar/test/parallel.c
agar: Re-entrant Abstract Graph Algorithms
[ccan] / ccan / agar / test / parallel.c
diff --git a/ccan/agar/test/parallel.c b/ccan/agar/test/parallel.c
new file mode 100644 (file)
index 0000000..741ff3a
--- /dev/null
@@ -0,0 +1,62 @@
+#include "config.h"
+
+#include <assert.h>
+
+#include <ccan/agar/agar.h>
+#include <ccan/container_of/container_of.h>
+#include <ccan/ptrint/ptrint.h>
+
+#include "simple-graphr.h"
+
+static const void *parallel_first_edge_r(const struct agar_graph *gr,
+                                        const void *nr)
+{
+       const struct parallel_graphr *pgr
+               = container_of(gr, struct parallel_graphr, gr);
+
+       if (ptr2int(nr) != 1) {
+               assert(ptr2int(nr) == 2);
+               return NULL;
+       }
+
+       if (pgr->nlinks)
+               return int2ptr(1);
+       else
+               return NULL;
+}
+
+static const void *parallel_next_edge_r(const struct agar_graph *gr,
+                                       const void *nr, const void *edge)
+{
+       const struct parallel_graphr *pgr
+               = container_of(gr, struct parallel_graphr, gr);
+       int index = ptr2int(edge);
+
+       if (ptr2int(nr) != 1) {
+               assert(ptr2int(nr) == 2);
+               return NULL;
+       }
+
+       if (index < pgr->nlinks)
+               return int2ptr(index + 1);
+       else
+               return NULL;
+}
+
+static int parallel_edge_info_r(const struct agar_graph *gr,
+                               const void *nr, const void *edge,
+                               struct agar_edge_info *eir)
+{
+       assert(ptr2int(nr) == 1);
+
+       eir->to = int2ptr(2);
+       return 0;
+}
+
+void parallel_graphr_init(struct parallel_graphr *pgr, int nlinks)
+{
+       pgr->nlinks = nlinks;
+
+       agar_init_graph(&pgr->gr, parallel_first_edge_r, parallel_next_edge_r,
+                       parallel_edge_info_r);
+}