summaryrefslogtreecommitdiff
path: root/ccan/agar/test/traversal1.c
blob: 0a465989f6f7e3a82ac291dd123f055475560214 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "config.h"

#include <assert.h>

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

#include <ccan/agar/agar.h>

#include "simple-graphr.h"

static const void *traversal1_first_edge_r(const struct agar_graph *gr,
					   const void *nr)
{
	int ni = ptr2int(nr);

	switch (ni) {
	case 1:
	case 2:
	case 3:

	case 7:
	case 8:
	case 9:
		return int2ptr(1);

	case 4:
	case 5:
	case 6:
		return NULL;

	default:
		assert(0);
	}
}

static const void *traversal1_next_edge_r(const struct agar_graph *gr,
					  const void *nr, const void *e)
{
	int ni = ptr2int(nr);
	int index = ptr2int(e);

	assert((ni < 4) || (ni > 6));
	if (index == 1)
		return int2ptr(2);
	else if (index == 2)
		return NULL;
	else
		assert(0);
}

static int traversal1_edge_info_r(const struct agar_graph *gr,
				  const void *nr, const void *e,
				  struct agar_edge_info *eir)
{
	int ni = ptr2int(nr);
	int index = ptr2int(e);

	assert((index == 1) || (index == 2));

	switch (ni) {
	case 1:
		if (index == 1)
			eir->to = int2ptr(2);
		else
			eir->to = int2ptr(3);
		break;

	case 2:
		if (index == 1)
			eir->to = int2ptr(4);
		else
			eir->to = int2ptr(5);
		break;
	case 3:
		if (index == 1)
			eir->to = int2ptr(5);
		else
			eir->to = int2ptr(6);
		break;

	case 7:
		if (index == 1)
			eir->to = int2ptr(5);
		else
			eir->to = int2ptr(4);
		break;

	case 8:
		if (index == 1)
			eir->to = int2ptr(6);
		else
			eir->to = int2ptr(5);
		break;

	case 9:
		if (index == 1)
			eir->to = int2ptr(8);
		else
			eir->to = int2ptr(7);
		break;

	default:
		assert(0);
	}
	return 0;
}

void traversal1_graphr_init(struct traversal1_graphr *t1gr)
{
	agar_init_graph(&t1gr->gr,
			traversal1_first_edge_r, traversal1_next_edge_r,
			traversal1_edge_info_r);
}