Commit b0761e6c authored by SirBlueRabbit's avatar SirBlueRabbit
Browse files

impl pool iter

parent 3586ab2b
......@@ -149,6 +149,42 @@ 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>]>,
......@@ -322,4 +358,25 @@ mod tests {
bag.insert(label1);
bag.insert(label2);
}
#[test]
fn pool_iter() {
// setup pool
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]);
}
}
}
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