-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayRef.cs
53 lines (44 loc) · 1.4 KB
/
ArrayRef.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
using System;
namespace MelonECS
{
public readonly struct ArrayRef<T>
{
public static ArrayRef<T> Empty => new ArrayRef<T>(null, 0, 0);
public ref T this[int i] => ref target[i];
public readonly int Length;
private readonly T[] target;
private readonly int startIndex;
public ArrayRef(T[] target, int start, int count)
{
this.target = target;
Length = count;
startIndex = start;
}
public Enumerator GetEnumerator() => new Enumerator(target, startIndex, Length);
public struct Enumerator
{
private readonly T[] target;
private int index;
private int count;
public Enumerator(T[] target, int index, int count)
{
this.target = target;
this.index = index;
this.count = count;
}
public readonly ref T Current
{
get
{
if (target is null || index < 0 || index > count)
{
throw new InvalidOperationException();
}
return ref target[index];
}
}
public bool MoveNext() => ++index < count;
public void Reset() => index = -1;
}
}
}