Skip to main content

weavatrix_graph/traversal_cache/
iter.rs

1use super::core::NeighborStorage;
2use crate::NodeIndex;
3
4/// Exact-size lazy iterator over direct neighbor node indexes.
5#[derive(Debug, Clone)]
6pub struct NeighborIter<'cache> {
7    storage: &'cache NeighborStorage,
8    front: usize,
9    back: usize,
10}
11
12impl<'cache> NeighborIter<'cache> {
13    pub(super) const fn new(storage: &'cache NeighborStorage, front: usize, back: usize) -> Self {
14        Self {
15            storage,
16            front,
17            back,
18        }
19    }
20
21    pub(super) const fn empty(storage: &'cache NeighborStorage) -> Self {
22        Self::new(storage, 0, 0)
23    }
24}
25
26impl Iterator for NeighborIter<'_> {
27    type Item = NodeIndex;
28
29    #[inline]
30    fn next(&mut self) -> Option<Self::Item> {
31        if self.front == self.back {
32            return None;
33        }
34        let value = self.storage.get(self.front);
35        self.front += 1;
36        Some(NodeIndex::new(value))
37    }
38
39    fn size_hint(&self) -> (usize, Option<usize>) {
40        let remaining = self.back - self.front;
41        (remaining, Some(remaining))
42    }
43}
44
45impl DoubleEndedIterator for NeighborIter<'_> {
46    #[inline]
47    fn next_back(&mut self) -> Option<Self::Item> {
48        if self.front == self.back {
49            return None;
50        }
51        self.back -= 1;
52        Some(NodeIndex::new(self.storage.get(self.back)))
53    }
54}
55
56impl ExactSizeIterator for NeighborIter<'_> {}