-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathArray2D.cs
63 lines (53 loc) · 1.75 KB
/
Array2D.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
using System.Collections;
using System.Runtime.CompilerServices;
using static System.Runtime.CompilerServices.MethodImplOptions;
namespace Plato
{
public readonly struct Array2D<T> : IArray2D<T>
{
public Integer Count { get; }
public Integer NumColumns { get; }
public Integer NumRows { get; }
public readonly Func<Integer, Integer, T> Func;
[MethodImpl(AggressiveInlining)]
public Array2D(Integer numColumns, Integer numRows, Func<Integer, Integer, T> func)
{
Func = func;
NumColumns = numColumns;
NumRows = numRows;
Count = numColumns * numRows;
}
[MethodImpl(AggressiveInlining)]
public T At(Integer col, Integer row)
=> this[col, row];
[MethodImpl(AggressiveInlining)]
public T At(Integer index)
=> Func(index / NumColumns, index % NumColumns);
T IReadOnlyList<T>.this[int index]
{
[MethodImpl(AggressiveInlining)]
get => At(index);
}
T IArray<T>.this[Integer index]
{
[MethodImpl(AggressiveInlining)]
get => Func(index / NumColumns, index % NumColumns);
}
public T this[Integer col, Integer row]
{
[MethodImpl(AggressiveInlining)]
get => Func(col, row);
}
int IReadOnlyCollection<T>.Count
{
[MethodImpl(AggressiveInlining)]
get => Count;
}
[MethodImpl(AggressiveInlining)]
public IEnumerator<T> GetEnumerator()
=> new ArrayEnumerator<T>(this);
[MethodImpl(AggressiveInlining)]
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
}
}