collections: Simplify VecDeque::is_empty
Improve is_empty on the VecDeque and its iterators by just comparing tail and head; this saves a few instructions (to be able to remove the `& (size - 1)` computation, it would have to know that size is a power of two).
This commit is contained in:
parent
7ba762253c
commit
343b4c321d
2 changed files with 37 additions and 4 deletions
|
@ -810,7 +810,7 @@ impl<T> VecDeque<T> {
|
|||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.len() == 0
|
||||
self.tail == self.head
|
||||
}
|
||||
|
||||
/// Create a draining iterator that removes the specified range in the
|
||||
|
@ -1916,7 +1916,11 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
|
|||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, T> ExactSizeIterator for Iter<'a, T> {}
|
||||
impl<'a, T> ExactSizeIterator for Iter<'a, T> {
|
||||
fn is_empty(&self) -> bool {
|
||||
self.head == self.tail
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "fused", issue = "35602")]
|
||||
impl<'a, T> FusedIterator for Iter<'a, T> {}
|
||||
|
@ -1980,7 +1984,11 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
|
|||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<'a, T> ExactSizeIterator for IterMut<'a, T> {}
|
||||
impl<'a, T> ExactSizeIterator for IterMut<'a, T> {
|
||||
fn is_empty(&self) -> bool {
|
||||
self.head == self.tail
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "fused", issue = "35602")]
|
||||
impl<'a, T> FusedIterator for IterMut<'a, T> {}
|
||||
|
@ -2017,7 +2025,11 @@ impl<T> DoubleEndedIterator for IntoIter<T> {
|
|||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T> ExactSizeIterator for IntoIter<T> {}
|
||||
impl<T> ExactSizeIterator for IntoIter<T> {
|
||||
fn is_empty(&self) -> bool {
|
||||
self.inner.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "fused", issue = "35602")]
|
||||
impl<T> FusedIterator for IntoIter<T> {}
|
||||
|
|
|
@ -1007,3 +1007,24 @@ fn assert_covariance() {
|
|||
d
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_empty() {
|
||||
let mut v = VecDeque::<i32>::new();
|
||||
assert!(v.is_empty());
|
||||
assert!(v.iter().is_empty());
|
||||
assert!(v.iter_mut().is_empty());
|
||||
v.extend(&[2, 3, 4]);
|
||||
assert!(!v.is_empty());
|
||||
assert!(!v.iter().is_empty());
|
||||
assert!(!v.iter_mut().is_empty());
|
||||
while let Some(_) = v.pop_front() {
|
||||
assert_eq!(v.is_empty(), v.len() == 0);
|
||||
assert_eq!(v.iter().is_empty(), v.iter().len() == 0);
|
||||
assert_eq!(v.iter_mut().is_empty(), v.iter_mut().len() == 0);
|
||||
}
|
||||
assert!(v.is_empty());
|
||||
assert!(v.iter().is_empty());
|
||||
assert!(v.iter_mut().is_empty());
|
||||
assert!(v.into_iter().is_empty());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue