Subscribed unsubscribe Subscribe Subscribe

Boost.Graph の graphviz リーダだけを使ってみる

もう単純に boost::adjacent_list とかその辺に興味がない場合どうすればいいかって話です。適当な MutableGraph コンセプトを実装するクラスを用意してやればまあ一応動くみたいです。

#include <map>
#include <string>
#include <iostream>
#include <boost/property_map/property_map.hpp>
#include <boost/property_map/dynamic_property_map.hpp>
#include <boost/graph/graph_mutability_traits.hpp>
#include <boost/graph/graph_selectors.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/graphviz.hpp>

class visitor
{
public:
    typedef int vertex_descriptor;
    typedef int edge_descriptor;
    typedef boost::directed_tag            directed_category;
    typedef boost::allow_parallel_edge_tag edge_parallel_category;
    typedef boost::vertex_list_graph_tag   traversal_category;

    typedef void adjacency_iterator;
    typedef void out_edge_iterator;
    typedef void in_edge_iterator;
    typedef void vertex_iterator;
    typedef void edge_iterator;
    typedef void vertices_size_type;
    typedef void edges_size_type;
    typedef void degree_size_type;


    std::pair<edge_descriptor, bool> add_edge(vertex_descriptor const& u,
                                              vertex_descriptor const& v)
    {
        std::cout << "add_edge(" << u << "," << v << ") = " << next_edge_desc_ << std::endl;
        return std::make_pair(next_edge_desc_++, true);
    }

    vertex_descriptor add_vertex()
    {
        std::cout << "add_vertex() = " << next_vertex_desc_ << std::endl;
        return next_vertex_desc_++;
    }

public:
    visitor(): next_edge_desc_(0), next_vertex_desc_(0) {}
private:
    int next_edge_desc_;
    int next_vertex_desc_;
};

std::pair<visitor::edge_descriptor, bool>
add_edge(visitor::vertex_descriptor const& u,
         visitor::vertex_descriptor const& v,
         visitor& g)
{
    return g.add_edge(u, v);
}

visitor::vertex_descriptor
add_vertex(visitor& g)
{
    return g.add_vertex();
}

int main(int, char**)
{
    visitor v;
    boost::dynamic_properties props;
    std::map<int, std::string> desc_to_node_id;
    boost::associative_property_map<std::map<int, std::string> > node_id_map(desc_to_node_id);
    props.property("node_id", node_id_map);
    boost::read_graphviz(std::cin, v, props);
    return 0;
}

Boost.Graph ってなんかアダプタ関数づかいが過剰。traits でもうちょっと綺麗にまとめられるはずなのに…これはあんまり手本にしちゃだめな気がする。