-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUShortRange.cs
275 lines (217 loc) · 9.17 KB
/
UShortRange.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
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace System
{
[Serializable]
public readonly struct UShortRange : IRange<ushort, UShortRange.Enumerator>, IEquatableReadOnlyStruct<UShortRange>, ISerializable
{
public ushort Start { get; }
public ushort End { get; }
public bool IsFromEnd { get; }
public UShortRange(ushort start, ushort end)
{
this.Start = start;
this.End = end;
this.IsFromEnd = false;
}
public UShortRange(ushort start, ushort end, bool fromEnd)
{
this.Start = start;
this.End = end;
this.IsFromEnd = fromEnd;
}
private UShortRange(SerializationInfo info, StreamingContext context)
{
this.Start = info.GetUInt16OrDefault(nameof(this.Start));
this.End = info.GetUInt16OrDefault(nameof(this.End));
this.IsFromEnd = info.GetBooleanOrDefault(nameof(this.IsFromEnd));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(nameof(this.Start), this.Start);
info.AddValue(nameof(this.End), this.End);
info.AddValue(nameof(this.IsFromEnd), this.IsFromEnd);
}
public void Deconstruct(out ushort start, out ushort end)
{
start = this.Start;
end = this.End;
}
public void Deconstruct(out ushort start, out ushort end, out bool fromEnd)
{
start = this.Start;
end = this.End;
fromEnd = this.IsFromEnd;
}
public UShortRange With(in ushort? Start = null, in ushort? End = null, bool? IsFromEnd = null)
=> new UShortRange(
Start ?? this.Start,
End ?? this.End,
IsFromEnd ?? this.IsFromEnd
);
public UShortRange FromStart()
=> new UShortRange(this.Start, this.End, false);
public UShortRange FromEnd()
=> new UShortRange(this.Start, this.End, true);
IRange<ushort> IRange<ushort>.FromStart()
=> FromStart();
IRange<ushort> IRange<ushort>.FromEnd()
=> FromEnd();
public ushort Count()
{
if (this.End > this.Start)
return (ushort)(this.End - this.Start + 1);
return (ushort)(this.Start - this.End + 1);
}
public bool Contains(ushort value)
=> this.Start < this.End
? value >= this.Start && value <= this.End
: value >= this.End && value <= this.Start;
public override bool Equals(object obj)
=> obj is UShortRange other &&
this.Start == other.Start && this.End == other.End &&
this.IsFromEnd == other.IsFromEnd;
public bool Equals(in UShortRange other)
=> this.Start == other.Start && this.End == other.End &&
this.IsFromEnd == other.IsFromEnd;
public bool Equals(UShortRange other)
=> this.Start == other.Start && this.End == 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<ushort> IRange<ushort>.Range()
=> GetEnumerator();
public UShortRange 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 UShortRange Normal(ushort a, ushort b)
=> a > b ? new UShortRange(b, a) : new UShortRange(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 UShortRange FromSize(ushort value, bool fromEnd = false)
{
if (value == 0)
throw new InvalidOperationException("Size must be greater than 0");
return new UShortRange(0, (ushort)(value > 0 ? value - 1 : value), fromEnd);
}
public static UShortRange FromStart(ushort start, ushort end)
=> new UShortRange(start, end, false);
public static UShortRange FromEnd(ushort start, ushort end)
=> new UShortRange(start, end, true);
public static implicit operator UShortRange(in (ushort start, ushort end) value)
=> new UShortRange(value.start, value.end);
public static implicit operator UShortRange(in (ushort start, ushort end, bool fromEnd) value)
=> new UShortRange(value.start, value.end, value.fromEnd);
public static implicit operator ReadRange<ushort, Enumerator.Default>(in UShortRange value)
=> new ReadRange<ushort, Enumerator.Default>(value.Start, value.End, value.IsFromEnd);
public static implicit operator ReadRange<ushort>(in UShortRange value)
=> new ReadRange<ushort>(value.Start, value.End, value.IsFromEnd, new Enumerator.Default());
public static implicit operator UShortRange(in ReadRange<ushort> value)
=> new UShortRange(value.Start, value.End, value.IsFromEnd);
public static implicit operator UShortRange(in ReadRange<ushort, Enumerator.Default> value)
=> new UShortRange(value.Start, value.End, value.IsFromEnd);
public static bool operator ==(in UShortRange lhs, in UShortRange rhs)
=> lhs.Start == rhs.Start && lhs.End == rhs.End &&
lhs.IsFromEnd == rhs.IsFromEnd;
public static bool operator !=(in UShortRange lhs, in UShortRange rhs)
=> lhs.Start != rhs.Start || lhs.End != rhs.End ||
lhs.IsFromEnd != rhs.IsFromEnd;
public struct Enumerator : IEnumerator<ushort>
{
private readonly ushort start;
private readonly ushort end;
private readonly sbyte sign;
private ushort current;
private sbyte flag;
public Enumerator(in UShortRange range)
: this(range.Start, range.End, range.IsFromEnd)
{ }
public Enumerator(ushort start, ushort end, bool fromEnd)
{
var increasing = start <= end;
if (fromEnd)
{
this.start = end;
this.end = start;
}
else
{
this.start = start;
this.end = end;
}
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 += (ushort)this.sign;
return true;
}
if (this.flag < 0)
{
this.flag = 0;
return true;
}
return false;
}
public ushort Current
{
get
{
if (this.flag < 0)
throw ThrowHelper.GetInvalidOperationException_InvalidOperation_EnumNotStarted();
if (this.flag > 0)
throw ThrowHelper.GetInvalidOperationException_InvalidOperation_EnumEnded();
return 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<ushort>
{
public IEnumerator<ushort> Enumerate(ushort start, ushort end, bool fromEnd)
=> new Enumerator(start, end, fromEnd);
}
}
}
}