Commit c18f5b34 authored by HankG's avatar HankG

Added Chameneosredux straight port

parent f5ebff31
/* The Computer Language Benchmarks Game
http://shootout.alioth.debian.org/
Based on Java version (#3) by Kirill Ilyukhin
contributed by Hank Grabowski
*/
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Exchanger
import java.util.concurrent.atomic.AtomicInteger
object chameneosredux {
internal var meetingPlace = MeetingPlace()
internal var latch = CountDownLatch(0)
internal var meetingsLeft = AtomicInteger()
@Throws(InterruptedException::class)
@JvmStatic
fun main(args: Array<String>) {
var N = 6000000
if (args.size > 0) {
try {
N = Integer.parseInt(args[0])
} catch (ignore: NumberFormatException) {
}
}
for (color1 in Color.colors) {
for (color2 in Color.colors) {
println(color1.toString() + " + " + color2 + " -> " + Color.complement(color1, color2))
}
}
println()
run(N, Color.blue, Color.red, Color.yellow)
run(N, Color.blue, Color.red, Color.yellow, Color.red, Color.yellow, Color.blue, Color.red, Color.yellow, Color.red, Color.blue)
}
@Throws(InterruptedException::class)
private fun run(N: Int, vararg colors: Color) {
meetingPlace = MeetingPlace()
latch = CountDownLatch(2 * N)
meetingsLeft = AtomicInteger(2 * N)
val creatures = arrayOfNulls<Creature>(colors.size)
for (i in colors.indices) {
print(" " + colors[i])
creatures[i] = Creature(colors[i])
}
println()
for (creature in creatures) {
creature?.start()
}
latch.await()
for (creature in creatures) {
creature?.interrupt()
}
for (creature in creatures) {
creature?.join()
}
var m = 0
for (creature in creatures) {
println("" + creature!!.meetings + spell(creature!!.meetingsWithSelf))
m += creature!!.meetings
}
println(spell(m))
println()
}
private val DIGITS = arrayOf(" zero", " one", " two", " three", " four", " five", " six", " seven", " eight", " nine")
private fun spell(number: Int): String {
var number = number
if (number == 0) {
return DIGITS[0]
}
var s = ""
while (number > 0) {
s = DIGITS[number % 10] + s
number /= 10
}
return s
}
internal class Creature(var color: Color) : Thread() {
var name: Int
var meetings = 0
var meetingsWithSelf = 0
init {
this.name = ++nameCounter
}
private fun createAgent(): Agent {
return Agent(this)
}
override fun run() {
while (true) {
try {
val agent = meetingPlace.enter(this.createAgent()) ?: return
if (agent.name == this.name) {
meetingsWithSelf++
}
color = Color.complement(this.color, agent.color)
meetings++
} catch (e: InterruptedException) {
break
}
}
}
companion object {
private var nameCounter: Int = 0
}
}
internal class MeetingPlace {
private val room: Exchanger<Agent>
init {
room = Exchanger()
}
@Throws(InterruptedException::class)
fun enter(visitor: Agent): Agent? {
if (meetingsLeft.get() < 0) {
return null
}
val agent = room.exchange(visitor)
latch.countDown()
return if (meetingsLeft.decrementAndGet() < 0) {
null
} else agent
}
}
internal class Agent(creature: Creature) {
val name: Int
val color: Color
init {
this.name = creature.name
this.color = creature.color
}
}
internal enum class Color {
blue,
red,
yellow,
none;
companion object {
val colors = arrayOf(Color.blue, Color.red, Color.yellow)
fun complement(color1: Color, color2: Color): Color {
when (color1) {
blue -> {
when (color2) {
blue -> return blue
red -> return yellow
yellow -> return red
}
}
red -> {
when (color2) {
blue -> return yellow
red -> return red
yellow -> return blue
}
}
yellow -> when (color2) {
blue -> return red
red -> return blue
yellow -> return yellow
}
}
return none
}
}
}
}
......@@ -68,6 +68,7 @@ onlydirs =
nbody
regexdna
binarytrees
chameneosredux
......
Revised BSD license
This is a specific instance of the Open Source Initiative (OSI) BSD license template
http://www.opensource.org/licenses/bsd-license.php
Copyright © 2004-2008 Brent Fulgham, 2005-2017 Isaac Gouy
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of "The Computer Language Benchmarks Game" nor the name of "The Computer Language Shootout Benchmarks" nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/**
* The Computer Language Benchmarks Game
* http://benchmarksgame.alioth.debian.org/
*
* based on Jarkko Miettinen's Java program
* contributed by Tristan Dupont
* *reset*
*/
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class binarytrees {
private static final int MIN_DEPTH = 4;
private static final ExecutorService EXECUTOR_SERVICE =
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
public static void main(final String[] args) throws Exception {
int n = 0;
if (0 < args.length) {
n = Integer.parseInt(args[0]);
}
final int maxDepth = n < (MIN_DEPTH + 2) ? MIN_DEPTH + 2 : n;
final int stretchDepth = maxDepth + 1;
System.out.println("stretch tree of depth " + stretchDepth + "\t check: "
+ bottomUpTree( stretchDepth).itemCheck());
final TreeNode longLivedTree = bottomUpTree(maxDepth);
final String[] results = new String[(maxDepth - MIN_DEPTH) / 2 + 1];
for (int d = MIN_DEPTH; d <= maxDepth; d += 2) {
final int depth = d;
EXECUTOR_SERVICE.execute(() -> {
int check = 0;
final int iterations = 1 << (maxDepth - depth + MIN_DEPTH);
for (int i = 1; i <= iterations; ++i) {
final TreeNode treeNode1 = bottomUpTree(depth);
check += treeNode1.itemCheck();
}
results[(depth - MIN_DEPTH) / 2] =
iterations + "\t trees of depth " + depth + "\t check: " + check;
});
}
EXECUTOR_SERVICE.shutdown();
EXECUTOR_SERVICE.awaitTermination(120L, TimeUnit.SECONDS);
for (final String str : results) {
System.out.println(str);
}
System.out.println("long lived tree of depth " + maxDepth +
"\t check: " + longLivedTree.itemCheck());
}
private static TreeNode bottomUpTree(final int depth) {
if (0 < depth) {
return new TreeNode(bottomUpTree(depth - 1), bottomUpTree(depth - 1));
}
return new TreeNode();
}
private static final class TreeNode {
private final TreeNode left;
private final TreeNode right;
private TreeNode(final TreeNode left, final TreeNode right) {
this.left = left;
this.right = right;
}
private TreeNode() {
this(null, null);
}
private int itemCheck() {
// if necessary deallocate here
if (null == left) {
return 1;
}
return 1 + left.itemCheck() + right.itemCheck();
}
}
}
/**
* The Computer Language Benchmarks Game
* http://benchmarksgame.alioth.debian.org/
*
* based on Tristan Dupont version which was
* based on Jarkko Miettinen's Java program
* contributed by Hank Grabowski
* *reset*
*/
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
object binarytrees {
private val MIN_DEPTH = 4
private val EXECUTOR_SERVICE = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())
@Throws(Exception::class)
@JvmStatic
fun main(args: Array<String>) {
var n = 0
if (args.isNotEmpty()) {
n = Integer.parseInt(args[0])
}
val maxDepth = if (n < MIN_DEPTH + 2) MIN_DEPTH + 2 else n
val stretchDepth = maxDepth + 1
println("stretch tree of depth " + stretchDepth + "\t check: "
+ bottomUpTree(stretchDepth).itemCheck())
val longLivedTree = bottomUpTree(maxDepth)
val results = arrayOfNulls<String>((maxDepth - MIN_DEPTH) / 2 + 1)
var d = MIN_DEPTH
while (d <= maxDepth) {
val depth = d
EXECUTOR_SERVICE.execute {
var check = 0
val iterations = 1 shl maxDepth - depth + MIN_DEPTH
for (i in 1..iterations) {
val treeNode1 = bottomUpTree(depth)
check += treeNode1.itemCheck()
}
results[(depth - MIN_DEPTH) / 2] = iterations.toString() + "\t trees of depth " + depth + "\t check: " + check
}
d += 2
}
EXECUTOR_SERVICE.shutdown()
EXECUTOR_SERVICE.awaitTermination(120L, TimeUnit.SECONDS)
for (str in results) {
println(str)
}
println("long lived tree of depth " + maxDepth +
"\t check: " + longLivedTree.itemCheck())
}
private fun bottomUpTree(depth: Int): TreeNode {
return if (0 < depth) {
TreeNode(bottomUpTree(depth - 1), bottomUpTree(depth - 1))
} else TreeNode()
}
private class TreeNode (private val left: TreeNode? = null, private val right: TreeNode? = null) {
fun itemCheck(): Int {
// if necessary deallocate here
return if (null == left) {
1
} else 1 + left.itemCheck() + right!!.itemCheck()
}
}
}
/* The Computer Language Benchmarks Game
http://benchmarksgame.alioth.debian.org/
contributed by Kirill Ilyukhin
*/
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Exchanger;
import java.util.concurrent.atomic.AtomicInteger;
public class chameneosredux {
static MeetingPlace meetingPlace;
static CountDownLatch latch;
static AtomicInteger meetingsLeft;
public static void main(String[] args) throws InterruptedException {
int N = 6_000_000;
if (args.length > 0) {
try {
N = Integer.parseInt(args[0]);
} catch (NumberFormatException ignore) {
}
}
for (Color color1 : Color.colors) {
for (Color color2 : Color.colors) {
System.out.println(color1 + " + " + color2 + " -> " + Color.complement(color1, color2));
}
}
System.out.println();
run(N, Color.blue, Color.red, Color.yellow);
run(N, Color.blue, Color.red, Color.yellow, Color.red, Color.yellow, Color.blue, Color.red, Color.yellow, Color.red, Color.blue);
}
private static void run(final int N, final Color... colors) throws InterruptedException {
meetingPlace = new MeetingPlace();
latch = new CountDownLatch(2*N);
meetingsLeft = new AtomicInteger(2*N);
Creature[] creatures = new Creature[colors.length];
for (int i=0; i < colors.length; i++) {
System.out.print(" " + colors[i]);
creatures[i] = new Creature(colors[i]);
}
System.out.println();
for (Creature creature : creatures) {
creature.start();
}
latch.await();
for (Creature creature : creatures) {
creature.interrupt();
}
for (Creature creature : creatures) {
creature.join();
}
int m = 0;
for (Creature creature : creatures) {
System.out.println("" + creature.meetings + spell(creature.meetingsWithSelf));
m += creature.meetings;
}
System.out.println(spell(m));
System.out.println();
}
private static final String[] DIGITS = {
" zero",
" one",
" two",
" three",
" four",
" five",
" six",
" seven",
" eight",
" nine"
};
static String spell(int number) {
if (number == 0) {
return DIGITS[0];
}
String s = "";
while (number > 0) {
s = DIGITS[number % 10] + s;
number /= 10;
}
return s;
}
static class Creature extends Thread {
private static int nameCounter;
private Color color;
private final int name;
int meetings = 0;
int meetingsWithSelf = 0;
Creature(Color color) {
this.name = ++nameCounter;
this.color = color;
}
private Agent createAgent() {
return new Agent(this);
}
@Override
public void run() {
while (true) {
try {
Agent agent = meetingPlace.enter(this.createAgent());
if (agent == null) {
return;
}
if (agent.name == this.name) {
meetingsWithSelf++;
}
color = Color.complement(this.color, agent.color);
meetings++;
} catch (InterruptedException e) {
break;
}
}
}
}
static class MeetingPlace {
private final Exchanger<Agent> room;
MeetingPlace() {
room = new Exchanger<>();
}
public Agent enter(Agent visitor) throws InterruptedException {
if (meetingsLeft.get() < 0) {
return null;
}
Agent agent = room.exchange(visitor);
latch.countDown();
if (meetingsLeft.decrementAndGet() < 0) {
return null;
}
return agent;
}
}
static class Agent {
final int name;
final Color color;
Agent(Creature creature) {
this.name = creature.name;
this.color = creature.color;
}
}
enum Color {
blue,
red,
yellow;
static final Color[] colors = {Color.blue, Color.red, Color.yellow};
public static Color complement(final Color color1, final Color color2) {
switch (color1) {
case blue:
switch (color2) {
case blue: return blue;
case red: return yellow;
case yellow: return red;
}
case red:
switch (color2) {
case blue: return yellow;
case red: return red;
case yellow: return blue;
}
case yellow:
switch (color2) {
case blue: return red;
case red: return blue;
case yellow: return yellow;
}
}
return null;
}
}
}
/* The Computer Language Benchmarks Game
http://shootout.alioth.debian.org/
Based on Java version (#3) by Kirill Ilyukhin
contributed by Hank Grabowski
*/
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Exchanger
import java.util.concurrent.atomic.AtomicInteger
object chameneosredux {
internal var meetingPlace = MeetingPlace()
internal var latch = CountDownLatch(0)
internal var meetingsLeft = AtomicInteger()
@Throws(InterruptedException::class)
@JvmStatic
fun main(args: Array<String>) {
var N = 6000000
if (args.size > 0) {
try {
N = Integer.parseInt(args[0])
} catch (ignore: NumberFormatException) {
}
}
for (color1 in Color.colors) {
for (color2 in Color.colors) {
println(color1.toString() + " + " + color2 + " -> " + Color.complement(color1, color2))
}
}
println()
run(N, Color.blue, Color.red, Color.yellow)
run(N, Color.blue, Color.red, Color.yellow, Color.red, Color.yellow, Color.blue, Color.red, Color.yellow, Color.red, Color.blue)
}
@Throws(InterruptedException::class)
private fun run(N: Int, vararg colors: Color) {
meetingPlace = MeetingPlace()
latch = CountDownLatch(2 * N)
meetingsLeft = AtomicInteger(2 * N)
val creatures = arrayOfNulls<Creature>(colors.size)
for (i in colors.indices) {
print(" " + colors[i])
creatures[i] = Creature(colors[i])
}
println()
for (creature in creatures) {
creature?.start()
}
latch.await()
for (creature in creatures) {
creature?.interrupt()
}
for (creature in creatures) {
creature?.join()
}
var m = 0
for (creature in creatures) {
println("" + creature!!.meetings + spell(creature!!.meetingsWithSelf))
m += creature!!.meetings
}
println(spell(m))
println()
}
private val DIGITS = arrayOf(" zero", " one", " two", " three", " four", " five", " six", " seven", " eight", " nine")
private fun spell(number: Int): String {
var number = number
if (number == 0) {
return DIGITS[0]
}
var s = ""
while (number > 0) {
s = DIGITS[number % 10] + s
number /= 10
}
return s
}
internal class Creature(var color: Color) : Thread() {
var name: Int
var meetings = 0
var meetingsWithSelf = 0
init {
this.name = ++nameCounter
}
private fun createAgent(): Agent {
return Agent(this)
}
override fun run() {
while (true) {
try {
val agent = meetingPlace.enter(this.createAgent()) ?: return
if (agent.name == this.name) {
meetingsWithSelf++
}
color = Color.complement(this.color, agent.color)
meetings++
} catch (e: InterruptedException) {
break
}
}
}
companion object {
private var nameCounter: Int = 0
}
}
internal class MeetingPlace {
private val room: Exchanger<Agent>
init {
room = Exchanger()
}
@Throws(InterruptedException::class)
fun enter(visitor: Agent): Agent? {
if (meetingsLeft.get() < 0) {
return null
}
val agent = room.exchange(visitor)
latch.countDown()
return if (meetingsLeft.decrementAndGet() < 0) {
null
} else agent
}
}
internal class Agent(creature: Creature) {
val name: Int
val color: Color
init {
this.name = creature.name
this.color = creature.color
}
}
internal enum class Color {
blue,
red,