public void Run <TView, TState>( TState state, int dimensions, GameOfLifeOptions options = null) where TView : struct, IView where TState : IEnumerable <Coordinate> { // setup var maxIters = options?.MaxIterations; var token = options?.CancellationToken ?? CancellationToken.None; TView view = default; var memory = new bool[dimensions, dimensions]; foreach (var liveCell in state) { memory[liveCell.Y, liveCell.X] = true; } // run // wanna see some weird shit to allow the compiler/jitter to inline? // let's hope it actually works lol if (maxIters.HasValue) { if (options?.CancellationToken != null) { Loop <IterCheck, CancellationTokenCheck, TView>(memory, view, maxIters.Value, token); } else { Loop <IterCheck, AlwaysTrueCancellationTokenCheck, TView>(memory, view, maxIters.Value, token); } } else { if (options?.CancellationToken != null) { Loop <AlwaysRun, CancellationTokenCheck, TView>(memory, view, cancellationToken: token); } else { Loop <AlwaysRun, AlwaysTrueCancellationTokenCheck, TView>(memory, view); } } }
public void Run <TConwayGrid, TView>( TConwayGrid grid, // todo: make this a regular interface. this way will do no good. GameOfLifeOptions options = null) where TConwayGrid : IParallelConwayGrid where TView : struct, IView { var maxIters = options?.MaxIterations; var token = options?.CancellationToken ?? CancellationToken.None; TView view = default; if (options.Dimensions.HasValue) { view.Dimensions = options.Dimensions.Value; } // wanna see some weird shit to allow the compiler/jitter to inline? // let's hope it actually works lol if (maxIters.HasValue) { if (options?.CancellationToken != null) { Loop <IterCheck, CancellationTokenCheck, TConwayGrid, TView>(grid, view, maxIters.Value, token); } else { Loop <IterCheck, AlwaysTrueCancellationTokenCheck, TConwayGrid, TView>(grid, view, maxIters.Value, token); } } else { if (options?.CancellationToken != null) { Loop <AlwaysRun, CancellationTokenCheck, TConwayGrid, TView>(grid, view, cancellationToken: token); } else { Loop <AlwaysRun, AlwaysTrueCancellationTokenCheck, TConwayGrid, TView>(grid, view); } } }