-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathmath.cpp
More file actions
69 lines (57 loc) · 1.65 KB
/
Copy pathmath.cpp
File metadata and controls
69 lines (57 loc) · 1.65 KB
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
#include <pineforge/math.hpp>
namespace pineforge {
namespace math {
Sum::Sum(int length)
: length_(length), sum_(0.0), saved_sum_(0.0),
has_saved_state_(false), current_value_added_(false),
current_value_evicted_(false), current_evicted_value_(0.0) {}
double Sum::apply(double src) {
current_value_added_ = false;
current_value_evicted_ = false;
if (is_na(src)) {
// Pine ignores na sources rather than consuming a window slot. Once
// seeded, the last-N-valid sum is therefore held on na-input bars.
if (length_ > 0 && static_cast<int>(buffer_.size()) >= length_) {
return sum_;
}
return na<double>();
}
buffer_.push_back(src);
sum_ += src;
current_value_added_ = true;
while ((int)buffer_.size() > length_) {
current_value_evicted_ = true;
current_evicted_value_ = buffer_.front();
sum_ -= buffer_.front();
buffer_.pop_front();
}
if (static_cast<int>(buffer_.size()) < length_) {
return na<double>();
}
return sum_;
}
void Sum::restore() {
if (current_value_added_ && length_ > 0) {
buffer_.pop_back();
if (current_value_evicted_) {
buffer_.push_front(current_evicted_value_);
}
}
sum_ = saved_sum_;
current_value_added_ = false;
current_value_evicted_ = false;
}
double Sum::compute(double src) {
saved_sum_ = sum_;
has_saved_state_ = true;
return apply(src);
}
double Sum::recompute(double src) {
if (!has_saved_state_) {
return compute(src);
}
restore();
return apply(src);
}
} // namespace math
} // namespace pineforge