mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
seq: add is_first_iteration to avoid comparisons
Add the `is_first_iteration` Boolean variable to the `print_seq()` function in order to avoid unnecessary comparisons. Specifically, before this change, the `done_printing()` function was called twice on each iteration of the main loop. After this change, it is only called once per iteration. Furthermore, this change makes the `print_seq()` function similar in structure to the `print_seq_integers()` function. Co-authored-by: Jan Verbeek <jan.verbeek@posteo.nl>
This commit is contained in:
parent
5c97c1ccc4
commit
53a91be2df
1 changed files with 6 additions and 4 deletions
|
@ -287,7 +287,12 @@ fn print_seq(
|
||||||
let (first, increment, last) = range;
|
let (first, increment, last) = range;
|
||||||
let mut i = 0isize;
|
let mut i = 0isize;
|
||||||
let mut value = first + i as f64 * increment;
|
let mut value = first + i as f64 * increment;
|
||||||
|
let mut is_first_iteration = true;
|
||||||
while !done_printing(&value, &increment, &last) {
|
while !done_printing(&value, &increment, &last) {
|
||||||
|
if !is_first_iteration {
|
||||||
|
write!(stdout, "{}", separator)?;
|
||||||
|
}
|
||||||
|
is_first_iteration = false;
|
||||||
let istr = format!("{:.*}", largest_dec, value);
|
let istr = format!("{:.*}", largest_dec, value);
|
||||||
let ilen = istr.len();
|
let ilen = istr.len();
|
||||||
let before_dec = istr.find('.').unwrap_or(ilen);
|
let before_dec = istr.find('.').unwrap_or(ilen);
|
||||||
|
@ -299,11 +304,8 @@ fn print_seq(
|
||||||
write!(stdout, "{}", istr)?;
|
write!(stdout, "{}", istr)?;
|
||||||
i += 1;
|
i += 1;
|
||||||
value = first + i as f64 * increment;
|
value = first + i as f64 * increment;
|
||||||
if !done_printing(&value, &increment, &last) {
|
|
||||||
write!(stdout, "{}", separator)?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (first >= last && increment < 0f64) || (first <= last && increment > 0f64) {
|
if !is_first_iteration {
|
||||||
write!(stdout, "{}", terminator)?;
|
write!(stdout, "{}", terminator)?;
|
||||||
}
|
}
|
||||||
stdout.flush()?;
|
stdout.flush()?;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue