-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCellSet.cs
56 lines (49 loc) · 1.37 KB
/
CellSet.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;
namespace GeodesicGrid
{
public class CellSet : IEnumerable<Cell>
{
private readonly BitArray set;
public CellSet(int level)
{
this.set = new BitArray((int)Cell.CountAtLevel(level));
}
public CellSet(int level, byte[] array)
{
this.set = new BitArray(array);
this.set.Length = (int)Cell.CountAtLevel(level);
}
public bool this[Cell cell]
{
get
{
if (cell.Index >= set.Count) { throw new ArgumentException(); }
return set[(int)cell.Index];
}
set
{
if (cell.Index >= set.Count) { throw new ArgumentException(); }
set[(int)cell.Index] = value;
}
}
public IEnumerator<Cell> GetEnumerator()
{
for (var i = 0; i < set.Count; i++)
{
if (set[i])
{
yield return new Cell((uint)i);
}
}
}
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
public byte[] ToByteArray()
{
byte[] ret = new byte[(set.Length - 1) / 8 + 1];
set.CopyTo(ret, 0);
return ret;
}
}
}