Skip to content

Commit

Permalink
Combine SetHandling and CollectionExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
joelverhagen committed Jan 19, 2024
1 parent 05a4c55 commit a77fc96
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="..\FactorioTools\SetHandling.cs" Link="SetHandling.cs" />
<Compile Include="..\FactorioTools\CollectionExtensions.cs" Link="CollectionExtensions.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
79 changes: 77 additions & 2 deletions src/FactorioTools/CollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,85 @@
using System;
#if USE_BITARRAY
global using CountedBitArray = Knapcode.FactorioTools.OilField.WrapperCountedBitArray;
#else
global using CountedBitArray = Knapcode.FactorioTools.OilField.CustomCountedBitArray;
#endif

using System;
using System.Collections.Generic;
using Knapcode.FactorioTools.OilField;

namespace Knapcode.FactorioTools;

public static class CollectionExtensions
internal static class CollectionExtensions
{
public static ILocationDictionary<TValue> ToDictionary<TItem, TValue>(
this IReadOnlyCollection<TItem> items,
Context context,
Func<TItem, Location> keySelector,
Func<TItem, TValue> valueSelector)
{
var dictionary = context.GetLocationDictionary<TValue>(items.Count);
foreach (var item in items)
{
dictionary.Add(keySelector(item), valueSelector(item));
}

return dictionary;
}

public static List<Location> Distinct(this IReadOnlyCollection<Location> locations, Context context)
{
var set = context.GetLocationSet(locations.Count);
var output = new List<Location>(locations.Count);
foreach (var location in locations)
{
if (set.Add(location))
{
output.Add(location);
}
}
return output;
}

public static ILocationSet ToSet(this IReadOnlyCollection<Location> locations, Context context, bool allowEnumerate)
{
return context.GetLocationSet(locations, allowEnumerate);
}

public static ILocationSet ToReadOnlySet(this IReadOnlyCollection<Location> locations, Context context)
{
return context.GetReadOnlyLocationSet(locations);
}

public static ILocationSet ToReadOnlySet(this IReadOnlyCollection<Location> locations, Context context, bool allowEnumerate)
{
return context.GetReadOnlyLocationSet(locations, allowEnumerate);
}

#if ENABLE_VISUALIZER
public static List<DelaunatorSharp.IPoint> ToDelaunatorPoints(this ILocationSet set)
{
var points = new List<DelaunatorSharp.IPoint>();
foreach (var item in set.EnumerateItems())
{
points.Add(new DelaunatorSharp.Point(item.X, item.Y));
}

return points;
}

public static List<DelaunatorSharp.IPoint> ToDelaunatorPoints<T>(this ILocationDictionary<T> dictionary)
{
var points = new List<DelaunatorSharp.IPoint>();
foreach (var item in dictionary.Keys)
{
points.Add(new DelaunatorSharp.Point(item.X, item.Y));
}

return points;
}
#endif

public static TSource? MaxBy<TSource, TKey>(this IReadOnlyCollection<TSource> source, Func<TSource, TKey> keySelector)
{
TKey? maxKey = default;
Expand Down
85 changes: 0 additions & 85 deletions src/FactorioTools/SetHandling.cs

This file was deleted.

2 changes: 1 addition & 1 deletion test/FactorioTools.Test/FactorioTools.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</ItemGroup>

<ItemGroup>
<Compile Include="..\..\src\FactorioTools\SetHandling.cs" Link="SetHandling.cs" />
<Compile Include="..\..\src\FactorioTools\CollectionExtensions.cs" Link="CollectionExtensions.cs" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit a77fc96

Please sign in to comment.