forked from andrewkirillov/AForge.NET
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathIntRange.cs
221 lines (205 loc) · 6.95 KB
/
IntRange.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// AForge Core Library
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © Andrew Kirillov, 2007-2009
// andrew.kirillov@aforgenet.com
//
namespace AForge
{
using System;
/// <summary>
/// Represents an integer range with minimum and maximum values.
/// </summary>
///
/// <remarks>
/// <para>The class represents an integer range with inclusive limits -
/// both minimum and maximum values of the range are included into it.
/// Mathematical notation of such range is <b>[min, max]</b>.</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // create [1, 10] range
/// IntRange range1 = new IntRange( 1, 10 );
/// // create [5, 15] range
/// IntRange range2 = new IntRange( 5, 15 );
/// // check if values is inside of the first range
/// if ( range1.IsInside( 7 ) )
/// {
/// // ...
/// }
/// // check if the second range is inside of the first range
/// if ( range1.IsInside( range2 ) )
/// {
/// // ...
/// }
/// // check if two ranges overlap
/// if ( range1.IsOverlapping( range2 ) )
/// {
/// // ...
/// }
/// </code>
/// </remarks>
///
[Serializable]
public struct IntRange
{
private int min, max;
/// <summary>
/// Minimum value of the range.
/// </summary>
///
/// <remarks><para>The property represents minimum value (left side limit) or the range -
/// [<b>min</b>, max].</para></remarks>
///
public int Min
{
get { return min; }
set { min = value; }
}
/// <summary>
/// Maximum value of the range.
/// </summary>
///
/// <remarks><para>The property represents maximum value (right side limit) or the range -
/// [min, <b>max</b>].</para></remarks>
///
public int Max
{
get { return max; }
set { max = value; }
}
/// <summary>
/// Length of the range (deffirence between maximum and minimum values).
/// </summary>
public int Length
{
get { return max - min; }
}
/// <summary>
/// Initializes a new instance of the <see cref="IntRange"/> structure.
/// </summary>
///
/// <param name="min">Minimum value of the range.</param>
/// <param name="max">Maximum value of the range.</param>
///
public IntRange( int min, int max )
{
this.min = min;
this.max = max;
}
/// <summary>
/// Check if the specified value is inside of the range.
/// </summary>
///
/// <param name="x">Value to check.</param>
///
/// <returns><b>True</b> if the specified value is inside of the range or
/// <b>false</b> otherwise.</returns>
///
public bool IsInside( int x )
{
return ( ( x >= min ) && ( x <= max ) );
}
/// <summary>
/// Check if the specified range is inside of the range.
/// </summary>
///
/// <param name="range">Range to check.</param>
///
/// <returns><b>True</b> if the specified range is inside of the range or
/// <b>false</b> otherwise.</returns>
///
public bool IsInside( IntRange range )
{
return ( ( IsInside( range.min ) ) && ( IsInside( range.max ) ) );
}
/// <summary>
/// Check if the specified range overlaps with the range.
/// </summary>
///
/// <param name="range">Range to check for overlapping.</param>
///
/// <returns><b>True</b> if the specified range overlaps with the range or
/// <b>false</b> otherwise.</returns>
///
public bool IsOverlapping( IntRange range )
{
return ( ( IsInside( range.min ) ) || ( IsInside( range.max ) ) ||
( range.IsInside( min ) ) || ( range.IsInside( max ) ) );
}
/// <summary>
/// Implicit conversion to <see cref="Range"/>.
/// </summary>
///
/// <param name="range">Integer range to convert to single precision range.</param>
///
/// <returns>Returns new single precision range which min/max values are implicitly converted
/// to floats from min/max values of the specified integer range.</returns>
///
public static implicit operator Range( IntRange range )
{
return new Range( range.Min, range.Max );
}
/// <summary>
/// Equality operator - checks if two ranges have equal min/max values.
/// </summary>
///
/// <param name="range1">First range to check.</param>
/// <param name="range2">Second range to check.</param>
///
/// <returns>Returns <see langword="true"/> if min/max values of specified
/// ranges are equal.</returns>
///
public static bool operator ==( IntRange range1, IntRange range2 )
{
return ( ( range1.min == range2.min ) && ( range1.max == range2.max ) );
}
/// <summary>
/// Inequality operator - checks if two ranges have different min/max values.
/// </summary>
///
/// <param name="range1">First range to check.</param>
/// <param name="range2">Second range to check.</param>
///
/// <returns>Returns <see langword="true"/> if min/max values of specified
/// ranges are not equal.</returns>
///
public static bool operator !=( IntRange range1, IntRange range2 )
{
return ( ( range1.min != range2.min ) || ( range1.max != range2.max ) );
}
/// <summary>
/// Check if this instance of <see cref="Range"/> equal to the specified one.
/// </summary>
///
/// <param name="obj">Another range to check equalty to.</param>
///
/// <returns>Return <see langword="true"/> if objects are equal.</returns>
///
public override bool Equals( object obj )
{
return ( obj is IntRange ) ? ( this == (IntRange) obj ) : false;
}
/// <summary>
/// Get hash code for this instance.
/// </summary>
///
/// <returns>Returns the hash code for this instance.</returns>
///
public override int GetHashCode( )
{
return min.GetHashCode( ) + max.GetHashCode( );
}
/// <summary>
/// Get string representation of the class.
/// </summary>
///
/// <returns>Returns string, which contains min/max values of the range in readable form.</returns>
///
public override string ToString( )
{
return string.Format( System.Globalization.CultureInfo.InvariantCulture, "{0}, {1}", min, max );
}
}
}