visitors.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //=======================================================================
  2. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  3. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. //
  10. // Revision History:
  11. // 01 April 2001: Modified to use new <boost/limits.hpp> header. (JMaddock)
  12. //
  13. #ifndef BOOST_GRAPH_GRAPH_SEARCH_VISITORS_HPP
  14. #define BOOST_GRAPH_GRAPH_SEARCH_VISITORS_HPP
  15. #include <iosfwd>
  16. #include <boost/config.hpp>
  17. #include <boost/type_traits/is_same.hpp>
  18. #include <boost/mpl/bool.hpp>
  19. #include <boost/property_map/property_map.hpp>
  20. #include <boost/graph/graph_traits.hpp>
  21. #include <boost/limits.hpp>
  22. namespace boost {
  23. // This is a bit more convenient than std::numeric_limits because
  24. // you don't have to explicitly provide type T.
  25. template <class T>
  26. inline T numeric_limits_max(T) { return (std::numeric_limits<T>::max)(); }
  27. //========================================================================
  28. // Event Tags
  29. namespace detail {
  30. // For partial specialization workaround
  31. enum event_visitor_enum
  32. { on_no_event_num,
  33. on_initialize_vertex_num, on_start_vertex_num,
  34. on_discover_vertex_num, on_finish_vertex_num, on_examine_vertex_num,
  35. on_examine_edge_num, on_tree_edge_num, on_non_tree_edge_num,
  36. on_gray_target_num, on_black_target_num,
  37. on_forward_or_cross_edge_num, on_back_edge_num, on_finish_edge_num,
  38. on_edge_relaxed_num, on_edge_not_relaxed_num,
  39. on_edge_minimized_num, on_edge_not_minimized_num
  40. };
  41. template<typename Event, typename Visitor>
  42. struct functor_to_visitor : Visitor
  43. {
  44. typedef Event event_filter;
  45. functor_to_visitor(const Visitor& visitor) : Visitor(visitor) {}
  46. };
  47. } // namespace detail
  48. struct on_no_event { enum { num = detail::on_no_event_num }; };
  49. struct on_initialize_vertex {
  50. enum { num = detail::on_initialize_vertex_num }; };
  51. struct on_start_vertex { enum { num = detail::on_start_vertex_num }; };
  52. struct on_discover_vertex { enum { num = detail::on_discover_vertex_num }; };
  53. struct on_examine_vertex { enum { num = detail::on_examine_vertex_num }; };
  54. struct on_finish_vertex { enum { num = detail::on_finish_vertex_num }; };
  55. struct on_examine_edge { enum { num = detail::on_examine_edge_num }; };
  56. struct on_tree_edge { enum { num = detail::on_tree_edge_num }; };
  57. struct on_non_tree_edge { enum { num = detail::on_non_tree_edge_num }; };
  58. struct on_gray_target { enum { num = detail::on_gray_target_num }; };
  59. struct on_black_target { enum { num = detail::on_black_target_num }; };
  60. struct on_forward_or_cross_edge {
  61. enum { num = detail::on_forward_or_cross_edge_num }; };
  62. struct on_back_edge { enum { num = detail::on_back_edge_num }; };
  63. struct on_finish_edge { enum { num = detail::on_finish_edge_num }; };
  64. struct on_edge_relaxed { enum { num = detail::on_edge_relaxed_num }; };
  65. struct on_edge_not_relaxed {
  66. enum { num = detail::on_edge_not_relaxed_num }; };
  67. struct on_edge_minimized { enum { num = detail::on_edge_minimized_num }; };
  68. struct on_edge_not_minimized {
  69. enum { num = detail::on_edge_not_minimized_num }; };
  70. //========================================================================
  71. // base_visitor and null_visitor
  72. // needed for MSVC workaround
  73. template <class Visitor>
  74. struct base_visitor {
  75. typedef on_no_event event_filter;
  76. template <class T, class Graph>
  77. void operator()(T, Graph&) { }
  78. };
  79. struct null_visitor : public base_visitor<null_visitor> {
  80. typedef on_no_event event_filter;
  81. template <class T, class Graph>
  82. void operator()(T, Graph&) { }
  83. };
  84. //========================================================================
  85. // The invoke_visitors() function
  86. namespace detail {
  87. template <class Visitor, class T, class Graph>
  88. inline void invoke_dispatch(Visitor& v, T x, Graph& g, mpl::true_) {
  89. v(x, g);
  90. }
  91. template <class Visitor, class T, class Graph>
  92. inline void invoke_dispatch(Visitor&, T, Graph&, mpl::false_)
  93. { }
  94. } // namespace detail
  95. template <class Visitor, class Rest, class T, class Graph, class Tag>
  96. inline void
  97. invoke_visitors(std::pair<Visitor, Rest>& vlist, T x, Graph& g, Tag tag) {
  98. typedef typename Visitor::event_filter Category;
  99. typedef typename is_same<Category, Tag>::type IsSameTag;
  100. detail::invoke_dispatch(vlist.first, x, g, IsSameTag());
  101. invoke_visitors(vlist.second, x, g, tag);
  102. }
  103. template <class Visitor, class T, class Graph, class Tag>
  104. inline void
  105. invoke_visitors(Visitor& v, T x, Graph& g, Tag) {
  106. typedef typename Visitor::event_filter Category;
  107. typedef typename is_same<Category, Tag>::type IsSameTag;
  108. detail::invoke_dispatch(v, x, g, IsSameTag());
  109. }
  110. //========================================================================
  111. // predecessor_recorder
  112. template <class PredecessorMap, class Tag>
  113. struct predecessor_recorder
  114. : public base_visitor<predecessor_recorder<PredecessorMap, Tag> >
  115. {
  116. typedef Tag event_filter;
  117. predecessor_recorder(PredecessorMap pa) : m_predecessor(pa) { }
  118. template <class Edge, class Graph>
  119. void operator()(Edge e, const Graph& g) {
  120. put(m_predecessor, target(e, g), source(e, g));
  121. }
  122. PredecessorMap m_predecessor;
  123. };
  124. template <class PredecessorMap, class Tag>
  125. predecessor_recorder<PredecessorMap, Tag>
  126. record_predecessors(PredecessorMap pa, Tag) {
  127. return predecessor_recorder<PredecessorMap, Tag> (pa);
  128. }
  129. //========================================================================
  130. // edge_predecessor_recorder
  131. template <class PredEdgeMap, class Tag>
  132. struct edge_predecessor_recorder
  133. : public base_visitor<edge_predecessor_recorder<PredEdgeMap, Tag> >
  134. {
  135. typedef Tag event_filter;
  136. edge_predecessor_recorder(PredEdgeMap pa) : m_predecessor(pa) { }
  137. template <class Edge, class Graph>
  138. void operator()(Edge e, const Graph& g) {
  139. put(m_predecessor, target(e, g), e);
  140. }
  141. PredEdgeMap m_predecessor;
  142. };
  143. template <class PredEdgeMap, class Tag>
  144. edge_predecessor_recorder<PredEdgeMap, Tag>
  145. record_edge_predecessors(PredEdgeMap pa, Tag) {
  146. return edge_predecessor_recorder<PredEdgeMap, Tag> (pa);
  147. }
  148. //========================================================================
  149. // distance_recorder
  150. template <class DistanceMap, class Tag>
  151. struct distance_recorder
  152. : public base_visitor<distance_recorder<DistanceMap, Tag> >
  153. {
  154. typedef Tag event_filter;
  155. distance_recorder(DistanceMap pa) : m_distance(pa) { }
  156. template <class Edge, class Graph>
  157. void operator()(Edge e, const Graph& g) {
  158. typename graph_traits<Graph>::vertex_descriptor
  159. u = source(e, g), v = target(e, g);
  160. put(m_distance, v, get(m_distance, u) + 1);
  161. }
  162. DistanceMap m_distance;
  163. };
  164. template <class DistanceMap, class Tag>
  165. distance_recorder<DistanceMap, Tag>
  166. record_distances(DistanceMap pa, Tag) {
  167. return distance_recorder<DistanceMap, Tag> (pa);
  168. }
  169. //========================================================================
  170. // time_stamper
  171. template <class TimeMap, class TimeT, class Tag>
  172. struct time_stamper
  173. : public base_visitor<time_stamper<TimeMap, TimeT, Tag> >
  174. {
  175. typedef Tag event_filter;
  176. time_stamper(TimeMap pa, TimeT& t) : m_time_pa(pa), m_time(t) { }
  177. template <class Vertex, class Graph>
  178. void operator()(Vertex u, const Graph&) {
  179. put(m_time_pa, u, ++m_time);
  180. }
  181. TimeMap m_time_pa;
  182. TimeT& m_time;
  183. };
  184. template <class TimeMap, class TimeT, class Tag>
  185. time_stamper<TimeMap, TimeT, Tag>
  186. stamp_times(TimeMap pa, TimeT& time_counter, Tag) {
  187. return time_stamper<TimeMap, TimeT, Tag>(pa, time_counter);
  188. }
  189. //========================================================================
  190. // property_writer
  191. template <class PA, class OutputIterator, class Tag>
  192. struct property_writer
  193. : public base_visitor<property_writer<PA, OutputIterator, Tag> >
  194. {
  195. typedef Tag event_filter;
  196. property_writer(PA pa, OutputIterator out) : m_pa(pa), m_out(out) { }
  197. template <class T, class Graph>
  198. void operator()(T x, Graph&) { *m_out++ = get(m_pa, x); }
  199. PA m_pa;
  200. OutputIterator m_out;
  201. };
  202. template <class PA, class OutputIterator, class Tag>
  203. property_writer<PA, OutputIterator, Tag>
  204. write_property(PA pa, OutputIterator out, Tag) {
  205. return property_writer<PA, OutputIterator, Tag>(pa, out);
  206. }
  207. //========================================================================
  208. // property_put
  209. /**
  210. * Functor which just sets a given value to a vertex or edge in a property map.
  211. */
  212. template <typename PropertyMap, typename EventTag>
  213. struct property_put
  214. {
  215. typedef EventTag event_filter;
  216. property_put (PropertyMap property_map,
  217. typename property_traits <PropertyMap>::value_type value) :
  218. property_map_ (property_map), value_ (value)
  219. {}
  220. template <typename VertexOrEdge, typename Graph>
  221. void operator() (VertexOrEdge v, const Graph&)
  222. {
  223. put (property_map_, v, value_);
  224. }
  225. private:
  226. PropertyMap property_map_;
  227. typename property_traits <PropertyMap>::value_type value_;
  228. };
  229. /**
  230. * Creates a property_put functor which just sets a given value to a vertex or edge.
  231. *
  232. * @param property_map Given writeable property map
  233. * @param value Fixed value of the map
  234. * @param tag Event Filter
  235. * @return The functor.
  236. */
  237. template <typename PropertyMap, typename EventTag>
  238. inline property_put <PropertyMap, EventTag>
  239. put_property (PropertyMap property_map,
  240. typename property_traits <PropertyMap>::value_type value,
  241. EventTag)
  242. {
  243. return property_put <PropertyMap, EventTag> (property_map, value);
  244. }
  245. #define BOOST_GRAPH_EVENT_STUB(Event,Kind) \
  246. typedef ::boost::Event Event##_type; \
  247. template<typename Visitor> \
  248. Kind##_visitor<std::pair<detail::functor_to_visitor<Event##_type, \
  249. Visitor>, Visitors> > \
  250. do_##Event(Visitor visitor) \
  251. { \
  252. typedef std::pair<detail::functor_to_visitor<Event##_type, Visitor>, \
  253. Visitors> visitor_list; \
  254. typedef Kind##_visitor<visitor_list> result_type; \
  255. return result_type(visitor_list(visitor, m_vis)); \
  256. }
  257. } /* namespace boost */
  258. #endif