new method: get_component($index)
[language-befunge-vector-xs.git] / XS.xs
bloba621e0dfc13892b8de1bbdb5060d645adb0e5a98
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
5 #include "ppport.h"
7 #include "const-c.inc"
9 MODULE = Math::Vector::XS               PACKAGE = Math::Vector::XS              
11 INCLUDE: const-xs.inc
13 #-- CONSTRUCTORS
16 # my $vec = Math::Vector::XS->new( \@numbers );
18 # Create a new vector. The only argument is a reference to an array containing
19 # numbers, used to fill in the vector. The dimension of the newly created
20 # vector is determined by the length of this array.
22 SV *
23 new( class, values )
24         char* class;
25         AV*   values;
27         INIT:
28             AV*  self;
29             I32  i;
30             SV** orig;
31             SV*  clone;
32             HV*  stash;
34         CODE:
35             /* create the object and populate it */
36             self = newAV();
37             for ( i=0; i<=av_len(values); i++ ) {
38                 orig  = av_fetch(values, i, 0);
39                 clone = newSViv( SvIV(*orig) );
40                 av_push(self, clone);
41             }
43             /* Return a blessed reference to the AV */
44             RETVAL = newRV_noinc( (SV *)self );
45             stash  = gv_stashpv( class, TRUE );
46             sv_bless( (SV *)RETVAL, stash );
48         OUTPUT:
49             RETVAL
53 # my $vec = Math::Vector::XS->new_zeroes( dimension );
55 # Create a new vector, set to the origin. The only argument is the dimension of
56 # the vector to be created.
58 # ->new_zeroes(2) is exactly equivalent to ->new([0,0])
60 SV *
61 new_zeroes( class, dimension )
62         char* class;
63         I32   dimension;
65         INIT:
66             AV*  self;
67             I32  i;
68             SV*  zero;
69             HV*  stash;
71         CODE:
72             /* create the object and populate it */
73             self = newAV();
74             for ( i=0; i<dimension; i++ ) {
75                 zero = newSViv(0);
76                 av_push(self, zero);
77             }
79             /* return a blessed reference to the AV */
80             RETVAL = newRV_noinc( (SV *)self );
81             stash  = gv_stashpv( class, TRUE );
82             sv_bless( (SV *)RETVAL, stash );
84         OUTPUT:
85             RETVAL
88 #-- PUBLIC METHODS
92 # my $dims = $vec->get_dims;
94 # Return the number of dimensions, an integer.
96 I32
97 get_dims( self )
98         AV* self
100     CODE:
101         RETVAL = av_len(self) + 1;
103     OUTPUT:
104         RETVAL
107 # my $val = $vec->get_component( $index );
109 # Return the value for dimension $index.
112 get_component( self, index )
113         AV* self
114         I32 index
116     INIT:
117         SV** val;
119     CODE:
120         val = av_fetch(self, index, 0);
121         RETVAL = SvIV(*val);
123     OUTPUT:
124         RETVAL