forked from IBM/sliding-window-aggregators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
52 lines (48 loc) · 1.21 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
49
50
51
52
use std::collections::VecDeque;
use alga::general::AbstractMagma;
use alga::general::Identity;
use alga::general::TwoSidedInverse;
use crate::FifoWindow;
use crate::ops::AggregateOperator;
use crate::ops::AggregateGroup;
#[derive(Clone)]
pub struct SoE<BinOp>
where
BinOp: AggregateGroup<BinOp> + AggregateOperator + Clone
{
stack: VecDeque<BinOp::Partial>,
agg: BinOp::Partial,
}
impl<BinOp> FifoWindow<BinOp> for SoE<BinOp>
where
BinOp: AggregateGroup<BinOp> + AggregateOperator + Clone
{
fn new() -> Self {
Self {
stack: VecDeque::new(),
agg: BinOp::Partial::identity(),
}
}
fn name() -> &'static str {
"soe"
}
fn push(&mut self, val: BinOp::In) {
let lifted = BinOp::lift(val);
self.agg = self.agg.operate(&lifted);
self.stack.push_back(lifted);
}
fn pop(&mut self) {
if let Some(top) = self.stack.pop_front() {
self.agg = self.agg.operate(&top.two_sided_inverse());
}
}
fn query(&self) -> BinOp::Out {
BinOp::lower(&self.agg.clone())
}
fn len(&self) -> usize {
self.stack.len()
}
fn is_empty(&self) -> bool {
self.stack.is_empty()
}
}