Boost logo

Boost Users :

Subject: [Boost-users] [BGL] Using pointers as indexes in graphs
From: Bruno Barberi Gnecco (brunobg_at_[hidden])
Date: 2010-09-01 10:22:01


Hi,

I want to store pointers in a graph. Can I somehow use their pointers as indexes?

Here's the code I'm trying to execute. It does not work correctly, however;
apparently graph add other nodes in the vector storage (to fill the holes?).
I suppose I could use listS instead of vecS, but then I can't get the
topological_sort code to work (I know I have to provide some form of indices, but I don't
understand how to do that). Any suggestions to solve this?

Thanks!

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

class Plugin {
public:
   // some data here
   int x;
   Plugin(int a) : x(a) {}
};

typedef std::list< std::pair<Plugin *, Plugin *> > EdgeList;
typedef boost::adjacency_list<boost::setS, boost::vecS, boost::directedS, Plugin *>
PipelineGraph;
typedef boost::graph_traits<PipelineGraph>::vertex_descriptor PipelineVertex;

void graph(EdgeList edges) {
   PipelineGraph graph;
        
   for (EdgeList::iterator it(edges.begin()); it != edges.end(); it++) {
     PipelineVertex a(boost::vertex(reinterpret_cast<unsigned long>
        (it->first), graph));
     PipelineVertex b(boost::vertex(reinterpret_cast<unsigned long>
        (it->second), graph));
     boost::add_edge(a, b, graph);
   }

   // sort by topology, so we run the plugins in the correct order
   std::vector<PipelineVertex> sorted;
   boost::topological_sort(graph, std::back_inserter(sorted));
   std::cout << sorted.size() << std::endl;

   // REVERSE topological order
   std::vector<PipelineVertex> vl(sorted.rbegin(), sorted.rend());
        
   // loop
   for (std::vector<PipelineVertex>::iterator it(vl.begin());
       it != vl.end(); it++) {
     Plugin *p = reinterpret_cast<Plugin *>(*it);
     std::cout << p->x << std::endl;
   }
}

int main() {
        EdgeList edges;
        Plugin *a = new Plugin(1);
        Plugin *b = new Plugin(2);
        Plugin *c = new Plugin(3);
        Plugin *d = new Plugin(4);

        edges.push_back(std::pair<Plugin*, Plugin*>(a, b));
        edges.push_back(std::pair<Plugin*, Plugin*>(b, c));
        edges.push_back(std::pair<Plugin*, Plugin*>(b, d));
        graph(edges);
        return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net