Skip to content

Commit

Permalink
TABS
Browse files Browse the repository at this point in the history
  • Loading branch information
davidaramant committed Feb 21, 2024
1 parent 1b55775 commit db6c2a6
Show file tree
Hide file tree
Showing 341 changed files with 17,596 additions and 17,595 deletions.
3 changes: 2 additions & 1 deletion .csharpierrc.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# This doesn't work in editorconfig
printWidth: 120
printWidth: 120
useTabs: true
56 changes: 28 additions & 28 deletions src/ConsoleApps/Benchmarks/ConnectedComponentLabelingBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,35 @@

namespace Benchmarks
{
public class ConnectedComponentLabelingBenchmarks
{
private const int Seed = 3;
private readonly Size _dimensions = new(1024, 1024);
private CellBoard _board;
public class ConnectedComponentLabelingBenchmarks
{
private const int Seed = 3;
private readonly Size _dimensions = new(1024, 1024);
private CellBoard _board;

[GlobalSetup]
public void Setup()
{
var random = new Random(Seed);
_board = new CellBoard(_dimensions)
.Fill(random, probabilityAlive: 0.6)
.MakeBorderAlive(thickness: 3)
.RunGenerations(6);
}
[GlobalSetup]
public void Setup()
{
var random = new Random(Seed);
_board = new CellBoard(_dimensions)
.Fill(random, probabilityAlive: 0.6)
.MakeBorderAlive(thickness: 3)
.RunGenerations(6);
}

[Benchmark]
public int Baseline()
{
return ConnectedAreaAnalyzer
.FindForegroundAreas(_board.Dimensions, p => _board[p] == CellType.Dead)
.Count();
}
[Benchmark]
public int Baseline()
{
return ConnectedAreaAnalyzer
.FindForegroundAreas(_board.Dimensions, p => _board[p] == CellType.Dead)
.Count();
}

//[Benchmark]
//public int Tweaks()
//{
// return ConnectedAreaAnalyzer
// .FindEmptyAreas2(_board.Dimensions, p => _board[p] == CellType.Dead).Count();
//}
}
//[Benchmark]
//public int Tweaks()
//{
// return ConnectedAreaAnalyzer
// .FindEmptyAreas2(_board.Dimensions, p => _board[p] == CellType.Dead).Count();
//}
}
}
38 changes: 19 additions & 19 deletions src/ConsoleApps/Benchmarks/EdgeDistanceBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@ namespace Benchmarks;

public class EdgeDistanceBenchmarks
{
private ConnectedArea _area;
private ConnectedArea _area;

[GlobalSetup]
public void Setup()
{
var random = new Random(3);
var board = new CellBoard(new Size(256, 256))
.Fill(random, probabilityAlive: 0.5)
.MakeBorderAlive(thickness: 1)
.GenerateStandardCave()
.ScaleAndSmooth(times: 3)
.TrimToLargestDeadArea();
[GlobalSetup]
public void Setup()
{
var random = new Random(3);
var board = new CellBoard(new Size(256, 256))
.Fill(random, probabilityAlive: 0.5)
.MakeBorderAlive(thickness: 1)
.GenerateStandardCave()
.ScaleAndSmooth(times: 3)
.TrimToLargestDeadArea();

_area = ConnectedAreaAnalyzer
.FindForegroundAreas(board.Dimensions, p => board[p] == CellType.Dead)
.MaxBy(a => a.Area);
}
_area = ConnectedAreaAnalyzer
.FindForegroundAreas(board.Dimensions, p => board[p] == CellType.Dead)
.MaxBy(a => a.Area);
}

[Benchmark]
public IReadOnlyDictionary<Position, int> Baseline() => _area.DetermineInteriorEdgeDistance(Neighborhood.Moore);
[Benchmark]
public IReadOnlyDictionary<Position, int> Baseline() => _area.DetermineInteriorEdgeDistance(Neighborhood.Moore);

//[Benchmark]
//public IReadOnlyDictionary<Position, int> Tweaked() => _area.DetermineDistanceToEdges2(Neighborhood.Moore);
//[Benchmark]
//public IReadOnlyDictionary<Position, int> Tweaked() => _area.DetermineDistanceToEdges2(Neighborhood.Moore);
}
4 changes: 2 additions & 2 deletions src/ConsoleApps/DataModelGenerator/DoomGameInfo/Actor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Tiledriver.DataModelGenerator.DoomGameInfo;

sealed record Actor(string Name, string CategoryName, int Id, string Description, int Radius, int Height)
{
public int Width => Radius * 2;
public string SafeName => Name.TrimStart('$');
public int Width => Radius * 2;
public string SafeName => Name.TrimStart('$');
}
218 changes: 109 additions & 109 deletions src/ConsoleApps/DataModelGenerator/DoomGameInfo/DoomActorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,113 +13,113 @@ namespace Tiledriver.DataModelGenerator.DoomGameInfo;

internal static partial class DoomActorGenerator
{
public static void WriteToPath(string basePath)
{
if (!Directory.Exists(basePath))
{
Directory.CreateDirectory(basePath);
}

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "Tiledriver.DataModelGenerator.DoomGameInfo.DoomActors.txt";

using Stream stream =
assembly.GetManifestResourceStream(resourceName) ?? throw new Exception($"Could not find {resourceName}");
using StreamReader reader = new(stream);

var categories = DoomActorParser.ParseCategories(reader);
var actors = FlattenDefinitions(categories).ToList();

WriteActorFile(basePath, actors);
}

static IEnumerable<Actor> FlattenDefinitions(IEnumerable<ActorCategory> categories) =>
from category in categories
from actor in category.Actors
let lookup = new PropertyLookup(category.GlobalAssignments, actor.Assignments)
select new Actor(
Name: lookup.GetString("class"),
CategoryName: category.Name.Titleize().Singularize(),
Id: actor.Id,
Description: lookup.GetString("title"),
Radius: lookup.GetInt("width"), // This is named poorly in the config file
Height: lookup.GetInt("height")
);

static void WriteActorFile(string basePath, IReadOnlyList<Actor> actors)
{
using var blockStream = File.CreateText(Path.Combine(basePath, "Actor.Generated.cs"));
using var output = new IndentedWriter(blockStream);

output
.WriteHeader(
"Tiledriver.Core.GameInfo.Doom",
new[] { "System.Collections.Generic", "System.CodeDom.Compiler" }
)
.Line($"[GeneratedCode(\"{CurrentLibraryInfo.Name}\", \"{CurrentLibraryInfo.Version}\")]")
.Line($"public sealed partial record Actor")
.OpenParen();

foreach (var actor in actors)
{
output
.Line($"/// <summary>{actor.Description}</summary>")
.Line($"public static readonly Actor {actor.SafeName} = new(")
.IncreaseIndent()
.JoinLines(
",",
new[]
{
$"Id: {actor.Id}",
$"Description: \"{actor.Description}\"",
$"Width: {actor.Width}",
$"Height: {actor.Height}"
}
)
.DecreaseIndent()
.Line(");")
.Line();
}

foreach (var category in actors.GroupBy(c => c.CategoryName))
{
output
.Line($"public static class {category.Key}")
.OpenParen()
.Lines(category.Select(actor => $"public static Actor {actor.SafeName} => Actor.{actor.SafeName};"))
.Line("public static IEnumerable<Actor> GetAll()")
.OpenParen()
.Lines(category.Select(actor => $"yield return {actor.SafeName};"))
.CloseParen()
.CloseParen();
}

output.CloseParen();
}

sealed class PropertyLookup
{
private readonly string _context;
private readonly IReadOnlyDictionary<string, object> _parentAssignments;
private readonly IReadOnlyDictionary<string, object> _actorAssignments;

public PropertyLookup(
IReadOnlyDictionary<string, object> parentAssignments,
IReadOnlyDictionary<string, object> actorAssignments
)
{
_parentAssignments = parentAssignments;
_actorAssignments = actorAssignments;
_context = GetString("class");
}

private object GetValue(string name) =>
_actorAssignments.TryLookupValue(name)
?? _parentAssignments.TryLookupValue(name)
?? throw new ArgumentException($"Could not find {name} for {_context}");

public int GetInt(string name) => (int)GetValue(name);

public string GetString(string name) => (string)GetValue(name);
}
public static void WriteToPath(string basePath)
{
if (!Directory.Exists(basePath))
{
Directory.CreateDirectory(basePath);
}

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "Tiledriver.DataModelGenerator.DoomGameInfo.DoomActors.txt";

using Stream stream =
assembly.GetManifestResourceStream(resourceName) ?? throw new Exception($"Could not find {resourceName}");
using StreamReader reader = new(stream);

var categories = DoomActorParser.ParseCategories(reader);
var actors = FlattenDefinitions(categories).ToList();

WriteActorFile(basePath, actors);
}

static IEnumerable<Actor> FlattenDefinitions(IEnumerable<ActorCategory> categories) =>
from category in categories
from actor in category.Actors
let lookup = new PropertyLookup(category.GlobalAssignments, actor.Assignments)
select new Actor(
Name: lookup.GetString("class"),
CategoryName: category.Name.Titleize().Singularize(),
Id: actor.Id,
Description: lookup.GetString("title"),
Radius: lookup.GetInt("width"), // This is named poorly in the config file
Height: lookup.GetInt("height")
);

static void WriteActorFile(string basePath, IReadOnlyList<Actor> actors)
{
using var blockStream = File.CreateText(Path.Combine(basePath, "Actor.Generated.cs"));
using var output = new IndentedWriter(blockStream);

output
.WriteHeader(
"Tiledriver.Core.GameInfo.Doom",
new[] { "System.Collections.Generic", "System.CodeDom.Compiler" }
)
.Line($"[GeneratedCode(\"{CurrentLibraryInfo.Name}\", \"{CurrentLibraryInfo.Version}\")]")
.Line($"public sealed partial record Actor")
.OpenParen();

foreach (var actor in actors)
{
output
.Line($"/// <summary>{actor.Description}</summary>")
.Line($"public static readonly Actor {actor.SafeName} = new(")
.IncreaseIndent()
.JoinLines(
",",
new[]
{
$"Id: {actor.Id}",
$"Description: \"{actor.Description}\"",
$"Width: {actor.Width}",
$"Height: {actor.Height}"
}
)
.DecreaseIndent()
.Line(");")
.Line();
}

foreach (var category in actors.GroupBy(c => c.CategoryName))
{
output
.Line($"public static class {category.Key}")
.OpenParen()
.Lines(category.Select(actor => $"public static Actor {actor.SafeName} => Actor.{actor.SafeName};"))
.Line("public static IEnumerable<Actor> GetAll()")
.OpenParen()
.Lines(category.Select(actor => $"yield return {actor.SafeName};"))
.CloseParen()
.CloseParen();
}

output.CloseParen();
}

sealed class PropertyLookup
{
private readonly string _context;
private readonly IReadOnlyDictionary<string, object> _parentAssignments;
private readonly IReadOnlyDictionary<string, object> _actorAssignments;

public PropertyLookup(
IReadOnlyDictionary<string, object> parentAssignments,
IReadOnlyDictionary<string, object> actorAssignments
)
{
_parentAssignments = parentAssignments;
_actorAssignments = actorAssignments;
_context = GetString("class");
}

private object GetValue(string name) =>
_actorAssignments.TryLookupValue(name)
?? _parentAssignments.TryLookupValue(name)
?? throw new ArgumentException($"Could not find {name} for {_context}");

public int GetInt(string name) => (int)GetValue(name);

public string GetString(string name) => (string)GetValue(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
namespace Tiledriver.DataModelGenerator.DoomGameInfo.Parsing;

sealed record ActorCategory(
string Name,
IReadOnlyDictionary<string, object> GlobalAssignments,
IReadOnlyList<ActorDefinition> Actors
string Name,
IReadOnlyDictionary<string, object> GlobalAssignments,
IReadOnlyList<ActorDefinition> Actors
)
{
public ActorCategory(string name, IEnumerable<Assignment> assignments, IEnumerable<ActorDefinition> actors)
: this(name, assignments.ToDictionary(a => a.Name, a => a.Value), actors.ToList()) { }
public ActorCategory(string name, IEnumerable<Assignment> assignments, IEnumerable<ActorDefinition> actors)
: this(name, assignments.ToDictionary(a => a.Name, a => a.Value), actors.ToList()) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace Tiledriver.DataModelGenerator.DoomGameInfo.Parsing;

sealed record ActorDefinition(int Id, IReadOnlyDictionary<string, object> Assignments)
{
public ActorDefinition(int Id, IEnumerable<Assignment> assignments)
: this(Id, assignments.ToDictionary(a => a.Name, a => a.Value)) { }
public ActorDefinition(int Id, IEnumerable<Assignment> assignments)
: this(Id, assignments.ToDictionary(a => a.Name, a => a.Value)) { }
}

0 comments on commit db6c2a6

Please sign in to comment.