precomputed_map/
seq.rs

1use core::marker::PhantomData;
2use crate::store::{ AsData, AccessSeq };
3
4
5pub struct PositionSeq<SEQ, BUF>(PhantomData<(SEQ, BUF)>);
6
7impl<
8    const B: usize,
9    SEQ,
10    BUF,
11> AccessSeq for PositionSeq<SEQ, BUF>
12where
13    SEQ: AccessSeq<Item = u32>,
14    BUF: AsData<Data = [u8; B]>
15{
16    type Item = &'static [u8];
17
18    const LEN: usize = SEQ::LEN;
19
20    #[inline(always)]
21    fn index(index: usize) -> Option<Self::Item> {
22        let start: usize = match index.checked_sub(1) {
23            Some(index) => SEQ::index(index)?.try_into().unwrap(),
24            None => 0
25        };
26        let end: usize = SEQ::index(index)?
27            .try_into()
28            .unwrap();
29        BUF::as_data().get(start..end)
30    }
31}
32
33pub struct PooledSeq<SEQ, ID>(PhantomData<(SEQ, ID)>);
34
35pub trait PooledId: From<u32> + Copy {
36    fn get(self) -> Option<&'static [u8]>;
37}
38
39impl<SEQ, ID> AccessSeq for PooledSeq<SEQ, ID>
40where
41    SEQ: AccessSeq<Item = u32>,
42    ID: PooledId
43{
44    type Item = &'static [u8];
45    const LEN: usize = SEQ::LEN;
46
47    #[inline(always)]
48    fn index(index: usize) -> Option<Self::Item> {
49        let id = SEQ::index(index)?;
50        ID::from(id).get()
51    }
52}
53
54#[inline(always)]
55pub fn pooled_unpack(n: u32) -> (usize, usize) {
56    const BIT: usize = 24;
57
58    let offset = (n & ((1 << BIT) - 1)).try_into().unwrap();
59    let len = (n >> BIT).try_into().unwrap();
60    
61    (offset, len)
62}