-
Notifications
You must be signed in to change notification settings - Fork 16
/
ReCalc.hpp
72 lines (59 loc) · 1.51 KB
/
ReCalc.hpp
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
#ifndef __RECALC_HPP_
#define __RECALC_HPP_
#include <deque>
#include <iostream>
#include <iterator>
#include <cassert>
#ifdef DEBUG
#define _IFDEBUG(x) x
#else
#define _IFDEBUG(x)
#endif
namespace recalc {
using namespace std;
template<typename binOpFunc,
typename queueT=deque<typename binOpFunc::In>>
class Aggregate {
public:
typedef typename binOpFunc::In inT;
typedef typename binOpFunc::Partial aggT;
typedef typename binOpFunc::Out outT;
Aggregate(binOpFunc binOp_, aggT identE_):
_q(), _binOp(binOp_), _identE(identE_)
{}
size_t size() { return _q.size(); }
void insert(inT v) {
_IFDEBUG(std::cerr << "inserting " << v << std::endl;);
_q.push_back(v);
}
void evict() {
_IFDEBUG(std::cerr << "evicting" << std::endl;);
_q.pop_front();
}
outT query() {
aggT accum = _identE;
for (auto it=_q.begin(); it!=_q.end(); it++) {
_binOp.recalc_combine(accum, *it);
}
return _binOp.lower(accum);
}
private:
queueT _q;
typedef typename queueT::iterator iterT;
binOpFunc _binOp;
aggT _identE;
};
template <class BinaryFunction, class T>
Aggregate<BinaryFunction> make_aggregate(BinaryFunction f, T elem) {
return Aggregate<BinaryFunction>(f, elem);
}
template <typename BinaryFunction>
struct MakeAggregate {
template <typename T>
Aggregate<BinaryFunction> operator()(T elem) {
BinaryFunction f;
return make_aggregate(f, elem);
}
};
}
#endif