forked from TASEmulators/BizHawk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ranges.cs
251 lines (199 loc) · 12.1 KB
/
Ranges.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
using System;
using System.Collections.Generic;
using System.Linq;
using BizHawk.Common.NumberExtensions;
namespace BizHawk.Common
{
/// <summary>represents a closed range of <typeparamref name="T"/> (class invariant: <see cref="Start"/> ≤ <see cref="EndInclusive"/>)</summary>
public interface Range<out T> where T : unmanaged, IComparable<T>
{
T Start { get; }
T EndInclusive { get; }
}
/// <summary>represents a closed range of <typeparamref name="T"/> which can be grown or shrunk (class invariant: <see cref="Start"/> ≤ <see cref="EndInclusive"/>)</summary>
public class MutableRange<T> : Range<T> where T : unmanaged, IComparable<T>
{
private (T Start, T EndInclusive) r;
/// <inheritdoc cref="Overwrite"/>
internal MutableRange(T start, T endInclusive) => Overwrite(start, endInclusive);
/// <exception cref="ArgumentOutOfRangeException">(from setter) <paramref name="value"/> > <see cref="EndInclusive"/></exception>
public T Start
{
get => r.Start;
set => r.Start = r.EndInclusive.CompareTo(value) < 0
? throw new ArgumentOutOfRangeException(nameof(value), value, "attempted to set start > end")
: value;
}
/// <exception cref="ArgumentOutOfRangeException">(from setter) <paramref name="value"/> < <see cref="Start"/></exception>
public T EndInclusive
{
get => r.EndInclusive;
set => r.EndInclusive = value.CompareTo(r.Start) < 0
? throw new ArgumentOutOfRangeException(nameof(value), value, "attempted to set end < start")
: value;
}
/// <exception cref="ArgumentException"><typeparamref name="T"/> is <see langword="float"/>/<see langword="double"/> and either bound is <see cref="float.NaN"/></exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="endInclusive"/> < <paramref name="start"/></exception>
public void Overwrite(T start, T endInclusive)
{
if (endInclusive.CompareTo(start) < 0) throw new ArgumentOutOfRangeException(nameof(endInclusive), endInclusive, "range end < start");
if (start is float fs)
{
if (float.IsNaN(fs)) throw new ArgumentException("range start is NaN", nameof(start));
if (endInclusive is float fe && float.IsNaN(fe)) throw new ArgumentException("range end is NaN", nameof(endInclusive));
}
else if (start is double ds)
{
if (double.IsNaN(ds)) throw new ArgumentException("range start is NaN", nameof(start));
if (endInclusive is double de && double.IsNaN(de)) throw new ArgumentException("range end is NaN", nameof(endInclusive));
}
r = (start, endInclusive);
}
}
/// <summary>contains most of the logic for ranges</summary>
/// <remarks>
/// non-generic overloads are used where the method requires an increment or decrement<br/>
/// TODO which Enumerate algorithm is faster - <c>yield return</c> in loop or <see cref="Enumerable.Range"/>?
/// </remarks>
public static class RangeExtensions
{
private const ulong MIN_LONG_NEGATION_AS_ULONG = 9223372036854775808UL;
private static readonly ArithmeticException ExclusiveRangeMinValExc = new ArithmeticException("exclusive range end is min value of integral type");
/// <returns><paramref name="value"/> if it's contained in <paramref name="range"/>, or else whichever bound of <paramref name="range"/> is closest to <paramref name="value"/></returns>
public static T ConstrainWithin<T>(this T value, Range<T> range) where T : unmanaged, IComparable<T> => value.CompareTo(range.Start) < 0
? range.Start
: range.EndInclusive.CompareTo(value) < 0
? range.EndInclusive
: value;
/// <returns>true iff <paramref name="value"/> is contained in <paramref name="range"/> (<paramref name="value"/> is considered to be in the range if it's exactly equal to either bound)</returns>
public static bool Contains<T>(this Range<T> range, T value) where T : unmanaged, IComparable<T> => !(value.CompareTo(range.Start) < 0 || range.EndInclusive.CompareTo(value) < 0);
public static uint Count(this Range<byte> range) => (uint) (range.EndInclusive - range.Start + 1);
/// <remarks>beware integer overflow when <paramref name="range"/> contains every value</remarks>
public static uint Count(this Range<int> range) => (uint) ((long) range.EndInclusive - range.Start) + 1U;
/// <inheritdoc cref="Count(Range{int})"/>
public static ulong Count(this Range<long> range) => (range.Contains(0L)
? (range.Start == long.MinValue ? MIN_LONG_NEGATION_AS_ULONG : (ulong) -range.Start) + (ulong) range.EndInclusive
: (ulong) (range.EndInclusive - range.Start)
) + 1UL;
public static uint Count(this Range<sbyte> range) => (uint) (range.EndInclusive - range.Start + 1);
public static uint Count(this Range<short> range) => (uint) (range.EndInclusive - range.Start + 1);
/// <inheritdoc cref="Count(Range{int})"/>
public static uint Count(this Range<uint> range) => range.EndInclusive - range.Start + 1U;
/// <inheritdoc cref="Count(Range{int})"/>
public static ulong Count(this Range<ulong> range) => range.EndInclusive - range.Start + 1UL;
public static uint Count(this Range<ushort> range) => (uint) (range.EndInclusive - range.Start + 1);
public static void Deconstruct<T>(this Range<T> range, out T start, out T endInclusive)
where T : unmanaged, IComparable<T>
{
start = range.Start;
endInclusive = range.EndInclusive;
}
public static IEnumerable<byte> Enumerate(this Range<byte> range) => Enumerable.Range(range.Start, (int) range.Count()).Select(i => (byte) i);
/// <inheritdoc cref="Enumerate(Range{float},float)"/>
public static IEnumerable<double> Enumerate(this Range<double> range, double step)
{
var d = range.Start;
while (d < range.EndInclusive)
{
yield return d;
d += step;
}
if (d.HawkFloatEquality(range.EndInclusive)) yield return d;
}
/// <remarks>beware precision errors</remarks>
public static IEnumerable<float> Enumerate(this Range<float> range, float step)
{
var f = range.Start;
while (f < range.EndInclusive)
{
yield return f;
f += step;
}
if (f.HawkFloatEquality(range.EndInclusive)) yield return f;
}
public static IEnumerable<int> Enumerate(this Range<int> range)
{
var i = range.Start;
while (i < range.EndInclusive) yield return i++;
yield return i;
}
public static IEnumerable<long> Enumerate(this Range<long> range)
{
var l = range.Start;
while (l < range.EndInclusive) yield return l++;
yield return l;
}
public static IEnumerable<sbyte> Enumerate(this Range<sbyte> range) => Enumerable.Range(range.Start, (int) range.Count()).Select(i => (sbyte) i);
public static IEnumerable<short> Enumerate(this Range<short> range) => Enumerable.Range(range.Start, (int) range.Count()).Select(i => (short) i);
public static IEnumerable<uint> Enumerate(this Range<uint> range)
{
var i = range.Start;
while (i < range.EndInclusive) yield return i++;
yield return i;
}
public static IEnumerable<ulong> Enumerate(this Range<ulong> range)
{
var l = range.Start;
while (l < range.EndInclusive) yield return l++;
yield return l;
}
public static IEnumerable<ushort> Enumerate(this Range<ushort> range) => Enumerable.Range(range.Start, (int) range.Count()).Select(i => (ushort) i);
public static Range<T> GetImmutableCopy<T>(this Range<T> range) where T : unmanaged, IComparable<T> => GetMutableCopy(range);
public static MutableRange<T> GetMutableCopy<T>(this Range<T> range) where T : unmanaged, IComparable<T> => new MutableRange<T>(range.Start, range.EndInclusive);
/// <inheritdoc cref="MutableRange{T}(T,T)"/>
public static MutableRange<T> MutableRangeTo<T>(this T start, T endInclusive) where T : unmanaged, IComparable<T> => new MutableRange<T>(start, endInclusive);
/// <inheritdoc cref="RangeToExclusive(int,int)"/>
public static MutableRange<byte> MutableRangeToExclusive(this byte start, byte endExclusive) => endExclusive == byte.MinValue
? throw ExclusiveRangeMinValExc
: new MutableRange<byte>(start, (byte) (endExclusive - 1U));
/// <inheritdoc cref="RangeToExclusive(int,int)"/>
public static MutableRange<int> MutableRangeToExclusive(this int start, int endExclusive) => endExclusive == int.MinValue
? throw ExclusiveRangeMinValExc
: new MutableRange<int>(start, endExclusive - 1);
/// <inheritdoc cref="RangeToExclusive(int,int)"/>
public static MutableRange<long> MutableRangeToExclusive(this long start, long endExclusive) => endExclusive == long.MinValue
? throw ExclusiveRangeMinValExc
: new MutableRange<long>(start, endExclusive - 1L);
/// <inheritdoc cref="RangeToExclusive(int,int)"/>
public static MutableRange<sbyte> MutableRangeToExclusive(this sbyte start, sbyte endExclusive) => endExclusive == sbyte.MinValue
? throw ExclusiveRangeMinValExc
: new MutableRange<sbyte>(start, (sbyte) (endExclusive - 1));
/// <inheritdoc cref="RangeToExclusive(int,int)"/>
public static MutableRange<short> MutableRangeToExclusive(this short start, short endExclusive) => endExclusive == short.MinValue
? throw ExclusiveRangeMinValExc
: new MutableRange<short>(start, (short) (endExclusive - 1));
/// <inheritdoc cref="RangeToExclusive(int,int)"/>
public static MutableRange<uint> MutableRangeToExclusive(this uint start, uint endExclusive) => endExclusive == uint.MinValue
? throw ExclusiveRangeMinValExc
: new MutableRange<uint>(start, endExclusive - 1U);
/// <inheritdoc cref="RangeToExclusive(int,int)"/>
public static MutableRange<ulong> MutableRangeToExclusive(this ulong start, ulong endExclusive) => endExclusive == ulong.MinValue
? throw ExclusiveRangeMinValExc
: new MutableRange<ulong>(start, endExclusive - 1UL);
/// <inheritdoc cref="RangeToExclusive(int,int)"/>
public static MutableRange<ushort> MutableRangeToExclusive(this ushort start, ushort endExclusive) => endExclusive == ushort.MinValue
? throw ExclusiveRangeMinValExc
: new MutableRange<ushort>(start, (ushort) (endExclusive - 1U));
/// <inheritdoc cref="MutableRange{T}(T,T)"/>
public static Range<T> RangeTo<T>(this T start, T endInclusive) where T : unmanaged, IComparable<T> => start.MutableRangeTo(endInclusive);
/// <inheritdoc cref="RangeToExclusive(int,int)"/>
public static Range<byte> RangeToExclusive(this byte start, byte endExclusive) => MutableRangeToExclusive(start, endExclusive);
/// <exception cref="ArgumentOutOfRangeException"><paramref name="endExclusive"/> ≤ <paramref name="start"/> (empty ranges where <paramref name="start"/> = <paramref name="endExclusive"/> are not permitted)</exception>
/// <exception cref="ArithmeticException"><paramref name="endExclusive"/> is min value of integral type (therefore <paramref name="endExclusive"/> ≤ <paramref name="start"/>)</exception>
public static Range<int> RangeToExclusive(this int start, int endExclusive) => MutableRangeToExclusive(start, endExclusive);
/// <inheritdoc cref="RangeToExclusive(int,int)"/>
public static Range<long> RangeToExclusive(this long start, long endExclusive) => MutableRangeToExclusive(start, endExclusive);
/// <inheritdoc cref="RangeToExclusive(int,int)"/>
public static Range<sbyte> RangeToExclusive(this sbyte start, sbyte endExclusive) => MutableRangeToExclusive(start, endExclusive);
/// <inheritdoc cref="RangeToExclusive(int,int)"/>
public static Range<short> RangeToExclusive(this short start, short endExclusive) => MutableRangeToExclusive(start, endExclusive);
/// <inheritdoc cref="RangeToExclusive(int,int)"/>
public static Range<uint> RangeToExclusive(this uint start, uint endExclusive) => MutableRangeToExclusive(start, endExclusive);
/// <inheritdoc cref="RangeToExclusive(int,int)"/>
public static Range<ulong> RangeToExclusive(this ulong start, ulong endExclusive) => MutableRangeToExclusive(start, endExclusive);
/// <inheritdoc cref="RangeToExclusive(int,int)"/>
public static Range<ushort> RangeToExclusive(this ushort start, ushort endExclusive) => MutableRangeToExclusive(start, endExclusive);
/// <returns>true iff <paramref name="value"/> is strictly contained in <paramref name="range"/> (<paramref name="value"/> is considered to be OUTSIDE the range if it's exactly equal to either bound)</returns>
public static bool StrictlyBoundedBy<T>(this T value, Range<T> range) where T : unmanaged, IComparable<T> => range.Start.CompareTo(value) < 0 && value.CompareTo(range.EndInclusive) < 0;
}
}