p_tan's blog

勉強日記です。ツッコミ大歓迎

EventVisitorを複数指定する

  • std::make_pairまたはboost::make_listを使う
    • boost::make_listはネストされたstd::pairを返す
#include <iostream>
#include <utility>

#include <boost/graph/graph_utility.hpp>	// for boost::make_list
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>

using namespace boost;
using namespace std;

// テンプレート引数でevent_filterを指定できるEventVisitor
template <class Tag>
struct print_visitor {
	typedef Tag event_filter;
	print_visitor(std::string msg) : m_msg(msg) { }
	template <class T, class Graph>
	void operator()(T t, Graph& G) {
		std::cout << m_msg << ": " << t << std::endl;
	}
	std::string m_msg;
};

void event_visiter_sample()
{

	using namespace boost;

	typedef adjacency_list<> Graph;
	typedef std::pair<int,int> E;
	E edges[] = { E(0, 4), E(1, 0), E(1, 3),
		E(2, 1), E(2, 3), E(3, 1), E(3, 4),
		E(4, 0), E(4, 1) };  

	Graph G(edges, edges + sizeof(edges)/sizeof(E), 5);
	// boost::make_listで複数のEventVisitorを指定
	cout << endl << "BFS categorized directed graph" << endl;
	boost::breadth_first_search
		(G, vertex(0, G),
		 visitor(make_bfs_visitor(
			make_list(print_visitor<on_discover_vertex>("discover"),
				  print_visitor<on_discover_vertex>("discover2"),  // 同じイベントに複数のEventVisitor指定も出来る
				  print_visitor<on_tree_edge>("tree"),
				  print_visitor<on_non_tree_edge>("cycle")
		))));
}

出力

BFS categorized directed graph
discover: 0
discover2: 0
tree: (0,4)
discover: 4
discover2: 4
cycle: (4,0)
tree: (4,1)
discover: 1
discover2: 1
cycle: (1,0)
tree: (1,3)
discover: 3
discover2: 3
cycle: (3,1)
cycle: (3,4)