diff options
author | Charlotte Pabst <charlotte.pabst@stud.tu-darmstadt.de> | 2024-03-07 21:52:30 +0100 |
---|---|---|
committer | Charlotte Pabst <charlotte.pabst@stud.tu-darmstadt.de> | 2024-03-24 17:20:01 +0100 |
commit | 219261b7042fba1a54ecd478b56e902d9ca8787b (patch) | |
tree | edbf297706624f2983e8f73c5e39d831c90ef65a /src/entity_iterator.rs | |
parent | 35cddacd85c61fd56032899d68b1c1c885377dda (diff) | |
download | dcel-219261b7042fba1a54ecd478b56e902d9ca8787b.tar.xz |
Diffstat (limited to 'src/entity_iterator.rs')
-rw-r--r-- | src/entity_iterator.rs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/entity_iterator.rs b/src/entity_iterator.rs new file mode 100644 index 0000000..58cd2a9 --- /dev/null +++ b/src/entity_iterator.rs @@ -0,0 +1,59 @@ +use crate::*; + +pub struct EntityIterator<'tok, 'brand, 'arena, T>(Option<(lens_t!(T), lens_t!(T))>); + +impl<'tok, 'brand, 'arena, T> Clone for EntityIterator<'tok, 'brand, 'arena, T> { + fn clone(&self) -> Self { + Self(self.0) + } +} + +impl<'tok, 'brand, 'arena, T> Copy for EntityIterator<'tok, 'brand, 'arena, T> {} + +impl<'tok, 'brand, 'arena, T: Entity<'brand, 'arena>> EntityIterator<'tok, 'brand, 'arena, T> { + pub(crate) fn new( + start: Option<ptr_t!(T)>, + token: &'tok impl ReflAsRef<GhostToken<'brand>>, + ) -> Self { + Self(start.map(|s| { + let l = Lens::new(s, token); + (l, l.prev()) + })) + } +} + +impl<'tok, 'brand, 'arena, T: Entity<'brand, 'arena>> Iterator + for EntityIterator<'tok, 'brand, 'arena, T> +{ + type Item = lens_t!(T); + + fn next(&mut self) -> Option<Self::Item> { + let range = self.0.as_mut()?; + let ret = range.0; + + if range.0 == range.1 { + self.0 = None; + } else { + range.0 = range.0.next(); + } + + Some(ret) + } +} + +impl<'tok, 'brand, 'arena, T: Entity<'brand, 'arena>> DoubleEndedIterator + for EntityIterator<'tok, 'brand, 'arena, T> +{ + fn next_back(&mut self) -> Option<Self::Item> { + let range = self.0.as_mut()?; + let ret = range.1; + + if range.0 == range.1 { + self.0 = None; + } else { + range.1 = range.1.prev(); + } + + Some(ret) + } +} |