Skip to content

Commit d2c52a0

Browse files
committed
Use RingBuffer in ValueHistory
1 parent 82ba102 commit d2c52a0

File tree

3 files changed

+16
-49
lines changed

3 files changed

+16
-49
lines changed

src/openvic-simulation/country/CountryInstance.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ using namespace OpenVic;
3535

3636
using enum CountryInstance::country_status_t;
3737

38+
static constexpr size_t DAYS_OF_BALANCE_HISTORY = 30;
3839
static constexpr colour_t ERROR_COLOUR = colour_t::from_integer(0xFF0000);
3940

4041
CountryInstance::CountryInstance(
@@ -72,7 +73,7 @@ CountryInstance::CountryInstance(
7273
building_type_unlock_levels { building_type_keys },
7374

7475
/* Budget */
75-
balance_history{30, 0},
76+
balance_history{DAYS_OF_BALANCE_HISTORY},
7677
taxable_income_by_pop_type { pop_type_keys },
7778
effective_tax_rate_by_strata {
7879
strata_keys,

src/openvic-simulation/types/RingBuffer.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <algorithm>
44
#include <bit>
5+
#include <cassert>
56
#include <cstddef>
67
#include <cstdlib>
78
#include <iterator>

src/openvic-simulation/types/ValueHistory.hpp

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#pragma once
22

3-
#include <numeric>
43
#include <ostream>
5-
#include <vector>
64

7-
#include "openvic-simulation/utility/Containers.hpp"
5+
#include "openvic-simulation/types/RingBuffer.hpp"
86

97
namespace OpenVic {
108

11-
template<typename T>
12-
struct ValueHistory : private memory::vector<T> {
13-
using base_type = memory::vector<T>;
9+
template<typename T, typename Allocator = std::allocator<T>>
10+
struct ValueHistory : private RingBuffer<T, Allocator> {
11+
using base_type = RingBuffer<T, Allocator>;
1412

13+
using typename base_type::allocator_type;
14+
using typename base_type::size_type;
1515
using typename base_type::iterator;
1616
using typename base_type::const_iterator;
1717
using typename base_type::reverse_iterator;
@@ -22,7 +22,6 @@ namespace OpenVic {
2222
using typename base_type::const_pointer;
2323
using typename base_type::difference_type;
2424
using base_type::operator[];
25-
using base_type::data;
2625
using base_type::size;
2726
using base_type::empty;
2827
using base_type::begin;
@@ -35,51 +34,17 @@ namespace OpenVic {
3534
using base_type::crend;
3635
using base_type::front;
3736
using base_type::back;
37+
using base_type::push_back;
3838

3939
constexpr ValueHistory() {};
40-
ValueHistory(size_t history_size, T const& fill_value = {}) : base_type(history_size, fill_value) {}
41-
ValueHistory(ValueHistory&&) = default;
42-
43-
void fill(T const& value) {
44-
std::fill(begin(), end(), value);
40+
explicit ValueHistory(size_type capacity) : base_type(capacity) {}
41+
explicit ValueHistory(size_type capacity, allocator_type const& allocator) : base_type(capacity, allocator) {}
42+
explicit ValueHistory(size_type size, T const& fill_value) : base_type(size) {
43+
fill(fill_value);
4544
}
4645

47-
T get_total() const {
48-
return std::accumulate(begin(), end(), T {});
49-
}
50-
51-
void push_back(T const& value) {
52-
if (!empty()) {
53-
// Move all values back by one (with the first value being discarded) and place the new value at the end
54-
for (iterator it = begin(); it != end(); ++it) {
55-
*it = *(it + 1);
56-
}
57-
back() = value;
58-
}
59-
}
60-
61-
void set_size(size_t new_size, T const& fill_value = {}) {
62-
const difference_type size_diff = new_size - size();
63-
64-
if (size_diff < 0) {
65-
// Move the last new_size elements to the front, discarding the values before them
66-
for (iterator it = begin(); it != begin() + new_size; ++it) {
67-
*it = *(it - size_diff);
68-
}
69-
70-
base_type::resize(new_size);
71-
} else if (size_diff > 0) {
72-
// Move the existing values to the end and fill the front with fill_value
73-
const size_t old_size = size();
74-
base_type::resize(new_size);
75-
76-
for (reverse_iterator rit = rbegin(); rit != rbegin() + old_size; ++rit) {
77-
*rit = *(rit + size_diff);
78-
}
79-
for (iterator it = begin(); it != begin() + size_diff; ++it) {
80-
*it = fill_value;
81-
}
82-
}
46+
void fill(T const& value) {
47+
base_type::resize(base_type::capacity(), value);
8348
}
8449
};
8550

0 commit comments

Comments
 (0)