2014-09-08 01:43:21 -05:00
|
|
|
// The Computer Language Benchmarks Game
|
|
|
|
// http://benchmarksgame.alioth.debian.org/
|
2012-12-10 19:32:48 -06:00
|
|
|
//
|
2014-09-08 01:43:21 -05:00
|
|
|
// contributed by the Rust Project Developers
|
|
|
|
|
|
|
|
// Copyright (c) 2012-2014 The Rust Project Developers
|
|
|
|
//
|
|
|
|
// 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.
|
2012-12-10 19:32:48 -06:00
|
|
|
|
2014-05-11 13:14:14 -05:00
|
|
|
// no-pretty-expanded
|
|
|
|
|
2014-11-06 02:05:53 -06:00
|
|
|
use self::Color::{Red, Yellow, Blue};
|
2015-01-02 11:24:56 -06:00
|
|
|
use std::sync::mpsc::{channel, Sender, Receiver};
|
2014-04-21 16:06:59 -05:00
|
|
|
use std::fmt;
|
2015-02-17 17:10:25 -06:00
|
|
|
use std::thread;
|
2014-04-21 16:06:59 -05:00
|
|
|
|
2012-07-06 20:20:36 -05:00
|
|
|
fn print_complements() {
|
2013-06-17 15:37:11 -05:00
|
|
|
let all = [Blue, Red, Yellow];
|
2015-01-31 11:20:46 -06:00
|
|
|
for aa in &all {
|
|
|
|
for bb in &all {
|
2015-01-06 18:16:35 -06:00
|
|
|
println!("{:?} + {:?} -> {:?}", *aa, *bb, transform(*aa, *bb));
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 08:38:27 -05:00
|
|
|
#[derive(Copy, Clone)]
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
enum Color {
|
|
|
|
Red,
|
|
|
|
Yellow,
|
|
|
|
Blue,
|
|
|
|
}
|
|
|
|
|
2015-01-28 07:34:18 -06:00
|
|
|
impl fmt::Debug for Color {
|
2014-04-21 16:06:59 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let str = match *self {
|
|
|
|
Red => "red",
|
|
|
|
Yellow => "yellow",
|
|
|
|
Blue => "blue",
|
|
|
|
};
|
2014-05-11 13:14:14 -05:00
|
|
|
write!(f, "{}", str)
|
2014-04-21 16:06:59 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-06 20:20:36 -05:00
|
|
|
|
2015-03-30 08:38:27 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2013-01-28 20:55:44 -06:00
|
|
|
struct CreatureInfo {
|
2015-03-25 19:06:52 -05:00
|
|
|
name: usize,
|
2014-04-21 16:06:59 -05:00
|
|
|
color: Color
|
2013-01-28 20:55:44 -06:00
|
|
|
}
|
2012-07-06 20:20:36 -05:00
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
fn show_color_list(set: Vec<Color>) -> String {
|
|
|
|
let mut out = String::new();
|
2015-01-31 11:20:46 -06:00
|
|
|
for col in &set {
|
2014-10-10 18:46:59 -05:00
|
|
|
out.push(' ');
|
2015-02-01 20:53:25 -06:00
|
|
|
out.push_str(&format!("{:?}", col));
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
2014-04-10 05:55:34 -05:00
|
|
|
out
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn show_digit(nn: usize) -> &'static str {
|
2014-01-19 02:21:14 -06:00
|
|
|
match nn {
|
2014-04-21 16:06:59 -05:00
|
|
|
0 => {" zero"}
|
|
|
|
1 => {" one"}
|
|
|
|
2 => {" two"}
|
|
|
|
3 => {" three"}
|
|
|
|
4 => {" four"}
|
|
|
|
5 => {" five"}
|
|
|
|
6 => {" six"}
|
|
|
|
7 => {" seven"}
|
|
|
|
8 => {" eight"}
|
|
|
|
9 => {" nine"}
|
2014-10-09 14:17:22 -05:00
|
|
|
_ => {panic!("expected digits from 0 to 9...")}
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
struct Number(usize);
|
2015-01-28 07:34:18 -06:00
|
|
|
impl fmt::Debug for Number {
|
2014-04-21 16:06:59 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let mut out = vec![];
|
|
|
|
let Number(mut num) = *self;
|
|
|
|
if num == 0 { out.push(show_digit(0)) };
|
|
|
|
|
|
|
|
while num != 0 {
|
|
|
|
let dig = num % 10;
|
|
|
|
num = num / 10;
|
|
|
|
let s = show_digit(dig);
|
|
|
|
out.push(s);
|
|
|
|
}
|
2012-07-06 20:20:36 -05:00
|
|
|
|
2014-04-21 16:06:59 -05:00
|
|
|
for s in out.iter().rev() {
|
2014-05-11 13:14:14 -05:00
|
|
|
try!(write!(f, "{}", s))
|
2014-04-21 16:06:59 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
2014-04-10 05:55:34 -05:00
|
|
|
}
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
|
2014-04-21 16:06:59 -05:00
|
|
|
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 }
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn creature(
|
2015-03-25 19:06:52 -05:00
|
|
|
name: usize,
|
2014-04-21 16:06:59 -05:00
|
|
|
mut color: Color,
|
|
|
|
from_rendezvous: Receiver<CreatureInfo>,
|
2014-03-09 16:58:32 -05:00
|
|
|
to_rendezvous: Sender<CreatureInfo>,
|
2014-05-22 18:57:53 -05:00
|
|
|
to_rendezvous_log: Sender<String>
|
2012-07-06 20:20:36 -05:00
|
|
|
) {
|
2015-03-03 02:42:26 -06:00
|
|
|
let mut creatures_met = 0;
|
2012-07-06 20:20:36 -05:00
|
|
|
let mut evil_clones_met = 0;
|
2014-04-21 16:06:59 -05:00
|
|
|
let mut rendezvous = from_rendezvous.iter();
|
2012-07-06 20:20:36 -05:00
|
|
|
|
|
|
|
loop {
|
|
|
|
// ask for a pairing
|
2015-01-02 11:24:56 -06:00
|
|
|
to_rendezvous.send(CreatureInfo {name: name, color: color}).unwrap();
|
2012-07-06 20:20:36 -05:00
|
|
|
|
2014-04-21 16:06:59 -05:00
|
|
|
// log and change, or quit
|
|
|
|
match rendezvous.next() {
|
|
|
|
Some(other_creature) => {
|
2012-07-06 20:20:36 -05:00
|
|
|
color = transform(color, other_creature.color);
|
|
|
|
|
|
|
|
// track some statistics
|
|
|
|
creatures_met += 1;
|
|
|
|
if other_creature.name == name {
|
|
|
|
evil_clones_met += 1;
|
|
|
|
}
|
|
|
|
}
|
2014-04-21 16:06:59 -05:00
|
|
|
None => break
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
}
|
2014-04-21 16:06:59 -05:00
|
|
|
// log creatures met and evil clones of self
|
2015-01-06 18:16:35 -06:00
|
|
|
let report = format!("{}{:?}", creatures_met, Number(evil_clones_met));
|
2015-01-02 11:24:56 -06:00
|
|
|
to_rendezvous_log.send(report).unwrap();
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn rendezvous(nn: usize, set: Vec<Color>) {
|
2012-07-07 23:43:12 -05:00
|
|
|
// these ports will allow us to hear from the creatures
|
2014-03-09 16:58:32 -05:00
|
|
|
let (to_rendezvous, from_creatures) = channel::<CreatureInfo>();
|
2012-07-07 23:43:12 -05:00
|
|
|
|
|
|
|
// these channels will be passed to the creatures so they can talk to us
|
2014-05-22 18:57:53 -05:00
|
|
|
let (to_rendezvous_log, from_creatures_log) = channel::<String>();
|
2012-07-07 23:43:12 -05:00
|
|
|
|
|
|
|
// these channels will allow us to talk to each creature by 'name'/index
|
2015-01-07 15:39:05 -06:00
|
|
|
let to_creature: Vec<Sender<CreatureInfo>> =
|
2014-04-21 16:06:59 -05:00
|
|
|
set.iter().enumerate().map(|(ii, &col)| {
|
2012-09-21 20:43:30 -05:00
|
|
|
// create each creature as a listener with a port, and
|
|
|
|
// give us a channel to talk to each
|
2013-01-29 01:54:39 -06:00
|
|
|
let to_rendezvous = to_rendezvous.clone();
|
|
|
|
let to_rendezvous_log = to_rendezvous_log.clone();
|
2014-03-09 16:58:32 -05:00
|
|
|
let (to_creature, from_rendezvous) = channel();
|
2015-02-17 17:10:25 -06:00
|
|
|
thread::spawn(move|| {
|
2013-12-03 18:44:16 -06:00
|
|
|
creature(ii,
|
|
|
|
col,
|
|
|
|
from_rendezvous,
|
2014-04-21 16:06:59 -05:00
|
|
|
to_rendezvous,
|
|
|
|
to_rendezvous_log);
|
2015-01-05 23:59:45 -06:00
|
|
|
});
|
2013-01-29 01:54:39 -06:00
|
|
|
to_creature
|
2013-06-29 00:05:50 -05:00
|
|
|
}).collect();
|
2012-07-06 20:20:36 -05:00
|
|
|
|
|
|
|
let mut creatures_met = 0;
|
|
|
|
|
|
|
|
// set up meetings...
|
2015-01-26 14:46:12 -06:00
|
|
|
for _ in 0..nn {
|
2015-01-02 11:24:56 -06:00
|
|
|
let fst_creature = from_creatures.recv().unwrap();
|
|
|
|
let snd_creature = from_creatures.recv().unwrap();
|
2012-07-07 23:43:12 -05:00
|
|
|
|
|
|
|
creatures_met += 2;
|
|
|
|
|
2015-01-02 11:24:56 -06:00
|
|
|
to_creature[fst_creature.name].send(snd_creature).unwrap();
|
|
|
|
to_creature[snd_creature.name].send(fst_creature).unwrap();
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// tell each creature to stop
|
2014-04-21 16:06:59 -05:00
|
|
|
drop(to_creature);
|
2012-07-06 20:20:36 -05:00
|
|
|
|
|
|
|
// print each color in the set
|
2014-01-09 04:06:55 -06:00
|
|
|
println!("{}", show_color_list(set));
|
2012-07-06 20:20:36 -05:00
|
|
|
|
|
|
|
// print each creature's stats
|
2014-04-21 16:06:59 -05:00
|
|
|
drop(to_rendezvous_log);
|
|
|
|
for rep in from_creatures_log.iter() {
|
|
|
|
println!("{}", rep);
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// print the total number of creatures met
|
2015-01-06 18:16:35 -06:00
|
|
|
println!("{:?}\n", Number(creatures_met));
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
|
2012-10-03 21:16:27 -05:00
|
|
|
fn main() {
|
2015-02-13 14:08:05 -06:00
|
|
|
let nn = if std::env::var_os("RUST_BENCH").is_some() {
|
2014-04-21 16:06:59 -05:00
|
|
|
200000
|
2012-07-06 20:20:36 -05:00
|
|
|
} else {
|
2015-02-16 08:04:02 -06:00
|
|
|
std::env::args()
|
|
|
|
.nth(1)
|
2015-01-28 00:52:32 -06:00
|
|
|
.and_then(|arg| arg.parse().ok())
|
2015-02-18 08:05:34 -06:00
|
|
|
.unwrap_or(600)
|
2012-07-06 20:20:36 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
print_complements();
|
2014-01-09 04:06:55 -06:00
|
|
|
println!("");
|
2012-07-06 20:20:36 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
rendezvous(nn, vec!(Blue, Red, Yellow));
|
2012-07-06 20:20:36 -05:00
|
|
|
|
|
|
|
rendezvous(nn,
|
2014-03-05 16:02:44 -06:00
|
|
|
vec!(Blue, Red, Yellow, Red, Yellow, Blue, Red, Yellow, Red, Blue));
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|