From ba0420cba278797804cb585149819fa2b7c9de2d Mon Sep 17 00:00:00 2001 From: johnfxgalea Date: Sun, 30 May 2021 20:22:25 +0100 Subject: [PATCH 1/2] Add for_each_predecessor() to grapht --- src/util/graph.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/util/graph.h b/src/util/graph.h index e5277ada414..9b9f6b6a5e8 100644 --- a/src/util/graph.h +++ b/src/util/graph.h @@ -315,6 +315,9 @@ class grapht std::vector get_predecessors(const node_indext &n) const; std::vector get_successors(const node_indext &n) const; void output_dot(std::ostream &out) const; + void for_each_predecessor( + const node_indext &n, + std::function f) const; void for_each_successor( const node_indext &n, std::function f) const; @@ -961,6 +964,17 @@ grapht::get_successors(const node_indext &n) const return result; } +template +void grapht::for_each_predecessor( + const node_indext &n, + std::function f) const +{ + std::for_each( + nodes[n].in.begin(), + nodes[n].in.end(), + [&](const std::pair &edge) { f(edge.first); }); +} + template void grapht::for_each_successor( const node_indext &n, From 538070c24613de971e95ba15665c9451a59b1e58 Mon Sep 17 00:00:00 2001 From: johnfxgalea Date: Sun, 30 May 2021 21:02:32 +0100 Subject: [PATCH 2/2] Add tests for for_each_predecessor() and for_each_successor() --- unit/util/graph.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/unit/util/graph.cpp b/unit/util/graph.cpp index 40ff2a4fc59..30afb8f2441 100644 --- a/unit/util/graph.cpp +++ b/unit/util/graph.cpp @@ -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); } } }