forked from laicasaane/unity-supplements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadArray1{T}.cs
250 lines (197 loc) · 6.95 KB
/
ReadArray1{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
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
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace System
{
public readonly struct ReadArray1<T> : IReadOnlyList<T>, IEquatableReadOnlyStruct<ReadArray1<T>>
{
private readonly T[] source;
public readonly int Length;
public readonly long LongLength;
int IReadOnlyCollection<T>.Count
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => this.Length;
}
public ref readonly T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((uint)index >= (uint)this.Length)
throw ThrowHelper.GetArgumentOutOfRange_IndexException();
return ref this.source[index];
}
}
public ref readonly T this[uint index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (index >= this.LongLength)
throw ThrowHelper.GetArgumentOutOfRange_IndexException();
return ref this.source[index];
}
}
public ref readonly T this[long index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (index >= this.LongLength)
throw ThrowHelper.GetArgumentOutOfRange_IndexException();
return ref this.source[index];
}
}
T IReadOnlyList<T>.this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((uint)index >= (uint)this.Length)
throw ThrowHelper.GetArgumentOutOfRange_IndexException();
return this.source[index];
}
}
public ReadArray1(T[] source)
{
if (source == null)
{
this = default;
return;
}
this.source = source;
this.Length = source.Length;
this.LongLength = source.LongLength;
}
public ReadArray1(T[] source, long longLength)
{
if (source == null)
{
this = default;
return;
}
var length = source.Length;
if (longLength < length)
length = (int)longLength;
this.source = source;
this.Length = length;
this.LongLength = Math.Min(source.LongLength, longLength);
}
public ReadArray1(T[] source, int length, long longLength)
{
if (source == null)
{
this = default;
return;
}
this.source = source;
this.Length = Math.Min(source.Length, length);
this.LongLength = Math.Min(source.LongLength, longLength);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal T[] GetSource()
=> this.source ?? _empty;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
=> GetSource().GetHashCode();
public override bool Equals(object obj)
=> obj is ReadArray1<T> other && Equals(in other);
public bool Equals(ReadArray1<T> other)
{
var source = GetSource();
var otherSource = other.GetSource();
return source == otherSource;
}
public bool Equals(in ReadArray1<T> other)
{
var source = GetSource();
var otherSource = other.GetSource();
return source == otherSource;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyTo(Array array, uint index)
=> GetSource().CopyTo(array, index);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyTo(Array array, int index)
=> GetSource().CopyTo(array, index);
public T[] ToArray()
{
var source = GetSource();
var length = Math.Min(source.LongLength, this.LongLength);
if (length == 0)
return Array.Empty<T>();
var array = new T[length];
Array.Copy(source, 0, array, 0, length);
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Enumerator GetEnumerator()
=> new Enumerator(this);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
IEnumerator<T> IEnumerable<T>.GetEnumerator()
=> GetEnumerator();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
private static readonly T[] _empty = Array.Empty<T>();
public static ReadArray1<T> Empty { get; } = new ReadArray1<T>(_empty);
public static implicit operator ReadArray1<T>(T[] source)
=> source == null ? Empty : new ReadArray1<T>(source);
public static implicit operator ReadCollection<T>(in ReadArray1<T> source)
=> new ReadCollection<T>(source.GetSource());
public static bool operator ==(in ReadArray1<T> a, in ReadArray1<T> b)
=> a.Equals(in b);
public static bool operator !=(in ReadArray1<T> a, in ReadArray1<T> b)
=> !a.Equals(in b);
public struct Enumerator : IEnumerator<T>
{
private readonly T[] source;
private readonly long length;
private long current;
private bool first;
internal Enumerator(in ReadArray1<T> array)
{
this.source = array.GetSource();
this.length = Math.Min(this.source.LongLength, array.LongLength);
this.current = 0;
this.first = true;
}
public bool MoveNext()
{
if (this.length == 0)
return false;
if (this.first)
{
this.first = false;
return true;
}
if (this.current < this.length - 1)
{
this.current++;
return true;
}
return false;
}
public T Current
{
get
{
if (this.current >= this.length)
throw ThrowHelper.GetInvalidOperationException_InvalidOperation_EnumEnded();
return this.source[this.current];
}
}
object IEnumerator.Current
=> this.Current;
public void Reset()
{
this.current = 0;
this.first = true;
}
public void Dispose()
{
}
}
}
}