1 /* https://github.com/mbuchetics/RangeTree よりコピペ。このファイルのみMITライセンスに従います */
4 namespace FooEditEngine
7 /// Represents a range of values.
8 /// Both values must be of the same type and comparable.
10 /// <typeparam name="T">Type of the values.</typeparam>
11 public struct Range<T> : IComparable<Range<T>>
12 where T : IComparable<T>
15 /// Represent is Start Point
19 /// Represent is End Point
24 /// Initializes a new <see cref="Range<T>"/> instance.
26 /// <param name="value">type of T value</param>
35 /// Initializes a new <see cref="Range<T>"/> instance.
37 /// <param name="from">start value</param>
38 /// <param name="to">end value</param>
39 public Range(T from, T to)
47 /// Whether the value is contained in the range.
48 /// Border values are considered inside.
50 /// <param name="value">type of T value</param>
51 /// <returns></returns>
52 public bool Contains(T value)
54 return value.CompareTo(From) >= 0 && value.CompareTo(To) <= 0;
58 /// Whether the value is contained in the range.
59 /// Border values are considered outside.
61 /// <param name="value">type of T value</param>
62 /// <returns></returns>
63 public bool ContainsExclusive(T value)
65 return value.CompareTo(From) > 0 && value.CompareTo(To) < 0;
69 /// Whether two ranges intersect each other.
71 /// <param name="other">The other.</param>
72 /// <returns></returns>
73 public bool Intersects(Range<T> other)
75 return other.To.CompareTo(From) >= 0 && other.From.CompareTo(To) <= 0;
79 /// Whether two ranges intersect each other.
81 /// <param name="other">The other.</param>
82 /// <returns></returns>
83 public bool IntersectsExclusive(Range<T> other)
85 return other.To.CompareTo(From) > 0 && other.From.CompareTo(To) < 0;
89 /// Returns a <see cref="System.String"/> that represents this instance.
92 /// A <see cref="System.String"/> that represents this instance.
94 public override string ToString()
96 return string.Format("{0} - {1}", From, To);
102 /// <returns>hash code</returns>
103 public override int GetHashCode()
106 hash = hash * 37 + From.GetHashCode();
107 hash = hash * 37 + To.GetHashCode();
111 #region IComparable<Range<T>> Members
114 /// Returns -1 if this range's From is less than the other, 1 if greater.
115 /// If both are equal, To is compared, 1 if greater, -1 if less.
116 /// 0 if both ranges are equal.
118 /// <param name="other">The other.</param>
119 /// <returns></returns>
120 public int CompareTo(Range<T> other)
122 if (From.CompareTo(other.From) < 0)
124 else if (From.CompareTo(other.From) > 0)
126 else if (To.CompareTo(other.To) < 0)
128 else if (To.CompareTo(other.To) > 0)
138 /// Static helper class to create Range instances.
143 /// Creates and returns a new <see cref="Range<T>"/> instance.
145 public static Range<T> Create<T>(T from, T to)
146 where T : IComparable<T>
148 return new Range<T>(from, to);
153 /// Interface for classes which provide a range.
155 /// <typeparam name="T"></typeparam>
156 interface IRangeProvider<T> where T : IComparable<T>
161 Range<T> Range { get; }