簡體   English   中英

如何將一組C ++動態分配的對象表示為BGL(提升圖庫)圖以獲得其依賴圖?

[英]How to represent a group of C++ dynamically allocated objects as a BGL (Boost Graph Library) graph in order to obtain their dependency graph?

有一組C ++動態分配的對象為:

class MyObject;
MyObject* a = ....;
MyObject* b = ....;
MyObject* c = ....;

具有已知依賴性:

  • b取決於a
  • c取決於b

為了獲得依賴圖,如何將這些對象盡可能簡單地表示為BGL

結果,依賴圖應該是一個指針列表,如: abc (或相反的順序)。

我知道可以使用boost :: topological_sort獲得依賴圖。 直到現在,我還沒有找到處理對象指針的BGL圖示例。 相反,我發現了許多基於整數的示例

整數是圖形的頂點和邊緣的索引。 因此,在您的示例中,第一個頂點為a,第二個頂點為b,而第一條邊連接了頂點1和2(a和b)

每個頂點都有屬性。 因此,在您的示例中,第一個頂點命名為a,並且具有指向a的指針。

圖算法使用整數索引來操作圖,並且您的代碼可以使用索引來返回您感興趣的屬性,例如名稱和指針。

這是我編寫您發布的示例的代碼:

/**

Bundled properties for graph vertices

Simply contains a pointer to the associated MyObject

*/

class cVertex
{
public:
    MyObject * pObject;
};

class cEdge
{

};

class cGraphProps
{

};

....

// The BGL graph
typedef boost::adjacency_list <
    boost::vecS, boost::vecS, boost::directedS,
    cVertex, cEdge, cGraphProps  >
    graph_t;
typedef boost::graph_traits<graph_t>::vertex_descriptor vertex_t;

graph_t myGraph;

// Create objects and associate them with graph vertices
MyObject * a = new MyObject( 'a');
vertex_t va = boost::add_vertex( myGraph );
myGraph[ va ].pObject = a;
MyObject * b = new MyObject( 'b');
vertex_t vb = boost::add_vertex( myGraph );
myGraph[ vb ].pObject = b;
MyObject * c = new MyObject( 'c');
vertex_t vc = boost::add_vertex( myGraph );
myGraph[ vc ].pObject = c;

// specify the 'dependencies' between the myObjects
boost::add_edge( vb, va, myGraph );
boost::add_edge( vc, vb, myGraph );

// sort the vertices into order according to dependencies
std::deque<int> topo_order;
boost::topological_sort( myGraph, std::front_inserter(topo_order));

// Print the results.
for(std::deque<int>::const_iterator i = topo_order.begin();
    i != topo_order.end();
    ++i)
{
    std::cout << myGraph[*i].pObject->x << std::endl;
}

我編寫了此實現,但如果可能的話,我希望使用一個更簡單的實現:

#include <iostream>
#include <vector>
#include <unordered_map>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/topological_sort.hpp>

class MyObject
{
public:
    char x;
    MyObject( const char c): x( c) {}
};

int main()
{
    auto a = new MyObject( 'a');
    auto b = new MyObject( 'b');
    auto c = new MyObject( 'c');

    typedef std::vector<const MyObject*> Objects;
    Objects objects;
    objects.push_back( c); // c = 0
    objects.push_back( a); // a = 1
    objects.push_back( b); // b = 2

    typedef std::vector<size_t> Indexes;
    Indexes indexes;

    {
        typedef std::unordered_map<Objects::value_type, size_t> PtrIntMap;
        PtrIntMap ptrIntMap;
        std::for_each( objects.begin(), objects.end(), [&ptrIntMap]( Objects::reference item) { ptrIntMap.insert( std::make_pair( item, ptrIntMap.size())); });

        using namespace boost;
        adjacency_list<> g( objects.size());
        add_edge( ptrIntMap.at( b), ptrIntMap.at( a), g);
        add_edge( ptrIntMap.at( c), ptrIntMap.at( b), g);

        topological_sort( g, std::back_inserter( indexes)); 
    }

    Objects dependencies( indexes.size());
    std::transform( indexes.begin(), indexes.end(), dependencies.begin(), [&objects]( Indexes::value_type index) { return objects[ index]; });
    std::for_each( dependencies.begin(), dependencies.end(), []( const Objects::value_type item) { std::cout << item->x << ' '; });
}

暫無
暫無

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

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