Skip to content
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

feature request: remove_children and detach_children #53

Open
PoiScript opened this issue Aug 8, 2019 · 5 comments
Open

feature request: remove_children and detach_children #53

PoiScript opened this issue Aug 8, 2019 · 5 comments

Comments

@PoiScript
Copy link

PoiScript commented Aug 8, 2019

When dealing with the tree structure, sometimes we want to remove the all children nodes of a specific node but keeping the node itself. For example, removing anything between tags <p> and </p>.

Here is a snippet demonstrating how to implement detach_children with indextree:

fn detach_children<T>(self, arena: &mut Arena<T>) {
    for child in self.children(arena) {
        let node = arena[child];
        node.parent = None;
        node.next_sibling = None;
        node.previous_sibling = None;
    }
    
    let node = arena[self];
    node.first_child = None;
    node.last_child = None;
}

I haven't tried it yet, so it may not be compiled.

@issue-label-bot
Copy link

Issue-Label Bot is automatically applying the label feature_request to this issue, with a confidence of 0.92. Please mark this comment with 👍 or 👎 to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

@lo48576
Copy link
Contributor

lo48576 commented Aug 8, 2019

indextree allows toplevel nodes to have siblings, so next_sibling and previous_sibling of children can be left unmodified.

@PoiScript
Copy link
Author

@lo48576 Then the following two codes will behave differently:

fn detach_children<T>(self, arena: &mut Arena<T>) {
    for child in self.children(arena) {
        let node = arena[child];
        node.parent = None;
    }
    
    let node = arena[self];
    node.first_child = None;
    node.last_child = None;
}
for child in self.children(arena) {
    child.detach(arena);
}

I don't know it is needed to keep the relation between detached children. But at least it should be mentioned on document in order not to get the user confused.

@lo48576
Copy link
Contributor

lo48576 commented Aug 9, 2019

Yes, I think the name detach_children() has a little ambiguity (which it means), and it should be explicitly documented (or better name should be chosen).

@GopherJ
Copy link

GopherJ commented Aug 15, 2020

I also need remove_children method to make a graph.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants