Chris@16
|
1 //=======================================================================
|
Chris@16
|
2 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
|
Chris@16
|
3 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
|
Chris@16
|
4 //
|
Chris@16
|
5 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
6 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
7 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 //=======================================================================
|
Chris@16
|
9
|
Chris@16
|
10 #ifndef BOOST_GRAPH_TRAITS_HPP
|
Chris@16
|
11 #define BOOST_GRAPH_TRAITS_HPP
|
Chris@16
|
12
|
Chris@16
|
13 #include <boost/config.hpp>
|
Chris@16
|
14 #include <iterator>
|
Chris@16
|
15 #include <utility> /* Primarily for std::pair */
|
Chris@16
|
16 #include <boost/tuple/tuple.hpp>
|
Chris@16
|
17 #include <boost/mpl/if.hpp>
|
Chris@16
|
18 #include <boost/mpl/eval_if.hpp>
|
Chris@16
|
19 #include <boost/mpl/bool.hpp>
|
Chris@16
|
20 #include <boost/mpl/not.hpp>
|
Chris@16
|
21 #include <boost/mpl/has_xxx.hpp>
|
Chris@16
|
22 #include <boost/mpl/void.hpp>
|
Chris@16
|
23 #include <boost/mpl/identity.hpp>
|
Chris@16
|
24 #include <boost/type_traits/is_same.hpp>
|
Chris@16
|
25 #include <boost/iterator/iterator_categories.hpp>
|
Chris@16
|
26 #include <boost/iterator/iterator_adaptor.hpp>
|
Chris@16
|
27 #include <boost/pending/property.hpp>
|
Chris@16
|
28 #include <boost/detail/workaround.hpp>
|
Chris@16
|
29
|
Chris@16
|
30 namespace boost {
|
Chris@16
|
31
|
Chris@16
|
32 namespace detail {
|
Chris@16
|
33 #define BOOST_GRAPH_MEMBER_OR_VOID(name) \
|
Chris@16
|
34 BOOST_MPL_HAS_XXX_TRAIT_DEF(name) \
|
Chris@16
|
35 template <typename T> struct BOOST_JOIN(get_member_, name) {typedef typename T::name type;}; \
|
Chris@16
|
36 template <typename T> struct BOOST_JOIN(get_opt_member_, name): \
|
Chris@16
|
37 boost::mpl::eval_if_c< \
|
Chris@16
|
38 BOOST_JOIN(has_, name)<T>::value, \
|
Chris@16
|
39 BOOST_JOIN(get_member_, name)<T>, \
|
Chris@16
|
40 boost::mpl::identity<void> > \
|
Chris@16
|
41 {};
|
Chris@16
|
42 BOOST_GRAPH_MEMBER_OR_VOID(adjacency_iterator)
|
Chris@16
|
43 BOOST_GRAPH_MEMBER_OR_VOID(out_edge_iterator)
|
Chris@16
|
44 BOOST_GRAPH_MEMBER_OR_VOID(in_edge_iterator)
|
Chris@16
|
45 BOOST_GRAPH_MEMBER_OR_VOID(vertex_iterator)
|
Chris@16
|
46 BOOST_GRAPH_MEMBER_OR_VOID(edge_iterator)
|
Chris@16
|
47 BOOST_GRAPH_MEMBER_OR_VOID(vertices_size_type)
|
Chris@16
|
48 BOOST_GRAPH_MEMBER_OR_VOID(edges_size_type)
|
Chris@16
|
49 BOOST_GRAPH_MEMBER_OR_VOID(degree_size_type)
|
Chris@16
|
50 }
|
Chris@16
|
51
|
Chris@16
|
52 template <typename G>
|
Chris@16
|
53 struct graph_traits {
|
Chris@16
|
54 #define BOOST_GRAPH_PULL_OPT_MEMBER(name) \
|
Chris@16
|
55 typedef typename detail::BOOST_JOIN(get_opt_member_, name)<G>::type name;
|
Chris@16
|
56
|
Chris@16
|
57 typedef typename G::vertex_descriptor vertex_descriptor;
|
Chris@16
|
58 typedef typename G::edge_descriptor edge_descriptor;
|
Chris@16
|
59 BOOST_GRAPH_PULL_OPT_MEMBER(adjacency_iterator)
|
Chris@16
|
60 BOOST_GRAPH_PULL_OPT_MEMBER(out_edge_iterator)
|
Chris@16
|
61 BOOST_GRAPH_PULL_OPT_MEMBER(in_edge_iterator)
|
Chris@16
|
62 BOOST_GRAPH_PULL_OPT_MEMBER(vertex_iterator)
|
Chris@16
|
63 BOOST_GRAPH_PULL_OPT_MEMBER(edge_iterator)
|
Chris@16
|
64
|
Chris@16
|
65 typedef typename G::directed_category directed_category;
|
Chris@16
|
66 typedef typename G::edge_parallel_category edge_parallel_category;
|
Chris@16
|
67 typedef typename G::traversal_category traversal_category;
|
Chris@16
|
68
|
Chris@16
|
69 BOOST_GRAPH_PULL_OPT_MEMBER(vertices_size_type)
|
Chris@16
|
70 BOOST_GRAPH_PULL_OPT_MEMBER(edges_size_type)
|
Chris@16
|
71 BOOST_GRAPH_PULL_OPT_MEMBER(degree_size_type)
|
Chris@16
|
72 #undef BOOST_GRAPH_PULL_OPT_MEMBER
|
Chris@16
|
73
|
Chris@16
|
74 static inline vertex_descriptor null_vertex();
|
Chris@16
|
75 };
|
Chris@16
|
76
|
Chris@16
|
77 template <typename G>
|
Chris@16
|
78 inline typename graph_traits<G>::vertex_descriptor
|
Chris@16
|
79 graph_traits<G>::null_vertex()
|
Chris@16
|
80 { return G::null_vertex(); }
|
Chris@16
|
81
|
Chris@16
|
82 // directed_category tags
|
Chris@16
|
83 struct directed_tag { };
|
Chris@16
|
84 struct undirected_tag { };
|
Chris@16
|
85 struct bidirectional_tag : public directed_tag { };
|
Chris@16
|
86
|
Chris@16
|
87 namespace detail {
|
Chris@16
|
88 inline bool is_directed(directed_tag) { return true; }
|
Chris@16
|
89 inline bool is_directed(undirected_tag) { return false; }
|
Chris@16
|
90 }
|
Chris@16
|
91
|
Chris@16
|
92 /** Return true if the given graph is directed. */
|
Chris@16
|
93 template <typename Graph>
|
Chris@16
|
94 bool is_directed(const Graph&) {
|
Chris@16
|
95 typedef typename graph_traits<Graph>::directed_category Cat;
|
Chris@16
|
96 return detail::is_directed(Cat());
|
Chris@16
|
97 }
|
Chris@16
|
98
|
Chris@16
|
99 /** Return true if the given graph is undirected. */
|
Chris@16
|
100 template <typename Graph>
|
Chris@16
|
101 bool is_undirected(const Graph& g) {
|
Chris@16
|
102 return !is_directed(g);
|
Chris@16
|
103 }
|
Chris@16
|
104
|
Chris@16
|
105 /** @name Directed/Undirected Graph Traits */
|
Chris@16
|
106 //@{
|
Chris@16
|
107 namespace graph_detail {
|
Chris@16
|
108 template <typename Tag>
|
Chris@16
|
109 struct is_directed_tag
|
Chris@16
|
110 : mpl::bool_<is_convertible<Tag, directed_tag>::value>
|
Chris@16
|
111 { };
|
Chris@16
|
112 } // namespace graph_detail
|
Chris@16
|
113
|
Chris@16
|
114 template <typename Graph>
|
Chris@16
|
115 struct is_directed_graph
|
Chris@16
|
116 : graph_detail::is_directed_tag<
|
Chris@16
|
117 typename graph_traits<Graph>::directed_category
|
Chris@16
|
118 >
|
Chris@16
|
119 { };
|
Chris@16
|
120
|
Chris@16
|
121 template <typename Graph>
|
Chris@16
|
122 struct is_undirected_graph
|
Chris@16
|
123 : mpl::not_< is_directed_graph<Graph> >
|
Chris@16
|
124 { };
|
Chris@16
|
125 //@}
|
Chris@16
|
126
|
Chris@16
|
127 // edge_parallel_category tags
|
Chris@16
|
128 struct allow_parallel_edge_tag { };
|
Chris@16
|
129 struct disallow_parallel_edge_tag { };
|
Chris@16
|
130
|
Chris@16
|
131 namespace detail {
|
Chris@16
|
132 inline bool allows_parallel(allow_parallel_edge_tag) { return true; }
|
Chris@16
|
133 inline bool allows_parallel(disallow_parallel_edge_tag) { return false; }
|
Chris@16
|
134 }
|
Chris@16
|
135
|
Chris@16
|
136 template <typename Graph>
|
Chris@16
|
137 bool allows_parallel_edges(const Graph&) {
|
Chris@16
|
138 typedef typename graph_traits<Graph>::edge_parallel_category Cat;
|
Chris@16
|
139 return detail::allows_parallel(Cat());
|
Chris@16
|
140 }
|
Chris@16
|
141
|
Chris@16
|
142 /** @name Parallel Edges Traits */
|
Chris@16
|
143 //@{
|
Chris@16
|
144 /**
|
Chris@16
|
145 * The is_multigraph metafunction returns true if the graph allows
|
Chris@16
|
146 * parallel edges. Technically, a multigraph is a simple graph that
|
Chris@16
|
147 * allows parallel edges, but since there are no traits for the allowance
|
Chris@16
|
148 * or disallowance of loops, this is a moot point.
|
Chris@16
|
149 */
|
Chris@16
|
150 template <typename Graph>
|
Chris@16
|
151 struct is_multigraph
|
Chris@16
|
152 : mpl::bool_<
|
Chris@16
|
153 is_same<
|
Chris@16
|
154 typename graph_traits<Graph>::edge_parallel_category,
|
Chris@16
|
155 allow_parallel_edge_tag
|
Chris@16
|
156 >::value
|
Chris@16
|
157 >
|
Chris@16
|
158 { };
|
Chris@16
|
159 //@}
|
Chris@16
|
160
|
Chris@16
|
161 // traversal_category tags
|
Chris@16
|
162 struct incidence_graph_tag { };
|
Chris@16
|
163 struct adjacency_graph_tag { };
|
Chris@16
|
164 struct bidirectional_graph_tag : virtual incidence_graph_tag { };
|
Chris@16
|
165 struct vertex_list_graph_tag { };
|
Chris@16
|
166 struct edge_list_graph_tag { };
|
Chris@16
|
167 struct adjacency_matrix_tag { };
|
Chris@16
|
168
|
Chris@16
|
169 // Parallel traversal_category tags
|
Chris@16
|
170 struct distributed_graph_tag { };
|
Chris@16
|
171 struct distributed_vertex_list_graph_tag { };
|
Chris@16
|
172 struct distributed_edge_list_graph_tag { };
|
Chris@16
|
173 #define BOOST_GRAPH_SEQUENTIAL_TRAITS_DEFINES_DISTRIBUTED_TAGS // Disable these from external versions of PBGL
|
Chris@16
|
174
|
Chris@16
|
175 /** @name Traversal Category Traits
|
Chris@16
|
176 * These traits classify graph types by their supported methods of
|
Chris@16
|
177 * vertex and edge traversal.
|
Chris@16
|
178 */
|
Chris@16
|
179 //@{
|
Chris@16
|
180 template <typename Graph>
|
Chris@16
|
181 struct is_incidence_graph
|
Chris@16
|
182 : mpl::bool_<
|
Chris@16
|
183 is_convertible<
|
Chris@16
|
184 typename graph_traits<Graph>::traversal_category,
|
Chris@16
|
185 incidence_graph_tag
|
Chris@16
|
186 >::value
|
Chris@16
|
187 >
|
Chris@16
|
188 { };
|
Chris@16
|
189
|
Chris@16
|
190 template <typename Graph>
|
Chris@16
|
191 struct is_bidirectional_graph
|
Chris@16
|
192 : mpl::bool_<
|
Chris@16
|
193 is_convertible<
|
Chris@16
|
194 typename graph_traits<Graph>::traversal_category,
|
Chris@16
|
195 bidirectional_graph_tag
|
Chris@16
|
196 >::value
|
Chris@16
|
197 >
|
Chris@16
|
198 { };
|
Chris@16
|
199
|
Chris@16
|
200 template <typename Graph>
|
Chris@16
|
201 struct is_vertex_list_graph
|
Chris@16
|
202 : mpl::bool_<
|
Chris@16
|
203 is_convertible<
|
Chris@16
|
204 typename graph_traits<Graph>::traversal_category,
|
Chris@16
|
205 vertex_list_graph_tag
|
Chris@16
|
206 >::value
|
Chris@16
|
207 >
|
Chris@16
|
208 { };
|
Chris@16
|
209
|
Chris@16
|
210 template <typename Graph>
|
Chris@16
|
211 struct is_edge_list_graph
|
Chris@16
|
212 : mpl::bool_<
|
Chris@16
|
213 is_convertible<
|
Chris@16
|
214 typename graph_traits<Graph>::traversal_category,
|
Chris@16
|
215 edge_list_graph_tag
|
Chris@16
|
216 >::value
|
Chris@16
|
217 >
|
Chris@16
|
218 { };
|
Chris@16
|
219
|
Chris@16
|
220 template <typename Graph>
|
Chris@16
|
221 struct is_adjacency_matrix
|
Chris@16
|
222 : mpl::bool_<
|
Chris@16
|
223 is_convertible<
|
Chris@16
|
224 typename graph_traits<Graph>::traversal_category,
|
Chris@16
|
225 adjacency_matrix_tag
|
Chris@16
|
226 >::value
|
Chris@16
|
227 >
|
Chris@16
|
228 { };
|
Chris@16
|
229 //@}
|
Chris@16
|
230
|
Chris@16
|
231 /** @name Directed Graph Traits
|
Chris@16
|
232 * These metafunctions are used to fully classify directed vs. undirected
|
Chris@16
|
233 * graphs. Recall that an undirected graph is also bidirectional, but it
|
Chris@16
|
234 * cannot be both undirected and directed at the same time.
|
Chris@16
|
235 */
|
Chris@16
|
236 //@{
|
Chris@16
|
237 template <typename Graph>
|
Chris@16
|
238 struct is_directed_unidirectional_graph
|
Chris@16
|
239 : mpl::and_<
|
Chris@16
|
240 is_directed_graph<Graph>, mpl::not_< is_bidirectional_graph<Graph> >
|
Chris@16
|
241 >
|
Chris@16
|
242 { };
|
Chris@16
|
243
|
Chris@16
|
244 template <typename Graph>
|
Chris@16
|
245 struct is_directed_bidirectional_graph
|
Chris@16
|
246 : mpl::and_<
|
Chris@16
|
247 is_directed_graph<Graph>, is_bidirectional_graph<Graph>
|
Chris@16
|
248 >
|
Chris@16
|
249 { };
|
Chris@16
|
250 //@}
|
Chris@16
|
251
|
Chris@16
|
252 //?? not the right place ?? Lee
|
Chris@16
|
253 typedef boost::forward_traversal_tag multi_pass_input_iterator_tag;
|
Chris@16
|
254
|
Chris@16
|
255 namespace detail {
|
Chris@16
|
256 BOOST_MPL_HAS_XXX_TRAIT_DEF(graph_property_type)
|
Chris@16
|
257 BOOST_MPL_HAS_XXX_TRAIT_DEF(edge_property_type)
|
Chris@16
|
258 BOOST_MPL_HAS_XXX_TRAIT_DEF(vertex_property_type)
|
Chris@16
|
259
|
Chris@16
|
260 template <typename G> struct get_graph_property_type {typedef typename G::graph_property_type type;};
|
Chris@16
|
261 template <typename G> struct get_edge_property_type {typedef typename G::edge_property_type type;};
|
Chris@16
|
262 template <typename G> struct get_vertex_property_type {typedef typename G::vertex_property_type type;};
|
Chris@16
|
263 }
|
Chris@16
|
264
|
Chris@16
|
265 template <typename G>
|
Chris@16
|
266 struct graph_property_type
|
Chris@16
|
267 : boost::mpl::eval_if<detail::has_graph_property_type<G>,
|
Chris@16
|
268 detail::get_graph_property_type<G>,
|
Chris@16
|
269 no_property> {};
|
Chris@16
|
270 template <typename G>
|
Chris@16
|
271 struct edge_property_type
|
Chris@16
|
272 : boost::mpl::eval_if<detail::has_edge_property_type<G>,
|
Chris@16
|
273 detail::get_edge_property_type<G>,
|
Chris@16
|
274 no_property> {};
|
Chris@16
|
275 template <typename G>
|
Chris@16
|
276 struct vertex_property_type
|
Chris@16
|
277 : boost::mpl::eval_if<detail::has_vertex_property_type<G>,
|
Chris@16
|
278 detail::get_vertex_property_type<G>,
|
Chris@16
|
279 no_property> {};
|
Chris@16
|
280
|
Chris@16
|
281 template<typename G>
|
Chris@16
|
282 struct graph_bundle_type {
|
Chris@16
|
283 typedef typename G::graph_bundled type;
|
Chris@16
|
284 };
|
Chris@16
|
285
|
Chris@16
|
286 template<typename G>
|
Chris@16
|
287 struct vertex_bundle_type {
|
Chris@16
|
288 typedef typename G::vertex_bundled type;
|
Chris@16
|
289 };
|
Chris@16
|
290
|
Chris@16
|
291 template<typename G>
|
Chris@16
|
292 struct edge_bundle_type {
|
Chris@16
|
293 typedef typename G::edge_bundled type;
|
Chris@16
|
294 };
|
Chris@16
|
295
|
Chris@16
|
296 namespace graph { namespace detail {
|
Chris@16
|
297 template<typename Graph, typename Descriptor>
|
Chris@16
|
298 class bundled_result {
|
Chris@16
|
299 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
|
Chris@16
|
300 typedef typename mpl::if_c<(is_same<Descriptor, Vertex>::value),
|
Chris@16
|
301 vertex_bundle_type<Graph>,
|
Chris@16
|
302 edge_bundle_type<Graph> >::type bundler;
|
Chris@16
|
303 public:
|
Chris@16
|
304 typedef typename bundler::type type;
|
Chris@16
|
305 };
|
Chris@16
|
306
|
Chris@16
|
307 template<typename Graph>
|
Chris@16
|
308 class bundled_result<Graph, graph_bundle_t> {
|
Chris@16
|
309 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
|
Chris@16
|
310 typedef graph_bundle_type<Graph> bundler;
|
Chris@16
|
311 public:
|
Chris@16
|
312 typedef typename bundler::type type;
|
Chris@16
|
313 };
|
Chris@16
|
314
|
Chris@16
|
315 } } // namespace graph::detail
|
Chris@16
|
316
|
Chris@16
|
317 namespace graph_detail {
|
Chris@16
|
318 // A helper metafunction for determining whether or not a type is
|
Chris@16
|
319 // bundled.
|
Chris@16
|
320 template <typename T>
|
Chris@16
|
321 struct is_no_bundle : mpl::bool_<is_same<T, no_property>::value>
|
Chris@16
|
322 { };
|
Chris@16
|
323 } // namespace graph_detail
|
Chris@16
|
324
|
Chris@16
|
325 /** @name Graph Property Traits
|
Chris@16
|
326 * These metafunctions (along with those above), can be used to access the
|
Chris@16
|
327 * vertex and edge properties (bundled or otherwise) of vertices and
|
Chris@16
|
328 * edges.
|
Chris@16
|
329 */
|
Chris@16
|
330 //@{
|
Chris@16
|
331 template<typename Graph>
|
Chris@16
|
332 struct has_graph_property
|
Chris@16
|
333 : mpl::not_<
|
Chris@16
|
334 typename detail::is_no_property<
|
Chris@16
|
335 typename graph_property_type<Graph>::type
|
Chris@16
|
336 >::type
|
Chris@16
|
337 >::type
|
Chris@16
|
338 { };
|
Chris@16
|
339
|
Chris@16
|
340 template<typename Graph>
|
Chris@16
|
341 struct has_bundled_graph_property
|
Chris@16
|
342 : mpl::not_<
|
Chris@16
|
343 graph_detail::is_no_bundle<typename graph_bundle_type<Graph>::type>
|
Chris@16
|
344 >
|
Chris@16
|
345 { };
|
Chris@16
|
346
|
Chris@16
|
347 template <typename Graph>
|
Chris@16
|
348 struct has_vertex_property
|
Chris@16
|
349 : mpl::not_<
|
Chris@16
|
350 typename detail::is_no_property<typename vertex_property_type<Graph>::type>
|
Chris@16
|
351 >::type
|
Chris@16
|
352 { };
|
Chris@16
|
353
|
Chris@16
|
354 template <typename Graph>
|
Chris@16
|
355 struct has_bundled_vertex_property
|
Chris@16
|
356 : mpl::not_<
|
Chris@16
|
357 graph_detail::is_no_bundle<typename vertex_bundle_type<Graph>::type>
|
Chris@16
|
358 >
|
Chris@16
|
359 { };
|
Chris@16
|
360
|
Chris@16
|
361 template <typename Graph>
|
Chris@16
|
362 struct has_edge_property
|
Chris@16
|
363 : mpl::not_<
|
Chris@16
|
364 typename detail::is_no_property<typename edge_property_type<Graph>::type>
|
Chris@16
|
365 >::type
|
Chris@16
|
366 { };
|
Chris@16
|
367
|
Chris@16
|
368 template <typename Graph>
|
Chris@16
|
369 struct has_bundled_edge_property
|
Chris@16
|
370 : mpl::not_<
|
Chris@16
|
371 graph_detail::is_no_bundle<typename edge_bundle_type<Graph>::type>
|
Chris@16
|
372 >
|
Chris@16
|
373 { };
|
Chris@16
|
374 //@}
|
Chris@16
|
375
|
Chris@16
|
376 } // namespace boost
|
Chris@16
|
377
|
Chris@16
|
378 // Since pair is in namespace std, Koenig lookup will find source and
|
Chris@16
|
379 // target if they are also defined in namespace std. This is illegal,
|
Chris@16
|
380 // but the alternative is to put source and target in the global
|
Chris@16
|
381 // namespace which causes name conflicts with other libraries (like
|
Chris@16
|
382 // SUIF).
|
Chris@16
|
383 namespace std {
|
Chris@16
|
384
|
Chris@16
|
385 /* Some helper functions for dealing with pairs as edges */
|
Chris@16
|
386 template <class T, class G>
|
Chris@16
|
387 T source(pair<T,T> p, const G&) { return p.first; }
|
Chris@16
|
388
|
Chris@16
|
389 template <class T, class G>
|
Chris@16
|
390 T target(pair<T,T> p, const G&) { return p.second; }
|
Chris@16
|
391
|
Chris@16
|
392 }
|
Chris@16
|
393
|
Chris@16
|
394 #if defined(__GNUC__) && defined(__SGI_STL_PORT)
|
Chris@16
|
395 // For some reason g++ with STLport does not see the above definition
|
Chris@16
|
396 // of source() and target() unless we bring them into the boost
|
Chris@16
|
397 // namespace.
|
Chris@16
|
398 namespace boost {
|
Chris@16
|
399 using std::source;
|
Chris@16
|
400 using std::target;
|
Chris@16
|
401 }
|
Chris@16
|
402 #endif
|
Chris@16
|
403
|
Chris@16
|
404 #endif // BOOST_GRAPH_TRAITS_HPP
|