| 1 | package Set::IntSpan::Partition; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use 5.008000; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | ▶ | use base qw(Exporter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 4 | our $VERSION = '0.02'; | |||||||||
| 9 | |||||||||||
| 10 | 4 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||
| 11 | |||||||||||
| 12 | ) ] ); | ||||||||||
| 13 | |||||||||||
| 14 | 4 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||
| 15 | |||||||||||
| 16 | 4 | our @EXPORT = qw( | |||||||||
| 17 | intspan_partition | ||||||||||
| 18 | intspan_partition_map | ||||||||||
| 19 | ); | ||||||||||
| 20 | |||||||||||
| 21 | sub _uniq (@) { | ||||||||||
| 22 | 101 | ▶ | my %h; | ||||||||
|
Subroutine
_uniq: called CC=2 SCAR=7.1
| |||||||||||
| 23 | 101 | ▶ | return map { $h{$_}++ == 0 ? $_ : () } @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | sub _add { | ||||||||||
| 27 | 452 | ▶ | my $rest = shift; | ||||||||
|
Subroutine
_add: called CC=2 SCAR=6.9
| |||||||||||
| 28 | |||||||||||
| 29 | my @parts = map { | ||||||||||
| 30 | 452 | my $old = $_; | |||||||||
| 31 | |||||||||||
| 32 | 4601 | my $right = $rest->diff($old); | |||||||||
| 33 | 4601 | my $left = $old->diff($rest); | |||||||||
| 34 | 4601 | my $both = $old->intersect($rest); | |||||||||
| 35 | |||||||||||
| 36 | 4601 | $rest = $right; | |||||||||
| 37 | |||||||||||
| 38 | 4601 | grep { !$_->empty } $left, $both | |||||||||
| 39 | |||||||||||
| 40 | } @_; | ||||||||||
| 41 | |||||||||||
| 42 | 452 | ▶ | push @parts, $rest unless $rest->empty; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 452 | return @parts; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | sub intspan_partition { | ||||||||||
| 47 | 101 | ▶ | my @parts = (); | ||||||||
|
Subroutine
intspan_partition: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 48 | |||||||||||
| 49 | 101 | @parts = _add($_, @parts) for @_; | |||||||||
| 50 | |||||||||||
| 51 | # TODO: It's not really possible to get non-unique | ||||||||||
| 52 | # items into the list? But play it safe for now. | ||||||||||
| 53 | 101 | return _uniq @parts; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | sub intspan_partition_map { | ||||||||||
| 57 | |||||||||||
| 58 | 4 | ▶ | use Heap::Simple qw//; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 59 | 4 | ▶ | use List::Util qw/min max/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 60 | 4 | ▶ | use List::MoreUtils qw/uniq/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 61 | |||||||||||
| 62 | my $heap = Heap::Simple->new(order => sub { | ||||||||||
| 63 | 391648 | ▶ | my ($x, $y) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||
| 64 | 391648 | ▶ | return 1 if $x->[0] < $y->[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 125261 | ▶ | return 0 if $x->[0] > $y->[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 31272 | ▶ | return 1 if $x->[1] < $y->[1]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 17160 | return 0; | |||||||||
| 68 | 100 | ▶ | }); | ||||||||
|
Subroutine
intspan_partition_map: called CC=12 SCAR=24.8
Pod
documented
| |||||||||||
| 69 | |||||||||||
| 70 | 100 | ▶ | for (my $ix = 0; $ix < @_; ++$ix) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 446 | my $obj = $_[$ix]; | |||||||||
| 72 | 446 | for ($obj->spans) { | |||||||||
| 73 | 9188 | $heap->insert([ $_->[0], $_->[1], [$ix] ]); | |||||||||
| 74 | } | ||||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 100 | my @result; | |||||||||
| 78 | |||||||||||
| 79 | 100 | while (1) { | |||||||||
| 80 | 15290 | my $x = $heap->extract_first; | |||||||||
| 81 | 15290 | my $y = $heap->extract_first; | |||||||||
| 82 | |||||||||||
| 83 | 15290 | ▶ | last unless defined $x; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 15277 | ▶ | push @result, $x unless defined $y; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 15277 | ▶ | last unless defined $y; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | |||||||||||
| 87 | 15190 | ▶ | if ($x->[1] < $y->[0]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 6526 | push @result, $x; | |||||||||
| 89 | 6526 | $heap->insert($y); | |||||||||
| 90 | 6526 | next; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 8664 | my $min = min($x->[1], $y->[0]); | |||||||||
| 94 | 8664 | my $max = max(min($y->[0], $x->[1]), min($x->[1], $y->[1])); | |||||||||
| 95 | 8664 | my $XandY = [ $min, $max, [ @{$x->[2]}, @{$y->[2]} ] ]; | |||||||||
| 96 | 8664 | my $prefX = [ $x->[0], $XandY->[0] - 1, $x->[2] ]; | |||||||||
| 97 | 8664 | my $suffX = [ $XandY->[1] + 1, $x->[1], $x->[2] ]; | |||||||||
| 98 | 8664 | my $onlyY = [ $XandY->[1] + 1, $y->[1], $y->[2] ]; | |||||||||
| 99 | |||||||||||
| 100 | 8664 | for ($prefX, $suffX, $onlyY, $XandY) { | |||||||||
| 101 | 34656 | ▶ | next unless $_->[0] <= $_->[1]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | 14753 | $heap->insert($_); | |||||||||
| 103 | } | ||||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | # group spans back into classes | ||||||||||
| 107 | 100 | my %group; | |||||||||
| 108 | 100 | for my $item (@result) { | |||||||||
| 109 | 6613 | my $key = join ',', uniq sort @{ $item->[2] }; | |||||||||
| 110 | 6613 | push @{ $group{$key} }, $item; | |||||||||
| 111 | } | ||||||||||
| 112 | |||||||||||
| 113 | 100 | my %map; | |||||||||
| 114 | 100 | ▶ | while (my ($k, $v) = each %group) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | my $class = Set::IntSpan->new([map { | ||||||||||
| 116 | 2583 | [ $_->[0], $_->[1] ] | |||||||||
| 117 | } @$v]); | ||||||||||
| 118 | 2583 | push @{ $map{$_} }, $class for uniq map { @{ $_->[2] } } @$v; | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | 100 | return %map; | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | 4 | 1; | |||||||||
| 125 | |||||||||||
| 126 | __END__ | ||||||||||