Math-SimpleHisto-XS
view release on metacpan - search on metacpan
view release on metacpan or search on metacpan
lib/Math/SimpleHisto/XS/Named.pm view on Meta::CPAN
$x = $namehash->{$x};
}
return $hist->fill($x, defined($w) ? ($w) : ());
}
*fill_by_bin = \&fill;
sub get_bin_names {
return @{ $_[0]->{names} };
}
# Not a fan, but unless I find an elegant way to attach more data to the
# XS object, I can't think of anything else. Retrofitting XS::Object::Magic
# to the SimpleHisto implementation is too annoying.
sub AUTOLOAD {
my $self = $_[0];
my $methname = $AUTOLOAD;
$methname =~ /^(.*)::([^:]+)$/ or die "Should not happen";
(my $class, $methname) = ($1, $2);
my $hist = $self->{hist};
if ($hist->can($methname)) {
my $delegate = sub {
my $self = shift;
return $self->{hist}->$methname(@_);
};
SCOPE: {
no strict 'refs';
*{"$methname"} = $delegate;
}
goto &$methname;
}
croak(qq{Can't locate object method "$methname" via package "$class"});
}
sub dump {
my $self = shift;
my $type = lc(shift);
my $hist_dump = $self->{hist}->dump($type);
my $rv = $Math::SimpleHisto::XS::JSON->encode({
%$self,
hist => $hist_dump,
class => ref($self),
histclass => ref($self->{hist})
});
return $rv;
}
sub new_from_dump {
my $class = shift;
my $type = lc(shift);
my $data = shift;
my $struct = $Math::SimpleHisto::XS::JSON->decode($data);
$class = delete $struct->{class};
my $hclass = delete $struct->{histclass};
$struct->{hist} = $hclass->new_from_dump($type, delete $struct->{hist});
return bless($struct => $class);
}
# Can't simply be delegated, eventhough the implementation is the same :(
sub STORABLE_freeze {
my $self = shift;
my $cloning = shift;
my $serialized = $self->dump('simple');
return $serialized;
}
# Can't simply be delegated, eventhough the implementation is the same :(
sub STORABLE_thaw {
my $self = shift;
my $cloning = shift;
my $serialized = shift;
my $new = ref($self)->new_from_dump('simple', $serialized);
$$self = $$new;
$new = undef; # need to care about DESTROY here, normally
}
sub DESTROY {}
1;
__END__
=head1 NAME
Math::SimpleHisto::XS::Named - Named histograms for Math::SimpleHisto::XS
=head1 SYNOPSIS
use Math::SimpleHisto::XS::Named;
my $hist = Math::SimpleHisto::XS::Named->new(
names => [qw(boys girls)],
);
$hist->fill('boys', 12);
$hist->fill($_) for map $_->gender, @kids;
=head1 DESCRIPTION
B<EXPERIMENTAL>
This module provides histograms with named bins. It is built on top of
L<Math::SimpleHisto::XS> and attempts to provide the same interface as
far as it makes sense to support. The following documentation covers only
the differences between the two modules, so a basic familiarity with
C<Math::SimpleHisto::XS> is required.
It is important to not attempt to use a histogram with named bins by looking
at its internal coordinates or bin numbering.
=head1 API DIFFERENCES TO Math::SimpleHisto::XS
=head2 Constructors
The regular constructor, C<new> requires one named parameter: C<names>,
an array reference of bin names.
view all matches for this distributionview release on metacpan - search on metacpan
( run in 0.447 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )