1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#![no_std]
use list_fn::{Empty, FlatScanFn, Id, ListFn, ListSome, ListState};
use uints::UInt;

// LSB first bit vector.
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub struct BitVec<T: UInt> {
    pub array: T,
    pub size: u8,
}

impl<T: UInt> BitVec<T> {
    pub fn new_full(array: T) -> Self {
        Self::new(array, T::BITS)
    }
    pub fn new(array: T, size: u8) -> Self {
        BitVec { array, size }
    }
    pub fn concat(self, v: Self) -> Self {
        BitVec {
            array: self.array | (v.array << self.size),
            size: self.size + v.size,
        }
    }
}

impl<T: UInt> ListFn for BitVec<T> {
    type Item = bool;
    type End = ();
    fn next(self) -> ListState<Self> {
        match self.size {
            0 => ListState::End(()),
            size => ListState::Some(ListSome {
                first: self.array & T::ONE != T::ZERO,
                next: BitVec::new(self.array >> 1, size - 1),
            }),
        }
    }
}

pub const fn bit_vec16_new(array: u16, size: u8) -> BitVec16 {
    BitVec16 { array, size }
}

pub type BitVec64 = BitVec<u64>;

pub type BitVec32 = BitVec<u32>;

pub type BitVec16 = BitVec<u16>;

#[derive(Default)]
pub struct ByteList(BitVec32);

impl ListFn for ByteList {
    type Item = u8;
    type End = ByteList;
    fn next(self) -> ListState<Self> {
        if self.0.size < 8 {
            ListState::End(self)
        } else {
            ListState::Some(ListSome {
                first: self.0.array as u8,
                next: ByteList(BitVec::new(self.0.array >> 8, self.0.size - 8)),
            })
        }
    }
}

impl FlatScanFn for ByteList {
    type InputItem = BitVec16;
    type InputResult = ();
    type OutputList = Self;
    type EndList = Empty<u8, Id<BitVec32>>;
    fn map_item(self, item: BitVec16) -> Self::OutputList {
        ByteList(self.0.concat(BitVec32::new(item.array as u32, item.size)))
    }
    fn map_result(self, _: ()) -> Self::EndList {
        Empty::new(Id::new(self.0))
    }
}