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