-
Notifications
You must be signed in to change notification settings - Fork 3
/
mono_wedge.h
189 lines (140 loc) · 6.18 KB
/
mono_wedge.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
#ifndef MONOTONIC_WEDGE_H
#define MONOTONIC_WEDGE_H
#include <algorithm>
#include <functional>
#if __cplusplus > 199711L
#include <utility> // For std::forward
#endif
/*
This header presents algorithms for fast running minimum and maximum using
the Daniel Lemire monotonic wedge algorithm with enhancements proposed
by Ethan Fenn.
The algorithm here is modeled on the C++ STL style and meant to be used
with vector, deque or ring-buffer structures.
The amortized complexity of the update operation is constant;
IE, N updates can be performed in linear time on a given wedge.
The worst-case complexity for a single update is below log2(N).
Usage recommendations:
This algorithm is most useful for "rolling" min / max evaluation.
Most applications will thus prefer deques or ring-buffers as wedges.
Generally, values beyond a certain age should be popped to limit the
size of the wedge, though amortized complexity will remain linear even
if this is not done.
The wedge must be monotonic at all times with respect to Compare,
EG. by only modifying the structure with wedge_update and pop_front.
See bottom for (MIT) license and IP remarks.
*/
namespace mono_wedge
{
/*
mono_wedge_search(begin, end, value, comp)
Search routine used to determine deletion range in mono_wedge_update.
Similar to std::lower_bound, returns first element in range for which
comp(value, element) returns false.
Range must be sorted with regard to comp.
Iterator must be a random access iterator.
Complexity is below log2(N) with respect to wedge size.
Facilitates amortized constant complexity in mono_wedge_update.
*/
template<class Iterator, class T, class Compare>
Iterator mono_wedge_search(
Iterator begin,
Iterator end,
const T& value,
Compare comp)
{
size_t size = std::distance(begin, end);
if (size <= 0ul) return end;
// Linear search through at most J elements, where J = log2(N-J).
Iterator search_pos = end; --search_pos;
size_t i = 1ul;
for (; ((size - i) >> i) > 0ul; ++i, --search_pos)
{
if (comp(*search_pos, value)) return ++search_pos;
}
// Afterwards run a binary search (use std::lower_bound)
return std::lower_bound<Iterator, T, Compare>(begin, ++search_pos, value, comp);
}
/*
mono_wedge_update(wedge, value, comp)
Update a monotonic wedge with a new value.
Erases values which do not satisfy comp(element, value), then
appends value to the wedge via push_back.
Complexity is less than log2(N) with respect to wedge size.
Complexity of N calls is O(N), if wedge is initially empty.
Thus, amortized complexity over many calls is constant.
Wedge type must:
- Produce random access iterators via begin/end.
- Support push_back.
A "less" comparator yields a min-wedge.
A "greater" comparator yields a max-wedge.
*/
template<class Wedge, class T, class Compare>
void mono_wedge_update(
Wedge& wedge,
const T& value,
Compare comp)
{
auto i = mono_wedge_search(wedge.begin(), wedge.end(), value, comp);
wedge.erase(i, wedge.end());
wedge.push_back(value);
}
/*
min_wedge_search(wedge, value)
min_wedge_search(wedge, value)
Convenience variants of mono_wedge_search for min and max wedges.
These will use std::greater/less, which default to operator >/<.
*/
template<class Iterator, class T>
Iterator min_wedge_search(Iterator begin, Iterator end, const T& value) { return mono_wedge_search(begin, end, value, std::less<T>()); }
template<class Iterator, class T>
Iterator max_wedge_search(Iterator begin, Iterator end, const T& value) { return mono_wedge_search(begin, end, value, std::greater<T>()); }
/*
min_wedge_update(wedge, value)
min_wedge_update(wedge, value)
Convenience variants of mono_wedge_update for min and max wedges.
These will use std::greater/less, which default to operator >/<.
*/
template<class Wedge, class T>
void min_wedge_update(Wedge& wedge, const T& value) { return mono_wedge_update(wedge, value, std::less<T>()); }
template<class Wedge, class T>
void max_wedge_update(Wedge& wedge, const T& value) { return mono_wedge_update(wedge, value, std::greater<T>()); }
#if __cplusplus > 199711L
/*
C++11 variants of mono_wedge_update supporting rvalue references.
*/
template<class Wedge, class T, class Compare>
void mono_wedge_update(Wedge& wedge, T&& value, Compare comp)
{
typename Wedge::iterator i = mono_wedge_search(wedge.begin(), wedge.end(), value, comp);
size_t erase_count = std::distance(i, wedge.end());
while (erase_count--) wedge.pop_back();
wedge.push_back(std::forward(value));
}
template<class Wedge, class T, class Compare>
void min_wedge_update(Wedge& wedge, T&& value) { mono_wedge_update(wedge, std::forward(value), std::less<T>()); }
template<class Wedge, class T, class Compare>
void max_wedge_update(Wedge& wedge, T&& value) { mono_wedge_update(wedge, std::forward(value), std::greater<T>()); }
#endif
}
#endif // MONOTONIC_WEDGE_H
/*
This code is available under the MIT license:
Copyright (c) 2016 Evan Balster
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
The original Lemire algorithm is "patent-free". For more information on the Lemire algorithm:
Code: https://github.com/lemire/runningmaxmin
Paper: https://arxiv.org/abs/cs/0610046
*/