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
use crate::{List, ListOrSingle, Lists};

pub trait UpsampleFill<T, V, Y>: Lists<T>
where
    V: List<T>,
    Y: List<T>
{
    fn upsample_fill(self, interleave: V, phase: usize) -> Self::RowsMapped<Y>;
}

impl<T, V, L> UpsampleFill<T, V, Vec<T>> for L
where
    V: List<T>,
    L: Lists<T, RowOwned: List<T>>,
    T: Clone
{
    fn upsample_fill(self, interleave: V, mut phase: usize) -> Self::RowsMapped<Vec<T>>
    {
        let interleave = interleave.into_vec();

        phase %= interleave.len() + 1;

        self.map_rows_into_owned(|x| {
            x.into_vec()
                .into_iter()
                .flat_map(|x| {
                    let mut y = interleave.clone();
                    y.insert(phase, x);
                    y
                }).collect()
        })
    }
}

impl<T, L, V> UpsampleFill<T, V, [T; L::WIDTH*(V::WIDTH + 1)]> for L
where
    V: List<T, Width = usize>,
    L: Lists<T, RowOwned: List<T>, Width = usize>,
    T: Clone
{
    fn upsample_fill(self, interleave: V, mut phase: usize) -> Self::RowsMapped<[T; L::WIDTH*(V::WIDTH + 1)]>
    {
        let interleave = interleave.into_vec();

        phase %= interleave.len() + 1;

        self.map_rows_into_owned(|x| {
            x.into_vec()
                .into_iter()
                .flat_map(|x| {
                    let mut y = interleave.clone();
                    y.insert(phase, x);
                    y
                }).collect::<Vec<_>>()
                .try_into()
                .ok()
                .unwrap()
        })
    }
}