3 # This file is part of Language::Befunge::Vector::XS.
4 # Copyright (c) 2008 Jerome Quelin, all rights reserved.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the same terms as Perl itself.
20 /* used for constructor new() */
22 void* intArrayPtr(int num) {
24 mortal = sv_2mortal( NEWSV(0, num * sizeof(intArray)) );
30 MODULE = Language::Befunge::Vector::XS PACKAGE = Language::Befunge::Vector::XS
36 # my $vec = LB::Vector->new( $x [, $y, ...] );
38 # Create a new vector. The arguments are the actual vector data; one
39 # integer per dimension.
42 new( class, array, ... )
54 /* create the object and populate it */
56 for ( i=0; i<ix_array; i++ ) {
57 val = newSViv( array[i] );
58 av_push(my_array, val);
61 /* Return a blessed reference to the AV */
62 self = newRV_noinc( (SV*)my_array );
63 stash = gv_stashpv( class, TRUE );
64 sv_bless( (SV*)RETVAL, stash );
72 # my $vec = Language::Befunge::Vector::XS->new_zeroes( $dims );
74 # Create a new vector of dimension $dims, set to the origin (all
75 # zeroes). LBVXS->new_zeroes(2) is exactly equivalent to LBVXS->new(0, 0).
78 new_zeroes( class, dimension )
90 /* create the object and populate it */
92 for ( i=0; i<dimension; i++ ) {
94 av_push(my_array, zero);
97 /* return a blessed reference to the AV */
98 self = newRV_noinc( (SV*)my_array );
99 stash = gv_stashpv( class, TRUE );
100 sv_bless( (SV*)self, stash );
108 # my $vec = $v->copy;
110 # Return a new LBVXS object, which has the same dimensions and
125 vec_array = (AV*)SvRV(vec);
127 /* create the object and populate it */
129 for ( i=0; i<=av_len(vec_array); i++ ) {
130 val = newSViv( SvIV(*av_fetch(vec_array, i, 0)) );
131 av_push(my_array, val);
134 /* return a blessed reference to the AV */
135 self = newRV_noinc( (SV*)my_array );
136 stash = SvSTASH( (SV*)vec_array );
137 sv_bless( (SV*)self, stash );
149 # my $dims = $vec->get_dims;
151 # Return the number of dimensions, an integer.
158 RETVAL = av_len(self) + 1;
164 # my $val = $vec->get_component( $index );
166 # Return the value for dimension $index.
169 get_component( self, index )
174 if ( index < 0 || index > av_len(self) )
175 croak( "No such dimension!" );
176 RETVAL = SvIV( *av_fetch(self, index, 0) );
183 # my @vals = $vec->get_all_components;
185 # Get the values for all dimensions, in order from 0..N.
188 get_all_components( self )
196 /* fetch the underlying array of the object */
197 my_array = (AV*)SvRV(self);
198 dim = av_len(my_array);
201 for ( i=0; i<=dim; i++ ) {
202 val = SvIV( *av_fetch(my_array, i, 0) );
203 PUSHs( sv_2mortal( newSViv(val) ) );
212 # Set the vector back to the origin, all 0's.
221 /* fetch the underlying array of the object */
222 my_array = (AV*)SvRV(self);
223 dim = av_len(my_array);
224 for ( i=0; i<dim; i++ ) {
226 av_store(my_array, i, zero);
235 # $v1->_add_inplace($v2);
238 # Adds $v2 to $v1, and stores the result back into $v1.
241 _add_inplace( v1, v2, variant )
254 /* fetch the underlying array of the object */
255 v1_array = (AV*)SvRV(v1);
256 v2_array = (AV*)SvRV(v2);
257 dimv1 = av_len(v1_array);
258 dimv2 = av_len(v2_array);
261 if ( dimv1 != dimv2 )
262 croak("uneven dimensions in vector addition!");
264 for ( i=0 ; i<=dimv1; i++ ) {
265 val1 = SvIV( *av_fetch(v1_array, i, 0) );
266 val2 = SvIV( *av_fetch(v2_array, i, 0) );
267 av_store( v1_array, i, newSViv(val1+val2) );
275 # $v1->_substract_inplace($v2);
278 # Substract $v2 to $v1, and stores the result back into $v1.
281 _substract_inplace( v1, v2, variant )
294 /* fetch the underlying array of the object */
295 v1_array = (AV*)SvRV(v1);
296 v2_array = (AV*)SvRV(v2);
297 dimv1 = av_len(v1_array);
298 dimv2 = av_len(v2_array);
301 if ( dimv1 != dimv2 )
302 croak("uneven dimensions in vector addition!");
304 for ( i=0 ; i<=dimv1; i++ ) {
305 val1 = SvIV( *av_fetch(v1_array, i, 0) );
306 val2 = SvIV( *av_fetch(v2_array, i, 0) );
307 av_store( v1_array, i, newSViv(val1-val2) );
317 # my $bool = $v1->_compare($v2);
318 # my $bool = $v1 <=> $v2;
320 # Check whether the vectors both point at the same spot. Return 0 if they
321 # do, 1 if they don't.
324 _compare( v1, v2, variant )
339 if ( dimv1 != dimv2 )
340 croak("uneven dimensions in bounds check!");
343 for ( i=0 ; i<=dimv1; i++ ) {
344 val1 = SvIV( *av_fetch(v1, i, 0) );
345 val2 = SvIV( *av_fetch(v2, i, 0) );
346 if ( val1 != val2 ) {