Skip to content

Commit f7d557d

Browse files
kinto-bPhilippe-Cholet
authored andcommitted
Implement helper on LazyBuffer for getting multiple elements by index
1 parent ed695af commit f7d557d

File tree

4 files changed

+16
-21
lines changed

4 files changed

+16
-21
lines changed

src/combinations.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,10 @@ where
162162
return None;
163163
}
164164

165-
Some(self.indices.iter().map(|i| self.pool[*i].clone()).collect())
165+
Some(self.pool.get_at(&self.indices))
166166
}
167167

168168
fn nth(&mut self, n: usize) -> Option<Self::Item> {
169-
if n == 0 {
170-
return self.next();
171-
}
172-
173169
let done = if self.first {
174170
self.init()
175171
} else {
@@ -186,7 +182,7 @@ where
186182
}
187183
}
188184

189-
Some(self.indices.iter().map(|i| self.pool[*i].clone()).collect())
185+
Some(self.pool.get_at(&self.indices))
190186
}
191187

192188
fn size_hint(&self) -> (usize, Option<usize>) {

src/combinations_with_replacement.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,6 @@ where
3030
debug_fmt_fields!(CombinationsWithReplacement, indices, pool, first);
3131
}
3232

33-
impl<I> CombinationsWithReplacement<I>
34-
where
35-
I: Iterator,
36-
I::Item: Clone,
37-
{
38-
/// Map the current mask over the pool to get an output combination
39-
fn current(&self) -> Vec<I::Item> {
40-
self.indices.iter().map(|i| self.pool[*i].clone()).collect()
41-
}
42-
}
43-
4433
/// Create a new `CombinationsWithReplacement` from a clonable iterator.
4534
pub fn combinations_with_replacement<I>(iter: I, k: usize) -> CombinationsWithReplacement<I>
4635
where
@@ -72,7 +61,7 @@ where
7261
// Otherwise, yield the initial state
7362
} else {
7463
self.first = false;
75-
Some(self.current())
64+
Some(self.pool.get_at(&self.indices))
7665
};
7766
}
7867

@@ -97,7 +86,7 @@ where
9786
for indices_index in increment_from..self.indices.len() {
9887
self.indices[indices_index] = increment_value;
9988
}
100-
Some(self.current())
89+
Some(self.pool.get_at(&self.indices))
10190
}
10291
// Otherwise, we're done
10392
None => None,

src/lazy_buffer.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ where
5151
}
5252
}
5353

54+
impl<I> LazyBuffer<I>
55+
where
56+
I: Iterator,
57+
I::Item: Clone,
58+
{
59+
pub fn get_at(&self, indices: &[usize]) -> Vec<I::Item> {
60+
indices.iter().map(|i| self.buffer[*i].clone()).collect()
61+
}
62+
}
63+
5464
impl<I, J> Index<J> for LazyBuffer<I>
5565
where
5666
I: Iterator,

src/permutations.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ where
9999
return None;
100100
}
101101
}
102-
let item = indices[0..*k].iter().map(|&i| vals[i].clone()).collect();
102+
let item = vals.get_at(&indices[0..*k]);
103103
*state = PermutationState::Loaded { indices, cycles };
104104
Some(item)
105105
}
@@ -110,7 +110,7 @@ where
110110
return None;
111111
}
112112
let k = cycles.len();
113-
Some(indices[0..k].iter().map(|&i| vals[i].clone()).collect())
113+
Some(vals.get_at(&indices[0..k]))
114114
}
115115
PermutationState::End => None,
116116
}

0 commit comments

Comments
 (0)