-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathArrayGrid{T}.GridIndexedValues.cs
73 lines (59 loc) · 2.46 KB
/
ArrayGrid{T}.GridIndexedValues.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
using System.Collections;
using System.Collections.ArrayBased;
using System.Collections.Generic;
namespace System.Grid.ArrayBased
{
public partial class ArrayGrid<T>
{
public readonly struct GridIndexedValues : IGridIndexedValues<T>
{
private readonly ArrayGrid<T> source;
private readonly IEnumerator<GridIndex> enumerator;
public GridIndexedValues(ArrayGrid<T> grid, IEnumerator<GridIndex> enumerator)
{
this.source = grid;
this.enumerator = enumerator;
}
public Enumerator GetEnumerator()
=> new Enumerator(this.source, this.enumerator);
IGridIndexedValueEnumerator<T> IGridIndexedValues<T>.GetEnumerator()
=> GetEnumerator();
IEnumerator<GridValue<T>> IEnumerable<GridValue<T>>.GetEnumerator()
=> GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
public readonly struct Enumerator : IGridIndexedValueEnumerator<T>
{
private readonly ReadArrayDictionary<GridIndex, T> source;
private readonly IEnumerator<GridIndex> enumerator;
private readonly bool hasSource;
public Enumerator(ArrayGrid<T> grid, IEnumerator<GridIndex> enumerator)
{
this.source = grid?.data ?? ReadArrayDictionary<GridIndex, T>.Empty;
this.enumerator = enumerator;
this.hasSource = true;
}
public bool ContainsCurrent()
=> this.enumerator != null &&
this.source.ContainsKey(this.enumerator.Current);
public GridValue<T> Current
{
get
{
var key = this.enumerator.Current;
this.source.TryGetValue(in key, out var value);
return new GridValue<T>(in key, value);
}
}
public bool MoveNext()
=> this.hasSource && this.enumerator.MoveNext();
object IEnumerator.Current
=> this.Current;
void IEnumerator.Reset()
=> this.enumerator?.Reset();
public void Dispose()
=> this.enumerator?.Dispose();
}
}
}
}