28enum eCyclicCheckVisitedState {
40 Relation *via_relation;
43struct CyclesSolverState {
44 CyclesSolverState(Depsgraph *graph) : graph(graph) {}
47 if (num_cycles != 0) {
48 printf(
"Detected %d dependency cycles\n", num_cycles);
52 Stack<StackEntry> traversal_stack;
56inline void set_node_visited_state(
Node *node, eCyclicCheckVisitedState
state)
58 node->custom_flags = (node->custom_flags & ~0x3) |
int(
state);
61inline eCyclicCheckVisitedState get_node_visited_state(
Node *node)
63 return (eCyclicCheckVisitedState)(node->custom_flags & 0x3);
66inline void set_node_num_visited_children(
Node *node,
int num_children)
68 node->custom_flags = (node->custom_flags & 0x3) | (num_children << 2);
71inline int get_node_num_visited_children(
Node *node)
73 return node->custom_flags >> 2;
81 entry.via_relation =
nullptr;
82 state->traversal_stack.push(entry);
83 set_node_visited_state(node, NODE_IN_STACK);
87void schedule_leaf_nodes(CyclesSolverState *
state)
90 bool has_inlinks =
false;
91 for (
Relation *rel : node->inlinks) {
96 node->custom_flags = 0;
97 if (has_inlinks ==
false) {
98 schedule_node_to_stack(
state, node);
101 set_node_visited_state(node, NODE_NOT_VISITED);
109bool schedule_non_checked_node(CyclesSolverState *
state)
112 if (get_node_visited_state(node) == NODE_NOT_VISITED) {
113 schedule_node_to_stack(
state, node);
120bool check_relation_can_murder(
Relation *relation)
128Relation *select_relation_to_murder(
Relation *relation, StackEntry *cycle_start_entry)
134 if (check_relation_can_murder(relation)) {
137 StackEntry *current = cycle_start_entry;
139 while (current->node != to_node) {
140 if (check_relation_can_murder(current->via_relation)) {
141 return current->via_relation;
143 current = current->from;
149void solve_cycles(CyclesSolverState *
state)
151 Stack<StackEntry> &traversal_stack =
state->traversal_stack;
152 while (!traversal_stack.
is_empty()) {
153 StackEntry *entry = &traversal_stack.
peek();
155 bool all_child_traversed =
true;
156 const int num_visited = get_node_num_visited_children(node);
157 for (
int i = num_visited;
i < node->outlinks.size();
i++) {
161 eCyclicCheckVisitedState to_state = get_node_visited_state(to);
162 if (to_state == NODE_IN_STACK) {
163 std::string cycle_str =
" " + to->full_identifier() +
" depends on\n " +
164 node->full_identifier() +
" via '" + rel->name +
"'\n";
165 StackEntry *current = entry;
166 while (current->node != to) {
168 cycle_str +=
" " + current->from->node->full_identifier() +
" via '" +
169 current->via_relation->name +
"'\n";
170 current = current->from;
172 printf(
"Dependency cycle detected:\n%s", cycle_str.c_str());
173 Relation *sacrificial_relation = select_relation_to_murder(rel, entry);
177 else if (to_state == NODE_NOT_VISITED) {
178 StackEntry new_entry;
180 new_entry.from = entry;
181 new_entry.via_relation = rel;
182 traversal_stack.
push(new_entry);
183 set_node_visited_state(node, NODE_IN_STACK);
184 all_child_traversed =
false;
185 set_node_num_visited_children(node,
i);
190 if (all_child_traversed) {
191 set_node_visited_state(node, NODE_VISITED);
192 traversal_stack.
pop();
201 CyclesSolverState
state(graph);
203 schedule_leaf_nodes(&
state);
204 solve_cycles(&
state);
209 while (schedule_non_checked_node(&
state)) {
210 solve_cycles(&
state);
void push(const T &value)
void deg_graph_detect_cycles(Depsgraph *graph)