Skip to content

Commit

Permalink
Towers: refactor to make Disk, StackOfDisk and pile more understandable
Browse files Browse the repository at this point in the history
  • Loading branch information
IR0NSIGHT committed Feb 12, 2024
1 parent 63fb8cf commit 0eed1d9
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/effekt/benchmark/towers.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,40 @@ import immutable/list
import src/effekt/cliRunner

//each list represents one smallerDisk.
type Disk = List[Int]
type SingleDisk = Int
type StackOfDisks = List[SingleDisk]

def Towers() = {

var piles: Array[Disk] = emptyArray();
var piles: Array[StackOfDisks] = emptyArray();
var movesDone: Int = 0;

def pushDisk(piles: Array[Disk], smallerDisk: Disk, pileIdx: Int): Disk = {
val currentTopDisk: Disk = piles.unsafeGet(pileIdx);
def pushDisk(piles: Array[StackOfDisks], smallerDisk: SingleDisk, pileIdx: Int): StackOfDisks = {
val currentTopDisk: StackOfDisks = piles.unsafeGet(pileIdx);
(smallerDisk, currentTopDisk) match {
case (Cons(smallerSize,_), Cons(largerSize,_)) =>
case (smallerSize, Cons(largerSize,_)) =>
if (smallerSize >= largerSize) {
panic("Cannot put a big smallerDisk on a smaller one:"++show(piles));
}
}

//push current top disk one down, put smaller on top
val replacer = smallerDisk match { //smallerDisk.next = currentTopDisk
case Cons(smallerSize, next) =>
case smallerSize =>
Cons(smallerSize, currentTopDisk)
case Nil() => panic("tried putting nothing on top of stack")
}
put(piles,pileIdx, replacer);
return currentTopDisk
}

def popDiskFrom(pileIdx: Int): Disk = {
def popDiskFrom(pileIdx: Int): SingleDisk = {
val currentTopDisk = piles.unsafeGet(pileIdx);

currentTopDisk match {
case Nil() => panic("Attempting to remove a smallerDisk from an empty pileIdx");
case Cons(size, next) =>
put(piles, pileIdx, next)
return Cons(size, Nil())
return size
}
}

Expand All @@ -49,7 +49,7 @@ def Towers() = {
def buildTowerAt(pileIdx: Int, disks: Int) = {
var i = disks;
while (i >= 0) {
pushDisk(piles, Cons(i,Nil()), pileIdx);
pushDisk(piles, i, pileIdx);
i = i -1;
}
}
Expand All @@ -67,9 +67,9 @@ def Towers() = {

def benchmark(): Int = {
piles = emptyArray(3)
put[Disk](piles,0,Nil())
put[Disk](piles,1,Nil())
put[Disk](piles,2,Nil())
put[StackOfDisks](piles,0,Nil())
put[StackOfDisks](piles,1,Nil())
put[StackOfDisks](piles,2,Nil())

buildTowerAt(0, 13);

Expand All @@ -85,7 +85,7 @@ def Towers() = {
innerBenchmarkLoop(1){benchmark}{verifyResult}
}

def diskSize(d: Disk): Int = {
def diskSize(d: StackOfDisks): Int = {
d match {
case Cons(s,_) => s
case Nil() => -1
Expand Down

0 comments on commit 0eed1d9

Please sign in to comment.