-
Notifications
You must be signed in to change notification settings - Fork 1
/
MovingMaxTask.cs
47 lines (42 loc) · 1.31 KB
/
MovingMaxTask.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace yield
{
public static class MovingMaxTask
{
private class Accountant
{
private LinkedList<Tuple<int, double>> window =
new LinkedList<Tuple<int, double>>();
private int index = 0;
private int width = 0;
public Accountant(int windowWidth)
{
width = windowWidth;
}
public void Push(double v)
{
while (0 < window.Count && window.Last.Value.Item2 <= v)
window.RemoveLast();
window.AddLast(new Tuple<int, double>(index++, v));
while (window.First.Value.Item1 < index - width)
window.RemoveFirst();
}
public double Max
{
get => window.First.Value.Item2;
}
}
public static IEnumerable<DataPoint> MovingMax(this IEnumerable<DataPoint> data, int windowWidth)
{
var accountant = new Accountant(windowWidth);
foreach (var sample in data)
{
accountant.Push(sample.OriginalY);
sample.MaxY = accountant.Max;
yield return sample;
}
}
}
}