Advertisement
Bunny83

RunningAverage.cs

Aug 28th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.77 KB | None | 0 0
  1. /* * * * *
  2.  * RunningAverage.cs
  3.  * ------------------------------
  4.  *
  5.  * This file defines two classes to perform a running average over a set number
  6.  * of samples. One works with double values, the other with Unity's Vector4 values.
  7.  * The "Add()" method can be used to add a new sample. The Current property returns
  8.  * the current average. Clear can be used to clear / reset the history.
  9.  *
  10.  *
  11.  * Written by Bunny83
  12.  * 2018-08-29
  13.  *
  14.  * The MIT License (MIT)
  15.  *
  16.  * Copyright (c) 2018 Markus Göbel (Bunny83)
  17.  *
  18.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  19.  * of this software and associated documentation files (the "Software"), to deal
  20.  * in the Software without restriction, including without limitation the rights
  21.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  22.  * copies of the Software, and to permit persons to whom the Software is
  23.  * furnished to do so, subject to the following conditions:
  24.  *
  25.  * The above copyright notice and this permission notice shall be included in all
  26.  * copies or substantial portions of the Software.
  27.  *
  28.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  29.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  30.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  31.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  32.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  33.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  34.  * SOFTWARE.
  35.  *
  36.  * * * * */
  37.  
  38. using System.Collections.Generic;
  39. using UnityEngine;
  40.  
  41. public abstract class RunningAverage<T>
  42. {
  43.     protected Queue<T> m_History = new Queue<T>();
  44.     protected T m_TotalSum;
  45.     protected int m_Count;
  46.     protected abstract void AddValue(T aVal);
  47.     protected abstract void SubtractValue(T aVal);
  48.     public abstract T Current { get; }
  49.     public int Count
  50.     {
  51.         get { return m_Count; }
  52.         set
  53.         {
  54.             if (value < 1)
  55.                 value = 1;
  56.             if (value < m_Count)
  57.             {
  58.                 while (m_History.Count > value)
  59.                     SubtractValue(m_History.Dequeue());
  60.             }
  61.             m_Count = value;
  62.         }
  63.     }
  64.  
  65.     public RunningAverage(int aCount)
  66.     {
  67.         m_Count = aCount;
  68.         if (m_Count < 1)
  69.             m_Count = 1;
  70.     }
  71.     public void Add(T aValue)
  72.     {
  73.         if (m_History.Count >= m_Count)
  74.             SubtractValue(m_History.Dequeue());
  75.         m_History.Enqueue(aValue);
  76.         AddValue(aValue);
  77.     }
  78.     public void Clear()
  79.     {
  80.         m_History.Clear();
  81.         m_TotalSum = default(T);
  82.     }
  83. }
  84.  
  85. public class RunningDoubleAverage : RunningAverage<double>
  86. {
  87.     public override double Current
  88.     {
  89.         get
  90.         {
  91.             if (m_History.Count > 0)
  92.                 return m_TotalSum / m_History.Count;
  93.             return 0;
  94.         }
  95.     }
  96.     public RunningDoubleAverage(int aCount) : base(aCount) { }
  97.  
  98.     protected override void AddValue(double aVal)
  99.     {
  100.         m_TotalSum += aVal;
  101.     }
  102.  
  103.     protected override void SubtractValue(double aVal)
  104.     {
  105.         m_TotalSum -= aVal;
  106.     }
  107. }
  108.  
  109. public class RunningVector4Average : RunningAverage<Vector4>
  110. {
  111.     public override Vector4 Current
  112.     {
  113.         get
  114.         {
  115.             if (m_History.Count > 0)
  116.                 return m_TotalSum / m_History.Count;
  117.             return Vector4.zero;
  118.         }
  119.     }
  120.     public RunningVector4Average(int aCount) : base(aCount) { }
  121.  
  122.     protected override void AddValue(Vector4 aVal)
  123.     {
  124.         m_TotalSum += aVal;
  125.     }
  126.  
  127.     protected override void SubtractValue(Vector4 aVal)
  128.     {
  129.         m_TotalSum -= aVal;
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement