1 /*****************************************************************************\
4 * Author : Chris Koeritz *
6 *******************************************************************************
7 * Copyright (c) 1992-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
17 #include <basis/common_outcomes.h>
18 #include <basis/functions.h>
19 #include <basis/guards.h>
22 // uncomment if you want lots of debugging info.
25 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
27 using namespace basis;
31 const int BACKWARDS_BRANCH = 0;
32 // BACKWARDS_BRANCH is the branch from this tree to its parent. this is
33 // steeped in the perspective that the root is the backwards direction (where
34 // we came from, in a sense) and that the children of this node are the
35 // forwards direction.
41 tree::iterator::iterator(const tree *initial, traversal_directions direction)
42 : path(initial), _order(direction), _aim(AWAY_FROM_ROOT)
46 tree::iterator::~iterator() { while (size()) pop(); }
48 bool tree::iterator::next_node(tree *&to_return)
53 to_return = NULL_POINTER;
55 if ( (_order != to_branches)
56 && (_order != reverse_branches) ) {
57 if (_aim == AWAY_FROM_ROOT) LOG("going down")
63 if (_aim == AWAY_FROM_ROOT) {
64 // going down means this is the first time we have seen the current top
66 to_return = (tree *)(*this)[size() - 1];
68 // LOG(a_sprintf("[%s] ", to_return->get_contents()->held().s());
69 if (to_return->branches()) LOG("pushing 0")
70 else LOG("switching direction");
72 if (to_return->branches())
73 push(to_return->branch(0));
77 // going up means that we need to get rid of some things before we
78 // start seeing new nodes again.
79 if (size() == 1) return false;
80 // the last node has been seen....
81 tree *last = (tree *)pop();
82 tree *current_tree = (tree *)current();
83 int lastnum = current_tree->which(last);
85 if (lastnum < current_tree->branches() - 1)
86 LOG(a_sprintf("going down %d", lastnum+1))
87 else LOG("still going up");
89 if (lastnum < current_tree->branches() - 1) {
90 _aim = AWAY_FROM_ROOT;
91 push(current_tree->branch(lastnum + 1));
92 } // else still going up.
97 if (_aim == AWAY_FROM_ROOT) {
98 // going down means starting on the left branch.
99 tree *temp = (tree *)current();
101 if (temp->branches()) LOG("pushing 0")
102 else LOG("switching direction");
104 if (temp->branches()) push(temp->branch(0));
107 to_return = (tree *)current();
109 // LOG(a_sprintf("[%s] ", to_return->get_contents()->held().s()));
113 // going up means that the left branch is done and we need to either
114 // keep going up or go down the right branch.
115 if (size() == 1) return false;
116 // the last node has been seen....
117 tree *last = (tree *)pop();
118 tree *current_tree = (tree *)current();
119 int lastnum = current_tree->which(last);
121 if (lastnum < 1) LOG(a_sprintf("going down %d", lastnum+1))
122 else LOG("still going up");
125 _aim = AWAY_FROM_ROOT;
126 to_return = (tree *)current();
128 /// LOG(a_sprintf("[%s] ", to_return->get_contents()->held().s()));
130 push(current_tree->branch(lastnum + 1));
131 } // else still going up.
136 if (_aim == TOWARD_ROOT) return false;
139 tree *temp = (tree *)current();
140 if (!temp->branches())
143 push(temp->branch(0));
145 tree *last = (tree *)pop();
146 tree *curr = (tree *)current();
147 int lastnum = curr->which(last);
148 if (lastnum < curr->branches() - 1)
149 push(curr->branch(lastnum + 1));
150 else _aim = TOWARD_ROOT;
156 case reverse_branches: {
157 if (_aim == TOWARD_ROOT) return false;
160 tree *temp = (tree *)current();
161 if (!temp->branches()) _aim = TOWARD_ROOT;
162 else push(temp->branch(temp->branches() - 1));
164 tree *last = (tree *)pop();
165 tree *curr = (tree *)current();
166 int lastnum = curr->which(last);
167 if (lastnum > 0) push(curr->branch(lastnum - 1));
168 else _aim = TOWARD_ROOT;
174 default: // intentional fall-through to postfix.
176 if (_aim == AWAY_FROM_ROOT) {
177 // going down means that the bottom is still being sought.
178 tree *temp = (tree *)current();
180 if (temp->branches()) LOG("pushing 0")
181 else LOG("switching direction");
183 if (temp->branches()) push(temp->branch(0));
184 else _aim = TOWARD_ROOT;
186 // going up means that all nodes below current have been hit.
187 if (!size()) return false; // the last node has been seen...
188 else if (size() == 1) {
189 to_return = (tree *)pop();
190 // this is the last node.
193 tree *last = (tree *)pop();
196 /// LOG(a_sprintf("[%s] ", to_return->get_contents()->held()));
198 tree *current_tree = (tree *)current();
199 int lastnum = current_tree->which(last);
201 if (lastnum < current_tree->branches() - 1)
202 LOG(a_sprintf("going down %d", lastnum+1))
203 else LOG("still going up");
205 if (lastnum < current_tree->branches() - 1) {
206 _aim = AWAY_FROM_ROOT;
207 push(current_tree->branch(lastnum + 1));
208 } // else still going up.
214 // it is assumed that termination conditions cause a return earlier on.
217 void tree::iterator::whack(tree *to_whack)
222 if (!to_whack) return; // that's a bad goof.
224 if (to_whack == current()) {
225 // we found that this is the one at the top right now.
228 LOG("found node in current top; removing it there.");
230 } else if (to_whack->parent() == current()) {
231 // the parent is the current top. make sure we don't mess up traversal.
233 LOG("found node's parent as current top; don't know what to do.");
237 LOG("found no match for either node to remove or parent in path.");
241 tree *parent = to_whack->parent();
244 LOG("no parent node for the one to whack! would have whacked "
248 parent->prune(to_whack);
253 tree *tree::iterator::next()
258 tree *to_return = NULL_POINTER;
259 bool found_tree = false;
260 while (!found_tree) {
261 bool still_running = next_node(to_return);
262 if (to_return || !still_running) found_tree = true;
273 { set_link(BACKWARDS_BRANCH, NULL_POINTER); }
277 // must at least unhook ourselves from the parent so we don't become a lost
279 tree *my_parent = parent();
280 if (my_parent) my_parent->prune(this);
281 my_parent = NULL_POINTER; // disavow since we are loose now.
285 //original version suffers from being too recursive on windoze,
286 //which blows out the stack. linux never showed this problem. so, i
287 //guess i'm glad i tested on windows.
288 //anyway, it's a general problem for a degenerate tree like the one
289 //i've constructed. current version has ~40000 direct descendants of
290 //the root in a single line, so the stack has to support 40000 frames
291 //for the delete implementation below to work.
293 // iterate over the child nodes and whack each individually.
294 while (branches()) delete branch(0);
295 // this relies on the child getting out of our branch list.
298 // newer version of delete doesn't recurse; it just iterates instead,
299 // which avoids the massive recursive depth of the original approach.
300 tree *curr_node = this;
301 while (curr_node != NULL_POINTER) {
302 // make a breadcrumb for getting back to 'here' in the tree.
303 tree *way_back = curr_node;
304 // our main operation here is to go down a node without using any
305 // stack frames. so we just pick the first kid; it's either valid
306 // or there are no kids at all.
307 curr_node = curr_node->branch(0);
309 if (curr_node = NULL_POINTER) {
310 // wayback has no children, so we can take action.
312 // if wayback is the same as "this", then we exit from iterations since
313 // we've cleaned all the kids out.
314 if (way_back == this) {
318 // we want to whack the wayback node at this point, since it's a parent
319 // with no kids, i.e. a leaf. we've guaranteed we're not going beyond
320 // our remit, since wayback is not the same node as the top level one
321 // in the destructor (as long as there are no cycles in the tree...).
322 curr_node = way_back->parent(); // go up in tree on next iteration.
323 delete way_back; // whack a node, finally.
326 // okay, there's a node below here. we will spider down to it.
338 tree *tree::parent() const { return (tree *)get_link(BACKWARDS_BRANCH); }
340 int tree::branches() const { return links() - 1; }
342 tree *tree::branch(int branch_number) const
345 bounds_return(branch_number, 1, branches(), NULL_POINTER);
346 return (tree *)get_link(branch_number);
349 int tree::which(tree *branch_to_find) const
350 { return node::which((node *)branch_to_find) - 1; }
352 tree *tree::root() const
354 const tree *traveler = this;
355 // keep looking at the backwards branch until it is a NULL_POINTER. the tree with
356 // a NULL_POINTER BACKWARDS_BRANCH is the root. return that tree.
357 while (traveler->get_link(BACKWARDS_BRANCH))
358 traveler = (tree *)traveler->get_link(BACKWARDS_BRANCH);
359 return const_cast<tree *>(traveler);
362 void tree::attach(tree *new_branch)
364 if (!new_branch) return;
365 insert_link(links(), new_branch);
366 new_branch->set_link(BACKWARDS_BRANCH, this);
369 void tree::insert(int branch_place, tree *new_branch)
372 insert_link(links(), NULL_POINTER);
373 if (branch_place >= links())
374 branch_place = links() - 1;
375 for (int i = links() - 1; i > branch_place; i--)
376 set_link(i, get_link(i-1));
377 set_link(branch_place, new_branch);
378 new_branch->set_link(BACKWARDS_BRANCH, this);
381 outcome tree::prune(tree *branch_to_cut)
383 int branch_number = which(branch_to_cut);
384 if (branch_number == basis::common::NOT_FOUND) return basis::common::NOT_FOUND;
385 return prune_index(branch_number);
388 outcome tree::prune_index(int branch_to_cut)
391 bounds_return(branch_to_cut, 1, branches(), basis::common::NOT_FOUND);
392 tree *that_branch = (tree *)get_link(branch_to_cut);
393 that_branch->set_link(BACKWARDS_BRANCH, NULL_POINTER);
394 zap_link(branch_to_cut);
395 return basis::common::OKAY;
398 int tree::depth() const
400 tree *my_root = root();
401 const tree *current_branch = this;
403 while (current_branch != my_root) {
404 current_branch = current_branch->parent();
410 //probably okay; we want to use this function rather than non-existent
411 //node base function which isn't implemented yet.
412 bool tree::generate_path(path &to_follow) const
414 if (to_follow.size()) {}
416 tree *traveller = this;
417 path to_accumulate(root());
418 while (traveller->parent() != NULL_POINTER) {
419 // int branch_number = traveller->parent()->which(traveller);
420 // if (branch_number == BRANCH_NOT_FOUND) non_continuable_error
421 // (class_name(), "generate_path", "branch not found during path construction");
422 // chunk *to_stuff = new chunk
423 // (SELF_OWNED, (byte *)&branch_number, sizeof(int));
424 to_accumulate.push(traveller);
425 traveller = traveller->parent();
427 // the order of things on the stack needs to be reversed now.
428 // path to_return = new stack(*to_accumulate.invert());
430 to_accumulate.invert();
431 return to_accumulate;
436 //hmmm: need text form!
438 tree::iterator tree::start(traversal_directions direction) const
439 { return iterator(this, direction); }