Skip to content

Commit

Permalink
Merge pull request #39 from IR0NSIGHT/benchmark/queens
Browse files Browse the repository at this point in the history
complete effekt + js benchmark: queens
  • Loading branch information
IR0NSIGHT authored Feb 12, 2024
2 parents 1c11043 + ce56bac commit 32a03cf
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 0 deletions.
98 changes: 98 additions & 0 deletions src/effekt/benchmark/queens.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import src/effekt/benchmark
import src/effekt/cliRunner
import mutable/array

interface Break[T] {
def returnValue(result: T): Nothing
}

def breakableLoop[T](from: Int, to: Int, finally: T){ fnc: Int => Unit / Break[T] } : T = {
if (from == to) {
finally
} else {
try {
fnc(from);
breakableLoop(from+1, to, finally){ fnc };
} with Break[T] {
def returnValue(value) = value
}
}
}

def Queens() = {
var freeMaxs = emptyArray[Boolean]();
var freeRows = emptyArray[Boolean]();
var freeMins = emptyArray[Boolean]();
var queenRows = emptyArray[Int]();

def getRowColumn(r: Int, c: Int) = {
freeRows.unsafeGet(r) && freeMaxs.unsafeGet(c + r) && freeMins.unsafeGet(c - r + 7);
}

def setRowColumn(r: Int, c: Int, v: Boolean) = {
put(freeRows,r, v);
put(freeMaxs,c + r, v);
put(freeMins,c - r + 7, v);
}

def placeQueen(c: Int): Boolean = {
def iterate(r: Int): Unit / Break[Boolean] = {
if (getRowColumn(r, c)) {
put(queenRows,r, c);
setRowColumn(r, c, false);

if (c == 7) {
do returnValue(true);
}

if (placeQueen(c + 1)) {
do returnValue(true);
}
setRowColumn(r, c, true);
}
}
breakableLoop[Boolean](0, 8, false){ iterate }
}

def queens() = {
freeRows = fill(8,true);
freeMaxs = fill(16,true);
freeMins = fill(16,true);
queenRows = fill(8,-1);

placeQueen(0);
}

def benchmark() = {
var result = true;
each(0,10){ i =>
result = result && queens();
}
return result;
}

def verifyResult(result: Boolean) = {
result
}

innerBenchmarkLoop(1){benchmark}{verifyResult}
}

def miniRun() = {
Queens();
}

def normalRun() = {
Queens();
}

def main() = {
//def it(i: Int): Unit / Break[Int] = {
// if (i == 12) {
// do returnValue(4);
// }
//}
//println( breakableLoop(0,13,-1){ it })

println(runFromCli{ miniRun }{ normalRun })
}
1 change: 1 addition & 0 deletions src/javascript/compare/comparator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const commands = [
["bounce","src/effekt/benchmark/bounce.effekt","node src/javascript/bounce.js"],
["sieve","src/effekt/benchmark/sieve.effekt", "node src/javascript/Sieve.js"],
["storage","src/effekt/benchmark/storage.effekt","node src/javascript/Storage.js"],
["queens","src/effekt/benchmark/queens.effekt","node src/javascript/queens.js"]
// Add more commands as needed
];

Expand Down
96 changes: 96 additions & 0 deletions src/javascript/queens.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// @ts-check
// This code is derived from the SOM benchmarks, see AUTHORS.md file.
//
// Copyright (c) 2015-2016 Stefan Marr <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the 'Software'), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

const { runFromCli } = require("./cliRunner");

// THE SOFTWARE.
class Queens {
constructor() {
this.freeMaxs = null;
this.freeRows = null;
this.freeMins = null;
this.queenRows = null;
}

benchmark() {
let result = true;
for (let i = 0; i < 10; i += 1) {
result = result && this.queens();
}
return result;
}

verifyResult(result) {
return result;
}

queens() {
this.freeRows = new Array(8).fill(true);
this.freeMaxs = new Array(16).fill(true);
this.freeMins = new Array(16).fill(true);
this.queenRows = new Array(8).fill(-1);

return this.placeQueen(0);
}

placeQueen(c) {
for (let r = 0; r < 8; r += 1) {
if (this.getRowColumn(r, c)) {
this.queenRows[r] = c;
this.setRowColumn(r, c, false);

if (c === 7) {
return true;
}

if (this.placeQueen(c + 1)) {
return true;
}
this.setRowColumn(r, c, true);
}
}
return false;
}

getRowColumn(r, c) {
return this.freeRows[r] && this.freeMaxs[c + r] && this.freeMins[c - r + 7];
}

setRowColumn(r, c, v) {
this.freeRows[r] = v;
this.freeMaxs[c + r] = v;
this.freeMins[c - r + 7] = v;
}
}

const miniRun = () => {
new Queens().benchmark()
}

const normalRun = () => {
new Queens().benchmark()
}

const main = () => {
console.log(runFromCli(miniRun, normalRun))
}
main()

0 comments on commit 32a03cf

Please sign in to comment.