forked from laicasaane/unity-supplements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArray1Extensions.cs
335 lines (251 loc) · 8.98 KB
/
Array1Extensions.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using System;
using System.Collections.Generic;
public static class Array1Extensions
{
public static bool ValidateIndex<T>(this T[] self, int index)
=> self != null && index >= 0 && index < self.Length;
public static bool ValidateIndex<T>(this T[] self, uint index)
=> self != null && index < self.LongLength;
public static bool ValidateIndex<T>(this T[] self, long index)
=> self != null && index >= 0 && index < self.LongLength;
public static ReadArray1<T> AsReadArray<T>(this T[] self)
=> self;
public static T Get<T>(this object[] self, int index)
{
if (self == null)
return default;
return self.Length > index && self[index] is T val ? val : default;
}
public static object[] Get<T>(this object[] self, int index, out T value)
{
value = default;
if (self != null && self.Length > index && self[index] is T val)
value = val;
return self;
}
public static object[] GetThenMoveNext<T>(this object[] self, ref int index, out T value)
{
value = default;
if (self != null)
{
if (self.Length > index && self[index] is T val)
value = val;
index += 1;
}
return self;
}
public static T[] Get<T, TResult>(this T[] self, int index, out TResult value)
{
value = default;
if (self != null && self.Length > index && self[index] is TResult val)
value = val;
return self;
}
public static T[] GetThenMoveNext<T, TResult>(this T[] self, ref int index, out TResult value)
{
value = default;
if (self != null)
{
if (self.Length > index && self[index] is TResult val)
value = val;
index += 1;
}
return self;
}
public static void Clear<T>(this T[] self)
{
if (self == null)
return;
for (var i = 0; i < self.Length; i++)
{
self[i] = default;
}
}
public static void Set<T>(this T[] self, params T[] source)
=> self.Set(0, source);
public static void Set<T>(this T[] self, int startIndex, params T[] source)
{
if (self == null || source == null)
return;
var length = Math.Min(self.Length, source.Length);
for (var i = startIndex; i < length; i++)
{
self[i] = source[i];
}
}
public static void Set<T>(this T[] self, IList<T> source)
=> self.Set(0, source);
public static void Set<T>(this T[] self, int startIndex, IList<T> source)
=> self.Set(startIndex, source.AsSegment());
public static void Set<T>(this T[] self, IReadOnlyList<T> source)
=> self.Set(0, source);
public static void Set<T>(this T[] self, int startIndex, IReadOnlyList<T> source)
{
if (self == null || source == null)
return;
var length = Math.Min(self.Length, source.Count);
for (var i = startIndex; i < length; i++)
{
self[i] = source[i];
}
}
public static void Set<T>(this T[] self, in Segment<T> source)
=> self.Set(0, in source);
public static void Set<T>(this T[] self, int startIndex, in Segment<T> source)
{
if (self == null)
return;
var length = Math.Min(self.Length, source.Count);
for (var i = startIndex; i < length; i++)
{
self[i] = source[i];
}
}
public static void Set<T>(this T[] self, IEnumerable<T> source)
=> self.Set(0, source);
public static void Set<T>(this T[] self, int startIndex, IEnumerable<T> source)
{
if (self == null || source == null)
return;
var index = startIndex;
foreach (var value in source)
{
if (!self.ValidateIndex(index))
break;
self[index++] = value;
}
}
public static void GetRange<T>(this T[] self, in IntRange range, ICollection<T> output, bool allowDuplicate = true, bool allowNull = false)
{
if ((uint)range.Start >= (uint)self.Length)
throw new IndexOutOfRangeException(nameof(range.Start));
if ((uint)range.End >= (uint)self.Length)
throw new IndexOutOfRangeException(nameof(range.End));
if (allowDuplicate)
{
foreach (var i in range)
{
ref var item = ref self[i];
if (allowNull || item != null)
output.Add(item);
}
return;
}
foreach (var i in range)
{
ref var item = ref self[i];
if ((allowNull || item != null) && !output.Contains(item))
output.Add(item);
}
}
public static void GetRange<T>(this T[] self, int offset, ICollection<T> output, bool allowDuplicate = true, bool allowNull = false)
=> self.GetRange(offset, -1, output, allowDuplicate, allowNull);
public static void GetRange<T>(this T[] self, int offset, int count, ICollection<T> output, bool allowDuplicate = true, bool allowNull = false)
{
if (self == null || output == null || count == 0)
return;
offset = Math.Max(offset, 0);
if (offset > self.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
if (count < 0)
count = self.Length - offset;
else
count += offset;
if (count > self.Length)
throw new ArgumentOutOfRangeException(nameof(count));
if (allowDuplicate)
{
for (var i = offset; i < count; i++)
{
ref var item = ref self[i];
if (allowNull || item != null)
output.Add(item);
}
return;
}
for (var i = offset; i < count; i++)
{
ref var item = ref self[i];
if ((allowNull || item != null) && !output.Contains(item))
output.Add(item);
}
}
public static void GetRange<T>(this T[] self, in LongRange range, ICollection<T> output, bool allowDuplicate = true, bool allowNull = false)
{
if ((ulong)range.Start >= (ulong)self.LongLength)
throw new IndexOutOfRangeException(nameof(range.Start));
if ((ulong)range.End >= (ulong)self.LongLength)
throw new IndexOutOfRangeException(nameof(range.End));
if (allowDuplicate)
{
foreach (var i in range)
{
ref var item = ref self[i];
if (allowNull || item != null)
output.Add(item);
}
return;
}
foreach (var i in range)
{
ref var item = ref self[i];
if ((allowNull || item != null) && !output.Contains(item))
output.Add(item);
}
}
public static void GetRange<T>(this T[] self, long offset, ICollection<T> output, bool allowDuplicate = true, bool allowNull = false)
=> self.GetRange(offset, -1, output, allowDuplicate, allowNull);
public static void GetRange<T>(this T[] self, long offset, long count, ICollection<T> output, bool allowDuplicate = true, bool allowNull = false)
{
if (self == null || output == null || count == 0)
return;
offset = Math.Max(offset, 0);
if (offset > self.LongLength)
throw new ArgumentOutOfRangeException(nameof(offset));
if (count < 0)
count = self.LongLength - offset;
else
count += offset;
if (count > self.LongLength)
throw new ArgumentOutOfRangeException(nameof(count));
if (allowDuplicate)
{
for (var i = offset; i < count; i++)
{
ref var item = ref self[i];
if (allowNull || item != null)
output.Add(item);
}
return;
}
for (var i = offset; i < count; i++)
{
ref var item = ref self[i];
if ((allowNull || item != null) && !output.Contains(item))
output.Add(item);
}
}
public static void GetRange<T>(this T[] self, in UIntRange range, ICollection<T> output, bool allowDuplicate = true, bool allowNull = false)
{
if (range.Start >= self.LongLength)
throw new IndexOutOfRangeException(nameof(range.Start));
if (range.End >= self.LongLength)
throw new IndexOutOfRangeException(nameof(range.End));
if (allowDuplicate)
{
foreach (var i in range)
{
ref var item = ref self[i];
if (allowNull || item != null)
output.Add(item);
}
return;
}
foreach (var i in range)
{
ref var item = ref self[i];
if ((allowNull || item != null) && !output.Contains(item))
output.Add(item);
}
}
}