Skip to content

[Features] Iterator over mutable childs, deep cloning for nodes #46

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Quotique
Copy link

@Quotique Quotique commented Jun 27, 2025

This PR contains following changes:

  1. deep_clone method for tree node allow to create independent copy of part of the tree. This can also be used to remove orphaned nodes.
  2. [reverted] children_mut method of mutable tree node allow to iterate over mutable children of the node
  3. removing mut from as_ref argument

src/iter.rs Outdated
impl<'a, T: 'a> Iterator for ChildrenMut<'a, T> {
type Item = NodeMut<'a, T>;
fn next(&mut self) -> Option<Self::Item> {
// lifetime cast here is safe because &mut self living shorter than 'a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please mark this as // SAFETY: comment.

There is also the aliasing question to discuss, i.e. the mutable reference must not only be valid but also exclusive.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, there is no exclusivity here. Do you have any recommendations on how to fix this?

Now I realize there was no children_mut here for a good reason :{

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I guess the first problem I see is that this will yield two children who can then above mutably access their parent via NodeMut::parent?

I don't think you can fix this using an Iterator of NodeMut. You would either need to use a different trait, e.g. a lending iterator which prevents have both NodeMut alive at the same time or return a different item which cannot navigate in the problematic directions.

I guess both changes would appear to limit the appeal of the API somewhat though?

src/lib.rs Outdated
let copy_id = id_map.get(&orig.id).unwrap();
let mut copy = unsafe { result.get_unchecked_mut(*copy_id) };
for child in orig.children() {
id_map.insert(child.id, copy.append(child.value().clone()).id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put copying, appending and map insertion into separate lines to make this easier to read.

src/lib.rs Outdated
pub fn deep_clone(&self, id: NodeId) -> Self {
let start_node = unsafe { self.get_unchecked(id) };
let mut result = Self::new(start_node.value().clone());
let mut id_map = std::collections::HashMap::new();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I understand why this is necessary? Wouldn't a breadth-first copy using a simple stack suffice here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an argument for this: when iterating over children, it is better if they are locate one after another. This prevents cache misses and works a bit faster.

In my case it doesn't matter. I can change it if you say so.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when iterating over children, it is better if they are locate one after another.

I think a breadth-first (but not depth-first) traversal would also produce that property. So "stack" was wrong in my comment, you want a queue, e.g. VecDeque.

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

Successfully merging this pull request may close these issues.

2 participants