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.363 kb
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
1 // chameneos
2
ea01ee2 @brson Convert 'use' to 'extern mod'. Remove old 'use' syntax
brson authored
3 extern mod std;
f686896 @pcwalton test: "import" -> "use"
pcwalton authored
4 use std::map;
cb7a539 @brson Convert std::map to camel case
brson authored
5 use std::map::HashMap;
f686896 @pcwalton test: "import" -> "use"
pcwalton authored
6 use std::sort;
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
7
8 fn print_complements() {
9 let all = ~[Blue, Red, Yellow];
8d4928f @nikomatsakis Revert "replace explicit calls to vec::each with vec::each_ref, parti…
nikomatsakis authored
10 for vec::each(all) |aa| {
11 for vec::each(all) |bb| {
9cf271f @nikomatsakis De-mode vec::each() and many of the str iteration routines
nikomatsakis authored
12 io::println(show_color(*aa) + ~" + " + show_color(*bb) +
13 ~" -> " + show_color(transform(*aa, *bb)));
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
14 }
15 }
16 }
17
638ee23 @killerswan Add improvements suggested by erickt and bblum
killerswan authored
18 enum color { Red, Yellow, Blue }
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
19
20 type creature_info = { name: uint, color: color };
21
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
22 fn show_color(cc: color) -> ~str {
ecaf9e3 @brson Convert alt to match. Stop parsing alt
brson authored
23 match (cc) {
025d866 @brson Switch alts to use arrows
brson authored
24 Red => {~"red"}
25 Yellow => {~"yellow"}
26 Blue => {~"blue"}
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
27 }
28 }
29
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
30 fn show_color_list(set: ~[color]) -> ~str {
31 let mut out = ~"";
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
32 for vec::eachi(set) |_ii, col| {
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
33 out += ~" ";
3d59ac3 @nikomatsakis De-mode vec::map, vec::eachi, vec::rev_each, vec::rev_eachi
nikomatsakis authored
34 out += show_color(*col);
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
35 }
b355936 @brson Convert ret to return
brson authored
36 return out;
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
37 }
38
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
39 fn show_digit(nn: uint) -> ~str {
ecaf9e3 @brson Convert alt to match. Stop parsing alt
brson authored
40 match (nn) {
025d866 @brson 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 @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
52 }
53 }
54
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
55 fn show_number(nn: uint) -> ~str {
56 let mut out = ~"";
2bd482e @killerswan 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 @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
65 out = show_digit(dig) + ~" " + out;
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
66 }
67
b355936 @brson Convert ret to return
brson authored
68 return out;
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
69 }
70
71 fn transform(aa: color, bb: color) -> color {
ecaf9e3 @brson Convert alt to match. Stop parsing alt
brson authored
72 match (aa, bb) {
025d866 @brson 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 @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
82 }
83 }
84
85 fn creature(
86 name: uint,
87 color: color,
8337fa1 @brson Camel case the option type
brson authored
88 from_rendezvous: comm::Port<Option<creature_info>>,
9c6890f @brson Convert more core types to camel case
brson authored
89 to_rendezvous: comm::Chan<creature_info>,
90 to_rendezvous_log: comm::Chan<~str>
2bd482e @killerswan 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 @brson Convert alt to match. Stop parsing alt
brson authored
102 match resp {
8337fa1 @brson Camel case the option type
brson authored
103 option::Some(other_creature) => {
2bd482e @killerswan 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 }
8337fa1 @brson Camel case the option type
brson authored
112 option::None => {
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
113 // log creatures met and evil clones of self
29f32b4 @paulstansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
114 let report = fmt!("%u", creatures_met) + ~" " +
2bd482e @killerswan 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 @killerswan Add improvements suggested by erickt and bblum
killerswan authored
124 // these ports will allow us to hear from the creatures
161a82e @brson Camel case various core constructors
brson authored
125 let from_creatures: comm::Port<creature_info> = comm::Port();
126 let from_creatures_log: comm::Port<~str> = comm::Port();
638ee23 @killerswan 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
161a82e @brson Camel case various core constructors
brson authored
129 let to_rendezvous = comm::Chan(from_creatures);
130 let to_rendezvous_log = comm::Chan(from_creatures_log);
638ee23 @killerswan Add improvements suggested by erickt and bblum
killerswan authored
131
132 // these channels will allow us to talk to each creature by 'name'/index
8337fa1 @brson Camel case the option type
brson authored
133 let to_creature: ~[comm::Chan<Option<creature_info>>] =
3d59ac3 @nikomatsakis De-mode vec::map, vec::eachi, vec::rev_each, vec::rev_eachi
nikomatsakis authored
134 vec::mapi(set, |ii, col| {
135 // create each creature as a listener with a port, and
136 // give us a channel to talk to each
137 let ii = ii;
138 let col = *col;
139 do task::spawn_listener |from_rendezvous, move ii, move col| {
140 creature(ii, col, from_rendezvous, to_rendezvous,
141 to_rendezvous_log);
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
142 }
3d59ac3 @nikomatsakis De-mode vec::map, vec::eachi, vec::rev_each, vec::rev_eachi
nikomatsakis authored
143 });
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
144
145 let mut creatures_met = 0;
146
147 // set up meetings...
638ee23 @killerswan 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
8337fa1 @brson Camel case the option type
brson authored
154 comm::send(to_creature[fst_creature.name], Some(snd_creature));
155 comm::send(to_creature[snd_creature.name], Some(fst_creature));
2bd482e @killerswan 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| {
3d59ac3 @nikomatsakis De-mode vec::map, vec::eachi, vec::rev_each, vec::rev_eachi
nikomatsakis authored
160 comm::send(*to_one, None);
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
161 }
162
163 // save each creature's meeting stats
164 let mut report = ~[];
8d4928f @nikomatsakis Revert "replace explicit calls to vec::each with vec::each_ref, parti…
nikomatsakis authored
165 for vec::each(to_creature) |_to_one| {
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
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
8d4928f @nikomatsakis Revert "replace explicit calls to vec::each with vec::each_ref, parti…
nikomatsakis authored
173 for vec::each(report) |rep| {
9cf271f @nikomatsakis De-mode vec::each() and many of the str iteration routines
nikomatsakis authored
174 io::println(*rep);
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
175 }
176
177 // print the total number of creatures met
178 io::println(show_number(creatures_met));
179 }
180
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
181 fn main(args: ~[~str]) {
182 let args = if os::getenv(~"RUST_BENCH").is_some() {
183 ~[~"", ~"200000"]
02ef651 @brson Increase the difficulty of chameneos-redux under 'make perf'
brson authored
184 } else if args.len() <= 1u {
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
185 ~[~"", ~"600"]
2bd482e @killerswan 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 @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
193 io::println(~"");
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
194
195 rendezvous(nn, ~[Blue, Red, Yellow]);
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
196 io::println(~"");
2bd482e @killerswan 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.