6 ms·
How would you perform destruction of a tree with more than one child tail recursively? I mean this as an honest question - my current implementation of array_m
by sonyandy 11y ago
How would you perform destruction of a tree with more than one child tail recursively? I mean this as an honest question - my current implementation of array_mapped_trie performs this operation via non-tail recursive calls.
EDIT: I also don't see how destruction of a list is any harder than iteration of a list, as far as writing it tail recursively. You would need to make sure your node destructors are trivial, but you probably need to do this anyway if you intend to support using a custom Allocator (would instead have template <typename Allocator> destroy(node*, Allocator&); that calls Allocator::destroy on the contained value and Allocator::deallocate on the node). The implementation would then look something along the lines of:
decltype(head) next;
for (auto ptr = head; ptr; ptr = next) {
next = ptr->next;
if (ptr->unique()) {
destroy(ptr, allocator);
}
}
(unique() is a method on node - intrusive ref count.)