-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add api get index of child and to insert child at given index. #15
Conversation
Children form a linked list, so "index" isn't really a concept that should apply to them. The crate documentation claims that all methods are constant time and all iterators are linear time, and I'd like to maintain that API. The "correct" solution here would be to have mutable iterators, but of course Rust makes that quite difficult. |
I understand not wanting "index" in the API, I'm just not sure how to do what I want without it.
Is this something that's possible now with existing ego-tree? If so can you give me a hint in how I would use? My goal is:
I couldn't figure out how to do this using existing high level API no matter what I tried. (I'm new to Rust, so quite possibly just missing something). The only way that I could get it to work was with low level id manipulation, and so that's why I though it was a good candidate for the crate. |
Would something like this work? fn insert_at<T>(parent: NodeMut<T>, index: usize, new_child: T) -> NodeMut<T> {
if index == 0 {
return parent.prepend(new_child) // covers the `first_child().is_none()` case
}
let mut child = parent.first_child().unwrap();
for _ in 1..index {
child = child.next_sibling().unwrap()
}
child.insert_after(new_child)
} |
This tries to implement a mutable iterator, but it doesn't quite work. The BC won't let you reassing |
I still would not implement this into master, because of what said previously here. We could maybe isolate the new non-constant time funtions, for making sure that whoever uses them is fully aware of the time complexity they require. |
I think an API organized around a type derived from std's EDIT: Of course, for insertion |
Thinking about this some more, impl<'a, T: 'a> NodeMut<'a, T> {
pub fn into_next_sibling(self) -> Option<Self> {
let id = self.node().next_sibling;
id.map(move |id| unsafe { self.tree.get_unchecked_mut(id) })
}
} to make code like that proposed in #15 (comment) work? |
Opened #33 as an alternative approach. |
I really like the approach in #15 (comment), |
Superseded by #33 |
No description provided.