-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathTwoStacks.hpp
97 lines (81 loc) · 2.51 KB
/
TwoStacks.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef __TWOSTACKS_H_
#define __TWOSTACKS_H_
#include<deque>
#include<iostream>
#include<iterator>
#include<cassert>
namespace twostacks {
using namespace std;
template<typename valT, typename aggT>
class __AggT {
public:
valT _val;
aggT _agg;
__AggT() {}
__AggT(valT val_, aggT agg_)
: _val(val_), _agg(agg_) {}
};
template<typename binOpFunc,
typename stackT=deque<__AggT<typename binOpFunc::Partial, typename binOpFunc::Partial>>>
class Aggregate {
public:
typedef typename binOpFunc::In inT;
typedef typename binOpFunc::Partial aggT;
typedef typename binOpFunc::Out outT;
typedef __AggT<aggT, aggT> AggT;
Aggregate(binOpFunc binOp_, aggT identE_)
: _front(), _back(), _binOp(binOp_), _identE(identE_) {}
size_t size() { return _front.size() + _back.size(); }
void insert(inT v) {
_IFDEBUG(std::cerr << "inserting " << v << std::endl;);
auto prev = _back.size()>0?_back.back()._agg:_identE;
aggT lifted = _binOp.lift(v);
_back.push_back(AggT(lifted, _binOp.combine(prev, lifted)));
}
void evict() {
_IFDEBUG(std::cerr << "evicting" << std::endl;);
if (_front.empty()) {
while (!_back.empty()) {
aggT v = _back.back()._val; _back.pop_back(); // eject from back
auto a = _front.size()>0?_front.back()._agg:_identE;
_front.push_back(AggT(v, _binOp.combine(v, a)));
}
}
_front.pop_back();
}
outT query() {
auto bp = _back.size()>0?_back.back()._agg:_identE;
auto fp = _front.size()>0?_front.back()._agg:_identE;
return _binOp.lower(_binOp.combine(fp, bp));
}
outT naive_query() {
aggT accum = _identE;
for (auto it=_front.rbegin(); it!=_front.rend(); it++){
accum = _binOp.combine(accum, it->_val);
}
for (iterT it=_back.begin(); it!=_back.end(); it++){
accum = _binOp.combine(accum, it->_val);
}
return _binOp.lower(accum);
}
private:
stackT _front, _back;
typedef typename stackT::iterator iterT;
// the binary operator deck
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