Calculate previous and next sibling in tree

This commit is contained in:
Joscha 2022-06-17 20:49:17 +02:00
parent 54fc4b59ce
commit 9a351b5eb3
2 changed files with 30 additions and 26 deletions

View file

@ -99,6 +99,24 @@ impl<M: Msg> Tree<M> {
None
}
}
pub fn prev_sibling(&self, id: &M::Id) -> Option<M::Id> {
let siblings = self.siblings(id)?;
siblings
.iter()
.zip(siblings.iter().skip(1))
.find(|(_, s)| *s == id)
.map(|(s, _)| s.clone())
}
pub fn next_sibling(&self, id: &M::Id) -> Option<M::Id> {
let siblings = self.siblings(id)?;
siblings
.iter()
.zip(siblings.iter().skip(1))
.find(|(s, _)| *s == id)
.map(|(_, s)| s.clone())
}
}
#[async_trait]