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
pub trait BatchData<T> {
    type Batch;

    /// The number of items in the batch
    fn len(&self) -> usize;

    /// Return the current batch, and reset any internal state
    fn take_batch(&mut self) -> Self::Batch;

    /// Add a single item to the batch
    fn push_item(&mut self, item: T);

    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl<T> BatchData<T> for Vec<T> {
    type Batch = Self;

    fn len(&self) -> usize {
        self.len()
    }
    fn take_batch(&mut self) -> Self::Batch {
        std::mem::take(self)
    }
    fn push_item(&mut self, item: T) {
        self.push(item);
    }
}

pub struct BatchReduce<F, S> {
    reducer: F,
    state: S,
    len: usize,
}
impl<F, S> BatchReduce<F, S>
where
    S: Default,
{
    pub fn new(reducer: F) -> BatchReduce<F, S> {
        BatchReduce {
            reducer,
            state: S::default(),
            len: 0,
        }
    }
}
impl<F, S, T> BatchData<T> for BatchReduce<F, S>
where
    F: FnMut(&mut S, T),
    S: Default,
{
    type Batch = S;

    fn len(&self) -> usize {
        self.len
    }

    fn take_batch(&mut self) -> Self::Batch {
        self.len = 0;
        std::mem::take(&mut self.state)
    }

    fn push_item(&mut self, item: T) {
        self.len += 1;
        (self.reducer)(&mut self.state, item);
    }
}