Language-Befunge-Vector-XS
view release on metacpan or search on metacpan
#-- PUBLIC METHODS
#- accessors
#
# my $dims = $vec->get_dims;
#
# Return the number of dimensions, an integer.
#
IV
get_dims( self )
SV* self;
PREINIT:
AV* my_array;
CODE:
/* fetch the underlying array of the object */
my_array = (AV*)SvRV(self);
RETVAL = av_len(my_array) + 1;
OUTPUT:
RETVAL
#
# my $val = $vec->get_component($dim);
#
# Return the value for dimension $dim.
#
IV
get_component( self, dim )
SV* self;
IV dim;
PREINIT:
AV* my_array;
CODE:
/* fetch the underlying array of the object */
my_array = (AV*)SvRV(self);
/* sanity checks */
if ( dim < 0 || dim > av_len(my_array) )
croak( "No such dimension!" );
RETVAL = SvIV( *av_fetch(my_array, dim, 0) );
OUTPUT:
RETVAL
#
# my @vals = $vec->get_all_components;
#
# Get the values for all dimensions, in order from 0..N.
#
void
get_all_components( self )
SV* self;
PREINIT:
IV dim, i, val;
AV* my_array;
PPCODE:
/* fetch the underlying array of the object */
my_array = (AV*)SvRV(self);
dim = av_len(my_array);
/* extend the return stack and populate it */
EXTEND(SP,dim+1);
for ( i=0; i<=dim; i++ ) {
val = SvIV( *av_fetch(my_array, i, 0) );
PUSHs( sv_2mortal( newSViv(val) ) );
}
#- mutators
#
# $vec->clear;
#
# Set the vector back to the origin, all 0's.
#
void
clear( self )
SV* self;
INIT:
IV dim, i;
SV* zero;
AV* my_array;
PPCODE:
/* fetch the underlying array of the object */
my_array = (AV*)SvRV(self);
dim = av_len(my_array);
/* clear each slot */
for ( i=0; i<=dim; i++ ) {
zero = newSViv(0);
av_store(my_array, i, zero);
}
#
# my $val = $vec->set_component( $dim, $value );
#
# Set the value for dimension $dim to $value.
#
void
set_component( self, dim, value )
SV* self;
IV dim;
IV value;
INIT:
AV* my_array;
CODE:
/* fetch the underlying array of the object */
my_array = (AV*)SvRV(self);
/* sanity checks */
if ( dim < 0 || dim > av_len(my_array) )
croak( "No such dimension!" );
/* storing new value */
av_store(my_array, dim, newSViv(value));
#- other methods
#
# my $is_within = $vec->bounds_check($begin, $end);
#
# Check whether $vec is within the box defined by $begin and $end.
# Return 1 if vector is contained within the box, and 0 otherwise.
#
IV
bounds_check( self, v1, v2 )
SV* self;
SV* v1;
SV* v2;
INIT:
IV i, mydim, dimv1, dimv2, myval, val1, val2;
AV* my_array;
AV* v1_array;
AV* v2_array;
CODE:
/* fetch the underlying array of the object */
my_array = (AV*)SvRV(self);
v1_array = (AV*)SvRV(v1);
v2_array = (AV*)SvRV(v2);
mydim = av_len(my_array);
dimv1 = av_len(v1_array);
( run in 1.696 second using v1.01-cache-2.11-cpan-5511b514fd6 )