properties.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #ifndef BOOST_GRAPH_PROPERTIES_HPP
  10. #define BOOST_GRAPH_PROPERTIES_HPP
  11. #include <boost/config.hpp>
  12. #include <boost/assert.hpp>
  13. #include <boost/pending/property.hpp>
  14. #include <boost/detail/workaround.hpp>
  15. // Include the property map library and extensions in the BGL.
  16. #include <boost/property_map/property_map.hpp>
  17. #include <boost/graph/property_maps/constant_property_map.hpp>
  18. #include <boost/graph/property_maps/null_property_map.hpp>
  19. #include <boost/graph/graph_traits.hpp>
  20. #include <boost/type_traits.hpp>
  21. #include <boost/limits.hpp>
  22. #include <boost/mpl/and.hpp>
  23. #include <boost/mpl/not.hpp>
  24. #include <boost/mpl/if.hpp>
  25. namespace boost {
  26. enum default_color_type { white_color, gray_color, green_color, red_color, black_color };
  27. template <class ColorValue>
  28. struct color_traits {
  29. static default_color_type white() { return white_color; }
  30. static default_color_type gray() { return gray_color; }
  31. static default_color_type green() { return green_color; }
  32. static default_color_type red() { return red_color; }
  33. static default_color_type black() { return black_color; }
  34. };
  35. // These functions are now obsolete, replaced by color_traits.
  36. inline default_color_type white(default_color_type) { return white_color; }
  37. inline default_color_type gray(default_color_type) { return gray_color; }
  38. inline default_color_type green(default_color_type) { return green_color; }
  39. inline default_color_type red(default_color_type) { return red_color; }
  40. inline default_color_type black(default_color_type) { return black_color; }
  41. struct graph_property_tag { };
  42. struct vertex_property_tag { };
  43. struct edge_property_tag { };
  44. // See examples/edge_property.cpp for how to use this.
  45. #define BOOST_INSTALL_PROPERTY(KIND, NAME) \
  46. template <> struct property_kind<KIND##_##NAME##_t> { \
  47. typedef KIND##_property_tag type; \
  48. }
  49. #define BOOST_DEF_PROPERTY(KIND, NAME) \
  50. enum KIND##_##NAME##_t { KIND##_##NAME }; \
  51. BOOST_INSTALL_PROPERTY(KIND, NAME)
  52. // These three are defined in boost/pending/property.hpp
  53. BOOST_INSTALL_PROPERTY(vertex, all);
  54. BOOST_INSTALL_PROPERTY(edge, all);
  55. BOOST_INSTALL_PROPERTY(graph, all);
  56. BOOST_DEF_PROPERTY(vertex, index);
  57. BOOST_DEF_PROPERTY(vertex, index1);
  58. BOOST_DEF_PROPERTY(vertex, index2);
  59. BOOST_DEF_PROPERTY(vertex, root);
  60. BOOST_DEF_PROPERTY(edge, index);
  61. BOOST_DEF_PROPERTY(edge, name);
  62. BOOST_DEF_PROPERTY(edge, weight);
  63. BOOST_DEF_PROPERTY(edge, weight2);
  64. BOOST_DEF_PROPERTY(edge, color);
  65. BOOST_DEF_PROPERTY(vertex, name);
  66. BOOST_DEF_PROPERTY(graph, name);
  67. BOOST_DEF_PROPERTY(vertex, distance);
  68. BOOST_DEF_PROPERTY(vertex, distance2);
  69. BOOST_DEF_PROPERTY(vertex, color);
  70. BOOST_DEF_PROPERTY(vertex, degree);
  71. BOOST_DEF_PROPERTY(vertex, in_degree);
  72. BOOST_DEF_PROPERTY(vertex, out_degree);
  73. BOOST_DEF_PROPERTY(vertex, current_degree);
  74. BOOST_DEF_PROPERTY(vertex, priority);
  75. BOOST_DEF_PROPERTY(vertex, discover_time);
  76. BOOST_DEF_PROPERTY(vertex, finish_time);
  77. BOOST_DEF_PROPERTY(vertex, predecessor);
  78. BOOST_DEF_PROPERTY(vertex, rank);
  79. BOOST_DEF_PROPERTY(vertex, centrality);
  80. BOOST_DEF_PROPERTY(vertex, lowpoint);
  81. BOOST_DEF_PROPERTY(vertex, potential);
  82. BOOST_DEF_PROPERTY(vertex, update);
  83. BOOST_DEF_PROPERTY(vertex, underlying);
  84. BOOST_DEF_PROPERTY(edge, reverse);
  85. BOOST_DEF_PROPERTY(edge, capacity);
  86. BOOST_DEF_PROPERTY(edge, flow);
  87. BOOST_DEF_PROPERTY(edge, residual_capacity);
  88. BOOST_DEF_PROPERTY(edge, centrality);
  89. BOOST_DEF_PROPERTY(edge, discover_time);
  90. BOOST_DEF_PROPERTY(edge, update);
  91. BOOST_DEF_PROPERTY(edge, finished);
  92. BOOST_DEF_PROPERTY(edge, underlying);
  93. BOOST_DEF_PROPERTY(graph, visitor);
  94. // These tags are used for property bundles
  95. // These three are defined in boost/pending/property.hpp
  96. BOOST_INSTALL_PROPERTY(graph, bundle);
  97. BOOST_INSTALL_PROPERTY(vertex, bundle);
  98. BOOST_INSTALL_PROPERTY(edge, bundle);
  99. // These tags are used to denote the owners and local descriptors
  100. // for the vertices and edges of a distributed graph.
  101. BOOST_DEF_PROPERTY(vertex, global);
  102. BOOST_DEF_PROPERTY(vertex, owner);
  103. BOOST_DEF_PROPERTY(vertex, local);
  104. BOOST_DEF_PROPERTY(edge, global);
  105. BOOST_DEF_PROPERTY(edge, owner);
  106. BOOST_DEF_PROPERTY(edge, local);
  107. BOOST_DEF_PROPERTY(vertex, local_index);
  108. BOOST_DEF_PROPERTY(edge, local_index);
  109. #undef BOOST_DEF_PROPERTY
  110. namespace detail {
  111. template <typename G, typename Tag>
  112. struct property_kind_from_graph: property_kind<Tag> {};
  113. #ifndef BOOST_GRAPH_NO_BUNDLED_PROPERTIES
  114. template <typename G, typename R, typename T>
  115. struct property_kind_from_graph<G, R T::*> {
  116. typedef typename boost::mpl::if_<
  117. boost::is_base_of<T, typename vertex_bundle_type<G>::type>,
  118. vertex_property_tag,
  119. typename boost::mpl::if_<
  120. boost::is_base_of<T, typename edge_bundle_type<G>::type>,
  121. edge_property_tag,
  122. typename boost::mpl::if_<
  123. boost::is_base_of<T, typename graph_bundle_type<G>::type>,
  124. graph_property_tag,
  125. void>::type>::type>::type type;
  126. };
  127. #endif
  128. struct dummy_edge_property_selector {
  129. template <class Graph, class Property, class Tag>
  130. struct bind_ {
  131. typedef identity_property_map type;
  132. typedef identity_property_map const_type;
  133. };
  134. };
  135. struct dummy_vertex_property_selector {
  136. template <class Graph, class Property, class Tag>
  137. struct bind_ {
  138. typedef identity_property_map type;
  139. typedef identity_property_map const_type;
  140. };
  141. };
  142. } // namespace detail
  143. // Graph classes can either partially specialize property_map
  144. // or they can specialize these two selector classes.
  145. template <class GraphTag>
  146. struct edge_property_selector {
  147. typedef detail::dummy_edge_property_selector type;
  148. };
  149. template <class GraphTag>
  150. struct vertex_property_selector {
  151. typedef detail::dummy_vertex_property_selector type;
  152. };
  153. namespace detail {
  154. template <typename A> struct return_void {typedef void type;};
  155. template <typename Graph, typename Enable = void>
  156. struct graph_tag_or_void {
  157. typedef void type;
  158. };
  159. template <typename Graph>
  160. struct graph_tag_or_void<Graph, typename return_void<typename Graph::graph_tag>::type> {
  161. typedef typename Graph::graph_tag type;
  162. };
  163. template <class Graph, class PropertyTag>
  164. struct edge_property_map
  165. : edge_property_selector<
  166. typename graph_tag_or_void<Graph>::type
  167. >::type::template bind_<
  168. Graph,
  169. typename edge_property_type<Graph>::type,
  170. PropertyTag>
  171. {};
  172. template <class Graph, class PropertyTag>
  173. struct vertex_property_map
  174. : vertex_property_selector<
  175. typename graph_tag_or_void<Graph>::type
  176. >::type::template bind_<
  177. Graph,
  178. typename vertex_property_type<Graph>::type,
  179. PropertyTag>
  180. {};
  181. } // namespace detail
  182. template <class Graph, class Property, class Enable = void>
  183. struct property_map:
  184. mpl::if_<
  185. is_same<typename detail::property_kind_from_graph<Graph, Property>::type, edge_property_tag>,
  186. detail::edge_property_map<Graph, Property>,
  187. detail::vertex_property_map<Graph, Property> >::type
  188. {};
  189. // shortcut for accessing the value type of the property map
  190. template <class Graph, class Property>
  191. class property_map_value {
  192. typedef typename property_map<Graph, Property>::const_type PMap;
  193. public:
  194. typedef typename property_traits<PMap>::value_type type;
  195. };
  196. template <class Graph, class Property>
  197. class graph_property {
  198. public:
  199. typedef typename property_value<
  200. typename boost::graph_property_type<Graph>::type, Property
  201. >::type type;
  202. };
  203. template <typename Graph> struct vertex_property: vertex_property_type<Graph> {};
  204. template <typename Graph> struct edge_property: edge_property_type<Graph> {};
  205. template <typename Graph>
  206. class degree_property_map
  207. : public put_get_helper<typename graph_traits<Graph>::degree_size_type,
  208. degree_property_map<Graph> >
  209. {
  210. public:
  211. typedef typename graph_traits<Graph>::vertex_descriptor key_type;
  212. typedef typename graph_traits<Graph>::degree_size_type value_type;
  213. typedef value_type reference;
  214. typedef readable_property_map_tag category;
  215. degree_property_map(const Graph& g) : m_g(g) { }
  216. value_type operator[](const key_type& v) const {
  217. return degree(v, m_g);
  218. }
  219. private:
  220. const Graph& m_g;
  221. };
  222. template <typename Graph>
  223. inline degree_property_map<Graph>
  224. make_degree_map(const Graph& g) {
  225. return degree_property_map<Graph>(g);
  226. }
  227. //========================================================================
  228. // Iterator Property Map Generating Functions contributed by
  229. // Kevin Vanhorn. (see also the property map generating functions
  230. // in boost/property_map/property_map.hpp)
  231. #if !defined(BOOST_NO_STD_ITERATOR_TRAITS)
  232. // A helper function for creating a vertex property map out of a
  233. // random access iterator and the internal vertex index map from a
  234. // graph.
  235. template <class PropertyGraph, class RandomAccessIterator>
  236. inline
  237. iterator_property_map<
  238. RandomAccessIterator,
  239. typename property_map<PropertyGraph, vertex_index_t>::type,
  240. typename std::iterator_traits<RandomAccessIterator>::value_type,
  241. typename std::iterator_traits<RandomAccessIterator>::reference
  242. >
  243. make_iterator_vertex_map(RandomAccessIterator iter, const PropertyGraph& g)
  244. {
  245. return make_iterator_property_map(iter, get(vertex_index, g));
  246. }
  247. // Use this next function when vertex_descriptor is known to be an
  248. // integer type, with values ranging from 0 to num_vertices(g).
  249. //
  250. template <class RandomAccessIterator>
  251. inline
  252. iterator_property_map<
  253. RandomAccessIterator,
  254. identity_property_map,
  255. typename std::iterator_traits<RandomAccessIterator>::value_type,
  256. typename std::iterator_traits<RandomAccessIterator>::reference
  257. >
  258. make_iterator_vertex_map(RandomAccessIterator iter)
  259. {
  260. return make_iterator_property_map(iter, identity_property_map());
  261. }
  262. #endif
  263. template <class PropertyGraph, class RandomAccessContainer>
  264. inline
  265. iterator_property_map<
  266. typename RandomAccessContainer::iterator,
  267. typename property_map<PropertyGraph, vertex_index_t>::type,
  268. typename RandomAccessContainer::value_type,
  269. typename RandomAccessContainer::reference
  270. >
  271. make_container_vertex_map(RandomAccessContainer& c, const PropertyGraph& g)
  272. {
  273. BOOST_ASSERT(c.size() >= num_vertices(g));
  274. return make_iterator_vertex_map(c.begin(), g);
  275. }
  276. template <class RandomAccessContainer> inline
  277. iterator_property_map<
  278. typename RandomAccessContainer::iterator,
  279. identity_property_map,
  280. typename RandomAccessContainer::value_type,
  281. typename RandomAccessContainer::reference
  282. >
  283. make_container_vertex_map(RandomAccessContainer& c)
  284. {
  285. return make_iterator_vertex_map(c.begin());
  286. }
  287. // NOTE: These functions are declared, but never defined since they need to
  288. // be overloaded by graph implementations. However, we need them to be
  289. // declared for the functions below.
  290. template<typename Graph, typename Tag>
  291. typename graph_property<Graph, graph_bundle_t>::type&
  292. get_property(Graph& g, Tag);
  293. template<typename Graph, typename Tag>
  294. typename graph_property<Graph, graph_bundle_t>::type const&
  295. get_property(Graph const& g, Tag);
  296. #ifndef BOOST_GRAPH_NO_BUNDLED_PROPERTIES
  297. // NOTE: This operation is a simple adaptor over the overloaded get_property
  298. // operations.
  299. template<typename Graph>
  300. inline typename graph_property<Graph, graph_bundle_t>::type&
  301. get_property(Graph& g) {
  302. return get_property(g, graph_bundle);
  303. }
  304. template<typename Graph>
  305. inline typename graph_property<Graph, graph_bundle_t>::type const&
  306. get_property(const Graph& g) {
  307. return get_property(g, graph_bundle);
  308. }
  309. #endif
  310. } // namespace boost
  311. #endif /* BOOST_GRAPH_PROPERTIES_HPP */