filtered_graph_properties_dijkstra.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // (c) Copyright Juergen Hunold 2012
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/graph/adjacency_list.hpp>
  6. #include <boost/graph/dijkstra_shortest_paths.hpp>
  7. #include <boost/graph/filtered_graph.hpp>
  8. namespace boost {
  9. enum edge_info_t { edge_info = 114 };
  10. BOOST_INSTALL_PROPERTY( edge, info );
  11. }
  12. template< typename EdgeInfo,
  13. typename Directed >
  14. class Graph
  15. {
  16. public:
  17. typedef boost::property< boost::edge_info_t, EdgeInfo > tEdge_property;
  18. typedef boost::adjacency_list< boost::setS,
  19. boost::vecS,
  20. Directed,
  21. boost::no_property,
  22. tEdge_property > tGraph;
  23. typedef typename boost::graph_traits< tGraph >::vertex_descriptor tNode;
  24. typedef typename boost::graph_traits< tGraph >::edge_descriptor tEdge;
  25. protected:
  26. tGraph m_Graph;
  27. };
  28. class DataEdge;
  29. class UndirectedGraph
  30. : public Graph< DataEdge*,
  31. boost::undirectedS >
  32. {
  33. public:
  34. template< class Evaluator, class Filter >
  35. void dijkstra( Evaluator const&,
  36. Filter const& ) const;
  37. };
  38. template< typename Graph, typename Derived >
  39. struct Evaluator : public boost::put_get_helper< int, Derived >
  40. {
  41. typedef int value_type;
  42. typedef typename Graph::tEdge key_type;
  43. typedef int reference;
  44. typedef boost::readable_property_map_tag category;
  45. explicit Evaluator( Graph const* pGraph );
  46. };
  47. template< typename Graph >
  48. struct LengthEvaluator : public Evaluator< Graph, LengthEvaluator<Graph> >
  49. {
  50. explicit LengthEvaluator( Graph const* pGraph );
  51. typedef typename Evaluator<Graph, LengthEvaluator<Graph> >::reference reference;
  52. typedef typename Evaluator<Graph, LengthEvaluator<Graph> >::key_type key_type;
  53. virtual reference operator[] ( key_type const& edge ) const;
  54. };
  55. template< class Graph >
  56. struct EdgeFilter
  57. {
  58. typedef typename Graph::tEdge key_type;
  59. EdgeFilter();
  60. explicit EdgeFilter( Graph const*);
  61. bool operator()( key_type const& ) const;
  62. private:
  63. const Graph* m_pGraph;
  64. };
  65. template< class Evaluator, class Filter >
  66. void
  67. UndirectedGraph::dijkstra( Evaluator const& rEvaluator,
  68. Filter const& rFilter ) const
  69. {
  70. tNode nodeSource = vertex(0, m_Graph);
  71. std::vector< tNode > predecessors( num_vertices(m_Graph) );
  72. boost::filtered_graph< tGraph, Filter > filteredGraph( m_Graph, rFilter );
  73. boost::dijkstra_shortest_paths( filteredGraph,
  74. nodeSource,
  75. boost::predecessor_map( &predecessors[0] )
  76. .weight_map( rEvaluator ) );
  77. }
  78. // explicit instantiation
  79. template void UndirectedGraph::dijkstra( LengthEvaluator<UndirectedGraph> const&,
  80. EdgeFilter<UndirectedGraph> const& ) const;
  81. int main(int, char**) {return 0;} // Tests above will fail to compile if anything is broken