]> git.donarmstrong.com Git - biopieces.git/blob - code_c/Maasha/src/lib/common.c
48b71e570b6063d8a0d4bb4297ae03f78af56e99
[biopieces.git] / code_c / Maasha / src / lib / common.c
1 #include "common.h"
2 #include "list.h"
3 #include "mem.h"
4
5
6 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ARRAYS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
7
8
9 bool binary_search_array( int *array, int array_size, int val )
10 {
11     /* Martin A. Hansen, June 2008 */
12
13     /* Binary search an array of integers for an integer value. */
14
15     int high;
16     int low;
17     int try;
18
19     high = array_size; 
20     low  = 0;                                                                                                                   
21
22     while ( low < high )                                                                                                        
23     {                                                                                                                           
24         try = ( ( high + low ) / 2 );                                                                                           
25
26         if ( val < array[ try ] ) {
27             high = try;
28         } else if ( val > array[ try ] ) {
29             low = try + 1;
30         } else {
31             return TRUE;
32         }   
33     }
34
35     return FALSE;
36 }
37
38
39 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MISC <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
40
41
42 char *bits2string( uint bin )
43 {
44     /* Martin A. Hansen, June 2008 */
45     
46     /* Return a binary number as a string of 1's and 0's. */
47
48     int   i;
49     uint  j;
50     char *string;
51     
52     string = mem_get( ( sizeof( uint ) * 8 ) + 1 );
53
54     j = 1;
55                                                                                                                                 
56     for ( i = 0; i < sizeof( uint ) * 8; i++ )                                                                          
57     {                                                                                                                           
58                                                                                                                                 
59         if ( ( bin & j ) != 0 ) {                                                                                               
60             string[ 31 - i ] = '1';                                                                                             
61         } else {                                                                                                                
62             string[ 31 - i ] = '0';                                                                                             
63         }                                                                                                                       
64                                                                                                                                 
65         j <<= 1;                                                                                                                
66     }                                                                                                                           
67                                                                                                                                 
68     string[ i ] = '\0';                                                                                                         
69                                                                                                                                 
70     return string;                                                                                                              
71 }
72
73
74 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/