-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph.cpp
More file actions
171 lines (130 loc) · 3.18 KB
/
graph.cpp
File metadata and controls
171 lines (130 loc) · 3.18 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <ggraph/fork.hpp>
#include <ggraph/grain.hpp>
#include <ggraph/graph.hpp>
#include <ggraph/join.hpp>
#include <typeindex>
#include <unordered_map>
namespace ggraph {
/* entry */
entry::~entry()
{}
std::string
entry::debug_string() const
{
return "entry";
}
std::unique_ptr<operation>
entry::copy() const
{
return std::make_unique<entry>(*this);
}
/* exit */
exit::~exit()
{}
std::string
exit::debug_string() const
{
return "exit";
}
std::unique_ptr<operation>
exit::copy() const
{
return std::make_unique<exit>(*this);
}
/* validity */
static inline bool
is_valid_entry(const ggraph::node * n)
{
GGRAPH_DEBUG_ASSERT(is_entry(n->operation()));
if (n->npredecessors() != 0)
return false;
if (n->nsuccessors() != 1)
return false;
return true;
}
static inline bool
is_valid_exit(const ggraph::node * n)
{
GGRAPH_DEBUG_ASSERT(is_exit(n->operation()));
if (n->npredecessors() != 1)
return false;
if (n->nsuccessors() != 0)
return false;
return true;
}
static inline bool
is_valid_grain(const ggraph::node * n)
{
GGRAPH_DEBUG_ASSERT(is_grain(n->operation()));
if (n->npredecessors() != 1)
return false;
if (!is_fork(n->predecessor(0)->operation())
&& !is_join(n->predecessor(0)->operation())
&& !is_entry(n->predecessor(0)->operation()))
return false;
if (n->nsuccessors() != 1)
return false;
if (!is_join(n->successor(0)->operation())
&& !is_fork(n->successor(0)->operation())
&& !is_exit(n->successor(0)->operation()))
return false;
return true;
}
static inline bool
is_valid_fork(const ggraph::node * n)
{
GGRAPH_DEBUG_ASSERT(is_fork(n->operation()));
if (n->npredecessors() != 1)
return false;
if (!is_grain(n->predecessor(0)->operation())
&& !is_join(n->predecessor(0)->operation())
&& !is_entry(n->predecessor(0)->operation()))
return false;
if (n->nsuccessors() == 0)
return false;
for (size_t s = 0; s < n->nsuccessors(); s++) {
if (!is_grain(n->successor(s)->operation())
&& !is_join(n->successor(s)->operation()))
return false;
}
return true;
}
static inline bool
is_valid_join(const ggraph::node * n)
{
GGRAPH_DEBUG_ASSERT(is_join(n->operation()));
if (n->npredecessors() == 0)
return false;
for (size_t p = 0; p < n->npredecessors(); p++) {
if (!is_join(n->predecessor(p)->operation())
&& !is_grain(n->predecessor(p)->operation()))
return false;
}
if (n->nsuccessors() != 1)
return false;
if (!is_join(n->successor(0)->operation())
&& !is_fork(n->successor(0)->operation())
&& !is_exit(n->successor(0)->operation()))
return false;
return true;
}
bool
is_valid(const ggraph::graph & g)
{
static std::unordered_map<std::type_index, std::function<bool(const ggraph::node*)>> map({
{std::type_index(typeid(ggraph::entry)), is_valid_entry}
, {std::type_index(typeid(ggraph::exit)), is_valid_exit}
, {std::type_index(typeid(ggraph::grain)), is_valid_grain}
, {std::type_index(typeid(ggraph::fork)), is_valid_fork}
, {std::type_index(typeid(ggraph::join)), is_valid_join}
});
bool is_valid = true;
for (const auto & node : g) {
auto it = map.find(std::type_index(typeid(node.operation())));
GGRAPH_DEBUG_ASSERT(it != map.end());
is_valid = it->second(&node);
if (!is_valid) break;
}
return is_valid;
}
}