]> git.donarmstrong.com Git - biopieces.git/blob - code_c/Maasha/src/lib/common.c
unit test of mem.c done
[biopieces.git] / code_c / Maasha / src / lib / common.c
1 #include "common.h"
2 #include "list.h"
3 #include "mem.h"
4
5
6 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ERROR HANDLING <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
7
8
9 void die( char *msg )
10 {
11     /* Martin A. Hansen, May 2008 */
12
13     /* Print error message and exits. */
14
15     fprintf( stderr, "ERROR: %s\n", msg );
16
17     exit( 1 );
18 }
19
20
21 void warn( char *msg )
22 {
23     /* Martin A. Hansen, May 2008 */
24
25     /* Print warning message and exits. */
26
27     fprintf( stderr, "WARNING: %s\n", msg );
28 }
29
30
31 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ARRAYS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
32
33
34 bool binary_search_array( int *array, int array_size, int val )
35 {
36     /* Martin A. Hansen, June 2008 */
37
38     /* Binary search an array of integers for an integer value. */
39
40     int high;
41     int low;
42     int try;
43
44     high = array_size; 
45     low  = 0;                                                                                                                   
46
47     while ( low < high )                                                                                                        
48     {                                                                                                                           
49         try = ( ( high + low ) / 2 );                                                                                           
50
51         if ( val < array[ try ] ) {
52             high = try;
53         } else if ( val > array[ try ] ) {
54             low = try + 1;
55         } else {
56             return TRUE;
57         }   
58     }
59
60     return FALSE;
61 }
62
63
64 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MISC <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
65
66
67 char *bits2string( uint bin )
68 {
69     /* Martin A. Hansen, June 2008 */
70     
71     /* Return a binary number as a string of 1's and 0's. */
72
73     int   i;
74     uint  j;
75     char *string;
76     
77     string = mem_get( ( sizeof( uint ) * 8 ) + 1 );
78
79     j = 1;
80                                                                                                                                 
81     for ( i = 0; i < sizeof( uint ) * 8; i++ )                                                                          
82     {                                                                                                                           
83                                                                                                                                 
84         if ( ( bin & j ) != 0 ) {                                                                                               
85             string[ 31 - i ] = '1';                                                                                             
86         } else {                                                                                                                
87             string[ 31 - i ] = '0';                                                                                             
88         }                                                                                                                       
89                                                                                                                                 
90         j <<= 1;                                                                                                                
91     }                                                                                                                           
92                                                                                                                                 
93     string[ i ] = '\0';                                                                                                         
94                                                                                                                                 
95     return string;                                                                                                              
96 }
97
98
99 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/