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