簡體   English   中英

增強圖形庫:named_graph和remove_vertex

[英]boost graph library: named_graph and remove_vertex

我正在嘗試使用named_graph mixin,但是我有點困惑remove_vertex()應該如何工作。

#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;;
  }
}

當我嘗試使用(3)或(4)時,出現錯誤,因為沒有匹配的函數調用'remove_vertex(vertex_info&,graph_t&)'

adjacency_list.hpp:2211候選:remove_vertex(類型名graph_t :: vertex_descriptor,graph_t&)

但是,當我嘗試使用(1)或(2)時,由於從'long unsigned int'到'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提取程序的result_type應該刪除const和引用限定符。 函數對象應指定它返回一個const引用。 這允許依賴於result_type的適當元函數而無需刪除它們。 可以更容易地指定重載的函子。

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;
  }         
}    ;  

如果我們指定自己的構造函數,則可以輕松擴展創建捆綁的VertexProperty的過程。

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使用MI mixin,其基類可能是_named_graph <>。 遵循在internal_vertex_name :: type為void時關閉named_graphs的示例,我添加了maybe_name_graph :: type>的部分特化,如下所示:

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);
  }
};

現在,鄰接表的remove_vertex(VertexDescriptor,Graph)調用我的專門的remove_node,該節點通過頂點描述符從named_graph中的頂點名稱中刪除。

注意:使用了vecS,因此您仍然必須小心迭代器無效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM