28 template <
typename TNodeId>
30 std::function<std::vector<TNodeId>(TNodeId)> getIncomingEdges,
31 std::map<TNodeId, NodeState>& nodeStates)
33 for (TNodeId childNode : getIncomingEdges(node))
35 if (nodeStates.find(childNode) == nodeStates.end())
41 if (nodeStates.find(childNode)->second == NodeState::Visiting)
51 template<
typename TNodeId>
52 bool TopologicallySort(
54 std::function<std::vector<TNodeId>(TNodeId)> getIncomingEdges,
55 std::vector<TNodeId>& outSorted,
56 std::map<TNodeId, NodeState>& nodeStates)
58 std::stack<TNodeId> nodeStack;
61 if (nodeStates.find(initialNode) == nodeStates.end())
63 nodeStack.push(initialNode);
66 while (!nodeStack.empty())
68 TNodeId current = nodeStack.top();
70 nodeStates[current] = NodeState::Visiting;
72 auto nextChildOfCurrent = GetNextChild(current, getIncomingEdges, nodeStates);
74 if (nextChildOfCurrent)
76 TNodeId nextChild = nextChildOfCurrent.value();
79 if (nodeStates.find(nextChild) == nodeStates.end())
81 nodeStack.push(nextChild);
86 if (nodeStates[nextChild] == NodeState::Visiting)
94 nodeStates[current] = NodeState::Visited;
95 outSorted.push_back(current);
109 template<
typename TNodeId,
typename TTargetNodes>
111 const TTargetNodes& targetNodes,
112 std::function<std::vector<TNodeId>(TNodeId)> getIncomingEdges,
113 std::vector<TNodeId>& outSorted)
116 std::map<TNodeId, NodeState> nodeStates;
118 for (TNodeId targetNode : targetNodes)
120 if (!TopologicallySort(targetNode, getIncomingEdges, outSorted, nodeStates))