2009-10-02 78 views
5

如何修改訪問者內部頂點的綁定屬性?從訪問者修改綁定屬性

我想用子腳本的曲線圖的簡單的方法,但傳遞到訪問者圖形參數是常量,所以編譯器不允許改變。

我可以在訪問者中存儲對圖形的引用,但這看起來很奇怪。

/** 

    A visitor which identifies vertices as leafs or trees 

*/ 
class bfs_vis_leaf_finder:public default_bfs_visitor { 

public: 
    /** 

    Constructor 

    @param[in] total reference to int variable to store total number of leaves 
    @param[in] g reference to graph (used to modify bundled properties) 

    */ 
    bfs_vis_leaf_finder(int& total, graph_t& g) : 
     myTotal(total), myGraph(g) 
     { 
      myTotal = 0; 
     } 

    /** 

    Called when the search finds a new vertex 

    If the vertex has no children, it is a leaf and the total leaf count is incremented 

    */ 
    template <typename Vertex, typename Graph> 
    void discover_vertex(Vertex u, Graph& g) 
    { 
     if(out_edges(u, g).first == out_edges(u, g).second) { 
      myTotal++; 
      //g[u].myLevel = s3d::cV::leaf; 
      myGraph[u].myLevel = s3d::cV::leaf; 
     } else { 
      //g[u].myLevel = s3d::cV::tree; 
      myGraph[u].myLevel = s3d::cV::tree; 
     } 
    } 

    int& myTotal; 
    graph_t& myGraph; 
}; 

回答

4

您的解決方案是正確的。

從訪客分離圖形類型,你可以只通過有趣的屬性映射到訪問者的構造和使用訪問其boost::get(property, u) = s3d::cV::leaf;元素。這樣,您就可以在任何類型兼容的頂點屬性傳遞給訪問者(訪問者將更加普遍和不理智的在圖表類型名稱更改)。

類型的屬性映射將是一個模板類型,名稱爲訪問者類,並且將是這樣的:

typedef property_map<graph_t, s3d_cv3_leaf_t your_vertex_info::*>::type your_property_map; 

約捆綁性質的完整論述見here

HTH

0

我只是在學習這個東西,但我認爲你必須在訪問者中存儲對圖形的引用是正確的。我不確定是否出於這個原因,但可能是因爲他們不想提供所有函數/需要派生的兩個版本來提供每個函數的兩個版本。特別是當圖表解決方案中的通行證可用時。

即使感覺不可思議,我認爲傳遞給圖的引用可能是「正確的方式」。

+0

我開始認爲eaoring的參考必須的方式。我認爲我忽略了一些簡單的東西,但是由於目前還沒有人提出任何其他建議...... – ravenspoint 2009-10-13 21:44:06