-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathAMTA.hpp
384 lines (349 loc) · 11.3 KB
/
AMTA.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#pragma once
#include <cassert>
#include <deque>
#include <iostream>
#include <ostream>
#include <vector>
#include <iterator>
/* This file contains an implementation of our interpretation of the Amortized
* MTA data structure from the following paper:
*
* Álvaro Villalba, Josep Lluís Berral, and David Carrera
*
* Constant-Time Sliding Window Framework with Reduced Memory Footprint and
* Efficient Bulk Evictions
*
* IEEE Transactions on Parallel and Distributed Systems
* (Volume: 30, Issue: 3, March 1 2019)
*
* https://doi.org/10.1109/TPDS.2018.2868960
* */
namespace amta {
using namespace std;
constexpr bool PRINT_DEBUG = false;
template <typename _timeT, typename binOpFunc>
class Aggregate {
public:
typedef typename binOpFunc::In inT;
typedef typename binOpFunc::Partial aggT;
typedef typename binOpFunc::Out outT;
typedef _timeT timeT;
private:
/* Node is an internal binary-tree node structure and as such, a node
* can have at most two children.
*
* o The "arity" field can be 0, 1, 2, -1, indicating how many children it
* has, where -1 means one child on the right (left has been popped).
*
* o For i = 0, 1, agg[i] stores the aggregated value of the subtree
* pointed to by children[i]--and times[i] is the largest timestamp in
* that subtree.
* */
struct Node {
aggT agg[2];
timeT times[2];
Node* children[2];
Node* parent;
int arity;
Node(aggT a, timeT t, Node* left) { init(a, t, left); }
void init(aggT a, timeT t, Node *left) {
parent = NULL, arity = 0;
push_back(a, t, left);
}
void push_back(aggT a, timeT t, Node *child) {
assert (arity == 0 || arity == 1);
agg[arity] = a; times[arity] = t; children[arity] = child; arity++;
if (child != NULL) child->parent = this;
}
Node* pop_front() {
assert(arity != 0);
if (abs(arity) == 1) {
arity = 0;
return this->children[1];
} else {
arity = -1;
return this->children[0];
}
}
bool leftPopped() { return arity == -1; }
bool rightEmpty() { return arity == 1; }
bool full() { return arity == 2; }
bool isLeaf() { return arity == 0; }
ostream& printNode(ostream& os) const {
os << "[";
for (int i = 0; i < arity; i++)
os << this->times[i] << "/" << this->agg[i] << ",";
if (arity == -1)
os << "-, " << this->times[1] << "/" << this->agg[1];
os << "]";
return os;
}
};
friend inline std::ostream& operator<<(std::ostream& os, Node const& x) {
return x.printNode(os);
}
binOpFunc _binOp;
size_t _size;
vector<Node *> _tails;
Node* _frontNode; // ptr to the node storing the oldest elt
deque<aggT> _frontStack;
aggT _identE, _frontSum, _backSum;
vector<Node *> _freeList;
public:
Aggregate(binOpFunc binOp, aggT identE_)
: _binOp(binOp), _size(0), _tails(), _frontNode(NULL),
_frontStack(), _identE(identE_), _frontSum(identE_), _backSum(identE_), _freeList() {
}
~Aggregate() {
while (!_freeList.empty())
_compactFreeList();
}
void print() {
std::cout << "_tails:";
for (int i = _tails.size() - 1; i >= 0; i--) {
std::cout << " " << *(_tails[i]);
}
std::cout << std::endl;
std::cout << "_frontSum=" << _frontSum << ", _backSum=" << _backSum
<< std::endl;
std::cout << "_frontStack:";
for (auto elt: _frontStack) { std::cout << elt << ", "; }
std::cout << std::endl;
std::cout << "_frontNode:" << *_frontNode << std::endl;
}
void rebuildFrontFrom(Node *c) {
aggT agg = _frontStack.empty()?_identE:_frontStack.back();
while (c != NULL) {
Node *next = c->leftPopped() ? c->children[1] : c->children[0];
if (c->full()) {
agg = _binOp.combine(c->agg[1], agg);
_frontStack.push_back(agg);
}
if (next == NULL) // reset the frontNode pointer
_frontNode = c;
c = next;
}
}
void rebuildFront() {
if (_tails.empty()) { _frontSum = _identE; return ;}
_frontStack.clear();
rebuildFrontFrom(_tails.back());
aggT agg = _frontStack.empty() ? _identE : _frontStack.back();
aggT other = (_frontNode->full() || _frontNode->rightEmpty())
? _frontNode->agg[0]
: _frontNode->agg[1];
_frontSum = _binOp.combine(other, agg);
}
void rebuildBack() {
if (_tails.empty()) { _backSum = _identE; return ;}
aggT agg = _identE;
auto it = _tails.rbegin();
while (++it != _tails.rend()) {
Node *c = *it;
aggT nodeAgg =
c->full() ? _binOp.combine(c->agg[0], c->agg[1]) : c->agg[0];
agg = _binOp.combine(agg, nodeAgg);
}
_backSum = agg;
}
void evict() {
_frontSum = _frontStack.empty() ? _identE : _frontStack.back();
if (_size > 0) _size--;
Node *c = _frontNode;
while (c != NULL) {
if (c->full()) _frontStack.pop_back(); // was a full node, no longer
c->pop_front();
if (c->arity != 0)
break;
Node *next = c->parent;
_deleteNode(c);
c = next;
}
if (c == NULL) { // hit the big root
if (PRINT_DEBUG) std::cout << "big root emptied, moving on..." << std::endl;
_tails.pop_back();
_frontNode = NULL;
rebuildFront(), rebuildBack();
}
else
rebuildFrontFrom(c);
}
void _slice(Node *node, timeT const& time) {
while (node != NULL) {
if (!node->leftPopped()) {
if (time < node->times[0]) {
node = node->children[0];
continue;
}
else if (time == node->times[0]) {
_deleteNode(node->pop_front());
break;
}
}
if (!node->rightEmpty() && time < node->times[1]) {
if (!node->leftPopped())
_deleteNode(node->pop_front());
node = node->children[1];
continue;
}
// should never reach this case
throw 1;
}
}
void _emptyOutNode(Node *node) {
if (node != NULL && !node->isLeaf()) {
Node *c = NULL;
if (!node->leftPopped() && (c = node->children[0]) != NULL)
_freeList.push_back(c);
if (!node->rightEmpty() && (c = node->children[1]) != NULL)
_freeList.push_back(c);
}
}
Node* _newNode(aggT a, timeT t, Node* left) {
if (_freeList.empty())
return new Node(a, t, left);
Node* node = _freeList.back();
_freeList.pop_back();
_emptyOutNode(node);
node->init(a, t, left);
return node;
}
void _deleteNode(Node* node, bool recursive=true) {
if (node == NULL)
return ; // nothing to delete
if (recursive) _emptyOutNode(node);
delete node;
}
void _compactFreeList() {
Node* node = _freeList.back();
_freeList.pop_back();
_emptyOutNode(node);
delete node;
}
void bulkEvict(timeT const& time) {
if (_tails.empty() || time < oldest()) return ; // nothing to evict
_size = -1; // size tracking will stop working from this point on
while (!_tails.empty()) {
Node *head = _tails.back();
timeT mostRecent = head->rightEmpty() ? head->times[0] : head->times[1];
if (PRINT_DEBUG) std::cout << "considering --" << *head << std::endl;
if (PRINT_DEBUG) std::cout << "mostRecent=" << mostRecent << std::endl;
if (time < mostRecent) {
if (PRINT_DEBUG) std::cout << "Slicing the head node" << std::endl;
// stop at this root, but slice it before we quit
if (head->full()) {
if (PRINT_DEBUG) std::cout << "head is full" << std::endl;
if (time >= head->times[0]) {
// delete the left subtree completely & slice the right tree
if (PRINT_DEBUG) std::cout << "== delete left; slice right" << std::endl;
_deleteNode(head->pop_front());
_slice(head->children[1], time);
} else {
if (PRINT_DEBUG) std::cout << "== slice left" << std::endl;
// slice the left subtree but leave the right tree alone
_slice(head->children[0], time);
}
} else {
int indicator = head->rightEmpty()?0:1;
if (PRINT_DEBUG) {
std::cout << "## not full, slicing indictor=" << indicator
<< std::endl;
}
_slice(head->children[indicator], time); // slice the only subtree
}
// done, no more eviction this time
break;
}
if (PRINT_DEBUG) std::cout << "Evicting whole head" << std::endl;
// evict this whole root, plus perhaps some more
_deleteNode(head); _tails.pop_back();
// shortcut the case where this head ends with time
if (mostRecent == time) break;
}
rebuildBack(), rebuildFront();
if (PRINT_DEBUG) this->print();
}
void bulkInsert(vector<pair<timeT, inT>> entries) {
bulkInsert(entries.begin(), entries.end());
}
template <class Iterator>
void bulkInsert(Iterator begin, Iterator end) {
for(auto it=begin;it!=end;it++) {
auto [time, value] = *it;
insert(time, value);
}
}
timeT oldest() const {
return _frontNode->leftPopped() ? _frontNode->times[1]
: _frontNode->times[0];
}
outT query() const {
return _binOp.lower(_binOp.combine(_frontSum, _backSum));
}
size_t size() { return _size; }
timeT youngest() const {
Node *backNode = _tails.front();
return (backNode->full() || backNode->leftPopped())
? backNode->times[1]
: backNode->times[0];
}
void insert_lifted(timeT const& time, aggT const& liftedValue) {
bool hasCarry = true;
Node *carriedFrom = NULL;
aggT carry = liftedValue;
timeT carryTime = time;
bool bigRootHit = false;
_backSum = _binOp.combine(_backSum, liftedValue);
for (auto it = _tails.begin(); it != _tails.end(); it++) {
Node *node = *it;
if (node->full() || node->leftPopped()) { // has room for carry
auto nextCarry = node->full()
? _binOp.combine(node->agg[0], node->agg[1])
: node->agg[1];
auto nextTime = node->times[1];
*it = _newNode(carry, carryTime, carriedFrom); // with just carry
carriedFrom = node, carry = nextCarry, carryTime = nextTime;
} else { // found a non-full node
node->push_back(carry, carryTime, carriedFrom);
hasCarry = false;
if (it + 1 == _tails.end())
bigRootHit = true;
break;
}
}
if (hasCarry) {
Node *n = _newNode(carry, carryTime, carriedFrom);
if (_tails.empty())
_frontNode = n;
_tails.push_back(n);
} else if (bigRootHit) {
rebuildFront(), rebuildBack();
}
}
void insert(timeT const& time, inT const& value) {
insert_lifted(time, _binOp.lift(value));
if (_size >= 0) _size++;
}
void insert(inT const& val) {
if (_tails.empty()) {
insert(0, val);
} else {
timeT const time = 1 + youngest();
insert(time, val);
}
}
};
template <typename timeT, class BinaryFunction, class T>
Aggregate<timeT, BinaryFunction>
make_aggregate(BinaryFunction f, T elem) {
return Aggregate<timeT, BinaryFunction>(f, elem);
}
template <typename BinaryFunction, typename timeT>
struct MakeAggregate {
template <typename T>
Aggregate<timeT, BinaryFunction> operator()(T elem) {
BinaryFunction f;
return make_aggregate<timeT, BinaryFunction>(f, elem);
}
};
} // namespace amta