From bea96fde5c0d0f9675a6035a0fa8f5c15e360589 Mon Sep 17 00:00:00 2001 From: Stefan Marr Date: Sat, 16 Jul 2022 18:19:59 +0100 Subject: [PATCH] [C#] Finish Towers benchmark Signed-off-by: Stefan Marr --- benchmarks/CSharp/Towers.cs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/benchmarks/CSharp/Towers.cs b/benchmarks/CSharp/Towers.cs index 862051bc..e7d80943 100644 --- a/benchmarks/CSharp/Towers.cs +++ b/benchmarks/CSharp/Towers.cs @@ -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) { @@ -14,30 +14,30 @@ public TowersDisk(int size) public class Towers : Benchmark { - public TowersDisk?[] Piles = Array.Empty(); - public int MovesDone; + private TowersDisk[] piles = Array.Empty(); + 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; } @@ -45,7 +45,7 @@ private TowersDisk PopDiskFrom(int pile) private void MoveTopDisk(int fromPile, int toPile) { PushDisk(PopDiskFrom(fromPile), toPile); - MovesDone++; + movesDone++; } private void BuildTowerAt(int pile, int disks) @@ -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)