@@ -826,7 +826,7 @@ impl<B: BitBlock> BitVec<B> {
826826 pub fn or ( & mut self , other : & Self ) -> bool {
827827 self . ensure_invariant ( ) ;
828828 debug_assert ! ( other. is_last_block_fixed( ) ) ;
829- self . process ( other, |w1, w2| ( w1 | w2) )
829+ self . process ( other, |w1, w2| w1 | w2)
830830 }
831831
832832 /// Calculates the bitwise `and` of two bitvectors.
@@ -857,7 +857,7 @@ impl<B: BitBlock> BitVec<B> {
857857 pub fn and ( & mut self , other : & Self ) -> bool {
858858 self . ensure_invariant ( ) ;
859859 debug_assert ! ( other. is_last_block_fixed( ) ) ;
860- self . process ( other, |w1, w2| ( w1 & w2) )
860+ self . process ( other, |w1, w2| w1 & w2)
861861 }
862862
863863 /// Calculates the difference between two bitvectors.
@@ -896,7 +896,7 @@ impl<B: BitBlock> BitVec<B> {
896896 pub fn difference ( & mut self , other : & Self ) -> bool {
897897 self . ensure_invariant ( ) ;
898898 debug_assert ! ( other. is_last_block_fixed( ) ) ;
899- self . process ( other, |w1, w2| ( w1 & !w2) )
899+ self . process ( other, |w1, w2| w1 & !w2)
900900 }
901901
902902 /// Calculates the xor of two bitvectors.
@@ -927,7 +927,7 @@ impl<B: BitBlock> BitVec<B> {
927927 pub fn xor ( & mut self , other : & Self ) -> bool {
928928 self . ensure_invariant ( ) ;
929929 debug_assert ! ( other. is_last_block_fixed( ) ) ;
930- self . process ( other, |w1, w2| ( w1 ^ w2) )
930+ self . process ( other, |w1, w2| w1 ^ w2)
931931 }
932932
933933 /// Calculates the nand of two bitvectors.
@@ -1322,30 +1322,35 @@ impl<B: BitBlock> BitVec<B> {
13221322 /// assert_eq!(bv.to_bytes(), [0b00100000, 0b10000000]);
13231323 /// ```
13241324 pub fn to_bytes ( & self ) -> Vec < u8 > {
1325+ static REVERSE_TABLE : [ u8 ; 256 ] = {
1326+ let mut tbl = [ 0u8 ; 256 ] ;
1327+ let mut i: u8 = 0 ;
1328+ loop {
1329+ tbl[ i as usize ] = i. reverse_bits ( ) ;
1330+ if i == 255 {
1331+ break ;
1332+ }
1333+ i += 1 ;
1334+ }
1335+ tbl
1336+ } ;
13251337 self . ensure_invariant ( ) ;
1326- // Oh lord, we're mapping this to bytes bit-by-bit!
1327- fn bit < B : BitBlock > ( bit_vec : & BitVec < B > , byte : usize , bit : usize ) -> u8 {
1328- let offset = byte * 8 + bit;
1329- if offset >= bit_vec. nbits {
1330- 0
1331- } else {
1332- ( bit_vec[ offset] as u8 ) << ( 7 - bit)
1338+
1339+ let len = self . nbits / 8 + if self . nbits % 8 == 0 { 0 } else { 1 } ;
1340+ let mut result = Vec :: with_capacity ( len) ;
1341+
1342+ for byte_idx in 0 ..len {
1343+ let mut byte = 0u8 ;
1344+ for bit_idx in 0 ..8 {
1345+ let offset = byte_idx * 8 + bit_idx;
1346+ if offset < self . nbits && self [ offset] {
1347+ byte |= 1 << bit_idx;
1348+ }
13331349 }
1350+ result. push ( REVERSE_TABLE [ byte as usize ] ) ;
13341351 }
13351352
1336- let len = self . nbits / 8 + if self . nbits % 8 == 0 { 0 } else { 1 } ;
1337- ( 0 ..len)
1338- . map ( |i| {
1339- bit ( self , i, 0 )
1340- | bit ( self , i, 1 )
1341- | bit ( self , i, 2 )
1342- | bit ( self , i, 3 )
1343- | bit ( self , i, 4 )
1344- | bit ( self , i, 5 )
1345- | bit ( self , i, 6 )
1346- | bit ( self , i, 7 )
1347- } )
1348- . collect ( )
1353+ result
13491354 }
13501355
13511356 /// Compares a `BitVec` to a slice of `bool`s.
0 commit comments