Skip to content

Commit d7df1a2

Browse files
authored
Merge pull request #6160 from johnfxgalea/add-for-each-predecessor
Add for_each_predecessor() to grapht
2 parents 26f9de7 + 538070c commit d7df1a2

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/util/graph.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ class grapht
315315
std::vector<node_indext> get_predecessors(const node_indext &n) const;
316316
std::vector<node_indext> get_successors(const node_indext &n) const;
317317
void output_dot(std::ostream &out) const;
318+
void for_each_predecessor(
319+
const node_indext &n,
320+
std::function<void(const node_indext &)> f) const;
318321
void for_each_successor(
319322
const node_indext &n,
320323
std::function<void(const node_indext &)> f) const;
@@ -961,6 +964,17 @@ grapht<N>::get_successors(const node_indext &n) const
961964
return result;
962965
}
963966

967+
template <class N>
968+
void grapht<N>::for_each_predecessor(
969+
const node_indext &n,
970+
std::function<void(const node_indext &)> f) const
971+
{
972+
std::for_each(
973+
nodes[n].in.begin(),
974+
nodes[n].in.end(),
975+
[&](const std::pair<node_indext, edget> &edge) { f(edge.first); });
976+
}
977+
964978
template <class N>
965979
void grapht<N>::for_each_successor(
966980
const node_indext &n,

unit/util/graph.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,17 @@ SCENARIO("predecessors-successors-graph", "[core][util][graph]")
329329
REQUIRE(graph.get_successors(indices[0]).size() == 1);
330330
REQUIRE(graph.get_predecessors(indices[1]).size() == 1);
331331
REQUIRE(graph.get_successors(indices[1]).size() == 0);
332+
333+
int count = 0;
334+
graph.for_each_predecessor(
335+
indices[1], [&](const simple_grapht::node_indext &n) { count++; });
336+
REQUIRE(count == 1);
337+
338+
// Refresh counter.
339+
count = 0;
340+
graph.for_each_successor(
341+
indices[1], [&](const simple_grapht::node_indext &n) { count++; });
342+
REQUIRE(count == 0);
332343
}
333344
}
334345
}

0 commit comments

Comments
 (0)