Switch all URLs from ftp.gnu.org to ftpmirror.gnu.org, along with usng http instead...
[foam-extend-3.2.git] / ThirdParty / mingwBuild / x64 / patches / ParMGridGen-1.0 / MGridGen / IMlib / IMlib.h
blob94dcf7673736cd08c65e88b25a5cb05f12256b2d
1 /*
2 * IMlib.h
4 * Irene's library of most frequently used routines
6 */
8 #ifndef _IMLIB_H_
9 #define _IMLIB_H_
12 /* Undefine the following #define in order to use short int as the idxtype */
13 #define IDXTYPE_INT
14 /* Undefine the following #define in order to use float as the realtype */
15 /*#define TYPE_REAL*/
17 /* Indexes are as long as integers for now */
18 #ifdef IDXTYPE_INT
19 typedef int idxtype;
20 #else
21 typedef short idxtype;
22 #endif
24 /* floats for now */
25 #ifdef TYPE_REAL
26 typedef float realtype;
27 #else
28 typedef double realtype;
29 #endif
32 /*************************************************************************
33 * Header file inclusion section
34 **************************************************************************/
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <math.h>
40 #include <stdarg.h>
41 #include <time.h>
42 #include "rand48.h"
44 #ifdef DMALLOC
45 #include <dmalloc.h>
46 #else
47 #include <malloc.h>
48 #endif
50 /*************************************************************************
51 * Data structure definition section
52 **************************************************************************/
53 /*-------------------------------------------------------------
54 * The following data structure stores int key-value pairs
55 *-------------------------------------------------------------*/
56 struct IKeyValueType {
57 int key;
58 int val;
61 typedef struct IKeyValueType IKeyValueType;
64 /*-------------------------------------------------------------
65 * The following data structure stores int key-value pairs
66 *-------------------------------------------------------------*/
67 struct idxKeyValueType {
68 idxtype key;
69 idxtype val;
72 typedef struct idxKeyValueType idxKeyValueType;
75 /*-------------------------------------------------------------
76 * The following data structure stores int-key - double-value pairs
77 *-------------------------------------------------------------*/
78 struct FKeyValueType {
79 double key;
80 int val, val1, val2;
83 typedef struct FKeyValueType FKeyValueType;
86 /*-------------------------------------------------------------
87 * The following data structure stores int-key - double-value pairs
88 *-------------------------------------------------------------*/
89 struct realKeyValueType {
90 realtype key;
91 int val, val1, val2;
94 typedef struct realKeyValueType realKeyValueType;
96 /*************************************************************************
97 * Definition Section
98 **************************************************************************/
99 #define LTERM (void **) 0 /* List terminator for IMfree() */
103 /*************************************************************************
104 * Macros Section
105 **************************************************************************/
106 /*-------------------------------------------------------------
107 * Usefull commands
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 /*-------------------------------------------------------------
124 * Timing macros
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 *-------------------------------------------------------------*/
135 #ifdef DMALLOC
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)))
145 #endif
147 #ifdef DMALLOC
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__); \
152 abort(); \
154 #else
155 # define MALLOC_CHECK(ptr) ;
156 #endif
159 /*-------------------------------------------------------------
160 * CSR conversion macros
161 *-------------------------------------------------------------*/
162 #define MAKECSR(i, n, a) \
163 do { \
164 for (i=1; i<n; i++) a[i] += a[i-1]; \
165 for (i=n; i>0; i--) a[i] = a[i-1]; \
166 a[0] = 0; \
167 } while(0)
170 /*-------------------------------------------------------------
171 * Program Assertions
172 *-------------------------------------------------------------*/
173 #ifdef DEBUG
174 # define ASSERT(expr) \
175 if (!(expr)) { \
176 printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
177 __LINE__, __FILE__); \
178 abort(); \
180 #else
181 # define ASSERT(expr) ;
182 #endif
184 #ifdef DEBUG
185 # define ASSERTP(expr,msg) \
186 if (!(expr)) { \
187 printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
188 __LINE__, __FILE__); \
189 printf msg ; \
190 printf("\n"); \
191 abort(); \
193 #else
194 # define ASSERTP(expr,msg) ;
195 #endif
198 /*************************************************************************
199 * Function prototypes
200 **************************************************************************/
201 /*-------------------------------------------------------------
202 * blas.c
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 /*-------------------------------------------------------------
226 * file.c
227 *-------------------------------------------------------------*/
228 FILE *IMfopen(char *, char *, char *);
229 void IMfclose(FILE *);
231 /*-------------------------------------------------------------
232 * memory.c
233 *-------------------------------------------------------------*/
234 #ifndef DMALLOC
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 *);
244 #endif
245 /* void IMfree(void **, ...); */
248 /*-------------------------------------------------------------
249 * util.c
250 *-------------------------------------------------------------*/
251 void *errexit(char *,...);
252 int IMlog2(int);
253 double flog2(double);
254 int ispow2(int);
255 double seconds(void);
258 /*-------------------------------------------------------------
259 * Sorting routines
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 /*-------------------------------------------------------------
273 * sort.c
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);
285 #endif