1 #ifndef __TAN_SRC_ANALYSIS_DEPENDENCY_GRAPH_H__
2 #define __TAN_SRC_ANALYSIS_DEPENDENCY_GRAPH_H__
5 #include <unordered_set>
17 _forward[dependent].push_back(depended);
18 _backward[depended].push_back(dependent);
19 _all.insert(depended);
20 _all.insert(dependent);
30 umap<T, int> num_depend{};
47 auto f = _backward.find(curr);
49 if (f != _backward.end()) {
52 for (
auto d : depended) {
54 if (num_depend[d] == 0) {
69 for (
auto [node, n_depend] : num_depend) {
71 return std::make_pair(std::nullopt, node);
74 return std::make_pair(ret, std::nullopt);
81 auto q = _backward.find(depended);
82 if (q == _backward.end()) {
85 return (
int)q->second.size();
93 auto q = _forward.find(dependent);
94 if (q == _forward.end()) {
97 return (
int)q->second.size();
108 umap<T, vector<T>> _forward{};
109 umap<T, vector<T>> _backward{};
110 std::unordered_set<T> _all{};
int num_depended(T dependent) const
Number of nodes that dependent depends on.
void add_dependency(T depended, T dependent)
dependent depends on depended
std::pair< std::optional< vector< T > >, std::optional< T > > topological_sort() const
Sort topologically so for no element is dependent on its succeeding element(s).
int num_dependent(T depended) const
Number of nodes that depends on depended.