-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da886b5
commit ac883e3
Showing
4 changed files
with
82 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,26 @@ | ||
use crate::compute::CityKey; | ||
use crate::record::Record; | ||
use fxhash::FxHashMap; | ||
use std::sync::mpsc::Receiver; | ||
|
||
pub fn reduce(rx: Receiver<FxHashMap<String, Record>>) -> Vec<(String, Record)> { | ||
pub fn reduce(rx: Receiver<FxHashMap<CityKey, Record>>) -> Vec<Record> { | ||
let mut hmap = FxHashMap::default(); | ||
while let Ok(stats) = rx.recv() { | ||
merge_records(&mut hmap, stats); | ||
} | ||
to_sorted_vec(hmap) | ||
} | ||
|
||
fn merge_records(dst: &mut FxHashMap<String, Record>, src: FxHashMap<String, Record>) { | ||
fn merge_records(dst: &mut FxHashMap<CityKey, Record>, src: FxHashMap<CityKey, Record>) { | ||
for (city, new_record) in src { | ||
dst.entry(city) | ||
.and_modify(|existing_record: &mut Record| existing_record.merge(&new_record)) | ||
.or_insert(new_record); | ||
} | ||
} | ||
|
||
fn to_sorted_vec(hmap: FxHashMap<String, Record>) -> Vec<(String, Record)> { | ||
let mut v = hmap.into_iter().collect::<Vec<_>>(); | ||
v.sort_unstable_by(|a, b| a.0.cmp(&b.0)); | ||
fn to_sorted_vec(hmap: FxHashMap<CityKey, Record>) -> Vec<Record> { | ||
let mut v = hmap.into_values().collect::<Vec<_>>(); | ||
v.sort_unstable_by(|a, b| a.name.cmp(&b.name)); | ||
v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters