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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
34 use vars qw ( @ISA @EXPORT );
36 @ISA = qw( Exporter );
38 use Inline ( C => Config => DIRECTORY => $ENV{ 'BP_TMP' } );
40 use Inline C => << 'END_C';
45 void c_array_interval_fill( SV *array, int beg, int end, int score )
47 /* Martin A. Hansen, November 2008. */
49 /* Incretments all values in a C array */
50 /* with a given score in a given interval. */
56 int *a = ( int * ) SvPV( array, len );
58 size = len / sizeof( int );
60 printf( "C: len: %d size: %d sizeof( int ): %ld\n", len, size, sizeof(int) );
64 assert( beg <= size );
65 assert( end <= size );
68 for ( i = beg; i < end + 1; i++ ) {
74 void c_array_interval_scan( SV *array, int pos )
76 /* Martin A. Hansen, November 2008. */
78 /* Locates the next interval of non-zero */
79 /* integers in a C array given a starting pos. */
87 int *a = ( int * ) SvPV( array, len );
89 size = len / sizeof( int );
92 assert( pos <= size );
94 while ( pos < size && a[ pos ] == 0 ) { pos++; }
98 while ( pos < size && a[ pos ] != 0 ) { pos++; }
111 Inline_Stack_Push( sv_2mortal( newSViv( beg ) ) );
112 Inline_Stack_Push( sv_2mortal( newSViv( end ) ) );
118 void c_array_interval_get( SV *array, int beg, int end )
120 /* Martin A. Hansen, November 2008. */
122 /* Get all values from an array in a given interval, */
123 /* and return these as a Perl list. */
129 int *a = ( int * ) SvPV( array, len );
131 size = len / sizeof( int );
135 assert( beg <= size );
136 assert( end <= size );
141 for ( i = beg; i < end + 1; i++ ) {
142 Inline_Stack_Push( sv_2mortal( newSViv( a[ i ] ) ) );
149 void c_array_print( SV *array )
151 /* Martin A. Hansen, November 2008. */
153 /* Outputs a C array to stdout. */
158 int *a = ( int * ) SvPV( array, len );
160 for ( i = 0; i < len / sizeof( int ); ++i ) {
161 printf( "%d\n", a[ i ] );
170 # Martin A. Hansen, November 2008.
172 # Initializes a zeroed C integer array using
173 # Perls vec function to create a bit array.
175 my ( $size, # number of elements in array
179 # Returns a bit vector
185 #vec( $vec, $size - 1, $bits ) = 0;
186 vec( $vec, 4 * $size - 1, $bits / 4 ) = 0;
188 printf STDERR "P: size: %d bits: %d len: %d size: %d\n", $size, $bits, length( $vec ), length( $vec ) / 4;
194 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<