hff_core/read/
table_iter.rs1use super::{Hff, TableView};
2use std::fmt::Debug;
3
4pub struct TableIter<'a, T: Debug> {
6 hff: &'a Hff<T>,
7 index: Option<usize>,
8}
9
10impl<'a, T: Debug> TableIter<'a, T> {
11 pub fn new(hff: &'a Hff<T>, start: usize) -> Self {
13 Self {
14 hff,
15 index: Some(start),
16 }
17 }
18
19 pub fn empty(hff: &'a Hff<T>) -> Self {
21 Self { hff, index: None }
22 }
23
24 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 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}