]> git.donarmstrong.com Git - biopieces.git/blob - code_perl/Maasha/C_bitarray.pm
fixed seq qual length check
[biopieces.git] / code_perl / Maasha / C_bitarray.pm
1 package Maasha::C_bitarray;
2
3 # Copyright (C) 2008 Martin A. Hansen.
4
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.
9
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.
14
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.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24
25 # This modules make all functions for casting, filling, and scanning a C integer array.
26
27
28 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
29
30
31
32 use warnings;
33 use strict;
34
35 use vars qw ( @ISA @EXPORT );
36
37 @ISA = qw( Exporter );
38
39 use Inline ( C => Config => DIRECTORY => $ENV{ 'BP_TMP' } );
40
41 use Inline C => << 'END_C';
42
43 #include <assert.h>
44
45
46 void c_array_interval_fill( SV *array, int beg, int end, int score )
47 {
48     /* Martin A. Hansen, November 2008. */
49
50     /* Incretments all values in a C array */
51     /* with a given score in a given interval. */
52
53     int i;
54     int len;
55     int size;
56
57     int *a = ( int * ) SvPV( array, len );
58
59     size = len / sizeof( int );
60
61  printf( "C: len: %d    size: %d   sizeof( int ): %ld\n", len, size, sizeof(int) );
62
63     assert( beg >= 0 );
64     assert( end >= 0 );
65     assert( beg <= size );
66     assert( end <= size );
67     assert( score > 0 );
68
69     for ( i = beg; i < end + 1; i++ ) {
70         a[ i ] += score;
71     }
72 }
73
74
75 void c_array_interval_scan( SV *array, int pos )
76 {
77     /* Martin A. Hansen, November 2008. */
78
79     /* Locates the next interval of non-zero */
80     /* integers in a C array given a starting pos. */
81
82     int i;
83     int len;
84     int size;
85     int beg;
86     int end;
87
88     int *a = ( int * ) SvPV( array, len );
89
90     size = len / sizeof( int );
91
92     assert( pos >= 0 );
93     assert( pos <= size );
94
95     while ( pos < size && a[ pos ] == 0 ) { pos++; }
96
97     beg = pos;
98
99     while ( pos < size && a[ pos ] != 0 ) { pos++; }
100
101     end = pos - 1;
102
103     if ( beg > end )
104     {
105         beg = -1;
106         end = -1;
107     }
108
109     Inline_Stack_Vars;
110     Inline_Stack_Reset;
111
112     Inline_Stack_Push( sv_2mortal( newSViv( beg ) ) );
113     Inline_Stack_Push( sv_2mortal( newSViv( end ) ) );
114
115     Inline_Stack_Done;
116 }
117
118
119 void c_array_interval_get( SV *array, int beg, int end )
120 {
121     /* Martin A. Hansen, November 2008. */
122
123     /* Get all values from an array in a given interval, */
124     /* and return these as a Perl list. */
125
126     int i;
127     int len;
128     int size;
129
130     int *a = ( int * ) SvPV( array, len );
131
132     size = len / sizeof( int );
133
134     assert( beg >= 0 );
135     assert( end >= 0 );
136     assert( beg <= size );
137     assert( end <= size );
138
139     Inline_Stack_Vars;
140     Inline_Stack_Reset;
141
142     for ( i = beg; i < end + 1; i++ ) {
143         Inline_Stack_Push( sv_2mortal( newSViv( a[ i ] ) ) );
144     }
145
146     Inline_Stack_Done;
147 }
148
149
150 void c_array_print( SV *array )
151 {
152     /* Martin A. Hansen, November 2008. */
153
154     /* Outputs a C array to stdout. */
155
156     int i;
157     int len;
158
159     int *a = ( int * ) SvPV( array, len );
160
161     for ( i = 0; i < len / sizeof( int ); ++i ) {
162         printf( "%d\n", a[ i ] );
163     }
164 }
165
166 END_C
167  
168
169 sub c_array_init
170 {
171     # Martin A. Hansen, November 2008.
172
173     # Initializes a zeroed C integer array using
174     # Perls vec function to create a bit array.
175
176     my ( $size,   # number of elements in array
177          $bits,   # bit size
178        ) = @_;
179
180     # Returns a bit vector
181
182     my ( $vec );
183
184     $vec = '';
185
186     #vec( $vec, $size - 1, $bits ) = 0;
187     vec( $vec, 4 * $size - 1, $bits / 4 ) = 0;
188
189     printf STDERR "P: size: %d   bits: %d   len: %d   size: %d\n", $size, $bits, length( $vec ), length( $vec ) / 4;
190
191     return $vec;
192 }
193
194
195 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
196
197
198 1;