forked from IBM/sliding-window-aggregators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
49 lines (44 loc) · 1.08 KB
/
mod.rs
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
use std::collections::VecDeque;
use alga::general::AbstractMagma;
use alga::general::Identity;
use crate::FifoWindow;
use crate::ops::AggregateOperator;
use crate::ops::AggregateMonoid;
#[derive(Clone)]
pub struct ReCalc<BinOp>
where
BinOp: AggregateMonoid<BinOp> + AggregateOperator + Clone
{
stack: VecDeque<BinOp::Partial>,
}
impl<BinOp> FifoWindow<BinOp> for ReCalc<BinOp>
where
BinOp: AggregateMonoid<BinOp> + AggregateOperator + Clone
{
fn new() -> Self {
Self {
stack: VecDeque::new(),
}
}
fn name() -> &'static str {
"recalc"
}
fn push(&mut self, val: BinOp::In) {
self.stack.push_back(BinOp::lift(val));
}
fn pop(&mut self) {
self.stack.pop_front();
}
fn query(&self) -> BinOp::Out {
let agg = self.stack
.iter()
.fold(BinOp::Partial::identity(), |acc, elem| acc.operate(&elem));
BinOp::lower(&agg)
}
fn len(&self) -> usize {
self.stack.len()
}
fn is_empty(&self) -> bool {
self.stack.is_empty()
}
}