/* https://github.com/mbuchetics/RangeTree よりコピペ。このファイルのみMITライセンスに従います */ using System; namespace FooEditEngine { /// /// Represents a range of values. /// Both values must be of the same type and comparable. /// /// Type of the values. public struct Range : IComparable> where T : IComparable { /// /// Represent is Start Point /// public T From; /// /// Represent is End Point /// public T To; /// /// Initializes a new instance. /// /// type of T value public Range(T value) : this() { From = value; To = value; } /// /// Initializes a new instance. /// /// start value /// end value public Range(T from, T to) : this() { From = from; To = to; } /// /// Whether the value is contained in the range. /// Border values are considered inside. /// /// type of T value /// public bool Contains(T value) { return value.CompareTo(From) >= 0 && value.CompareTo(To) <= 0; } /// /// Whether the value is contained in the range. /// Border values are considered outside. /// /// type of T value /// public bool ContainsExclusive(T value) { return value.CompareTo(From) > 0 && value.CompareTo(To) < 0; } /// /// Whether two ranges intersect each other. /// /// The other. /// public bool Intersects(Range other) { return other.To.CompareTo(From) >= 0 && other.From.CompareTo(To) <= 0; } /// /// Whether two ranges intersect each other. /// /// The other. /// public bool IntersectsExclusive(Range other) { return other.To.CompareTo(From) > 0 && other.From.CompareTo(To) < 0; } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override string ToString() { return string.Format("{0} - {1}", From, To); } /// /// Obtain hash code /// /// hash code public override int GetHashCode() { int hash = 23; hash = hash * 37 + From.GetHashCode(); hash = hash * 37 + To.GetHashCode(); return hash; } #region IComparable> Members /// /// Returns -1 if this range's From is less than the other, 1 if greater. /// If both are equal, To is compared, 1 if greater, -1 if less. /// 0 if both ranges are equal. /// /// The other. /// public int CompareTo(Range other) { if (From.CompareTo(other.From) < 0) return -1; else if (From.CompareTo(other.From) > 0) return 1; else if (To.CompareTo(other.To) < 0) return -1; else if (To.CompareTo(other.To) > 0) return 1; else return 0; } #endregion } /// /// Static helper class to create Range instances. /// static class Range { /// /// Creates and returns a new instance. /// public static Range Create(T from, T to) where T : IComparable { return new Range(from, to); } } /// /// Interface for classes which provide a range. /// /// interface IRangeProvider where T : IComparable { /// /// Range Property /// Range Range { get; } } }