rust/src/test/bench/shootout-chameneos-redux.rs

202 lines
5.2 KiB
Rust
Raw Normal View History

// chameneos
extern mod std;
2012-09-05 14:32:05 -05:00
use std::map;
2012-09-10 17:38:28 -05:00
use std::map::HashMap;
2012-09-05 14:32:05 -05:00
use std::sort;
fn print_complements() {
let all = ~[Blue, Red, Yellow];
for vec::each(all) |aa| {
for vec::each(all) |bb| {
io::println(show_color(*aa) + ~" + " + show_color(*bb) +
~" -> " + show_color(transform(*aa, *bb)));
}
}
}
enum color { Red, Yellow, Blue }
type creature_info = { name: uint, color: color };
fn show_color(cc: color) -> ~str {
2012-08-06 14:34:08 -05:00
match (cc) {
2012-08-03 21:59:04 -05:00
Red => {~"red"}
Yellow => {~"yellow"}
Blue => {~"blue"}
}
}
fn show_color_list(set: ~[color]) -> ~str {
let mut out = ~"";
for vec::eachi(set) |_ii, col| {
out += ~" ";
out += show_color(*col);
}
2012-08-01 19:30:05 -05:00
return out;
}
fn show_digit(nn: uint) -> ~str {
2012-08-06 14:34:08 -05:00
match (nn) {
2012-08-03 21:59:04 -05:00
0 => {~"zero"}
1 => {~"one"}
2 => {~"two"}
3 => {~"three"}
4 => {~"four"}
5 => {~"five"}
6 => {~"six"}
7 => {~"seven"}
8 => {~"eight"}
9 => {~"nine"}
_ => {fail ~"expected digits from 0 to 9..."}
}
}
fn show_number(nn: uint) -> ~str {
let mut out = ~"";
let mut num = nn;
let mut dig;
if num == 0 { out = show_digit(0) };
while num != 0 {
dig = num % 10;
num = num / 10;
out = show_digit(dig) + ~" " + out;
}
2012-08-01 19:30:05 -05:00
return out;
}
fn transform(aa: color, bb: color) -> color {
2012-08-06 14:34:08 -05:00
match (aa, bb) {
2012-08-03 21:59:04 -05:00
(Red, Red ) => { Red }
(Red, Yellow) => { Blue }
(Red, Blue ) => { Yellow }
(Yellow, Red ) => { Blue }
(Yellow, Yellow) => { Yellow }
(Yellow, Blue ) => { Red }
(Blue, Red ) => { Yellow }
(Blue, Yellow) => { Red }
(Blue, Blue ) => { Blue }
}
}
fn creature(
name: uint,
color: color,
2012-08-20 14:23:37 -05:00
from_rendezvous: comm::Port<Option<creature_info>>,
2012-08-15 16:10:46 -05:00
to_rendezvous: comm::Chan<creature_info>,
to_rendezvous_log: comm::Chan<~str>
) {
let mut color = color;
let mut creatures_met = 0;
let mut evil_clones_met = 0;
loop {
// ask for a pairing
comm::send(to_rendezvous, {name: name, color: color});
let resp = comm::recv(from_rendezvous);
// log and change, or print and quit
2012-08-06 14:34:08 -05:00
match resp {
2012-08-20 14:23:37 -05:00
option::Some(other_creature) => {
color = transform(color, other_creature.color);
// track some statistics
creatures_met += 1;
if other_creature.name == name {
evil_clones_met += 1;
}
}
2012-08-20 14:23:37 -05:00
option::None => {
// log creatures met and evil clones of self
2012-08-22 19:24:52 -05:00
let report = fmt!("%u", creatures_met) + ~" " +
show_number(evil_clones_met);
comm::send(to_rendezvous_log, report);
break;
}
}
}
}
fn rendezvous(nn: uint, set: ~[color]) {
// these ports will allow us to hear from the creatures
2012-08-27 16:22:25 -05:00
let from_creatures: comm::Port<creature_info> = comm::Port();
let from_creatures_log: comm::Port<~str> = comm::Port();
// these channels will be passed to the creatures so they can talk to us
2012-08-27 16:22:25 -05:00
let to_rendezvous = comm::Chan(from_creatures);
let to_rendezvous_log = comm::Chan(from_creatures_log);
// these channels will allow us to talk to each creature by 'name'/index
2012-08-20 14:23:37 -05:00
let to_creature: ~[comm::Chan<Option<creature_info>>] =
vec::mapi(set, |ii, col| {
// create each creature as a listener with a port, and
// give us a channel to talk to each
let ii = ii;
let col = *col;
do task::spawn_listener |from_rendezvous, move ii, move col| {
creature(ii, col, from_rendezvous, to_rendezvous,
to_rendezvous_log);
}
});
let mut creatures_met = 0;
// set up meetings...
for nn.times {
let fst_creature: creature_info = comm::recv(from_creatures);
let snd_creature: creature_info = comm::recv(from_creatures);
creatures_met += 2;
2012-08-20 14:23:37 -05:00
comm::send(to_creature[fst_creature.name], Some(snd_creature));
comm::send(to_creature[snd_creature.name], Some(fst_creature));
}
// tell each creature to stop
for vec::eachi(to_creature) |_ii, to_one| {
comm::send(*to_one, None);
}
// save each creature's meeting stats
let mut report = ~[];
for vec::each(to_creature) |_to_one| {
report.push(comm::recv(from_creatures_log));
}
// print each color in the set
io::println(show_color_list(set));
// print each creature's stats
for vec::each(report) |rep| {
io::println(*rep);
}
// print the total number of creatures met
io::println(show_number(creatures_met));
}
fn main(++args: ~[~str]) {
let args = if os::getenv(~"RUST_BENCH").is_some() {
~[~"", ~"200000"]
} else if args.len() <= 1u {
~[~"", ~"600"]
} else {
args
};
let nn = uint::from_str(args[1]).get();
print_complements();
io::println(~"");
rendezvous(nn, ~[Blue, Red, Yellow]);
io::println(~"");
rendezvous(nn,
~[Blue, Red, Yellow, Red, Yellow, Blue, Red, Yellow, Red, Blue]);
}