-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEnumUIntRange{T}.cs
314 lines (247 loc) · 10.3 KB
/
EnumUIntRange{T}.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
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace System
{
[Serializable]
public readonly struct EnumUIntRange<T> : IRange<T, EnumUIntRange<T>.Enumerator>, IEquatableReadOnlyStruct<EnumUIntRange<T>>, ISerializable
where T : unmanaged, Enum
{
public T Start { get; }
public T End { get; }
public bool IsFromEnd { get; }
public EnumUIntRange(T start, T end)
{
this.Start = start;
this.End = end;
this.IsFromEnd = false;
}
public EnumUIntRange(T start, T end, bool fromEnd)
{
this.Start = start;
this.End = end;
this.IsFromEnd = fromEnd;
}
private EnumUIntRange(SerializationInfo info, StreamingContext context)
{
if (!Enum<T>.TryParse(info.GetStringOrDefault(nameof(this.Start)), out var start))
start = default;
if (!Enum<T>.TryParse(info.GetStringOrDefault(nameof(this.End)), out var end))
end = default;
this.Start = start;
this.End = end;
this.IsFromEnd = info.GetBooleanOrDefault(nameof(this.IsFromEnd));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(nameof(this.Start), this.Start.ToString());
info.AddValue(nameof(this.End), this.End.ToString());
info.AddValue(nameof(this.IsFromEnd), this.IsFromEnd);
}
public void Deconstruct(out T start, out T end)
{
start = this.Start;
end = this.End;
}
public void Deconstruct(out T start, out T end, out bool fromEnd)
{
start = this.Start;
end = this.End;
fromEnd = this.IsFromEnd;
}
public EnumUIntRange<T> With(in T? Start = null, in T? End = null, bool? IsFromEnd = null)
=> new EnumUIntRange<T>(
Start ?? this.Start,
End ?? this.End,
IsFromEnd ?? this.IsFromEnd
);
public EnumUIntRange<T> FromStart()
=> new EnumUIntRange<T>(this.Start, this.End, false);
public EnumUIntRange<T> FromEnd()
=> new EnumUIntRange<T>(this.Start, this.End, true);
IRange<T> IRange<T>.FromStart()
=> FromStart();
IRange<T> IRange<T>.FromEnd()
=> FromEnd();
public uint Count()
{
var startVal = Enum<T>.ToUInt(this.Start);
var endVal = Enum<T>.ToUInt(this.End);
if (endVal > startVal)
return endVal - startVal + 1;
return startVal - endVal + 1;
}
public bool Contains(T value)
{
var startVal = Enum<T>.ToUInt(value);
var endVal = Enum<T>.ToUInt(value);
var val = Enum<T>.ToUInt(value);
return startVal < endVal
? val >= startVal && val <= endVal
: val >= endVal && val <= startVal;
}
public override bool Equals(object obj)
=> obj is EnumUIntRange<T> other &&
this.Start.Equals(other.Start) && this.End.Equals(other.End) &&
this.IsFromEnd == other.IsFromEnd;
public bool Equals(in EnumUIntRange<T> other)
=> this.Start.Equals(other.Start) && this.End.Equals(other.End) &&
this.IsFromEnd == other.IsFromEnd;
public bool Equals(EnumUIntRange<T> other)
=> this.Start.Equals(other.Start) && this.End.Equals(other.End) &&
this.IsFromEnd == other.IsFromEnd;
public override int GetHashCode()
{
#if USE_SYSTEM_HASHCODE
return HashCode.Combine(this.Start, this.End, this.IsFromEnd);
#endif
#pragma warning disable CS0162 // Unreachable code detected
var hashCode = -1418356749;
hashCode = hashCode * -1521134295 + this.Start.GetHashCode();
hashCode = hashCode * -1521134295 + this.End.GetHashCode();
hashCode = hashCode * -1521134295 + this.IsFromEnd.GetHashCode();
return hashCode;
#pragma warning restore CS0162 // Unreachable code detected
}
public override string ToString()
=> $"{{ {nameof(this.Start)}={this.Start}, {nameof(this.End)}={this.End}, {nameof(this.IsFromEnd)}={this.IsFromEnd} }}";
public Enumerator GetEnumerator()
=> new Enumerator(this);
public Enumerator Range()
=> GetEnumerator();
IEnumerator<T> IRange<T>.Range()
=> GetEnumerator();
public EnumUIntRange<T> Normalize()
=> Normal(this.Start, this.End);
/// <summary>
/// Create a normal range from (a, b) where <see cref="Start"/> is lesser than <see cref="End"/>.
/// </summary>
public static EnumUIntRange<T> Normal(T a, T b)
{
var aVal = Enum<T>.ToUInt(a);
var bVal = Enum<T>.ToUInt(b);
return aVal > bVal ? new EnumUIntRange<T>(b, a) : new EnumUIntRange<T>(a, b);
}
/// <summary>
/// Create a range from a size which is greater than 0
/// </summary>
/// <exception cref="InvalidOperationException">Size must be greater than 0</exception>
public static EnumUIntRange<T> FromSize(uint value, bool fromEnd = false)
{
var values = Enum<T>.Values;
var size = Math.Min(value, values.LongLength);
if (size <= 0)
throw new InvalidOperationException("Size must be greater than 0");
var start = default(T);
var end = default(T);
var last = size - 1;
for (var i = 0u; i < size; i++)
{
if (i == 0u)
start = values[i];
else if (i == last)
end = values[i];
}
return new EnumUIntRange<T>(start, end, fromEnd);
}
public static EnumUIntRange<T> FromStart(T start, T end)
=> new EnumUIntRange<T>(start, end, false);
public static EnumUIntRange<T> FromEnd(T start, T end)
=> new EnumUIntRange<T>(start, end, true);
public static implicit operator EnumUIntRange<T>(in (T start, T end) value)
=> new EnumUIntRange<T>(value.start, value.end);
public static implicit operator EnumUIntRange<T>(in (T start, T end, bool fromEnd) value)
=> new EnumUIntRange<T>(value.start, value.end, value.fromEnd);
public static implicit operator ReadRange<T, Enumerator.Default>(in EnumUIntRange<T> value)
=> new ReadRange<T, Enumerator.Default>(value.Start, value.End, value.IsFromEnd);
public static implicit operator ReadRange<T>(in EnumUIntRange<T> value)
=> new ReadRange<T>(value.Start, value.End, value.IsFromEnd, new Enumerator.Default());
public static implicit operator EnumUIntRange<T>(in ReadRange<T> value)
=> new EnumUIntRange<T>(value.Start, value.End, value.IsFromEnd);
public static implicit operator EnumUIntRange<T>(in ReadRange<T, Enumerator.Default> value)
=> new EnumUIntRange<T>(value.Start, value.End, value.IsFromEnd);
public static bool operator ==(in EnumUIntRange<T> lhs, in EnumUIntRange<T> rhs)
=> lhs.Start.Equals(rhs.Start) && lhs.End.Equals(rhs.End) &&
lhs.IsFromEnd == rhs.IsFromEnd;
public static bool operator !=(in EnumUIntRange<T> lhs, in EnumUIntRange<T> rhs)
=> !lhs.Start.Equals(rhs.Start) || !lhs.End.Equals(rhs.End) ||
lhs.IsFromEnd != rhs.IsFromEnd;
public struct Enumerator : IEnumerator<T>
{
private readonly uint start;
private readonly uint end;
private readonly sbyte sign;
private uint current;
private sbyte flag;
public Enumerator(in EnumUIntRange<T> range)
: this(range.Start, range.End, range.IsFromEnd)
{ }
public Enumerator(T start, T end, bool fromEnd)
{
var startVal = Enum<T>.ToUInt(start);
var endVal = Enum<T>.ToUInt(end);
var increasing = startVal <= endVal;
if (fromEnd)
{
this.start = endVal;
this.end = startVal;
}
else
{
this.start = startVal;
this.end = endVal;
}
this.sign = (sbyte)(increasing
? (fromEnd ? -1 : 1)
: (fromEnd ? 1 : -1));
this.current = this.start;
this.flag = -1;
}
public bool MoveNext()
{
if (this.flag == 0)
{
if (this.current == this.end)
{
this.flag = 1;
return false;
}
this.current += (uint)this.sign;
return true;
}
if (this.flag < 0)
{
this.flag = 0;
return true;
}
return false;
}
public T Current
{
get
{
if (this.flag < 0)
throw ThrowHelper.GetInvalidOperationException_InvalidOperation_EnumNotStarted();
if (this.flag > 0)
throw ThrowHelper.GetInvalidOperationException_InvalidOperation_EnumEnded();
return Enum<T>.From(this.current);
}
}
public void Dispose()
{
}
object IEnumerator.Current
=> this.Current;
void IEnumerator.Reset()
{
this.current = this.start;
this.flag = -1;
}
public readonly struct Default : IRangeEnumerator<T>
{
public IEnumerator<T> Enumerate(T start, T end, bool fromEnd)
=> new Enumerator(start, end, fromEnd);
}
}
}
}