5 #ifndef HERALD_SAMPLING_H
6 #define HERALD_SAMPLING_H
8 #include "../datatype/date.h"
12 #include <type_traits>
27 template <
typename ValT>
29 using value_type = ValT;
34 Sample() : taken(), value() {}
35 Sample(
Date sampled, ValT v) : taken(
Date{sampled.secondsSinceUnixEpoch()}), value(v) {}
36 Sample(
const Sample& other) : taken(
Date{other.taken.secondsSinceUnixEpoch()}), value(other.value) {}
37 Sample(
Sample&& other) : taken(std::move(other.taken)), value(std::move(other.value)) {}
38 template <
typename... Args>
39 Sample(
int secondsSinceEpoch,Args... args) : taken(
Date(secondsSinceEpoch)), value(ValT(args...)) {}
43 taken = std::move(other.taken);
44 value = std::move(other.value);
49 taken =
Date{other.taken.secondsSinceUnixEpoch()};
86 operator double()
const noexcept {
92 template <
typename SampleListT,
93 typename ValT =
typename SampleListT::value_type>
98 template <
typename SampleT,
100 typename SampleValueT =
typename std::remove_cv<typename SampleT::value_type>::type
103 using value_type = SampleT;
104 using difference_type = std::size_t;
106 using size_type = std::size_t;
108 static constexpr std::size_t max_size = MaxSize;
110 SampleList() : data(), oldestPosition(SIZE_MAX), newestPosition(SIZE_MAX) {}
112 SampleList(
SampleList&& other) noexcept : data(std::move(other.data)), oldestPosition(other.oldestPosition), newestPosition(other.newestPosition) {}
115 std::swap(data,other.data);
116 oldestPosition = other.oldestPosition;
117 newestPosition = other.newestPosition;
122 template <
typename... MultiSampleT>
123 SampleList(MultiSampleT... initialiserElements) : data(), oldestPosition(SIZE_MAX), newestPosition(SIZE_MAX) {
124 appendData(initialiserElements...);
134 data[newestPosition] = sample;
137 void push(
Date taken, SampleValueT val) {
139 data[newestPosition] = SampleT{taken,val};
142 std::size_t size()
const {
143 if (newestPosition == SIZE_MAX)
return 0;
144 if (newestPosition >= oldestPosition) {
146 return newestPosition - oldestPosition + 1;
149 return (1 + newestPosition) + (data.size() - oldestPosition);
152 const SampleT& operator[](std::size_t idx)
const {
153 if (newestPosition >= oldestPosition) {
154 return data[idx + oldestPosition];
156 if (idx + oldestPosition >= data.size()) {
158 return data[idx + oldestPosition - data.size()];
160 return data[idx + oldestPosition];
163 void clearBeforeDate(
const Date& before) {
164 if (SIZE_MAX == oldestPosition)
return;
165 while (oldestPosition != newestPosition) {
166 if (data[oldestPosition].taken < before) {
168 if (data.size() == oldestPosition) {
177 if (data[oldestPosition].taken < before) {
179 oldestPosition = SIZE_MAX;
180 newestPosition = SIZE_MAX;
185 oldestPosition = SIZE_MAX;
186 newestPosition = SIZE_MAX;
193 return data[newestPosition].taken;
206 std::array<SampleT,MaxSize> data;
207 std::size_t oldestPosition;
208 std::size_t newestPosition;
210 void incrementNewest() {
211 if (SIZE_MAX == newestPosition) {
215 if (newestPosition == (oldestPosition - 1)) {
217 if (oldestPosition == data.size()) {
223 if (newestPosition == data.size()) {
226 if (0 == oldestPosition) {
232 template <
typename LastT>
233 void appendData(LastT last) {
237 template <
typename FirstT,
typename SecondT,
typename... Inits>
238 void appendData(FirstT first, SecondT second, Inits... initialiserElements) {
240 appendData(second, initialiserElements...);
244 template <
typename... ValT,
typename CommonValT =
typename std::common_type_t<ValT...>,
typename CommonValTValue =
typename CommonValT::value_type>
249 template <
typename SampleListT,
252 using difference_type = std::size_t;
253 using value_type = ValT;
254 using iterator_category = std::forward_iterator_tag;
255 using pointer = value_type*;
256 using reference = value_type&;
259 SampleIterator(SampleListT& sl, std::size_t from) : list(sl), pos(from) {}
265 const ValT& operator*() {
272 if (pos > list.size()) {
290 return pos - other.pos;
307 return pos == otherIter.pos;
310 bool operator!=(
const SampleIterator<SampleListT>& otherIter)
const {
311 return pos != otherIter.pos;
SampleIterator< T >::difference_type distance(SampleIterator< T > first, SampleIterator< T > last)
for std::distance
Definition: sampling.h:321
std::size_t SampledID
Definition: sampling.h:24
Contains all low-level Herald datatype implementations.
Definition: base64_string.h:14
Acts as a non-global memory arena for arbitrary classes.
Definition: aggregates.h:15
The Sample taken from an object with ID of type SampledID.
Definition: sampling.h:28
FWD DECLARATION.
Definition: sampling.h:251
SampleIterator< SampleListT > & operator-(std::size_t by)
Implement operator+(int amt) to move this iterator forward.
Definition: sampling.h:279
SampleIterator< SampleListT > & operator+(std::size_t by)
Implement operator+(int amt) to move this iterator forward.
Definition: sampling.h:270
SampleIterator< SampleListT > & operator++()
prefix operator
Definition: sampling.h:294
Definition: sampling.h:102