-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrunningmaxmin.h
638 lines (613 loc) · 22.6 KB
/
runningmaxmin.h
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/**
* Copyright 2006-..., Daniel Lemire
* Streaming Maximum-Minimum Filter Using No More than 3 Comparisons per Element
*
* This program is free software; you can
* redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation (version 2). This
* program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef RUNNINGMAXMIN_H
#define RUNNINGMAXMIN_H
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <vector>
#include "common.h"
#include "deque.h"
inline void display(const std::vector<floattype> & a) {
for (floattype i : a)
std::cout << i << " ";
std::cout << std::endl;
}
class minmaxfilter {
public:
virtual std::vector<floattype> & getmaxvalues() = 0;
virtual std::vector<floattype> & getminvalues() = 0;
virtual ~minmaxfilter() = default;
};
/**
* This is the naive algorithm one might try first.
*/
class slowmaxmin : public minmaxfilter {
public:
slowmaxmin(std::vector<floattype> & array, int width)
: maxvalues(array.size() - width + 1),
minvalues(array.size() - width + 1) {
floattype maxvalue, minvalue;
for (uint s = 0; s < array.size() - width + 1; ++s) {
maxvalue = array[s];
minvalue = array[s];
// could be done with iterators
for (uint k = s + 1; k < s + width; ++k) {
if (maxvalue < array[k])
maxvalue = array[k];
if (minvalue > array[k])
minvalue = array[k];
}
maxvalues[s] = maxvalue;
minvalues[s] = minvalue;
}
}
std::vector<floattype> & getmaxvalues() {
return maxvalues;
}
std::vector<floattype> & getminvalues() {
return minvalues;
}
std::vector<floattype> maxvalues;
std::vector<floattype> minvalues;
};
/**
* This is an implementation of the patented Gil-Kimmel algorithm.
* Could be rewritten to use less memory.
*/
class GilKimmel : public minmaxfilter {
public:
GilKimmel(std::vector<floattype> & array, int width)
: maxvalues(array.size() - width + 1),
minvalues(array.size() - width + 1) {
std::vector<floattype> R(array.size() + 1);
std::vector<floattype> S(array.size() + 1);
computePrefixSuffixMax(R, S, array,
width); // implements the cut in the middle trick
for (int j = 0; j < static_cast<int>(array.size()) - width + 1;
j += width) {
const int endofblock =
std::min(j + width, static_cast<int>(array.size()) - width + 1);
int begin = j;
int end = endofblock;
int midpoint = (end - begin + 1) / 2 + begin;
while (midpoint != end) {
if (S[midpoint + width - 1] <= R[midpoint]) {
begin = midpoint;
midpoint = (end - begin + 1) / 2 + begin;
} else {
end = midpoint;
midpoint = (end - begin + 1) / 2 + begin;
}
}
for (int jj = j; jj < midpoint; ++jj) {
maxvalues[jj] = R[jj];
}
for (int jj = midpoint; jj < endofblock; ++jj) {
maxvalues[jj] = S[jj + width - 1];
}
}
computePrefixSuffixMin(R, S, array,
width); // implements the cut in the middle trick
for (int j = 0; j < static_cast<int>(array.size()) - width + 1;
j += width) {
const int endofblock =
std::min(j + width, static_cast<int>(array.size()) - width + 1);
int begin = j;
int end = endofblock;
int midpoint = (end - begin + 1) / 2 + begin;
while (midpoint != end) {
if (S[midpoint + width - 1] >= R[midpoint]) {
begin = midpoint;
midpoint = (end - begin + 1) / 2 + begin;
} else {
end = midpoint;
midpoint = (end - begin + 1) / 2 + begin;
}
}
for (int jj = j; jj < midpoint; ++jj) {
minvalues[jj] = R[jj];
}
for (int jj = midpoint; jj < endofblock; ++jj) {
minvalues[jj] = S[jj + width - 1];
}
}
}
void computePrefixSuffixMax(std::vector<floattype> & R,
std::vector<floattype> & S,
const std::vector<floattype> & array,
const int width) {
for (int j = 0; j < static_cast<int>(array.size()); j += width) {
const int begin = j;
const int end = std::min(static_cast<int>(array.size()), j + width);
const int midpoint = (end - begin + 1) / 2 + begin;
S[begin] = array[begin];
for (int jj = begin + 1; jj < midpoint; ++jj) {
S[jj] = std::max(array[jj], S[jj - 1]);
}
R[end - 1] = array[end - 1];
for (int jj = end - 2; jj >= midpoint; --jj) {
R[jj] = std::max(R[jj + 1], array[jj]);
}
if (std::max(R[midpoint], S[midpoint - 1]) == R[midpoint]) {
for (int jj = midpoint; jj < end; ++jj)
S[jj] = std::max(array[jj], S[jj - 1]);
for (int jj = midpoint - 1; jj >= begin; --jj)
R[jj] = R[midpoint];
} else {
for (int jj = midpoint - 1; jj >= begin; --jj)
R[jj] = std::max(R[jj + 1], array[jj]);
for (int jj = midpoint; jj < end; ++jj)
S[jj] = S[midpoint - 1];
}
}
}
void computePrefixSuffixMin(std::vector<floattype> & R,
std::vector<floattype> & S,
const std::vector<floattype> & array,
const int width) {
for (int j = 0; j < static_cast<int>(array.size()); j += width) {
const int begin = j;
const int end = std::min(static_cast<int>(array.size()), j + width);
const int midpoint = (end - begin + 1) / 2 + begin;
S[begin] = array[begin];
for (int jj = begin + 1; jj < midpoint; ++jj) {
S[jj] = std::min(array[jj], S[jj - 1]);
}
R[end - 1] = array[end - 1];
for (int jj = end - 2; jj >= midpoint; --jj) {
R[jj] = std::min(R[jj + 1], array[jj]);
}
if (std::min(R[midpoint], S[midpoint - 1]) == R[midpoint]) {
for (int jj = midpoint; jj < end; ++jj)
S[jj] = std::min(array[jj], S[jj - 1]);
for (int jj = midpoint - 1; jj >= begin; --jj)
R[jj] = R[midpoint];
} else {
for (int jj = midpoint - 1; jj >= begin; --jj)
R[jj] = std::min(R[jj + 1], array[jj]);
for (int jj = midpoint; jj < end; ++jj)
S[jj] = S[midpoint - 1];
}
}
}
std::vector<floattype> & getmaxvalues() {
return maxvalues;
}
std::vector<floattype> & getminvalues() {
return minvalues;
}
std::vector<floattype> maxvalues;
std::vector<floattype> minvalues;
};
/**
* This should be very close to the van Herk algorithm.
*/
class vanHerkGilWermanmaxmin : public minmaxfilter {
public:
vanHerkGilWermanmaxmin(std::vector<floattype> & array, int width)
: maxvalues(array.size() - width + 1),
minvalues(array.size() - width + 1) {
std::vector<floattype> R(width);
std::vector<floattype> S(width);
for (uint j = 0; j < array.size() - width + 1; j += width) {
uint Rpos = std::min(j + width - 1,
static_cast<unsigned>(array.size() - 1));
R[0] = array[Rpos];
for (uint i = Rpos - 1; i + 1 > j; i -= 1)
R[Rpos - i] = std::max(R[Rpos - i - 1], array[i]);
S[0] = array[Rpos];
uint m1 = std::min(j + 2 * width - 1,
static_cast<unsigned>(array.size()));
for (uint i = Rpos + 1; i < m1; ++i) {
S[i - Rpos] = std::max(S[i - Rpos - 1], array[i]);
}
for (uint i = 0; i < m1 - Rpos; i += 1)
maxvalues[j + i] = std::max(S[i], R[(Rpos - j + 1) - i - 1]);
}
for (uint j = 0; j < array.size() - width + 1; j += width) {
uint Rpos = std::min(j + width - 1,
static_cast<unsigned>(array.size() - 1));
R[0] = array[Rpos];
for (uint i = Rpos - 1; i + 1 > j; i -= 1)
R[Rpos - i] = std::min(R[Rpos - i - 1], array[i]);
S[0] = array[Rpos];
uint m1 = std::min(j + 2 * width - 1,
static_cast<unsigned>(array.size()));
for (uint i = Rpos + 1; i < m1; ++i) {
S[i - Rpos] = std::min(S[i - Rpos - 1], array[i]);
}
for (uint i = 0; i < m1 - Rpos; i += 1)
minvalues[j + i] = std::min(S[i], R[(Rpos - j + 1) - i - 1]);
}
assert(maxvalues.size() == array.size() - width + 1);
assert(minvalues.size() == array.size() - width + 1);
}
std::vector<floattype> & getmaxvalues() {
return maxvalues;
}
std::vector<floattype> & getminvalues() {
return minvalues;
}
std::vector<floattype> maxvalues;
std::vector<floattype> minvalues;
};
/**
* implementation of the streaming algorithm
*/
class lemiremaxmin : public minmaxfilter {
public:
lemiremaxmin(std::vector<floattype> & array, uint width)
: maxvalues(array.size() - width + 1),
minvalues(array.size() - width + 1) {
std::deque<int> maxfifo, minfifo;
for (uint i = 1; i < width; ++i) {
if (array[i] > array[i - 1]) { // overshoot
minfifo.push_back(i - 1);
while (!maxfifo.empty()) {
if (array[i] <= array[maxfifo.back()]) {
if (i == width + maxfifo.front())
maxfifo.pop_front();
break;
}
maxfifo.pop_back();
}
} else {
maxfifo.push_back(i - 1);
while (!minfifo.empty()) {
if (array[i] >= array[minfifo.back()]) {
if (i == width + minfifo.front())
minfifo.pop_front();
break;
}
minfifo.pop_back();
}
}
}
for (uint i = width; i < array.size(); ++i) {
maxvalues[i - width] =
array[maxfifo.empty() ? i - 1 : maxfifo.front()];
minvalues[i - width] =
array[minfifo.empty() ? i - 1 : minfifo.front()];
if (array[i] > array[i - 1]) { // overshoot
minfifo.push_back(i - 1);
if (i == width + minfifo.front())
minfifo.pop_front();
while (!maxfifo.empty()) {
if (array[i] <= array[maxfifo.back()]) {
if (i == width + maxfifo.front())
maxfifo.pop_front();
break;
}
maxfifo.pop_back();
}
} else {
maxfifo.push_back(i - 1);
if (i == width + maxfifo.front())
maxfifo.pop_front();
while (!minfifo.empty()) {
if (array[i] >= array[minfifo.back()]) {
if (i == width + minfifo.front())
minfifo.pop_front();
break;
}
minfifo.pop_back();
}
}
}
maxvalues[array.size() - width] =
array[maxfifo.empty() ? array.size() - 1 : maxfifo.front()];
minvalues[array.size() - width] =
array[minfifo.empty() ? array.size() - 1 : minfifo.front()];
}
std::vector<floattype> & getmaxvalues() {
return maxvalues;
}
std::vector<floattype> & getminvalues() {
return minvalues;
}
std::vector<floattype> maxvalues;
std::vector<floattype> minvalues;
};
// actual streaming implementation
class lemiremaxmintruestreaming {
public:
explicit lemiremaxmintruestreaming(uint width)
: up(), lo(), n(0), ww(width) {
init(&up, ww);
init(&lo, ww);
}
~lemiremaxmintruestreaming() {
free(&up);
free(&lo);
}
void update(floattype value) {
if (nonempty(&up) != 0) {
if (value > tailvalue(&up)) {
prunetail(&up);
while (((nonempty(&up)) != 0) && (value >= tailvalue(&up))) {
prunetail(&up);
}
} else {
prunetail(&lo);
while (((nonempty(&lo)) != 0) && (value <= tailvalue(&lo))) {
prunetail(&lo);
}
}
}
push(&up, n, value);
if (n == ww + headindex(&up)) {
prunehead(&up);
}
push(&lo, n, value);
if (n == ww + headindex(&lo)) {
prunehead(&lo);
}
n++;
}
floattype max() {
return headvalue(&up);
}
floattype min() {
return headvalue(&lo);
}
intfloatqueue up;
intfloatqueue lo;
uint n;
uint ww;
};
// wrapper over the streaming version
class lemiremaxminwrap : public minmaxfilter {
public:
lemiremaxminwrap(std::vector<floattype> & array, uint width)
: maxvalues(array.size() - width + 1),
minvalues(array.size() - width + 1) {
lemiremaxmintruestreaming lts(width);
for (uint i = 0; i < width - 1; ++i) {
lts.update(array[i]);
}
for (uint i = width - 1; i < array.size(); ++i) {
lts.update(array[i]);
maxvalues[i - width + 1] = lts.max();
minvalues[i - width + 1] = lts.min();
}
}
std::vector<floattype> & getmaxvalues() {
return maxvalues;
}
std::vector<floattype> & getminvalues() {
return minvalues;
}
std::vector<floattype> maxvalues;
std::vector<floattype> minvalues;
};
#include "mono_wedge.h"
struct Sample {
floattype value;
uint time;
bool operator<(const Sample &o) const {return value < o.value;}
bool operator>(const Sample &o) const {return value > o.value;}
};
// wrapper over the monowedge streaming version
class monowedgewrap : public minmaxfilter {
public:
monowedgewrap(std::vector<floattype> & array, uint width)
: maxvalues(array.size() - width + 1),
minvalues(array.size() - width + 1) {
std::deque<Sample> max_wedge;
std::deque<Sample> min_wedge;
for (uint i = 0; i < width - 1; ++i) {
Sample sample = {array[i], i};
mono_wedge::max_wedge_update(max_wedge, sample);
mono_wedge::min_wedge_update(min_wedge, sample);
}
for (uint i = width; i < array.size(); ++i) {
Sample sample = {array[i], i};
mono_wedge::max_wedge_update(max_wedge, sample);
mono_wedge::min_wedge_update(min_wedge, sample);
while (max_wedge.front().time <= i-width) max_wedge.pop_front();
while (min_wedge.front().time <= i-width) min_wedge.pop_front();
maxvalues[i - width + 1] = max_wedge.front().value;
minvalues[i - width + 1] = min_wedge.front().value;
}
}
std::vector<floattype> & getmaxvalues() {
return maxvalues;
}
std::vector<floattype> & getminvalues() {
return minvalues;
}
std::vector<floattype> maxvalues;
std::vector<floattype> minvalues;
};
/**
* implementation of the bitmap-based streaming algorithm
*/
class lemirebitmapmaxmin : public minmaxfilter {
public:
// TODO: make the code portable to non-GCC-like compilers
// TODO: extend beyond 64-bit to include 128-bit
lemirebitmapmaxmin(std::vector<floattype> & array, const uint width)
: maxvalues(array.size() - width + 1),
minvalues(array.size() - width + 1) {
assert(width <= sizeof(unsigned long) * 8);
unsigned long maxfifo = 0;
unsigned long minfifo = 0;
for (uint i = 1; i < width; ++i) {
if (array[i] > array[i - 1]) { // overshoot
minfifo |= 1;
minfifo <<= 1;
maxfifo <<= 1;
while (maxfifo != 0) {
const long t = maxfifo & -maxfifo;
const int bitpos = __builtin_popcountl(t - 1);
if (array[i] <= array[i - bitpos]) {
break;
}
maxfifo ^= t;
}
} else {
maxfifo |= 1;
minfifo <<= 1;
maxfifo <<= 1;
while (minfifo != 0) {
const long t = minfifo & -minfifo;
const int bitpos = __builtin_popcountl(t - 1);
if (array[i] >= array[i - bitpos]) {
break;
}
minfifo ^= t;
}
}
}
unsigned long mask = ~0l;
if (width < sizeof(unsigned long) * 8) {
mask = (1UL << width) - 1;
}
for (uint i = width; i < array.size(); ++i) {
maxfifo &= mask;
minfifo &= mask;
if (maxfifo == 0)
maxvalues[i - width] = array[i - 1];
else {
maxvalues[i - width] = array[i - (sizeof(unsigned long) * 8 -
__builtin_clzl(maxfifo))];
}
if (minfifo == 0)
minvalues[i - width] = array[i - 1];
else {
minvalues[i - width] = array[i - (sizeof(unsigned long) * 8 -
__builtin_clzl(minfifo))];
}
if (array[i] > array[i - 1]) { // overshoot
minfifo |= 1;
minfifo <<= 1;
maxfifo <<= 1;
while (maxfifo != 0) {
const long t = maxfifo & -maxfifo;
const int bitpos = __builtin_popcountl(t - 1);
if (array[i] <= array[i - bitpos]) {
break;
}
maxfifo ^= t;
}
} else {
maxfifo |= 1;
maxfifo <<= 1;
minfifo <<= 1;
while (minfifo != 0) {
const long t = minfifo & -minfifo;
const int bitpos = __builtin_popcountl(t - 1);
if (array[i] >= array[i - bitpos]) {
break;
}
minfifo ^= t;
}
}
}
maxfifo = maxfifo & mask;
minfifo = minfifo & mask;
if (maxfifo == 0)
maxvalues[array.size() - width] = array[array.size() - 1];
else
maxvalues[array.size() - width] =
array[array.size() -
(sizeof(unsigned long) * 8 - __builtin_clzl(maxfifo))];
if (minfifo == 0)
minvalues[array.size() - width] = array[array.size() - 1];
else
minvalues[array.size() - width] =
array[array.size() -
(sizeof(unsigned long) * 8 - __builtin_clzl(minfifo))];
}
std::vector<floattype> & getmaxvalues() {
return maxvalues;
}
std::vector<floattype> & getminvalues() {
return minvalues;
}
std::vector<floattype> maxvalues;
std::vector<floattype> minvalues;
};
/**
* simplest implementation (pseudocode-like)
*/
class simplelemiremaxmin : public minmaxfilter {
public:
simplelemiremaxmin(std::vector<floattype> & array, uint width)
: maxvalues(array.size() - width + 1),
minvalues(array.size() - width + 1) {
std::deque<int> maxfifo, minfifo;
maxfifo.push_back(0);
minfifo.push_back(0);
for (uint i = 1; i < width; ++i) {
if (array[i] > array[i - 1]) { // overshoot
maxfifo.pop_back();
while (!maxfifo.empty()) {
if (array[i] <= array[maxfifo.back()])
break;
maxfifo.pop_back();
}
} else {
minfifo.pop_back();
while (!minfifo.empty()) {
if (array[i] >= array[minfifo.back()])
break;
minfifo.pop_back();
}
}
maxfifo.push_back(i);
minfifo.push_back(i);
}
for (uint i = width; i < array.size(); ++i) {
maxvalues[i - width] = array[maxfifo.front()];
minvalues[i - width] = array[minfifo.front()];
if (array[i] > array[i - 1]) { // overshoot
maxfifo.pop_back();
while (!maxfifo.empty()) {
if (array[i] <= array[maxfifo.back()])
break;
maxfifo.pop_back();
}
} else {
minfifo.pop_back();
while (!minfifo.empty()) {
if (array[i] >= array[minfifo.back()])
break;
minfifo.pop_back();
}
}
maxfifo.push_back(i);
minfifo.push_back(i);
if (i == width + maxfifo.front())
maxfifo.pop_front();
else if (i == width + minfifo.front())
minfifo.pop_front();
}
maxvalues[array.size() - width] = array[maxfifo.front()];
minvalues[array.size() - width] = array[minfifo.front()];
}
std::vector<floattype> & getmaxvalues() {
return maxvalues;
}
std::vector<floattype> & getminvalues() {
return minvalues;
}
std::vector<floattype> maxvalues;
std::vector<floattype> minvalues;
};
#endif