forked from laicasaane/unity-supplements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListSegment.cs
286 lines (218 loc) · 8.77 KB
/
ListSegment.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
namespace System.Collections.Generic
{
public readonly partial struct ListSegment<T> : ISegment<T>, IEquatableReadOnlyStruct<ListSegment<T>>
{
private readonly ReadList<T> source;
private readonly bool hasSource;
private readonly int count;
private readonly int offset;
public bool HasSource => this.hasSource;
public int Offset => this.offset;
public int Count => this.count;
public T this[int index]
{
get
{
if ((uint)index >= (uint)this.count)
throw ThrowHelper.GetArgumentOutOfRange_IndexException();
return this.source[this.offset + index];
}
}
public ListSegment(in ReadList<T> source)
{
this.source = source;
this.hasSource = true;
this.offset = 0;
this.count = this.source.Count;
}
public ListSegment(in ReadList<T> source, int offset, int count)
{
this.source = source;
// Validate arguments, check is minimal instructions with reduced branching for inlinable fast-path
// Negative values discovered though conversion to high values when converted to unsigned
// Failure should be rare and location determination and message is delegated to failure functions
if ((uint)offset > (uint)this.source.Count || (uint)count > (uint)(this.source.Count - offset))
throw ThrowHelper.GetSegmentCtorValidationFailedException(this.source, offset, count);
this.hasSource = true;
this.offset = offset;
this.count = count;
}
public ListSegment<T> Slice(int index)
{
if (!this.hasSource)
return Empty;
if ((uint)index > (uint)this.count)
throw ThrowHelper.GetArgumentOutOfRange_IndexException();
return new ListSegment<T>(this.source, this.offset + index, this.count - index);
}
public ListSegment<T> Slice(int index, int count)
{
if (!this.hasSource)
return Empty;
if ((uint)index > (uint)this.count || (uint)count > (uint)(this.count - index))
throw ThrowHelper.GetArgumentOutOfRange_IndexException();
return new ListSegment<T>(this.source, this.offset + index, count);
}
public ListSegment<T> Skip(int count)
{
if (!this.hasSource)
return Empty;
if ((uint)count > (uint)this.count)
throw ThrowHelper.GetArgumentOutOfRange_CountException();
return new ListSegment<T>(this.source, this.offset + count, this.count - count);
}
public ListSegment<T> Take(int count)
{
if (!this.hasSource)
return Empty;
if ((uint)count > (uint)this.count)
throw ThrowHelper.GetArgumentOutOfRange_CountException();
return new ListSegment<T>(this.source, this.offset, count);
}
public ListSegment<T> TakeLast(int count)
=> Skip(this.count - count);
public ListSegment<T> SkipLast(int count)
=> Take(this.count - count);
ISegment<T> ISegment<T>.Slice(int index)
=> Slice(index);
ISegment<T> ISegment<T>.Slice(int index, int count)
=> Slice(index, count);
ISegment<T> ISegment<T>.Skip(int count)
=> Skip(count);
ISegment<T> ISegment<T>.Take(int count)
=> Take(count);
ISegment<T> ISegment<T>.TakeLast(int count)
=> TakeLast(count);
ISegment<T> ISegment<T>.SkipLast(int count)
=> SkipLast(count);
public T[] ToArray()
{
if (!this.hasSource || this.count == 0)
return new T[0];
var array = new T[this.count];
var count = this.count + this.offset;
for (int i = this.offset, j = 0; i < count; i++, j++)
{
array[j] = this.source[i];
}
return array;
}
public int IndexOf(T item)
{
var index = -1;
if (!this.hasSource)
return index;
var count = this.count + this.offset;
for (var i = this.offset; i < count; i++)
{
if (this.source[i].Equals(item))
{
index = i;
break;
}
}
return index >= 0 ? index - this.offset : -1;
}
public bool Contains(T item)
{
if (!this.hasSource)
return false;
var count = this.count + this.offset;
for (var i = this.offset; i < count; i++)
{
if (this.source[i].Equals(item))
return true;
}
return false;
}
public Enumerator GetEnumerator()
=> new Enumerator(this.hasSource ? this : Empty);
IEnumerator<T> IEnumerable<T>.GetEnumerator()
=> GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
public override bool Equals(object obj)
=> obj is ListSegment<T> other && Equals(in other);
public bool Equals(ListSegment<T> other)
=> this.hasSource == other.HasSource && this.source.Equals(in other.source) &&
other.Count == this.count && other.Offset == this.offset;
public bool Equals(in ListSegment<T> other)
=> this.hasSource == other.HasSource && this.source.Equals(in other.source) &&
other.Count == this.count && other.Offset == this.offset;
public override int GetHashCode()
{
#if USE_SYSTEM_HASHCODE
return HashCode.Combine(this.hasSource, this.source, this.offset, this.count);
#endif
#pragma warning disable CS0162 // Unreachable code detected
var hashCode = 1328453276;
hashCode = hashCode * -1521134295 + this.hasSource.GetHashCode();
hashCode = hashCode * -1521134295 + this.source.GetHashCode();
hashCode = hashCode * -1521134295 + this.offset.GetHashCode();
hashCode = hashCode * -1521134295 + this.count.GetHashCode();
return hashCode;
#pragma warning restore CS0162 // Unreachable code detected
}
public static ListSegment<T> Empty { get; } = new ListSegment<T>();
public static implicit operator ListSegment<T>(List<T> source)
=> new ListSegment<T>(source.AsReadList());
public static implicit operator ListSegment<T>(in ReadList<T> source)
=> new ListSegment<T>(source);
public static implicit operator Segment<T>(in ListSegment<T> segment)
=> new Segment<T>(segment.source, segment.Offset, segment.Count);
public static bool operator ==(in ListSegment<T> a, in ListSegment<T> b)
=> a.Equals(in b);
public static bool operator !=(in ListSegment<T> a, in ListSegment<T> b)
=> !a.Equals(in b);
public struct Enumerator : IEnumerator<T>
{
private readonly ReadList<T> source;
private readonly int start;
private readonly int end;
private int current;
internal Enumerator(in ListSegment<T> segment)
{
this.source = segment.source;
if (!segment.HasSource)
{
this.start = 0;
this.end = 0;
this.current = 0;
return;
}
this.start = segment.Offset;
this.end = segment.Offset + segment.Count;
this.current = this.start - 1;
}
public bool MoveNext()
{
if (this.current < this.end)
{
this.current++;
return (this.current < this.end);
}
return false;
}
public T Current
{
get
{
if (this.current < this.start)
throw ThrowHelper.GetInvalidOperationException_InvalidOperation_EnumNotStarted();
if (this.current >= this.end)
throw ThrowHelper.GetInvalidOperationException_InvalidOperation_EnumEnded();
return this.source[this.current];
}
}
object IEnumerator.Current
=> this.Current;
void IEnumerator.Reset()
{
this.current = this.start - 1;
}
public void Dispose()
{
}
}
}
}