Skip to content

Commit 3373c7a

Browse files
Itertools::tail: return VecDeque rather than Vec
`rotate_left` is more efficient on `VecDeque` than on a slice. `VecDeque::from(vec)` is O(1) on recent rust. Co-Authored-By: scottmcm <[email protected]>
1 parent 0578354 commit 3373c7a

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/lib.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ extern crate core as std;
5555
extern crate alloc;
5656

5757
#[cfg(feature = "use_alloc")]
58-
use alloc::{string::String, vec::Vec};
58+
use alloc::{collections::VecDeque, string::String, vec::Vec};
5959

6060
pub use either::Either;
6161

@@ -72,6 +72,8 @@ use std::fmt::Write;
7272
use std::hash::Hash;
7373
use std::iter::{once, IntoIterator};
7474
#[cfg(feature = "use_alloc")]
75+
type VecDequeIntoIter<T> = alloc::collections::vec_deque::IntoIter<T>;
76+
#[cfg(feature = "use_alloc")]
7577
type VecIntoIter<T> = alloc::vec::IntoIter<T>;
7678
use std::iter::FromIterator;
7779

@@ -3146,8 +3148,10 @@ pub trait Itertools: Iterator {
31463148

31473149
/// Consumes the iterator and return an iterator of the last `n` elements.
31483150
///
3149-
/// The iterator, if directly collected to a `Vec`, is converted
3151+
/// The iterator, if directly collected to a `VecDeque`, is converted
31503152
/// without any extra copying or allocation cost.
3153+
/// If directly collected to a `Vec`, it may need some data movement
3154+
/// but no re-allocation.
31513155
///
31523156
/// ```
31533157
/// use itertools::{assert_equal, Itertools};
@@ -3167,20 +3171,22 @@ pub trait Itertools: Iterator {
31673171
/// `.rev().take(n).rev()` to have a similar result (lazy and non-allocating)
31683172
/// without consuming the entire iterator.
31693173
#[cfg(feature = "use_alloc")]
3170-
fn tail(self, n: usize) -> VecIntoIter<Self::Item>
3174+
fn tail(self, n: usize) -> VecDequeIntoIter<Self::Item>
31713175
where
31723176
Self: Sized,
31733177
{
31743178
match n {
31753179
0 => {
31763180
self.last();
3177-
Vec::new()
3181+
VecDeque::new()
31783182
}
31793183
1 => self.last().into_iter().collect(),
31803184
_ => {
31813185
// Skip the starting part of the iterator if possible.
31823186
let (low, _) = self.size_hint();
31833187
let mut iter = self.fuse().skip(low.saturating_sub(n));
3188+
// TODO: If VecDeque has a more efficient method than
3189+
// `.pop_front();.push_back(val)` in the future then maybe revisit this.
31843190
let mut data: Vec<_> = iter.by_ref().take(n).collect();
31853191
// Update `data` cyclically.
31863192
let idx = iter.fold(0, |i, val| {
@@ -3191,7 +3197,8 @@ pub trait Itertools: Iterator {
31913197
i + 1
31923198
}
31933199
});
3194-
// Respect the insertion order.
3200+
// Respect the insertion order, efficiently.
3201+
let mut data = VecDeque::from(data);
31953202
data.rotate_left(idx);
31963203
data
31973204
}

0 commit comments

Comments
 (0)