properties.hpp 13 KB

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