Commit 31dbdca4 authored by tgerstel's avatar tgerstel
Browse files

replace pool iter with labels method

parent b0761e6c
......@@ -94,7 +94,19 @@ impl<T> Pool<T> {
self.size -= 1;
}
/// Retun the index of the current hole.
/// Return a `Vec` of `Label`s contained in the `Pool`.
pub fn labels(&self) -> Vec<Label<T>> {
self.elements
.iter()
.enumerate()
.filter_map(|(i, elem)| match elem {
Element::Object(_) => Some(Label::from(i)),
Element::Hole(_) => None,
})
.collect()
}
/// Return the index of the current hole.
pub fn current_hole(&self) -> usize {
self.current_hole
}
......@@ -149,42 +161,6 @@ impl<T> IndexMut<Label<T>> for Pool<T> {
}
}
impl<T> IntoIterator for Pool<T> {
type Item = Label<T>;
type IntoIter = PoolIter<T>;
fn into_iter(self) -> Self::IntoIter {
PoolIter {
pool: self,
index: 0,
}
}
}
#[derive(Debug, Clone)]
pub struct PoolIter<T> {
pool: Pool<T>,
index: usize,
}
impl<T> Iterator for PoolIter<T> {
type Item = Label<T>;
fn next(&mut self) -> Option<Self::Item> {
for i in (self.index)..(self.pool.elements.len()) {
match self.pool.elements[i] {
Element::Object(_) => {
self.index = i + 1;
return Some(Label::from(i));
}
Element::Hole(_) => (),
}
}
self.index = self.pool.size;
None
}
}
#[derive(Debug, Clone)]
pub struct Bag<T> {
indices: Box<[Option<usize>]>,
......@@ -360,23 +336,16 @@ mod tests {
}
#[test]
fn pool_iter() {
// setup pool
fn pool_labels() {
let mut pool = Pool::with_capacity(3);
let label1 = pool.insert(Foo);
let label2 = pool.insert(Foo);
let label3 = pool.insert(Foo);
pool.remove(label2);
// create pool iterator
let mut pool_iter = pool.clone().into_iter();
assert_eq!(pool_iter.next(), Some(label1));
assert_eq!(pool_iter.next(), Some(label3));
assert_eq!(pool_iter.next(), None);
// check whether labels from iterator can be used to index
for label in pool.clone().into_iter() {
dbg!(pool[label]);
}
let labels = pool.labels();
assert!(labels.contains(&label1));
assert!(!labels.contains(&label2));
assert!(labels.contains(&label3));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment