-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathULongRange.cs
275 lines (217 loc) · 9.05 KB
/
ULongRange.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 ULongRange : IRange<ulong, ULongRange.Enumerator>, IEquatableReadOnlyStruct<ULongRange>, ISerializable
{
public ulong Start { get; }
public ulong End { get; }
public bool IsFromEnd { get; }
public ULongRange(ulong start, ulong end)
{
this.Start = start;
this.End = end;
this.IsFromEnd = false;
}
public ULongRange(ulong start, ulong end, bool fromEnd)
{
this.Start = start;
this.End = end;
this.IsFromEnd = fromEnd;
}
private ULongRange(SerializationInfo info, StreamingContext context)
{
this.Start = info.GetUInt64OrDefault(nameof(this.Start));
this.End = info.GetUInt64OrDefault(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 ulong start, out ulong end)
{
start = this.Start;
end = this.End;
}
public void Deconstruct(out ulong start, out ulong end, out bool fromEnd)
{
start = this.Start;
end = this.End;
fromEnd = this.IsFromEnd;
}
public ULongRange With(in ulong? Start = null, in ulong? End = null, bool? IsFromEnd = null)
=> new ULongRange(
Start ?? this.Start,
End ?? this.End,
IsFromEnd ?? this.IsFromEnd
);
public ULongRange FromStart()
=> new ULongRange(this.Start, this.End, false);
public ULongRange FromEnd()
=> new ULongRange(this.Start, this.End, true);
IRange<ulong> IRange<ulong>.FromStart()
=> FromStart();
IRange<ulong> IRange<ulong>.FromEnd()
=> FromEnd();
public ulong Count()
{
if (this.End > this.Start)
return this.End - this.Start + 1;
return this.Start - this.End + 1;
}
public bool Contains(ulong 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 ULongRange other &&
this.Start == other.Start && this.End == other.End &&
this.IsFromEnd == other.IsFromEnd;
public bool Equals(in ULongRange other)
=> this.Start == other.Start && this.End == other.End &&
this.IsFromEnd == other.IsFromEnd;
public bool Equals(ULongRange 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<ulong> IRange<ulong>.Range()
=> GetEnumerator();
public ULongRange 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 ULongRange Normal(ulong a, ulong b)
=> a > b ? new ULongRange(b, a) : new ULongRange(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 ULongRange FromSize(ulong value, bool fromEnd = false)
{
if (value == 0)
throw new InvalidOperationException("Size must be greater than 0");
return new ULongRange(0, value > 0 ? value - 1 : value, fromEnd);
}
public static ULongRange FromStart(ulong start, ulong end)
=> new ULongRange(start, end, false);
public static ULongRange FromEnd(ulong start, ulong end)
=> new ULongRange(start, end, true);
public static implicit operator ULongRange(in (ulong start, ulong end) value)
=> new ULongRange(value.start, value.end);
public static implicit operator ULongRange(in (ulong start, ulong end, bool fromEnd) value)
=> new ULongRange(value.start, value.end, value.fromEnd);
public static implicit operator ReadRange<ulong, Enumerator.Default>(in ULongRange value)
=> new ReadRange<ulong, Enumerator.Default>(value.Start, value.End, value.IsFromEnd);
public static implicit operator ReadRange<ulong>(in ULongRange value)
=> new ReadRange<ulong>(value.Start, value.End, value.IsFromEnd, new Enumerator.Default());
public static implicit operator ULongRange(in ReadRange<ulong> value)
=> new ULongRange(value.Start, value.End, value.IsFromEnd);
public static implicit operator ULongRange(in ReadRange<ulong, Enumerator.Default> value)
=> new ULongRange(value.Start, value.End, value.IsFromEnd);
public static bool operator ==(in ULongRange lhs, in ULongRange rhs)
=> lhs.Start == rhs.Start && lhs.End == rhs.End &&
lhs.IsFromEnd == rhs.IsFromEnd;
public static bool operator !=(in ULongRange lhs, in ULongRange rhs)
=> lhs.Start != rhs.Start || lhs.End != rhs.End ||
lhs.IsFromEnd != rhs.IsFromEnd;
public struct Enumerator : IEnumerator<ulong>
{
private readonly ulong start;
private readonly ulong end;
private readonly sbyte sign;
private ulong current;
private sbyte flag;
public Enumerator(in ULongRange range)
: this(range.Start, range.End, range.IsFromEnd)
{ }
public Enumerator(ulong start, ulong 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 += (ulong)this.sign;
return true;
}
if (this.flag < 0)
{
this.flag = 0;
return true;
}
return false;
}
public ulong 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<ulong>
{
public IEnumerator<ulong> Enumerate(ulong start, ulong end, bool fromEnd)
=> new Enumerator(start, end, fromEnd);
}
}
}
}