RLP  1.5
read.c
Go to the documentation of this file.
1 
10 #include <string.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include "read.h"
14 
19 #define POS(R,C,NC) ((R)*(NC)+(C))
20 
22 static char* errors[]={"ERROR! Function \'setCoefHead\' -> \'malloc\'.\n"
23  ,"ERROR! Function \'setCoefHead\' -> \'listInsertLst\'.\n"
24  ,"ERROR! Function \'setCoefHead\' -> \'arrayInsert\'.\n"
25  ,"ERROR! Function \'setCoefHead\' -> \'newExp\'.\n"
26  ,"ERROR! Function \'setCoefHead\' -> \'hashInsert\'.\n"
27  ,"ERROR! Function \'setCoefTail\' -> \'malloc\'.\n"
28  ,"ERROR! Function \'setCoefTail\' -> \'hashInsert\' or"
29  "\'arrayInsert\'.\n"
30  ,"ERROR! Function \'setCoefTail\' -> \'arrayInsert\'.\n"
31  };
32 
33 //##############################################################################
34 
35 Exp newExp(int size)
36 {
37  Exp res;
38 
39  res=malloc(sizeof(SExp));
40 
41  if(res)
42  {
43  res->coefs=newArray(size);
44  res->op=0;
45  res->rhs=0;
46  }
47 
48  return res;
49 }
50 
51 //##############################################################################
52 
54 {
55  Prob res;
56 
57  res=malloc(sizeof(SProb));
58 
59  if(res)
60  {
61  res->pos=newHash(32,0.6,(int(*)(void*))varhash,(int(*)(void*,void*))strcmp);
62  res->invpos=newArray(10);
63  res->exps=newList();
64  }
65 
66  return res;
67 }
68 
69 //##############################################################################
70 
71 int varhash(const char* varid)
72 {
73  int i,res;
74 
75  for(i=0,res=0;varid[i];i++)
76  res+=varid[i];
77 
78  return res;
79 }
80 
81 //##############################################################################
82 
83 int addCoefHead(Prob prob,const char* varid,int varc,double coef)
84 {
85  int* vpos,pos,erro,res;
86  double* tmp;
87  Exp exp;
88 
89  vpos=&pos;
90  res=0;
91 
92  if(hashGet(prob->pos,(void*)varid,(void**)&vpos))
93  {
94  vpos=malloc(sizeof(int));
95 
96  if(!vpos){
97  fputs(errors[0],stderr);
98  exit(1);
99  }
100 
101  *vpos=varc;
102  erro=hashInsert(prob->pos,(void**)varid,vpos,0);
103  erro+=arrayInsert(prob->invpos,varc,(void**)varid,0);
104 
105  if(erro){
106  fputs(errors[4],stderr);
107  exit(5);
108  }
109 
110  res=1;
111  }
112 
113  exp=newExp((varc+1>10)?(varc+1):10);
114 
115  if(!exp){
116  fputs(errors[3],stderr);
117  exit(4);
118  }
119 
120  erro=listInsertLst(prob->exps,exp);
121 
122  if(erro){
123  fputs(errors[1],stderr);
124  exit(2);
125  }
126 
127  tmp=malloc(sizeof(double));
128 
129  if(!tmp){
130  fputs(errors[0],stderr);
131  exit(1);
132  }
133 
134  *tmp=coef;
135  erro=arrayInsert(exp->coefs,*vpos,tmp,0);
136 
137  if(erro){
138  fputs(errors[2],stderr);
139  exit(3);
140  }
141 
142  return res;
143 }
144 
145 //##############################################################################
146 
147 int addCoefTail(Prob prob,const char* varid,int varc,double coef)
148 {
149  int* vpos,pos,res,error;
150  double* tmp;
151  Exp exp;
152 
153  vpos=&pos;
154  res=0;
155 
156  if(hashGet(prob->pos,(void*)varid,(void**)&vpos))
157  {
158  vpos=malloc(sizeof(int));
159 
160  if(!vpos){
161  fputs(errors[5],stderr);
162  exit(6);
163  }
164 
165  *vpos=varc;
166  error=hashInsert(prob->pos,(void**)varid,vpos,0);
167  error+=arrayInsert(prob->invpos,varc,(void**)varid,0);
168 
169  if(error)
170  {
171  fputs(errors[6],stderr);
172  exit(7);
173  }
174 
175  res=1;
176  }
177 
178  listLst(prob->exps,(void**)&exp);
179  tmp=malloc(sizeof(double));
180 
181  if(!tmp){
182  fputs(errors[5],stderr);
183  exit(6);
184  }
185 
186  *tmp=coef;
187  error=arrayInsert(exp->coefs,*vpos,tmp,0);
188 
189  if(error)
190  {
191  fputs(errors[7],stderr);
192  fprintf(stderr,"\n%d\n\n",error);
193  exit(8);
194  }
195 
196  return res;
197 }
198 
199 //##############################################################################
200 
201 int setOpRHS(Prob prob,int op,double rhs)
202 {
203  Exp exp;
204 
205  listLst(prob->exps,(void**)&exp);
206 
207  exp->op=op;
208  exp->rhs=rhs;
209 
210  return 0;
211 }
Prob newProb()
Creates a problem.
Definition: read.c:53
int hashInsert(HashMap hmap, void *key, void *value, int replace)
Associates a value to a key in a hash table.
Definition: hashmap.c:129
Array newArray(int size)
Creates an empty array, with the specified initial capacity.
Definition: array.c:12
Array coefs
Coefficients of the variables.
Definition: read.h:23
int setOpRHS(Prob prob, int op, double rhs)
Adds the relational operator and the right-hand side to a condition.
Definition: read.c:201
int op
Operator of the expression.
Definition: read.h:25
int listInsertLst(List list, void *value)
Inserts an element at the end of a list.
Definition: list.c:79
int hashGet(HashMap hmap, void *key, void **value)
Provides the mapping for a key from a hash table.
Definition: hashmap.c:193
int addCoefTail(Prob prob, const char *varid, int varc, double coef)
Adds the coefficient of a variable.
Definition: read.c:147
int varhash(const char *varid)
Hash function for variables names.
Definition: read.c:71
HashMap pos
Variables and their indexes.
Definition: read.h:41
List exps
List of expressions (objective function, and conditions).
Definition: read.h:45
static char * errors[]
Error messages.
Definition: read.c:22
Problem structure.
Definition: read.h:38
Definitions of data types and functions required to read and store the data about the problem...
HashMap newHash(int size, float factor, int(*hash)(void *), int(*equals)(void *, void *))
Creates a hash table.
Definition: hashmap.c:52
int addCoefHead(Prob prob, const char *varid, int varc, double coef)
Adds a new condition to the problem, and adds the coefficients of a variable.
Definition: read.c:83
Array invpos
Variables on each index.
Definition: read.h:43
Exp newExp(int size)
Creates an expression.
Definition: read.c:35
double rhs
Right-hand side of the expression.
Definition: read.h:27
int listLst(List list, void **value)
Provides the value at the last position of a list.
Definition: list.c:264
List newList(void)
Creates a list.
Definition: list.c:13
int arrayInsert(Array array, int index, void *elem, int replace)
Inserts an new element at the specified position of an array.
Definition: array.c:43
Expression structure.
Definition: read.h:20

RLP © 2006, 2009, 2015   Rui Carlos Gonçalves