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.
150 /* fetch the underlying array of the object */
151 my_array = (AV*)SvRV(self);
153 RETVAL = av_len(my_array) + 1;
159 # my $val = $vec->get_component($dim);
161 # Return the value for dimension $dim.
164 get_component( self, dim )
170 /* fetch the underlying array of the object */
171 my_array = (AV*)SvRV(self);
174 if ( dim < 0 || dim > av_len(my_array) )
175 croak( "No such dimension!" );
177 RETVAL = SvIV( *av_fetch(my_array, dim, 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 )
194 /* fetch the underlying array of the object */
195 my_array = (AV*)SvRV(self);
196 dim = av_len(my_array);
199 for ( i=0; i<=dim; i++ ) {
200 val = SvIV( *av_fetch(my_array, i, 0) );
201 PUSHs( sv_2mortal( newSViv(val) ) );
210 # Set the vector back to the origin, all 0's.
219 /* fetch the underlying array of the object */
220 my_array = (AV*)SvRV(self);
221 dim = av_len(my_array);
222 for ( i=0; i<=dim; i++ ) {
224 av_store(my_array, i, zero);
229 # my $val = $vec->set_component( $dim, $value );
231 # Set the value for dimension $dim to $value.
234 set_component( self, dim, value )
241 /* fetch the underlying array of the object */
242 my_array = (AV*)SvRV(self);
245 if ( dim < 0 || dim > av_len(my_array) )
246 croak( "No such dimension!" );
248 /* storing new value */
249 av_store(my_array, dim, newSViv(value));
257 # $v1->_add_inplace($v2);
260 # Adds $v2 to $v1, and stores the result back into $v1.
263 _add_inplace( v1, v2, variant )
268 IV dimv1, dimv2, i, val1, val2;
272 /* fetch the underlying array of the object */
273 v1_array = (AV*)SvRV(v1);
274 v2_array = (AV*)SvRV(v2);
275 dimv1 = av_len(v1_array);
276 dimv2 = av_len(v2_array);
279 if ( dimv1 != dimv2 )
280 croak("uneven dimensions in vector addition!");
282 for ( i=0 ; i<=dimv1; i++ ) {
283 val1 = SvIV( *av_fetch(v1_array, i, 0) );
284 val2 = SvIV( *av_fetch(v2_array, i, 0) );
285 av_store( v1_array, i, newSViv(val1+val2) );
292 # $v1->_substract_inplace($v2);
295 # Substract $v2 to $v1, and stores the result back into $v1.
298 _substract_inplace( v1, v2, variant )
303 IV dimv1, dimv2, i, val1, val2;
307 /* fetch the underlying array of the object */
308 v1_array = (AV*)SvRV(v1);
309 v2_array = (AV*)SvRV(v2);
310 dimv1 = av_len(v1_array);
311 dimv2 = av_len(v2_array);
314 if ( dimv1 != dimv2 )
315 croak("uneven dimensions in vector addition!");
317 for ( i=0 ; i<=dimv1; i++ ) {
318 val1 = SvIV( *av_fetch(v1_array, i, 0) );
319 val2 = SvIV( *av_fetch(v2_array, i, 0) );
320 av_store( v1_array, i, newSViv(val1-val2) );
329 # my $bool = $v1->_compare($v2);
330 # my $bool = $v1 <=> $v2;
332 # Check whether the vectors both point at the same spot. Return 0 if they
333 # do, 1 if they don't.
336 _compare( v1, v2, variant )
342 IV dimv1, dimv2, i, val1, val2;
346 /* fetch the underlying array of the object */
347 v1_array = (AV*)SvRV(v1);
348 v2_array = (AV*)SvRV(v2);
349 dimv1 = av_len(v1_array);
350 dimv2 = av_len(v2_array);
353 if ( dimv1 != dimv2 )
354 croak("uneven dimensions in bounds check!");
357 for ( i=0 ; i<=dimv1; i++ ) {
358 val1 = SvIV( *av_fetch(v1_array, i, 0) );
359 val2 = SvIV( *av_fetch(v2_array, i, 0) );
360 if ( val1 != val2 ) {