Advertisement
Graf_Rav

Untitled

Mar 1st, 2019
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace yield {
  6.     public static class MovingMaxTask {
  7.         //структура элемента последовательности, включающая значение и номер в последовательности
  8.         private struct SequenceElement {
  9.             public int Number {
  10.                 get; private set;
  11.             }
  12.             public DataPoint Value {
  13.                 get; private set;
  14.             }
  15.  
  16.             public SequenceElement(int number, DataPoint value) {
  17.                 Number = number;
  18.                 Value = value;
  19.             }
  20.         }
  21.  
  22.         public static IEnumerable<DataPoint> MovingMax(this IEnumerable<DataPoint> data, int windowWidth) {
  23.             //Используем дек (Deque), в котором хранятся структуры (значение, номер)
  24.             //Если номер переднего элемента очереди — тот, что уходит из окна, то удаляем его из начала очереди.
  25.             //Удаляем из конца очереди элементы, значение которых меньше, чем у новодобавляемого (они уже не максимумы)
  26.             //Вставляем новый элемент в конец очереди
  27.             //Максимум на каждом шаге — в начале очереди.
  28.             //Время амортизированное O(1).
  29.             Deque<SequenceElement> sequence = new Deque<SequenceElement>();
  30.             int index = 0;
  31.             foreach (DataPoint point in data) {
  32.                 index++;
  33.                 if (index > windowWidth) {
  34.                     if (sequence.GetHeadValue().Number <= index - windowWidth) {
  35.                         sequence.RemoveHead();
  36.                     }
  37.                 }
  38.                 while(sequence.Count > 0 && sequence.GetTailValue().Value.OriginalY < point.OriginalY) {
  39.                     sequence.RemoveTail();
  40.                 }
  41.                 sequence.AddTail(new SequenceElement(index, point));
  42.                 point.MaxY = sequence.GetHeadValue().Value.OriginalY;
  43.                 yield return point;
  44.             }
  45.         }
  46.     }
  47.  
  48.  
  49.     public class Deque<T> {//Самописная двусторонняя очередь на связных списках
  50.         public class DequeElement<T> {
  51.             public DequeElement<T> Next {
  52.                 get; set;
  53.             }
  54.  
  55.             public DequeElement<T> Prev {
  56.                 get; set;
  57.             }
  58.  
  59.             public T Value;
  60.  
  61.             public DequeElement(T value) {
  62.                 Value = value;
  63.             }
  64.         }
  65.  
  66.         private DequeElement<T> head;
  67.         private DequeElement<T> tail;
  68.  
  69.         public int Count {
  70.             get; private set;
  71.         }
  72.  
  73.         public void AddHead(T value) {
  74.             if (Count == 0) {
  75.                 head = new DequeElement<T>(value);
  76.                 tail = head;
  77.             }
  78.             else {
  79.                 head.Next = new DequeElement<T>(value);
  80.                 head.Next.Prev = head;
  81.                 head = head.Next;
  82.             }
  83.             Count++;
  84.         }
  85.  
  86.         public void AddTail(T value) {
  87.             if (Count == 0) {
  88.                 head = new DequeElement<T>(value);
  89.                 tail = head;
  90.             }
  91.             else {
  92.                 tail.Prev = new DequeElement<T>(value);
  93.                 tail.Prev.Next = tail;
  94.                 tail = tail.Prev;
  95.             }
  96.             Count++;
  97.         }
  98.  
  99.         public T RemoveHead() {
  100.             if (Count == 0) {
  101.                 throw new Exception("Deque is empty");
  102.             }
  103.             T value = head.Value;
  104.             if (Count > 1) {
  105.                 head.Prev.Next = null;
  106.                 head = head.Prev;
  107.             }
  108.             Count--;
  109.             return value;
  110.         }
  111.  
  112.         public T RemoveTail() {
  113.             if (Count == 0) {
  114.                 throw new Exception("Deque is empty");
  115.             }
  116.             T value = tail.Value;
  117.             if (Count > 1) {
  118.                 tail.Next.Prev = null;
  119.                 tail = tail.Next;
  120.             }
  121.             Count--;
  122.             return value;
  123.         }
  124.  
  125.         public T GetHeadValue() {
  126.             if (Count == 0) {
  127.                 throw new Exception("Deque is empty");
  128.             }
  129.             return head.Value;
  130.         }
  131.  
  132.         public T GetTailValue() {
  133.             if (Count == 0) {
  134.                 throw new Exception("Deque is empty");
  135.             }
  136.             return tail.Value;
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement