OSDN Git Service

インストーラーのパスを変更した
[fooeditengine/FooEditEngine.git] / Common / Range.cs
1 /* https://github.com/mbuchetics/RangeTree よりコピペ。このファイルのみMITライセンスに従います */
2 using System;
3
4 namespace FooEditEngine
5 {
6     /// <summary>
7     /// Represents a range of values. 
8     /// Both values must be of the same type and comparable.
9     /// </summary>
10     /// <typeparam name="T">Type of the values.</typeparam>
11     public struct Range<T> : IComparable<Range<T>>
12         where T : IComparable<T>
13     {
14         /// <summary>
15         /// Represent is Start Point
16         /// </summary>
17         public T From;
18         /// <summary>
19         /// Represent is End Point
20         /// </summary>
21         public T To;
22
23         /// <summary>
24         /// Initializes a new <see cref="Range&lt;T&gt;"/> instance.
25         /// </summary>
26         /// <param name="value">type of T value</param>
27         public Range(T value)
28             : this()
29         {
30             From = value;
31             To = value;
32         }
33
34         /// <summary>
35         /// Initializes a new <see cref="Range&lt;T&gt;"/> instance.
36         /// </summary>
37         /// <param name="from">start value</param>
38         /// <param name="to">end value</param>
39         public Range(T from, T to)
40             : this()
41         {
42             From = from;
43             To = to;
44         }
45
46         /// <summary>
47         /// Whether the value is contained in the range. 
48         /// Border values are considered inside.
49         /// </summary>
50         /// <param name="value">type of T value</param>
51         /// <returns></returns>
52         public bool Contains(T value)
53         {
54             return value.CompareTo(From) >= 0 && value.CompareTo(To) <= 0;
55         }
56
57         /// <summary>
58         /// Whether the value is contained in the range. 
59         /// Border values are considered outside.
60         /// </summary>
61         /// <param name="value">type of T value</param>
62         /// <returns></returns>
63         public bool ContainsExclusive(T value)
64         {
65             return value.CompareTo(From) > 0 && value.CompareTo(To) < 0;
66         }
67
68         /// <summary>
69         /// Whether two ranges intersect each other.
70         /// </summary>
71         /// <param name="other">The other.</param>
72         /// <returns></returns>
73         public bool Intersects(Range<T> other)
74         {
75             return other.To.CompareTo(From) >= 0 && other.From.CompareTo(To) <= 0;
76         }
77
78         /// <summary>
79         /// Whether two ranges intersect each other.
80         /// </summary>
81         /// <param name="other">The other.</param>
82         /// <returns></returns>
83         public bool IntersectsExclusive(Range<T> other)
84         {
85             return other.To.CompareTo(From) > 0 && other.From.CompareTo(To) < 0;
86         }
87
88         /// <summary>
89         /// Returns a <see cref="System.String"/> that represents this instance.
90         /// </summary>
91         /// <returns>
92         /// A <see cref="System.String"/> that represents this instance.
93         /// </returns>
94         public override string ToString()
95         {
96             return string.Format("{0} - {1}", From, To);
97         }
98
99         /// <summary>
100         /// Obtain hash code
101         /// </summary>
102         /// <returns>hash code</returns>
103         public override int GetHashCode()
104         {
105             int hash = 23;
106             hash = hash * 37 + From.GetHashCode();
107             hash = hash * 37 + To.GetHashCode();
108             return hash;
109         }
110
111         #region IComparable<Range<T>> Members
112
113         /// <summary>
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.
117         /// </summary>
118         /// <param name="other">The other.</param>
119         /// <returns></returns>
120         public int CompareTo(Range<T> other)
121         {
122             if (From.CompareTo(other.From) < 0)
123                 return -1;
124             else if (From.CompareTo(other.From) > 0)
125                 return 1;
126             else if (To.CompareTo(other.To) < 0)
127                 return -1;
128             else if (To.CompareTo(other.To) > 0)
129                 return 1;
130             else
131                 return 0;
132         }
133
134         #endregion
135     }
136
137     /// <summary>
138     /// Static helper class to create Range instances.
139     /// </summary>
140     static class Range
141     {
142         /// <summary>
143         /// Creates and returns a new <see cref="Range&lt;T&gt;"/> instance.
144         /// </summary>
145         public static Range<T> Create<T>(T from, T to)
146             where T : IComparable<T>
147         {
148             return new Range<T>(from, to);
149         }
150     }
151
152     /// <summary>
153     /// Interface for classes which provide a range.
154     /// </summary>
155     /// <typeparam name="T"></typeparam>
156     interface IRangeProvider<T> where T : IComparable<T>
157     {
158         /// <summary>
159         /// Range Property
160         /// </summary>
161         Range<T> Range { get; }
162     }
163 }