Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graph/labeled_graph: added remove_labeled_vertex #354

Merged
merged 3 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading