Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. using System.Collections.Generic;
  2.  
  3. namespace yield
  4. {
  5. public static class MovingMaxTask
  6. {
  7. public static IEnumerable<DataPoint> MovingMax(this IEnumerable<DataPoint> data, int windowWidth)
  8. {
  9. var points = new LinkedList<double>();
  10. var xPoints = new Queue<double>();
  11. foreach (var point in data)
  12. {
  13. xPoints.Enqueue(point.X);
  14. if (xPoints.Count > windowWidth && points.First.Value <= xPoints.Dequeue())
  15. {
  16. points.RemoveFirst();
  17. points.RemoveFirst();
  18. }
  19. while (points.Count != 0 && points.Last.Value < point.OriginalY)
  20. {
  21. points.RemoveLast();
  22. points.RemoveLast();
  23. }
  24. points.AddLast(point.X);
  25. points.AddLast(point.OriginalY);
  26. if (points.First.Next != null) point.MaxY = points.First.Next.Value;
  27. yield return point;
  28. }
  29. }
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement