@@ -28,6 +28,24 @@ const UNSET_BIT_MASK: [u8; 8] = [
28
28
255 - 128 ,
29
29
] ;
30
30
31
+ /// A bit set representing at most 8 bits with a underlying u8.
32
+ pub struct OneByteBitSet ( pub u8 ) ;
33
+
34
+ impl OneByteBitSet {
35
+ /// Create from a given boolean slice.
36
+ ///
37
+ /// The values in the `bits` whose index is greater than 8 will be ignored.
38
+ pub fn from_slice ( bits : & [ bool ] ) -> Self {
39
+ let mut v = 0u8 ;
40
+ for ( idx, set) in bits. iter ( ) . take ( 8 ) . map ( |v| * v as u8 ) . enumerate ( ) {
41
+ let ( _, bit_idx) = RoBitSet :: compute_byte_bit_index ( idx) ;
42
+ v |= set << bit_idx
43
+ }
44
+
45
+ Self ( v)
46
+ }
47
+ }
48
+
31
49
/// A basic implementation supporting read/write.
32
50
#[ derive( Debug , Default , Clone ) ]
33
51
pub struct BitSet {
@@ -46,19 +64,6 @@ impl BitSet {
46
64
}
47
65
}
48
66
49
- /// Create a u8 according to a given 8bits array.
50
- ///
51
- /// The values in the `bits` whose index is greater than 8 will be ignored.
52
- pub fn one_byte ( bits : & [ bool ] ) -> u8 {
53
- let mut v = 0u8 ;
54
- for ( idx, set) in bits. iter ( ) . take ( 8 ) . map ( |v| * v as u8 ) . enumerate ( ) {
55
- let ( _, bit_idx) = RoBitSet :: compute_byte_bit_index ( idx) ;
56
- v |= set << bit_idx
57
- }
58
-
59
- v
60
- }
61
-
62
67
/// Initialize a [`BitSet`] with all bits set.
63
68
pub fn all_set ( num_bits : usize ) -> Self {
64
69
Self {
@@ -185,6 +190,7 @@ mod tests {
185
190
use std:: assert_eq;
186
191
187
192
use super :: BitSet ;
193
+ use crate :: row:: bitset:: OneByteBitSet ;
188
194
189
195
#[ test]
190
196
fn test_set_op ( ) {
@@ -245,17 +251,17 @@ mod tests {
245
251
#[ test]
246
252
fn test_one_byte ( ) {
247
253
let bits = [ false , false , false , false , false , false ] ;
248
- assert_eq ! ( 0 , BitSet :: one_byte ( & bits) ) ;
254
+ assert_eq ! ( 0 , OneByteBitSet :: from_slice ( & bits) . 0 ) ;
249
255
250
256
let bits = [ true , false , false , false , false , false ] ;
251
- assert_eq ! ( 1 , BitSet :: one_byte ( & bits) ) ;
257
+ assert_eq ! ( 1 , OneByteBitSet :: from_slice ( & bits) . 0 ) ;
252
258
253
259
let bits = [ false , false , false , true , false , false , true , true ] ;
254
- assert_eq ! ( 128 + 64 + 8 , BitSet :: one_byte ( & bits) ) ;
260
+ assert_eq ! ( 128 + 64 + 8 , OneByteBitSet :: from_slice ( & bits) . 0 ) ;
255
261
256
262
let bits = [
257
263
false , false , false , false , false , false , true , true , true , true ,
258
264
] ;
259
- assert_eq ! ( 128 + 64 , BitSet :: one_byte ( & bits) ) ;
265
+ assert_eq ! ( 128 + 64 , OneByteBitSet :: from_slice ( & bits) . 0 ) ;
260
266
}
261
267
}
0 commit comments