scenario_simulator_v2 C++ API
circular_check.hpp
Go to the documentation of this file.
1 
2 // Copyright 2015 TIER IV, Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef OPENSCENARIO_INTERPRETER__UTILITY__CIRCULAR_CHECK_HPP_
17 #define OPENSCENARIO_INTERPRETER__UTILITY__CIRCULAR_CHECK_HPP_
18 
19 #include <unordered_map>
20 #include <utility>
21 
23 {
24 inline namespace utility
25 {
26 namespace detail
27 {
28 template <class Node, class NodeToChildren, class NodeToBool>
29 bool circular_check(const Node & node, NodeToChildren && node_to_children, NodeToBool & visit_flag)
30 {
31  if (visit_flag[node]) {
32  return true;
33  }
34 
35  visit_flag[node] = true;
36 
37  for (auto && child : node_to_children(node)) {
38  if (child == node) continue;
39 
40  if (circular_check(child, node_to_children, visit_flag)) {
41  return true;
42  }
43  visit_flag[child] = false;
44  }
45 
46  return false;
47 }
48 } // namespace detail
49 
50 template <class Node, class NodeToChildren, class Hash = std::hash<Node>>
51 bool circular_check(const Node & init, NodeToChildren && node_to_children)
52 {
53  std::unordered_map<Node, bool, Hash> visit_flag;
54  return detail::circular_check(init, node_to_children, visit_flag);
55 }
56 } // namespace utility
57 } // namespace openscenario_interpreter
58 #endif // OPENSCENARIO_INTERPRETER__UTILITY__CIRCULAR_CHECK_HPP_
bool circular_check(const Node &node, NodeToChildren &&node_to_children, NodeToBool &visit_flag)
Definition: circular_check.hpp:29
bool circular_check(const Node &init, NodeToChildren &&node_to_children)
Definition: circular_check.hpp:51
Definition: escape_sequence.hpp:22