File:  [Isaki's NoNo m68k/m88k emulator] / nono / lib / statistics.h
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Wed Apr 29 17:04:51 2026 UTC (2 months, 3 weeks ago) by root
Branches: MAIN, Isaki
CVS tags: v014, v013, v012, v011, v010, HEAD
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;
};

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.