Skip to content

Commit 937aa06

Browse files
committed
factor _one2nd out of whichND
1 parent 6fbfd4c commit 937aa06

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

lib/PDL/Primitive.pd

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3304,7 +3304,7 @@ will not give the answer you expect.
33043304

33053305
*one2nd = \&PDL::one2nd;
33063306
sub PDL::one2nd {
3307-
barf "Usage: one2nd \$array \$indices\n" if @_ != 2;
3307+
barf "Usage: one2nd \$array, \$indices\n" if @_ != 2;
33083308
my ($x, $ind)=@_;
33093309
my @dimension=$x->dims;
33103310
$ind = indx($ind);
@@ -3789,6 +3789,21 @@ L<PDL::Slices/indexND> can be fed the coordinates to return the values.
37893789

37903790
=cut
37913791

3792+
sub _one2nd {
3793+
my ($mask, $ind) = @_;
3794+
my $ndims = my @mdims = $mask->dims;
3795+
# In the empty case, explicitly return the correct type of structured empty
3796+
return PDL->new_from_specification(indx, $ndims, 0) if !$ind->nelem;
3797+
my $mult = ones(indx, $ndims);
3798+
$mult->index($_+1) .= $mult->index($_) * $mdims[$_] for 0..$#mdims-1;
3799+
for my $i (0..$#mdims) {
3800+
my $s = $ind->index($i);
3801+
$s /= $mult->index($i);
3802+
$s %= $mdims[$i];
3803+
}
3804+
$ind;
3805+
}
3806+
37923807
*whichND = \&PDL::whichND;
37933808
sub PDL::whichND {
37943809
my $mask = PDL->topdl(shift);
@@ -3805,29 +3820,12 @@ sub PDL::whichND {
38053820
}
38063821

38073822
# Scalar context: generate an N-D index ndarray
3808-
return PDL->new_from_specification(indx,$mask->ndims,0) if !$mask->nelem;
3809-
return $mask ? pdl(indx,0) : PDL->new_from_specification(indx,0)
3810-
if !$mask->getndims;
3811-
3812-
my $ind = $mask->flat->which->dummy(0,$mask->getndims)->make_physical;
3813-
# In the empty case, explicitly return the correct type of structured empty
3814-
return PDL->new_from_specification(indx,$mask->ndims, 0) if !$ind->nelem;
3815-
3816-
my $mult = ones(indx, $mask->getndims);
3817-
my @mdims = $mask->dims;
3818-
3819-
for my $i (0..$#mdims-1) {
3820-
# use $tmp for 5.005_03 compatibility
3821-
(my $tmp = $mult->index($i+1)) .= $mult->index($i)*$mdims[$i];
3822-
}
3823-
3824-
for my $i (0..$#mdims) {
3825-
my($s) = $ind->index($i);
3826-
$s /= $mult->index($i);
3827-
$s %= $mdims[$i];
3828-
}
3823+
my $ndims = $mask->getndims;
3824+
return PDL->new_from_specification(indx,$ndims,0) if !$mask->nelem;
3825+
return $mask ? pdl(indx,0) : PDL->new_from_specification(indx,0) if !$ndims;
38293826

3830-
return $ind;
3827+
my $ind = $mask->flat->which->dummy(0,$ndims)->make_physical;
3828+
_one2nd($mask, $ind);
38313829
}
38323830
EOD
38333831

t/primitive-selector.t

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,15 @@ subtest 'which' => sub {
130130
my $r = xvals( 10, 10 ) + 10 * yvals( 10, 10 );
131131
my $x = whichND( $r % 12 == 0 );
132132

133+
my $got;
133134
is_deeply(
134-
$x->unpdl,
135+
$got = $x->unpdl,
135136
[
136137
[ 0, 0 ], [ 2, 1 ], [ 4, 2 ], [ 6, 3 ],
137138
[ 8, 4 ], [ 0, 6 ], [ 2, 7 ], [ 4, 8 ],
138139
[ 6, 9 ]
139140
]
140-
);
141+
) or diag 'got: ', explain $got;
141142
is $x->type, 'indx', 'returns indx-type';
142143
};
143144

0 commit comments

Comments
 (0)