one_bit_color_map.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (C) 2005-2010 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. // One bit per color property map (gray and black are the same, green is not
  9. // supported)
  10. #ifndef BOOST_ONE_BIT_COLOR_MAP_HPP
  11. #define BOOST_ONE_BIT_COLOR_MAP_HPP
  12. #include <boost/property_map/property_map.hpp>
  13. #include <boost/graph/properties.hpp>
  14. #include <boost/graph/detail/mpi_include.hpp>
  15. #include <boost/shared_array.hpp>
  16. #include <boost/config.hpp>
  17. #include <boost/assert.hpp>
  18. #include <algorithm>
  19. #include <limits>
  20. namespace boost {
  21. enum one_bit_color_type {
  22. one_bit_white = 0,
  23. one_bit_not_white = 1
  24. };
  25. template <>
  26. struct color_traits<one_bit_color_type>
  27. {
  28. static one_bit_color_type white() { return one_bit_white; }
  29. static one_bit_color_type gray() { return one_bit_not_white; }
  30. static one_bit_color_type black() { return one_bit_not_white; }
  31. };
  32. template<typename IndexMap = identity_property_map>
  33. struct one_bit_color_map
  34. {
  35. BOOST_STATIC_CONSTANT(int, bits_per_char = std::numeric_limits<unsigned char>::digits);
  36. std::size_t n;
  37. IndexMap index;
  38. shared_array<unsigned char> data;
  39. typedef typename property_traits<IndexMap>::key_type key_type;
  40. typedef one_bit_color_type value_type;
  41. typedef void reference;
  42. typedef read_write_property_map_tag category;
  43. explicit one_bit_color_map(std::size_t n, const IndexMap& index = IndexMap())
  44. : n(n), index(index), data(new unsigned char[(n + bits_per_char - 1) / bits_per_char])
  45. {
  46. // Fill to white
  47. std::fill(data.get(), data.get() + (n + bits_per_char - 1) / bits_per_char, 0);
  48. }
  49. };
  50. template<typename IndexMap>
  51. inline one_bit_color_type
  52. get(const one_bit_color_map<IndexMap>& pm,
  53. typename property_traits<IndexMap>::key_type key)
  54. {
  55. BOOST_STATIC_CONSTANT(int, bits_per_char = one_bit_color_map<IndexMap>::bits_per_char);
  56. typename property_traits<IndexMap>::value_type i = get(pm.index, key);
  57. BOOST_ASSERT ((std::size_t)i < pm.n);
  58. return one_bit_color_type((pm.data.get()[i / bits_per_char] >> (i % bits_per_char)) & 1);
  59. }
  60. template<typename IndexMap>
  61. inline void
  62. put(const one_bit_color_map<IndexMap>& pm,
  63. typename property_traits<IndexMap>::key_type key,
  64. one_bit_color_type value)
  65. {
  66. BOOST_STATIC_CONSTANT(int, bits_per_char = one_bit_color_map<IndexMap>::bits_per_char);
  67. typename property_traits<IndexMap>::value_type i = get(pm.index, key);
  68. BOOST_ASSERT ((std::size_t)i < pm.n);
  69. BOOST_ASSERT (value >= 0 && value < 2);
  70. std::size_t byte_num = i / bits_per_char;
  71. std::size_t bit_position = (i % bits_per_char);
  72. pm.data.get()[byte_num] =
  73. (unsigned char)
  74. ((pm.data.get()[byte_num] & ~(1 << bit_position))
  75. | (value << bit_position));
  76. }
  77. template<typename IndexMap>
  78. inline one_bit_color_map<IndexMap>
  79. make_one_bit_color_map(std::size_t n, const IndexMap& index_map)
  80. {
  81. return one_bit_color_map<IndexMap>(n, index_map);
  82. }
  83. } // end namespace boost
  84. #include BOOST_GRAPH_MPI_INCLUDE(<boost/graph/distributed/one_bit_color_map.hpp>)
  85. #endif // BOOST_ONE_BIT_COLOR_MAP_HPP