fix(algos): refactor extrema circular buffer
This commit is contained in:
parent
5fd49dfb1e
commit
3c949f3da3
4 changed files with 2 additions and 18 deletions
|
|
@ -201,11 +201,6 @@
|
||||||
<div class="code" data-file="monotonic.cpp"></div>
|
<div class="code" data-file="monotonic.cpp"></div>
|
||||||
<h3>further improvements</h3>
|
<h3>further improvements</h3>
|
||||||
<ol>
|
<ol>
|
||||||
<li>
|
|
||||||
While the final approach is <i>theoretically</i> faster than the
|
|
||||||
second, with small data sets the overhead of the latter is
|
|
||||||
likely to upset any performance gains.
|
|
||||||
</li>
|
|
||||||
<li>
|
<li>
|
||||||
The class could leverage templates to take in a comparator
|
The class could leverage templates to take in a comparator
|
||||||
<span><code>std::less<double></code></span>
|
<span><code>std::less<double></code></span>
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,6 @@
|
||||||
|
|
||||||
class ExtremaCircularBuffer {
|
class ExtremaCircularBuffer {
|
||||||
public:
|
public:
|
||||||
ExtremaCircularBuffer(size_t capacity) : capacity(capacity) {}
|
|
||||||
|
|
||||||
void push_back(double value) {
|
void push_back(double value) {
|
||||||
if (prices.size() == capacity) {
|
if (prices.size() == capacity) {
|
||||||
double front = prices.front();
|
double front = prices.front();
|
||||||
|
|
@ -49,8 +47,5 @@ public:
|
||||||
return sorted_prices.begin()->first;
|
return sorted_prices.begin()->first;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
/* methods & fields omitted for brevity */
|
||||||
std::deque<double> prices;
|
|
||||||
std::map<double, size_t> sorted_prices;
|
|
||||||
size_t capacity;
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,6 @@
|
||||||
|
|
||||||
class ExtremaCircularBuffer {
|
class ExtremaCircularBuffer {
|
||||||
public:
|
public:
|
||||||
explicit ExtremaCircularBuffer(size_t capacity) : capacity(capacity) {}
|
|
||||||
|
|
||||||
void push_back(double value) {
|
void push_back(double value) {
|
||||||
if (prices.size() == capacity) {
|
if (prices.size() == capacity) {
|
||||||
double front_value = prices.front();
|
double front_value = prices.front();
|
||||||
|
|
@ -27,8 +25,6 @@ public:
|
||||||
prices.pop_front();
|
prices.pop_front();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const { return prices.size(); }
|
|
||||||
|
|
||||||
double get_max() const {
|
double get_max() const {
|
||||||
if (prices.empty()) {
|
if (prices.empty()) {
|
||||||
throw std::out_of_range("Cannot find max() of empty buffer");
|
throw std::out_of_range("Cannot find max() of empty buffer");
|
||||||
|
|
@ -59,7 +55,5 @@ private:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::deque<double> prices;
|
/* methods & fields omitted for brevity */
|
||||||
std::deque<std::pair<double, size_t>> maxs;
|
|
||||||
size_t capacity;
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue