44 typedef std::vector<MCSampleStatistic<T>* >
List;
64 Size(size < 2 ? 2 : size), Valid(false), LastElementIndex(-1)
66 AddStatistic(statistic);
112 return LastElementIndex == -1;
138 if (unlikely(IsEmpty()))
146 return LastElementIndex+1;
161 Statistics.push_back(&statistic);
176 return *Statistics[0];
190 for (
auto& statistic : Statistics)
192 if (statistic->GetName() == statistic_name)
210 for (
auto& statistic_iter : Statistics)
212 if (*statistic_iter == &statistic)
214 statistic_iter = Statistics.erase(statistic_iter);
230 LastElementIndex = -1;
232 for (
auto& statistic : Statistics)
245 for (
auto& statistic : Statistics)
259 template <
typename U>
262 for (
auto& value : container)
279 if (unlikely(Size != other.
Size))
292 Statistics.push_back(statistic->Clone());
297 Samples =
new T[Size];
298 for (
int i = 0; i < Size; ++i)
301 for (
auto& statistic : Statistics)
302 statistic->SetStorage(Samples);
307 Weights =
new float[Size];
308 for (
int i = 0; i < Size; ++i)
329 for (
int i = 0; i < Size; ++i)
331 if (Samples[i] != other.
Samples[i])
334 for (
int i = 0; i < Size; ++i)
336 if (Weights[i] != other.
Weights[i])
353 return !operator==(other);
370 return operator+=(value);
387 return operator+=(container);
405 return operator+=(value);
456 return operator+=(std::pair<T, float>(value, 0.0));
473 for (
auto& value : container)
498 Samples =
new T[Size];
499 Weights =
new float[Size];
501 for (
auto& statistic : Statistics)
502 statistic->SetStorage(Samples);
504 int OldIndex = LastElementIndex;
509 NewIndex = LastElementIndex % Size;
510 NewerIndex = (NewIndex+1) % Size;
512 if (unlikely(!Valid && LastElementIndex == Size-1))
516 for (
auto& statistic : Statistics)
518 if (likely(statistic->IsValid()))
519 statistic->RemoveValue(Samples[NewIndex], Weights[NewIndex], Samples[NewerIndex], Weights[NewerIndex]);
523 Samples[NewIndex] = value.first;
524 Weights[NewIndex] = value.second;
525 LastElementIndex = NewIndex;
528 for (
auto& statistic : Statistics)
530 if (unlikely(OldIndex == -1))
532 statistic->AddValue(value.first, value.second, (T)0,
MCFloatInfinity());
534 statistic->AddValue(value.first, value.second, Samples[OldIndex], Weights[OldIndex]);
558 template <
typename T>
607 virtual float GetResult() = 0;
630 void AddValue(
const T value,
const float weight,
const T prev_value,
const float prev_weight)
633 if (!Add(value, weight, prev_value, prev_weight))
636 if (unlikely(!
Valid))
654 void RemoveValue(
const T value,
const float weight,
const T prev_value,
const float prev_weight)
656 Remove(value, weight, prev_value, prev_weight);
673 virtual bool Add(
const T value,
const float weight,
const T prev_value,
const float prev_weight) = 0;
685 virtual void Remove(
const T value,
const float weight,
const T next_value,
const float next_weight) = 0;
MCSamples< T > & operator=(const MCSamples< T > &other)
Copy assignment operator.
float * Weights
Weight storage.
int GetSampleCount() const
Get the sample count in the cache.
MCSampleStatistic< T > & GetDefaultStatistic()
Get the default (first) statistic.
bool operator==(const MCSamples< T > &other) const
Equality (==) operator.
MCSamples(int size, MCSampleStatistic< T > &statistic)
Class constructor.
MCSamples(const MCSamples &other)
Class copy constructor.
int LastElementIndex
Last element index in the cache.
bool Valid
Whenever the sample cache has been filled at least once to do calculations.
void RemoveValue(const T value, const float weight, const T prev_value, const float prev_weight)
Remove a value.
bool Valid
Whenever the statistic is valid after the first complete sample storage.
const T * Storage
Pointer to the sample storage.
const std::string & GetName() const
Get the statistic name.
int GetSize() const
Get the size of the sample cache.
virtual void Reset()
Reset the statistic to a default state.
bool IsEmpty() const
Check if the sample cache is empty.
virtual void SetSize(int size)
Set the sample vector size for the calculation.
bool operator!=(const MCSamples< T > &other) const
Inequality (!=) operator.
int Size
The size of the sample cache.
int Samples
Counted samples in the calculations.
MCSamples< T > & operator+=(const std::pair< const T, float > &value)
Addition assignment (+=) operator.
Sample statistic base class.
void SetStorage(const T *storage)
Set the sample storage pointer.
bool IsValid()
Check if the statistic is valid.
bool IsValid() const
Check if the sample cache is valid for calculations.
MCSampleStatistic< T > * GetStatistic(const std::string &statistic_name)
Get a statistic by name.
A wrapper class because of the template typedefs.
std::string Name
Statistic name.
void RemoveStatistic(MCSampleStatistic< T > &statistic)
Remove a statistic calculation.
T * Samples
Sample storage.
void AddValue(const T value, const float weight, const T prev_value, const float prev_weight)
Add an input value.
MCSamples< T > & operator+=(const std::vector< T > &container)
Addition assignment (+=) operator.
int Size
Sample storage size.
float MCFloatInfinity()
Get float infinity.
MCSamples< T > & operator+=(const T value)
Addition assignment (+=) operator.
const MCSamples< T > operator+(const T value) const
Increment (+) operator.
void ClearStatistics()
Clear the statistics of the sample cache.
virtual ~MCSamples()
Class destructor.
MC::Statistic< T >::List Statistics
The list of the calculated statistics for the sample cache.
void AddValues(const U &container)
Put a whole container into the cache.
std::vector< MCSampleStatistic< T > * > List
Statistic list type.
const MCSamples< T > operator+(const std::pair< const T, float > &value) const
Increment (+) operator.
MCSampleStatistic(const std::string &name)
Class constructor.
void Reset()
Get the size of the sample cache.
void AddStatistic(MCSampleStatistic< T > &statistic)
Add a statistic calculation.