Skip to content

Commit

Permalink
Lift Cell.Set to a namespace-level class
Browse files Browse the repository at this point in the history
  • Loading branch information
Majiir committed Sep 1, 2014
1 parent 652abe2 commit be87d24
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 57 deletions.
52 changes: 0 additions & 52 deletions Plugin/GeodesicGrid/Cell.cs
Expand Up @@ -828,57 +828,5 @@ public override bool Equals(object obj)
public static bool operator !=(Cell a, Cell b) { return !(a == b); }

#endregion

#region Specialized collections

public class Set : IEnumerable<Cell>
{
private readonly BitArray set;

public Set(int level)
{
this.set = new BitArray((int)Cell.CountAtLevel(level));
}

public Set(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()
{
return set.ToByteArray();
}
}

#endregion
}
}
54 changes: 54 additions & 0 deletions Plugin/GeodesicGrid/Set.cs
@@ -0,0 +1,54 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace Kethane.GeodesicGrid
{
public class Set : IEnumerable<Cell>
{
private readonly BitArray set;

public Set(int level)
{
this.set = new BitArray((int)Cell.CountAtLevel(level));
}

public Set(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()
{
return set.ToByteArray();
}
}
}
1 change: 1 addition & 0 deletions Plugin/Kethane.csproj
Expand Up @@ -50,6 +50,7 @@
<Compile Include="CellularResourceGenerator.cs" />
<Compile Include="CompatibilityChecker.cs" />
<Compile Include="GeodesicGrid\Map.cs" />
<Compile Include="GeodesicGrid\Set.cs" />
<Compile Include="InstallCleanup.cs" />
<Compile Include="IWindowToggle.cs" />
<Compile Include="LegacyResourceGenerator.cs" />
Expand Down
10 changes: 5 additions & 5 deletions Plugin/KethaneData.cs
Expand Up @@ -29,7 +29,7 @@ public static KethaneData Current
}

public Dictionary<string, Dictionary<string, IBodyResources>> PlanetDeposits = new Dictionary<string,Dictionary<string,IBodyResources>>();
public Dictionary<string, Dictionary<string, Cell.Set>> Scans = new Dictionary<string,Dictionary<string,Cell.Set>>();
public Dictionary<string, Dictionary<string, Set>> Scans = new Dictionary<string,Dictionary<string,Set>>();

private Dictionary<string, ConfigNode> generatorNodes = new Dictionary<string, ConfigNode>();
private Dictionary<string, IResourceGenerator> generators = new Dictionary<string, IResourceGenerator>();
Expand Down Expand Up @@ -82,7 +82,7 @@ public override void OnLoad(ConfigNode config)
var resourceNode = resourceNodes.SingleOrDefault(n => n.GetValue("Resource") == resourceName) ?? new ConfigNode();

PlanetDeposits[resourceName] = new Dictionary<string, IBodyResources>();
Scans[resourceName] = new Dictionary<string, Cell.Set>();
Scans[resourceName] = new Dictionary<string, Set>();

generatorNodes[resourceName] = resourceNode.GetNode("Generator") ?? resource.Generator;
var generator = createGenerator(generatorNodes[resourceName].CreateCopy());
Expand All @@ -100,14 +100,14 @@ public override void OnLoad(ConfigNode config)
var bodyNode = bodyNodes.SingleOrDefault(n => n.GetValue("Name") == body.name) ?? new ConfigNode();

PlanetDeposits[resourceName][body.name] = generator.Load(body, bodyNode.GetNode("GeneratorData"));
Scans[resourceName][body.name] = new Cell.Set(MapOverlay.GridLevel);
Scans[resourceName][body.name] = new Set(MapOverlay.GridLevel);

var scanMask = bodyNode.GetValue("ScanMask");
if (scanMask != null)
{
try
{
Scans[resourceName][body.name] = new Cell.Set(MapOverlay.GridLevel, Misc.FromBase64String(scanMask));
Scans[resourceName][body.name] = new Set(MapOverlay.GridLevel, Misc.FromBase64String(scanMask));
}
catch (FormatException e)
{
Expand All @@ -127,7 +127,7 @@ public override void OnLoad(ConfigNode config)
foreach (var body in FlightGlobals.Bodies)
{
var old = Scans[resource.Resource][body.name];
var set = new Cell.Set(MapOverlay.GridLevel);
var set = new Set(MapOverlay.GridLevel);

foreach (var cell in Cell.AtLevel(MapOverlay.GridLevel))
{
Expand Down

0 comments on commit be87d24

Please sign in to comment.