diff --git a/Directory.Build.props b/Directory.Build.props
index 5068e67a..ffbcc1df 100644
--- a/Directory.Build.props
+++ b/Directory.Build.props
@@ -14,6 +14,11 @@
$(DefineConstants);USE_BITARRAY
+
+ $(UseDefines)
+ $(DefineConstants);USE_ARRAY
+
+
$(UseDefines)
$(DefineConstants);LOCATION_AS_STRUCT
diff --git a/src/FactorioTools/CollectionExtensions.cs b/src/FactorioTools/CollectionExtensions.cs
index 5523321e..47eff92b 100644
--- a/src/FactorioTools/CollectionExtensions.cs
+++ b/src/FactorioTools/CollectionExtensions.cs
@@ -27,10 +27,26 @@ public static ILocationDictionary ToDictionary(
return dictionary;
}
- public static List Distinct(this IReadOnlyCollection locations, Context context)
+ public static ILocationDictionary ToDictionary(
+ this IReadOnlyTableList items,
+ Context context,
+ Func keySelector,
+ Func valueSelector)
+ {
+ var dictionary = context.GetLocationDictionary(items.Count);
+ for (var i = 0; i < items.Count; i++)
+ {
+ var item = items[i];
+ dictionary.Add(keySelector(item), valueSelector(item));
+ }
+
+ return dictionary;
+ }
+
+ public static ITableList Distinct(this IReadOnlyCollection locations, Context context)
{
var set = context.GetLocationSet(locations.Count);
- var output = new List(locations.Count);
+ var output = TableList.New(locations.Count);
foreach (var location in locations)
{
if (set.Add(location))
@@ -41,25 +57,74 @@ public static List Distinct(this IReadOnlyCollection locatio
return output;
}
+ public static ILocationSet ToSet(this IReadOnlyCollection locations, Context context)
+ {
+ return locations.ToSet(context, allowEnumerate: false);
+ }
+
public static ILocationSet ToSet(this IReadOnlyCollection locations, Context context, bool allowEnumerate)
{
- return context.GetLocationSet(locations, allowEnumerate);
+ var set = context.GetLocationSet(allowEnumerate);
+ foreach (var location in locations)
+ {
+ set.Add(location);
+ }
+
+ return set;
}
public static ILocationSet ToReadOnlySet(this IReadOnlyCollection locations, Context context)
{
- return context.GetReadOnlyLocationSet(locations);
+ return locations.ToReadOnlySet(context, allowEnumerate: false);
}
public static ILocationSet ToReadOnlySet(this IReadOnlyCollection locations, Context context, bool allowEnumerate)
{
- return context.GetReadOnlyLocationSet(locations, allowEnumerate);
+ Location firstLocation = Location.Invalid;
+ int itemCount = 0;
+ ILocationSet? set = null;
+ foreach (var location in locations)
+ {
+ if (itemCount == 0)
+ {
+ firstLocation = location;
+ }
+ else if (itemCount == 1)
+ {
+ set = context.GetLocationSet(allowEnumerate);
+ set.Add(firstLocation);
+ set.Add(location);
+ }
+ else
+ {
+ set!.Add(location);
+ }
+
+ itemCount++;
+ }
+
+ if (set is null)
+ {
+ if (itemCount == 0)
+ {
+ set = EmptyLocationSet.Instance;
+ }
+ else if (itemCount == 1)
+ {
+ set = context.GetSingleLocationSet(firstLocation);
+ }
+ else
+ {
+ throw new NotImplementedException();
+ }
+ }
+ return set;
}
#if ENABLE_VISUALIZER
- public static List ToDelaunatorPoints(this ILocationSet set)
+ public static ITableList ToDelaunatorPoints(this ILocationSet set)
{
- var points = new List();
+ var points = TableList.New();
foreach (var item in set.EnumerateItems())
{
points.Add(new DelaunatorSharp.Point(item.X, item.Y));
@@ -68,9 +133,9 @@ public static ILocationSet ToReadOnlySet(this IReadOnlyCollection loca
return points;
}
- public static List ToDelaunatorPoints(this ILocationDictionary dictionary)
+ public static ITableList ToDelaunatorPoints(this ILocationDictionary dictionary)
{
- var points = new List();
+ var points = TableList.New();
foreach (var item in dictionary.Keys)
{
points.Add(new DelaunatorSharp.Point(item.X, item.Y));
@@ -186,9 +251,27 @@ public static ILocationSet ToReadOnlySet(this IReadOnlyCollection loca
return min;
}
- public static List ToList(this IReadOnlyCollection source)
+ public static TSource[] ToArray(this IReadOnlyTableList source)
+ {
+ var output = new TSource[source.Count];
+ for (var i = 0; i < source.Count; i++)
+ {
+ output[i] = source[i];
+ }
+
+ return output;
+ }
+
+ public static ITableList ToTableList(this IReadOnlyCollection source)
+ {
+ var output = TableList.New(source.Count);
+ output.AddCollection(source);
+ return output;
+ }
+
+ public static ITableList ToTableList(this IReadOnlyTableList source)
{
- var output = new List(source.Count);
+ var output = TableList.New(source.Count);
output.AddRange(source);
return output;
}
@@ -269,11 +352,10 @@ public static TSource First(this IReadOnlyCollection source, F
throw new FactorioToolsException("An item should have matched the predicate.");
}
- public static TSource? FirstOrDefault(this IReadOnlyList source, Func predicate)
+ public static TSource? FirstOrDefault(this IReadOnlyCollection source, Func predicate)
{
- for (int i = 0; i < source.Count; i++)
+ foreach (var item in source)
{
- var item = source[i];
if (predicate(item))
{
return item;
@@ -301,7 +383,7 @@ public static double Average(this IReadOnlyCollection source,
return sum / count;
}
- public static bool SequenceEqual(this IReadOnlyList first, IReadOnlyList second)
+ public static bool SequenceEqual(this IReadOnlyTableList first, IReadOnlyTableList second)
{
if (first.Count != second.Count)
{
@@ -321,12 +403,12 @@ public static bool SequenceEqual(this IReadOnlyList first, IRe
return true;
}
- public static int Sum(this IReadOnlyList source, Func selector)
+ public static int Sum(this IReadOnlyCollection source, Func selector)
{
var sum = 0;
- for (var i = 0; i < source.Count; i++)
+ foreach (var item in source)
{
- sum += selector(source[i]);
+ sum += selector(item);
}
return sum;
diff --git a/src/FactorioTools/OilField/Algorithms/AStar.cs b/src/FactorioTools/OilField/Algorithms/AStar.cs
index 7547ddaa..562d70eb 100644
--- a/src/FactorioTools/OilField/Algorithms/AStar.cs
+++ b/src/FactorioTools/OilField/Algorithms/AStar.cs
@@ -10,7 +10,15 @@ namespace Knapcode.FactorioTools.OilField;
///
public static class AStar
{
- public static AStarResult GetShortestPath(Context context, SquareGrid grid, Location start, ILocationSet goals, bool preferNoTurns = true, int xWeight = 1, int yWeight = 1, List? outputList = null)
+ public static AStarResult GetShortestPath(
+ Context context,
+ SquareGrid grid,
+ Location start,
+ ILocationSet goals,
+ bool preferNoTurns = true,
+ int xWeight = 1,
+ int yWeight = 1,
+ ITableList? outputList = null)
{
if (goals.Contains(start))
{
@@ -20,7 +28,7 @@ public static AStarResult GetShortestPath(Context context, SquareGrid grid, Loca
}
else
{
- outputList = new List { start };
+ outputList = TableList.New(start);
}
return new AStarResult(success: true, start, outputList);
diff --git a/src/FactorioTools/OilField/Algorithms/AStarResult.cs b/src/FactorioTools/OilField/Algorithms/AStarResult.cs
index 8b9f5688..850586c1 100644
--- a/src/FactorioTools/OilField/Algorithms/AStarResult.cs
+++ b/src/FactorioTools/OilField/Algorithms/AStarResult.cs
@@ -4,9 +4,9 @@ namespace Knapcode.FactorioTools.OilField;
public class AStarResult
{
- private readonly List? _path;
+ private readonly ITableList? _path;
- public AStarResult(bool success, Location reachedGoal, List? path)
+ public AStarResult(bool success, Location reachedGoal, ITableList? path)
{
Success = success;
ReachedGoal = reachedGoal;
@@ -16,7 +16,7 @@ public AStarResult(bool success, Location reachedGoal, List? path)
public bool Success { get; }
public Location ReachedGoal { get; }
- public List Path
+ public ITableList Path
{
get
{
diff --git a/src/FactorioTools/OilField/Algorithms/BreadthFirstFinder.cs b/src/FactorioTools/OilField/Algorithms/BreadthFirstFinder.cs
index e0eb9e5b..e103aa3e 100644
--- a/src/FactorioTools/OilField/Algorithms/BreadthFirstFinder.cs
+++ b/src/FactorioTools/OilField/Algorithms/BreadthFirstFinder.cs
@@ -5,7 +5,7 @@ namespace Knapcode.FactorioTools.OilField
{
public static class BreadthFirstFinder
{
- public static List? GetShortestPath(Context context, Location start, Location goal)
+ public static ITableList? GetShortestPath(Context context, Location start, Location goal)
{
#if !USE_SHARED_INSTANCES
var toExplore = new Queue();
@@ -40,7 +40,7 @@ public static class BreadthFirstFinder
if (current == goal)
{
- var output = new List { current };
+ var output = TableList.New(current);
while (parents.TryGetValue(current, out var parent))
{
output.Add(parent);
diff --git a/src/FactorioTools/OilField/Algorithms/BresenhamsLine.cs b/src/FactorioTools/OilField/Algorithms/BresenhamsLine.cs
index 6ae1d8b2..886a6c82 100644
--- a/src/FactorioTools/OilField/Algorithms/BresenhamsLine.cs
+++ b/src/FactorioTools/OilField/Algorithms/BresenhamsLine.cs
@@ -5,9 +5,9 @@ namespace Knapcode.FactorioTools.OilField;
public static class BresenhamsLine
{
- public static List GetPath(Location a, Location b)
+ public static ITableList GetPath(Location a, Location b)
{
- var line = new List();
+ var line = TableList.New();
var dx = Math.Abs(b.X - a.X);
var sx = a.X < b.X ? 1 : -1;
var dy = -1 * Math.Abs(b.Y - a.Y);
diff --git a/src/FactorioTools/OilField/Algorithms/DijkstrasResult.cs b/src/FactorioTools/OilField/Algorithms/DijkstrasResult.cs
index 7f8e6798..3ebaa6b1 100644
--- a/src/FactorioTools/OilField/Algorithms/DijkstrasResult.cs
+++ b/src/FactorioTools/OilField/Algorithms/DijkstrasResult.cs
@@ -14,16 +14,16 @@ public DijkstrasResult(ILocationDictionary locationToPrevious, ILo
public ILocationDictionary LocationToPrevious { get; }
public ILocationSet ReachedGoals { get; }
- public List> GetStraightPaths(Location goal)
+ public ITableList> GetStraightPaths(Location goal)
{
- var paths = new List>();
+ var paths = TableList.New>();
if (LocationToPrevious.TryGetValue(goal, out var previousLocations))
{
if (previousLocations.Count == 0)
{
// This is a special case when the goal matches the starting point.
- paths.Add(new List { goal });
+ paths.Add(TableList.New(goal));
}
foreach (var beforeGoal in previousLocations.EnumerateItems())
@@ -43,7 +43,7 @@ public List> GetStraightPaths(Location goal)
};
var current = goal;
- var path = new List();
+ var path = TableList.New();
while (true)
{
path.Add(current);
diff --git a/src/FactorioTools/OilField/Algorithms/Prims.cs b/src/FactorioTools/OilField/Algorithms/Prims.cs
index 77ecc62f..fb8c7a47 100644
--- a/src/FactorioTools/OilField/Algorithms/Prims.cs
+++ b/src/FactorioTools/OilField/Algorithms/Prims.cs
@@ -57,8 +57,10 @@ public static ILocationDictionary GetMinimumSpanningTree(
if (!digraph)
{
// Make the MST bidirectional (a graph, not a digraph).
- foreach (var center in mst.Keys.ToList())
+ var keys = mst.Keys.ToTableList();
+ for (var i = 0; i < keys.Count; i++)
{
+ var center = keys[i];
foreach (var neighbor in mst[center].EnumerateItems())
{
if (!mst.TryGetValue(neighbor, out var otherNeighbors))
diff --git a/src/FactorioTools/OilField/Containers/DictionaryTableList.cs b/src/FactorioTools/OilField/Containers/DictionaryTableList.cs
new file mode 100644
index 00000000..85f6f9cd
--- /dev/null
+++ b/src/FactorioTools/OilField/Containers/DictionaryTableList.cs
@@ -0,0 +1,186 @@
+using System;
+using System.Collections.Generic;
+
+namespace Knapcode.FactorioTools.OilField;
+
+public class DictionaryTableList : ITableList
+{
+ private readonly Dictionary _dictionary;
+
+ public DictionaryTableList()
+ {
+ _dictionary = new Dictionary();
+ }
+
+ public DictionaryTableList(int capacity)
+ {
+ _dictionary = new Dictionary(capacity);
+ }
+
+ public T this[int index]
+ {
+ get
+ {
+ if (index >= _dictionary.Count)
+ {
+ throw new IndexOutOfRangeException();
+ }
+
+ return _dictionary[index];
+ }
+ set
+ {
+ if (index >= _dictionary.Count)
+ {
+ throw new IndexOutOfRangeException();
+ }
+
+ _dictionary[index] = value;
+ }
+ }
+
+ public int Count => _dictionary.Count;
+
+ public void Add(T item)
+ {
+ _dictionary.Add(_dictionary.Count, item);
+ }
+
+ public void AddCollection(IReadOnlyCollection collection)
+ {
+ foreach (var item in collection)
+ {
+ _dictionary.Add(_dictionary.Count, item);
+ }
+ }
+
+ public void AddRange(IReadOnlyTableList collection)
+ {
+ var other = (DictionaryTableList)collection;
+ for (var i = 0; i < other.Count; i++)
+ {
+ _dictionary.Add(_dictionary.Count, other[i]);
+ }
+ }
+
+ public void Clear()
+ {
+ _dictionary.Clear();
+ }
+
+ public bool Contains(T item)
+ {
+ var comparer = EqualityComparer.Default;
+ foreach (var value in _dictionary.Values)
+ {
+ if (comparer.Equals(item, value))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ public IReadOnlyCollection EnumerateItems()
+ {
+ return _dictionary.Values;
+ }
+
+ public bool Remove(T item)
+ {
+ var comparer = EqualityComparer.Default;
+ var index = -1;
+ foreach (var (key, value) in _dictionary)
+ {
+ if (comparer.Equals(item, value))
+ {
+ index = key;
+ break;
+ }
+ }
+
+ if (index == -1)
+ {
+ return false;
+ }
+
+ RemoveAt(index);
+ return true;
+ }
+
+ public void RemoveAt(int index)
+ {
+ RemoveRange(index, 1);
+ }
+
+ public void RemoveRange(int index, int count)
+ {
+ var originalCount = _dictionary.Count;
+ if (index + count > originalCount)
+ {
+ throw new IndexOutOfRangeException();
+ }
+
+ var startIndex = index + count;
+ for (var i = startIndex; i < originalCount; i++)
+ {
+ _dictionary[i - count] = _dictionary[i];
+ _dictionary.Remove(i);
+ }
+
+ for (var i = originalCount - count; i < startIndex; i++)
+ {
+ _dictionary.Remove(i);
+ }
+ }
+
+ public void Reverse()
+ {
+ var count = _dictionary.Count;
+ for (var i = 0; i < count / 2; i++)
+ {
+ var temp = _dictionary[i];
+ var otherIndex = count - 1 - i;
+ _dictionary[i] = _dictionary[otherIndex];
+ _dictionary[otherIndex] = temp;
+ }
+ }
+
+ public void Sort(Comparison comparison)
+ {
+ var keys = new int[_dictionary.Count];
+ var values = new T[_dictionary.Count];
+ for (var i = 0; i < keys.Length; i++)
+ {
+ keys[i] = i;
+ values[i] = _dictionary[i];
+ }
+
+ Array.Sort(keys, (a, b) => comparison(this[a], this[b]));
+
+ for (var i = 0; i < keys.Length; i++)
+ {
+ _dictionary[i] = values[keys[i]];
+ }
+ }
+
+ public void SortRange(int index, int count, IComparer comparer)
+ {
+ var keys = new int[_dictionary.Count];
+ var values = new T[_dictionary.Count];
+ for (var i = 0; i < keys.Length; i++)
+ {
+ keys[i] = i;
+ values[i] = _dictionary[i];
+ }
+
+ var keyComparer = Comparer.Create((a, b) => comparer.Compare(this[a], this[b]));
+ Array.Sort(keys, index, count, keyComparer);
+
+ for (var i = 0; i < keys.Length; i++)
+ {
+ _dictionary[i] = values[keys[i]];
+ }
+ }
+}
diff --git a/src/FactorioTools/OilField/Containers/IReadOnlyTableList.cs b/src/FactorioTools/OilField/Containers/IReadOnlyTableList.cs
new file mode 100644
index 00000000..58161ec2
--- /dev/null
+++ b/src/FactorioTools/OilField/Containers/IReadOnlyTableList.cs
@@ -0,0 +1,12 @@
+using System.Collections.Generic;
+
+namespace Knapcode.FactorioTools.OilField;
+
+public interface IReadOnlyTableList
+{
+ T this[int index] { get; }
+ int Count { get; }
+
+ bool Contains(T item);
+ IReadOnlyCollection EnumerateItems();
+}
diff --git a/src/FactorioTools/OilField/Containers/ITableList.cs b/src/FactorioTools/OilField/Containers/ITableList.cs
new file mode 100644
index 00000000..366882e3
--- /dev/null
+++ b/src/FactorioTools/OilField/Containers/ITableList.cs
@@ -0,0 +1,20 @@
+using System;
+using System.Collections.Generic;
+
+namespace Knapcode.FactorioTools.OilField;
+
+public interface ITableList : IReadOnlyTableList
+{
+ new T this[int index] { get; set; }
+
+ void Add(T item);
+ void AddCollection(IReadOnlyCollection collection);
+ void AddRange(IReadOnlyTableList collection);
+ void Clear();
+ bool Remove(T item);
+ void RemoveAt(int index);
+ void RemoveRange(int index, int count);
+ void Reverse();
+ void Sort(Comparison comparison);
+ void SortRange(int index, int count, IComparer comparer);
+}
diff --git a/src/FactorioTools/OilField/Containers/ListTableList.cs b/src/FactorioTools/OilField/Containers/ListTableList.cs
new file mode 100644
index 00000000..6a694010
--- /dev/null
+++ b/src/FactorioTools/OilField/Containers/ListTableList.cs
@@ -0,0 +1,87 @@
+using System;
+using System.Collections.Generic;
+
+namespace Knapcode.FactorioTools.OilField;
+
+public class ListTableList : ITableList
+{
+ private readonly List _list;
+
+ public ListTableList()
+ {
+ _list = new List();
+ }
+
+ public ListTableList(int capacity)
+ {
+ _list = new List(capacity);
+ }
+
+ public T this[int index]
+ {
+ get => _list[index];
+ set => _list[index] = value;
+ }
+
+ public int Count => _list.Count;
+
+ public void Add(T item)
+ {
+ _list.Add(item);
+ }
+
+ public void AddCollection(IReadOnlyCollection collection)
+ {
+ _list.AddRange(collection);
+ }
+
+ public void AddRange(IReadOnlyTableList collection)
+ {
+ _list.AddRange(((ListTableList)collection)._list);
+ }
+
+ public void Clear()
+ {
+ _list.Clear();
+ }
+
+ public bool Contains(T item)
+ {
+ return _list.Contains(item);
+ }
+
+ public IReadOnlyCollection EnumerateItems()
+ {
+ return _list;
+ }
+
+ public bool Remove(T item)
+ {
+ return _list.Remove(item);
+ }
+
+ public void RemoveAt(int index)
+ {
+ _list.RemoveAt(index);
+ }
+
+ public void RemoveRange(int index, int count)
+ {
+ _list.RemoveRange(index, count);
+ }
+
+ public void Reverse()
+ {
+ _list.Reverse();
+ }
+
+ public void Sort(Comparison comparison)
+ {
+ _list.Sort(comparison);
+ }
+
+ public void SortRange(int index, int count, IComparer comparer)
+ {
+ _list.Sort(index, count, comparer);
+ }
+}
diff --git a/src/FactorioTools/OilField/Containers/TableList.cs b/src/FactorioTools/OilField/Containers/TableList.cs
new file mode 100644
index 00000000..6e49a08c
--- /dev/null
+++ b/src/FactorioTools/OilField/Containers/TableList.cs
@@ -0,0 +1,39 @@
+namespace Knapcode.FactorioTools.OilField;
+
+public static class TableList
+{
+ public static ITableList New()
+ {
+#if USE_ARRAY
+ return new ListTableList();
+#else
+ return new DictionaryTableList();
+#endif
+ }
+
+ public static ITableList New(int capacity)
+ {
+#if USE_ARRAY
+ return new ListTableList(capacity);
+#else
+ return new DictionaryTableList(capacity);
+#endif
+ }
+
+ public static ITableList New(T item)
+ {
+ var list = New();
+ list.Add(item);
+ return list;
+ }
+
+ public static IReadOnlyTableList Empty()
+ {
+ return EmptyInstances.Instance;
+ }
+
+ private static class EmptyInstances
+ {
+ public static IReadOnlyTableList Instance { get; } = New(capacity: 0);
+ }
+}
diff --git a/src/FactorioTools/OilField/Helpers.cs b/src/FactorioTools/OilField/Helpers.cs
index fddb8d2a..f2c0c21d 100644
--- a/src/FactorioTools/OilField/Helpers.cs
+++ b/src/FactorioTools/OilField/Helpers.cs
@@ -40,18 +40,19 @@ public static PumpjackCenter AddPumpjack(SquareGrid grid, Location center)
return centerEntity;
}
- public static ILocationDictionary> GetCenterToTerminals(Context context, SquareGrid grid, IReadOnlyCollection centers)
+ public static ILocationDictionary> GetCenterToTerminals(Context context, SquareGrid grid, IReadOnlyTableList centers)
{
- var centerToTerminals = context.GetLocationDictionary>();
+ var centerToTerminals = context.GetLocationDictionary>();
PopulateCenterToTerminals(centerToTerminals, grid, centers);
return centerToTerminals;
}
- public static void PopulateCenterToTerminals(ILocationDictionary> centerToTerminals, SquareGrid grid, IReadOnlyCollection centers)
+ public static void PopulateCenterToTerminals(ILocationDictionary> centerToTerminals, SquareGrid grid, IReadOnlyTableList centers)
{
- foreach (var center in centers)
+ for (var i = 0; i < centers.Count; i++)
{
- var candidateTerminals = new List();
+ var center = centers[i];
+ var candidateTerminals = TableList.New();
foreach ((var direction, var translation) in TerminalOffsets)
{
var location = center.Translate(translation);
@@ -72,22 +73,23 @@ public static void PopulateCenterToTerminals(ILocationDictionary> GetLocationToTerminals(Context context, ILocationDictionary> centerToTerminals)
+ public static ILocationDictionary> GetLocationToTerminals(Context context, ILocationDictionary> centerToTerminals)
{
- var locationToTerminals = context.GetLocationDictionary>();
+ var locationToTerminals = context.GetLocationDictionary>();
PopulateLocationToTerminals(locationToTerminals, centerToTerminals);
return locationToTerminals;
}
- public static void PopulateLocationToTerminals(ILocationDictionary> locationToTerminals, ILocationDictionary> centerToTerminals)
+ public static void PopulateLocationToTerminals(ILocationDictionary> locationToTerminals, ILocationDictionary> centerToTerminals)
{
foreach (var terminals in centerToTerminals.Values)
{
- foreach (var terminal in terminals)
+ for (var i = 0; i < terminals.Count; i++)
{
+ var terminal = terminals[i];
if (!locationToTerminals.TryGetValue(terminal.Terminal, out var list))
{
- list = new List(2);
+ list = TableList.New(2);
locationToTerminals.Add(terminal.Terminal, list);
}
@@ -98,7 +100,7 @@ public static void PopulateLocationToTerminals(ILocationDictionary CandidateToInfo, CountedBitArray CoveredEntities, ILocationDictionary Providers) GetBeaconCandidateToCovered(
Context context,
- List recipients,
+ IReadOnlyTableList recipients,
ICandidateFactory candidateFactory,
bool removeUnused)
where TInfo : CandidateInfo
@@ -118,7 +120,7 @@ public static (ILocationDictionary CandidateToInfo, CountedBitArray Cover
public static (ILocationDictionary CandidateToInfo, CountedBitArray CoveredEntities, ILocationDictionary Providers) GetElectricPoleCandidateToCovered(
Context context,
- List recipients,
+ IReadOnlyTableList recipients,
ICandidateFactory candidateFactory,
bool removeUnused)
where TInfo : CandidateInfo
@@ -138,7 +140,7 @@ public static (ILocationDictionary CandidateToInfo, CountedBitArray Cover
private static (ILocationDictionary CandidateToInfo, CountedBitArray CoveredEntities, ILocationDictionary Providers) GetCandidateToCovered(
Context context,
- List recipients,
+ IReadOnlyTableList recipients,
ICandidateFactory candidateFactory,
int providerWidth,
int providerHeight,
@@ -294,7 +296,7 @@ private static (ILocationDictionary CandidateToInfo, CountedBitArray Cove
if (providers.Count > 0 || unusedProviders.Count > 0)
{
// Remove candidates that only cover recipients that are already covered.
- var toRemove = new List();
+ var toRemove = TableList.New();
foreach ((var candidate, var info) in candidateToInfo.EnumeratePairs())
{
var subset = new CountedBitArray(info.Covered);
@@ -443,7 +445,7 @@ public static bool DoesProviderFit(
return true;
}
- public static double GetEntityDistance(List poweredEntities, Location candidate, CountedBitArray covered)
+ public static double GetEntityDistance(ITableList poweredEntities, Location candidate, CountedBitArray covered)
{
double sum = 0;
for (var i = 0; i < poweredEntities.Count; i++)
@@ -463,7 +465,7 @@ public static void AddProviderAndPreventMultipleProviders(
TInfo centerInfo,
int providerWidth,
int providerHeight,
- List recipients,
+ IReadOnlyTableList recipients,
CountedBitArray coveredEntities,
Dictionary> coveredToCandidates,
ILocationDictionary candidateToInfo)
@@ -491,7 +493,7 @@ public static void AddProviderAndPreventMultipleProviders(
coveredToCandidates);
#if !USE_SHARED_INSTANCES
- var toRemove = new List();
+ var toRemove = TableList.New();
var updated = context.GetLocationSet();
#else
var toRemove = context.SharedInstances.LocationListA;
@@ -542,7 +544,7 @@ public static void AddProviderAndAllowMultipleProviders(
TInfo centerInfo,
int providerWidth,
int providerHeight,
- List recipients,
+ IReadOnlyTableList recipients,
CountedBitArray coveredEntities,
Dictionary> coveredToCandidates,
ILocationDictionary candidateToInfo,
@@ -578,7 +580,7 @@ public static void AddProviderAndAllowMultipleProviders(
}
#if !USE_SHARED_INSTANCES
- var toRemove = new List();
+ var toRemove = TableList.New();
var updated = context.GetLocationSet();
#else
var toRemove = context.SharedInstances.LocationListA;
@@ -667,7 +669,7 @@ public static void AddProviderAndAllowMultipleProviders(
public static (ILocationDictionary PoleCenterToCoveredCenters, ILocationDictionary CoveredCenterToPoleCenters) GetElectricPoleCoverage(
Context context,
- List poweredEntities,
+ IReadOnlyTableList poweredEntities,
IReadOnlyCollection electricPoleCenters)
{
var poleCenterToCoveredCenters = GetProviderCenterToCoveredCenters(
@@ -690,9 +692,9 @@ public static (ILocationDictionary PoleCenterToCoveredCenters, ILo
return (PoleCenterToCoveredCenters: poleCenterToCoveredCenters, CoveredCenterToPoleCenters: coveredCenterToPoleCenters);
}
- public static (List PoweredEntities, bool HasBeacons) GetPoweredEntities(Context context)
+ public static (ITableList PoweredEntities, bool HasBeacons) GetPoweredEntities(Context context)
{
- var poweredEntities = new List();
+ var poweredEntities = TableList.New();
var hasBeacons = false;
foreach (var location in context.Grid.EntityLocations.EnumerateItems())
@@ -924,13 +926,13 @@ public static void AddProviderToGrid(
}
}
- public static void AddBeaconsToGrid(SquareGrid grid, OilFieldOptions options, IEnumerable centers)
+ public static void AddBeaconsToGrid(SquareGrid grid, OilFieldOptions options, IReadOnlyTableList centers)
{
- foreach (var center in centers)
+ for (var i = 0; i < centers.Count; i++)
{
AddProviderToGrid(
grid,
- center,
+ centers[i],
new BeaconCenter(grid.GetId()),
c => new BeaconSide(grid.GetId(), c),
options.BeaconWidth,
@@ -979,15 +981,15 @@ public static void EliminateOtherTerminals(Context context, TerminalLocation sel
terminalOptions.Add(selectedTerminal);
}
- public static List GetPath(ILocationDictionary cameFrom, Location start, Location reachedGoal)
+ public static ITableList GetPath(ILocationDictionary cameFrom, Location start, Location reachedGoal)
{
var sizeEstimate = 2 * start.GetManhattanDistance(reachedGoal);
- var path = new List(sizeEstimate);
+ var path = TableList.New(sizeEstimate);
AddPath(cameFrom, reachedGoal, path);
return path;
}
- public static void AddPath(ILocationDictionary cameFrom, Location reachedGoal, List outputList)
+ public static void AddPath(ILocationDictionary cameFrom, Location reachedGoal, ITableList outputList)
{
var current = reachedGoal;
while (true)
@@ -1003,7 +1005,7 @@ public static void AddPath(ILocationDictionary cameFrom, Location reac
}
}
- public static bool AreLocationsCollinear(IReadOnlyList locations)
+ public static bool AreLocationsCollinear(IReadOnlyTableList locations)
{
double lastSlope = 0;
for (var i = 0; i < locations.Count; i++)
@@ -1030,7 +1032,7 @@ public static bool AreLocationsCollinear(IReadOnlyList locations)
return false;
}
- public static int CountTurns(List path)
+ public static int CountTurns(IReadOnlyTableList path)
{
var previousDirection = -1;
var turns = 0;
@@ -1051,12 +1053,12 @@ public static int CountTurns(List path)
return turns;
}
- public static List? MakeStraightLineOnEmpty(SquareGrid grid, Location a, Location b)
+ public static ITableList? MakeStraightLineOnEmpty(SquareGrid grid, Location a, Location b)
{
if (a.X == b.X)
{
(var min, var max) = a.Y < b.Y ? (a.Y, b.Y) : (b.Y, a.Y);
- var line = new List(max - min + 1);
+ var line = TableList.New(max - min + 1);
for (var y = min; y <= max; y++)
{
if (!grid.IsEmpty(new Location(a.X, y)))
@@ -1073,7 +1075,7 @@ public static int CountTurns(List path)
if (a.Y == b.Y)
{
(var min, var max) = a.X < b.X ? (a.X, b.X) : (b.X, a.X);
- var line = new List(max - min + 1);
+ var line = TableList.New