Skip to content

Commit 9936169

Browse files
authored
Merge pull request #6158 from johnfxgalea/add-get-predecessors
Adds get_predecessors() to grapht
2 parents e64fcec + a3c1e1e commit 9936169

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/util/graph.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ class grapht
312312

313313
std::list<node_indext> topsort() const;
314314

315+
std::vector<node_indext> get_predecessors(const node_indext &n) const;
315316
std::vector<node_indext> get_successors(const node_indext &n) const;
316317
void output_dot(std::ostream &out) const;
317318
void for_each_successor(
@@ -934,6 +935,19 @@ void output_dot_generic(
934935
});
935936
}
936937

938+
template <class N>
939+
std::vector<typename grapht<N>::node_indext>
940+
grapht<N>::get_predecessors(const node_indext &n) const
941+
{
942+
std::vector<node_indext> result;
943+
std::transform(
944+
nodes[n].in.begin(),
945+
nodes[n].in.end(),
946+
std::back_inserter(result),
947+
[&](const std::pair<node_indext, edget> &edge) { return edge.first; });
948+
return result;
949+
}
950+
937951
template <class N>
938952
std::vector<typename grapht<N>::node_indext>
939953
grapht<N>::get_successors(const node_indext &n) const

unit/util/graph.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,25 @@ SCENARIO("graph-connected-subgraphs",
310310
}
311311
}
312312
}
313+
314+
SCENARIO("predecessors-successors-graph", "[core][util][graph]")
315+
{
316+
GIVEN("A graph")
317+
{
318+
simple_grapht graph;
319+
simple_grapht::node_indext indices[2];
320+
321+
for(int i = 0; i < 2; ++i)
322+
indices[i] = graph.add_node();
323+
324+
graph.add_edge(indices[0], indices[1]);
325+
326+
THEN("Nodes should have correct successors and predecessors")
327+
{
328+
REQUIRE(graph.get_predecessors(indices[0]).size() == 0);
329+
REQUIRE(graph.get_successors(indices[0]).size() == 1);
330+
REQUIRE(graph.get_predecessors(indices[1]).size() == 1);
331+
REQUIRE(graph.get_successors(indices[1]).size() == 0);
332+
}
333+
}
334+
}

0 commit comments

Comments
 (0)