diff options
| author | Kevin Leimkuhler <kevin@kleimkuhler.com> | 2018-10-14 20:45:28 -0700 |
|---|---|---|
| committer | Kevin Leimkuhler <kevin@kleimkuhler.com> | 2019-01-17 22:34:43 -0800 |
| commit | 67729b4040a17508640af17c21876650b34ff6de (patch) | |
| tree | aa209c8d5ae1fca2707e72c92a05cae0b163e6d8 | |
| parent | ccc027eff7604bf75f1ad490f415a338e44c1038 (diff) | |
| download | rust-67729b4040a17508640af17c21876650b34ff6de.tar.gz rust-67729b4040a17508640af17c21876650b34ff6de.zip | |
Compare pairs with `slice::windows`
| -rw-r--r-- | src/libcore/iter/iterator.rs | 5 | ||||
| -rw-r--r-- | src/libcore/slice/mod.rs | 12 |
2 files changed, 9 insertions, 8 deletions
diff --git a/src/libcore/iter/iterator.rs b/src/libcore/iter/iterator.rs index 4035ad6b77c..ac21586c0b8 100644 --- a/src/libcore/iter/iterator.rs +++ b/src/libcore/iter/iterator.rs @@ -2655,7 +2655,10 @@ pub trait Iterator { }; while let Some(curr) = self.next() { - if compare(&last, &curr).map(|o| o == Ordering::Greater).unwrap_or(true) { + if compare(&last, &curr) + .map(|o| o == Ordering::Greater) + .unwrap_or(true) + { return false; } last = curr; diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index 9c82aca9ab2..11ce8115d30 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -2293,13 +2293,11 @@ impl<T> [T] { where F: FnMut(&T, &T) -> Option<Ordering> { - let len = self.len(); - if len <= 1 { - return true; - } - - for i in 1..len { - if compare(&self[i - 1], &self[i]).map(|o| o == Ordering::Greater).unwrap_or(true) { + for pair in self.windows(2) { + if compare(&pair[0], &pair[1]) + .map(|o| o == Ordering::Greater) + .unwrap_or(true) + { return false; } } |
