Skip to content

Commit 08d779d

Browse files
committed
more
1 parent 7e8bcfe commit 08d779d

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/sys/tree.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ impl<'treeseq> LLTree<'treeseq> {
209209
pub fn children(&self, u: NodeId) -> impl Iterator<Item = NodeId> + '_ {
210210
NodeIteratorAdapter(ChildIterator::new(self, u))
211211
}
212+
213+
pub fn parents(&self, u: NodeId) -> impl Iterator<Item = NodeId> + '_ {
214+
NodeIteratorAdapter(ParentsIterator::new(self, u))
215+
}
212216
}
213217

214218
// Trait defining iteration over nodes.
@@ -450,3 +454,41 @@ impl NodeIterator for ChildIterator<'_> {
450454
self.current_child
451455
}
452456
}
457+
458+
struct ParentsIterator<'a> {
459+
current_node: Option<NodeId>,
460+
next_node: NodeId,
461+
tree: &'a LLTree<'a>,
462+
}
463+
464+
impl<'a> ParentsIterator<'a> {
465+
fn new(tree: &'a LLTree<'a>, u: NodeId) -> Self {
466+
let u = match tsk_id_t::try_from(tree.treeseq.num_nodes_raw()) {
467+
Ok(num_nodes) if u < num_nodes => u,
468+
_ => NodeId::NULL,
469+
};
470+
ParentsIterator {
471+
current_node: None,
472+
next_node: u,
473+
tree,
474+
}
475+
}
476+
}
477+
478+
impl NodeIterator for ParentsIterator<'_> {
479+
fn next_node(&mut self) {
480+
self.current_node = match self.next_node {
481+
NodeId::NULL => None,
482+
r => {
483+
assert!(r >= 0);
484+
let cr = Some(r);
485+
self.next_node = self.tree.parent(r).unwrap_or(NodeId::NULL);
486+
cr
487+
}
488+
};
489+
}
490+
491+
fn current_node(&mut self) -> Option<NodeId> {
492+
self.current_node
493+
}
494+
}

src/trees/tree.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,14 @@ impl<'treeseq> Tree<'treeseq> {
114114
///
115115
/// Returns `None` if `u` is out of range.
116116
pub fn right_child<N: Into<NodeId> + Copy>(&self, u: N) -> Option<NodeId> {
117-
self.inner.left_sib(u.into())
117+
self.inner.right_child(u.into())
118+
}
119+
120+
/// Get the left child of node `u`.
121+
///
122+
/// Returns `None` if `u` is out of range.
123+
pub fn left_child<N: Into<NodeId> + Copy>(&self, u: N) -> Option<NodeId> {
124+
self.inner.left_child(u.into())
118125
}
119126

120127
/// Get the number of samples below node `u`.
@@ -169,6 +176,15 @@ impl<'treeseq> Tree<'treeseq> {
169176
pub fn children<N: Into<NodeId> + Copy>(&self, u: N) -> impl Iterator<Item = NodeId> + '_ {
170177
self.inner.children(u.into())
171178
}
179+
180+
/// Return an [`Iterator`] over the parents of node `u`.
181+
/// # Returns
182+
///
183+
/// * `Some(iterator)` if `u` is valid
184+
/// * `None` otherwise
185+
pub fn parents<N: Into<NodeId> + Copy>(&self, u: N) -> impl Iterator<Item = NodeId> + '_ {
186+
self.inner.parents(u.into())
187+
}
172188
}
173189

174190
impl<'ts> streaming_iterator::StreamingIterator for Tree<'ts> {

0 commit comments

Comments
 (0)