File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -312,6 +312,7 @@ class grapht
312
312
313
313
std::list<node_indext> topsort () const ;
314
314
315
+ std::vector<node_indext> get_predecessors (const node_indext &n) const ;
315
316
std::vector<node_indext> get_successors (const node_indext &n) const ;
316
317
void output_dot (std::ostream &out) const ;
317
318
void for_each_successor (
@@ -934,6 +935,19 @@ void output_dot_generic(
934
935
});
935
936
}
936
937
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
+
937
951
template <class N >
938
952
std::vector<typename grapht<N>::node_indext>
939
953
grapht<N>::get_successors(const node_indext &n) const
Original file line number Diff line number Diff line change @@ -310,3 +310,25 @@ SCENARIO("graph-connected-subgraphs",
310
310
}
311
311
}
312
312
}
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
+ }
You can’t perform that action at this time.
0 commit comments