]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/hashmap-memory.rs
Merge pull request #20674 from jbcrail/fix-misspelled-comments
[rust.git] / src / test / run-pass / hashmap-memory.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(unboxed_closures)]
12
13 /**
14    A somewhat reduced test case to expose some Valgrind issues.
15
16    This originally came from the word-count benchmark.
17 */
18
19 pub fn map(filename: String, mut emit: map_reduce::putter) {
20     emit(filename, "1".to_string());
21 }
22
23 mod map_reduce {
24     use std::collections::HashMap;
25     use std::sync::mpsc::{channel, Sender};
26     use std::str;
27     use std::thread::Thread;
28
29     pub type putter<'a> = Box<FnMut(String, String) + 'a>;
30
31     pub type mapper = extern fn(String, putter);
32
33     enum ctrl_proto { find_reducer(Vec<u8>, Sender<int>), mapper_done, }
34
35     fn start_mappers(ctrl: Sender<ctrl_proto>, inputs: Vec<String>) {
36         for i in inputs.iter() {
37             let ctrl = ctrl.clone();
38             let i = i.clone();
39             Thread::spawn(move|| map_task(ctrl.clone(), i.clone()) );
40         }
41     }
42
43     fn map_task(ctrl: Sender<ctrl_proto>, input: String) {
44         let mut intermediates = HashMap::new();
45
46         fn emit(im: &mut HashMap<String, int>,
47                 ctrl: Sender<ctrl_proto>, key: String,
48                 _val: String) {
49             if im.contains_key(&key) {
50                 return;
51             }
52             let (tx, rx) = channel();
53             println!("sending find_reducer");
54             ctrl.send(ctrl_proto::find_reducer(key.as_bytes().to_vec(), tx)).unwrap();
55             println!("receiving");
56             let c = rx.recv().unwrap();
57             println!("{}", c);
58             im.insert(key, c);
59         }
60
61         let ctrl_clone = ctrl.clone();
62         ::map(input, box |a,b| emit(&mut intermediates, ctrl.clone(), a, b) );
63         ctrl_clone.send(ctrl_proto::mapper_done).unwrap();
64     }
65
66     pub fn map_reduce(inputs: Vec<String>) {
67         let (tx, rx) = channel();
68
69         // This task becomes the master control task. It spawns others
70         // to do the rest.
71
72         let mut reducers: HashMap<String, int>;
73
74         reducers = HashMap::new();
75
76         start_mappers(tx, inputs.clone());
77
78         let mut num_mappers = inputs.len() as int;
79
80         while num_mappers > 0 {
81             match rx.recv().unwrap() {
82               ctrl_proto::mapper_done => { num_mappers -= 1; }
83               ctrl_proto::find_reducer(k, cc) => {
84                 let mut c;
85                 match reducers.get(&str::from_utf8(
86                         k.as_slice()).unwrap().to_string()) {
87                   Some(&_c) => { c = _c; }
88                   None => { c = 0; }
89                 }
90                 cc.send(c).unwrap();
91               }
92             }
93         }
94     }
95 }
96
97 pub fn main() {
98     map_reduce::map_reduce(
99         vec!("../src/test/run-pass/hashmap-memory.rs".to_string()));
100 }