Compare() public static method

Compares two IComparables returning -1 if the left is null and 1 if the right is null.
public static Compare ( IComparable left, IComparable right ) : int
left IComparable The left comparable.
right IComparable The right comparable.
return int
示例#1
0
        /// <summary>
        /// Modifies a range to respect the minimum and maximum axis values.
        /// </summary>
        /// <param name="range">The range of data.</param>
        /// <returns>A range modified to  respect the minimum and maximum axis
        /// values.</returns>
        private Range <IComparable> EnforceMaximumAndMinimum(Range <IComparable> range)
        {
            if (range.HasData)
            {
                IComparable minimum = ProtectedMinimum ?? range.Minimum;
                IComparable maximum = ProtectedMaximum ?? range.Maximum;

                if (ValueHelper.Compare(minimum, maximum) > 0)
                {
                    IComparable temp = maximum;
                    maximum = minimum;
                    minimum = temp;
                }

                return(new Range <IComparable>(minimum, maximum));
            }
            else
            {
                IComparable minimum = ProtectedMinimum;
                IComparable maximum = ProtectedMaximum;
                if (ProtectedMinimum != null && ProtectedMaximum == null)
                {
                    maximum = minimum;
                }
                else if (ProtectedMaximum != null && ProtectedMinimum == null)
                {
                    minimum = maximum;
                }
                else
                {
                    return(range);
                }
                return(new Range <IComparable>(minimum, maximum));
            }
        }
示例#2
0
        /// <summary>
        /// Returns a sequence of major values.
        /// </summary>
        /// <param name="availableSize">The available size.</param>
        /// <returns>A sequence of major values.</returns>
        protected virtual IEnumerable <DateTime> GetMajorAxisValues(Size availableSize)
        {
            if (!ActualRange.HasData || ValueHelper.Compare(ActualRange.Minimum, ActualRange.Maximum) == 0 || GetLength(availableSize) == 0.0)
            {
                yield break;
            }

            this.ActualInterval = CalculateActualInterval(availableSize);
            DateTime date = ActualDateTimeRange.Minimum;

            DateTime start = AlignIntervalStart(date, this.ActualInterval, ActualIntervalType);

            while (start < date)
            {
                start = IncrementDateTime(start, this.ActualInterval);
            }

            IEnumerable <DateTime> intermediateDates =
                EnumerableFunctions
                .Iterate(start, next => IncrementDateTime(next, this.ActualInterval))
                .TakeWhile(current => ActualDateTimeRange.Contains(current));

            foreach (DateTime current in intermediateDates)
            {
                yield return(current);
            }
        }
        /// <summary>
        /// Returns the minimum and maximum values in a stream.
        /// </summary>
        /// <typeparam name="T">The type of the stream.</typeparam>
        /// <param name="that">The stream.</param>
        /// <returns>The range of values in the stream.</returns>
        public static Range <T> GetRange <T>(this IEnumerable <T> that)
            where T : IComparable
        {
            IEnumerator <T> enumerator = that.GetEnumerator();

            if (!enumerator.MoveNext())
            {
                return(new Range <T>());
            }
            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 <T>(minimum, maximum));
        }
示例#4
0
        /////// <summary>
        /////// Returns a new range that contains the value.
        /////// </summary>
        /////// <param name="value">The value to extend the range to.</param>
        /////// <returns>The range which contains the value.</returns>
        ////public Range<T> ExtendTo(T value)
        ////{
        ////    if (!HasData)
        ////    {
        ////        return new Range<T>(value, value);
        ////    }

        ////    if (ValueHelper.Compare(Minimum, value) > 0)
        ////    {
        ////        return new Range<T>(value, Maximum);
        ////    }
        ////    else if (ValueHelper.Compare(Maximum, value) < 0)
        ////    {
        ////        return new Range<T>(Minimum, value);
        ////    }

        ////    return this;
        ////}

        /// <summary>
        /// Returns a value indicating whether two ranges intersect.
        /// </summary>
        /// <param name="range">The range to compare against this range.</param>
        /// <returns>A value indicating whether the ranges intersect.</returns>
        public bool IntersectsWith(Range <T> range)
        {
            if (!this.HasData || !range.HasData)
            {
                return(false);
            }

            Func <Range <T>, Range <T>, bool> rightCollidesWithLeft =
                (leftRange, rightRange) =>
                (ValueHelper.Compare(rightRange.Minimum, leftRange.Maximum) <= 0 && ValueHelper.Compare(rightRange.Minimum, leftRange.Minimum) >= 0) ||
                (ValueHelper.Compare(leftRange.Minimum, rightRange.Maximum) <= 0 && ValueHelper.Compare(leftRange.Minimum, rightRange.Minimum) >= 0);

            return(rightCollidesWithLeft(this, range) || rightCollidesWithLeft(range, this));
        }
示例#5
0
        /// <summary>
        /// Adds a range to the current range.
        /// </summary>
        /// <param name="range">A range to add to the current range.</param>
        /// <returns>A new range that encompasses the instance range and the
        /// range parameter.</returns>
        public Range <T> Add(Range <T> range)
        {
            if (!this.HasData)
            {
                return(range);
            }
            else if (!range.HasData)
            {
                return(this);
            }
            T minimum = ValueHelper.Compare(this.Minimum, range.Minimum) == -1 ? this.Minimum : range.Minimum;
            T maximum = ValueHelper.Compare(this.Maximum, range.Maximum) == 1 ? this.Maximum : range.Maximum;

            return(new Range <T>(minimum, maximum));
        }
示例#6
0
        /// <summary>
        /// Initializes a new instance of the Range class.
        /// </summary>
        /// <param name="minimum">The minimum value.</param>
        /// <param name="maximum">The maximum value.</param>
        public Range(T minimum, T maximum)
        {
            if (minimum == null)
            {
                throw new ArgumentNullException("minimum");
            }
            if (maximum == null)
            {
                throw new ArgumentNullException("maximum");
            }

            _hasData = true;
            _minimum = minimum;
            _maximum = maximum;

            int compareValue = ValueHelper.Compare(minimum, maximum);

            if (compareValue == 1)
            {
                throw new InvalidOperationException(Resources.Range_ctor_MaximumValueMustBeLargerThanOrEqualToMinimumValue);
            }
        }
        /// <summary>
        /// Returns a sequence of major axis values.
        /// </summary>
        /// <param name="availableSize">The available size.</param>
        /// <returns>A sequence of major axis values.
        /// </returns>
        private IEnumerable <double> GetMajorValues(Size availableSize)
        {
            if (!ActualRange.HasData || ValueHelper.Compare(ActualRange.Minimum, ActualRange.Maximum) == 0 || GetLength(availableSize) == 0.0)
            {
                yield break;
            }
            this.ActualInterval = CalculateActualInterval(availableSize);
            double startValue = AlignToInterval(ActualDoubleRange.Minimum, this.ActualInterval);

            if (startValue < ActualDoubleRange.Minimum)
            {
                startValue = AlignToInterval(ActualDoubleRange.Minimum + this.ActualInterval, this.ActualInterval);
            }
            double nextValue = startValue;

            for (int counter = 1; nextValue <= ActualDoubleRange.Maximum; counter++)
            {
                yield return(nextValue);

                nextValue = startValue + (counter * this.ActualInterval);
            }
        }
示例#8
0
 /// <summary>
 /// Returns a value indicating whether a value is within a range.
 /// </summary>
 /// <param name="value">The value.</param>
 /// <returns>Whether the value is within the range.</returns>
 public bool Contains(T value)
 {
     return(ValueHelper.Compare(Minimum, value) <= 0 && ValueHelper.Compare(value, Maximum) <= 0);
 }
示例#9
0
        protected override Range <IComparable> OverrideDataRange(Range <IComparable> range)
        {
            Range <IComparable> overriddenActualRange = range;

            if (!overriddenActualRange.HasData)
            {
                int year = DateTime.Now.Year;
                return(new Range <IComparable>(new DateTime(year, 1, 1), new DateTime(year + 1, 1, 1)));
            }
            else if (ValueHelper.Compare(overriddenActualRange.Minimum, overriddenActualRange.Maximum) == 0)
            {
                DateTime minimum  = ValueHelper.ToDateTime(overriddenActualRange.Minimum);
                DateTime midpoint = ((DateTime.MinValue == minimum) ? DateTime.Now : minimum).Date;
                return(new Range <IComparable>(midpoint.AddMonths(-6), midpoint.AddMonths(6)));
            }

            // ActualLength of 1.0 or less maps all points to the same coordinate
            if (range.HasData && this.ActualLength > 1.0)
            {
                IList <ValueMarginCoordinateAndOverlap> valueMargins = new List <ValueMarginCoordinateAndOverlap>();
                foreach (ValueMargin valueMargin in
                         this.RegisteredListeners
                         .OfType <IValueMarginProvider>()
                         .SelectMany(provider => provider.GetValueMargins(this)))
                {
                    valueMargins.Add(
                        new ValueMarginCoordinateAndOverlap
                    {
                        ValueMargin = valueMargin,
                    });
                }

                if (valueMargins.Count > 0)
                {
                    double maximumPixelMarginLength =
                        valueMargins
                        .Select(valueMargin => valueMargin.ValueMargin.LowMargin + valueMargin.ValueMargin.HighMargin)
                        .MaxOrNullable().Value;

                    // Requested margin is larger than the axis so give up
                    // trying to find a range that will fit it.
                    if (maximumPixelMarginLength > this.ActualLength)
                    {
                        return(range);
                    }

                    Range <DateTime> currentRange = range.ToDateTimeRange();

                    // Ensure range is not empty.
                    if (currentRange.Minimum == currentRange.Maximum)
                    {
                        int year = DateTime.Now.Year;
                        currentRange = new Range <DateTime>(new DateTime(year, 1, 1), new DateTime(year + 1, 1, 1));
                    }

                    // priming the loop
                    double actualLength = this.ActualLength;
                    ValueMarginCoordinateAndOverlap maxLeftOverlapValueMargin;
                    ValueMarginCoordinateAndOverlap maxRightOverlapValueMargin;
                    UpdateValueMargins(valueMargins, currentRange.ToComparableRange());
                    GetMaxLeftAndRightOverlap(valueMargins, out maxLeftOverlapValueMargin, out maxRightOverlapValueMargin);

                    while (maxLeftOverlapValueMargin.LeftOverlap > 0 || maxRightOverlapValueMargin.RightOverlap > 0)
                    {
                        long     unitOverPixels = currentRange.GetLength().Value.Ticks / ((long)actualLength);
                        DateTime newMinimum     = new DateTime(currentRange.Minimum.Ticks - (long)((maxLeftOverlapValueMargin.LeftOverlap + 0.5) * unitOverPixels));
                        DateTime newMaximum     = new DateTime(currentRange.Maximum.Ticks + (long)((maxRightOverlapValueMargin.RightOverlap + 0.5) * unitOverPixels));

                        currentRange = new Range <DateTime>(newMinimum, newMaximum);
                        UpdateValueMargins(valueMargins, currentRange.ToComparableRange());
                        GetMaxLeftAndRightOverlap(valueMargins, out maxLeftOverlapValueMargin, out maxRightOverlapValueMargin);
                    }

                    return(currentRange.ToComparableRange());
                }
            }

            return(range);
        }
        /// <summary>
        /// Overrides the actual range to ensure that it is never set to an
        /// empty range.
        /// </summary>
        /// <param name="range">The range to override.</param>
        /// <returns>Returns the overridden range.</returns>
        protected override Range <IComparable> OverrideDataRange(Range <IComparable> range)
        {
            range = base.OverrideDataRange(range);
            if (!range.HasData)
            {
                return(new Range <IComparable>(0.0, 1.0));
            }
            else if (ValueHelper.Compare(range.Minimum, range.Maximum) == 0)
            {
                Range <IComparable> outputRange = new Range <IComparable>((ValueHelper.ToDouble(range.Minimum)) - 1, (ValueHelper.ToDouble(range.Maximum)) + 1);
                return(outputRange);
            }

            // ActualLength of 1.0 or less maps all points to the same coordinate
            if (range.HasData && this.ActualLength > 1.0)
            {
                bool isDataAnchoredToOrigin = false;
                IList <ValueMarginCoordinateAndOverlap> valueMargins = new List <ValueMarginCoordinateAndOverlap>();
                foreach (IValueMarginProvider valueMarginProvider in this.RegisteredListeners.OfType <IValueMarginProvider>())
                {
                    foreach (ValueMargin valueMargin in valueMarginProvider.GetValueMargins(this))
                    {
                        IAnchoredToOrigin dataAnchoredToOrigin = valueMarginProvider as IAnchoredToOrigin;
                        isDataAnchoredToOrigin = (dataAnchoredToOrigin != null && dataAnchoredToOrigin.AnchoredAxis == this);

                        valueMargins.Add(
                            new ValueMarginCoordinateAndOverlap
                        {
                            ValueMargin = valueMargin,
                        });
                    }
                }

                if (valueMargins.Count > 0)
                {
                    double maximumPixelMarginLength =
                        valueMargins
                        .Select(valueMargin => valueMargin.ValueMargin.LowMargin + valueMargin.ValueMargin.HighMargin)
                        .MaxOrNullable().Value;

                    // Requested margin is larger than the axis so give up
                    // trying to find a range that will fit it.
                    if (maximumPixelMarginLength > this.ActualLength)
                    {
                        return(range);
                    }

                    Range <double> originalRange = range.ToDoubleRange();
                    Range <double> currentRange  = range.ToDoubleRange();

                    // Ensure range is not empty.
                    if (currentRange.Minimum == currentRange.Maximum)
                    {
                        currentRange = new Range <double>(currentRange.Maximum - 1, currentRange.Maximum + 1);
                    }

                    // priming the loop
                    double actualLength = this.ActualLength;
                    ValueMarginCoordinateAndOverlap maxLeftOverlapValueMargin;
                    ValueMarginCoordinateAndOverlap maxRightOverlapValueMargin;
                    UpdateValueMargins(valueMargins, currentRange.ToComparableRange());
                    GetMaxLeftAndRightOverlap(valueMargins, out maxLeftOverlapValueMargin, out maxRightOverlapValueMargin);

                    while (maxLeftOverlapValueMargin.LeftOverlap > 0 || maxRightOverlapValueMargin.RightOverlap > 0)
                    {
                        double unitOverPixels = currentRange.GetLength().Value / actualLength;
                        double newMinimum     = currentRange.Minimum - ((maxLeftOverlapValueMargin.LeftOverlap + 0.5) * unitOverPixels);
                        double newMaximum     = currentRange.Maximum + ((maxRightOverlapValueMargin.RightOverlap + 0.5) * unitOverPixels);

                        currentRange = new Range <double>(newMinimum, newMaximum);
                        UpdateValueMargins(valueMargins, currentRange.ToComparableRange());
                        GetMaxLeftAndRightOverlap(valueMargins, out maxLeftOverlapValueMargin, out maxRightOverlapValueMargin);
                    }

                    if (isDataAnchoredToOrigin)
                    {
                        if (originalRange.Minimum >= 0 && currentRange.Minimum < 0)
                        {
                            currentRange = new Range <double>(0, currentRange.Maximum);
                        }
                        else if (originalRange.Maximum <= 0 && currentRange.Maximum > 0)
                        {
                            currentRange = new Range <double>(currentRange.Minimum, 0);
                        }
                    }

                    return(currentRange.ToComparableRange());
                }
            }
            return(range);
        }