Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace yield
  5. {
  6.  
  7. public static class MovingMaxTask
  8. {
  9. public class QueueItem<T>
  10. {
  11. public T Value { get; set; }
  12. public QueueItem<T> Next { get; set; }
  13. }
  14.  
  15. public class Queues<T>
  16. {
  17. public QueueItem<T> head { get; set; }
  18. QueueItem<T> tail;
  19.  
  20. public bool IsEmpty { get { return head == null; } }
  21.  
  22. public void Enqueue(T value)
  23. {
  24. if (IsEmpty)
  25. tail = head = new QueueItem<T> { Value = value, Next = null };
  26. else
  27. {
  28. var item = new QueueItem<T> { Value = value, Next = null };
  29. tail.Next = item;
  30. tail = item;
  31. }
  32. }
  33.  
  34. public T Dequeue()
  35. {
  36. if (head == null) throw new InvalidOperationException();
  37. var result = head.Value;
  38. head = head.Next;
  39. if (head == null)
  40. tail = null;
  41. return result;
  42. }
  43. }
  44.  
  45. public static void Search(Queues<DataPoint> maxes, DataPoint element)
  46. {
  47. var current = maxes.head;
  48. if (current.Value == element)
  49. {
  50. if (current.Next!= null) maxes.head = current.Next;
  51. else current.Value = null;
  52. return;
  53. }
  54.  
  55. while (current.Next != null)
  56. {
  57. if (current.Next.Value == element)
  58. {
  59. if (current.Next.Next != null) current.Next= current.Next.Next;
  60.  
  61. else current.Next.Value = null;
  62.  
  63. break;
  64. }
  65. current = current.Next;
  66. }
  67. }
  68.  
  69. public static void Checker(Queues<DataPoint> maxes, DataPoint next)
  70. {
  71. var current = maxes.head;
  72.  
  73. while (true)
  74. {
  75. if (next.OriginalY >= current.Value.OriginalY)
  76. {
  77. current.Value = next;
  78. current.Next = null;
  79. break;
  80. }
  81.  
  82. else
  83. {
  84. if (current.Next == null)
  85. {
  86. current.Next = new QueueItem<DataPoint> { Value = next, Next = null };
  87. break;
  88. }
  89. else current = current.Next;
  90. }
  91. }
  92. }
  93.  
  94.  
  95. public static IEnumerable<DataPoint> MovingMax(this IEnumerable<DataPoint> data, int windowWidth)
  96. {
  97.  
  98. var maxes = new Queues<DataPoint>();
  99. var queue = new Queues<DataPoint>();
  100. int queueCount = 0;
  101.  
  102. foreach(var next in data)
  103. {
  104. queue.Enqueue(next);
  105. queueCount += 1;
  106. if (maxes.head == null)
  107. maxes.Enqueue(next);
  108.  
  109. if (queueCount > windowWidth)
  110. {
  111. Search(maxes, queue.Dequeue());
  112. Checker(maxes, next);
  113. }
  114.  
  115. else
  116. Checker(maxes, next);
  117.  
  118. next.MaxY = maxes.head.Value.OriginalY;
  119. yield return next;
  120.  
  121. }
  122. yield break;
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement