nostd_bv/traits/
bits_push.rs

1use super::BitsMut;
2use crate::storage::BlockType;
3use alloc::vec::Vec;
4
5/// Bit vector operations that change the length.
6pub trait BitsPush: BitsMut {
7    /// Adds the given bit to the end of the bit vector.
8    fn push_bit(&mut self, value: bool);
9
10    /// Removes and returns the last bit, if any.
11    fn pop_bit(&mut self) -> Option<bool>;
12
13    /// Pushes `value` 0 or more times until the size of the bit
14    /// vector is block-aligned.
15    fn align_block(&mut self, value: bool) {
16        while Self::Block::mod_nbits(self.bit_len()) != 0 {
17            self.push_bit(value);
18        }
19    }
20
21    /// Pushes the given block onto the end of the bit vector.
22    ///
23    /// If the end of the bit vector is not currently block-aligned,
24    /// it pads with 0s up to the next block before pushing.
25    ///
26    /// The default implementation pushes the block one bit at a time;
27    /// override it with something more efficient.
28    fn push_block(&mut self, mut value: Self::Block) {
29        self.align_block(false);
30
31        for _ in 0..Self::Block::nbits() {
32            self.push_bit(value & Self::Block::one() != Self::Block::zero());
33            value = value >> 1;
34        }
35    }
36}
37
38impl BitsPush for Vec<bool> {
39    fn push_bit(&mut self, value: bool) {
40        self.push(value);
41    }
42
43    fn pop_bit(&mut self) -> Option<bool> {
44        self.pop()
45    }
46}