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
use std::marker::PhantomData;

use crate::lang::lattice::{LatticeRepr, Merge};

#[derive(Debug)]
pub struct BatchJoinState<L>
where
    L: LatticeRepr,
    L::Repr: Default,
{
    state: L::Repr,
}

impl<L> Default for BatchJoinState<L>
where
    L: LatticeRepr,
    L::Repr: Default,
{
    fn default() -> Self {
        Self {
            state: Default::default(),
        }
    }
}

pub struct BatchJoin<'a, Buf, Stream, L, Update, Tick>
where
    Buf: Iterator<Item = Update::Repr>,
    Stream: Iterator<Item = Tick>,
    Update: LatticeRepr,
    L: LatticeRepr + Merge<Update>,
    L::Repr: Default,
{
    buf: Buf,
    stream: Stream,
    state: &'a mut BatchJoinState<L>,
    _marker: PhantomData<(Update, Tick)>,
}

impl<'a, Buf, Stream, L, Update, Tick> Iterator for BatchJoin<'a, Buf, Stream, L, Update, Tick>
where
    Buf: Iterator<Item = Update::Repr>,
    Stream: Iterator<Item = Tick>,
    Update: LatticeRepr,
    L: LatticeRepr + Merge<Update>,
    L::Repr: Default,
{
    type Item = (Tick, L::Repr);

    fn next(&mut self) -> Option<Self::Item> {
        for p in &mut self.buf {
            <L as Merge<Update>>::merge(&mut self.state.state, p);
        }

        self.stream
            .next()
            .map(|t| (t, std::mem::take(&mut self.state.state)))
    }
}
impl<'a, Buf, Stream, L, Update, Tick> BatchJoin<'a, Buf, Stream, L, Update, Tick>
where
    Buf: Iterator<Item = Update::Repr>,
    Stream: Iterator<Item = Tick>,
    Update: LatticeRepr,
    L: LatticeRepr + Merge<Update>,
    L::Repr: Default,
{
    pub fn new(buf: Buf, stream: Stream, state: &'a mut BatchJoinState<L>) -> Self {
        Self {
            buf,
            stream,
            state,
            _marker: PhantomData,
        }
    }
}