summaryrefslogtreecommitdiff
path: root/ccan/aga/test/parallel.c
blob: 997280bc24ec964b57c2fdaf3ed674d13f5a9969 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "config.h"

#include <assert.h>

#include <ccan/aga/aga.h>
#include <ccan/container_of/container_of.h>
#include <ccan/ptrint/ptrint.h>

#include "simple-graph.h"

static ptrint_t *parallel_first_edge(const struct aga_graph *g,
				       const struct aga_node *n)
{
	struct parallel_graph *pg = container_of(g, struct parallel_graph, sg.g);

	if (n != &pg->sg.nodes[1]) {
		assert(n == &pg->sg.nodes[2]);
		return NULL;
	}

	if (pg->nlinks)
		return int2ptr(1);
	else
		return NULL;
}

static ptrint_t *parallel_next_edge(const struct aga_graph *g,
				      const struct aga_node *n,
				      ptrint_t *edge)
{
	struct parallel_graph *pg = container_of(g, struct parallel_graph, sg.g);
	int index = ptr2int(edge);

	if (n != &pg->sg.nodes[1]) {
		assert(n == &pg->sg.nodes[2]);
		return NULL;
	}

	if (index < pg->nlinks)
		return int2ptr(index + 1);
	else
		return NULL;
}

static int parallel_edge_info(const struct aga_graph *g, const struct aga_node *n,
			      ptrint_t *edge, struct aga_edge_info *ei)
{
	struct parallel_graph *pg = container_of(g, struct parallel_graph, sg.g);

	assert(n == &pg->sg.nodes[1]);

	ei->to = &pg->sg.nodes[2];
	return 0;
}

void parallel_graph_init(struct parallel_graph *pg, int nlinks)
{
	pg->nlinks = nlinks;

	simple_graph_init(&pg->sg, parallel_first_edge, parallel_next_edge,
			  parallel_edge_info);
}