Skip to content

Commit d8de4b8

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

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/lazy_buffer.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use alloc::boxed::Box;
12
use alloc::vec::Vec;
23
use core::borrow::BorrowMut;
4+
use core::ops::Deref as _;
35
use std::iter::Fuse;
46
use std::ops::Index;
57

@@ -148,6 +150,30 @@ impl ArrayOrVecHelper for Vec<usize> {
148150
}
149151
}
150152

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