forked from Ken98045/On-Guard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMostRecentCollection.cs
More file actions
53 lines (43 loc) · 930 Bytes
/
MostRecentCollection.cs
File metadata and controls
53 lines (43 loc) · 930 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.Collections.Generic;
using System.Linq;
namespace SAAI
{
/// <summary>
/// A class to clollect a list of recent values.
/// In this case it is used to track AI processing time
/// so we can average them out.
/// </summary>
public class MostRecentCollection : List<double>
{
readonly int _numberOfItems;
readonly object _lock = new object();
public MostRecentCollection(int numberOfItems)
{
_numberOfItems = numberOfItems;
}
public void AddValue(double v)
{
lock (_lock)
{
if (Count == _numberOfItems)
{
RemoveAt(0);
}
Add(v);
}
}
public double Avg()
{
double result;
lock (_lock)
{
result = 0.0;
if (Count > 0)
{
result = this.Average();
}
}
return result;
}
}
}