| 1 | package Math::SimpleHisto::XS::Named; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Math::SimpleHisto::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use vars qw($AUTOLOAD); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 13 | 1 | my %param = @_; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | my $names = $param{names}; | |||||||||||||||||||||||||||
| 16 | 1 | ▶ | croak("Need list of bin names!") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$names and ref $names eq "ARRAY"
MC/DC0%0/2
$names and ref $names eq "ARRAY"
$names
ref $names eq "ARRAY"
Truth table33%1/3
A and B
A= $namesB= ref $names eq "ARRAY"
| |||||||||||||||||||||||||||||
| 17 | if not $names or not ref($names) eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | my $nbins = @$names; | |||||||||||||||||||||||||||
| 20 | 1 | my $hist = Math::SimpleHisto::XS->new( | |||||||||||||||||||||||||||
| 21 | nbins => $nbins, | ||||||||||||||||||||||||||||
| 22 | min => 0, | ||||||||||||||||||||||||||||
| 23 | max => $nbins, | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | my %namehash; | |||||||||||||||||||||||||||
| 27 | 1 | my $i = 0; | |||||||||||||||||||||||||||
| 28 | 1 | $namehash{$_} = $i++ for @$names; | |||||||||||||||||||||||||||
| 29 | 1 | my $self = bless( | |||||||||||||||||||||||||||
| 30 | { | ||||||||||||||||||||||||||||
| 31 | # careful about the cloning logic below and dump() | ||||||||||||||||||||||||||||
| 32 | hist => $hist, | ||||||||||||||||||||||||||||
| 33 | names => [@$names], | ||||||||||||||||||||||||||||
| 34 | namehash => \%namehash, | ||||||||||||||||||||||||||||
| 35 | } => $class | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | return $self; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # Generate bin-number-as-first-parameter delegation | ||||||||||||||||||||||||||||
| 42 | 1 | foreach my $methname (qw( | |||||||||||||||||||||||||||
| 43 | bin_content set_bin_content | ||||||||||||||||||||||||||||
| 44 | )) { | ||||||||||||||||||||||||||||
| 45 | 12 | ▶ | my $sub = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 46 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 47 | 12 | my $name = shift; | |||||||||||||||||||||||||||
| 48 | 12 | ▶ | croak("Invalid bin name '$name'") if not exists $self->{namehash}{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 12 | my $ibin = $self->{namehash}{$name}; | |||||||||||||||||||||||||||
| 50 | 12 | return $self->{hist}->$methname($ibin, @_); | |||||||||||||||||||||||||||
| 51 | 2 | }; | |||||||||||||||||||||||||||
| 52 | SCOPE: { | ||||||||||||||||||||||||||||
| 53 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 2 | *{"$methname"} = $sub; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # generate cloning delegation | ||||||||||||||||||||||||||||
| 59 | 1 | foreach my $methname (qw(clone new_alike)) { | |||||||||||||||||||||||||||
| 60 | 3 | ▶ | my $sub = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 62 | my $clone = bless({ | ||||||||||||||||||||||||||||
| 63 | %$self, | ||||||||||||||||||||||||||||
| 64 | 3 | names => [@{$self->{names}}], | |||||||||||||||||||||||||||
| 65 | 3 | namehash => {%{$self->{namehash}}}, | |||||||||||||||||||||||||||
| 66 | 3 | hist => $self->{hist}->$methname(@_), | |||||||||||||||||||||||||||
| 67 | } => ref($self)); | ||||||||||||||||||||||||||||
| 68 | 3 | return $clone; | |||||||||||||||||||||||||||
| 69 | 2 | }; | |||||||||||||||||||||||||||
| 70 | SCOPE: { | ||||||||||||||||||||||||||||
| 71 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | 2 | *{"$methname"} = $sub; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # Generate methods that croak because they make no sense | ||||||||||||||||||||||||||||
| 77 | # on named bins | ||||||||||||||||||||||||||||
| 78 | 1 | foreach my $methname (qw( | |||||||||||||||||||||||||||
| 79 | find_bin min max binsize width | ||||||||||||||||||||||||||||
| 80 | new_from_bin_range new_alike_from_bin_range | ||||||||||||||||||||||||||||
| 81 | integral | ||||||||||||||||||||||||||||
| 82 | rand | ||||||||||||||||||||||||||||
| 83 | bin_center bin_lower_boundary bin_upper_boundary | ||||||||||||||||||||||||||||
| 84 | bin_centers bin_lower_boundaries bin_upper_boundaries | ||||||||||||||||||||||||||||
| 85 | )) { | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | my $sub = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 87 | 0 | croak("The '$methname' method makes little sense for named bins"); | |||||||||||||||||||||||||||
| 88 | 15 | }; | |||||||||||||||||||||||||||
| 89 | SCOPE: { | ||||||||||||||||||||||||||||
| 90 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | 15 | *{"$methname"} = $sub; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 5 | ▶ | sub fill { | ||||||||||||||||||||||||||
|
Subroutine
fill: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 98 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 99 | 5 | ▶ | croak("Need at least one argument") if not @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 5 | my ($x, $w) = @_; | |||||||||||||||||||||||||||
| 101 | 5 | my $hist = $self->{hist}; | |||||||||||||||||||||||||||
| 102 | 5 | my $namehash = $self->{namehash}; | |||||||||||||||||||||||||||
| 103 | 5 | ▶ | if (ref($x) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 2 | $x = [map $namehash->{$_}, @$x]; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | else { | ||||||||||||||||||||||||||||
| 107 | 3 | $x = $namehash->{$x}; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 5 | ▶ | return $hist->fill($x, defined($w) ? ($w) : ()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | *fill_by_bin = \&fill; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub get_bin_names { | ||||||||||||||||||||||||||
|
Subroutine
get_bin_names: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 115 | 0 | return @{ $_[0]->{names} }; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # Not a fan, but unless I find an elegant way to attach more data to the | ||||||||||||||||||||||||||||
| 119 | # XS object, I can't think of anything else. Retrofitting XS::Object::Magic | ||||||||||||||||||||||||||||
| 120 | # to the SimpleHisto implementation is too annoying. | ||||||||||||||||||||||||||||
| 121 | 6 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 122 | 6 | my $self = $_[0]; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 6 | my $methname = $AUTOLOAD; | |||||||||||||||||||||||||||
| 125 | 6 | ▶ | $methname =~ /^(.*)::([^:]+)$/ or die "Should not happen"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 6 | (my $class, $methname) = ($1, $2); | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 6 | my $hist = $self->{hist}; | |||||||||||||||||||||||||||
| 129 | 6 | ▶ | if ($hist->can($methname)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 51 | ▶ | my $delegate = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 131 | 51 | my $self = shift; | |||||||||||||||||||||||||||
| 132 | 51 | return $self->{hist}->$methname(@_); | |||||||||||||||||||||||||||
| 133 | 6 | }; | |||||||||||||||||||||||||||
| 134 | SCOPE: { | ||||||||||||||||||||||||||||
| 135 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 136 | 6 | *{"$methname"} = $delegate; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 6 | goto &$methname; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 0 | croak(qq{Can't locate object method "$methname" via package "$class"}); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 1 | ▶ | sub dump { | ||||||||||||||||||||||||||
|
Subroutine
dump: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 144 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 145 | 1 | my $type = lc(shift); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 1 | my $hist_dump = $self->{hist}->dump($type); | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | my $rv = $Math::SimpleHisto::XS::JSON->encode({ | ||||||||||||||||||||||||||||
| 150 | %$self, | ||||||||||||||||||||||||||||
| 151 | hist => $hist_dump, | ||||||||||||||||||||||||||||
| 152 | class => ref($self), | ||||||||||||||||||||||||||||
| 153 | histclass => ref($self->{hist}) | ||||||||||||||||||||||||||||
| 154 | 1 | }); | |||||||||||||||||||||||||||
| 155 | 1 | return $rv; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 1 | ▶ | sub new_from_dump { | ||||||||||||||||||||||||||
|
Subroutine
new_from_dump: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 159 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 160 | 1 | my $type = lc(shift); | |||||||||||||||||||||||||||
| 161 | 1 | my $data = shift; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 1 | my $struct = $Math::SimpleHisto::XS::JSON->decode($data); | |||||||||||||||||||||||||||
| 164 | 1 | $class = delete $struct->{class}; | |||||||||||||||||||||||||||
| 165 | 1 | my $hclass = delete $struct->{histclass}; | |||||||||||||||||||||||||||
| 166 | 1 | $struct->{hist} = $hclass->new_from_dump($type, delete $struct->{hist}); | |||||||||||||||||||||||||||
| 167 | 1 | return bless($struct => $class); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | # Can't simply be delegated, eventhough the implementation is the same :( | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | sub STORABLE_freeze { | ||||||||||||||||||||||||||
|
Subroutine
STORABLE_freeze: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 172 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 173 | 0 | my $cloning = shift; | |||||||||||||||||||||||||||
| 174 | 0 | my $serialized = $self->dump('simple'); | |||||||||||||||||||||||||||
| 175 | 0 | return $serialized; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # Can't simply be delegated, eventhough the implementation is the same :( | ||||||||||||||||||||||||||||
| 179 | 0 | ▶ | sub STORABLE_thaw { | ||||||||||||||||||||||||||
|
Subroutine
STORABLE_thaw: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 180 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 181 | 0 | my $cloning = shift; | |||||||||||||||||||||||||||
| 182 | 0 | my $serialized = shift; | |||||||||||||||||||||||||||
| 183 | 0 | my $new = ref($self)->new_from_dump('simple', $serialized); | |||||||||||||||||||||||||||
| 184 | 0 | $$self = $$new; | |||||||||||||||||||||||||||
| 185 | 0 | $new = undef; # need to care about DESTROY here, normally | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 0 | ▶ | sub DESTROY {} | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 1 | 1; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | __END__ | ||||||||||||||||||||||||||||