From 6f9008247b82aa207447ebd5884a884c396aee40 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Quelin?= Date: Tue, 15 Jan 2008 18:21:11 +0100 Subject: [PATCH] LBVXS::_add_inplace() moved to XS.xs --- XS.xs | 48 ++++++++++++++++++++++++++++++++++++--- lib/Language/Befunge/Vector/XS.pm | 2 +- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/XS.xs b/XS.xs index d363922..4e91154 100644 --- a/XS.xs +++ b/XS.xs @@ -111,7 +111,7 @@ new_zeroes( class, dimension ) # coordinates as $v. # SV* -copy( vec ) +copy( vec, ... ) SV* vec; INIT: @@ -180,18 +180,60 @@ get_component( self, index ) #- mutators - + # -- PRIVATE METHODS #- inplace math ops # +# $v1->_add_inplace($v2); +# $v1 += $v2; +# +# Adds $v2 to $v1, and stores the result back into $v1. +# +SV* +_add_inplace( v1, v2, variant ) + SV* v1; + SV* v2; + SV* variant; + + INIT: + I32 dimv1, dimv2; + I32 i; + I32 val1, val2; + SV* rv; + AV* v1_array; + AV* v2_array; + HV* stash; + + CODE: + /* fetch the underlying array of the object */ + v1_array = (AV*)SvRV(v1); + v2_array = (AV*)SvRV(v2); + dimv1 = av_len(v1_array); + dimv2 = av_len(v2_array); + + /* sanity checks */ + if ( dimv1 != dimv2 ) + croak("uneven dimensions in vector addition!"); + + for ( i=0 ; i<=dimv1; i++ ) { + val1 = SvIV( *av_fetch(v1_array, i, 0) ); + val2 = SvIV( *av_fetch(v2_array, i, 0) ); + av_store( v1_array, i, newSViv(val1+val2) ); + } + + OUTPUT: + v1 + + +# # $v1->_substract_inplace($v2); # $v1 -= $v2; # # Substract $v2 to $v1, and stores the result back into $v1. # -AV* _substract_inplace( v1, v2 ) +AV* _substract_inplace( v1, v2, variant ) AV* v1; AV* v2; diff --git a/lib/Language/Befunge/Vector/XS.pm b/lib/Language/Befunge/Vector/XS.pm index b1d3b31..662046f 100644 --- a/lib/Language/Befunge/Vector/XS.pm +++ b/lib/Language/Befunge/Vector/XS.pm @@ -35,6 +35,7 @@ sub as_string { return "(@$self)"; } +#sub copy { my $vec = shift; return bless [@$vec], ref $vec; } sub get_all_components {$_[0]} sub clear {$_[0]} sub set_component {$_[0]} @@ -42,7 +43,6 @@ sub bounds_check {$_[0]} sub _add {$_[0]} sub _substract {$_[0]} sub _invert {$_[0]} -sub _add_inplace {$_[0]} 1; __END__ -- 2.11.4.GIT