@@ -26,6 +26,7 @@ void test_temp();
2626void test_bacon ();
2727void test_remove_labeled_vertex ();
2828void test_multiple_associative_container ();
29+ void test_remove_vertex_all_types ();
2930
3031int main ()
3132{
@@ -35,6 +36,7 @@ int main()
3536 test_bacon ();
3637 test_remove_labeled_vertex ();
3738 test_multiple_associative_container ();
39+ test_remove_vertex_all_types ();
3840}
3941
4042// ////////////////////////////////////
@@ -208,3 +210,216 @@ void test_multiple_associative_container()
208210 g.remove_vertex (" test" );
209211 BOOST_ASSERT (num_vertices (g) == 0 );
210212}
213+
214+ template <typename LabeledGraph, typename Label>
215+ void test_remove_vertex_suite (Label l1, Label l2, Label l3)
216+ {
217+ // remove + label cleanup
218+ {
219+ LabeledGraph g;
220+ g.add_vertex (l1);
221+ BOOST_ASSERT (num_vertices (g) == 1 );
222+ BOOST_ASSERT (g.vertex (l1) != LabeledGraph::null_vertex ());
223+
224+ g.remove_vertex (l1);
225+ BOOST_ASSERT (num_vertices (g) == 0 );
226+ BOOST_ASSERT (g.vertex (l1) == LabeledGraph::null_vertex ());
227+ }
228+
229+ // remove one + keep others
230+ {
231+ LabeledGraph g;
232+ g.add_vertex (l1);
233+ g.add_vertex (l2);
234+ g.add_vertex (l3);
235+ BOOST_ASSERT (num_vertices (g) == 3 );
236+
237+ g.remove_vertex (l2);
238+ BOOST_ASSERT (num_vertices (g) == 2 );
239+ BOOST_ASSERT (g.vertex (l2) == LabeledGraph::null_vertex ());
240+ BOOST_ASSERT (g.vertex (l1) != LabeledGraph::null_vertex ());
241+ BOOST_ASSERT (g.vertex (l3) != LabeledGraph::null_vertex ());
242+ }
243+
244+ // reuse label after removal
245+ {
246+ LabeledGraph g;
247+ g.add_vertex (l1);
248+ g.remove_vertex (l1);
249+ g.add_vertex (l1);
250+ BOOST_ASSERT (num_vertices (g) == 1 );
251+ BOOST_ASSERT (g.vertex (l1) != LabeledGraph::null_vertex ());
252+ }
253+
254+ // Remove all
255+ {
256+ LabeledGraph g;
257+ g.add_vertex (l1);
258+ g.add_vertex (l2);
259+ g.add_vertex (l3);
260+
261+ g.remove_vertex (l1);
262+ g.remove_vertex (l2);
263+ g.remove_vertex (l3);
264+ BOOST_ASSERT (num_vertices (g) == 0 );
265+ }
266+
267+ // Remove vertex with incident edges
268+ {
269+ LabeledGraph g;
270+ g.add_vertex (l1);
271+ g.add_vertex (l2);
272+ add_edge_by_label (l1, l2, g);
273+ BOOST_ASSERT (num_edges (g) == 1 );
274+
275+ // BGL requires clearing edges before removing a vertex
276+ clear_vertex_by_label (1 ,g);
277+ g.remove_vertex (l1);
278+ BOOST_ASSERT (g.vertex (l1) == LabeledGraph::null_vertex ());
279+ BOOST_ASSERT (num_edges (g) == 0 );
280+ }
281+ }
282+
283+ template <typename Graph, typename LabeledGraph, typename Label>
284+ void test_remove_vertex_suite_ptr_variant (Label l1, Label l2, Label l3)
285+ {
286+ // remove + label cleanup
287+ {
288+ Graph graph;
289+ LabeledGraph g (&graph);
290+ g.add_vertex (l1);
291+ BOOST_ASSERT (num_vertices (g) == 1 );
292+ BOOST_ASSERT (g.vertex (l1) != LabeledGraph::null_vertex ());
293+
294+ g.remove_vertex (l1);
295+ BOOST_ASSERT (num_vertices (g) == 0 );
296+ BOOST_ASSERT (g.vertex (l1) == LabeledGraph::null_vertex ());
297+ }
298+
299+ // remove one + keep others
300+ {
301+ Graph graph;
302+ LabeledGraph g (&graph);
303+ g.add_vertex (l1);
304+ g.add_vertex (l2);
305+ g.add_vertex (l3);
306+ BOOST_ASSERT (num_vertices (g) == 3 );
307+
308+ g.remove_vertex (l2);
309+ BOOST_ASSERT (num_vertices (g) == 2 );
310+ BOOST_ASSERT (g.vertex (l2) == LabeledGraph::null_vertex ());
311+ BOOST_ASSERT (g.vertex (l1) != LabeledGraph::null_vertex ());
312+ BOOST_ASSERT (g.vertex (l3) != LabeledGraph::null_vertex ());
313+ }
314+
315+ // reuse label after removal
316+ {
317+ Graph graph;
318+ LabeledGraph g (&graph);
319+ g.add_vertex (l1);
320+ g.remove_vertex (l1);
321+ g.add_vertex (l1);
322+ BOOST_ASSERT (num_vertices (g) == 1 );
323+ BOOST_ASSERT (g.vertex (l1) != LabeledGraph::null_vertex ());
324+ }
325+
326+ // Remove all
327+ {
328+ Graph graph;
329+ LabeledGraph g (&graph);
330+ g.add_vertex (l1);
331+ g.add_vertex (l2);
332+ g.add_vertex (l3);
333+
334+ g.remove_vertex (l1);
335+ g.remove_vertex (l2);
336+ g.remove_vertex (l3);
337+ BOOST_ASSERT (num_vertices (g) == 0 );
338+ }
339+
340+ // Remove vertex with incident edges
341+ {
342+ Graph graph;
343+ LabeledGraph g (&graph);
344+ g.add_vertex (l1);
345+ g.add_vertex (l2);
346+ add_edge_by_label (l1, l2, g);
347+ BOOST_ASSERT (num_edges (g) == 1 );
348+
349+ // BGL requires clearing edges before removing a vertex
350+ clear_vertex_by_label (1 ,g);
351+ g.remove_vertex (l1);
352+ BOOST_ASSERT (g.vertex (l1) == LabeledGraph::null_vertex ());
353+ BOOST_ASSERT (num_edges (g) == 0 );
354+ }
355+ }
356+
357+ void test_remove_vertex_all_types ()
358+ {
359+ // defaultS
360+ test_remove_vertex_suite<
361+ labeled_graph<directed_graph<>, string>
362+ >(" a" , " b" , " c" );
363+
364+ test_remove_vertex_suite<
365+ labeled_graph<undirected_graph<>, string>
366+ >(" a" , " b" , " c" );
367+
368+ // mapS
369+ test_remove_vertex_suite<
370+ labeled_graph<directed_graph<>, string, mapS>
371+ >(" a" , " b" , " c" );
372+
373+ test_remove_vertex_suite<
374+ labeled_graph<undirected_graph<>, string, mapS>
375+ >(" a" , " b" , " c" );
376+
377+ // hash_mapS
378+ test_remove_vertex_suite<
379+ labeled_graph<directed_graph<>, string, hash_mapS>
380+ >(" a" , " b" , " c" );
381+
382+ test_remove_vertex_suite<
383+ labeled_graph<undirected_graph<>, string, hash_mapS>
384+ >(" a" , " b" , " c" );
385+
386+ // adjacency_list + vecS not supported (unstable removal would require remapping the map)
387+
388+ // adjacency_list + listS (stable removal)
389+ test_remove_vertex_suite<
390+ labeled_graph<adjacency_list<listS, listS, directedS>, string, mapS>
391+ >(" a" , " b" , " c" );
392+
393+ test_remove_vertex_suite<
394+ labeled_graph<adjacency_list<listS, listS, undirectedS>, string, mapS>
395+ >(" a" , " b" , " c" );
396+
397+ // unsigned labels (vecS)
398+ test_remove_vertex_suite<
399+ labeled_graph<adjacency_list<listS, listS, directedS>, unsigned , vecS>
400+ >(0u , 1u , 2u );
401+
402+ // Pointer specialization:
403+ // temporarily attach labels to it without copying.
404+
405+ test_remove_vertex_suite_ptr_variant<
406+ directed_graph<>,
407+ labeled_graph<directed_graph<>*, string>
408+ >(" a" , " b" , " c" );
409+
410+ test_remove_vertex_suite_ptr_variant<
411+ undirected_graph<>,
412+ labeled_graph<undirected_graph<>*, string>
413+ >(" a" , " b" , " c" );
414+
415+ test_remove_vertex_suite_ptr_variant<
416+ directed_graph<>,
417+ labeled_graph<directed_graph<>*, string, hash_mapS>
418+ >(" a" , " b" , " c" );
419+
420+ // unsigned labels
421+ test_remove_vertex_suite_ptr_variant<
422+ adjacency_list<listS, listS, directedS>,
423+ labeled_graph<adjacency_list<listS, listS, directedS>*, unsigned , vecS>
424+ >(0u , 1u , 2u );
425+ }
0 commit comments