-
Notifications
You must be signed in to change notification settings - Fork 39
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
Rust: Express the main loop through an iterator #20
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,24 +25,22 @@ impl Connection { | |
} | ||
|
||
fn csa_main_loop(timetable: &[Connection], arrival_station: usize, earliest_arrival: &mut [u32], in_connection: &mut [usize]) { | ||
let mut earliest = std::u32::MAX; | ||
|
||
for (i, connection) in timetable.iter().enumerate() { | ||
timetable.iter().enumerate().fold(std::u32::MAX, |earliest, (i, connection)| { | ||
if connection.departure_timestamp >= earliest_arrival[connection.departure_station] && | ||
connection.arrival_timestamp < earliest_arrival[connection.arrival_station] { | ||
connection.arrival_timestamp < earliest_arrival[connection.arrival_station] { | ||
earliest_arrival[connection.arrival_station] = connection.arrival_timestamp; | ||
in_connection[connection.arrival_station] = i; | ||
} | ||
|
||
if connection.arrival_station == arrival_station && connection.arrival_timestamp < earliest { | ||
earliest = connection.arrival_timestamp; | ||
} | ||
} else if connection.arrival_timestamp > earliest { | ||
break; | ||
if connection.arrival_station == arrival_station && connection.arrival_timestamp < earliest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is done more than necessary. |
||
connection.arrival_timestamp | ||
} else { | ||
earliest | ||
} | ||
} | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You loose this early break |
||
} | ||
|
||
fn csa_print_result(timetable: &Vec<Connection>, in_connection: &[usize], arrival_station: usize) { | ||
fn csa_print_result(timetable: &[Connection], in_connection: &[usize], arrival_station: usize) { | ||
if in_connection[arrival_station] == std::u32::MAX as usize { | ||
println!("NO_SOLUTION"); | ||
} else { | ||
|
@@ -62,7 +60,7 @@ fn csa_print_result(timetable: &Vec<Connection>, in_connection: &[usize], arriva | |
println!(""); | ||
} | ||
|
||
fn csa_compute(timetable: &Vec<Connection>, departure_station: usize, arrival_station: usize, departure_time: u32) | ||
fn csa_compute(timetable: &[Connection], departure_station: usize, arrival_station: usize, departure_time: u32) | ||
{ | ||
let mut in_connection = vec!(std::u32::MAX as usize; MAX_STATIONS); | ||
let mut earliest_arrival = vec!(std::u32::MAX; MAX_STATIONS); | ||
|
@@ -83,7 +81,7 @@ fn main() { | |
let timetable = buffered_in.map(|r| { r.ok().expect("failed to read connection line") }) | ||
.take_while(|l| { !l.is_empty() }) | ||
.map(|l| { Connection::parse(l.trim_right()) }) | ||
.collect(); | ||
.collect::<Vec<Connection>>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can |
||
|
||
// Responding to requests from stdin | ||
|
||
|
@@ -100,6 +98,6 @@ fn main() { | |
let arrival_station = params[1] as usize; | ||
let departure_time = params[2]; | ||
|
||
csa_compute(&timetable, departure_station, arrival_station, departure_time); | ||
csa_compute(&timetable[..], departure_station, arrival_station, departure_time); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's not needed, no? And using a collect for collecting a Vec<()> is code obfuscation and useless memory allocation. Better to use a for loop, it's not evil. |
||
}).collect::<Vec<_>>(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using fold in this case is particularly unnatural. The goal of fold is to pass a value throw a stream using a function, not removing a side effect. If you don't use the result of fold, please don't use it.