Graph/UnionFind.pm

filter: Statementstmt 100% 44 / 44 Branchbran 82.1% 23 / 28 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 9 / 9 Podpod 100% 5 / 5 totaltotal 86.4% 83 / 96 CC 19 SCAR 15.8
CC
19
Cov
88%
_rank
7.1
_parent
6.6
union
5.0
CRAP 4.9
« Graph.pm ↑ Summary Graph/AdjacencyMap/Light.pm »
1 package Graph::UnionFind;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 sub _PARENT () { 0 }
7 sub _RANK () { 1 }
8  
9 31 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
10 31 my $class = shift;
11 31 bless { }, $class;
12 }
13  
14 507 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod documented
15 507 my ($self, @elems) = @_;
16 507 @elems = grep !defined $self->{$_}, @elems;
17 507 @$self{ @elems } = map [ $_, 0 ], @elems;
18 }
19  
20 18485 ▶sub _parent {
Subroutine _parent: called CC=6 SCAR=18.8
21 18485 ▶ return undef unless defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 0 18485 18485
22 18485 ▶ Graph::__carp_confess(__PACKAGE__ . "::_parent: bad arity") if @_ < 2 or @_ > 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 or @_ > 3 0 18485 18485
Condition33%1/3
@_ < 2 or @_ > 3
l!l&&r!l&&!r
0018485
MC/DC0%0/2
@_ < 2 or @_ > 3
@_ < 2 @_ > 3
Truth table33%1/3
A or B
A= @_ < 2B= @_ > 3
ABresult
1X1
011
000
23 18485 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 14863 3622 18485
24 14863 ▶ exists $_[0]->{ $_[ 1 ] } ? $_[0]->{ $_[1] }->[ _PARENT ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{$_[1]} ? : 14860 3 14863
25 } else {
26 3622 $_[0]->{ $_[1] }->[ _PARENT ] = $_[2];
27 }
28 }
29  
30 1010 ▶sub _rank {
Subroutine _rank: called CC=6 SCAR=19.6
31 1010 ▶ return unless defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 0 1010 1010
32 1010 ▶ Graph::__carp_confess(__PACKAGE__ . "::_rank: bad arity") if @_ < 2 or @_ > 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 or @_ > 3 0 1010 1010
Condition33%1/3
@_ < 2 or @_ > 3
l!l&&r!l&&!r
001010
MC/DC0%0/2
@_ < 2 or @_ > 3
@_ < 2 @_ > 3
Truth table33%1/3
A or B
A= @_ < 2B= @_ > 3
ABresult
1X1
011
000
33 1010 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 968 42 1010
34 968 ▶ exists $_[0]->{ $_[1] } ? $_[0]->{ $_[1] }->[ _RANK ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{$_[1]} ? : 968 0 968
35 } else {
36 42 $_[0]->{ $_[1] }->[ _RANK ] = $_[2];
37 }
38 }
39  
40 5289 ▶sub find {
Subroutine find: called CC=4 SCAR=13.9
Pod documented
41 5289 my ($self, @v) = @_;
42 5289 my @ret;
43 5289 for my $x (@v) {
44 7433 ▶ push(@ret, undef), next unless defined(my $px = $self->_parent($x));
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $px = $self->_parent($x)) 3 7430 7433
45 7430 ▶ $self->_parent( $x, $self->find( $px ) ) if $px ne $x;
Branch: 1 branch
BranchTrueFalseTotal
if $px ne $x 3138 4292 7430
46 7430 push @ret, $self->_parent( $x );
47 }
48 5289 @ret;
49 }
50  
51 475 ▶sub union {
Subroutine union: called CC=5 SCAR=16.1
Pod documented
52 475 my ($self, @edges) = @_;
53 475 $self->add(map @$_, @edges);
54 475 for my $e (@edges) {
55 500 my ($px, $py) = $self->find( @$e );
56 500 ▶ next if $px eq $py;
Branch: 1 branch
BranchTrueFalseTotal
if $px eq $py 16 484 500
57 484 my $rx = $self->_rank( $px );
58 484 my $ry = $self->_rank( $py );
59 # print "union($x, $y): px = $px, py = $py, rx = $rx, ry = $ry\n";
60 484 ▶ if ( $rx > $ry ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rx > $ry) { } 422 62 484
61 422 $self->_parent( $py, $px );
62 } else {
63 62 $self->_parent( $px, $py );
64 62 ▶ $self->_rank( $py, $ry + 1 ) if $rx == $ry;
Branch: 1 branch
BranchTrueFalseTotal
if $rx == $ry 42 20 62
65 }
66 }
67 }
68  
69 1623 ▶sub same {
Subroutine same: called CC=2 SCAR=6.9
Pod documented
70 1623 my ($uf, $u, $v) = @_;
71 1623 my ($fu, $fv) = $uf->find($u, $v);
72 1623 ▶ return undef if grep !defined, $fu, $fv;
Branch: 1 branch
BranchTrueFalseTotal
if grep((!defined($_)), $fu, $fv) 1 1622 1623
73 1622 $fu eq $fv;
74 }
75  
76 7 1;
77 __END__
« Graph.pm ↑ Summary Graph/AdjacencyMap/Light.pm »