Finish Parallel Letter Frequency
This commit is contained in:
parent
bbe4e7fbc3
commit
56252ddfd3
2 changed files with 60 additions and 17 deletions
|
@ -1,12 +1,64 @@
|
||||||
use std::collections::HashMap;
|
use std::{collections::HashMap, thread::{JoinHandle, self}};
|
||||||
|
|
||||||
pub fn frequency(input: &[&str], worker_count: usize) -> HashMap<char, usize> {
|
pub fn frequency(input: &[&str], worker_count: usize) -> HashMap<char, usize> {
|
||||||
unimplemented!(
|
let mut output = HashMap::new();
|
||||||
"Count the frequency of letters in the given input '{:?}'. Ensure that you are using {} to process the input.",
|
let mut threads:Vec<JoinHandle<HashMap<char,usize>>> = Vec::new();
|
||||||
input,
|
let mut input_strings:Vec<Vec<String>> = Vec::new();
|
||||||
match worker_count {
|
let mut counter = 0;
|
||||||
1 => "1 worker".to_string(),
|
for section in input.iter(){
|
||||||
_ => format!("{} workers", worker_count),
|
match input_strings.get_mut(counter){
|
||||||
|
Some(existing_vec) => {
|
||||||
|
existing_vec.push(section.to_ascii_lowercase());
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
let mut temp = Vec::new();
|
||||||
|
temp.push(section.to_ascii_lowercase());
|
||||||
|
input_strings.push(temp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
);
|
counter = counter + 1 % worker_count;
|
||||||
|
}
|
||||||
|
for mut vec in input_strings.into_iter(){
|
||||||
|
threads.push(
|
||||||
|
thread::spawn(move || {
|
||||||
|
let mut thread_output = HashMap::new();
|
||||||
|
for string in vec.iter_mut(){
|
||||||
|
for char in string.chars(){
|
||||||
|
if !char.is_alphabetic(){ continue; }
|
||||||
|
match thread_output.get_mut(&char){
|
||||||
|
Some(count) => {
|
||||||
|
*count += 1;
|
||||||
|
},
|
||||||
|
None =>{
|
||||||
|
thread_output.insert(char.clone(), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
thread_output
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
for thread in threads{
|
||||||
|
let thread_output = thread.join().unwrap();
|
||||||
|
for char in thread_output.keys(){
|
||||||
|
match output.get_mut(char){
|
||||||
|
Some(count) => {
|
||||||
|
*count += thread_output.get(char).unwrap();
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
output.insert(*char, *thread_output.get(char).unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
output
|
||||||
|
//unimplemented!(
|
||||||
|
// "Count the frequency of letters in the given input '{:?}'. Ensure that you are using {} to process the input.",
|
||||||
|
// input,
|
||||||
|
// match worker_count {
|
||||||
|
// 1 => "1 worker".to_string(),
|
||||||
|
// _ => format!("{} workers", worker_count),
|
||||||
|
// }
|
||||||
|
//);
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,6 @@ fn test_no_texts() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_one_letter() {
|
fn test_one_letter() {
|
||||||
let mut hm = HashMap::new();
|
let mut hm = HashMap::new();
|
||||||
hm.insert('a', 1);
|
hm.insert('a', 1);
|
||||||
|
@ -52,7 +51,6 @@ fn test_one_letter() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_case_insensitivity() {
|
fn test_case_insensitivity() {
|
||||||
let mut hm = HashMap::new();
|
let mut hm = HashMap::new();
|
||||||
hm.insert('a', 2);
|
hm.insert('a', 2);
|
||||||
|
@ -60,14 +58,12 @@ fn test_case_insensitivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_many_empty_lines() {
|
fn test_many_empty_lines() {
|
||||||
let v = vec![""; 1000];
|
let v = vec![""; 1000];
|
||||||
assert_eq!(frequency::frequency(&v[..], 4), HashMap::new());
|
assert_eq!(frequency::frequency(&v[..], 4), HashMap::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_many_times_same_text() {
|
fn test_many_times_same_text() {
|
||||||
let v = vec!["abc"; 1000];
|
let v = vec!["abc"; 1000];
|
||||||
let mut hm = HashMap::new();
|
let mut hm = HashMap::new();
|
||||||
|
@ -78,19 +74,16 @@ fn test_many_times_same_text() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_punctuation_doesnt_count() {
|
fn test_punctuation_doesnt_count() {
|
||||||
assert!(!frequency::frequency(&WILHELMUS, 4).contains_key(&','));
|
assert!(!frequency::frequency(&WILHELMUS, 4).contains_key(&','));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_numbers_dont_count() {
|
fn test_numbers_dont_count() {
|
||||||
assert!(!frequency::frequency(&["Testing, 1, 2, 3"], 4).contains_key(&'1'));
|
assert!(!frequency::frequency(&["Testing, 1, 2, 3"], 4).contains_key(&'1'));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_all_three_anthems_1_worker() {
|
fn test_all_three_anthems_1_worker() {
|
||||||
let mut v = Vec::new();
|
let mut v = Vec::new();
|
||||||
for anthem in [ODE_AN_DIE_FREUDE, WILHELMUS, STAR_SPANGLED_BANNER].iter() {
|
for anthem in [ODE_AN_DIE_FREUDE, WILHELMUS, STAR_SPANGLED_BANNER].iter() {
|
||||||
|
@ -105,7 +98,6 @@ fn test_all_three_anthems_1_worker() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_all_three_anthems_3_workers() {
|
fn test_all_three_anthems_3_workers() {
|
||||||
let mut v = Vec::new();
|
let mut v = Vec::new();
|
||||||
for anthem in [ODE_AN_DIE_FREUDE, WILHELMUS, STAR_SPANGLED_BANNER].iter() {
|
for anthem in [ODE_AN_DIE_FREUDE, WILHELMUS, STAR_SPANGLED_BANNER].iter() {
|
||||||
|
@ -120,7 +112,6 @@ fn test_all_three_anthems_3_workers() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_non_integer_multiple_of_threads() {
|
fn test_non_integer_multiple_of_threads() {
|
||||||
let v = vec!["abc"; 999];
|
let v = vec!["abc"; 999];
|
||||||
let mut hm = HashMap::new();
|
let mut hm = HashMap::new();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue