hff_core/read/
table_iter.rs

1use super::{Hff, TableView};
2use std::fmt::Debug;
3
4/// An iterator over tables at a given level.
5pub struct TableIter<'a, T: Debug> {
6    hff: &'a Hff<T>,
7    index: Option<usize>,
8}
9
10impl<'a, T: Debug> TableIter<'a, T> {
11    /// Create a new table iterator.
12    pub fn new(hff: &'a Hff<T>, start: usize) -> Self {
13        Self {
14            hff,
15            index: Some(start),
16        }
17    }
18
19    /// Create an empty iterator.
20    pub fn empty(hff: &'a Hff<T>) -> Self {
21        Self { hff, index: None }
22    }
23
24    /// Create a table iterator for the child of the current table.
25    pub fn children(&self) -> Self {
26        if let Some(index) = &self.index {
27            if self.hff.tables_array()[*index].child_count() > 0 {
28                let next = *index + 1;
29                assert!(next < self.hff.tables_array().len());
30                return Self::new(self.hff, next);
31            }
32        }
33
34        // Nothing to iterate.
35        Self::empty(self.hff)
36    }
37}
38
39impl<'a, T: Debug> Iterator for TableIter<'a, T> {
40    type Item = TableView<'a, T>;
41
42    fn next(&mut self) -> Option<Self::Item> {
43        if let Some(index) = self.index.take() {
44            let result = Some(TableView::new(self.hff, index));
45
46            let sibling = self.hff.tables_array()[index].sibling() as usize;
47            if sibling > 0 && index + sibling < self.hff.tables_array().len() {
48                self.index = Some(index + sibling)
49            }
50
51            result
52        } else {
53            None
54        }
55    }
56}