| 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
| |||||||||||||||||||||||||||||
| 22 | 18485 | ▶ | Graph::__carp_confess(__PACKAGE__ . "::_parent: bad arity") if @_ < 2 or @_ > 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ < 2 or @_ > 3
MC/DC0%0/2
@_ < 2 or @_ > 3
@_ < 2
@_ > 3
Truth table33%1/3
A or B
A= @_ < 2B= @_ > 3
| |||||||||||||||||||||||||||||
| 23 | 18485 | ▶ | if (@_ == 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 14863 | ▶ | exists $_[0]->{ $_[ 1 ] } ? $_[0]->{ $_[1] }->[ _PARENT ] : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 32 | 1010 | ▶ | Graph::__carp_confess(__PACKAGE__ . "::_rank: bad arity") if @_ < 2 or @_ > 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ < 2 or @_ > 3
MC/DC0%0/2
@_ < 2 or @_ > 3
@_ < 2
@_ > 3
Truth table33%1/3
A or B
A= @_ < 2B= @_ > 3
| |||||||||||||||||||||||||||||
| 33 | 1010 | ▶ | if (@_ == 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 968 | ▶ | exists $_[0]->{ $_[1] } ? $_[0]->{ $_[1] }->[ _RANK ] : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 45 | 7430 | ▶ | $self->_parent( $x, $self->find( $px ) ) if $px ne $x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 73 | 1622 | $fu eq $fv; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 7 | 1; | |||||||||||||||||||||||||||
| 77 | __END__ | ||||||||||||||||||||||||||||