14#ifndef OR_TOOLS_UTIL_RUNNING_STAT_H_
15#define OR_TOOLS_UTIL_RUNNING_STAT_H_
39 void Reset(
int window_size);
64 std::deque<int> values_;
69template <
class Number =
double>
87 const int window_size_;
90 std::vector<Number> values_;
102 : window_size_(window_size),
106 CHECK_GT(window_size_, 0);
110 window_size_ = window_size;
118 global_sum_ +=
value;
120 values_.push_back(
value);
121 if (
static_cast<int>(values_.size()) > window_size_) {
122 local_sum_ -= values_.front();
128 return num_adds_ == 0 ? 0.0 : global_sum_ /
static_cast<double>(num_adds_);
132 return values_.empty() ? 0.0
133 : local_sum_ /
static_cast<double>(values_.size());
142 return static_cast<int>(values_.size()) == window_size_;
145template <
class Number>
147 : window_size_(window_size), values_(), last_index_(0), max_index_(0) {
148 DCHECK_GT(window_size, 0);
151template <
class Number>
153 if (
static_cast<size_t>(values_.size()) < window_size_) {
156 if (values_.empty() ||
value >= GetCurrentMax()) {
157 max_index_ = values_.size();
159 values_.push_back(
value);
163 DCHECK_EQ(values_.size(), window_size_);
166 if (
value >= GetCurrentMax()) {
167 max_index_ = last_index_;
168 values_[last_index_] =
value;
170 values_[last_index_] =
value;
171 if (last_index_ == max_index_) {
176 Number max_value = values_[max_index_];
177 for (
int i = 1; i < static_cast<int>(values_.size()); ++i) {
178 if (values_[i] > max_value) {
179 max_value = values_[i];
185 if (++last_index_ == window_size_) {
190template <
class Number>
192 DCHECK(!values_.empty());
193 return values_[max_index_];
RunningAverage & operator=(const RunningAverage &)=delete
void ClearWindow()
Clears the current window.
double WindowAverage() const
RunningAverage(const RunningAverage &)=delete
This type is neither copyable nor movable.
bool IsWindowFull() const
double GlobalAverage() const
void Reset(int window_size)
void Add(int value)
Adds the next integer of the stream.
RunningAverage(int window_size=1)
################## Implementations below #####################
RunningMax(int window_size)
Takes the size of the running window. The size must be positive.
void Add(Number value)
Processes a new element from the stream.
RunningMax & operator=(const RunningMax &)=delete
RunningMax(const RunningMax &)=delete
This type is neither copyable nor movable.
In SWIG mode, we don't want anything besides these top-level includes.