What pointer lifetime to use when iterating through a tree?
I've created a tree data structure:
which I've added nodes to. I now want to search for a node by iterating over its children:
As you can see, I'm using
UnsafePointer
here which I don't really like. I tried to use Pointer
, but I can't figure out how to get the lifetime right. If I use:
I get an error at the append
call: invalid call to 'append': method argument #0 cannot be converted from 'Pointer[0, BKTreeNode, self.root._value.children, 0]' to 'Pointer[0, BKTreeNode, self.root._value, 0]'
.
Is there an idiomatic way to do this correctly? I've tried Arc
but this seems to slow the search down by a lot. I'm using Pointer
s to avoid copying nodes. Is this the right way to go about this in the first place?1 Reply
Not sure if you are using nightly or stable. But I'll say origin instead of lifetime since it became the new term (in current stable it's still the
__lifetime_of()
operator).
You are trying to append a child with a different origin than the parent and you declared the list as being a list of pointers to node roots. Unsafe pointer lets you bypass that. Currently Pointer is very limited.
You might want to read some of /utils/span.mojo code in the stdlib to see some parametrized origin use. If your child nodes can have the origin of the parent you might avoid the issue.
You could also try to force a rebind[Pointer[BKTNode, __origin_of(self.root.value())]](Pointer.address_of(child[]))
Play around with those concepts, but don't worry it's mainly issues with the language not being fully fledged yet. And this is a solid use case for Unsafe pointer, where the language doesn't let you go on safe mode, go raw. (UnsafePointer will also be able to carry an origin in the future to be safer, I'm working on a PR for that)