two_bit_color_map.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (C) 2005-2006 The Trustees of Indiana University.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Jeremiah Willcock
  6. // Douglas Gregor
  7. // Andrew Lumsdaine
  8. // Two bit per color property map
  9. #ifndef BOOST_TWO_BIT_COLOR_MAP_HPP
  10. #define BOOST_TWO_BIT_COLOR_MAP_HPP
  11. #include <boost/property_map/property_map.hpp>
  12. #include <boost/graph/properties.hpp>
  13. #include <boost/graph/detail/mpi_include.hpp>
  14. #include <boost/shared_array.hpp>
  15. #include <boost/config.hpp>
  16. #include <boost/assert.hpp>
  17. #include <algorithm>
  18. #include <limits>
  19. namespace boost {
  20. enum two_bit_color_type {
  21. two_bit_white = 0,
  22. two_bit_gray = 1,
  23. two_bit_green = 2,
  24. two_bit_black = 3
  25. };
  26. template <>
  27. struct color_traits<two_bit_color_type>
  28. {
  29. static two_bit_color_type white() { return two_bit_white; }
  30. static two_bit_color_type gray() { return two_bit_gray; }
  31. static two_bit_color_type green() { return two_bit_green; }
  32. static two_bit_color_type black() { return two_bit_black; }
  33. };
  34. template<typename IndexMap = identity_property_map>
  35. struct two_bit_color_map
  36. {
  37. std::size_t n;
  38. IndexMap index;
  39. shared_array<unsigned char> data;
  40. BOOST_STATIC_CONSTANT(int, bits_per_char = std::numeric_limits<unsigned char>::digits);
  41. BOOST_STATIC_CONSTANT(int, elements_per_char = bits_per_char / 2);
  42. typedef typename property_traits<IndexMap>::key_type key_type;
  43. typedef two_bit_color_type value_type;
  44. typedef void reference;
  45. typedef read_write_property_map_tag category;
  46. explicit two_bit_color_map(std::size_t n, const IndexMap& index = IndexMap())
  47. : n(n), index(index), data(new unsigned char[(n + elements_per_char - 1) / elements_per_char])
  48. {
  49. // Fill to white
  50. std::fill(data.get(), data.get() + (n + elements_per_char - 1) / elements_per_char, 0);
  51. }
  52. };
  53. template<typename IndexMap>
  54. inline two_bit_color_type
  55. get(const two_bit_color_map<IndexMap>& pm,
  56. typename property_traits<IndexMap>::key_type key)
  57. {
  58. BOOST_STATIC_CONSTANT(int, elements_per_char = two_bit_color_map<IndexMap>::elements_per_char);
  59. typename property_traits<IndexMap>::value_type i = get(pm.index, key);
  60. BOOST_ASSERT ((std::size_t)i < pm.n);
  61. std::size_t byte_num = i / elements_per_char;
  62. std::size_t bit_position = ((i % elements_per_char) * 2);
  63. return two_bit_color_type((pm.data.get()[byte_num] >> bit_position) & 3);
  64. }
  65. template<typename IndexMap>
  66. inline void
  67. put(const two_bit_color_map<IndexMap>& pm,
  68. typename property_traits<IndexMap>::key_type key,
  69. two_bit_color_type value)
  70. {
  71. BOOST_STATIC_CONSTANT(int, elements_per_char = two_bit_color_map<IndexMap>::elements_per_char);
  72. typename property_traits<IndexMap>::value_type i = get(pm.index, key);
  73. BOOST_ASSERT ((std::size_t)i < pm.n);
  74. BOOST_ASSERT (value >= 0 && value < 4);
  75. std::size_t byte_num = i / elements_per_char;
  76. std::size_t bit_position = ((i % elements_per_char) * 2);
  77. pm.data.get()[byte_num] =
  78. (unsigned char)
  79. ((pm.data.get()[byte_num] & ~(3 << bit_position))
  80. | (value << bit_position));
  81. }
  82. template<typename IndexMap>
  83. inline two_bit_color_map<IndexMap>
  84. make_two_bit_color_map(std::size_t n, const IndexMap& index_map)
  85. {
  86. return two_bit_color_map<IndexMap>(n, index_map);
  87. }
  88. } // end namespace boost
  89. #include BOOST_GRAPH_MPI_INCLUDE(<boost/graph/distributed/two_bit_color_map.hpp>)
  90. #endif // BOOST_TWO_BIT_COLOR_MAP_HPP