开发者

boost graph library: named_graph and remove_vertex

I'm experimenting with using the named_graph mixin, and I'm a little confused how remove_vertex() should work.

#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/graph/adjacency_list.hpp>

struct vertex_info {
  std::string name;          // uses vertex_from_name<vertex_info>
  vertex_info(const std::string &name_) : name(name_) { }   
};

ostream& operator<<(ostream & os, const vertex_info &v)
{
  os << v.name;
  return os;
}

namespace boost { namespace graph {

template<typename Type>
struct vertex_name_extractor
{
  typedef Type type;
  typedef const std::string& result_type;    
  result_type operator()(const Type& v) const
  {
    return v.name;
  }
};

template<>
struct internal_vertex_name<vertex_info>
{
  typedef vertex_name_extractor<vertex_info> type;
};

template<>
struct internal_vertex_constructor<vertex_info>
{
  typedef vertex_from_name<vertex_info> type;
};

} }

typedef adjacency_list< vecS, vecS, undirectedS, vertex_info, edge_info> graph_t;

namespace bg=boost::graph;
int main()
{
  using namespace std;
  graph_t g;

  int i;
  typedef graph_traits<graph_t>::vertex_descriptor vert;

  for(i=0;i < 10;++i)
  {
    开发者_开发百科string t_name("Vertex");
    vert V;
    t_name += lexical_cast<string>(i);
    V = add_vertex(t_name,g);
  }

  typedef graph_t::vertex_name_type name_t;
  name_t s_temp("Vertex2");

  optional<vert> V(
  find_vertex(s_temp,g));

  if( V ) {
    cout << "Found vertex:" << *V << '\n';
    //remove_vertex(*V,g);           // (1)
    //remove_vertex(vertex(*V,g),g); // (2)
    //remove_vertex(g[*V],g);        // (3)
    //remove_vertex(s_temp,g);       // (4)
  } else {
    cout << "Vertex not found\n";
  }


  graph_traits<graph_t>::vertex_iterator v_i, v_end;

  for(tie(v_i,v_end) = vertices(g); v_i != v_end; ++v_i)
  {
    cout << '\'' << g[*v_i] << '\'' << endl;;
  }
}

When I try using (3) or (4), I get an error for no matching function call to ‘remove_vertex(vertex_info&, graph_t&)’

adjacency_list.hpp:2211 candidates: remove_vertex(typename graph_t::vertex_descriptor, graph_t &)

But when I try using (1) or (2), I get an error for invalid conversion from ‘long unsigned int’ to ‘const char*’.

  error:   initializing argument 1 of ‘std::basic_string<...'
  boost/graph/named_graph.hpp:349
  template<BGL_NAMED_GRAPH_PARAMS>
  inline void BGL_NAMED_GRAPH::removing_vertex(Vertex vertex)
  {
      named_vertices.erase(vertex);  //line 349
  }


vertex_name extractor's result_type should have const and reference qualifiers removed. The function object should specify that it returns a const reference. This allows for proper meta-functions that rely on result_type without removing them. Overloaded functors can be specified easier.

template<typename Type>
struct vertex_name_extractor 
{ 
typedef Type type;
  typedef std::string result_type;                                                                  
  const result_type& operator()(const Type& v) const                                      
  {         
    return v.name;
  }         
}    ;  

The creating a bundled VertexProperty can be extended easier if we specify our own constructor.

template<typename VertexProperty>                                                         
struct vertex_info_constructor
{
  typedef VertexProperty return_type;                                                     
  typedef typename vertex_name_extractor<VertexProperty>::result_type argument_type;      
  return_type operator()(argument_type n)                                                 
  {                                                                                       
    VertexProperty v(n);
    return v;
  }                                                                                       
};

template<>                                                                                
struct internal_vertex_constructor<vertex_info>                                           
{
  typedef vertex_info_constructor<vertex_info> type;                                      
};

adjacency_list uses MI mixin, with its base class maybe_named_graph<>. Following the example for turning off named_graphs when the internal_vertex_name::type is void, I added a partial specialization of maybe_name_graph::type > as follows:

template<typename Graph, typename Vertex>
struct maybe_named_graph<Graph, Vertex, vertex_info, vertex_name_extractor<vertex_info> >
    : public named_graph<Graph, Vertex, vertex_info>
{
  typedef named_graph<Graph, Vertex, vertex_info> Base;
  //maybe_named_graph() { }

  typedef typename detail::extract_bundled_vertex<vertex_info>::type
    bundled_vertex_property_type;

  void added_vertex(Vertex v) { Base::added_vertex(v); }

  void removing_vertex(Vertex v) {
          const std::string &name = extract_name((Base::derived()[v]));
          Base::named_vertices.erase(name);
  }

  void clearing_graph() { Base::clearing_graph(); }

  optional<Vertex>
  vertex_by_property(const bundled_vertex_property_type& t)
  {
    return Base::vertex_by_property(t);
  }
};

Now adjacency_list's remove_vertex( VertexDescriptor, Graph ) calls my specialized removing_node that removes from the vertex_name from the named_graph by vertex descriptor.

note: vecS is used, so you still have to be careful with iterator invalidation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜