10 |
|
# goto(TreeNode) : Tell the monkey to go to a particular node in the tree |
11 |
|
|
12 |
|
package TreeMonkey; |
13 |
+ |
require 5.001; |
14 |
+ |
use Utilities::ListIterator; |
15 |
|
|
16 |
|
sub new { |
17 |
|
my $class=shift; |
22 |
|
|
23 |
|
sub goto { |
24 |
|
my $self=shift; |
25 |
< |
$self->{currentnode)=shift; |
25 |
> |
my $node=shift; |
26 |
> |
|
27 |
> |
$self->{currentnode}=$node; |
28 |
|
} |
29 |
|
|
30 |
|
sub find { |
33 |
|
my @words; |
34 |
|
my $first; |
35 |
|
my $found=0; |
36 |
+ |
my $branch=""; |
37 |
|
|
38 |
|
# Split string |
39 |
|
($first, @words)=split '/', $string; |
40 |
|
|
41 |
|
# Search towards root for first word or until we reach root |
42 |
< |
while ( ( $self->{currentnode}->name() ne $first ) || |
43 |
< |
( $self->{currentnode)->parent() ne "" ) ) { |
44 |
< |
$self->{currentnode)=$self->{currentnode)->parent(); |
42 |
> |
while ( ( $self->{currentnode}->parent() ne '/' ) && |
43 |
> |
( $self->{currentnode}->name() ne $first ) ) { |
44 |
> |
$self->{currentnode}=$self->{currentnode}->parent(); |
45 |
|
} |
46 |
|
|
47 |
|
# Check the case of just a single word |
48 |
< |
if (( $#words == 0 ) && ( $self->{currentnode}->name() eq $first )) { |
48 |
> |
if (( $#words < 0 )) { |
49 |
> |
if (( $self->{currentnode}->name() eq $first )) { |
50 |
|
$found=1; |
51 |
+ |
} |
52 |
+ |
} |
53 |
+ |
else { #if we have some more words... |
54 |
+ |
$f=1; |
55 |
+ |
$n=0; |
56 |
+ |
while ( ($f!=0) && ( $n < $#words ) ) { |
57 |
+ |
my $branchlist=$self->{currentnode}->nodeList(); |
58 |
+ |
$f=$self->_checklist($words[$n],$branchlist); |
59 |
+ |
$n++; |
60 |
+ |
print $n." ".$f."\n"; |
61 |
+ |
} |
62 |
+ |
$found=$f; |
63 |
|
} |
64 |
|
|
65 |
|
# Now Search the child branches for the pattern |
48 |
– |
my $n=0; |
49 |
– |
my $i=0; |
50 |
– |
while ( $found==0 && $n>=0 ) { |
51 |
– |
$children=$self->{currentnode}->listbranch()) { |
52 |
– |
$n=$#children; |
53 |
– |
$branch=$children[$n] |
54 |
– |
if ( $branch eq $words[$i] ) { |
55 |
– |
$self->{currentnode}=$branch; |
56 |
– |
$i++; |
57 |
– |
if ( $n > $#words ) { $found=1; } |
58 |
– |
} |
59 |
– |
else { |
60 |
– |
$n--; |
61 |
– |
} |
62 |
– |
} # end while |
63 |
– |
|
66 |
|
# Now return the object ID of the found node or null |
67 |
|
if ( $found==1 ) { |
68 |
|
return $self->{currentnode}; |
71 |
|
return ""; |
72 |
|
} |
73 |
|
} |
74 |
+ |
|
75 |
+ |
# ------------------------ Support Routines ------------------------------ |
76 |
+ |
|
77 |
+ |
sub _checklist { |
78 |
+ |
my $self=shift; |
79 |
+ |
my $word=shift; |
80 |
+ |
my $branchlist=shift; |
81 |
+ |
my $bit; |
82 |
+ |
my $branch; |
83 |
+ |
my $found=0; |
84 |
+ |
|
85 |
+ |
$bit=ListIterator->new($branchlist); |
86 |
+ |
|
87 |
+ |
while ( ($found==0) && ( ! $bit->last() ) ) { |
88 |
+ |
$branch=$bit->next(); |
89 |
+ |
if ( $branch->name() eq $word) { |
90 |
+ |
$self->{currentnode}=$branch; |
91 |
+ |
$found=1; |
92 |
+ |
} |
93 |
+ |
} # end while |
94 |
+ |
return $found; |
95 |
+ |
} |
96 |
+ |
|
97 |
+ |
|