Skip to content

Commit e2524c6

Browse files
committed
fix
1 parent 0851770 commit e2524c6

File tree

5 files changed

+39
-28
lines changed

5 files changed

+39
-28
lines changed

week12/rowlab/src/aggregation.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl StationAggregation {
4040
}
4141

4242
/// Updates the aggregation with a new measurement.
43-
///
43+
///
4444
/// TODO(student): Is processing measurements one-by-one the best way to compute aggregations?
4545
/// Remember that you are allowed to add other methods in this implementation block!
4646
pub fn add_measurement(&mut self, measurement: f64) {
@@ -62,7 +62,7 @@ impl StationAggregation {
6262
/// might best be located.
6363
#[derive(Debug)]
6464
pub struct AggregationResults {
65-
pub(crate) results: HashMap<String, StationAggregation>,
65+
results: HashMap<String, StationAggregation>,
6666
}
6767

6868
impl AggregationResults {
@@ -76,6 +76,20 @@ impl AggregationResults {
7676
}
7777
}
7878

79+
// Updates the metrics for the given station with a measurement.
80+
pub fn insert_measurement(&mut self, station: &str, measurement: f64) {
81+
// We don't use the `entry` API in the `Some` case since it would require us to always turn
82+
// `station` into an owned `String`, since `.entry()` requires an owned type.
83+
match self.results.get_mut(station) {
84+
Some(val) => val.add_measurement(measurement),
85+
None => self
86+
.results
87+
.entry(station.to_string())
88+
.or_default()
89+
.add_measurement(measurement),
90+
}
91+
}
92+
7993
/// Converts the aggregations results into a [`String`].
8094
///
8195
/// TODO(student): Is this function efficient? Is there another way you could do this? Note that

week12/rowlab/src/lib.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn aggregate(measurements_path: impl AsRef<Path>) -> AggregationResults {
2020
let measurements = File::open(measurements_path).expect("unable to read measurements");
2121
let buf_reader = std::io::BufReader::new(measurements);
2222

23-
let mut aggr = AggregationResults::new();
23+
let mut results = AggregationResults::new();
2424

2525
for line_res in buf_reader.lines() {
2626
let line = line_res.expect("Was unable to read the line");
@@ -33,17 +33,8 @@ pub fn aggregate(measurements_path: impl AsRef<Path>) -> AggregationResults {
3333
.parse::<f64>()
3434
.expect("unable to parse temperature");
3535

36-
// We don't use the `entry` API in the `Some` case since it would require us to always turn
37-
// `station` into an owned `String`, since `.entry()` requires an owned type.
38-
match aggr.results.get_mut(station) {
39-
Some(val) => val.add_measurement(parsed_temperature),
40-
None => aggr
41-
.results
42-
.entry(station.to_string())
43-
.or_default()
44-
.add_measurement(parsed_temperature),
45-
}
36+
results.insert_measurement(station, parsed_temperature);
4637
}
4738

48-
aggr
39+
results
4940
}

week12/rowlab_ref/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ indicatif = "0.17.11"
1010
itertools = "0.14.0"
1111
rand = "0.9.0"
1212
rand_distr = "0.5.1"
13+
rayon = "1.10.0"
1314
regex = "1.11.1"

week12/rowlab_ref/src/aggregation.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl StationAggregation {
4040
}
4141

4242
/// Updates the aggregation with a new measurement.
43-
///
43+
///
4444
/// TODO(student): Is processing measurements one-by-one the best way to compute aggregations?
4545
/// Remember that you are allowed to add other methods in this implementation block!
4646
pub fn add_measurement(&mut self, measurement: f64) {
@@ -62,7 +62,7 @@ impl StationAggregation {
6262
/// might best be located.
6363
#[derive(Debug)]
6464
pub struct AggregationResults {
65-
pub(crate) results: HashMap<String, StationAggregation>,
65+
results: HashMap<String, StationAggregation>,
6666
}
6767

6868
impl AggregationResults {
@@ -76,6 +76,20 @@ impl AggregationResults {
7676
}
7777
}
7878

79+
// Updates the metrics for the given station with a measurement.
80+
pub fn insert_measurement(&mut self, station: &str, measurement: f64) {
81+
// We don't use the `entry` API in the `Some` case since it would require us to always turn
82+
// `station` into an owned `String`, since `.entry()` requires an owned type.
83+
match self.results.get_mut(station) {
84+
Some(val) => val.add_measurement(measurement),
85+
None => self
86+
.results
87+
.entry(station.to_string())
88+
.or_default()
89+
.add_measurement(measurement),
90+
}
91+
}
92+
7993
/// Converts the aggregations results into a [`String`].
8094
///
8195
/// TODO(student): Is this function efficient? Is there another way you could do this? Note that

week12/rowlab_ref/src/lib.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn aggregate(measurements_path: impl AsRef<Path>) -> AggregationResults {
2020
let measurements = File::open(measurements_path).expect("unable to read measurements");
2121
let buf_reader = std::io::BufReader::new(measurements);
2222

23-
let mut aggr = AggregationResults::new();
23+
let mut results = AggregationResults::new();
2424

2525
for line_res in buf_reader.lines() {
2626
let line = line_res.expect("Was unable to read the line");
@@ -33,17 +33,8 @@ pub fn aggregate(measurements_path: impl AsRef<Path>) -> AggregationResults {
3333
.parse::<f64>()
3434
.expect("unable to parse temperature");
3535

36-
// We don't use the `entry` API in the `Some` case since it would require us to always turn
37-
// `station` into an owned `String`, since `.entry()` requires an owned type.
38-
match aggr.results.get_mut(station) {
39-
Some(val) => val.add_measurement(parsed_temperature),
40-
None => aggr
41-
.results
42-
.entry(station.to_string())
43-
.or_default()
44-
.add_measurement(parsed_temperature),
45-
}
36+
results.insert_measurement(station, parsed_temperature);
4637
}
4738

48-
aggr
39+
results
4940
}

0 commit comments

Comments
 (0)