|
|
1.1 ! root 1: // ! 2: // nono ! 3: // Copyright (C) 2021 nono project ! 4: // Licensed under nono-license.txt ! 5: // ! 6: ! 7: #pragma once ! 8: ! 9: #include <limits> ! 10: ! 11: // 統計計算 ! 12: template <class T> ! 13: class Statistics ! 14: { ! 15: public: ! 16: Statistics() ! 17: { ! 18: Clear(); ! 19: } ! 20: ! 21: // 統計をクリアする。 ! 22: void Clear() ! 23: { ! 24: count = 0; ! 25: sum = 0; ! 26: min = std::numeric_limits<T>::max(); ! 27: max = std::numeric_limits<T>::min(); ! 28: } ! 29: ! 30: // 統計に値を追加する。 ! 31: void Add(T v) ! 32: { ! 33: count++; ! 34: sum += v; ! 35: ! 36: if (min > v) { ! 37: min = v; ! 38: } ! 39: if (max < v) { ! 40: max = v; ! 41: } ! 42: } ! 43: ! 44: // 個数を返す。 ! 45: T Count() const { return count; } ! 46: ! 47: // 合計を返す。 ! 48: T Sum() const { return sum; } ! 49: ! 50: // 平均を返す。値がないときは 0 を返す。 ! 51: T Average() const { return count == 0 ? 0 : sum / count; } ! 52: ! 53: // 最小を返す。 ! 54: T Min() const { return count == 0 ? 0 : min; } ! 55: ! 56: // 最大を返す。 ! 57: T Max() const { return count == 0 ? 0 : max; } ! 58: ! 59: private: ! 60: T count; ! 61: T sum; ! 62: T min; ! 63: T max; ! 64: };
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.