1 package Maasha::C_bitarray;
3 # Copyright (C) 2008 Martin A. Hansen.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
25 # This modules make all functions for casting, filling, and scanning a C integer array.
28 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
35 use vars qw ( @ISA @EXPORT );
37 @ISA = qw( Exporter );
39 use Inline ( C => Config => DIRECTORY => $ENV{ 'BP_TMP' } );
41 use Inline C => << 'END_C';
46 void c_array_interval_fill( SV *array, int beg, int end, int score )
48 /* Martin A. Hansen, November 2008. */
50 /* Incretments all values in a C array */
51 /* with a given score in a given interval. */
57 int *a = ( int * ) SvPV( array, len );
59 size = len / sizeof( int );
61 printf( "C: len: %d size: %d sizeof( int ): %ld\n", len, size, sizeof(int) );
65 assert( beg <= size );
66 assert( end <= size );
69 for ( i = beg; i < end + 1; i++ ) {
75 void c_array_interval_scan( SV *array, int pos )
77 /* Martin A. Hansen, November 2008. */
79 /* Locates the next interval of non-zero */
80 /* integers in a C array given a starting pos. */
88 int *a = ( int * ) SvPV( array, len );
90 size = len / sizeof( int );
93 assert( pos <= size );
95 while ( pos < size && a[ pos ] == 0 ) { pos++; }
99 while ( pos < size && a[ pos ] != 0 ) { pos++; }
112 Inline_Stack_Push( sv_2mortal( newSViv( beg ) ) );
113 Inline_Stack_Push( sv_2mortal( newSViv( end ) ) );
119 void c_array_interval_get( SV *array, int beg, int end )
121 /* Martin A. Hansen, November 2008. */
123 /* Get all values from an array in a given interval, */
124 /* and return these as a Perl list. */
130 int *a = ( int * ) SvPV( array, len );
132 size = len / sizeof( int );
136 assert( beg <= size );
137 assert( end <= size );
142 for ( i = beg; i < end + 1; i++ ) {
143 Inline_Stack_Push( sv_2mortal( newSViv( a[ i ] ) ) );
150 void c_array_print( SV *array )
152 /* Martin A. Hansen, November 2008. */
154 /* Outputs a C array to stdout. */
159 int *a = ( int * ) SvPV( array, len );
161 for ( i = 0; i < len / sizeof( int ); ++i ) {
162 printf( "%d\n", a[ i ] );
171 # Martin A. Hansen, November 2008.
173 # Initializes a zeroed C integer array using
174 # Perls vec function to create a bit array.
176 my ( $size, # number of elements in array
180 # Returns a bit vector
186 #vec( $vec, $size - 1, $bits ) = 0;
187 vec( $vec, 4 * $size - 1, $bits / 4 ) = 0;
189 printf STDERR "P: size: %d bits: %d len: %d size: %d\n", $size, $bits, length( $vec ), length( $vec ) / 4;
195 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<