-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCellMap.cs
56 lines (48 loc) · 1.4 KB
/
CellMap.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace GeodesicGrid
{
public class CellMap<T> : IEnumerable<KeyValuePair<Cell, T>>
{
private readonly T[] values;
public CellMap(int level)
{
values = new T[Cell.CountAtLevel(level)];
}
public CellMap(int level, Func<Cell, T> selector)
{
values = Cell.AtLevel(level).Select(selector).ToArray();
}
public CellMap(CellMap<T> other)
{
values = other.values.ToArray();
}
public int Level
{
get { return new Cell((uint)values.Length - 1).Level; }
}
public T this[Cell cell]
{
get
{
if (cell.Index >= values.Length) { throw new ArgumentException(); }
return values[cell.Index];
}
set
{
if (cell.Index >= values.Length) { throw new ArgumentException(); }
values[cell.Index] = value;
}
}
public IEnumerator<KeyValuePair<Cell, T>> GetEnumerator()
{
for (uint i = 0; i < values.Length; i++)
{
yield return new KeyValuePair<Cell, T>(new Cell(i), values[i]);
}
}
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
}
}