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
use crate::{Operator, TickValue, Tickable};
use arrayvec::ArrayVec;

/// [`Array`] combinator.
#[derive(Debug, Clone)]
pub struct Array<I, P, const LEN: usize> {
    ops: ArrayVec<P, LEN>,
    _input: core::marker::PhantomData<fn() -> I>,
}

impl<I, T, P, const LEN: usize> Operator<I> for Array<I, P, LEN>
where
    I: Tickable<Value = [T; LEN]>,
    P: Operator<TickValue<T>>,
    P::Output: Tickable,
{
    type Output = TickValue<[<P::Output as Tickable>::Value; LEN]>;

    fn next(&mut self, input: I) -> Self::Output {
        let TickValue { tick, value } = input.into_tick_value();
        let mut idx = 0;
        let res = value.map(|x| {
            let x = TickValue { tick, value: x };
            let y = self.ops[idx].next(x).into_tick_value().value;
            idx += 1;
            y
        });
        TickValue { tick, value: res }
    }
}

/// Apply a ticked operator on an array input to get an array of output.
/// ```
/// use indicator::*;
///
/// fn plus_one<M, I, Q>(mode: M) -> impl Operator<I, Output = TickValue<[usize; 3]>>
/// where
///     M: TumblingWindow,
///     I: Tickable<Value = usize>,
///     Q: QueueCapAtLeast<0, Item = usize>,
/// {
///     cached(mode, |_q: &Q, _n, x: &usize| [*x, *x, *x])
///         .then(array_t(|| map_t(|x| x + 1)))
/// }
/// ```
pub fn array_t<F, I, T, P, const LEN: usize>(mut f: F) -> Array<I, P, LEN>
where
    I: Tickable<Value = [T; LEN]>,
    P: Operator<TickValue<T>>,
    P::Output: Tickable,
    F: FnMut() -> P,
{
    let mut ops = ArrayVec::new();
    for _ in 0..LEN {
        ops.push((f)())
    }
    Array {
        ops,
        _input: core::marker::PhantomData::default(),
    }
}