Skip to content

Commit

Permalink
Optimize is_sorted for Range and RangeInclusive
Browse files Browse the repository at this point in the history
The `Step` trait guarantees that `Range<impl Step>` yields items in
sorted order.  We can override the `Iterator::is_sorted` method based on
this guarantee, as we already do for `Iterator::min` and `max`.
  • Loading branch information
mbrubeck committed Sep 28, 2021
1 parent 8f8092c commit 830ecbd
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions library/core/src/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,11 @@ impl<A: Step> Iterator for ops::Range<A> {
self.next_back()
}

#[inline]
fn is_sorted(self) -> bool {
true
}

#[inline]
#[doc(hidden)]
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item
Expand Down Expand Up @@ -1095,6 +1100,11 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
fn max(mut self) -> Option<A> {
self.next_back()
}

#[inline]
fn is_sorted(self) -> bool {
true
}
}

#[stable(feature = "inclusive_range", since = "1.26.0")]
Expand Down

0 comments on commit 830ecbd

Please sign in to comment.