| 1 | package Algorithm::BreakOverlappingRectangles; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | our $VERSION = '0.01'; | |||||||||
| 8 | |||||||||||
| 9 | 1 | require XSLoader; | |||||||||
| 10 | 1 | XSLoader::load('Algorithm::BreakOverlappingRectangles', $VERSION); | |||||||||
| 11 | } | ||||||||||
| 12 | |||||||||||
| 13 | |||||||||||
| 14 | 1 | ▶ | use constant X0 => 0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | ▶ | use constant Y0 => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | ▶ | use constant X1 => 2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | use constant Y1 => 3; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 1 | our $verbose = 0; | |||||||||
| 20 | |||||||||||
| 21 | 1 | ▶ | use constant NVSIZE => length pack F => 1.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 1 | ▶ | use constant IDOFFSET => NVSIZE * 4; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 5 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 25 | 5 | my $class = shift; | |||||||||
| 26 | 5 | my $self = { rects => [], | |||||||||
| 27 | name2id => {}, | ||||||||||
| 28 | names => [], | ||||||||||
| 29 | n => 0 }; | ||||||||||
| 30 | 5 | bless $self, $class; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 1005 | ▶ | sub add_rectangle { | ||||||||
|
Subroutine
add_rectangle: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 34 | 1005 | my ($self, $x0, $y0, $x1, $y1, @names) = @_; | |||||||||
| 35 | |||||||||||
| 36 | 1005 | ▶ | ($x0, $x1) = ($x1, $x0) if $x0 > $x1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 1005 | ▶ | ($y0, $y1) = ($y1, $y0) if $y0 > $y1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | |||||||||||
| 39 | 1005 | my @ids; | |||||||||
| 40 | 1005 | for (@names) { | |||||||||
| 41 | 1005 | my $id = $self->{name2id}{$_}; | |||||||||
| 42 | 1005 | ▶ | unless (defined $id) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 1005 | $id = $self->{name2id}{$_} = @{$self->{names}}; | |||||||||
| 44 | 1005 | push @{$self->{names}}, $_; | |||||||||
| 45 | } | ||||||||||
| 46 | 1005 | push @ids, $id; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 1005 | push @{$self->{rects}}, pack 'F4L*' => $x0, $y0, $x1, $y1, @ids; | |||||||||
| 50 | 1005 | delete $self->{broken}; | |||||||||
| 51 | 1005 | ++($self->{n}); | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 5 | ▶ | sub _do_break { | ||||||||
|
Subroutine
_do_break: called CC=1 SCAR=0.0
| |||||||||||
| 55 | 5 | my $self = shift; | |||||||||
| 56 | 5 | _break_rectangles $self->{rects}; | |||||||||
| 57 | 5 | $self->{broken} = 1; | |||||||||
| 58 | 5 | $self->{iter} = 0; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 1 | * _brute_force_break = \&_brute_force_break_xs; | |||||||||
| 62 | |||||||||||
| 63 | 0 | ▶ | sub dump { | ||||||||
|
Subroutine
dump: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 64 | 0 | my $self = shift; | |||||||||
| 65 | 0 | ▶ | $self->_do_break unless $self->{broken}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 0 | for (@{$self->{rects}}) { | |||||||||
| 67 | 0 | my ($x0, $y0, $x1, $y1, @ids) = unpack 'F4L*' => $_; | |||||||||
| 68 | 0 | my @names = map $self->{names}[$_], @ids; | |||||||||
| 69 | # my @names = @ids; | ||||||||||
| 70 | 0 | print "[$x0 $y0 $x1 $y1 | @names]\n"; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 0 | print "$self->{n} rectangles broken into ".(scalar @{$self->{rects}})."\n"; | |||||||||
| 74 | |||||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 0 | ▶ | sub dump_stats { | ||||||||
|
Subroutine
dump_stats: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 78 | 0 | my $self = shift; | |||||||||
| 79 | 0 | ▶ | $self->_do_break unless $self->{broken}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 0 | print "$self->{n} rectangles broken into ".(scalar @{$self->{rects}})."\n"; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 0 | ▶ | sub get_rectangles { | ||||||||
|
Subroutine
get_rectangles: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 84 | 0 | my $self = shift; | |||||||||
| 85 | 0 | ▶ | $self->_do_break unless $self->{broken}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 0 | my $names = $self->{names}; | |||||||||
| 87 | map { | ||||||||||
| 88 | 0 | my @a = unpack "F4I*" => $_; | |||||||||
| 89 | 0 | $a[$_] = $names->[$a[$_]] for (4..$#a); | |||||||||
| 90 | 0 | \@a; | |||||||||
| 91 | 0 | } @{$self->{rects}} | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | |||||||||||
| 95 | 5 | ▶ | sub get_rectangles_as_array_ref { | ||||||||
|
Subroutine
get_rectangles_as_array_ref: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 96 | 5 | my $self = shift; | |||||||||
| 97 | 5 | tie my @iter, 'Algorithm::BreakOverlappingRectangles::Iterator', $self; | |||||||||
| 98 | 5 | return \@iter; | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | package Algorithm::BreakOverlappingRectangles::Iterator; | ||||||||||
| 102 | |||||||||||
| 103 | 1 | ▶ | use base 'Tie::Array'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 104 | |||||||||||
| 105 | 5 | ▶ | sub TIEARRAY { | ||||||||
|
Subroutine
TIEARRAY: called CC=1 SCAR=0.0
| |||||||||||
| 106 | 5 | my ($class, $abor) = @_; | |||||||||
| 107 | 5 | my $self = bless \$abor, $class; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 122650 | ▶ | sub FETCH { | ||||||||
|
Subroutine
FETCH: called CC=3 SCAR=11.3
| |||||||||||
| 111 | 122650 | my ($self, $index) = @_; | |||||||||
| 112 | 122650 | my $abor = $$self; | |||||||||
| 113 | 122650 | ▶ | $abor->_do_break unless $abor->{broken}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 122650 | my $r = $abor->{rects}[$index]; | |||||||||
| 115 | 122650 | ▶ | if (defined $r) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | # print "."; | ||||||||||
| 117 | 122650 | my $names = $abor->{names}; | |||||||||
| 118 | 122650 | my ($x0, $y0, $x1, $y1, @ids) = unpack 'F4I*' => $r; | |||||||||
| 119 | 122650 | return [$x0, $y0, $x1, $y1, map($names->[$_], @ids)]; | |||||||||
| 120 | } | ||||||||||
| 121 | () | ||||||||||
| 122 | 0 | } | |||||||||
| 123 | |||||||||||
| 124 | 0 | ▶ | sub EXISTS { | ||||||||
|
Subroutine
EXISTS: not called CC=2 SCAR=17.9
| |||||||||||
| 125 | 0 | my ($self, $index) = @_; | |||||||||
| 126 | 0 | my $abor = $$self; | |||||||||
| 127 | 0 | ▶ | $abor->_do_break unless $abor->{broken}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | 0 | my $rects = $abor->{rects}; | |||||||||
| 129 | 0 | return (@$rects > $index); | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 122432 | ▶ | sub FETCHSIZE { | ||||||||
|
Subroutine
FETCHSIZE: called CC=2 SCAR=6.9
| |||||||||||
| 133 | 122432 | my ($self) = @_; | |||||||||
| 134 | 122432 | my $abor = $$self; | |||||||||
| 135 | 122432 | ▶ | $abor->_do_break unless $abor->{broken}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 122432 | my $rects = $abor->{rects}; | |||||||||
| 137 | 122432 | return scalar(@$rects); | |||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | 0 | ▶ | sub PUSH { | ||||||||
|
Subroutine
PUSH: not called CC=2 SCAR=17.9
| |||||||||||
| 141 | 0 | my $self = shift; | |||||||||
| 142 | 0 | $self->[0] = 0; | |||||||||
| 143 | 0 | my $abor = $$self; | |||||||||
| 144 | 0 | $abor->add_rectangle(@$_) for (@_); | |||||||||
| 145 | 0 | 1; | |||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | |||||||||||
| 149 | 1 | 1; | |||||||||
| 150 | |||||||||||
| 151 | __END__ | ||||||||||