Skip to content

Commit

Permalink
[C#] Finish Towers benchmark
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Marr <[email protected]>
  • Loading branch information
smarr committed Jul 16, 2022
1 parent 0d1aaf3 commit bea96fd
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions benchmarks/CSharp/Towers.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace Benchmarks;

public class TowersDisk
public sealed class TowersDisk
{
public readonly int Size;
public TowersDisk? Next;
public int Size { get; }
public TowersDisk? Next { get; set; }

public TowersDisk(int size)
{
Expand All @@ -14,38 +14,38 @@ public TowersDisk(int size)

public class Towers : Benchmark
{
public TowersDisk?[] Piles = Array.Empty<TowersDisk>();
public int MovesDone;
private TowersDisk[] piles = Array.Empty<TowersDisk>();
private int movesDone;

private void PushDisk(TowersDisk disk, int pile)
{
TowersDisk? top = Piles[pile];
TowersDisk? top = piles[pile];
if (!(top == null) && (disk.Size >= top.Size))
{
throw new InvalidOperationException("Cannot put a big disk on a smaller one");
}

disk.Next = top;
Piles[pile] = disk;
piles[pile] = disk;
}

private TowersDisk PopDiskFrom(int pile)
{
TowersDisk? top = Piles[pile];
TowersDisk? top = piles[pile];
if (top == null)
{
throw new InvalidOperationException("Attempting to remove a disk from an empty pile");
}

Piles[pile] = top.Next;
piles[pile] = top.Next;
top.Next = null;
return top;
}

private void MoveTopDisk(int fromPile, int toPile)
{
PushDisk(PopDiskFrom(fromPile), toPile);
MovesDone++;
movesDone++;
}

private void BuildTowerAt(int pile, int disks)
Expand Down Expand Up @@ -73,11 +73,11 @@ private void MoveDisks(int disks, int fromPile, int toPile)

public override object Execute()
{
Piles = new TowersDisk[3];
piles = new TowersDisk[3];
BuildTowerAt(0, 13);
MovesDone = 0;
movesDone = 0;
MoveDisks(13, 0, 1);
return MovesDone;
return movesDone;
}

public override bool VerifyResult(object result)
Expand Down

0 comments on commit bea96fd

Please sign in to comment.