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