-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySpan.cs
More file actions
49 lines (44 loc) · 1.29 KB
/
MySpan.cs
File metadata and controls
49 lines (44 loc) · 1.29 KB
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
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
readonly ref struct MySpan<T>
{
private readonly ref T _reference ;
private readonly int _length;
public MySpan(ref T reference)
{
_reference = ref reference;
_length = 1;
}
public int Length => _length;
public MySpan(ref T reference, int length)
{
_reference = ref reference;
_length = length;
}
public MySpan(T[] array)
{
ArgumentNullException.ThrowIfNull(array);
_reference = ref array[0];
// _reference = ref MemoryMarshal.GetArrayDataReference(array);
_length = array.Length;
}
public ref T this[int index]
{
get
{
ArgumentOutOfRangeException.ThrowIfNegative(index);
if((uint) index >= (uint) _length)
{
throw new IndexOutOfRangeException();
}
return ref Unsafe.Add(ref _reference, index);
}
}
public MySpan<T> Slice(int offset)
{
ArgumentOutOfRangeException.ThrowIfNegative(offset);
ArgumentOutOfRangeException.ThrowIfGreaterThan(offset, _length);
int newLength = _length - offset;
return new MySpan<T>(ref Unsafe.Add(ref _reference, offset), newLength);
}
}