Skip to content

Add for_each_predecessor() to grapht #6160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/util/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ class grapht
std::vector<node_indext> get_predecessors(const node_indext &n) const;
std::vector<node_indext> get_successors(const node_indext &n) const;
void output_dot(std::ostream &out) const;
void for_each_predecessor(
const node_indext &n,
std::function<void(const node_indext &)> f) const;
void for_each_successor(
const node_indext &n,
std::function<void(const node_indext &)> f) const;
Expand Down Expand Up @@ -961,6 +964,17 @@ grapht<N>::get_successors(const node_indext &n) const
return result;
}

template <class N>
void grapht<N>::for_each_predecessor(
const node_indext &n,
std::function<void(const node_indext &)> f) const
{
std::for_each(
nodes[n].in.begin(),
nodes[n].in.end(),
[&](const std::pair<node_indext, edget> &edge) { f(edge.first); });
}

template <class N>
void grapht<N>::for_each_successor(
const node_indext &n,
Expand Down
11 changes: 11 additions & 0 deletions unit/util/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,17 @@ SCENARIO("predecessors-successors-graph", "[core][util][graph]")
REQUIRE(graph.get_successors(indices[0]).size() == 1);
REQUIRE(graph.get_predecessors(indices[1]).size() == 1);
REQUIRE(graph.get_successors(indices[1]).size() == 0);

int count = 0;
graph.for_each_predecessor(
indices[1], [&](const simple_grapht::node_indext &n) { count++; });
REQUIRE(count == 1);

// Refresh counter.
count = 0;
graph.for_each_successor(
indices[1], [&](const simple_grapht::node_indext &n) { count++; });
REQUIRE(count == 0);
}
}
}