Skip to content

Commit

Permalink
XS topdl - #451
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Feb 17, 2024
1 parent ecd148c commit 6d015dc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
12 changes: 0 additions & 12 deletions Basic/Core/Core.pm
Original file line number Diff line number Diff line change
Expand Up @@ -656,18 +656,6 @@ below for usage).
$y = topdl $ndarray; # fall through
$x = topdl (1,2,3,4); # Convert 1D array
=cut

# Convert numbers to PDL if not already
sub PDL::topdl {
return $_[0]->new(@_[1..$#_]) if
@_ > 2 # PDLify a list
or ref(\$_[1]) eq 'SCALAR'
or ref($_[1]) eq 'ARRAY';
return $_[1] if blessed($_[1]); # Fall through
barf("Can not convert a ".ref($_[1])." to a ".$_[0]);
0;}

=head2 set_datatype
=for ref
Expand Down
23 changes: 23 additions & 0 deletions Basic/Core/Core.xs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,29 @@ inplace(self, ...)
OUTPUT:
RETVAL

SV *
topdl(klass, arg1, ...)
SV *klass;
SV *arg1;
CODE:
if (items > 2 ||
(!SvROK(arg1) && SvTYPE(arg1) < SVt_PVAV) ||
(SvROK(arg1) && SvTYPE(SvRV(arg1)) == SVt_PVAV)
) {
SP -= items; PUSHMARK(SP); SPAGAIN; /* these pass this set of args on */
int retvals = perl_call_method("new", G_SCALAR);
SPAGAIN;
if (retvals != 1) barf("new returned no values");
RETVAL = POPs;
} else if (SvROK(arg1) && SvOBJECT(SvRV(arg1))) {
RETVAL = arg1;
} else {
barf("Can not convert a %s to a %s", sv_reftype(arg1, 1), SvPV_nolen(klass));
}
SvREFCNT_inc(RETVAL);
OUTPUT:
RETVAL

# Return the transformation object or an undef otherwise.
pdl_trans *
trans_parent(self)
Expand Down
2 changes: 2 additions & 0 deletions t/core.t
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ isa_ok( PDL->topdl([1,2,3]), "PDL", "topdl([1,2,3]) returns an ndarray" );
isa_ok( PDL->topdl(1,2,3), "PDL", "topdl(1,2,3) returns an ndarray" );
$x=PDL->topdl(1,2,3);
ok (($x->nelem == 3 and all($x == pdl(1,2,3))), "topdl(1,2,3) returns a 3-ndarray containing (1,2,3)");
eval {PDL->topdl({})};
isnt $@, '', 'topdl({}) no segfault';

# stringification
{
Expand Down

0 comments on commit 6d015dc

Please sign in to comment.