-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMovingMaxTask.cs
More file actions
33 lines (32 loc) · 1019 Bytes
/
MovingMaxTask.cs
File metadata and controls
33 lines (32 loc) · 1019 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
using System;
using System.Collections.Generic;
using System.Linq;
namespace yield
{
public static class MovingMaxTask
{
public static IEnumerable<DataPoint> MovingMax(this IEnumerable<DataPoint> data, int windowWidth)
{
if (windowWidth <= 0)
throw new System.ArgumentOutOfRangeException();
int i = 1;
LinkedList<double> maxPotentials = new LinkedList<double>();
Queue<double> windowNumbers = new Queue<double>();
foreach (DataPoint dataPoint in data)
{
if (i <= windowWidth)
i++;
else if (maxPotentials.Count == 0)
windowNumbers.Dequeue();
else if (maxPotentials.First.Value == windowNumbers.Dequeue())
maxPotentials.RemoveFirst();
windowNumbers.Enqueue(dataPoint.OriginalY);
while (maxPotentials.Count > 0 && maxPotentials.Last.Value <= dataPoint.OriginalY)
maxPotentials.RemoveLast();
maxPotentials.AddLast(dataPoint.OriginalY);
var newDataPoint = dataPoint.WithMaxY(maxPotentials.First.Value);
yield return newDataPoint;
}
}
}
}