forked from laicasaane/unity-supplements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSegment.cs
238 lines (179 loc) · 7.59 KB
/
Segment.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
using System.Runtime.CompilerServices;
namespace System.Collections.Generic
{
public readonly partial struct Segment<T> : ISegment<T>, IEquatableReadOnlyStruct<Segment<T>>
{
private readonly ISegmentSource<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]
=> GetSource()[this.offset + index];
public Segment(in ReadArray1<T> source)
{
this.source = source == null ? _empty : new Array1Source(source);
this.hasSource = true;
this.offset = 0;
this.count = this.source.Count;
}
public Segment(in ReadArray1<T> source, int offset, int count)
{
this.source = source == null ? _empty : new Array1Source(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 Segment(ISegmentSource<T> source)
{
this.source = source ?? _empty;
this.hasSource = true;
this.offset = 0;
this.count = source.Count;
}
public Segment(ISegmentSource<T> source, int offset, int count)
{
if (source == null || (uint)offset > (uint)source.Count || (uint)count > (uint)(source.Count - offset))
throw ThrowHelper.GetSegmentCtorValidationFailedException(source, offset, count);
this.source = source;
this.hasSource = true;
this.offset = offset;
this.count = count;
}
public Segment<T> Slice(int index)
{
if (!this.hasSource)
return Empty;
if ((uint)index > (uint)this.count)
throw ThrowHelper.GetArgumentOutOfRange_IndexException();
return new Segment<T>(this.source, this.offset + index, this.count - index);
}
public Segment<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 Segment<T>(this.source, this.offset + index, count);
}
public Segment<T> Skip(int count)
{
if (!this.hasSource)
return Empty;
if ((uint)count > (uint)this.count)
throw ThrowHelper.GetArgumentOutOfRange_CountException();
return new Segment<T>(this.source, this.offset + count, this.count - count);
}
public Segment<T> Take(int count)
{
if (!this.hasSource)
return Empty;
if ((uint)count > (uint)this.count)
throw ThrowHelper.GetArgumentOutOfRange_CountException();
return new Segment<T>(this.source, this.offset, count);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ISegmentSource<T> GetSource()
=> this.hasSource ? this.source : _empty;
public Segment<T> TakeLast(int count)
=> Skip(this.count - count);
public Segment<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()
{
var source = GetSource();
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] = source[i];
}
return array;
}
public int IndexOf(T item)
{
var index = -1;
var source = GetSource();
if (source.Count <= 0)
return index;
var count = this.count + this.offset;
for (var i = this.offset; i < count; i++)
{
if (source[i].Equals(item))
{
index = i;
break;
}
}
return index >= 0 ? index - this.offset : -1;
}
public bool Contains(T item)
{
var source = GetSource();
var count = this.count + this.offset;
for (var i = this.offset; i < count; i++)
{
if (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 Segment<T> other && Equals(in other);
public bool Equals(Segment<T> other)
=> this.hasSource == other.HasSource && this.source.Equals(other) &&
this.count == other.Count && this.offset == other.Offset;
public bool Equals(in Segment<T> other)
=> this.hasSource == other.HasSource && this.source.Equals(other) &&
this.count == other.Count && this.offset == other.Offset;
public override int GetHashCode()
{
#if USE_SYSTEM_HASHCODE
return HashCode.Combine(this.hasSource, GetSource(), this.offset, this.count);
#endif
#pragma warning disable CS0162 // Unreachable code detected
var hashCode = 1328453276;
hashCode = hashCode * -1521134295 + this.hasSource.GetHashCode();
hashCode = hashCode * -1521134295 + GetSource().GetHashCode();
hashCode = hashCode * -1521134295 + this.offset.GetHashCode();
hashCode = hashCode * -1521134295 + this.count.GetHashCode();
return hashCode;
#pragma warning restore CS0162 // Unreachable code detected
}
private static Array1Source _empty { get; } = new Array1Source(new T[0]);
public static Segment<T> Empty { get; } = new Segment<T>(_empty);
public static implicit operator Segment<T>(T[] source)
=> new Segment<T>(source.AsReadArray());
public static implicit operator Segment<T>(in ReadArray1<T> source)
=> new Segment<T>(source);
public static bool operator ==(in Segment<T> a, in Segment<T> b)
=> a.Equals(in b);
public static bool operator !=(in Segment<T> a, in Segment<T> b)
=> !a.Equals(in b);
}
}