nostd_bv/adapter/
bit_fill.rs

1use crate::iter::BlockIter;
2use crate::Bits;
3use crate::BlockType;
4
5use crate::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, "BitFill::get_bit: out of bounds");
23        self.block != Block::zero()
24    }
25
26    fn get_block(&self, position: usize) -> Self::Block {
27        assert!(
28            position < self.block_len(),
29            "BitFill::get_block: out of bounds"
30        );
31        get_masked_block(self, position)
32    }
33
34    fn get_raw_block(&self, position: usize) -> Self::Block {
35        assert!(
36            position < self.block_len(),
37            "BitFill::get_raw_block: out of bounds"
38        );
39        self.block
40    }
41
42    fn get_bits(&self, position: u64, len: usize) -> Self::Block {
43        assert!(
44            position + (len as u64) <= self.bit_len(),
45            "BitFill::get_bits: out of bounds"
46        );
47        self.block
48    }
49}
50
51impl<Block: BlockType> BitFill<Block> {
52    /// Constructs a compact bit-vector-like of `len` 0s.
53    pub fn zeroes(len: u64) -> Self {
54        BitFill {
55            len,
56            block: Block::zero(),
57        }
58    }
59
60    /// Constructs a compact bit-vector-like of `len` 1s.
61    pub fn ones(len: u64) -> Self {
62        BitFill {
63            len,
64            block: !Block::zero(),
65        }
66    }
67}
68
69impl<T: Bits> PartialEq<T> for BitFill<T::Block> {
70    fn eq(&self, other: &T) -> bool {
71        BlockIter::new(self) == BlockIter::new(other)
72    }
73}
74
75impl_index_from_bits! {
76    impl[Block: BlockType] Index<u64> for BitFill<Block>;
77}
78
79impl_bit_sliceable_adapter! {
80    impl[Block: BlockType] BitSliceable for BitFill<Block>;
81    impl['a, Block: BlockType] BitSliceable for &'a BitFill<Block>;
82}