| 45 | } |
| 46 | |
| 47 | bool LoopCheck::addEdge(SymbolId from, SymbolId to) { |
| 48 | // Create Graph |
| 49 | std::map<SymbolId, Node*>::iterator fromIt = m_nodes.find(from); |
| 50 | std::map<SymbolId, Node*>::iterator toIt = m_nodes.find(to); |
| 51 | Node* nodeFrom = nullptr; |
| 52 | Node* nodeTo = nullptr; |
| 53 | if (fromIt == m_nodes.end()) { |
| 54 | nodeFrom = new Node(from); |
| 55 | m_nodes.emplace(from, nodeFrom); |
| 56 | } else { |
| 57 | nodeFrom = (*fromIt).second; |
| 58 | } |
| 59 | if (toIt == m_nodes.end()) { |
| 60 | nodeTo = new Node(to); |
| 61 | m_nodes.emplace(to, nodeTo); |
| 62 | } else { |
| 63 | nodeTo = (*toIt).second; |
| 64 | } |
| 65 | nodeFrom->m_toList.insert(nodeTo); |
| 66 | |
| 67 | for (auto& itr : m_nodes) { |
| 68 | itr.second->m_visited = false; |
| 69 | } |
| 70 | |
| 71 | // BFS |
| 72 | std::queue<Node*> queue; |
| 73 | queue.push(nodeTo); |
| 74 | nodeTo->m_visited = true; |
| 75 | while (!queue.empty()) { |
| 76 | Node* tmp = queue.front(); |
| 77 | queue.pop(); |
| 78 | tmp->m_visited = true; |
| 79 | for (auto next : tmp->m_toList) { |
| 80 | if (next->m_visited) |
| 81 | return true; |
| 82 | else { |
| 83 | queue.push(next); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | std::vector<SymbolId> LoopCheck::reportLoop() const { |
| 92 | std::vector<SymbolId> loop; |