Skip to content

Commit

Permalink
Added chunked map that's not faster
Browse files Browse the repository at this point in the history
  • Loading branch information
devedse committed Nov 26, 2023
1 parent e5873c4 commit 680178b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
4 changes: 2 additions & 2 deletions DeveMazeGeneratorCore.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,15 @@ public static void TestWithSave()
int size = 128;
var fastestElapsed = TimeSpan.MaxValue;

var alg = new AlgorithmBacktrack2Deluxe2WithBorder_AsByte();
var alg = new AlgorithmBacktrack2Deluxe2_AsByte();

Console.WriteLine($"Generating maze using {alg.GetType().Name}...");

int seed = 1337;

var w = Stopwatch.StartNew();

var innerMapFactory = new InnerMapFactory<BitArreintjeFastInnerMap>();
var innerMapFactory = new InnerMapFactory<BitArreintjeFastChunkedInnerMap>();
var randomFactory = new RandomFactory<XorShiftRandom>();

var actionThing = new NoAction();
Expand Down
1 change: 1 addition & 0 deletions DeveMazeGeneratorCore/Factories/InnerMapFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public T Create(int desiredWidth, int desiredHeight)
{
var typeSwitcher = new TypeSwitch<InnerMap>()
.Case(() => new BitArreintjeFastInnerMap(desiredWidth, desiredHeight))
.Case(() => new BitArreintjeFastChunkedInnerMap(desiredWidth, desiredHeight))

Check failure on line 15 in DeveMazeGeneratorCore/Factories/InnerMapFactory.cs

View workflow job for this annotation

GitHub Actions / codeqlanalyze (csharp)

The type or namespace name 'BitArreintjeFastChunkedInnerMap' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 15 in DeveMazeGeneratorCore/Factories/InnerMapFactory.cs

View workflow job for this annotation

GitHub Actions / codeqlanalyze (csharp)

The type or namespace name 'BitArreintjeFastChunkedInnerMap' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 15 in DeveMazeGeneratorCore/Factories/InnerMapFactory.cs

View workflow job for this annotation

GitHub Actions / codeqlanalyze (csharp)

The type or namespace name 'BitArreintjeFastChunkedInnerMap' could not be found (are you missing a using directive or an assembly reference?)
.Case(() => new BitArreintjeFastHilbertInnerMap(desiredWidth, desiredHeight))
.Case(() => new BoolInnerMap(desiredWidth, desiredHeight));

Expand Down
79 changes: 79 additions & 0 deletions DeveMazeGeneratorCore/InnerMaps/BitArreintjeFastChunkedInnerMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using DeveMazeGeneratorCore.InnerMaps.InnerStuff;
using System.Runtime.CompilerServices;

namespace DeveMazeGeneratorCore.InnerMaps
{
public class BitArreintjeFastChunkedInnerMap : InnerMap
{
private BitArreintjeFastInnerMapArray[] _innerData;

public const int ChunkSize = 8;
private int _chunksPerRow;

public BitArreintjeFastChunkedInnerMap(int width, int height)
: base(width, height)
{
_chunksPerRow = width / ChunkSize;
var chunkCount = _chunksPerRow * (height / ChunkSize);

_innerData = new BitArreintjeFastInnerMapArray[chunkCount];
for (int i = 0; i < chunkCount; i++)
{
_innerData[i] = new BitArreintjeFastInnerMapArray(ChunkSize * ChunkSize);
}
}

public override void FillMap(bool state)
{
for (int i = 0; i < _innerData.Length; i++)
{
_innerData[i].FillMap(state);
}
}

public override InnerMap Clone()
{
var innerMapTarget = new BitArreintjeFastChunkedInnerMap(Width, Height);
for (int i = 0; i < _innerData.Length; i++)
{
innerMapTarget._innerData[i] = _innerData[i].Clone();
}
return innerMapTarget;
}

public override bool this[int x, int y]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
int chunkX = x / ChunkSize;
int chunkY = y / ChunkSize;

var chunkNumber = chunkY * _chunksPerRow + chunkX;

int localX = x % ChunkSize;
int localY = y % ChunkSize;

var positionInChunk = localY * ChunkSize + localX;


return _innerData[chunkNumber][positionInChunk];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
int chunkX = x / ChunkSize;
int chunkY = y / ChunkSize;

var chunkNumber = chunkY * _chunksPerRow + chunkX;

int localX = x % ChunkSize;
int localY = y % ChunkSize;

var positionInChunk = localY * ChunkSize + localX;

_innerData[chunkNumber][positionInChunk] = value;
}
}
}
}

0 comments on commit 680178b

Please sign in to comment.