27 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), astring(s) + a_sprintf(" [obj=%p]", this))
29 using namespace basis;
46 :
path(initial), _order(direction), _aim(AWAY_FROM_ROOT)
52 bool tree::iterator::next_node(
tree *&to_return)
62 else
LOG("going up...");
70 to_return = (
tree *)(*
this)[size() - 1];
74 else
LOG("switching direction");
77 push(to_return->
branch(0));
83 if (size() == 1)
return false;
86 tree *current_tree = (
tree *)current();
87 int lastnum = current_tree->
which(last);
89 if (lastnum < current_tree->
branches() - 1)
91 else LOG(
"still going up");
93 if (lastnum < current_tree->
branches() - 1) {
95 push(current_tree->
branch(lastnum + 1));
105 if (temp->branches())
LOG(
"pushing 0")
106 else LOG(
"switching direction");
108 if (temp->branches()) push(temp->branch(0));
111 to_return = (
tree *)current();
119 if (size() == 1)
return false;
122 tree *current_tree = (
tree *)current();
123 int lastnum = current_tree->
which(last);
125 if (lastnum < 1)
LOG(
a_sprintf(
"going down %d", lastnum+1))
126 else
LOG("still going up");
130 to_return = (
tree *)current();
134 push(current_tree->branch(lastnum + 1));
144 if (!temp->branches())
147 push(temp->branch(0));
151 int lastnum = curr->which(last);
153 push(curr->branch(lastnum + 1));
166 else push(temp->branch(temp->branches() - 1));
170 int lastnum = curr->which(last);
171 if (lastnum > 0) push(curr->branch(lastnum - 1));
184 if (temp->branches())
LOG(
"pushing 0")
185 else LOG(
"switching direction");
187 if (temp->branches()) push(temp->branch(0));
191 if (!size())
return false;
192 else if (size() == 1) {
193 to_return = (
tree *)pop();
202 tree *current_tree = (
tree *)current();
203 int lastnum = current_tree->which(last);
205 if (lastnum < current_tree->
branches() - 1)
207 else LOG(
"still going up");
209 if (lastnum < current_tree->
branches() - 1) {
211 push(current_tree->branch(lastnum + 1));
226 if (!to_whack)
return;
228 if (to_whack == current()) {
232 LOG(
"found node in current top; removing it there.");
234 }
else if (to_whack->
parent() == current()) {
237 LOG(
"found node's parent as current top; don't know what to do.");
241 LOG(
"found no match for either node to remove or parent in path.");
248 LOG(
"no parent node for the one to whack! would have whacked "
263 bool found_tree =
false;
264 while (!found_tree) {
265 bool still_running = next_node(to_return);
266 if (to_return || !still_running) found_tree =
true;
283 LOG(
"entry to tree destructor");
289 if (my_parent) my_parent->
prune(
this);
294 LOG(
"old code for tree destructor activating...");
310 LOG(
"new code for tree destructor activating...");
320 tree *curr_node = stop_node;
323 tree *way_back = curr_node;
327 curr_node = curr_node->
branch(0);
331 LOG(
"tree dtor: can take action on childless node...");
335 if (way_back == stop_node) {
336 LOG(
"tree dtor: stopping since at wayback node...");
340 LOG(
"tree dtor: whacked wayback before inner break.");
348 curr_node = way_back->
parent();
350 LOG(
"tree dtor: reset currnode, about to whack wayback...");
352 if (curr_node == stop_node) {
353 LOG(
"tree dtor: seeing curr_node hits our stop_node!");
355 }
else if (curr_node == stop_node->
parent()) {
356 LOG(
"tree dtor: seeing curr_node ABOVE our stop_node!");
361 LOG(
"tree dtor: after whacking wayback...");
364 LOG(
"tree dtor: spidering to node below...");
373 LOG(
"newest code for tree destructor (stack based) activating...");
392 LOG(
a_sprintf(
"tree dtor: adding child %p on stack.", bran));
398 while (sleestak.
size()) {
403 LOG(
a_sprintf(
"tree dtor: popped a tree %p off stack.", popper));
411 LOG(
a_sprintf(
"tree dtor: inner, adding child %p on stack.", bran));
419 LOG(
a_sprintf(
"tree dtor: about to zap a tree %p.", popper));
423 LOG(
"tree dtor: whacked that tree.");
445 const tree *traveler =
this;
450 return const_cast<tree *
>(traveler);
455 if (!new_branch)
return;
464 if (branch_place >=
links())
465 branch_place =
links() - 1;
466 for (
int i =
links() - 1; i > branch_place; i--)
474 int branch_number =
which(branch_to_cut);
475 if (branch_number == basis::common::NOT_FOUND)
return basis::common::NOT_FOUND;
485 LOG(
a_sprintf(
"got legit branch %d to cut", branch_to_cut));
493 LOG(
a_sprintf(
"zapping link branch %d", branch_to_cut));
497 LOG(
"returning, finished.");
499 return basis::common::OKAY;
505 const tree *current_branch =
this;
507 while (current_branch != my_root) {
508 current_branch = current_branch->
parent();
518 if (to_follow.
size()) {}
543 {
return iterator(
this, direction); }
a_sprintf is a specialization of astring that provides printf style support.
Outcomes describe the state of completion for an operation.
An object representing the interstitial cell in most linked data structures.
void zap_link(int link_number)
the specified link is removed from the node.
void set_link(int link_number, node *new_link)
Connects the node "new_link" to this node.
int links() const
Returns the number of links the node currently holds.
void insert_link(int where, node *to_add=NULL_POINTER)
adds a new link prior to the position specified in "where".
int which(node *to_find) const
locates the index where "to_find" lives in our list of links.
node * get_link(int link_number) const
Returns the node that is connected to the specified "link_number".
A method for tracing a route from a tree's root to a particular node.
int size() const
returns the number of items in the path.
void whack(tree *to_whack)
destroys the tree "to_whack".
tree * next()
Returns a pointer to the next tree in the direction of traversal.
A dynamically linked tree with an arbitrary number of branches.
virtual int depth() const
Returns the distance of "this" from the root. The root's depth is 0.
virtual void insert(int branch_place, tree *new_branch)
inserts "new_branch" before the branches starting at "branch_place".
virtual basis::outcome prune_index(int branch_to_cut)
Removes the branch at the specified index from this tree.
virtual int which(tree *branch_to_find) const
Returns the branch number for a particular branch in this tree.
virtual ~tree()
destroys the tree by recursively destroying all child tree nodes.
tree()
constructs a new tree with a root and zero branches.
virtual int branches() const
Returns the number of branches currently connected to this tree.
virtual void attach(tree *new_branch)
Attaches the specified branch to the current tree.
virtual basis::outcome prune(tree *branch_to_cut)
Removes the specified branch from this tree.
virtual tree * parent() const
Returns the tree node that is the immediate ancestor of this one.
iterator start(traversal_directions direction) const
Returns a fresh iterator positioned at this tree node.
virtual bool generate_path(path &to_follow) const
Returns the path to "this" path_tree from its root.
virtual tree * root() const
Locates and returns the absolute root of the tree containing this tree.
virtual tree * branch(int branch_number) const
Returns the specified branch of this tree.
An abstraction that represents a stack data structure.
basis::outcome push(const contents &element)
Enters a new element onto the top of the stack.
basis::outcome acquire_pop(contents &to_stuff)
Used to grab the top off of the stack.
int size() const
returns the size of the stack.
#define NULL_POINTER
The value representing a pointer to nothing.
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
#define bounds_return(value, low, high, to_return)
Verifies that "value" is between "low" and "high", inclusive.
The guards collection helps in testing preconditions and reporting errors.
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
A logger that sends to the console screen using the standard output device.
const int BACKWARDS_BRANCH
A dynamic container class that holds any kind of object via pointers.