Finish Parallel Letter Frequency

This commit is contained in:
Blizzard Finnegan 2023-08-22 10:47:31 -04:00
parent bbe4e7fbc3
commit 56252ddfd3
Signed by: blizzardfinnegan
GPG key ID: 61C1E13067E0018E
2 changed files with 60 additions and 17 deletions

View file

@ -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> {
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),
let mut output = HashMap::new();
let mut threads:Vec<JoinHandle<HashMap<char,usize>>> = Vec::new();
let mut input_strings:Vec<Vec<String>> = Vec::new();
let mut counter = 0;
for section in input.iter(){
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),
// }
//);
}

View file

@ -44,7 +44,6 @@ fn test_no_texts() {
}
#[test]
#[ignore]
fn test_one_letter() {
let mut hm = HashMap::new();
hm.insert('a', 1);
@ -52,7 +51,6 @@ fn test_one_letter() {
}
#[test]
#[ignore]
fn test_case_insensitivity() {
let mut hm = HashMap::new();
hm.insert('a', 2);
@ -60,14 +58,12 @@ fn test_case_insensitivity() {
}
#[test]
#[ignore]
fn test_many_empty_lines() {
let v = vec![""; 1000];
assert_eq!(frequency::frequency(&v[..], 4), HashMap::new());
}
#[test]
#[ignore]
fn test_many_times_same_text() {
let v = vec!["abc"; 1000];
let mut hm = HashMap::new();
@ -78,19 +74,16 @@ fn test_many_times_same_text() {
}
#[test]
#[ignore]
fn test_punctuation_doesnt_count() {
assert!(!frequency::frequency(&WILHELMUS, 4).contains_key(&','));
}
#[test]
#[ignore]
fn test_numbers_dont_count() {
assert!(!frequency::frequency(&["Testing, 1, 2, 3"], 4).contains_key(&'1'));
}
#[test]
#[ignore]
fn test_all_three_anthems_1_worker() {
let mut v = Vec::new();
for anthem in [ODE_AN_DIE_FREUDE, WILHELMUS, STAR_SPANGLED_BANNER].iter() {
@ -105,7 +98,6 @@ fn test_all_three_anthems_1_worker() {
}
#[test]
#[ignore]
fn test_all_three_anthems_3_workers() {
let mut v = Vec::new();
for anthem in [ODE_AN_DIE_FREUDE, WILHELMUS, STAR_SPANGLED_BANNER].iter() {
@ -120,7 +112,6 @@ fn test_all_three_anthems_3_workers() {
}
#[test]
#[ignore]
fn test_non_integer_multiple_of_threads() {
let v = vec!["abc"; 999];
let mut hm = HashMap::new();