10
10
11
11
use std:: mem;
12
12
13
- /// `BitSlice` provides helper methods for treating a `[usize]`
13
+ pub type Word = usize ;
14
+
15
+ /// `BitSlice` provides helper methods for treating a `[Word]`
14
16
/// as a bitvector.
15
17
pub trait BitSlice {
16
18
fn clear_bit ( & mut self , idx : usize ) -> bool ;
17
19
fn set_bit ( & mut self , idx : usize ) -> bool ;
18
20
fn get_bit ( & self , idx : usize ) -> bool ;
19
21
}
20
22
21
- impl BitSlice for [ usize ] {
23
+ impl BitSlice for [ Word ] {
22
24
/// Clears bit at `idx` to 0; returns true iff this changed `self.`
23
25
fn clear_bit ( & mut self , idx : usize ) -> bool {
24
26
let words = self ;
25
27
debug ! ( "clear_bit: words={} idx={}" ,
26
- bits_to_string( words, words. len( ) * mem:: size_of:: <usize >( ) ) , bit_str( idx) ) ;
28
+ bits_to_string( words, words. len( ) * mem:: size_of:: <Word >( ) ) , bit_str( idx) ) ;
27
29
let BitLookup { word, bit_in_word, bit_mask } = bit_lookup ( idx) ;
28
30
debug ! ( "word={} bit_in_word={} bit_mask={}" , word, bit_in_word, bit_mask) ;
29
31
let oldv = words[ word] ;
@@ -36,7 +38,7 @@ impl BitSlice for [usize] {
36
38
fn set_bit ( & mut self , idx : usize ) -> bool {
37
39
let words = self ;
38
40
debug ! ( "set_bit: words={} idx={}" ,
39
- bits_to_string( words, words. len( ) * mem:: size_of:: <usize >( ) ) , bit_str( idx) ) ;
41
+ bits_to_string( words, words. len( ) * mem:: size_of:: <Word >( ) ) , bit_str( idx) ) ;
40
42
let BitLookup { word, bit_in_word, bit_mask } = bit_lookup ( idx) ;
41
43
debug ! ( "word={} bit_in_word={} bit_mask={}" , word, bit_in_word, bit_mask) ;
42
44
let oldv = words[ word] ;
@@ -54,31 +56,31 @@ impl BitSlice for [usize] {
54
56
}
55
57
56
58
struct BitLookup {
57
- /// An index of the word holding the bit in original `[usize ]` of query.
59
+ /// An index of the word holding the bit in original `[Word ]` of query.
58
60
word : usize ,
59
61
/// Index of the particular bit within the word holding the bit.
60
62
bit_in_word : usize ,
61
63
/// Word with single 1-bit set corresponding to where the bit is located.
62
- bit_mask : usize ,
64
+ bit_mask : Word ,
63
65
}
64
66
65
67
#[ inline]
66
68
fn bit_lookup ( bit : usize ) -> BitLookup {
67
- let usize_bits = mem:: size_of :: < usize > ( ) * 8 ;
68
- let word = bit / usize_bits ;
69
- let bit_in_word = bit % usize_bits ;
69
+ let word_bits = mem:: size_of :: < Word > ( ) * 8 ;
70
+ let word = bit / word_bits ;
71
+ let bit_in_word = bit % word_bits ;
70
72
let bit_mask = 1 << bit_in_word;
71
73
BitLookup { word : word, bit_in_word : bit_in_word, bit_mask : bit_mask }
72
74
}
73
75
74
76
75
- fn bit_str ( bit : usize ) -> String {
77
+ fn bit_str ( bit : Word ) -> String {
76
78
let byte = bit >> 3 ;
77
79
let lobits = 1 << ( bit & 0b111 ) ;
78
80
format ! ( "[{}:{}-{:02x}]" , bit, byte, lobits)
79
81
}
80
82
81
- pub fn bits_to_string ( words : & [ usize ] , bits : usize ) -> String {
83
+ pub fn bits_to_string ( words : & [ Word ] , bits : usize ) -> String {
82
84
let mut result = String :: new ( ) ;
83
85
let mut sep = '[' ;
84
86
0 commit comments