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, ... )
52 /* create the object and populate it */
54 for ( i=0; i<ix_array; i++ ) {
55 val = newSViv( array[i] );
56 av_push(my_array, val);
59 /* Return a blessed reference to the AV */
60 self = newRV_noinc( (SV*)my_array );
61 stash = gv_stashpv( class, TRUE );
62 sv_bless( (SV*)RETVAL, stash );
69 # my $vec = Language::Befunge::Vector::XS->new_zeroes( $dims );
71 # Create a new vector of dimension $dims, set to the origin (all
72 # zeroes). LBVXS->new_zeroes(2) is exactly equivalent to LBVXS->new(0, 0).
75 new_zeroes( class, dim )
85 /* create the object and populate it */
87 for ( i=0; i<dim; i++ ) {
89 av_push(my_array, zero);
92 /* return a blessed reference to the AV */
93 self = newRV_noinc( (SV*)my_array );
94 stash = gv_stashpv( class, TRUE );
95 sv_bless( (SV*)self, stash );
102 # my $vec = $v->copy;
104 # Return a new LBVXS object, which has the same dimensions and
117 vec_array = (AV*)SvRV(vec);
119 /* create the object and populate it */
121 for ( i=0; i<=av_len(vec_array); i++ ) {
122 val = newSViv( SvIV(*av_fetch(vec_array, i, 0)) );
123 av_push(my_array, val);
126 /* return a blessed reference to the AV */
127 self = newRV_noinc( (SV*)my_array );
128 stash = SvSTASH( (SV*)vec_array );
129 sv_bless( (SV*)self, stash );
140 # my $dims = $vec->get_dims;
142 # Return the number of dimensions, an integer.
148 RETVAL = av_len(self) + 1;
154 # my $val = $vec->get_component($dim);
156 # Return the value for dimension $dim.
159 get_component( self, dim )
163 if ( dim < 0 || dim > av_len(self) )
164 croak( "No such dimension!" );
165 RETVAL = SvIV( *av_fetch(self, dim, 0) );
171 # my @vals = $vec->get_all_components;
173 # Get the values for all dimensions, in order from 0..N.
176 get_all_components( self )
182 /* fetch the underlying array of the object */
183 my_array = (AV*)SvRV(self);
184 dim = av_len(my_array);
187 for ( i=0; i<=dim; i++ ) {
188 val = SvIV( *av_fetch(my_array, i, 0) );
189 PUSHs( sv_2mortal( newSViv(val) ) );
198 # Set the vector back to the origin, all 0's.
207 /* fetch the underlying array of the object */
208 my_array = (AV*)SvRV(self);
209 dim = av_len(my_array);
210 for ( i=0; i<dim; i++ ) {
212 av_store(my_array, i, zero);
217 # my $val = $vec->set_component( $dim, $value );
219 # Set the value for dimension $dim to $value.
222 set_component( self, dim, value )
229 /* fetch the underlying array of the object */
230 my_array = (AV*)SvRV(self);
233 if ( dim < 0 || dim > av_len(my_array) )
234 croak( "No such dimension!" );
236 /* storing new value */
237 av_store(my_array, dim, newSViv(value));
245 # $v1->_add_inplace($v2);
248 # Adds $v2 to $v1, and stores the result back into $v1.
251 _add_inplace( v1, v2, variant )
256 IV dimv1, dimv2, i, val1, val2;
260 /* fetch the underlying array of the object */
261 v1_array = (AV*)SvRV(v1);
262 v2_array = (AV*)SvRV(v2);
263 dimv1 = av_len(v1_array);
264 dimv2 = av_len(v2_array);
267 if ( dimv1 != dimv2 )
268 croak("uneven dimensions in vector addition!");
270 for ( i=0 ; i<=dimv1; i++ ) {
271 val1 = SvIV( *av_fetch(v1_array, i, 0) );
272 val2 = SvIV( *av_fetch(v2_array, i, 0) );
273 av_store( v1_array, i, newSViv(val1+val2) );
280 # $v1->_substract_inplace($v2);
283 # Substract $v2 to $v1, and stores the result back into $v1.
286 _substract_inplace( v1, v2, variant )
291 IV dimv1, dimv2, i, val1, val2;
295 /* fetch the underlying array of the object */
296 v1_array = (AV*)SvRV(v1);
297 v2_array = (AV*)SvRV(v2);
298 dimv1 = av_len(v1_array);
299 dimv2 = av_len(v2_array);
302 if ( dimv1 != dimv2 )
303 croak("uneven dimensions in vector addition!");
305 for ( i=0 ; i<=dimv1; i++ ) {
306 val1 = SvIV( *av_fetch(v1_array, i, 0) );
307 val2 = SvIV( *av_fetch(v2_array, i, 0) );
308 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 )
330 IV dimv1, dimv2, i, val1, val2;
336 if ( dimv1 != dimv2 )
337 croak("uneven dimensions in bounds check!");
340 for ( i=0 ; i<=dimv1; i++ ) {
341 val1 = SvIV( *av_fetch(v1, i, 0) );
342 val2 = SvIV( *av_fetch(v2, i, 0) );
343 if ( val1 != val2 ) {