From af3658fc8b70b6e40c09bea4ac5c219498675e14 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Quelin?= Date: Tue, 15 Jan 2008 17:51:23 +0100 Subject: [PATCH] new method LBVXS::copy() moreover, using more robust xs code. objects are always SV, even if their underlying stuff is an AV. thanks rgs for helping. --- XS.xs | 70 ++++++++++++++++++++++++++++++--------- lib/Language/Befunge/Vector/XS.pm | 1 - 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/XS.xs b/XS.xs index e2588e9..d363922 100644 --- a/XS.xs +++ b/XS.xs @@ -44,23 +44,25 @@ new( class, array, ... ) intArray* array INIT: - AV* self; I32 i; + SV* self; SV* val; + AV* my_array; HV* stash; CODE: /* create the object and populate it */ - self = newAV(); + my_array = newAV(); for ( i=0; inew_zeroes( $dims ); # -# Create a new vector, set to the origin. The only argument is the dimension of -# the vector to be created. -# -# ->new_zeroes(2) is exactly equivalent to ->new([0,0]) +# Create a new vector of dimension $dims, set to the origin (all +# zeroes). LBVXS->new_zeroes(2) is exactly equivalent to LBVXS->new(0, 0). # SV * new_zeroes( class, dimension ) @@ -80,28 +80,66 @@ new_zeroes( class, dimension ) I32 dimension; INIT: - AV* self; I32 i; + SV* self; SV* zero; + AV* my_array; HV* stash; CODE: /* create the object and populate it */ - self = newAV(); + my_array = newAV(); for ( i=0; icopy; +# +# Return a new LBVXS object, which has the same dimensions and +# coordinates as $v. +# +SV* +copy( vec ) + SV* vec; + + INIT: + I32 val, i; + SV* self; + AV* my_array; + AV* vec_array; + HV* stash; + + CODE: + vec_array = (AV*)SvRV(vec); + + /* create the object and populate it */ + my_array = newAV(); + for ( i=0; i<=av_len(vec_array); i++ ) { + val = newSViv( SvIV(*av_fetch(vec_array, i, 0)) ); + av_push(my_array, val); + } + + /* return a blessed reference to the AV */ + self = newRV_noinc( (SV*)my_array ); + stash = SvSTASH( (SV*)vec_array ); + sv_bless( (SV*)self, stash ); + RETVAL = self; + + OUTPUT: + RETVAL + #-- PUBLIC METHODS diff --git a/lib/Language/Befunge/Vector/XS.pm b/lib/Language/Befunge/Vector/XS.pm index baa519b..b1d3b31 100644 --- a/lib/Language/Befunge/Vector/XS.pm +++ b/lib/Language/Befunge/Vector/XS.pm @@ -35,7 +35,6 @@ sub as_string { return "(@$self)"; } -sub copy {$_[0]} sub get_all_components {$_[0]} sub clear {$_[0]} sub set_component {$_[0]} -- 2.11.4.GIT