-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGrid{T}.GridValues.cs
72 lines (58 loc) · 2.28 KB
/
Grid{T}.GridValues.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
using System.Collections;
using System.Collections.Generic;
namespace System.Grid
{
public partial class Grid<T>
{
public readonly struct GridValues : IGridValues<T>
{
private readonly Grid<T> source;
private readonly IEnumerator<GridIndex> enumerator;
public GridValues(Grid<T> grid, IEnumerator<GridIndex> enumerator)
{
this.source = grid;
this.enumerator = enumerator;
}
public Enumerator GetEnumerator()
=> new Enumerator(this.source, this.enumerator);
IGridValueEnumerator<T> IGridValues<T>.GetEnumerator()
=> GetEnumerator();
IEnumerator<T> IEnumerable<T>.GetEnumerator()
=> GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
public struct Enumerator : IGridValueEnumerator<T>
{
private readonly ReadDictionary<GridIndex, T> source;
private readonly IEnumerator<GridIndex> enumerator;
private readonly bool hasSource;
public Enumerator(Grid<T> grid, IEnumerator<GridIndex> enumerator)
{
this.source = grid?.data ?? ReadDictionary<GridIndex, T>.Empty;
this.enumerator = enumerator;
this.hasSource = true;
}
public bool ContainsCurrent()
=> this.enumerator != null &&
this.source.ContainsKey(this.enumerator.Current);
public T Current
{
get
{
var key = this.enumerator.Current;
this.source.TryGetValue(key, out var value);
return 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();
}
}
}
}