properties.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2004 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Douglas Gregor
  6. // Andrew Lumsdaine
  7. #ifndef BOOST_GRAPH_PARALLEL_PROPERTIES_HPP
  8. #define BOOST_GRAPH_PARALLEL_PROPERTIES_HPP
  9. #ifndef BOOST_GRAPH_USE_MPI
  10. #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
  11. #endif
  12. #include <boost/graph/properties.hpp>
  13. #include <boost/property_map/parallel/distributed_property_map.hpp>
  14. namespace boost {
  15. /***************************************************************************
  16. * Property map reduction operations
  17. ***************************************************************************/
  18. /**
  19. * Metafunction that produces a reduction operation for the given
  20. * property. The default behavior merely forwards to @ref
  21. * basic_reduce, but it is expected that this class template will be
  22. * specified for important properties.
  23. */
  24. template<typename Property>
  25. struct property_reduce
  26. {
  27. template<typename Value>
  28. class apply : public parallel::basic_reduce<Value> {};
  29. };
  30. /**
  31. * Reduction of vertex colors can only darken, not lighten, the
  32. * color. Black cannot turn black, grey can only turn black, and
  33. * white can be changed to either color. The default color is white.
  34. */
  35. template<>
  36. struct property_reduce<vertex_color_t>
  37. {
  38. template<typename Color>
  39. class apply
  40. {
  41. typedef color_traits<Color> traits;
  42. public:
  43. BOOST_STATIC_CONSTANT(bool, non_default_resolver = true);
  44. template<typename Key>
  45. Color operator()(const Key&) const { return traits::white(); }
  46. template<typename Key>
  47. Color operator()(const Key&, Color local, Color remote) const {
  48. if (local == traits::white()) return remote;
  49. else if (remote == traits::black()) return remote;
  50. else return local;
  51. }
  52. };
  53. };
  54. /**
  55. * Reduction of a distance always takes the shorter distance. The
  56. * default distance value is the maximum value for the data type.
  57. */
  58. template<>
  59. struct property_reduce<vertex_distance_t>
  60. {
  61. template<typename T>
  62. class apply
  63. {
  64. public:
  65. BOOST_STATIC_CONSTANT(bool, non_default_resolver = true);
  66. template<typename Key>
  67. T operator()(const Key&) const { return (std::numeric_limits<T>::max)(); }
  68. template<typename Key>
  69. T operator()(const Key&, T x, T y) const { return x < y? x : y; }
  70. };
  71. };
  72. template<>
  73. struct property_reduce<vertex_predecessor_t>
  74. {
  75. template<typename T>
  76. class apply
  77. {
  78. public:
  79. BOOST_STATIC_CONSTANT(bool, non_default_resolver = true);
  80. template<typename Key>
  81. T operator()(Key key) const { return key; }
  82. template<typename Key>
  83. T operator()(Key key, T, T y) const { return y; }
  84. };
  85. };
  86. template<typename Property, typename PropertyMap>
  87. inline void set_property_map_role(Property p, PropertyMap pm)
  88. {
  89. typedef typename property_traits<PropertyMap>::value_type value_type;
  90. typedef property_reduce<Property> property_red;
  91. typedef typename property_red::template apply<value_type> reduce;
  92. pm.set_reduce(reduce());
  93. }
  94. } // end namespace boost
  95. #endif // BOOST_GRAPH_PARALLEL_PROPERTIES_HPP