Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 202 lines (167 sloc) 5.395 kb
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
1 // chameneos
2
3 use std;
4 import std::map;
5 import std::map::hashmap;
6 import std::sort;
7
8 fn print_complements() {
9 let all = ~[Blue, Red, Yellow];
10 for vec::each(all) |aa| {
11 for vec::each(all) |bb| {
92743dc Michael J. Sullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
12 io::println(show_color(aa) + ~" + " + show_color(bb) +
13 ~" -> " + show_color(transform(aa,bb)));
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
14 }
15 }
16 }
17
638ee23 Kevin Cantu Add improvements suggested by erickt and bblum
killerswan authored
18 enum color { Red, Yellow, Blue }
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
19
20 type creature_info = { name: uint, color: color };
21
92743dc Michael J. Sullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
22 fn show_color(cc: color) -> ~str {
ecaf9e3 Brian Anderson Convert alt to match. Stop parsing alt
brson authored
23 match (cc) {
025d866 Brian Anderson Switch alts to use arrows
brson authored
24 Red => {~"red"}
25 Yellow => {~"yellow"}
26 Blue => {~"blue"}
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
27 }
28 }
29
92743dc Michael J. Sullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
30 fn show_color_list(set: ~[color]) -> ~str {
31 let mut out = ~"";
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
32 for vec::eachi(set) |_ii, col| {
92743dc Michael J. Sullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
33 out += ~" ";
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
34 out += show_color(col);
35 }
b355936 Brian Anderson Convert ret to return
brson authored
36 return out;
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
37 }
38
92743dc Michael J. Sullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
39 fn show_digit(nn: uint) -> ~str {
ecaf9e3 Brian Anderson Convert alt to match. Stop parsing alt
brson authored
40 match (nn) {
025d866 Brian Anderson Switch alts to use arrows
brson authored
41 0 => {~"zero"}
42 1 => {~"one"}
43 2 => {~"two"}
44 3 => {~"three"}
45 4 => {~"four"}
46 5 => {~"five"}
47 6 => {~"six"}
48 7 => {~"seven"}
49 8 => {~"eight"}
50 9 => {~"nine"}
51 _ => {fail ~"expected digits from 0 to 9..."}
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
52 }
53 }
54
92743dc Michael J. Sullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
55 fn show_number(nn: uint) -> ~str {
56 let mut out = ~"";
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
57 let mut num = nn;
58 let mut dig;
59
60 if num == 0 { out = show_digit(0) };
61
62 while num != 0 {
63 dig = num % 10;
64 num = num / 10;
92743dc Michael J. Sullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
65 out = show_digit(dig) + ~" " + out;
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
66 }
67
b355936 Brian Anderson Convert ret to return
brson authored
68 return out;
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
69 }
70
71 fn transform(aa: color, bb: color) -> color {
ecaf9e3 Brian Anderson Convert alt to match. Stop parsing alt
brson authored
72 match (aa, bb) {
025d866 Brian Anderson Switch alts to use arrows
brson authored
73 (Red, Red ) => { Red }
74 (Red, Yellow) => { Blue }
75 (Red, Blue ) => { Yellow }
76 (Yellow, Red ) => { Blue }
77 (Yellow, Yellow) => { Yellow }
78 (Yellow, Blue ) => { Red }
79 (Blue, Red ) => { Yellow }
80 (Blue, Yellow) => { Red }
81 (Blue, Blue ) => { Blue }
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
82 }
83 }
84
85 fn creature(
86 name: uint,
87 color: color,
9c6890f Brian Anderson Convert more core types to camel case
brson authored
88 from_rendezvous: comm::Port<option<creature_info>>,
89 to_rendezvous: comm::Chan<creature_info>,
90 to_rendezvous_log: comm::Chan<~str>
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
91 ) {
92 let mut color = color;
93 let mut creatures_met = 0;
94 let mut evil_clones_met = 0;
95
96 loop {
97 // ask for a pairing
98 comm::send(to_rendezvous, {name: name, color: color});
99 let resp = comm::recv(from_rendezvous);
100
101 // log and change, or print and quit
ecaf9e3 Brian Anderson Convert alt to match. Stop parsing alt
brson authored
102 match resp {
025d866 Brian Anderson Switch alts to use arrows
brson authored
103 option::some(other_creature) => {
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
104 color = transform(color, other_creature.color);
105
106 // track some statistics
107 creatures_met += 1;
108 if other_creature.name == name {
109 evil_clones_met += 1;
110 }
111 }
025d866 Brian Anderson Switch alts to use arrows
brson authored
112 option::none => {
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
113 // log creatures met and evil clones of self
29f32b4 Paul Stansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
114 let report = fmt!("%u", creatures_met) + ~" " +
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
115 show_number(evil_clones_met);
116 comm::send(to_rendezvous_log, report);
117 break;
118 }
119 }
120 }
121 }
122
123 fn rendezvous(nn: uint, set: ~[color]) {
638ee23 Kevin Cantu Add improvements suggested by erickt and bblum
killerswan authored
124 // these ports will allow us to hear from the creatures
9c6890f Brian Anderson Convert more core types to camel case
brson authored
125 let from_creatures: comm::Port<creature_info> = comm::port();
126 let from_creatures_log: comm::Port<~str> = comm::port();
638ee23 Kevin Cantu Add improvements suggested by erickt and bblum
killerswan authored
127
128 // these channels will be passed to the creatures so they can talk to us
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
129 let to_rendezvous = comm::chan(from_creatures);
130 let to_rendezvous_log = comm::chan(from_creatures_log);
638ee23 Kevin Cantu Add improvements suggested by erickt and bblum
killerswan authored
131
132 // these channels will allow us to talk to each creature by 'name'/index
9c6890f Brian Anderson Convert more core types to camel case
brson authored
133 let to_creature: ~[comm::Chan<option<creature_info>>] =
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
134 vec::mapi(set,
9c6890f Brian Anderson Convert more core types to camel case
brson authored
135 fn@(ii: uint, col: color) -> comm::Chan<option<creature_info>> {
638ee23 Kevin Cantu Add improvements suggested by erickt and bblum
killerswan authored
136 // create each creature as a listener with a port, and
137 // give us a channel to talk to each
b355936 Brian Anderson Convert ret to return
brson authored
138 return do task::spawn_listener |from_rendezvous| {
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
139 creature(ii, col, from_rendezvous, to_rendezvous,
140 to_rendezvous_log);
141 };
142 }
143 );
144
145 let mut creatures_met = 0;
146
147 // set up meetings...
638ee23 Kevin Cantu Add improvements suggested by erickt and bblum
killerswan authored
148 for nn.times {
149 let fst_creature: creature_info = comm::recv(from_creatures);
150 let snd_creature: creature_info = comm::recv(from_creatures);
151
152 creatures_met += 2;
153
154 comm::send(to_creature[fst_creature.name], some(snd_creature));
155 comm::send(to_creature[snd_creature.name], some(fst_creature));
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
156 }
157
158 // tell each creature to stop
159 for vec::eachi(to_creature) |_ii, to_one| {
160 comm::send(to_one, none);
161 }
162
163 // save each creature's meeting stats
164 let mut report = ~[];
165 for vec::each(to_creature) |_to_one| {
166 vec::push(report, comm::recv(from_creatures_log));
167 }
168
169 // print each color in the set
170 io::println(show_color_list(set));
171
172 // print each creature's stats
173 for vec::each(report) |rep| {
174 io::println(rep);
175 }
176
177 // print the total number of creatures met
178 io::println(show_number(creatures_met));
179 }
180
92743dc Michael J. Sullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
181 fn main(args: ~[~str]) {
182 let args = if os::getenv(~"RUST_BENCH").is_some() {
183 ~[~"", ~"200000"]
02ef651 Brian Anderson Increase the difficulty of chameneos-redux under 'make perf'
brson authored
184 } else if args.len() <= 1u {
92743dc Michael J. Sullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
185 ~[~"", ~"600"]
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
186 } else {
187 args
188 };
189
190 let nn = uint::from_str(args[1]).get();
191
192 print_complements();
92743dc Michael J. Sullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
193 io::println(~"");
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
194
195 rendezvous(nn, ~[Blue, Red, Yellow]);
92743dc Michael J. Sullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
196 io::println(~"");
2bd482e Kevin Cantu Add the Alioth chameneos-redux benchmark
killerswan authored
197
198 rendezvous(nn,
199 ~[Blue, Red, Yellow, Red, Yellow, Blue, Red, Yellow, Red, Blue]);
200 }
201
Something went wrong with that request. Please try again.