/// <summary> /// Replaces inclusive boundaries with exclusive ones with the values from the selector callbacks /// </summary> /// <typeparam name="T">The type of the range values.</typeparam> /// <param name="range">The source range.</param> /// <param name="fromValueSelector">Callback to obtain a new value for the From boundary. Used if the boundary is inclusive.</param> /// <param name="toValueSelector">Callback to obtain a new value for the To boundary. Used if the boundary is inclusive.</param> /// <returns>A range with exclusive boundaries.</returns> public static Range <T> MakeExclusive <T>( this Range <T> range, [NotNull, InstantHandle] Func <T, T> fromValueSelector, [NotNull, InstantHandle] Func <T, T> toValueSelector) { if (range.IsEmpty || (!range.From.IsInclusiveBoundary && !range.To.IsInclusiveBoundary)) { return(range); } var from = range.From; if (from.IsInclusiveBoundary) { from = Range.BoundaryFromExclusive(fromValueSelector(from.GetValueOrDefault())); } var to = range.To; if (to.IsInclusiveBoundary) { to = Range.BoundaryToExclusive(toValueSelector(to.GetValueOrDefault())); } return(range.TryCreateRange(from, to)); }
public static Range <T> Union <T, TRange>(this Range <T> range, TRange other) where TRange : IRange <T> { if (range.IsEmpty) { return(range.TryCreateRange(other.From, other.To)); } if (other.IsEmpty) { return(range); } return(range.TryCreateRange( other.From >= range.From ? range.From : other.From, range.To >= other.To ? range.To : other.To)); }
public static Range <T> WithValues <T>( this Range <T> range, [NotNull, InstantHandle] Func <T, T> fromValueSelector, [NotNull, InstantHandle] Func <T, T> toValueSelector) { var from = range.From.WithValue(fromValueSelector); var to = range.To.WithValue(toValueSelector); return(range.TryCreateRange(from, to)); }
public static Range <T> ExtendTo <T>(this Range <T> range, RangeBoundaryTo <T> to) { if (range.IsEmpty || to.IsEmpty) { return(range); } return(range.To >= to ? range : range.TryCreateRange(range.From, to)); }
public static Range <T> ExtendFrom <T>(this Range <T> range, RangeBoundaryFrom <T> from) { if (range.IsEmpty || from.IsEmpty) { return(range); } return(range.From <= from ? range : range.TryCreateRange(from, range.To)); }
public static Range <T> TrimTo <T>(this Range <T> range, RangeBoundaryTo <T> to) => to.IsNotEmpty && range.To <= to ? range : range.TryCreateRange(range.From, to);
public static Range <T> TrimFrom <T>(this Range <T> range, RangeBoundaryFrom <T> from) => from.IsNotEmpty && range.From >= from ? range : range.TryCreateRange(from, range.To);
public static Range <T> Intersect <T, TRange>(this Range <T> range, TRange other) where TRange : IRange <T> => range.TryCreateRange( (range.IsEmpty || range.From >= other.From) ? range.From : other.From, range.To <= other.To ? range.To : other.To);