Skip to content

Commit

Permalink
Merge pull request #354 from DeuceBox/remove-labeled-vertex
Browse files Browse the repository at this point in the history
Fix labeled_graph::remove_vertex(Label const&) so that it removes the label too
  • Loading branch information
jeremy-murphy authored Nov 28, 2023
2 parents f315868 + e94408c commit fa2636d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
50 changes: 49 additions & 1 deletion include/boost/graph/labeled_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,54 @@ namespace graph_detail
}
//@}

/** @name Remove Labeled Vertex */
//@{
// Tag dispatch on random access containers (i.e., vectors)
template <typename Container, typename Label, typename Graph>
void remove_labeled_vertex(Container& c, Graph& g, Label const& l,
random_access_container_tag)
{
if (l < c.size())
{
boost::remove_vertex(c[l], g);
c.erase(c.begin() + l);
}
}

// Tag dispatch on multi associative containers (i.e. multimaps).
template <typename Container, typename Label, typename Graph>
void remove_labeled_vertex(Container& c, Graph& g, Label const& l,
multiple_associative_container_tag)
{
typename Container::iterator c_it = c.find(l);
if (c_it != c.end())
{
boost::remove_vertex(c_it->second, g);
c.erase(c_it);
}
}

// Tag dispatch on unique associative containers (i.e. maps).
template <typename Container, typename Label, typename Graph>
void remove_labeled_vertex(Container& c, Graph& g, Label const& l,
unique_associative_container_tag)
{
typename Container::iterator c_it = c.find(l);
if (c_it != c.end())
{
boost::remove_vertex(c_it->second, g);
c.erase(c_it);
}
}

// Dispatcher
template <typename Container, typename Label, typename Graph>
void remove_labeled_vertex(Container& c, Graph& g, Label const& l)
{
remove_labeled_vertex(c, g, l, container_category(c));
}
//@}

} // namespace detail

struct labeled_graph_class_tag
Expand Down Expand Up @@ -451,7 +499,7 @@ class labeled_graph : protected labeled_graph_types< Graph, Label, Selector >
/** Remove the vertex with the given label. */
void remove_vertex(Label const& l)
{
return boost::remove_vertex(vertex(l), _graph);
return graph_detail::remove_labeled_vertex(_map, _graph, l);
}

/** Return a descriptor for the given label. */
Expand Down
37 changes: 37 additions & 0 deletions test/labeled_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ using namespace boost;
void test_norm();
void test_temp();
void test_bacon();
void test_remove_labeled_vertex();
void test_multiple_associative_container();

int main()
{
test_norm();
test_temp();
test_bacon();
test_remove_labeled_vertex();
test_multiple_associative_container();
}

//////////////////////////////////////
Expand Down Expand Up @@ -147,3 +151,36 @@ void test_bacon()
add_edge_by_label(bacon, slater, murder, g);
}
}

void test_remove_labeled_vertex()
{
typedef labeled_graph< directed_graph<>, string > Graph;

Graph g;
g.add_vertex("foo");

auto v = g.vertex("foo");
BOOST_ASSERT(v != nullptr);

g.remove_vertex("foo");

auto v2 = g.vertex("foo");
BOOST_ASSERT(v2 == nullptr);
}

void test_multiple_associative_container()
{
typedef labeled_graph<adjacency_list<listS, multisetS, directedS>, string, multimapS> Graph;

Graph g;
g.add_vertex("test");
g.add_vertex("test");

BOOST_ASSERT(num_vertices(g) == 2);

g.remove_vertex("test");
BOOST_ASSERT(num_vertices(g) == 1);

g.remove_vertex("test");
BOOST_ASSERT(num_vertices(g) == 0);
}

0 comments on commit fa2636d

Please sign in to comment.