← Index
NYTProf Performance Profile   « block view • line view • sub view »
For bin/pan_genome_post_analysis
  Run on Fri Mar 27 11:43:32 2015
Reported on Fri Mar 27 11:45:28 2015

Filename/Users/ap13/perl5/lib/perl5/Graph/UnionFind.pm
StatementsExecuted 3 statements in 484µs
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11112µs25µsGraph::UnionFind::::BEGIN@3Graph::UnionFind::BEGIN@3
0000s0sGraph::UnionFind::::_parentGraph::UnionFind::_parent
0000s0sGraph::UnionFind::::_rankGraph::UnionFind::_rank
0000s0sGraph::UnionFind::::addGraph::UnionFind::add
0000s0sGraph::UnionFind::::findGraph::UnionFind::find
0000s0sGraph::UnionFind::::hasGraph::UnionFind::has
0000s0sGraph::UnionFind::::newGraph::UnionFind::new
0000s0sGraph::UnionFind::::sameGraph::UnionFind::same
0000s0sGraph::UnionFind::::unionGraph::UnionFind::union
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Graph::UnionFind;
2
32482µs238µs
# spent 25µs (12+13) within Graph::UnionFind::BEGIN@3 which was called: # once (12µs+13µs) by Graph::BEGIN@31 at line 3
use strict;
# spent 25µs making 1 call to Graph::UnionFind::BEGIN@3 # spent 13µs making 1 call to strict::import
4
5sub _PARENT () { 0 }
6sub _RANK () { 1 }
7
8sub new {
9 my $class = shift;
10 bless { }, $class;
11}
12
13sub add {
14 my ($self, $elem) = @_;
15 $self->{ $elem } = [ $elem, 0 ] unless defined $self->{$elem};
16}
17
18sub has {
19 my ($self, $elem) = @_;
20 exists $self->{ $elem };
21}
22
23sub _parent {
24 return undef unless defined $_[1];
25 if (@_ == 2) {
26 exists $_[0]->{ $_[ 1 ] } ? $_[0]->{ $_[1] }->[ _PARENT ] : undef;
27 } elsif (@_ == 3) {
28 $_[0]->{ $_[1] }->[ _PARENT ] = $_[2];
29 } else {
30 require Carp;
31 Carp::croak(__PACKAGE__ . "::_parent: bad arity");
32 }
33}
34
35sub _rank {
36 return unless defined $_[1];
37 if (@_ == 2) {
38 exists $_[0]->{ $_[1] } ? $_[0]->{ $_[1] }->[ _RANK ] : undef;
39 } elsif (@_ == 3) {
40 $_[0]->{ $_[1] }->[ _RANK ] = $_[2];
41 } else {
42 require Carp;
43 Carp::croak(__PACKAGE__ . "::_rank: bad arity");
44 }
45}
46
47sub find {
48 my ($self, $x) = @_;
49 my $px = $self->_parent( $x );
50 return unless defined $px;
51 $self->_parent( $x, $self->find( $px ) ) if $px ne $x;
52 $self->_parent( $x );
53}
54
55sub union {
56 my ($self, $x, $y) = @_;
57 $self->add($x) unless $self->has($x);
58 $self->add($y) unless $self->has($y);
59 my $px = $self->find( $x );
60 my $py = $self->find( $y );
61 return if $px eq $py;
62 my $rx = $self->_rank( $px );
63 my $ry = $self->_rank( $py );
64 # print "union($x, $y): px = $px, py = $py, rx = $rx, ry = $ry\n";
65 if ( $rx > $ry ) {
66 $self->_parent( $py, $px );
67 } else {
68 $self->_parent( $px, $py );
69 $self->_rank( $py, $ry + 1 ) if $rx == $ry;
70 }
71}
72
73sub same {
74 my ($uf, $u, $v) = @_;
75 my $fu = $uf->find($u);
76 return undef unless defined $fu;
77 my $fv = $uf->find($v);
78 return undef unless defined $fv;
79 $fu eq $fv;
80}
81
8212µs1;
83__END__