Beispiel #1
0
        /// <summary>
        /// Sliding windows
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source">Source</param>
        /// <param name="beforeCapacity">Before Capacity</param>
        /// <param name="afterCapacity">After Capacity</param>
        /// <returns>Sliding Window</returns>
        public static IEnumerable <SlidingWindow <T> > SlidingWindow <T>(this IEnumerable <T> source,
                                                                         int beforeCapacity,
                                                                         int afterCapacity,
                                                                         bool completedOnly,
                                                                         EventHandler <SlidingWindowChangedEventArgs <T> > changed)
        {
            if (null == source)
            {
                throw new ArgumentNullException(nameof(source));
            }
            else if (beforeCapacity < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(beforeCapacity));
            }
            else if (afterCapacity < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(afterCapacity));
            }

            SlidingWindow <T> window = new SlidingWindow <T>(beforeCapacity, afterCapacity);

            if (null != changed)
            {
                window.Changed += changed;
            }

            try {
                foreach (T item in source)
                {
                    window.Enqueue(item);

                    if (completedOnly && window.IsCompleted)
                    {
                        yield return(window);
                    }
                }

                while (window.Dequeue())
                {
                    if (completedOnly && window.IsCompleted)
                    {
                        yield return(window);
                    }
                }
            }
            finally {
                if (null != changed)
                {
                    window.Changed -= changed;
                }
            }
        }
Beispiel #2
0
 /// <summary>
 /// Standard constructor
 /// </summary>
 public SlidingWindowChangedEventArgs(
     SlidingWindow <T> window,
     Optional <T> current,
     Optional <T> currentPrevious,
     Optional <T> itemAdded,
     Optional <T> itemRemoved,
     bool completed,
     bool completedPrevious)
 {
     Window            = window;
     Current           = current;
     CurrentPrevious   = currentPrevious;
     ItemAdded         = itemAdded;
     ItemRemoved       = itemRemoved;
     Completed         = completed;
     CompletedPrevious = completedPrevious;
 }