/
running_stat_impl.hpp
231 lines (196 loc) · 7.84 KB
/
running_stat_impl.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
//----------------------------------------------------------------------------
/// \file running_stat_impl.hpp
/// \author Serge Aleynikov
//----------------------------------------------------------------------------
/// This file implements a class that calculates running mean and
/// standard deviation.
//----------------------------------------------------------------------------
// Created: 2010-05-20
//----------------------------------------------------------------------------
/*
***** BEGIN LICENSE BLOCK *****
This file is part of the utxx open-source project
Copyright (C) 2010 Serge Aleynikov <saleyn@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***** END LICENSE BLOCK *****
*/
#ifndef _UTXX_RUNNING_STAT_IMPL_HPP_
#define _UTXX_RUNNING_STAT_IMPL_HPP_
#include <deque>
#include <utxx/compiler_hints.hpp>
#ifdef UTXX_RUNNING_MINMAX_DEBUG
#include <iostream>
#endif
namespace utxx {
namespace detail {
template<typename Derived, typename T, bool FastMinMax>
struct minmax_impl;
// See:
// http://www.archipel.uqam.ca/309/1/webmaximinalgo.pdf
template <typename Derived, typename T>
class minmax_impl<Derived, T, true> {
std::deque<size_t> m_min_fifo;
std::deque<size_t> m_max_fifo;
size_t m_min_idx;
size_t m_max_idx;
Derived const* derived_this() const { return static_cast<Derived const*>(this); }
T data(size_t a_idx) const { return derived_this()->data(a_idx); }
size_t end() const { return derived_this()->end_idx(); }
#ifdef UTXX_RUNNING_MINMAX_DEBUG
size_t capacity() const { return derived_this()->capacity(); }
#endif
public:
minmax_impl()
: m_min_idx(0)
, m_max_idx(0)
{}
minmax_impl(const minmax_impl& a_rhs)
: m_min_fifo(a_rhs.m_min_fifo)
, m_max_fifo(a_rhs.m_max_fifo)
, m_min_idx (a_rhs.m_min_idx)
, m_max_idx (a_rhs.m_max_idx)
{}
#if __cplusplus >= 201103L
minmax_impl(minmax_impl&& a_rhs)
: m_min_idx(a_rhs.m_min_idx)
, m_max_idx(a_rhs.m_max_idx)
{
m_min_fifo.swap(a_rhs.m_min_fifo);
m_max_fifo.swap(a_rhs.m_max_fifo);
}
#endif
void clear() {
m_min_fifo.clear();
m_max_fifo.clear();
m_min_idx = m_max_idx = 0;
}
protected:
bool outside_window(size_t a_idx) const {
size_t iend = end();
size_t begin = likely(iend > derived_this()->MASK)
? iend - derived_this()->MASK : 0;
bool res = a_idx < begin;
#ifdef UTXX_RUNNING_MINMAX_DEBUG
std::cout << "Check outside [" << a_idx << "]: " << (res ? "true" : "false")
<< std::endl;
#endif
return res;
}
void update_minmax(T a_sample) {
if (unlikely(derived_this()->empty())) {
m_min_idx = m_max_idx = end();
return;
}
size_t prev = end() - 1;
if (a_sample > data(prev)) { //overshoot
m_min_fifo.push_back(prev);
if (outside_window(m_min_fifo.front()))
m_min_fifo.pop_front();
while (!m_max_fifo.empty()) {
size_t idx = m_max_fifo.back();
if (a_sample <= data(idx)) {
if (outside_window(m_max_fifo.front())) {
if (m_max_idx == m_max_fifo.front())
m_max_idx = end();
m_max_fifo.pop_front();
}
break;
}
m_max_fifo.pop_back();
}
} else {
m_max_fifo.push_back(prev);
if (outside_window(m_max_fifo.front()))
m_max_fifo.pop_front();
while (!m_min_fifo.empty()) {
size_t idx = m_min_fifo.back();
if (a_sample >= data(idx)) {
if (outside_window(m_min_fifo.front())) {
if (m_min_idx == m_min_fifo.front())
m_min_idx = end();
m_min_fifo.pop_front();
}
break;
}
m_min_fifo.pop_back();
}
}
auto idx = m_max_fifo.empty() ? m_max_idx : m_max_fifo.front();
m_max_idx = a_sample > data(idx) ? end() : idx;
idx = m_min_fifo.empty() ? m_min_idx : m_min_fifo.front();
m_min_idx = a_sample < data(idx) ? end() : idx;
#ifdef UTXX_RUNNING_MINMAX_DEBUG
std::cout << "========";
for (size_t i = derived_this()->begin_idx(), e = end(); i < e; ++i)
std::cout << " [" << i << "]" << data(i);
std::cout << " [" << end() << "]" << a_sample << std::endl;
std::cout << "Max: " << max() << " |";
for (auto i : m_max_fifo) std::cout << " [" << i << "]" << data(i);
std::cout << std::endl;
std::cout << "Min: " << min() << " |";
for (auto i : m_min_fifo) std::cout << " [" << i << "]" << data(i);
std::cout << std::endl;
#endif
}
T min() const { return data(m_min_idx); }
T max() const { return data(m_max_idx); }
std::pair<T,T> minmax() const {
return std::make_pair(min(), max());
}
};
template <typename Derived, typename T>
class minmax_impl<Derived, T, false> {
Derived const* derived_this() const { return static_cast<Derived const*>(this); }
size_t begin_idx() const { return derived_this()->begin_idx(); }
size_t end_idx() const { return derived_this()->end_idx(); }
T data(size_t a_idx) const { return derived_this()->data(a_idx); }
void max();
public:
minmax_impl() {}
minmax_impl(const minmax_impl& a_rhs) {}
#if __cplusplus >= 201103L
minmax_impl(minmax_impl&& a_rhs) {}
#endif
void clear() {}
protected:
T min() const {
T n = std::numeric_limits<T>::max();
for (size_t i = begin_idx(), e = end_idx(); i != e; ++i) {
T d = data(i);
if (d < n) n = d;
}
return n;
}
T max() const {
T n = std::numeric_limits<T>::lowest();
for (size_t i = begin_idx(), e = end_idx(); i != e; ++i) {
T d = data(i);
if (d > n) n = d;
}
return n;
}
std::pair<T,T> minmax() const {
T mn = std::numeric_limits<T>::max();
T mx = std::numeric_limits<T>::lowest();
for (size_t i = begin_idx(), e = end_idx(); i != e; ++i) {
T d = data(i);
if (d > mx) mx = d;
if (d < mn) mn = d;
}
return std::make_pair(mn, mx);
}
void update_minmax(T sample) {}
};
}
} // namespace utxx
#endif // _UTXX_RUNNING_STAT_IMPL_HPP_