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