Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 366 lines (302 sloc) 9.463 kB
a005b1a @brson Fix some broken tests
brson authored
1 /*!
640886c Polymorphic MapReduce!
Eric Holk authored
2 A parallel word-frequency counting program.
3
4 This is meant primarily to demonstrate Rust's MapReduce framework.
5
6 It takes a list of files on the command line and outputs a list of
7 words along with how many times each word is used.
8
9 */
10
05543fd @eholk Make tests pass
eholk authored
11 // xfail-pretty
12
e653d49 @pcwalton rustc: Remove legacy mode inference, unless #[legacy_modes] is used
pcwalton authored
13 #[legacy_modes];
14
ea01ee2 @brson Convert 'use' to 'extern mod'. Remove old 'use' syntax
brson authored
15 extern mod std;
640886c Polymorphic MapReduce!
Eric Holk authored
16
f686896 @pcwalton test: "import" -> "use"
pcwalton authored
17 use option = option;
18 use option::Some;
19 use option::None;
20 use std::map;
cb7a539 @brson Convert std::map to camel case
brson authored
21 use std::map::HashMap;
00d1d4e @killerswan (fix incoming) Add the necessary traits to some generic functions
killerswan authored
22 use hash::Hash;
f686896 @pcwalton test: "import" -> "use"
pcwalton authored
23 use io::WriterUtil;
24
25 use std::time;
26
27 use comm::Chan;
28 use comm::Port;
29 use comm::recv;
30 use comm::send;
00d1d4e @killerswan (fix incoming) Add the necessary traits to some generic functions
killerswan authored
31 use cmp::Eq;
32 use to_bytes::IterBytes;
0c3a128 @eholk Update word-count-generic to latest syntax and un-xfail it. Closes #1…
eholk authored
33
38891b9 @paulstansifer Update invocation syntax for `macro_rules!`
paulstansifer authored
34 macro_rules! move_out (
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
35 { $x:expr } => { unsafe { let y <- *ptr::addr_of($x); y } }
38891b9 @paulstansifer Update invocation syntax for `macro_rules!`
paulstansifer authored
36 )
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
37
a00478b @eholk word-count-generic now generates random words in benchmark mode.
eholk authored
38 trait word_reader {
8337fa1 @brson Camel case the option type
brson authored
39 fn read_word() -> Option<~str>;
a00478b @eholk word-count-generic now generates random words in benchmark mode.
eholk authored
40 }
41
0c3a128 @eholk Update word-count-generic to latest syntax and un-xfail it. Closes #1…
eholk authored
42 // These used to be in task, but they disappeard.
9c6890f @brson Convert more core types to camel case
brson authored
43 type joinable_task = Port<()>;
d1fc2b5 @brson Convert to new closure syntax
brson authored
44 fn spawn_joinable(+f: fn~()) -> joinable_task {
161a82e @brson Camel case various core constructors
brson authored
45 let p = Port();
46 let c = Chan(p);
d1fc2b5 @brson Convert to new closure syntax
brson authored
47 do task::spawn() |move f| {
0c3a128 @eholk Update word-count-generic to latest syntax and un-xfail it. Closes #1…
eholk authored
48 f();
49 c.send(());
50 }
51 p
52 }
53
54 fn join(t: joinable_task) {
55 t.recv()
56 }
640886c Polymorphic MapReduce!
Eric Holk authored
57
1125831 @brson Convert more core types to camel case
brson authored
58 impl io::Reader: word_reader {
8337fa1 @brson Camel case the option type
brson authored
59 fn read_word() -> Option<~str> { read_word(self) }
a00478b @eholk word-count-generic now generates random words in benchmark mode.
eholk authored
60 }
61
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
62 fn file_word_reader(filename: ~str) -> word_reader {
c284b8b @graydon Start using core::path2::Path in a lot of places.
graydon authored
63 match io::file_reader(&Path(filename)) {
0c6e470 @brson Convert core::result to camel case
brson authored
64 result::Ok(f) => { f as word_reader }
65 result::Err(e) => { fail fmt!("%?", e) }
a00478b @eholk word-count-generic now generates random words in benchmark mode.
eholk authored
66 }
67 }
640886c Polymorphic MapReduce!
Eric Holk authored
68
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
69 fn map(f: fn~() -> word_reader, emit: map_reduce::putter<~str, int>) {
a00478b @eholk word-count-generic now generates random words in benchmark mode.
eholk authored
70 let f = f();
321fd80 @catamorphism Add an infinite loop construct
catamorphism authored
71 loop {
ecaf9e3 @brson Convert alt to match. Stop parsing alt
brson authored
72 match f.read_word() {
8337fa1 @brson Camel case the option type
brson authored
73 Some(w) => { emit(w, 1); }
74 None => { break; }
640886c Polymorphic MapReduce!
Eric Holk authored
75 }
76 }
77 }
78
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
79 fn reduce(&&word: ~str, get: map_reduce::getter<int>) {
0c3a128 @eholk Update word-count-generic to latest syntax and un-xfail it. Closes #1…
eholk authored
80 let mut count = 0;
640886c Polymorphic MapReduce!
Eric Holk authored
81
8337fa1 @brson Camel case the option type
brson authored
82 loop { match get() { Some(_) => { count += 1; } None => { break; } } }
ba3eebd @nikomatsakis Make it illegal to use modes in a fn signature with providing
nikomatsakis authored
83
29f32b4 @paulstansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
84 io::println(fmt!("%s\t%?", word, count));
640886c Polymorphic MapReduce!
Eric Holk authored
85 }
86
3ab4b01 @brson Remove the class keyword
brson authored
87 struct box<T> {
2572e80 @brson Remove 'let' syntax for struct fields
brson authored
88 mut contents: Option<T>,
93d3b8a @brson Convert class methods to impl methods. Stop parsing class methods
brson authored
89 }
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
90
93d3b8a @brson Convert class methods to impl methods. Stop parsing class methods
brson authored
91 impl<T> box<T> {
ba3eebd @nikomatsakis Make it illegal to use modes in a fn signature with providing
nikomatsakis authored
92 fn swap(f: fn(+v: T) -> T) {
8337fa1 @brson Camel case the option type
brson authored
93 let mut tmp = None;
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
94 self.contents <-> tmp;
8337fa1 @brson Camel case the option type
brson authored
95 self.contents = Some(f(option::unwrap(tmp)));
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
96 }
97
98 fn unwrap() -> T {
8337fa1 @brson Camel case the option type
brson authored
99 let mut tmp = None;
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
100 self.contents <-> tmp;
101 option::unwrap(tmp)
102 }
103 }
104
b4e547d @brson Remove struct ctors
brson authored
105 fn box<T>(+x: T) -> box<T> {
106 box {
107 contents: Some(x)
108 }
109 }
110
640886c Polymorphic MapReduce!
Eric Holk authored
111 mod map_reduce {
dffe188 @graydon Install new pub/priv/export rules as defaults, old rules accessible u…
graydon authored
112 #[legacy_exports];
640886c Polymorphic MapReduce!
Eric Holk authored
113 export putter;
114 export getter;
115 export mapper;
116 export reducer;
117 export map_reduce;
118
3bd1f32 @brson Convert all kind bounds to camel case. Remove send, owned keywords.
brson authored
119 type putter<K: Send, V: Send> = fn(K, V);
640886c Polymorphic MapReduce!
Eric Holk authored
120
3bd1f32 @brson Convert all kind bounds to camel case. Remove send, owned keywords.
brson authored
121 type mapper<K1: Send, K2: Send, V: Send> = fn~(K1, putter<K2, V>);
640886c Polymorphic MapReduce!
Eric Holk authored
122
3bd1f32 @brson Convert all kind bounds to camel case. Remove send, owned keywords.
brson authored
123 type getter<V: Send> = fn() -> Option<V>;
640886c Polymorphic MapReduce!
Eric Holk authored
124
3bd1f32 @brson Convert all kind bounds to camel case. Remove send, owned keywords.
brson authored
125 type reducer<K: Copy Send, V: Copy Send> = fn~(K, getter<V>);
640886c Polymorphic MapReduce!
Eric Holk authored
126
3bd1f32 @brson Convert all kind bounds to camel case. Remove send, owned keywords.
brson authored
127 enum ctrl_proto<K: Copy Send, V: Copy Send> {
9c6890f @brson Convert more core types to camel case
brson authored
128 find_reducer(K, Chan<Chan<reduce_proto<V>>>),
0c3a128 @eholk Update word-count-generic to latest syntax and un-xfail it. Closes #1…
eholk authored
129 mapper_done
640886c Polymorphic MapReduce!
Eric Holk authored
130 }
131
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
132
77e83d8 @paulstansifer Change calls of `proto!` to use parens.
paulstansifer authored
133 proto! ctrl_proto (
3bd1f32 @brson Convert all kind bounds to camel case. Remove send, owned keywords.
brson authored
134 open: send<K: Copy Send, V: Copy Send> {
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
135 find_reducer(K) -> reducer_response<K, V>,
6748f78 @eholk Polymorphic protocols work well enough to do MapReduce.
eholk authored
136 mapper_done -> !
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
137 }
138
3bd1f32 @brson Convert all kind bounds to camel case. Remove send, owned keywords.
brson authored
139 reducer_response: recv<K: Copy Send, V: Copy Send> {
9c6890f @brson Convert more core types to camel case
brson authored
140 reducer(Chan<reduce_proto<V>>) -> open<K, V>
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
141 }
77e83d8 @paulstansifer Change calls of `proto!` to use parens.
paulstansifer authored
142 )
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
143
3bd1f32 @brson Convert all kind bounds to camel case. Remove send, owned keywords.
brson authored
144 enum reduce_proto<V: Copy Send> { emit_val(V), done, addref, release }
640886c Polymorphic MapReduce!
Eric Holk authored
145
5e41739 @graydon Remove final bits of residual hokey-hash functions. Close #1616.
graydon authored
146 fn start_mappers<K1: Copy Send, K2: Hash IterBytes Eq Const Copy Send,
3bd1f32 @brson Convert all kind bounds to camel case. Remove send, owned keywords.
brson authored
147 V: Copy Send>(
0c3a128 @eholk Update word-count-generic to latest syntax and un-xfail it. Closes #1…
eholk authored
148 map: mapper<K1, K2, V>,
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
149 &ctrls: ~[ctrl_proto::server::open<K2, V>],
150 inputs: ~[K1])
98e161f @msullivan Switch the compiler over to using ~[] notation instead of []/~. Close…
msullivan authored
151 -> ~[joinable_task]
0c3a128 @eholk Update word-count-generic to latest syntax and un-xfail it. Closes #1…
eholk authored
152 {
98e161f @msullivan Switch the compiler over to using ~[] notation instead of []/~. Close…
msullivan authored
153 let mut tasks = ~[];
d1fc2b5 @brson Convert to new closure syntax
brson authored
154 for inputs.each |i| {
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
155 let (ctrl, ctrl_server) = ctrl_proto::init();
156 let ctrl = box(ctrl);
cfed923 @nikomatsakis demode the each() method on vec and other iterables.
nikomatsakis authored
157 let i = copy *i;
67a8e71 @nikomatsakis Demode vec::push (and convert to method)
nikomatsakis authored
158 tasks.push(spawn_joinable(|move i| map_task(map, ctrl, i)));
159 ctrls.push(ctrl_server);
640886c Polymorphic MapReduce!
Eric Holk authored
160 }
b355936 @brson Convert ret to return
brson authored
161 return tasks;
640886c Polymorphic MapReduce!
Eric Holk authored
162 }
163
5e41739 @graydon Remove final bits of residual hokey-hash functions. Close #1616.
graydon authored
164 fn map_task<K1: Copy Send, K2: Hash IterBytes Eq Const Copy Send, V: Copy Send>(
0c3a128 @eholk Update word-count-generic to latest syntax and un-xfail it. Closes #1…
eholk authored
165 map: mapper<K1, K2, V>,
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
166 ctrl: box<ctrl_proto::client::open<K2, V>>,
0c3a128 @eholk Update word-count-generic to latest syntax and un-xfail it. Closes #1…
eholk authored
167 input: K1)
168 {
f0dfbe7 @graydon Register new snapshots, purge log_err and log_full in favour of log(.…
graydon authored
169 // log(error, "map_task " + input);
cb7a539 @brson Convert std::map to camel case
brson authored
170 let intermediates = map::HashMap();
640886c Polymorphic MapReduce!
Eric Holk authored
171
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
172 do map(input) |key, val| {
8337fa1 @brson Camel case the option type
brson authored
173 let mut c = None;
a12e90b @killerswan (partially fix incoming) More hash function simplification
killerswan authored
174 let found = intermediates.find(key);
175 match found {
8337fa1 @brson Camel case the option type
brson authored
176 Some(_c) => { c = Some(_c); }
177 None => {
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
178 do ctrl.swap |ctrl| {
179 let ctrl = ctrl_proto::client::find_reducer(ctrl, key);
ecaf9e3 @brson Convert alt to match. Stop parsing alt
brson authored
180 match pipes::recv(ctrl) {
025d866 @brson Switch alts to use arrows
brson authored
181 ctrl_proto::reducer(c_, ctrl) => {
8337fa1 @brson Camel case the option type
brson authored
182 c = Some(c_);
29f32b4 @paulstansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
183 move_out!(ctrl)
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
184 }
185 }
186 }
187 intermediates.insert(key, c.get());
c5437c0 @brson Fix failing tests
brson authored
188 send(c.get(), addref);
640886c Polymorphic MapReduce!
Eric Holk authored
189 }
190 }
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
191 send(c.get(), emit_val(val));
640886c Polymorphic MapReduce!
Eric Holk authored
192 }
193
3bd1f32 @brson Convert all kind bounds to camel case. Remove send, owned keywords.
brson authored
194 fn finish<K: Copy Send, V: Copy Send>(_k: K, v: Chan<reduce_proto<V>>)
0c3a128 @eholk Update word-count-generic to latest syntax and un-xfail it. Closes #1…
eholk authored
195 {
640886c Polymorphic MapReduce!
Eric Holk authored
196 send(v, release);
197 }
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
198 for intermediates.each_value |v| { send(v, release) }
199 ctrl_proto::client::mapper_done(ctrl.unwrap());
640886c Polymorphic MapReduce!
Eric Holk authored
200 }
201
3bd1f32 @brson Convert all kind bounds to camel case. Remove send, owned keywords.
brson authored
202 fn reduce_task<K: Copy Send, V: Copy Send>(
0c3a128 @eholk Update word-count-generic to latest syntax and un-xfail it. Closes #1…
eholk authored
203 reduce: reducer<K, V>,
204 key: K,
9c6890f @brson Convert more core types to camel case
brson authored
205 out: Chan<Chan<reduce_proto<V>>>)
0c3a128 @eholk Update word-count-generic to latest syntax and un-xfail it. Closes #1…
eholk authored
206 {
161a82e @brson Camel case various core constructors
brson authored
207 let p = Port();
640886c Polymorphic MapReduce!
Eric Holk authored
208
161a82e @brson Camel case various core constructors
brson authored
209 send(out, Chan(p));
640886c Polymorphic MapReduce!
Eric Holk authored
210
4dcf84e @brson Remove bind. Issue #2189
brson authored
211 let mut ref_count = 0;
212 let mut is_done = false;
640886c Polymorphic MapReduce!
Eric Holk authored
213
3bd1f32 @brson Convert all kind bounds to camel case. Remove send, owned keywords.
brson authored
214 fn get<V: Copy Send>(p: Port<reduce_proto<V>>,
0c3a128 @eholk Update word-count-generic to latest syntax and un-xfail it. Closes #1…
eholk authored
215 &ref_count: int, &is_done: bool)
8337fa1 @brson Camel case the option type
brson authored
216 -> Option<V> {
640886c Polymorphic MapReduce!
Eric Holk authored
217 while !is_done || ref_count > 0 {
ecaf9e3 @brson Convert alt to match. Stop parsing alt
brson authored
218 match recv(p) {
025d866 @brson Switch alts to use arrows
brson authored
219 emit_val(v) => {
29f32b4 @paulstansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
220 // error!("received %d", v);
8337fa1 @brson Camel case the option type
brson authored
221 return Some(v);
640886c Polymorphic MapReduce!
Eric Holk authored
222 }
025d866 @brson Switch alts to use arrows
brson authored
223 done => {
29f32b4 @paulstansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
224 // error!("all done");
640886c Polymorphic MapReduce!
Eric Holk authored
225 is_done = true;
226 }
025d866 @brson Switch alts to use arrows
brson authored
227 addref => { ref_count += 1; }
228 release => { ref_count -= 1; }
640886c Polymorphic MapReduce!
Eric Holk authored
229 }
230 }
8337fa1 @brson Camel case the option type
brson authored
231 return None;
640886c Polymorphic MapReduce!
Eric Holk authored
232 }
233
d1fc2b5 @brson Convert to new closure syntax
brson authored
234 reduce(key, || get(p, ref_count, is_done) );
640886c Polymorphic MapReduce!
Eric Holk authored
235 }
236
5e41739 @graydon Remove final bits of residual hokey-hash functions. Close #1616.
graydon authored
237 fn map_reduce<K1: Copy Send, K2: Hash IterBytes Eq Const Copy Send, V: Copy Send>(
0c3a128 @eholk Update word-count-generic to latest syntax and un-xfail it. Closes #1…
eholk authored
238 map: mapper<K1, K2, V>,
239 reduce: reducer<K2, V>,
98e161f @msullivan Switch the compiler over to using ~[] notation instead of []/~. Close…
msullivan authored
240 inputs: ~[K1])
0c3a128 @eholk Update word-count-generic to latest syntax and un-xfail it. Closes #1…
eholk authored
241 {
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
242 let mut ctrl = ~[];
640886c Polymorphic MapReduce!
Eric Holk authored
243
244 // This task becomes the master control task. It task::_spawns
245 // to do the rest.
246
cb7a539 @brson Convert std::map to camel case
brson authored
247 let reducers = map::HashMap();
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
248 let mut tasks = start_mappers(map, ctrl, inputs);
0c3a128 @eholk Update word-count-generic to latest syntax and un-xfail it. Closes #1…
eholk authored
249 let mut num_mappers = vec::len(inputs) as int;
640886c Polymorphic MapReduce!
Eric Holk authored
250
251 while num_mappers > 0 {
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
252 let (_ready, message, ctrls) = pipes::select(ctrl);
ecaf9e3 @brson Convert alt to match. Stop parsing alt
brson authored
253 match option::unwrap(message) {
025d866 @brson Switch alts to use arrows
brson authored
254 ctrl_proto::mapper_done => {
29f32b4 @paulstansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
255 // error!("received mapper terminated.");
640886c Polymorphic MapReduce!
Eric Holk authored
256 num_mappers -= 1;
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
257 ctrl = ctrls;
640886c Polymorphic MapReduce!
Eric Holk authored
258 }
025d866 @brson Switch alts to use arrows
brson authored
259 ctrl_proto::find_reducer(k, cc) => {
640886c Polymorphic MapReduce!
Eric Holk authored
260 let c;
f0dfbe7 @graydon Register new snapshots, purge log_err and log_full in favour of log(.…
graydon authored
261 // log(error, "finding reducer for " + k);
ecaf9e3 @brson Convert alt to match. Stop parsing alt
brson authored
262 match reducers.find(k) {
8337fa1 @brson Camel case the option type
brson authored
263 Some(_c) => {
f0dfbe7 @graydon Register new snapshots, purge log_err and log_full in favour of log(.…
graydon authored
264 // log(error,
8b58095 @graydon Register snapshots and switch logging over to use of log_full or #err…
graydon authored
265 // "reusing existing reducer for " + k);
640886c Polymorphic MapReduce!
Eric Holk authored
266 c = _c;
267 }
8337fa1 @brson Camel case the option type
brson authored
268 None => {
f0dfbe7 @graydon Register new snapshots, purge log_err and log_full in favour of log(.…
graydon authored
269 // log(error, "creating new reducer for " + k);
161a82e @brson Camel case various core constructors
brson authored
270 let p = Port();
271 let ch = Chan(p);
640886c Polymorphic MapReduce!
Eric Holk authored
272 let r = reduce, kk = k;
67a8e71 @nikomatsakis Demode vec::push (and convert to method)
nikomatsakis authored
273 tasks.push(spawn_joinable(|| reduce_task(r, kk, ch) ));
640886c Polymorphic MapReduce!
Eric Holk authored
274 c = recv(p);
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
275 reducers.insert(k, c);
640886c Polymorphic MapReduce!
Eric Holk authored
276 }
277 }
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
278 ctrl = vec::append_one(
279 ctrls,
29f32b4 @paulstansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
280 ctrl_proto::server::reducer(move_out!(cc), c));
640886c Polymorphic MapReduce!
Eric Holk authored
281 }
282 }
283 }
284
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
285 for reducers.each_value |v| { send(v, done) }
640886c Polymorphic MapReduce!
Eric Holk authored
286
cfed923 @nikomatsakis demode the each() method on vec and other iterables.
nikomatsakis authored
287 for tasks.each |t| { join(*t); }
640886c Polymorphic MapReduce!
Eric Holk authored
288 }
289 }
290
267ab11 @nikomatsakis use + mode for (almost) everything when not using legacy modes
nikomatsakis authored
291 fn main(++argv: ~[~str]) {
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
292 if vec::len(argv) < 2u && !os::getenv(~"RUST_BENCH").is_some() {
640886c Polymorphic MapReduce!
Eric Holk authored
293 let out = io::stdout();
294
29f32b4 @paulstansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
295 out.write_line(fmt!("Usage: %s <filename> ...", argv[0]));
640886c Polymorphic MapReduce!
Eric Holk authored
296
b355936 @brson Convert ret to return
brson authored
297 return;
640886c Polymorphic MapReduce!
Eric Holk authored
298 }
299
a00478b @eholk word-count-generic now generates random words in benchmark mode.
eholk authored
300 let readers: ~[fn~() -> word_reader] = if argv.len() >= 2 {
3d59ac3 @nikomatsakis De-mode vec::map, vec::eachi, vec::rev_each, vec::rev_eachi
nikomatsakis authored
301 vec::view(argv, 1u, argv.len()).map(|f| {
302 let f = *f;
303 fn~() -> word_reader { file_word_reader(f) }
304 })
a00478b @eholk word-count-generic now generates random words in benchmark mode.
eholk authored
305 }
306 else {
307 let num_readers = 50;
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
308 let words_per_reader = 600;
a00478b @eholk word-count-generic now generates random words in benchmark mode.
eholk authored
309 vec::from_fn(
310 num_readers,
311 |_i| fn~() -> word_reader {
312 random_word_reader(words_per_reader) as word_reader
313 })
314 };
315
640886c Polymorphic MapReduce!
Eric Holk authored
316 let start = time::precise_time_ns();
317
a00478b @eholk word-count-generic now generates random words in benchmark mode.
eholk authored
318 map_reduce::map_reduce(map, reduce, readers);
640886c Polymorphic MapReduce!
Eric Holk authored
319 let stop = time::precise_time_ns();
320
0c3a128 @eholk Update word-count-generic to latest syntax and un-xfail it. Closes #1…
eholk authored
321 let elapsed = (stop - start) / 1000000u64;
640886c Polymorphic MapReduce!
Eric Holk authored
322
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
323 log(error, ~"MapReduce completed in "
324 + u64::str(elapsed) + ~"ms");
640886c Polymorphic MapReduce!
Eric Holk authored
325 }
326
8337fa1 @brson Camel case the option type
brson authored
327 fn read_word(r: io::Reader) -> Option<~str> {
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
328 let mut w = ~"";
640886c Polymorphic MapReduce!
Eric Holk authored
329
330 while !r.eof() {
331 let c = r.read_char();
332
333 if is_word_char(c) {
ab6bb03 @brson Rename std::istr to std::str. Issue #855
brson authored
334 w += str::from_char(c);
8337fa1 @brson Camel case the option type
brson authored
335 } else { if w != ~"" { return Some(w); } }
640886c Polymorphic MapReduce!
Eric Holk authored
336 }
8337fa1 @brson Camel case the option type
brson authored
337 return None;
640886c Polymorphic MapReduce!
Eric Holk authored
338 }
a00478b @eholk word-count-generic now generates random words in benchmark mode.
eholk authored
339
df83a79 @eholk In generic word count, use str instead of [u8], and use built in is_a…
eholk authored
340 fn is_word_char(c: char) -> bool {
a00478b @eholk word-count-generic now generates random words in benchmark mode.
eholk authored
341 char::is_alphabetic(c) || char::is_digit(c) || c == '_'
342 }
343
93d3b8a @brson Convert class methods to impl methods. Stop parsing class methods
brson authored
344 struct random_word_reader {
2572e80 @brson Remove 'let' syntax for struct fields
brson authored
345 mut remaining: uint,
346 rng: rand::Rng,
93d3b8a @brson Convert class methods to impl methods. Stop parsing class methods
brson authored
347 }
a00478b @eholk word-count-generic now generates random words in benchmark mode.
eholk authored
348
93d3b8a @brson Convert class methods to impl methods. Stop parsing class methods
brson authored
349 impl random_word_reader: word_reader {
8337fa1 @brson Camel case the option type
brson authored
350 fn read_word() -> Option<~str> {
a00478b @eholk word-count-generic now generates random words in benchmark mode.
eholk authored
351 if self.remaining > 0 {
352 self.remaining -= 1;
1a276db @eholk Switch map-reduce control protocol to use pipes. This exposed a bug i…
eholk authored
353 let len = self.rng.gen_uint_range(1, 4);
8337fa1 @brson Camel case the option type
brson authored
354 Some(self.rng.gen_str(len))
a00478b @eholk word-count-generic now generates random words in benchmark mode.
eholk authored
355 }
8337fa1 @brson Camel case the option type
brson authored
356 else { None }
a00478b @eholk word-count-generic now generates random words in benchmark mode.
eholk authored
357 }
358 }
b4e547d @brson Remove struct ctors
brson authored
359
360 fn random_word_reader(count: uint) -> random_word_reader {
361 random_word_reader {
362 remaining: count,
363 rng: rand::Rng()
364 }
365 }
Something went wrong with that request. Please try again.