|
|
nono 0.1.6
//
// nono
// Copyright (C) 2021 nono project
// Licensed under nono-license.txt
//
#pragma once
#include <limits>
// 統計計算
template <class T>
class Statistics
{
public:
Statistics()
{
Clear();
}
// 統計をクリアする。
void Clear()
{
count = 0;
sum = 0;
min = std::numeric_limits<T>::max();
max = std::numeric_limits<T>::min();
}
// 統計に値を追加する。
void Add(T v)
{
count++;
sum += v;
if (min > v) {
min = v;
}
if (max < v) {
max = v;
}
}
// 個数を返す。
T Count() const { return count; }
// 合計を返す。
T Sum() const { return sum; }
// 平均を返す。値がないときは 0 を返す。
T Average() const { return count == 0 ? 0 : sum / count; }
// 最小を返す。
T Min() const { return count == 0 ? 0 : min; }
// 最大を返す。
T Max() const { return count == 0 ? 0 : max; }
private:
T count;
T sum;
T min;
T max;
};
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.