precomputed_map/
seq.rs

1use crate::aligned::AlignedArray;
2use crate::store::ConstSlice;
3use crate::AccessSeq;
4use crate::store::AsData;
5
6pub struct List<'data, const N: usize, T>(pub &'data [T; N]);
7pub struct RefList<'data, const N: usize, T>(pub &'data [T; N]);
8
9pub struct CompactSeq<
10    'data,
11    const O: usize,
12    const L: usize,
13    SEQ,
14    BUF: ?Sized,
15> {
16    seq: SEQ,
17    data: ConstSlice<'data, O, L, BUF>,
18}
19
20impl<
21    'data,
22    const O: usize,
23    const L: usize,
24    SEQ,
25    BUF: ?Sized,
26> CompactSeq<'data, O, L, SEQ, BUF> {
27    pub const fn new(seq: SEQ, data: ConstSlice<'data, O, L, BUF>) -> Self {
28        CompactSeq { seq, data }
29    }
30}
31
32impl<'data, const N: usize, T: Copy> AccessSeq<'data> for List<'data, N, T> {
33    type Item = T;
34
35    const LEN: usize = N;
36
37    #[inline]
38    fn index(&self, index: usize) -> Self::Item {
39        self.0[index]
40    }
41}
42
43impl<'data, const N: usize, T> AccessSeq<'data> for RefList<'data, N, T> {
44    type Item = &'data T;
45
46    const LEN: usize = N;
47
48    #[inline]
49    fn index(&self, index: usize) -> Self::Item {
50        &self.0[index]
51    }
52}
53
54impl<
55    'data,
56    const B: usize,
57    const O: usize,
58    const L: usize,
59    SEQ,
60> AccessSeq<'data> for CompactSeq<'data, O, L, SEQ, [u8; B]>
61where
62    SEQ: AccessSeq<'data, Item = u32>,
63{
64    type Item = &'data [u8];
65
66    const LEN: usize = SEQ::LEN;
67
68    #[inline]
69    fn index(&self, index: usize) -> Self::Item {
70        let start: usize = index.checked_sub(1)
71            .map(|index| self.seq.index(index))
72            .unwrap_or_default()
73            .try_into()
74            .unwrap();
75        let end: usize = self.seq.index(index)
76            .try_into()
77            .unwrap();
78        &self.data.as_data()[start..end]
79    }
80}
81
82impl<
83    'data,
84    const O: usize,
85    const L: usize,
86    SEQ,
87> AccessSeq<'data> for CompactSeq<'data, O, L, SEQ, str>
88where
89    SEQ: AccessSeq<'data, Item = u32>,
90{
91    type Item = &'data str;
92
93    const LEN: usize = SEQ::LEN;
94
95    #[inline]
96    fn index(&self, index: usize) -> Self::Item {
97        let start: usize = index.checked_sub(1)
98            .map(|index| self.seq.index(index))
99            .unwrap_or_default()
100            .try_into()
101            .unwrap();
102        let end: usize = self.seq.index(index)
103            .try_into()
104            .unwrap();
105        &self.data.as_data()[start..end]
106    }
107}
108
109impl<
110    'data,
111    const B: usize,
112    DATA
113> AccessSeq<'data> for AlignedArray<B, u32, DATA>
114where
115    DATA: AsData<Data = &'data [u8; B]>
116{
117    type Item = u32;
118
119    const LEN: usize = <AlignedArray<B, u32, DATA>>::LEN;
120
121    #[inline]
122    fn index(&self, index: usize) -> Self::Item {
123        self.get(index).unwrap()
124    }
125}