-
Notifications
You must be signed in to change notification settings - Fork 10
/
GorgonRange.cs
420 lines (375 loc) · 18.2 KB
/
GorgonRange.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Gorgon.
// Copyright (C) 2024 Michael Winsor
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// Created: March 15, 2024 6:15:16 PM
//
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.Json.Serialization;
using Gorgon.Properties;
namespace Gorgon.Core;
/// <summary>
/// A type that represents a range between two values.
/// </summary>
/// <typeparam name="T">The type of numeric value. Must be a value type and implememnt <see cref="INumber{TSelf}"/>.</typeparam>
/// <remarks>
/// <para>
/// This type is used to represent the range from a <see cref="Minimum"/> to a <see cref="Maximum"/> value. It is useful for determining whether a value lies between that <see cref="Minimum"/> and
/// <see cref="Maximum"/>, or even clamping a value between the <see cref="Minimum"/> and <see cref="Maximum"/>.
/// </para>
/// <para>
/// This type can also be converted, explicitly, to and from the .NET <see cref="Range"/> type. Making conversions like this possible:
/// <code language="csharp">
/// <![CDATA[GorgonRange<int> myRange = (GorgonRange<int>)(10..20);]]>
/// </code>
/// </para>
/// </remarks>
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public readonly struct GorgonRange<T>
: IComparable<GorgonRange<T>>, IGorgonEquatableByRef<GorgonRange<T>>
where T : struct, INumber<T>
{
/// <summary>
/// The size of the this value, in bytes.
/// </summary>
public static readonly int SizeInBytes = Unsafe.SizeOf<GorgonRange<T>>();
/// <summary>
/// An empty range value.
/// </summary>
public static readonly GorgonRange<T> Empty = new(default, default);
/// <summary>
/// The minimum value in the range.
/// </summary>
public readonly T Minimum;
/// <summary>
/// The maximum value in the range.
/// </summary>
public readonly T Maximum;
/// <summary>
/// Property to return the range between the two values.
/// </summary>
[JsonIgnore]
public readonly T Range => Maximum - Minimum;
/// <summary>
/// Property to return whether the range is empty or not.
/// </summary>
[JsonIgnore]
public bool IsEmpty => Maximum.Equals(default) && Minimum.Equals(default);
/// <summary>
/// Function to deconstruct this type into its component values.
/// </summary>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
public void Deconstruct(out T min, out T max)
{
min = Minimum;
max = Maximum;
}
/// <summary>
/// Function to expand a <see cref="GorgonRange{T}"/> by a specific amount.
/// </summary>
/// <param name="range">A <see cref="GorgonRange{T}"/> to expand.</param>
/// <param name="amount">The amount to expand the <see cref="GorgonRange{T}"/> by.</param>
/// <param name="result">A new <see cref="GorgonRange{T}"/> value, increased in size by <paramref name="amount"/>.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Expand(ref readonly GorgonRange<T> range, T amount, out GorgonRange<T> result)
{
T min = range.Minimum - amount;
T max = range.Maximum + amount;
result = new(min, max);
}
/// <summary>
/// Function to expand a <see cref="GorgonRange{T}"/> by a specific amount.
/// </summary>
/// <param name="range">A <see cref="GorgonRange{T}"/> to expand.</param>
/// <param name="amount">The amount to expand the <see cref="GorgonRange{T}"/> by.</param>
/// <returns>A new <see cref="GorgonRange{T}"/> value, increased in size by <paramref name="amount"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static GorgonRange<T> Expand(GorgonRange<T> range, T amount)
{
Expand(in range, amount, out GorgonRange<T> result);
return result;
}
/// <summary>
/// Function to shift the range <see cref="GorgonRange{T}.Minimum"/> and <see cref="GorgonRange{T}.Maximum"/> by a specific amount.
/// </summary>
/// <param name="range">A <see cref="GorgonRange{T}"/> to shift.</param>
/// <param name="amount">The amount to shift the <see cref="GorgonRange{T}"/> extents by.</param>
/// <param name="result">A new <see cref="GorgonRange{T}"/> value, with its <see cref="GorgonRange{T}.Minimum"/> and <see cref="GorgonRange{T}.Maximum"/> shifted by the <paramref name="amount"/>.</param>
/// <remarks>
/// <para>
/// This will not alter the size of the range, just the <see cref="GorgonRange{T}.Minimum"/> and <see cref="GorgonRange{T}.Maximum"/>.
/// </para>
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Shift(ref readonly GorgonRange<T> range, T amount, out GorgonRange<T> result)
{
T min = range.Minimum + amount;
T max = range.Maximum + amount;
result = new(min, max);
}
/// <summary>
/// Function to shift the range <see cref="GorgonRange{T}.Minimum"/> and <see cref="GorgonRange{T}.Maximum"/> by a specific amount.
/// </summary>
/// <param name="range">A <see cref="GorgonRange{T}"/> to shift.</param>
/// <param name="amount">The amount to shift the <see cref="GorgonRange{T}"/> extents by.</param>
/// <returns>A new <see cref="GorgonRange{T}"/> value, with its <see cref="GorgonRange{T}.Minimum"/> and <see cref="GorgonRange{T}.Maximum"/> shifted by the <paramref name="amount"/>.</returns>
/// <remarks>
/// <para>
/// This will not alter the size of the range, just the <see cref="GorgonRange{T}.Minimum"/> and <see cref="GorgonRange{T}.Maximum"/>.
/// </para>
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static GorgonRange<T> Shift(GorgonRange<T> range, T amount)
{
Shift(in range, amount, out GorgonRange<T> result);
return result;
}
/// <summary>
/// Function to produce the union of two ranges.
/// </summary>
/// <param name="left">The left <see cref="GorgonRange{T}"/> value to join in the union.</param>
/// <param name="right">The right <see cref="GorgonRange{T}"/> value to join in the union..</param>
/// <param name="result">A new <see cref="GorgonRange{T}"/> representing the union of both ranges.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Union(ref readonly GorgonRange<T> left, ref readonly GorgonRange<T> right, out GorgonRange<T> result)
{
T newMin = left.Minimum < right.Minimum ? left.Minimum : right.Minimum;
T newMax = left.Maximum > right.Maximum ? left.Maximum : right.Maximum;
result = new(newMin, newMax);
}
/// <summary>
/// Function to produce the union of two ranges.
/// </summary>
/// <param name="left">The left <see cref="GorgonRange{T}"/> value to join in the union.</param>
/// <param name="right">The right <see cref="GorgonRange{T}"/> value to join in the union..</param>
/// <returns>A new <see cref="GorgonRange{T}"/> representing the union of both ranges.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static GorgonRange<T> Union(GorgonRange<T> left, GorgonRange<T> right)
{
Union(in left, in right, out GorgonRange<T> result);
return result;
}
/// <summary>
/// Function to intersect two ranges.
/// </summary>
/// <param name="left">The left <see cref="GorgonRange{T}"/> to intersect.</param>
/// <param name="right">The right <see cref="GorgonRange{T}"/> to intersect.</param>
/// <param name="result">The result of the intersection.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Intersect(ref readonly GorgonRange<T> left, ref readonly GorgonRange<T> right, out GorgonRange<T> result)
{
if (!left.Intersects(right))
{
result = Empty;
return;
}
T newMin = left.Minimum > right.Minimum ? left.Minimum : right.Minimum;
T newMax = left.Maximum < right.Maximum ? left.Maximum : right.Maximum;
result = new(newMin, newMax);
}
/// <summary>
/// Function to intersect two ranges.
/// </summary>
/// <param name="left">The left <see cref="GorgonRange{T}"/> to intersect.</param>
/// <param name="right">The right <see cref="GorgonRange{T}"/> to intersect.</param>
/// <returns>The result of the intersection.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static GorgonRange<T> Intersect(GorgonRange<T> left, GorgonRange<T> right)
{
Intersect(in left, in right, out GorgonRange<T> result);
return result;
}
/// <summary>
/// Function to return whether the value falls within this <see cref="GorgonRange{T}"/>.
/// </summary>
/// <param name="value">Value to evaluate.</param>
/// <returns><b>true</b> if the value falls into the range, <b>false</b> if not.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool Contains(T value) => value >= Minimum && value <= Maximum;
/// <summary>
/// Indicates whether this instance and a specified object are equal.
/// </summary>
/// <param name="obj">Another object to compare to.</param>
/// <returns>
/// true if <paramref name="obj"/> and this instance are the same type and represent the same value; otherwise, false.
/// </returns>
public override readonly bool Equals(object? obj) => obj is GorgonRange<T> range ? Equals(in this, in range) : base.Equals(obj);
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// </returns>
public override readonly int GetHashCode() => HashCode.Combine(Minimum, Maximum);
/// <summary>
/// Returns the fully qualified type name of this instance.
/// </summary>
/// <returns>
/// A <see cref="string"/> containing a fully qualified type name.
/// </returns>
public override readonly string ToString() => string.Format(Resources.GOR_TOSTR_GORGONRANGE, Minimum, Maximum, Range);
/// <summary>
/// Function to compare two instances for equality.
/// </summary>
/// <param name="left">The left instance to compare.</param>
/// <param name="right">The right instance to compare.</param>
/// <returns><b>true</b> if equal, <b>false</b> if not.</returns>
public static bool Equals(ref readonly GorgonRange<T> left, ref readonly GorgonRange<T> right) => (left.Minimum.Equals(right.Minimum)) && (left.Maximum.Equals(right.Maximum));
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool Equals(GorgonRange<T> other) => Equals(in this, in other);
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool Equals(ref readonly GorgonRange<T> other) => Equals(in this, in other);
/// <summary>
/// Compares the current object with another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
/// A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has the following meanings: Value Meaning Less than zero This object is less than the other parameter.Zero This object is equal to other. Greater than zero This object is greater than other.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly int CompareTo(GorgonRange<T> other) => Range.CompareTo(other.Range);
/// <summary>
/// Function to determine if two ranges would intersect each others <see cref="Minimum"/> and <see cref="Maximum"/>.
/// </summary>
/// <param name="other">The other range to compare.</param>
/// <returns><b>true</b> if there is an intersection, <b>false</b> if not.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool Intersects(GorgonRange<T> other) => (Minimum <= other.Maximum) && (Maximum >= other.Minimum);
/// <summary>
/// Function to clamp a value between the <see cref="Minimum"/> and <see cref="Maximum"/> values.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <returns>The clamped value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Clamp(T value)
{
if (value < Minimum)
{
return Minimum;
}
if (value > Maximum)
{
return Maximum;
}
return value;
}
/// <summary>
/// Implements the operator ==.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(in GorgonRange<T> left, in GorgonRange<T> right) => Equals(in left, in right);
/// <summary>
/// Implements the operator ==.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(in GorgonRange<T> left, in GorgonRange<T> right) => !Equals(in left, in right);
/// <summary>
/// Implements the operator >.
/// </summary>
/// <param name="left">The left instance to compare.</param>
/// <param name="right">The right instance to compare.</param>
/// <returns><b>true</b> if left is greater than right.</returns>
public static bool operator >(in GorgonRange<T> left, in GorgonRange<T> right) => left.Range > right.Range;
/// <summary>
/// Implements the operator >=.
/// </summary>
/// <param name="left">The left instance to compare.</param>
/// <param name="right">The right instance to compare.</param>
/// <returns><b>true</b> if left is greater than right.</returns>
public static bool operator >=(in GorgonRange<T> left, in GorgonRange<T> right) => left.Range >= right.Range;
/// <summary>
/// Implements the operator >.
/// </summary>
/// <param name="left">The left instance to compare.</param>
/// <param name="right">The right instance to compare.</param>
/// <returns><b>true</b> if left is greater than right.</returns>
public static bool operator <(in GorgonRange<T> left, in GorgonRange<T> right) => left.Range < right.Range;
/// <summary>
/// Implements the operator >=.
/// </summary>
/// <param name="left">The left instance to compare.</param>
/// <param name="right">The right instance to compare.</param>
/// <returns><b>true</b> if left is greater than right.</returns>
public static bool operator <=(in GorgonRange<T> left, in GorgonRange<T> right) => left.Range <= right.Range;
/// <summary>
/// Explicit operator to convert a <see cref="GorgonRange{T}"/> to a <see cref="Range"/> value.
/// </summary>
/// <param name="range">The Gorgon range value to convert.</param>
/// <returns>The range type.</returns>
public static explicit operator Range(in GorgonRange<T> range) => ToRange(in range);
/// <summary>
/// Explicit operator to convert a <see cref="Range"/> to a <see cref="GorgonRange{T}"/> value.
/// </summary>
/// <param name="range">The range value to convert.</param>
/// <returns>The Gorgon range type.</returns>
public static explicit operator GorgonRange<T>(Range range) => FromRange(range);
/// <summary>
/// Function to convert a <see cref="GorgonRange{T}"/> to a <see cref="Range"/> value.
/// </summary>
/// <param name="range">The Gorgon range value to convert.</param>
/// <returns>The range type.</returns>
public static Range ToRange(ref readonly GorgonRange<T> range) => new(int.CreateChecked(range.Minimum), int.CreateChecked(range.Maximum));
/// <summary>
/// Explicit operator to convert a <see cref="Range"/> to a <see cref="GorgonRange{T}"/> value.
/// </summary>
/// <param name="range">The range value to convert.</param>
/// <returns>The Gorgon range type.</returns>
public static GorgonRange<T> FromRange(Range range) => new(!range.Start.IsFromEnd ? T.CreateChecked(range.Start.Value) : T.CreateChecked(range.End.Value),
!range.End.IsFromEnd ? T.CreateChecked(range.End.Value) : T.CreateChecked(range.Start.Value));
/// <summary>
/// Initializes a new instance of the <see cref="GorgonRange{T}"/> struct.
/// </summary>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
[JsonConstructor]
public GorgonRange(T min, T max)
{
if (min < max)
{
Minimum = min;
Maximum = max;
}
else
{
Maximum = min;
Minimum = max;
}
}
}