File Coverage

blib/lib/PDL/NDBin/Action/Sum.pm
Criterion Covered Total %
statement 32 32 100.0
branch 9 10 90.0
condition n/a
subroutine 8 8 100.0
pod 3 3 100.0
total 52 53 98.1


line stmt bran cond sub pod time code
1             package PDL::NDBin::Action::Sum;
2             # ABSTRACT: Action for PDL::NDBin that computes sum
3             $PDL::NDBin::Action::Sum::VERSION = '0.020';
4              
5 2     2   6051 use strict;
  2         40  
  2         66  
6 2     2   12 use warnings;
  2         4  
  2         62  
7 2     2   11 use PDL::Lite; # do not import any functions into this namespace
  2         15  
  2         14  
8 2     2   1073 use PDL::NDBin::Actions_PP;
  2         5  
  2         18  
9 2     2   334 use Params::Validate qw( validate OBJECT SCALAR UNDEF );
  2         5  
  2         807  
10              
11              
12             sub new
13             {
14 41     41 1 6423 my $class = shift;
15 41         730 my $self = validate( @_, {
16             N => { type => SCALAR, regex => qr/^\d+$/ },
17             type => { type => OBJECT | UNDEF, isa => 'PDL::Type', optional => 1 }
18             } );
19 41         884 return bless $self, $class;
20             }
21              
22              
23             sub process
24             {
25 53     53 1 136 my $self = shift;
26 53         70 my $iter = shift;
27 53 100       157 if( ! defined $self->{out} ) {
28 41         62 my $type = $self->{type};
29 41 100       85 if( ! defined $type ) {
30 33 100       86 $type = $iter->data->type < PDL::long() ? PDL::long : $iter->data->type;
31             }
32 41         897 $self->{out} = PDL->zeroes( $type, $self->{N} );
33             }
34 53 50       2739 $self->{count} = PDL->zeroes( defined(&PDL::indx) ? PDL::indx() : PDL::long, $self->{N} ) unless defined $self->{count};
    100          
35 53         2412 PDL::NDBin::Actions_PP::_isum_loop( $iter->data, $iter->idx, $self->{out}, $self->{count}, $self->{N} );
36             # as the plugin processes all bins at once, every variable
37             # needs to be visited only once
38 53         201 $iter->var_active( 0 );
39 53         157 return $self;
40             }
41              
42              
43             sub result
44             {
45 41     41 1 463 my $self = shift;
46 41         111 $self->{out}->inplace->_setnulltobad( $self->{count} );
47 41         1256 return $self->{out};
48             }
49              
50             1;
51              
52             __END__