| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="IntervalExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using MyNet.Primitives.Intervals; |
| | | 9 | | |
| | | 10 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 11 | | namespace MyNet.Primitives; |
| | | 12 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 13 | | |
| | | 14 | | public static class IntervalExtensions |
| | | 15 | | { |
| | | 16 | | extension<T>(IInterval<T> left) |
| | | 17 | | where T : struct, IComparable<T> |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Determines whether the current interval is strictly before the specified interval. An interval A is consider |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="right">The interval to compare with the current interval.</param> |
| | | 23 | | /// <returns><c>true</c> if the current interval is strictly before the specified interval; otherwise, <c>false< |
| | | 24 | | public bool IsBefore(IInterval<T> right) |
| | | 25 | | { |
| | 0 | 26 | | if (!left.HasEnd || !right.HasStart) |
| | 0 | 27 | | return false; |
| | | 28 | | |
| | 0 | 29 | | var comparison = |
| | 0 | 30 | | left.End!.Value.Value.CompareTo(right.Start!.Value.Value); |
| | | 31 | | |
| | 0 | 32 | | return comparison < 0 || (comparison <= 0 && (!left.End.Value.IsInclusive || !right.Start.Value.IsInclusive) |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Determines whether the current interval is strictly after the specified interval. An interval A is considere |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="right">The interval to compare with the current interval.</param> |
| | | 39 | | /// <returns><c>true</c> if the current interval is strictly after the specified interval; otherwise, <c>false</ |
| | 0 | 40 | | public bool IsAfter(IInterval<T> right) => right.IsBefore(left); |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Determines whether the current interval touches the specified interval. Two intervals are considered to touc |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="right">The interval to compare with the current interval.</param> |
| | | 46 | | /// <returns><c>true</c> if the current interval touches the specified interval; otherwise, <c>false</c>.</retur |
| | | 47 | | public bool Touches(IInterval<T> right) |
| | | 48 | | { |
| | 18 | 49 | | var touchesOnRight = false; |
| | 18 | 50 | | if (left.HasEnd && right.HasStart) |
| | | 51 | | { |
| | 18 | 52 | | var comparison = left.End!.Value.Value.CompareTo(right.Start!.Value.Value); |
| | 18 | 53 | | touchesOnRight = comparison == 0 && !(left.End.Value.IsInclusive && right.Start.Value.IsInclusive); |
| | | 54 | | } |
| | | 55 | | |
| | 18 | 56 | | var touchesOnLeft = false; |
| | 18 | 57 | | if (right.HasEnd && left.HasStart) |
| | | 58 | | { |
| | 18 | 59 | | var comparison = right.End!.Value.Value.CompareTo(left.Start!.Value.Value); |
| | 18 | 60 | | touchesOnLeft = comparison == 0 && !(right.End.Value.IsInclusive && left.Start.Value.IsInclusive); |
| | | 61 | | } |
| | | 62 | | |
| | 18 | 63 | | return touchesOnRight || touchesOnLeft; |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Determines whether the current interval overlaps with the specified interval. Two intervals are considered t |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="right">The interval to compare with the current interval.</param> |
| | | 70 | | /// <returns><c>true</c> if the current interval overlaps with the specified interval; otherwise, <c>false</c>.< |
| | 6 | 71 | | public bool Overlaps(IInterval<T> right) => left.Intersects(right); |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Determines whether the current interval is entirely contained within the specified container interval. An in |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="container">The interval to check against.</param> |
| | | 77 | | /// <returns><c>true</c> if the current interval is inside the specified container interval; otherwise, <c>false |
| | 0 | 78 | | public bool IsInside(IInterval<T> container) => container.Contains(left); |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Determines whether the current interval starts before the specified interval. An interval A is considered to |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="right">The interval to compare with the current interval.</param> |
| | | 84 | | /// <returns><c>true</c> if the current interval starts before the specified interval; otherwise, <c>false</c>.< |
| | | 85 | | public bool StartsBefore(IInterval<T> right) |
| | 0 | 86 | | => !left.HasStart ? right.HasStart : right.HasStart && left.Start!.Value.Value.CompareTo(right.Start!.Value. |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Determines whether the current interval ends after the specified interval. An interval A is considered to en |
| | | 90 | | /// </summary> |
| | | 91 | | /// <param name="right">The interval to compare with the current interval.</param> |
| | | 92 | | /// <returns><c>true</c> if the current interval ends after the specified interval; otherwise, <c>false</c>.</re |
| | | 93 | | public bool EndsAfter(IInterval<T> right) |
| | 0 | 94 | | => !left.HasEnd ? right.HasEnd : right.HasEnd && left.End!.Value.Value.CompareTo(right.End!.Value.Value) > 0 |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Clamps a value to be within the current interval. If the value is less than the start of the interval, it re |
| | | 98 | | /// </summary> |
| | | 99 | | /// <param name="value">The value to clamp.</param> |
| | | 100 | | /// <returns>The clamped value.</returns> |
| | | 101 | | public T Clamp(T value) => |
| | 6 | 102 | | left.HasStart && value.CompareTo(left.Start!.Value.Value) < 0 |
| | 6 | 103 | | ? left.Start.Value.Value |
| | 6 | 104 | | : left.HasEnd && value.CompareTo(left.End!.Value.Value) > 0 |
| | 6 | 105 | | ? left.End.Value.Value |
| | 6 | 106 | | : value; |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Determines whether the current interval is empty, meaning that it has no points in common. An interval is co |
| | | 110 | | /// </summary> |
| | | 111 | | /// <returns>True if the interval is empty; otherwise, false.</returns> |
| | | 112 | | public bool IsEmpty() |
| | | 113 | | { |
| | 0 | 114 | | if (!left.HasStart || !left.HasEnd) |
| | 0 | 115 | | return false; |
| | | 116 | | |
| | 0 | 117 | | var comparison = left.Start!.Value.Value.CompareTo(left.End!.Value.Value); |
| | | 118 | | |
| | 0 | 119 | | return comparison == 0 && (!left.Start.Value.IsInclusive || !left.End.Value.IsInclusive); |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | extension<T>(IBoundedInterval<T> interval) |
| | | 124 | | where T : struct, IComparable<T> |
| | | 125 | | { |
| | | 126 | | /// <summary> |
| | | 127 | | /// Gets the start value of the bounded interval. |
| | | 128 | | /// </summary> |
| | | 129 | | /// <returns>The start value of the interval.</returns> |
| | 21 | 130 | | public T StartValue() => interval.Start.Value; |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Gets the end value of the bounded interval. |
| | | 134 | | /// </summary> |
| | | 135 | | /// <returns>The end value of the interval.</returns> |
| | 18 | 136 | | public T EndValue() => interval.End.Value; |
| | | 137 | | } |
| | | 138 | | } |
| | | 139 | | |