1 // Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // FIXME: merge with `bitvec`
15 pub type Word = usize;
17 /// `BitSlice` provides helper methods for treating a `[Word]`
20 fn clear_bit(&mut self, idx: usize) -> bool;
21 fn set_bit(&mut self, idx: usize) -> bool;
22 fn get_bit(&self, idx: usize) -> bool;
25 impl BitSlice for [Word] {
26 /// Clears bit at `idx` to 0; returns true iff this changed `self.`
28 fn clear_bit(&mut self, idx: usize) -> bool {
30 debug!("clear_bit: words={} idx={}",
31 bits_to_string(words, words.len() * mem::size_of::<Word>() * 8), idx);
32 let BitLookup { word, bit_in_word, bit_mask } = bit_lookup(idx);
33 debug!("word={} bit_in_word={} bit_mask=0x{:x}", word, bit_in_word, bit_mask);
34 let oldv = words[word];
35 let newv = oldv & !bit_mask;
40 /// Sets bit at `idx` to 1; returns true iff this changed `self.`
42 fn set_bit(&mut self, idx: usize) -> bool {
44 debug!("set_bit: words={} idx={}",
45 bits_to_string(words, words.len() * mem::size_of::<Word>() * 8), idx);
46 let BitLookup { word, bit_in_word, bit_mask } = bit_lookup(idx);
47 debug!("word={} bit_in_word={} bit_mask={}", word, bit_in_word, bit_mask);
48 let oldv = words[word];
49 let newv = oldv | bit_mask;
54 /// Extracts value of bit at `idx` in `self`.
56 fn get_bit(&self, idx: usize) -> bool {
58 let BitLookup { word, bit_mask, .. } = bit_lookup(idx);
59 (words[word] & bit_mask) != 0
64 /// An index of the word holding the bit in original `[Word]` of query.
66 /// Index of the particular bit within the word holding the bit.
68 /// Word with single 1-bit set corresponding to where the bit is located.
73 fn bit_lookup(bit: usize) -> BitLookup {
74 let word_bits = mem::size_of::<Word>() * 8;
75 let word = bit / word_bits;
76 let bit_in_word = bit % word_bits;
77 let bit_mask = 1 << bit_in_word;
78 BitLookup { word, bit_in_word, bit_mask }
81 pub fn bits_to_string(words: &[Word], bits: usize) -> String {
82 let mut result = String::new();
85 // Note: this is a little endian printout of bytes.
87 // i tracks how many bits we have printed so far.
89 for &word in words.iter() {
91 for _ in 0..mem::size_of::<Word>() { // for each byte in `v`:
92 let remain = bits - i;
93 // If less than a byte remains, then mask just that many bits.
94 let mask = if remain <= 8 { (1 << remain) - 1 } else { 0xFF };
95 assert!(mask <= 0xFF);
98 result.push_str(&format!("{}{:02x}", sep, byte));
100 if remain <= 8 { break; }
113 pub fn bitwise<Op:BitwiseOperator>(out_vec: &mut [Word],
116 assert_eq!(out_vec.len(), in_vec.len());
117 let mut changed = false;
118 for (out_elt, in_elt) in out_vec.iter_mut().zip(in_vec) {
119 let old_val = *out_elt;
120 let new_val = op.join(old_val, *in_elt);
122 changed |= old_val != new_val;
127 pub trait BitwiseOperator {
128 /// Applies some bit-operation pointwise to each of the bits in the two inputs.
129 fn join(&self, pred1: Word, pred2: Word) -> Word;
132 pub struct Intersect;
133 impl BitwiseOperator for Intersect {
135 fn join(&self, a: Word, b: Word) -> Word { a & b }
138 impl BitwiseOperator for Union {
140 fn join(&self, a: Word, b: Word) -> Word { a | b }
143 impl BitwiseOperator for Subtract {
145 fn join(&self, a: Word, b: Word) -> Word { a & !b }