comparison DEPENDENCIES/generic/include/boost/graph/visitors.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
comparison
equal deleted inserted replaced
15:663ca0da4350 16:2665513ce2d3
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
16 #include <iosfwd>
17 #include <boost/config.hpp>
18 #include <boost/type_traits/is_same.hpp>
19 #include <boost/mpl/bool.hpp>
20 #include <boost/property_map/property_map.hpp>
21 #include <boost/graph/graph_traits.hpp>
22 #include <boost/limits.hpp>
23
24 #if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
25 // Stay out of the way of the concept checking class
26 # define Graph Graph_
27 #endif
28
29 namespace boost {
30
31 // This is a bit more convenient than std::numeric_limits because
32 // you don't have to explicitly provide type T.
33 template <class T>
34 inline T numeric_limits_max(T) { return (std::numeric_limits<T>::max)(); }
35
36 //========================================================================
37 // Event Tags
38
39 namespace detail {
40 // For partial specialization workaround
41 enum event_visitor_enum
42 { on_no_event_num,
43 on_initialize_vertex_num, on_start_vertex_num,
44 on_discover_vertex_num, on_finish_vertex_num, on_examine_vertex_num,
45 on_examine_edge_num, on_tree_edge_num, on_non_tree_edge_num,
46 on_gray_target_num, on_black_target_num,
47 on_forward_or_cross_edge_num, on_back_edge_num, on_finish_edge_num,
48 on_edge_relaxed_num, on_edge_not_relaxed_num,
49 on_edge_minimized_num, on_edge_not_minimized_num
50 };
51
52 template<typename Event, typename Visitor>
53 struct functor_to_visitor : Visitor
54 {
55 typedef Event event_filter;
56 functor_to_visitor(const Visitor& visitor) : Visitor(visitor) {}
57 };
58
59 } // namespace detail
60
61 struct on_no_event { enum { num = detail::on_no_event_num }; };
62
63 struct on_initialize_vertex {
64 enum { num = detail::on_initialize_vertex_num }; };
65 struct on_start_vertex { enum { num = detail::on_start_vertex_num }; };
66 struct on_discover_vertex { enum { num = detail::on_discover_vertex_num }; };
67 struct on_examine_vertex { enum { num = detail::on_examine_vertex_num }; };
68 struct on_finish_vertex { enum { num = detail::on_finish_vertex_num }; };
69
70 struct on_examine_edge { enum { num = detail::on_examine_edge_num }; };
71 struct on_tree_edge { enum { num = detail::on_tree_edge_num }; };
72 struct on_non_tree_edge { enum { num = detail::on_non_tree_edge_num }; };
73 struct on_gray_target { enum { num = detail::on_gray_target_num }; };
74 struct on_black_target { enum { num = detail::on_black_target_num }; };
75 struct on_forward_or_cross_edge {
76 enum { num = detail::on_forward_or_cross_edge_num }; };
77 struct on_back_edge { enum { num = detail::on_back_edge_num }; };
78 struct on_finish_edge { enum { num = detail::on_finish_edge_num }; };
79
80 struct on_edge_relaxed { enum { num = detail::on_edge_relaxed_num }; };
81 struct on_edge_not_relaxed {
82 enum { num = detail::on_edge_not_relaxed_num }; };
83 struct on_edge_minimized { enum { num = detail::on_edge_minimized_num }; };
84 struct on_edge_not_minimized {
85 enum { num = detail::on_edge_not_minimized_num }; };
86
87 //========================================================================
88 // base_visitor and null_visitor
89
90 // needed for MSVC workaround
91 template <class Visitor>
92 struct base_visitor {
93 typedef on_no_event event_filter;
94 template <class T, class Graph>
95 void operator()(T, Graph&) { }
96 };
97
98 struct null_visitor : public base_visitor<null_visitor> {
99 typedef on_no_event event_filter;
100 template <class T, class Graph>
101 void operator()(T, Graph&) { }
102 };
103
104 //========================================================================
105 // The invoke_visitors() function
106
107 namespace detail {
108 template <class Visitor, class T, class Graph>
109 inline void invoke_dispatch(Visitor& v, T x, Graph& g, mpl::true_) {
110 v(x, g);
111 }
112
113 template <class Visitor, class T, class Graph>
114 inline void invoke_dispatch(Visitor&, T, Graph&, mpl::false_)
115 { }
116 } // namespace detail
117
118 template <class Visitor, class Rest, class T, class Graph, class Tag>
119 inline void
120 invoke_visitors(std::pair<Visitor, Rest>& vlist, T x, Graph& g, Tag tag) {
121 typedef typename Visitor::event_filter Category;
122 typedef typename is_same<Category, Tag>::type IsSameTag;
123 detail::invoke_dispatch(vlist.first, x, g, IsSameTag());
124 invoke_visitors(vlist.second, x, g, tag);
125 }
126 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
127 template <class Visitor, class T, class Graph, class Tag>
128 inline void
129 invoke_visitors(base_visitor<Visitor>& vis, T x, Graph& g, Tag) {
130 typedef typename Visitor::event_filter Category;
131 typedef typename is_same<Category, Tag>::type IsSameTag;
132 Visitor& v = static_cast<Visitor&>(vis);
133 detail::invoke_dispatch(v, x, g, IsSameTag());
134 }
135 #else
136 template <class Visitor, class T, class Graph, class Tag>
137 inline void
138 invoke_visitors(Visitor& v, T x, Graph& g, Tag) {
139 typedef typename Visitor::event_filter Category;
140 typedef typename is_same<Category, Tag>::type IsSameTag;
141 detail::invoke_dispatch(v, x, g, IsSameTag());
142 }
143 #endif
144
145 //========================================================================
146 // predecessor_recorder
147
148 template <class PredecessorMap, class Tag>
149 struct predecessor_recorder
150 : public base_visitor<predecessor_recorder<PredecessorMap, Tag> >
151 {
152 typedef Tag event_filter;
153 predecessor_recorder(PredecessorMap pa) : m_predecessor(pa) { }
154 template <class Edge, class Graph>
155 void operator()(Edge e, const Graph& g) {
156 put(m_predecessor, target(e, g), source(e, g));
157 }
158 PredecessorMap m_predecessor;
159 };
160 template <class PredecessorMap, class Tag>
161 predecessor_recorder<PredecessorMap, Tag>
162 record_predecessors(PredecessorMap pa, Tag) {
163 return predecessor_recorder<PredecessorMap, Tag> (pa);
164 }
165
166 //========================================================================
167 // edge_predecessor_recorder
168
169 template <class PredEdgeMap, class Tag>
170 struct edge_predecessor_recorder
171 : public base_visitor<edge_predecessor_recorder<PredEdgeMap, Tag> >
172 {
173 typedef Tag event_filter;
174 edge_predecessor_recorder(PredEdgeMap pa) : m_predecessor(pa) { }
175 template <class Edge, class Graph>
176 void operator()(Edge e, const Graph& g) {
177 put(m_predecessor, target(e, g), e);
178 }
179 PredEdgeMap m_predecessor;
180 };
181 template <class PredEdgeMap, class Tag>
182 edge_predecessor_recorder<PredEdgeMap, Tag>
183 record_edge_predecessors(PredEdgeMap pa, Tag) {
184 return edge_predecessor_recorder<PredEdgeMap, Tag> (pa);
185 }
186
187 //========================================================================
188 // distance_recorder
189
190 template <class DistanceMap, class Tag>
191 struct distance_recorder
192 : public base_visitor<distance_recorder<DistanceMap, Tag> >
193 {
194 typedef Tag event_filter;
195 distance_recorder(DistanceMap pa) : m_distance(pa) { }
196 template <class Edge, class Graph>
197 void operator()(Edge e, const Graph& g) {
198 typename graph_traits<Graph>::vertex_descriptor
199 u = source(e, g), v = target(e, g);
200 put(m_distance, v, get(m_distance, u) + 1);
201 }
202 DistanceMap m_distance;
203 };
204 template <class DistanceMap, class Tag>
205 distance_recorder<DistanceMap, Tag>
206 record_distances(DistanceMap pa, Tag) {
207 return distance_recorder<DistanceMap, Tag> (pa);
208 }
209
210 //========================================================================
211 // time_stamper
212
213
214 template <class TimeMap, class TimeT, class Tag>
215 struct time_stamper
216 : public base_visitor<time_stamper<TimeMap, TimeT, Tag> >
217 {
218 typedef Tag event_filter;
219 time_stamper(TimeMap pa, TimeT& t) : m_time_pa(pa), m_time(t) { }
220 template <class Vertex, class Graph>
221 void operator()(Vertex u, const Graph&) {
222 put(m_time_pa, u, ++m_time);
223 }
224 TimeMap m_time_pa;
225 TimeT& m_time;
226 };
227 template <class TimeMap, class TimeT, class Tag>
228 time_stamper<TimeMap, TimeT, Tag>
229 stamp_times(TimeMap pa, TimeT& time_counter, Tag) {
230 return time_stamper<TimeMap, TimeT, Tag>(pa, time_counter);
231 }
232
233 //========================================================================
234 // property_writer
235
236 template <class PA, class OutputIterator, class Tag>
237 struct property_writer
238 : public base_visitor<property_writer<PA, OutputIterator, Tag> >
239 {
240 typedef Tag event_filter;
241
242 property_writer(PA pa, OutputIterator out) : m_pa(pa), m_out(out) { }
243
244 template <class T, class Graph>
245 void operator()(T x, Graph&) { *m_out++ = get(m_pa, x); }
246 PA m_pa;
247 OutputIterator m_out;
248 };
249 template <class PA, class OutputIterator, class Tag>
250 property_writer<PA, OutputIterator, Tag>
251 write_property(PA pa, OutputIterator out, Tag) {
252 return property_writer<PA, OutputIterator, Tag>(pa, out);
253 }
254
255 //========================================================================
256 // property_put
257
258 /**
259 * Functor which just sets a given value to a vertex or edge in a property map.
260 */
261
262 template <typename PropertyMap, typename EventTag>
263 struct property_put
264 {
265 typedef EventTag event_filter;
266
267 property_put (PropertyMap property_map,
268 typename property_traits <PropertyMap>::value_type value) :
269 property_map_ (property_map), value_ (value)
270 {}
271
272 template <typename VertexOrEdge, typename Graph>
273 void operator() (VertexOrEdge v, const Graph&)
274 {
275 put (property_map_, v, value_);
276 }
277
278 private:
279 PropertyMap property_map_;
280 typename property_traits <PropertyMap>::value_type value_;
281 };
282
283 /**
284 * Creates a property_put functor which just sets a given value to a vertex or edge.
285 *
286 * @param property_map Given writeable property map
287 * @param value Fixed value of the map
288 * @param tag Event Filter
289 * @return The functor.
290 */
291
292 template <typename PropertyMap, typename EventTag>
293 inline property_put <PropertyMap, EventTag>
294 put_property (PropertyMap property_map,
295 typename property_traits <PropertyMap>::value_type value,
296 EventTag)
297 {
298 return property_put <PropertyMap, EventTag> (property_map, value);
299 }
300
301 #define BOOST_GRAPH_EVENT_STUB(Event,Kind) \
302 typedef ::boost::Event Event##_type; \
303 template<typename Visitor> \
304 Kind##_visitor<std::pair<detail::functor_to_visitor<Event##_type, \
305 Visitor>, Visitors> > \
306 do_##Event(Visitor visitor) \
307 { \
308 typedef std::pair<detail::functor_to_visitor<Event##_type, Visitor>, \
309 Visitors> visitor_list; \
310 typedef Kind##_visitor<visitor_list> result_type; \
311 return result_type(visitor_list(visitor, m_vis)); \
312 }
313
314 } /* namespace boost */
315
316 #if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
317 // Stay out of the way of the concept checking class
318 # undef Graph
319 #endif
320
321 #endif