4 * Irene's library of most frequently used routines
12 /* Undefine the following #define in order to use short int as the idxtype */
14 /* Undefine the following #define in order to use float as the realtype */
17 /* Indexes are as long as integers for now */
21 typedef short idxtype
;
26 typedef float realtype
;
28 typedef double realtype
;
32 /*************************************************************************
33 * Header file inclusion section
34 **************************************************************************/
50 /*************************************************************************
51 * Data structure definition section
52 **************************************************************************/
53 /*-------------------------------------------------------------
54 * The following data structure stores int key-value pairs
55 *-------------------------------------------------------------*/
56 struct IKeyValueType
{
61 typedef struct IKeyValueType IKeyValueType
;
64 /*-------------------------------------------------------------
65 * The following data structure stores int key-value pairs
66 *-------------------------------------------------------------*/
67 struct idxKeyValueType
{
72 typedef struct idxKeyValueType idxKeyValueType
;
75 /*-------------------------------------------------------------
76 * The following data structure stores int-key - double-value pairs
77 *-------------------------------------------------------------*/
78 struct FKeyValueType
{
83 typedef struct FKeyValueType FKeyValueType
;
86 /*-------------------------------------------------------------
87 * The following data structure stores int-key - double-value pairs
88 *-------------------------------------------------------------*/
89 struct realKeyValueType
{
94 typedef struct realKeyValueType realKeyValueType
;
96 /*************************************************************************
98 **************************************************************************/
99 #define LTERM (void **) 0 /* List terminator for IMfree() */
103 /*************************************************************************
105 **************************************************************************/
106 /*-------------------------------------------------------------
108 *-------------------------------------------------------------*/
109 #define sign(a, b) ((b) >= 0 ? ((a) >= 0.0 ? a : -a) : ((a) >= 0.0 ? -a : a))
110 #define amax(a, b) ((a) >= (b) ? (a) : (b))
111 #define amin(a, b) ((a) >= (b) ? (b) : (a))
112 #define RandomInRange(u) ((int)(drand48()*((double)(u))))
113 #define RandomInRangeFast(u) ((rand()>>3)%(u))
114 #define SWAP(a, b, tmp) do {(tmp) = (a); (a) = (b); (b) = (tmp);} while(0)
115 #define INC_DEC(a, b, val) do {(a) += (val); (b) -= (val);} while(0)
116 #define icopy(n, a, b) (int *)memcpy((void *)(b), (void *)(a), sizeof(int)*(n))
117 #define idxcopy(n, a, b) (idxtype *)memcpy((void *)(b), (void *)(a), sizeof(idxtype)*(n))
118 #define scopy(n, a, b) (double *)memcpy((void *)(b), (void *)(a), sizeof(double)*(n))
119 #define fcopy(n, a, b) (double *)memcpy((void *)(b), (void *)(a), sizeof(double)*(n))
120 #define realcopy(n, a, b) (realtype *)memcpy((void *)(b), (void *)(a), sizeof(realtype)*(n))
123 /*-------------------------------------------------------------
125 *-------------------------------------------------------------*/
126 #define cleartimer(tmr) (tmr = 0.0)
127 #define starttimer(tmr) (tmr -= seconds())
128 #define stoptimer(tmr) (tmr += seconds())
129 #define gettimer(tmr) (tmr)
132 /*-------------------------------------------------------------
133 * Debuging memory leaks
134 *-------------------------------------------------------------*/
136 #define imalloc(n, msg) (malloc(sizeof(int)*(n)))
137 #define fmalloc(n, msg) (malloc(sizeof(double)*(n)))
138 #define idxmalloc(n, msg) ((idxtype *)malloc(sizeof(idxtype)*(n)))
139 #define realmalloc(n, msg) ((realtype *)malloc(sizeof(realtype)*(n)))
140 #define ismalloc(n, val, msg) (iset((n), (val), malloc(sizeof(int)*(n))))
141 #define idxsmalloc(n, val, msg) (idxset((n), (val), (idxtype *)malloc(sizeof(idxtype)*(n))))
142 #define fsmalloc(n, val, msg) (fset((n), (val), malloc(sizeof(double)*(n))))
143 #define realsmalloc(n, val, msg) (realset((n), (val), (realtype *)malloc(sizeof(realtype)*(n))))
144 #define IMmalloc(a, b) (malloc((a)))
148 # define MALLOC_CHECK(ptr) \
149 if (malloc_verify((ptr)) == DMALLOC_VERIFY_ERROR) { \
150 printf("***MALLOC_CHECK failed on line %d of file %s: " #ptr "\n", \
151 __LINE__, __FILE__); \
155 # define MALLOC_CHECK(ptr) ;
159 /*-------------------------------------------------------------
160 * CSR conversion macros
161 *-------------------------------------------------------------*/
162 #define MAKECSR(i, n, a) \
164 for (i=1; i<n; i++) a[i] += a[i-1]; \
165 for (i=n; i>0; i--) a[i] = a[i-1]; \
170 /*-------------------------------------------------------------
172 *-------------------------------------------------------------*/
174 # define ASSERT(expr) \
176 printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
177 __LINE__, __FILE__); \
181 # define ASSERT(expr) ;
185 # define ASSERTP(expr,msg) \
187 printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
188 __LINE__, __FILE__); \
194 # define ASSERTP(expr,msg) ;
198 /*************************************************************************
199 * Function prototypes
200 **************************************************************************/
201 /*-------------------------------------------------------------
203 *-------------------------------------------------------------*/
204 int *iset(int, int, int *);
205 idxtype
*idxset(int, idxtype
, idxtype
*);
206 double *fset(int, double, double *);
207 realtype
*realset(int, realtype
, realtype
*);
208 int iamax(int, int *);
209 int idxamax(int, idxtype
*);
210 int famax(int, double *);
211 int iamin(int, int *);
212 int idxamin(int, idxtype
*);
213 int famin(int, double *);
214 int charsum(int, char *);
215 int isum(int, int *);
216 int idxsum(int, idxtype
*);
217 double ssum(int, double *);
218 double ssum_strd(int, double *, int);
219 void sscale(int, double, double *);
220 double snorm2(int, double *);
221 double sdot(int, double *, double *);
222 void saxpy(int, double, double *, int, double *, int);
225 /*-------------------------------------------------------------
227 *-------------------------------------------------------------*/
228 FILE *IMfopen(char *, char *, char *);
229 void IMfclose(FILE *);
231 /*-------------------------------------------------------------
233 *-------------------------------------------------------------*/
235 int *imalloc(int, char *);
236 idxtype
*idxmalloc(int, char *);
237 double *fmalloc(int, char *);
238 realtype
*realmalloc(int, char *);
239 int *ismalloc(int, int, char *);
240 idxtype
*idxsmalloc(int, idxtype
, char *);
241 double *fsmalloc(int, double, char *);
242 realtype
*realsmalloc(int, realtype
, char *);
243 void *IMmalloc(int, char *);
245 /* void IMfree(void **, ...); */
248 /*-------------------------------------------------------------
250 *-------------------------------------------------------------*/
251 void *errexit(char *,...);
253 double flog2(double);
255 double seconds(void);
258 /*-------------------------------------------------------------
260 *-------------------------------------------------------------*/
261 void dfkeysort(int, FKeyValueType
*);
262 void dkeysort(int, IKeyValueType
*);
263 void ifkeysort(int, FKeyValueType
*);
264 void ifkeysort2(int, FKeyValueType
*);
265 void ifloatsort(int, double *);
266 void iintsort(int, int *);
267 void ikeysort(int, IKeyValueType
*);
268 void idxkeysort(int, idxKeyValueType
*);
269 void ikeysort2(int, IKeyValueType
*);
270 void idxkeysort2(int, idxKeyValueType
*);
272 /*-------------------------------------------------------------
274 *-------------------------------------------------------------*/
275 void ikeyvalsort_org(int, IKeyValueType
*);
276 int IncKeyValueCmp(const void *, const void *);
277 void dkeyvalsort(int, IKeyValueType
*);
278 void BucketSortKeysInc(int, idxtype
, idxtype
*, int *, int *);
279 int DecKeyValueCmp(const void *, const void *);
280 int BSearch(int, idxtype
*, int);
281 void RandomPermute(int, idxtype
*, int);
282 void RandomPermuteFine(int, int *, int);
283 void FastRandomPermute(int, idxtype
*, int);