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