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
97namespace 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