Skip to content

Commit 19f0a26

Browse files
committed
Use Box<[usize]> instead of Vec<usize> in Permutations
1 parent 99de003 commit 19f0a26

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/lazy_buffer.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use alloc::vec::Vec;
22
use core::borrow::BorrowMut;
3+
use core::ops::Deref as _;
34
use std::iter::Fuse;
45
use std::ops::Index;
56

@@ -148,6 +149,30 @@ impl ArrayOrVecHelper for Vec<usize> {
148149
}
149150
}
150151

152+
impl ArrayOrVecHelper for Box<[usize]> {
153+
type Item<T> = Vec<T>;
154+
type Length = usize;
155+
156+
fn extract_item<I: Iterator>(&self, pool: &LazyBuffer<I>) -> Self::Item<I::Item>
157+
where
158+
I::Item: Clone,
159+
{
160+
pool.get_at(self)
161+
}
162+
163+
fn item_from_fn<T, F: Fn(usize) -> T>(len: Self::Length, f: F) -> Self::Item<T> {
164+
(0..len).map(f).collect()
165+
}
166+
167+
fn len(&self) -> Self::Length {
168+
self.deref().len()
169+
}
170+
171+
fn from_fn<F: Fn(usize) -> usize>(k: Self::Length, f: F) -> Self {
172+
(0..k).map(f).collect()
173+
}
174+
}
175+
151176
impl<const K: usize> ArrayOrVecHelper for [usize; K] {
152177
type Item<T> = [T; K];
153178
type Length = ConstUsize<K>;

src/permutations.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use alloc::boxed::Box;
2-
use alloc::vec::Vec;
32
use std::fmt;
43
use std::iter::FusedIterator;
54

@@ -17,7 +16,7 @@ pub struct PermutationsGeneric<I: Iterator, Idx: ArrayOrVecHelper> {
1716
///
1817
/// See [`.permutations()`](crate::Itertools::permutations) for
1918
/// more information.
20-
pub type Permutations<I> = PermutationsGeneric<I, Vec<usize>>;
19+
pub type Permutations<I> = PermutationsGeneric<I, Box<[usize]>>;
2120

2221
impl<I, Idx> Clone for PermutationsGeneric<I, Idx>
2322
where

0 commit comments

Comments
 (0)