Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* * * * *
- * RunningAverage.cs
- * ------------------------------
- *
- * This file defines two classes to perform a running average over a set number
- * of samples. One works with double values, the other with Unity's Vector4 values.
- * The "Add()" method can be used to add a new sample. The Current property returns
- * the current average. Clear can be used to clear / reset the history.
- *
- *
- * Written by Bunny83
- * 2018-08-29
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2018 Markus Göbel (Bunny83)
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- *
- * * * * */
- using System.Collections.Generic;
- using UnityEngine;
- public abstract class RunningAverage<T>
- {
- protected Queue<T> m_History = new Queue<T>();
- protected T m_TotalSum;
- protected int m_Count;
- protected abstract void AddValue(T aVal);
- protected abstract void SubtractValue(T aVal);
- public abstract T Current { get; }
- public int Count
- {
- get { return m_Count; }
- set
- {
- if (value < 1)
- value = 1;
- if (value < m_Count)
- {
- while (m_History.Count > value)
- SubtractValue(m_History.Dequeue());
- }
- m_Count = value;
- }
- }
- public RunningAverage(int aCount)
- {
- m_Count = aCount;
- if (m_Count < 1)
- m_Count = 1;
- }
- public void Add(T aValue)
- {
- if (m_History.Count >= m_Count)
- SubtractValue(m_History.Dequeue());
- m_History.Enqueue(aValue);
- AddValue(aValue);
- }
- public void Clear()
- {
- m_History.Clear();
- m_TotalSum = default(T);
- }
- }
- public class RunningDoubleAverage : RunningAverage<double>
- {
- public override double Current
- {
- get
- {
- if (m_History.Count > 0)
- return m_TotalSum / m_History.Count;
- return 0;
- }
- }
- public RunningDoubleAverage(int aCount) : base(aCount) { }
- protected override void AddValue(double aVal)
- {
- m_TotalSum += aVal;
- }
- protected override void SubtractValue(double aVal)
- {
- m_TotalSum -= aVal;
- }
- }
- public class RunningVector4Average : RunningAverage<Vector4>
- {
- public override Vector4 Current
- {
- get
- {
- if (m_History.Count > 0)
- return m_TotalSum / m_History.Count;
- return Vector4.zero;
- }
- }
- public RunningVector4Average(int aCount) : base(aCount) { }
- protected override void AddValue(Vector4 aVal)
- {
- m_TotalSum += aVal;
- }
- protected override void SubtractValue(Vector4 aVal)
- {
- m_TotalSum -= aVal;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement