[yc]Multi Bit Mask
boost的integer/integer_mask.hpp仅仅做了单个位的bit mask
要多个位必须写很多遍high_bit_mask_t
使用low_bits_mask_t也不能完全解决问题
所以自己用Typelist的那种写法写了一个
用法举例
bit_mask<INT_LIST_2(2, 3)>::value返回一个值,该值的第2、3位被置为1
其余位为0
1
2 namespace multi_bit_mask
3 {
4 namespace details
5 {
6
7 template < typename T >
8 struct get_size
9 {
10 enum {size = sizeof (T)};
11 };
12
13 template < int Bit >
14 struct bit_storage
15 {
16 typedef typename bit_storage < Bit - 1 > ::storage_type storage_type;
17 };
18
19 // ---------platform dependency-----------------------
20
21 typedef unsigned int smallest_storage_type;
22 typedef unsigned long long largest_storage_type;
23
24
25
26 template <>
27 struct bit_storage < 0 >
28 {
29 typedef smallest_storage_type storage_type;
30 };
31
32 template <>
33 struct bit_storage < get_size < smallest_storage_type > ::size * 8 >
34 {
35 typedef largest_storage_type storage_type;
36 };
37
38 // disable the 65th bit
39 template <>
40 struct bit_storage < get_size < largest_storage_type > ::size * 8 >
41 {
42 typedef void storage_type;
43 };
44
45 // ---------end of platform dependency----------------
46
47
48 template < unsigned int N, typename Next >
49 struct int_list
50 {
51 typedef typename bit_storage < N > ::storage_type storage_type;
52 static const storage_type value = N;
53 typedef Next next;
54 };
55
56 struct null_type{};
57
58 template < typename T1, typename T2, bool is_first >
59 struct selector
60 {
61 typedef T1 type;
62 };
63
64 template < typename T1, typename T2 >
65 struct compare_type
66 {
67 const static bool is_larger = sizeof (T1) > sizeof (T2);
68 typedef typename selector < T1, T2, is_larger > ::type large_type;
69 typedef typename selector < T1, T2, ! is_larger > ::type small_type;
70 };
71
72
73
74 template < typename T1, typename T2 >
75 struct selector < T1, T2, false >
76 {
77 typedef T2 type;
78 };
79
80 template < typename List >
81 class find_largest_storage
82 {
83 typedef typename find_largest_storage < typename List::next > ::storage_type T1;
84 typedef typename bit_storage < List::value > ::storage_type T2;
85 public :
86 typedef typename compare_type < T1, T2 > ::large_type storage_type;
87 };
88
89 template <>
90 class find_largest_storage < null_type >
91 {
92 public :
93 typedef smallest_storage_type storage_type;
94 };
95
96
97 }
98
99
100
101
102
103 template < int N >
104 struct single_bit_mask
105 {
106 typedef typename details::bit_storage < N > ::storage_type storage_type;
107 static const storage_type value
108 = static_cast < storage_type > (single_bit_mask < N - 1 > ::value) * 2 ;
109 };
110
111 template <>
112 struct single_bit_mask < 0 >
113 {
114 typedef details::bit_storage < 0 > ::storage_type storage_type;
115 static const storage_type value = 1 ;
116 };
117
118
119 typedef details::null_type null_type;
120
121 template < int N, typename Next >
122 struct int_list_t : public details::int_list < N, Next > {};
123
124 template < typename List >
125 struct bit_mask
126 {
127 public :
128
129 typedef typename details::find_largest_storage < List > ::storage_type storage_type;
130
131 static const storage_type value
132 = static_cast < storage_type > (single_bit_mask < List::value > ::value)
133 | static_cast < storage_type > (bit_mask < typename List::next > ::value);
134 };
135
136 template <>
137 struct bit_mask < null_type >
138 {
139 typedef details::bit_storage < 0 > ::storage_type storage_type;
140 static const storage_type value = 0 ;
141 };
142
143
144
145
146
147 #define INT_LIST_1(n1) multi_bit_mask::int_list_t<n1, multi_bit_mask::null_type>
148 #define INT_LIST_2(n1, n2) multi_bit_mask::int_list_t<n1, INT_LIST_1(n2) >
149 #define INT_LIST_3(n1, n2, n3) multi_bit_mask::int_list_t<n1, INT_LIST_2(n2, n3) >
150 #define INT_LIST_4(n1, n2, n3, n4) multi_bit_mask::int_list_t<n1, INT_LIST_3(n2, n3, n4) >
151 #define INT_LIST_5(n1, n2, n3, n4, n5) multi_bit_mask::int_list_t<n1, INT_LIST_4(n2, n3, n4, n5) >
152 #define INT_LIST_6(n1, n2, n3, n4, n5, n6) multi_bit_mask::int_list_t<n1, INT_LIST_5(n2, n3, n4, n5, n6) >
153 #define INT_LIST_7(n1, n2, n3, n4, n5, n6, n7) multi_bit_mask::int_list_t<n1, INT_LIST_6(n2, n3, n4, n5, n6, n7) >
154 #define INT_LIST_8(n1, n2, n3, n4, n5, n6, n7, n8) multi_bit_mask::int_list_t<n1, INT_LIST_7(n2, n3, n4, n5, n6, n7, n8) >
155
156 }
157
158
159
2 namespace multi_bit_mask
3 {
4 namespace details
5 {
6
7 template < typename T >
8 struct get_size
9 {
10 enum {size = sizeof (T)};
11 };
12
13 template < int Bit >
14 struct bit_storage
15 {
16 typedef typename bit_storage < Bit - 1 > ::storage_type storage_type;
17 };
18
19 // ---------platform dependency-----------------------
20
21 typedef unsigned int smallest_storage_type;
22 typedef unsigned long long largest_storage_type;
23
24
25
26 template <>
27 struct bit_storage < 0 >
28 {
29 typedef smallest_storage_type storage_type;
30 };
31
32 template <>
33 struct bit_storage < get_size < smallest_storage_type > ::size * 8 >
34 {
35 typedef largest_storage_type storage_type;
36 };
37
38 // disable the 65th bit
39 template <>
40 struct bit_storage < get_size < largest_storage_type > ::size * 8 >
41 {
42 typedef void storage_type;
43 };
44
45 // ---------end of platform dependency----------------
46
47
48 template < unsigned int N, typename Next >
49 struct int_list
50 {
51 typedef typename bit_storage < N > ::storage_type storage_type;
52 static const storage_type value = N;
53 typedef Next next;
54 };
55
56 struct null_type{};
57
58 template < typename T1, typename T2, bool is_first >
59 struct selector
60 {
61 typedef T1 type;
62 };
63
64 template < typename T1, typename T2 >
65 struct compare_type
66 {
67 const static bool is_larger = sizeof (T1) > sizeof (T2);
68 typedef typename selector < T1, T2, is_larger > ::type large_type;
69 typedef typename selector < T1, T2, ! is_larger > ::type small_type;
70 };
71
72
73
74 template < typename T1, typename T2 >
75 struct selector < T1, T2, false >
76 {
77 typedef T2 type;
78 };
79
80 template < typename List >
81 class find_largest_storage
82 {
83 typedef typename find_largest_storage < typename List::next > ::storage_type T1;
84 typedef typename bit_storage < List::value > ::storage_type T2;
85 public :
86 typedef typename compare_type < T1, T2 > ::large_type storage_type;
87 };
88
89 template <>
90 class find_largest_storage < null_type >
91 {
92 public :
93 typedef smallest_storage_type storage_type;
94 };
95
96
97 }
98
99
100
101
102
103 template < int N >
104 struct single_bit_mask
105 {
106 typedef typename details::bit_storage < N > ::storage_type storage_type;
107 static const storage_type value
108 = static_cast < storage_type > (single_bit_mask < N - 1 > ::value) * 2 ;
109 };
110
111 template <>
112 struct single_bit_mask < 0 >
113 {
114 typedef details::bit_storage < 0 > ::storage_type storage_type;
115 static const storage_type value = 1 ;
116 };
117
118
119 typedef details::null_type null_type;
120
121 template < int N, typename Next >
122 struct int_list_t : public details::int_list < N, Next > {};
123
124 template < typename List >
125 struct bit_mask
126 {
127 public :
128
129 typedef typename details::find_largest_storage < List > ::storage_type storage_type;
130
131 static const storage_type value
132 = static_cast < storage_type > (single_bit_mask < List::value > ::value)
133 | static_cast < storage_type > (bit_mask < typename List::next > ::value);
134 };
135
136 template <>
137 struct bit_mask < null_type >
138 {
139 typedef details::bit_storage < 0 > ::storage_type storage_type;
140 static const storage_type value = 0 ;
141 };
142
143
144
145
146
147 #define INT_LIST_1(n1) multi_bit_mask::int_list_t<n1, multi_bit_mask::null_type>
148 #define INT_LIST_2(n1, n2) multi_bit_mask::int_list_t<n1, INT_LIST_1(n2) >
149 #define INT_LIST_3(n1, n2, n3) multi_bit_mask::int_list_t<n1, INT_LIST_2(n2, n3) >
150 #define INT_LIST_4(n1, n2, n3, n4) multi_bit_mask::int_list_t<n1, INT_LIST_3(n2, n3, n4) >
151 #define INT_LIST_5(n1, n2, n3, n4, n5) multi_bit_mask::int_list_t<n1, INT_LIST_4(n2, n3, n4, n5) >
152 #define INT_LIST_6(n1, n2, n3, n4, n5, n6) multi_bit_mask::int_list_t<n1, INT_LIST_5(n2, n3, n4, n5, n6) >
153 #define INT_LIST_7(n1, n2, n3, n4, n5, n6, n7) multi_bit_mask::int_list_t<n1, INT_LIST_6(n2, n3, n4, n5, n6, n7) >
154 #define INT_LIST_8(n1, n2, n3, n4, n5, n6, n7, n8) multi_bit_mask::int_list_t<n1, INT_LIST_7(n2, n3, n4, n5, n6, n7, n8) >
155
156 }
157
158
159
sample











