| 1 | package PMLTQ::Relation::TransitiveIterator; | ||||||||||||||||||||||||||||
| 2 | 12 | our $AUTHORITY = 'cpan:MATY'; | |||||||||||||||||||||||||||
| 3 | 12 | $PMLTQ::Relation::TransitiveIterator::VERSION = '1.5.0'; | |||||||||||||||||||||||||||
| 4 | # ABSTRACT: Iterates over nodes that are transitive | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 12 | ▶ | use base qw(PMLTQ::Relation::Iterator); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 12 | ▶ | use constant CONDITIONS=>0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 12 | ▶ | use constant ITERATOR=>1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 12 | ▶ | use constant MIN=>2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 12 | ▶ | use constant MAX=>3; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 12 | ▶ | use constant ITER_STACK=>4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 12 | ▶ | use constant SEEN=>5; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 12 | ▶ | use constant FILE=>6; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 12 | ▶ | use constant DEPTH=>7; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 12 | ▶ | use constant FOUND=>8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 12 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 12 | our $DEBUG; ### newly added | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub new { | ||||||||||||||||||||||||||||
| 25 | 0 | ▶ | my ($class,$iterator,$min,$max)=@_; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | confess "usage: $class->new(\$iterator,\$min,\$max)" unless UNIVERSAL::DOES::does($iterator,'PMLTQ::Relation::Iterator'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | $min||=1; | ||||||||||||||||||||||||||
|
Condition0%0/2
$min ||= 1
MC/DC0%0/1
$min ||= 1
$min
Truth table0%0/2
A
A= $min
| |||||||||||||||||||||||||||||
| 28 | 0 | my $conditions = $iterator->conditions; | |||||||||||||||||||||||||||
| 29 | 0 | ▶ | $iterator->set_conditions(sub{1}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | warn "blessed $iterator,$min,$max,0.\n" if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 0 | return bless [$conditions,$iterator,$min,$max,0],$class; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | sub clone { | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | my ($self)=@_; | ||||||||||||||||||||||||||
|
Subroutine
clone: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | 0 | return bless [$self->[CONDITIONS],$self->[ITERATOR],$self->[MIN],$self->[MAX]], ref($self); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | sub start { | ||||||||||||||||||||||||||||
| 38 | 0 | ▶ | my ($self,$parent,$fsfile)=@_; | ||||||||||||||||||||||||||
|
Subroutine
start: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 39 | 0 | $self->[FILE]=$fsfile; | |||||||||||||||||||||||||||
| 40 | 0 | my $conditions = $self->[CONDITIONS]; | |||||||||||||||||||||||||||
| 41 | 0 | my $seen = $self->[SEEN]={}; | |||||||||||||||||||||||||||
| 42 | 0 | my $iter = $self->[ITERATOR]->clone(); | |||||||||||||||||||||||||||
| 43 | 0 | my $iterators = $self->[ITER_STACK]=[ $iter ]; | |||||||||||||||||||||||||||
| 44 | 0 | my $n = $iter->start($parent,$fsfile); | |||||||||||||||||||||||||||
| 45 | 0 | $seen->{$n}=1; | |||||||||||||||||||||||||||
| 46 | 0 | $self->[DEPTH] = 1; | |||||||||||||||||||||||||||
| 47 | 0 | my $found = $self->[FOUND]={}; | |||||||||||||||||||||||||||
| 48 | 0 | ▶ | warn "START $parent->{id},".($n?$n->{id}:q//).".\n" if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | $found->{$n}=1 if $conditions->($n, $iter->file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | return $self->[MIN]<=1 && scalar(keys %$found) > 0 ? $n : $self->next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->[2] <= 1 and scalar keys %$found > 0
MC/DC0%0/2
$self->[2] <= 1 and scalar keys %$found > 0
$self->[2] <= 1
scalar keys %$found > 0
Truth table0%0/3
A and B
A= $self->[2] <= 1B= scalar keys %$found > 0
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | sub next { | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my ($self)=@_; | ||||||||||||||||||||||||||
|
Subroutine
next: not called CC=23 SCAR=63.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 0 | my $depth = $self->[DEPTH]; | |||||||||||||||||||||||||||
| 55 | 0 | my $found = $self->[FOUND]; | |||||||||||||||||||||||||||
| 56 | 0 | ▶ | return if $depth<1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | my $seen = $self->[SEEN]; | |||||||||||||||||||||||||||
| 58 | 0 | my $conditions = $self->[CONDITIONS]; | |||||||||||||||||||||||||||
| 59 | 0 | my $iterators = $self->[ITER_STACK]; | |||||||||||||||||||||||||||
| 60 | 0 | my $iter = $iterators->[-1]; | |||||||||||||||||||||||||||
| 61 | 0 | my $min = $self->[MIN]; | |||||||||||||||||||||||||||
| 62 | 0 | my $max = $self->[MAX]; | |||||||||||||||||||||||||||
| 63 | 0 | my $n = $iter->node; | |||||||||||||||||||||||||||
| 64 | 0 | ▶ | warn "NEXT: $min,$max: $n->{id} (depth $depth)\n" if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | while ($n) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | if (!$max or $depth<$max) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $max or $depth < $max
MC/DC0%0/2
not $max or $depth < $max
not $max
$depth < $max
Truth table0%0/3
not(A) or B
A= not $maxB= $depth < $max
| |||||||||||||||||||||||||||||
| 67 | # prolong the iteratator (depth first) | ||||||||||||||||||||||||||||
| 68 | 0 | my $new_it = $self->[ITERATOR]->clone(); | |||||||||||||||||||||||||||
| 69 | 0 | my $new_n = $new_it->start($n,$self->[FILE]); | |||||||||||||||||||||||||||
| 70 | 0 | ▶ | $new_n = $new_it->next while ($new_n and $seen->{$new_n}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$new_n and $seen->{$new_n}
MC/DC0%0/2
$new_n and $seen->{$new_n}
$new_n
$seen->{$new_n}
Truth table0%0/3
A and B
A= $new_nB= $seen->{$new_n}
| |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | warn "NEWN $n->{id}" if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ($new_n) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | push @$iterators, $new_it; | |||||||||||||||||||||||||||
| 74 | 0 | $n = $new_n; | |||||||||||||||||||||||||||
| 75 | 0 | $seen->{$n}=1; | |||||||||||||||||||||||||||
| 76 | 0 | $iter = $new_it; | |||||||||||||||||||||||||||
| 77 | 0 | $depth ++; | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | $found->{$n}=1 if $conditions->($new_n, $new_it->file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | next if scalar(keys %$found)<$min; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | last; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | # continue top-level iterator (go breadth) | ||||||||||||||||||||||||||||
| 84 | 0 | delete $seen->{$n}; | |||||||||||||||||||||||||||
| 85 | 0 | $n = $iter->next; | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | $n = $n->next while ($n and $seen->{$n}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$n and $seen->{$n}
MC/DC0%0/2
$n and $seen->{$n}
$n
$seen->{$n}
Truth table0%0/3
A and B
A= $nB= $seen->{$n}
| |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | while (!$n and $depth>1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $n and $depth > 1
MC/DC0%0/2
not $n and $depth > 1
not $n
$depth > 1
Truth table0%0/3
not(A) and B
A= not $nB= $depth > 1
| |||||||||||||||||||||||||||||
| 88 | 0 | pop @$iterators; # drop current iterator | |||||||||||||||||||||||||||
| 89 | 0 | $depth --; | |||||||||||||||||||||||||||
| 90 | 0 | $iter = $iterators->[-1]; # current top-level iterator | |||||||||||||||||||||||||||
| 91 | 0 | delete $seen->{ $iter->node }; | |||||||||||||||||||||||||||
| 92 | 0 | delete $found->{ $iter->node }; | |||||||||||||||||||||||||||
| 93 | 0 | $n = $iter->next; | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | $n = $n->next while ($n and $seen->{$n}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$n and $seen->{$n}
MC/DC0%0/2
$n and $seen->{$n}
$n
$seen->{$n}
Truth table0%0/3
A and B
A= $nB= $seen->{$n}
| |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | $seen->{$n}=1 if $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | last unless (scalar(keys %$found)<$min and | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
scalar keys %$found < $min and scalar keys %$found >= 1
MC/DC0%0/2
scalar keys %$found < $min and scalar keys %$found >= 1
scalar keys %$found < $min
scalar keys %$found >= 1
Truth table0%0/3
A and B
A= scalar keys %$found < $minB= scalar keys %$found >= 1
| |||||||||||||||||||||||||||||
| 98 | scalar(keys %$found)>=1); # prolong if found but not long enough | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 0 | $self->[DEPTH]=$depth; | |||||||||||||||||||||||||||
| 101 | 0 | ▶ | warn 'NEXT RETURN ',$n?$n->{id}:'empty',".\n" if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 102 | 0 | return $n; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | sub node { | ||||||||||||||||||||||||||||
| 105 | 0 | ▶ | my ($self)=@_; | ||||||||||||||||||||||||||
|
Subroutine
node: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if ($self->[DEPTH]>0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | return $self->[ITER_STACK][-1]->node; | |||||||||||||||||||||||||||
| 108 | } else { | ||||||||||||||||||||||||||||
| 109 | 0 | return undef; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | sub file { | ||||||||||||||||||||||||||||
| 113 | 0 | ▶ | my ($self)=@_; | ||||||||||||||||||||||||||
|
Subroutine
file: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if ($self->[DEPTH]>0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | return $self->[ITER_STACK][-1]->file; | |||||||||||||||||||||||||||
| 116 | } else { | ||||||||||||||||||||||||||||
| 117 | 0 | return undef; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | sub reset { | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | my ($self)=@_; | ||||||||||||||||||||||||||
|
Subroutine
reset: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 122 | 0 | $self->[FILE]=undef; | |||||||||||||||||||||||||||
| 123 | 0 | $self->[ITER_STACK]=undef; | |||||||||||||||||||||||||||
| 124 | 0 | $self->[DEPTH]=undef; | |||||||||||||||||||||||||||
| 125 | 0 | $self->[SEEN]=undef; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 12 | 1; # End of PMLTQ::Relation::TransitiveIterator | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | __END__ | ||||||||||||||||||||||||||||