// (c) Copyright Microsoft Corporation. // This source is subject to the Microsoft Public License (Ms-PL). // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details. // All other rights reserved. using System; using System.Collections.Generic; using System.Linq; namespace System.Windows.Controls.DataVisualization { /// /// Collection of functions that manipulate streams of ranges. /// internal static class RangeEnumerableExtensions { /// /// Returns the minimum and maximum values in a stream. /// /// The type of the stream. /// The stream. /// The range of values in the stream. public static Range GetRange(this IEnumerable that) where T : IComparable { IEnumerator enumerator = that.GetEnumerator(); if (!enumerator.MoveNext()) { return new Range(); } T minimum = enumerator.Current; T maximum = minimum; while (enumerator.MoveNext()) { T current = enumerator.Current; if (ValueHelper.Compare(minimum, current) == 1) { minimum = current; } if (ValueHelper.Compare(maximum, current) == -1) { maximum = current; } } return new Range(minimum, maximum); } /// /// Returns a range encompassing all ranges in a stream. /// /// The type of the minimum and maximum values. /// /// The stream. /// A range encompassing all ranges in a stream. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "Nesting necessary to provide method for use with all types of Range.")] public static Range Sum(this IEnumerable> that) where T : IComparable { Range sum = new Range(); IEnumerator> enumerator = that.GetEnumerator(); while (enumerator.MoveNext()) { sum = sum.Add(enumerator.Current); } return sum; } } }