X-Git-Url: https://feistymeow.org/gitweb/?a=blobdiff_plain;f=nucleus%2Flibrary%2Fnodes%2Ftree.cpp;fp=nucleus%2Flibrary%2Fnodes%2Ftree.cpp;h=0a2fbf9eea119fd052362fb24356b9a60273e8fe;hb=5a8e13e7a44ed98d9683bc6cd3bb374e9d3b0756;hp=a087b0b74edaeb032771f1b725eb80d3a4e000bb;hpb=24b2947ed9364f3e83fa1bb544ff6b1fdbf0428f;p=feisty_meow.git diff --git a/nucleus/library/nodes/tree.cpp b/nucleus/library/nodes/tree.cpp index a087b0b7..0a2fbf9e 100644 --- a/nucleus/library/nodes/tree.cpp +++ b/nucleus/library/nodes/tree.cpp @@ -278,10 +278,61 @@ tree::~tree() // cousin. tree *my_parent = parent(); if (my_parent) my_parent->prune(this); + my_parent = NIL; // disavow since we are loose now. + +#if 0 + + //original version suffers from being too recursive on windoze, + //which blows out the stack. linux never showed this problem. so, i + //guess i'm glad i tested on windows. + //anyway, it's a general problem for a degenerate tree like the one + //i've constructed. current version has ~40000 direct descendants of + //the root in a single line, so the stack has to support 40000 frames + //for the delete implementation below to work. // iterate over the child nodes and whack each individually. while (branches()) delete branch(0); // this relies on the child getting out of our branch list. +#else + + // newer version of delete doesn't recurse; it just iterates instead, + // which avoids the massive recursive depth of the original approach. + tree *curr_node = this; + while (curr_node != NIL) { + // make a breadcrumb for getting back to 'here' in the tree. + tree *way_back = curr_node; + // our main operation here is to go down a node without using any + // stack frames. so we just pick the first kid; it's either valid + // or there are no kids at all. + curr_node = curr_node->branch(0); + + if (curr_node = NIL) { + // wayback has no children, so we can take action. + + // if wayback is the same as "this", then we exit from iterations since + // we've cleaned all the kids out. + if (way_back == this) { + break; + } + + // we want to whack the wayback node at this point, since it's a parent + // with no kids, i.e. a leaf. we've guaranteed we're not going beyond + // our remit, since wayback is not the same node as the top level one + // in the destructor (as long as there are no cycles in the tree...). + curr_node = way_back->parent(); // go up in tree on next iteration. + delete way_back; // whack a node, finally. + + } else { + // okay, there's a node below here. we will spider down to it. + continue; + } + + + } + +#endif + + } tree *tree::parent() const { return (tree *)get_link(BACKWARDS_BRANCH); }