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) );
189 # $v1->_substract_inplace($v2);
192 # Substract $v2 to $v1, and stores the result back into $v1.
194 AV* _substract_inplace( v1, v2 )
208 if ( dimv1 != dimv2 )
209 croak("uneven dimensions in vector substraction!");
211 for ( i=0 ; i<=dimv1; i++ ) {
212 val1 = SvIV( *av_fetch(v1, i, 0) );
213 val2 = SvIV( *av_fetch(v2, i, 0) );
214 av_store( v1, i, newSViv(val1-val2) );
225 # my $bool = $v1->_compare($v2);
226 # my $bool = $v1 <=> $v2;
228 # Check whether the vectors both point at the same spot. Return 0 if they
229 # do, 1 if they don't.
232 _compare( v1, v2, variant )
247 if ( dimv1 != dimv2 )
248 croak("uneven dimensions in bounds check!");
251 for ( i=0 ; i<=dimv1; i++ ) {
252 val1 = SvIV( *av_fetch(v1, i, 0) );
253 val2 = SvIV( *av_fetch(v2, i, 0) );
254 if ( val1 != val2 ) {