/Src/Dependencies/Boost/boost/graph/one_bit_color_map.hpp

http://hadesmem.googlecode.com/ · C++ Header · 104 lines · 76 code · 18 blank · 10 comment · 1 complexity · 2d2f0d2abcf03cf26b16e314ad28b796 MD5 · raw file

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