-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNativeArraySegment.cs
297 lines (227 loc) · 8.96 KB
/
NativeArraySegment.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
using System;
using System.Collections;
using System.Collections.Generic;
namespace Unity.Collections
{
public readonly partial struct NativeArraySegment<T> : ISegment<T>, IEquatableReadOnlyStruct<NativeArraySegment<T>>
where T : struct
{
private readonly ReadNativeArray<T> source;
public bool HasSource { get; }
public int Offset { get; }
public int Count { get; }
public T this[int index]
{
get
{
if (index < 0 || index >= this.Count)
throw ThrowHelper.GetArgumentOutOfRange_IndexException();
return this.source[this.Offset + index];
}
}
public NativeArraySegment(in ReadNativeArray<T> source)
{
this.source = source;
this.HasSource = true;
this.Offset = 0;
this.Count = this.source.Length;
}
public NativeArraySegment(in ReadNativeArray<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.Length || (uint)count > (uint)(this.source.Length - offset))
throw ThrowHelper.GetSegmentCtorValidationFailedException(this.source, offset, count);
this.HasSource = true;
this.Offset = offset;
this.Count = count;
}
public NativeArraySegment<T> Slice(int index)
{
if (!this.HasSource)
return Empty;
if ((uint)index > (uint)this.Count)
throw ThrowHelper.GetArgumentOutOfRange_IndexException();
return new NativeArraySegment<T>(this.source, this.Offset + index, this.Count - index);
}
public NativeArraySegment<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 NativeArraySegment<T>(this.source, this.Offset + index, count);
}
public NativeArraySegment<T> Skip(int count)
{
if (!this.HasSource)
return Empty;
if ((uint)count > (uint)this.Count)
throw ThrowHelper.GetArgumentOutOfRange_CountException();
return new NativeArraySegment<T>(this.source, this.Offset + count, this.Count - count);
}
public NativeArraySegment<T> Take(int count)
{
if (!this.HasSource)
return Empty;
if ((uint)count > (uint)this.Count)
throw ThrowHelper.GetArgumentOutOfRange_CountException();
return new NativeArraySegment<T>(this.source, this.Offset, count);
}
public NativeArraySegment<T> TakeLast(int count)
=> Skip(this.Count - count);
public NativeArraySegment<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 int GetHashCode()
{
#if USE_SYSTEM_HASHCODE
return HashCode.Combine(this.Offset, this.Count, this.source);
#endif
#pragma warning disable CS0162 // Unreachable code detected
unchecked
{
var hash = (int)2166136261;
hash = (hash * 16777619) ^ this.Offset;
hash = (hash * 16777619) ^ this.Count;
hash ^= this.source.GetHashCode();
return hash;
}
#pragma warning restore CS0162 // Unreachable code detected
}
public override bool Equals(object obj)
=> obj is NativeArraySegment<T> other && Equals(in other);
public bool Equals(NativeArraySegment<T> other)
{
return this.source.Equals(in other.source) &&
other.Offset == this.Offset &&
other.Count == this.Count;
}
public bool Equals(in NativeArraySegment<T> other)
{
return this.source.Equals(in other.source) &&
other.Offset == this.Offset &&
other.Count == this.Count;
}
public static NativeArraySegment<T> Empty { get; } = new NativeArraySegment<T>(ReadNativeArray<T>.Empty);
public static implicit operator NativeArraySegment<T>(in NativeArray<T> source)
=> source.IsCreated ? new NativeArraySegment<T>(source) : Empty;
public static implicit operator NativeArraySegment<T>(in ReadNativeArray<T> source)
=> new NativeArraySegment<T>(source);
public static implicit operator Segment<T>(in NativeArraySegment<T> segment)
=> new Segment<T>(new NativeArraySource(segment.source.GetSource()), segment.Offset, segment.Count);
public static bool operator ==(in NativeArraySegment<T> a, in NativeArraySegment<T> b)
=> a.Equals(in b);
public static bool operator !=(in NativeArraySegment<T> a, in NativeArraySegment<T> b)
=> !a.Equals(in b);
public struct Enumerator : IEnumerator<T>
{
private readonly ReadNativeArray<T> source;
private readonly int start;
private readonly int end;
private int current;
internal Enumerator(in NativeArraySegment<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;
public void Reset()
{
this.current = this.start - 1;
}
public void Dispose()
{
}
}
}
}