bv/adapter/
bit_fill.rs

1use Bits;
2use BlockType;
3use iter::BlockIter;
4
5use traits::get_masked_block;
6
7/// Emulates a constant-valued bit-vector of a given size.
8#[derive(Debug, Clone)]
9pub struct BitFill<Block> {
10    len: u64,
11    block: Block,
12}
13
14impl<Block: BlockType> Bits for BitFill<Block> {
15    type Block = Block;
16
17    fn bit_len(&self) -> u64 {
18        self.len
19    }
20
21    fn get_bit(&self, position: u64) -> bool {
22        assert!(position < self.len,
23                "BitFill::get_bit: out of bounds");
24        self.block != Block::zero()
25    }
26
27    fn get_block(&self, position: usize) -> Self::Block {
28        assert!(position < self.block_len(),
29                "BitFill::get_block: out of bounds");
30        get_masked_block(self, position)
31    }
32
33    fn get_raw_block(&self, position: usize) -> Self::Block {
34        assert!(position < self.block_len(),
35                "BitFill::get_raw_block: out of bounds");
36        self.block
37    }
38
39    fn get_bits(&self, position: u64, len: usize) -> Self::Block {
40        assert!(position + (len as u64) <= self.bit_len(),
41                "BitFill::get_bits: out of bounds");
42        self.block
43    }
44}
45
46impl<Block: BlockType> BitFill<Block> {
47    /// Constructs a compact bit-vector-like of `len` 0s.
48    pub fn zeroes(len: u64) -> Self {
49        BitFill {
50            len,
51            block: Block::zero(),
52        }
53    }
54
55    /// Constructs a compact bit-vector-like of `len` 1s.
56    pub fn ones(len: u64) -> Self {
57        BitFill {
58            len,
59            block: !Block::zero(),
60        }
61    }
62}
63
64impl<T: Bits> PartialEq<T> for BitFill<T::Block> {
65    fn eq(&self, other: &T) -> bool {
66        BlockIter::new(self) == BlockIter::new(other)
67    }
68}
69
70impl_index_from_bits! {
71    impl[Block: BlockType] Index<u64> for BitFill<Block>;
72}
73
74impl_bit_sliceable_adapter! {
75    impl[Block: BlockType] BitSliceable for BitFill<Block>;
76    impl['a, Block: BlockType] BitSliceable for &'a BitFill<Block>;
77}