Skip to content

parallel-letter-frequency, tournament, word-count: Use Entry#or_insert #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions exercises/grade-school/example.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::collections::hash_map::Entry;

pub struct School {
grades: HashMap<u32, Vec<String>>
Expand All @@ -11,14 +10,9 @@ impl School {
}

pub fn add(&mut self, grade: u32, student: &str) {
match self.grades.entry(grade) {
Entry::Vacant(view) => { view.insert(vec![student.to_string()]); }
Entry::Occupied(mut view) => {
let l = view.get_mut();
l.push(student.to_string());
l.sort();
},
};
let mut entry = self.grades.entry(grade).or_insert(Vec::new());
entry.push(student.to_string());
entry.sort();
}

pub fn grades(&self) -> Vec<u32> {
Expand Down
15 changes: 2 additions & 13 deletions exercises/parallel-letter-frequency/example.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::thread;
use std::sync::mpsc::channel;

Expand Down Expand Up @@ -36,12 +35,7 @@ pub fn frequency(texts: &[&str], num_workers: usize) -> HashMap<char, usize> {
for _ in 0..num_workers {
let part_results = rx.recv().unwrap();
for (c, n) in part_results.into_iter() {
match results.entry(c) {
Entry::Vacant(view) => { view.insert(n); },
Entry::Occupied(mut view) => {
*view.get_mut() += n;
}
}
*results.entry(c).or_insert(0) += n;
}
}
results
Expand All @@ -52,12 +46,7 @@ fn count(lines: Vec<String>) -> HashMap<char, usize> {
for line in lines.iter() {
for c in line.chars() {
if c.is_alphabetic() {
match results.entry(c.to_lowercase().next().unwrap()) {
Entry::Vacant(view) => { view.insert(1); },
Entry::Occupied(mut view) => {
*view.get_mut() += 1;
}
}
*results.entry(c.to_lowercase().next().unwrap()).or_insert(0) += 1;
}
}
}
Expand Down
12 changes: 1 addition & 11 deletions exercises/tournament/example.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::cmp::Ordering::Equal;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::fs::File;
use std::io::{BufRead, BufReader, Write, Result};
use std::path::Path;
Expand Down Expand Up @@ -87,14 +86,5 @@ fn write_tally(results: &HashMap<String, TeamResult>, output_filename: &Path) ->
}

fn add_game_result(results: &mut HashMap<String, TeamResult>, team: String, result: GameResult) {
match results.entry(team) {
Entry::Vacant(entry) => {
let mut tr = TeamResult::new();
tr.add_game_result(result);
entry.insert(tr);
}
Entry::Occupied(mut entry) => {
(*entry.get_mut()).add_game_result(result);
}
};
results.entry(team).or_insert(TeamResult::new()).add_game_result(result);
}
8 changes: 1 addition & 7 deletions exercises/word-count/example.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::collections::hash_map::Entry;

fn lowercase(word: &str) -> String {
let mut lower = String::with_capacity(word.len());
Expand All @@ -16,12 +15,7 @@ pub fn word_count(input: &str) -> HashMap<String, u32> {
let lower = lowercase(input);
let slice: &str = lower.as_ref();
for word in slice.split(|c: char| !c.is_alphanumeric()).filter(|s| !s.is_empty()) {
match map.entry(word.to_string()) {
Entry::Vacant(entry) => { entry.insert(1); },
Entry::Occupied(mut entry) => {
*entry.get_mut() += 1;
}
};
*map.entry(word.to_string()).or_insert(0) += 1;
}
map
}