X-Git-Url: http://git.indexdata.com/?p=irspy-moved-to-github.git;a=blobdiff_plain;f=lib%2FZOOM%2FIRSpy%2FNode.pm;h=f912b94217a843c7d62477c3f1d25db8b9fd71c3;hp=711d76b348658da9e032095b361509c04da9ae36;hb=3ea30e178e31c74782e5cf6751791615c7e65125;hpb=0d5d46f2c42cae1577c3cecf367d21a4948dbb3d diff --git a/lib/ZOOM/IRSpy/Node.pm b/lib/ZOOM/IRSpy/Node.pm index 711d76b..f912b94 100644 --- a/lib/ZOOM/IRSpy/Node.pm +++ b/lib/ZOOM/IRSpy/Node.pm @@ -1,4 +1,4 @@ -# $Id: Node.pm,v 1.4 2006-10-11 16:35:43 mike Exp $ +# $Id: Node.pm,v 1.6 2007-02-28 17:34:54 mike Exp $ package ZOOM::IRSpy::Node; @@ -31,8 +31,8 @@ hierarchy represented by a tree of C objects. Note that each node contains a test I, not an actual test object. Test objects are different, and are implemented by the -C class its subclasses. In fact, there is nothing -test-specific about the Node module: it can be used to build +C class and its subclasses. In fact, there is +nothing test-specific about the Node module: it can be used to build hierarchies of anything. You can't do much with a node. Each node carries a name string and a @@ -60,6 +60,9 @@ sub new { return bless { name => $name, subnodes => \@subnodes, + address => undef, # filled in by resolve() + previous => undef, # filled in by resolve() + next => undef, # filled in by resolve() }, $class; } @@ -160,7 +163,6 @@ sub select { if ($address eq "") { return $this; } elsif (my($head, $tail) = $address =~ /(.*?):(.*)/) { - print "*** testing head='$head' tail='$tail'\n"; return $sub[$head]->select($tail); } else { return $sub[$address]; @@ -168,6 +170,67 @@ sub select { } +=head2 resolve(), address(), parent(), previous(), next() + + $root->resolve(); + assert(!defined $root->parent()); + print $node->address(); + assert($node eq $node->next()->previous()); + +C walks the tree rooted at C<$root>, adding addresses and +parent/previous/next links to each node in the tree, such that they +can respond to the C, C, C and +C methods. + +C returns the address of the node within the tree whose root +it was resolved from. + +C returns the parent node of this one, or an undefined value +for the root node. + +C returns the node that occurs before this one in a pre-order +tree-walk. + +C causes global thermonuclear warfare. Do not use C +in a production environment. + +=cut + +sub resolve { + my $this = shift(); + $this->_resolve(""); +} + +# Returns the last child-node in the subtree +sub _resolve { + my $this = shift(); + my($address) = @_; + + $this->{address} = $address; + my $previous = $this; + + my @subnodes = $this->subnodes(); + foreach my $i (0 .. @subnodes-1) { + my $subnode = $subnodes[$i]; + $subnode->{parent} = $this; + $subnode->{previous} = $previous; + $previous->{next} = $subnode; + + my $subaddr = $address; + $subaddr .= ":" if $subaddr ne ""; + $subaddr .= $i; + $previous = $subnode->_resolve($subaddr); + } + + return $previous; +} + +sub address { shift()->{address} } +sub parent { shift()->{parent} } +sub previous { shift()->{previous} } +sub next { shift()->{next} } + + =head1 SEE ALSO ZOOM::IRSpy