File Coverage

blib/lib/PDL/Apply.pm
Criterion Covered Total %
statement 46 46 100.0
branch 10 10 100.0
condition n/a
subroutine 8 8 100.0
pod 3 3 100.0
total 67 67 100.0


line stmt bran cond sub pod time code
1             package PDL::Apply;
2              
3 2     2   295146 use strict;
  2         4  
  2         50  
4 2     2   10 use warnings;
  2         3  
  2         203  
5              
6             require Exporter;
7             our @ISA = qw(Exporter);
8             our @EXPORT_OK = qw(apply_rolling apply_over apply_slice);
9             our %EXPORT_TAGS = (all => \@EXPORT_OK);
10              
11             our $VERSION = '0.002';
12              
13             sub import {
14 2     2   16 my $package = shift;
15             {
16 2     2   10 no strict 'refs';
  2         7  
  2         455  
  2         5  
17 2 100       7 *{'PDL::apply_rolling'} = \&apply_rolling if grep { /^(:all|apply_rolling)$/ } @_;
  1         5  
  1         10  
18 2 100       7 *{'PDL::apply_over'} = \&apply_over if grep { /^(:all|apply_over)$/ } @_;
  1         4  
  1         6  
19 2 100       8 *{'PDL::apply_slice'} = \&apply_slice if grep { /^(:all|apply_slice)$/ } @_;
  1         5  
  1         7  
20             }
21 2 100       2663 __PACKAGE__->export_to_level(1, $package, @_) if @_;
22             }
23              
24 2     2   891 use PDL;
  2         11  
  2         12  
25              
26             thread_define '_apply_over(data(n);[o]output()),NOtherPars=>2', over {
27             # args: $data, $output, $func_name, \@func_args
28             my $func = $_[2];
29             my $args = $_[3];
30             if (ref $func) {
31             $_[1] .= PDL::Core::topdl($func->($_[0], @$args));
32             }
33             else {
34             $_[1] .= PDL::Core::topdl($_[0]->$func(@$args));
35             }
36             };
37              
38             thread_define '_apply_slice_ND(data(n);sl(2,m);[o]output(m)),NOtherPars=>2', over {
39             # args: $data, $slices, $output, $func_name, \@func_args
40             _apply_slice_1D($_[1], ones($_[0]->type), my $output = null, $_[0], $_[3], $_[4]);
41             $_[2] .= $output;
42             };
43              
44             thread_define '_apply_slice_1D(slices(n);dummy();[o]output()),NOtherPars=>3', over {
45             # args: $slices, $dummy, $output, $data, $func_name, \@func_args
46             # XXX-HACK: $dummy is workaround to avoid the output piddle to be of type 'indx'
47             # XXX-HACK: in fact this whole function is one big hack
48             my $func = $_[4];
49             my $args = $_[5];
50             my $data = slice($_[3], $_[0]->unpdl);
51             if ($data->ngood == 0) {
52             $_[2] .= PDL->new('BAD');
53             }
54             else {
55             if (ref $func) {
56             $_[2] .= PDL::Core::topdl($func->($data, @$args));
57             }
58             else {
59             $_[2] .= PDL::Core::topdl($data->$func(@$args));
60             }
61             }
62             };
63              
64             sub apply_rolling {
65 2     2 1 1671 my ($pdl, $width, $func, @fargs) = @_;
66 2         9 my @d = $pdl->dims;
67 2         41 my $n = shift @d;
68 2         7 my $start = sequence(indx, $n - $width + 1);
69 2         278 my $end = $start + $width - 1;
70 2         23 my $ind = cat($start, $end)->transpose;
71 2         326 my $result = apply_slice($pdl, $ind, $func, @fargs);
72 2         8 my $bad_start = zeroes($pdl->type, $width - 1, @d);
73 2         145 $bad_start .= PDL->new('BAD');
74 2         925 return $bad_start->glue(0, $result);
75             }
76              
77             sub apply_slice {
78 4     4 1 2238 my ($pdl, $slices, $func, @fargs) = @_;
79 4         11 my $result = null;
80 4         47 $result->badflag(1);
81 4 100       10 if ($pdl->dims > 1) {
82 2         98 _apply_slice_ND($pdl, $slices, $result, $func, \@fargs);
83             }
84             else {
85 2         44 _apply_slice_1D($slices, ones($pdl->type), $result, $pdl, $func, \@fargs);
86             }
87 4         247 return $result;
88             }
89              
90             sub apply_over {
91 2     2 1 251 my ($pdl, $func, @fargs) = @_;
92 2         6 my $result = null;
93 2         36 $result->badflag(1);
94 2         57 _apply_over($pdl, $result, $func, \@fargs);
95 2         425 return $result;
96             }
97              
98             1;
99              
100             __END__