Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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