1 /*
2 * Name: misc.h
3 * Author: Pietro Belotti
4 *
5 * This code is published under the Eclipse Public License (EPL).
6 * See http://www.eclipse.org/legal/epl-v10.html
7 *
8 */
9
10 #ifndef MISC_H
11 #define MISC_H
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <math.h>
17 #include <bzlib.h>
18
19 #include "sparse.h"
20 #include "lpio.h"
21 #include "rtr.h"
22
23 #define MAX_STR 40000
24 #define MALLOC_BLOCK 5000
25 #define CHUNKS_PER_LINE 10
26
27
28 void printLP (int *, sparseLP *); /* Print LP (for debugging purposes) */
29 void clearLP (sparseLP *); /* Remove LP from memory */
30
31 /* Functions to read from a bzip compressed file */
32
33 int bzgetdbl (BZFILE *, double *); /* get double from .bz2 file */
34 int bzgetchar (BZFILE *, char *); /* get char from .bz2 file */
35
bzgetint(BZFILE * b,register int * val)36 static inline int bzgetint (BZFILE *b, register int *val) { /* get integer from .bz2 file */
37
38 double x;
39 register int ret_val;
40
41 ret_val = bzgetdbl (b, &x);
42
43 *val = floor (x);
44
45 return ret_val;
46 }
47
48 /* Auxiliary functions to reallocate vectors of double/int */
49
reallocate_double(int req,int * size,double ** buf)50 static inline void reallocate_double (int req, int *size, double **buf) {
51
52 if (req >= *size)
53 *buf = (double *) realloc (*buf, (*size = (MALLOC_BLOCK * (req/MALLOC_BLOCK + 1))) * sizeof (double));
54 }
55
reallocate_int(int req,int * size,int ** buf)56 static inline void reallocate_int (int req, int *size, int **buf) {
57
58 if (req >= *size)
59 *buf = (int *) realloc (*buf, (*size = (MALLOC_BLOCK * (req/MALLOC_BLOCK + 1))) * sizeof (int));
60 }
61
62 void create_transpose (sparseLP *); /* Transpose coefficient matrix */
63
64
65 /* Get the norm of a vector of size l */
66
get_norm(register double * v,register int l)67 static inline double get_norm (register double *v, register int l) {
68
69 register double norm = 0;
70 for (; l>0; --l, ++v) norm += *v * *v;
71 return sqrt (norm);
72 }
73
74 #endif
75