9 #include "imstkTaskNode.h" 13 #include <unordered_map> 14 #include <unordered_set> 24 struct hash<
std::shared_ptr<imstk::TaskNode>>
26 std::size_t operator()(
const std::shared_ptr<imstk::TaskNode>& node)
const 29 return node->getGlobalId();
39 bool operator()(
const std::shared_ptr<imstk::TaskNode>& lhs,
40 const std::shared_ptr<imstk::TaskNode>& rhs)
const 42 return lhs->getGlobalId() == rhs->getGlobalId();
49 using TaskNodeVector = std::vector<std::shared_ptr<TaskNode>>;
50 using TaskNodeList = std::list<std::shared_ptr<TaskNode>>;
51 using TaskNodeSet = std::unordered_set<std::shared_ptr<TaskNode>>;
52 using TaskNodeAdjList = std::unordered_map<std::shared_ptr<TaskNode>, TaskNodeSet>;
53 using TaskNodeNameMap = std::unordered_map<std::shared_ptr<TaskNode>, std::string>;
64 TaskGraph(std::string sourceName =
"Source", std::string sinkName =
"Sink");
68 std::shared_ptr<TaskNode> getSource()
const {
return m_source; }
69 std::shared_ptr<TaskNode> getSink()
const {
return m_sink; }
80 const TaskNodeAdjList&
getAdjList()
const {
return m_adjList; }
92 TaskNodeVector::iterator findNode(std::string name);
93 TaskNodeVector::const_iterator findNode(std::string name)
const;
97 TaskNodeVector::iterator findNode(std::shared_ptr<TaskNode> node);
98 TaskNodeVector::const_iterator findNode(std::shared_ptr<TaskNode> node)
const;
103 bool containsNode(std::shared_ptr<TaskNode> node)
const;
108 TaskNodeVector::iterator
endNode() {
return m_nodes.end(); }
109 TaskNodeVector::const_iterator endNode()
const {
return m_nodes.end(); }
113 TaskNodeVector::iterator
beginNode() {
return m_nodes.begin(); }
114 TaskNodeVector::const_iterator beginNode()
const {
return m_nodes.begin(); }
120 bool addNode(std::shared_ptr<TaskNode> node);
125 void addNodes(
const std::vector<std::shared_ptr<TaskNode>>& nodes);
130 std::shared_ptr<TaskNode> addFunction(std::string name, std::function<
void()> func);
136 bool removeNode(std::shared_ptr<TaskNode> node);
142 bool removeNodeAndRedirect(std::shared_ptr<TaskNode> node);
147 void insertAfter(std::shared_ptr<TaskNode> refNode, std::shared_ptr<TaskNode> newNode);
152 void insertBefore(std::shared_ptr<TaskNode> refNode, std::shared_ptr<TaskNode> newNode);
159 bool containsEdge(std::shared_ptr<TaskNode> srcNode, std::shared_ptr<TaskNode> destNode)
const;
164 void addEdge(std::shared_ptr<TaskNode> srcNode, std::shared_ptr<TaskNode> destNode);
169 void addEdges(
const std::vector<std::pair<std::shared_ptr<TaskNode>, std::shared_ptr<TaskNode>>>& edges);
175 void nestGraph(std::shared_ptr<TaskGraph> subgraph, std::shared_ptr<TaskNode> source, std::shared_ptr<TaskNode> sink);
180 void removeEdge(std::shared_ptr<TaskNode> srcNode, std::shared_ptr<TaskNode> destNode);
185 bool isReachable(std::shared_ptr<TaskNode> srcNode, std::shared_ptr<TaskNode> destNode);
199 m_invAdjList.clear();
212 static std::shared_ptr<TaskNodeList> topologicalSort(std::shared_ptr<TaskGraph> graph);
218 static std::shared_ptr<TaskGraph> transitiveReduce(std::shared_ptr<TaskGraph> graph);
223 static std::shared_ptr<TaskGraph> removeRedundantNodes(std::shared_ptr<TaskGraph> graph);
229 static std::shared_ptr<TaskGraph>
reduce(std::shared_ptr<TaskGraph> graph)
231 std::shared_ptr<TaskGraph> reducedGraph = transitiveReduce(graph);
232 if (reducedGraph !=
nullptr)
234 return removeRedundantNodes(reducedGraph);
242 static std::shared_ptr<TaskGraph> removeUnusedNodes(std::shared_ptr<TaskGraph> graph);
247 static bool isCyclic(std::shared_ptr<TaskGraph> graph);
252 static TaskNodeNameMap getUniqueNodeNames(std::shared_ptr<TaskGraph> graph,
bool apply =
false);
257 static std::unordered_map<std::shared_ptr<TaskNode>,
double> getNodeStartTimes(std::shared_ptr<TaskGraph> graph);
262 static TaskNodeList getCriticalPath(std::shared_ptr<TaskGraph> graph);
265 TaskNodeVector m_nodes;
269 std::shared_ptr<TaskNode> m_source =
nullptr;
270 std::shared_ptr<TaskNode> m_sink =
nullptr;
Base class for TaskGraph, a collection of TaskNode's. Maintains nodes, adjacencyList, and invAdjacencyList.
const TaskNodeAdjList & getAdjList() const
Get the edges belonging to this graph.
const TaskNodeAdjList & getInvAdjList() const
Get the inverse edges belonging to this graph.
TaskNodeVector::iterator endNode()
Returns the start of the node container.
TaskNodeVector & getNodes()
Get the nodes belonging to this graph HS This is bad, there are algorithms that change the nodes of t...
static std::shared_ptr< TaskGraph > reduce(std::shared_ptr< TaskGraph > graph)
Simplifies the graph in a way that retains functionality Performs transitive reduction then redundant...
TaskNodeVector::iterator beginNode()
Returns the start of the node container.
Returns a hash value for a PointEntry.
TaskNodeAdjList m_adjList
This gives the outputs of every node.
TaskNodeAdjList m_invAdjList
This gives the inputs of every node.
void clearEdges()
Removes all edges from the graph.
Test if two points are the same or not by comparing their id and coordinates.