-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathReadNativeSlice{T}.cs
151 lines (115 loc) · 4.4 KB
/
ReadNativeSlice{T}.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Unity.Collections
{
public readonly struct ReadNativeSlice<T> : IReadOnlyList<T>, IEquatableReadOnlyStruct<ReadNativeSlice<T>>
where T : struct
{
private readonly NativeSlice<T> source;
private readonly bool hasSource;
public ReadNativeSlice(in NativeSlice<T> source)
{
this.source = source.Stride > 0 ? source : _empty;
this.Length = this.source.Length;
this.hasSource = true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal NativeSlice<T> GetSource()
=> (this.hasSource && this.source.Stride > 0) ? this.source : _empty;
public T this[int index]
{
get
{
if (index < 0 || index >= this.Length)
throw ThrowHelper.GetArgumentOutOfRange_IndexException();
return this.source[index];
}
}
public int Length { get; }
int IReadOnlyCollection<T>.Count
=> this.Length;
public override int GetHashCode()
=> GetSource().GetHashCode();
public override bool Equals(object obj)
=> obj is ReadNativeSlice<T> other && Equals(in other);
public bool Equals(ReadNativeSlice<T> other)
{
if (this.source == null && other.source == null)
return true;
if (this.source == null || other.source == null)
return false;
return ReferenceEquals(this.source, other.source);
}
public bool Equals(in ReadNativeSlice<T> other)
{
if (this.source == null && other.source == null)
return true;
if (this.source == null || other.source == null)
return false;
return ReferenceEquals(this.source, other.source);
}
public void CopyTo(T[] array)
=> GetSource().CopyTo(array);
public void CopyTo(NativeArray<T> array)
=> GetSource().CopyTo(array);
public T[] ToArray()
=> GetSource().ToArray();
public Enumerator GetEnumerator()
=> new Enumerator(this);
IEnumerator<T> IEnumerable<T>.GetEnumerator()
=> GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
private static NativeSlice<T> _empty { get; } = new NativeSlice<T>(ReadNativeArray<T>.Empty.GetSource());
public static ReadNativeSlice<T> Empty { get; } = new ReadNativeSlice<T>(_empty);
public static implicit operator ReadNativeSlice<T>(in NativeSlice<T> source)
=> source.Stride > 0 ? new ReadNativeSlice<T>(source) : Empty;
public static bool operator ==(in ReadNativeSlice<T> a, in ReadNativeSlice<T> b)
=> a.Equals(in b);
public static bool operator !=(in ReadNativeSlice<T> a, in ReadNativeSlice<T> b)
=> !a.Equals(in b);
public struct Enumerator : IEnumerator<T>
{
private readonly NativeSlice<T> source;
private readonly int end;
private int current;
internal Enumerator(in ReadNativeSlice<T> array)
{
this.source = array.GetSource();
this.end = this.source.Length;
this.current = -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 < 0)
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 = -1;
}
public void Dispose()
{
}
}
}
}