variables.c
Go to the documentation of this file.
1 #include "variables.h"
2 
3 #include "error.h"
4 #include "boolean.h"
5 #include "defines.h"
6 
7 #include <stdlib.h>
8 #include <string.h>
9 
10 
21 /*
22  * Init the set of variables.
23  */
25 {
26  unsigned int i;
27 
29  NEW(vs->vs,vs->max_variables,Tvariable *);
30  vs->n=0; /*total*/
31  vs->sys=0; /*system*/
32  vs->sec=0; /*secondary*/
33  vs->dum=0; /*dummies*/
34  vs->car=0; /*cartesian*/
35 
36  for(i=0;i<vs->max_variables;i++)
37  vs->vs[i]=NULL;
38 }
39 
40 void CopyVariables(Tvariables *vs_dst,Tvariables *vs_src)
41 {
42  unsigned int i;
43 
44  vs_dst->sys=vs_src->sys;
45  vs_dst->sec=vs_src->sec;
46  vs_dst->dum=vs_src->dum;
47  vs_dst->car=vs_src->car;
48 
49  vs_dst->n=vs_src->n;
50 
51  vs_dst->max_variables=vs_src->max_variables;
52  NEW(vs_dst->vs,vs_dst->max_variables,Tvariable *);
53 
54  for(i=0;i<vs_src->max_variables;i++)
55  {
56  if (vs_src->vs[i]!=NULL)
57  {
58  NEW(vs_dst->vs[i],1,Tvariable);
59  CopyVariable(vs_dst->vs[i],vs_src->vs[i]);
60  }
61  else
62  vs_dst->vs[i]=NULL;
63  }
64 }
65 
66 /*
67  * Returns the number of variables included in the set 'vs'
68  */
69 unsigned int NVariables(Tvariables *vs)
70 {
71  return(vs->n);
72 }
73 
74 /*
75  * Returns the number of cartesian variables
76  */
78 {
79  return(vs->car);
80 }
81 
83 {
84  return(vs->dum);
85 }
86 
87 /*
88  * Returns the number of system variables
89  */
91 {
92  return(vs->sys);
93 }
94 
95 /*
96  * Returns the number of secondary variables
97  */
99 {
100  return(vs->sec);
101 }
102 
103 boolean IsSystemVariable(unsigned int n,Tvariables *vs)
104 {
105  return((n<vs->n)&&(GetVariableType(vs->vs[n])==SYSTEM_VAR));
106 }
107 
108 boolean IsSecondaryVariable(unsigned int n,Tvariables *vs)
109 {
110  return((n<vs->n)&&(GetVariableType(vs->vs[n])==SECONDARY_VAR));
111 }
112 
113 boolean IsDummyVariable(unsigned int n,Tvariables *vs)
114 {
115  return((n<vs->n)&&(GetVariableType(vs->vs[n])==DUMMY_VAR));
116 }
117 
118 boolean IsCartesianVariable(unsigned int n,Tvariables *vs)
119 {
120  return((n<vs->n)&&(GetVariableType(vs->vs[n])==CARTESIAN_VAR));
121 }
122 
123 unsigned int GetVariableTypeN(unsigned int n,Tvariables *vs)
124 {
125  if (n>=vs->n)
126  Error("Unknown variable ID in GetVariableTypeN");
127 
128  return(GetVariableType(vs->vs[n]));
129 }
130 
131 /*
132  * Add a new variable (pointed by 'v') to the set 'vs'.
133  * If 'v' has the same name of an already existing variable in 'vs' and error is issued (and
134  * the program stopped).
135  * Returns the ID assigned to this variable (the position where it is inserted) and not its order
136  * in the set (first, second,...)
137  */
138 unsigned int AddVariable(Tvariable *v,Tvariables *vs)
139 {
140  if (GetVariableWithName(GetVariableName(v),vs)!=NULL)
141  Error("Duplicated variable");
142  else
143  {
144  unsigned int t;
145 
146  /*look for a free position*/
147  if (vs->n==vs->max_variables)
148  {
149  unsigned int i,k;
150 
151  k=vs->max_variables;
152  MEM_DUP(vs->vs,vs->max_variables,Tvariable *);
153  for(i=k;i<vs->max_variables;i++)
154  vs->vs[i]=NULL;
155  }
156 
157  NEW(vs->vs[vs->n],1,Tvariable);
158  CopyVariable(vs->vs[vs->n],v);
159 
160  t=GetVariableType(v);
161 
162  if (t==SYSTEM_VAR) vs->sys++;
163  if (t==SECONDARY_VAR) vs->sec++;
164  if (t==DUMMY_VAR) vs->dum++;
165  if (t==CARTESIAN_VAR) vs->car++;
166  vs->n++;
167  }
168  return(vs->n-1);
169 }
170 
171 /*
172  * Returns a pointer to the variable description of the variable with the given 'name'
173  * If there is no variable with that name NULL is returned.
174  */
176 {
177  unsigned int i;
178 
179  i=GetVariableID(name,vs);
180  if (i==NO_UINT)
181  return(NULL);
182  else
183  return(vs->vs[i]);
184 }
185 
186 /*
187  * Returns a pointer to the variable with n
188  */
189 Tvariable *GetVariable(unsigned int n,Tvariables *vs)
190 {
191  if (n>=vs->n)
192  Error("Undefined variable referenced in function GetVariable");
193 
194  return(vs->vs[n]);
195 }
196 
197 /*
198  * Returns the ID of variable with name 'name'.
199  * returns -1 if there is no variable with this name
200  */
201 unsigned int GetVariableID(char *name,Tvariables *vs)
202 {
203  boolean found;
204  unsigned int i;
205 
206  found=FALSE;
207  i=0;
208  while((!found)&&(i<vs->n))
209  {
210  if (strcmp(name,GetVariableName(vs->vs[i]))==0)
211  found=TRUE;
212  else
213  i++;
214  }
215 
216  if (found)
217  return(i);
218  else
219  return(NO_UINT);
220 }
221 
222 unsigned int GetVariablesTopology(unsigned int **t,Tvariables *vs)
223 {
224  unsigned int i;
225 
226  NEW(*t,vs->n,unsigned int);
227 
228  for(i=0;i<vs->n;i++)
229  (*t)[i]=GetVariableTopology(vs->vs[i]);
230 
231  return(vs->n);
232 }
233 
234 void GetVariableNames(char **varNames,Tvariables *vs)
235 {
236  unsigned int i;
237 
238  for(i=0;i<vs->n;i++)
239  varNames[i]=GetVariableName(vs->vs[i]);
240 }
241 
242 char *VariableName(unsigned int i,Tvariables *vs)
243 {
244  if (i<vs->n)
245  return(GetVariableName(vs->vs[i]));
246  else
247  return(NULL);
248 }
249 
250 /*
251  * Remove a variable from the set.
252  * WARNING: if you remove a variable from a set used in a
253  * cuiksystem without removing the variable from all the
254  * equations (for instance, by fixing it to zero) you
255  * will get generate an inconsistency on the cuiksystem
256  */
257 void RemoveVariable(unsigned int n,Tvariables *vs)
258 {
259  if (n<vs->n)
260  {
261  unsigned int i,t;
262 
263  t=GetVariableType(vs->vs[n]);
264 
265  DeleteVariable(vs->vs[n]);
266  free(vs->vs[n]);
267 
268  for(i=n+1;i<vs->n;i++)
269  vs->vs[i-1]=vs->vs[i];
270  vs->vs[vs->n-1]=NULL;
271 
272  if (t==SYSTEM_VAR) vs->sys--;
273  if (t==SECONDARY_VAR) vs->sec--;
274  if (t==DUMMY_VAR) vs->dum--;
275  if (t==CARTESIAN_VAR) vs->car--;
276  vs->n--;
277  }
278 }
279 
280 /*
281  Define the initial search space from a set of variables
282 */
284 {
285  unsigned int i,n;
286 
287  n=NVariables(vs);
288  InitBox(n,NULL,b);
289 
290  for(i=0;i<n;i++)
292 }
293 
294 void PointFromVariables(double **p,Tvariables *vs)
295 {
296  unsigned int i,n;
297 
298  n=NVariables(vs);
299  NEW(*p,n,double);
300 
301  for(i=0;i<n;i++)
303 }
304 
306 {
307  unsigned int i,n;
308 
309  n=NVariables(vs);
310 
311  for(i=0;i<n;i++)
313 }
314 
315 /*
316  * Prints the set of variables 'vs' on the file 'f'
317  */
318 void PrintVariables(FILE *f,Tvariables *vs)
319 {
320  unsigned int i;
321  unsigned int t,tn;
322 
323  for(i=0;i<vs->n;i++)
324  {
325  tn=GetVariableType(GetVariable(i,vs));
326  if ((i==0)||(t!=tn))
327  {
328  switch (tn)
329  {
330  case SYSTEM_VAR:
331  fprintf(f,"\n[SYSTEM VARS]\n");
332  break;
333  case SECONDARY_VAR:
334  fprintf(f,"\n[SECONDARY VARS]\n");
335  break;
336  case DUMMY_VAR:
337  fprintf(f,"\n[DUMMY VARS]\n");
338  break;
339  case CARTESIAN_VAR:
340  fprintf(f,"\n[CARTESIAN VARS]\n");
341  break;
342  default:
343  Error("Unknown variable type in PrintVariables");
344  }
345  }
346  t=tn;
347  fprintf(f," ");PrintVariable(f,vs->vs[i]);fprintf(f,"\n");//fprintf(f," %% %u\n",i);
348  }
349 }
350 
351 /*
352  * Deletes the set of variables 'vs' (recursively deleting all the included variables).
353  */
355 {
356  unsigned int i;
357 
358  for(i=0;i<vs->n;i++)
359  {
360  DeleteVariable(vs->vs[i]);
361  free(vs->vs[i]);
362  }
363  free(vs->vs);
364  vs->vs=NULL;
365 }
366 
Definition of the boolean type.
Set of variables of a cuiksystem.
Definition: variables.h:38
#define CARTESIAN_VAR
One of the possible type of variables.
Definition: variable.h:62
unsigned int NVariables(Tvariables *vs)
Gets the number of variables in a set.
Definition: variables.c:69
Tinterval * GetBoxInterval(unsigned int n, Tbox *b)
Returns a pointer to one of the intervals defining the box.
Definition: box.c:270
unsigned int dum
Definition: variables.h:43
Tvariable ** vs
Definition: variables.h:45
#define FALSE
FALSE.
Definition: boolean.h:30
void BoxFromVariables(Tbox *b, Tvariables *vs)
Creates a box from the ranges of a set of variables.
Definition: variables.c:283
unsigned int GetVariableType(Tvariable *v)
Gets the variable type.
Definition: variable.c:60
#define NEW(_var, _n, _type)
Allocates memory space.
Definition: defines.h:385
void PrintVariables(FILE *f, Tvariables *vs)
Prints a set of variables to an stream.
Definition: variables.c:318
Definition of the Tvariables type and the associated functions.
boolean IsSystemVariable(unsigned int n, Tvariables *vs)
Identifies system variables in a set.
Definition: variables.c:103
void SetBoxInterval(unsigned int n, Tinterval *is, Tbox *b)
Replaces a particular interval in a box.
Definition: box.c:259
#define SYSTEM_VAR
One of the possible type of variables.
Definition: variable.h:24
void GetVariableNames(char **varNames, Tvariables *vs)
Gets the name for all the variables in the set.
Definition: variables.c:234
void PrintVariable(FILE *f, Tvariable *v)
Prints a variable.
Definition: variable.c:80
void SetVariableInterval(Tinterval *i, Tvariable *v)
Sets the new range for the variable.
Definition: variable.c:70
#define TRUE
TRUE.
Definition: boolean.h:21
void InitBox(unsigned int dim, Tinterval *is, Tbox *b)
Initializes a box.
Definition: box.c:23
void Error(const char *s)
General error function.
Definition: error.c:80
boolean IsCartesianVariable(unsigned int n, Tvariables *vs)
Identifies cartesian variables in a set.
Definition: variables.c:118
void PointFromVariables(double **p, Tvariables *vs)
Creates a point from the center of the ranges of a set of variables.
Definition: variables.c:294
Error and warning functions.
void VariablesFromBox(Tbox *b, Tvariables *vs)
Define the range for the variables from a box.
Definition: variables.c:305
unsigned int sec
Definition: variables.h:42
unsigned int GetVariableID(char *name, Tvariables *vs)
Gets the variable identifier (i.e., its position in the set) given a variable name.
Definition: variables.c:201
unsigned int max_variables
Definition: variables.h:39
unsigned int n
Definition: variables.h:40
void CopyVariable(Tvariable *v_dst, Tvariable *v_src)
Copy constructor.
Definition: variable.c:33
unsigned int GetNumSystemVariables(Tvariables *vs)
Gets the number of system variables in a set.
Definition: variables.c:90
unsigned int sys
Definition: variables.h:41
void DeleteVariable(Tvariable *v)
Destructor.
Definition: variable.c:95
Definitions of constants and macros used in several parts of the cuik library.
unsigned int GetVariablesTopology(unsigned int **t, Tvariables *vs)
Gets the topology for the variables in the set.
Definition: variables.c:222
unsigned int GetVariableTopology(Tvariable *v)
Gets the topology of the variable.
Definition: variable.c:52
void CopyVariables(Tvariables *vs_dst, Tvariables *vs_src)
Copy onstructor.
Definition: variables.c:40
void RemoveVariable(unsigned int n, Tvariables *vs)
Removes a variable from a set.
Definition: variables.c:257
unsigned int GetNumCartesianVariables(Tvariables *vs)
Gets the number of cartesian variables in a set.
Definition: variables.c:77
#define SECONDARY_VAR
One of the possible type of variables.
Definition: variable.h:44
unsigned int GetNumSecondaryVariables(Tvariables *vs)
Gets the number of secondary variables in a set.
Definition: variables.c:98
#define INIT_NUM_VARIABLES
Initial room for variables.
Definition: variable_set.h:28
unsigned int car
Definition: variables.h:44
A box.
Definition: box.h:83
Data associated with each variable in the problem.
Definition: variable.h:84
#define MEM_DUP(_var, _n, _type)
Duplicates a previously allocated memory space.
Definition: defines.h:414
#define NO_UINT
Used to denote an identifier that has not been initialized.
Definition: defines.h:435
double IntervalCenter(Tinterval *i)
Gets the interval center.
Definition: interval.c:129
boolean IsDummyVariable(unsigned int n, Tvariables *vs)
Identifies dummy variables in a set.
Definition: variables.c:113
void DeleteVariables(Tvariables *vs)
Destructor.
Definition: variables.c:354
char * VariableName(unsigned int i, Tvariables *vs)
Gets the name for a particular variable.
Definition: variables.c:242
char * GetVariableName(Tvariable *v)
Gets the variable name.
Definition: variable.c:65
Tvariable * GetVariable(unsigned int n, Tvariables *vs)
Gets the information of a variable given its number in the set (i.e., its identifier).
Definition: variables.c:189
Tinterval * GetVariableInterval(Tvariable *v)
Gets the range of valid values for the variable.
Definition: variable.c:75
void InitVariables(Tvariables *vs)
Constructor.
Definition: variables.c:24
boolean IsSecondaryVariable(unsigned int n, Tvariables *vs)
Identifies secondary variables in a set.
Definition: variables.c:108
unsigned int GetNumDummyVariables(Tvariables *vs)
Gets the number of dummy variables in a set.
Definition: variables.c:82
unsigned int AddVariable(Tvariable *v, Tvariables *vs)
Adds a variable to the set.
Definition: variables.c:138
Tvariable * GetVariableWithName(char *name, Tvariables *vs)
Gets the information of a variable given the name of this variable.
Definition: variables.c:175
#define DUMMY_VAR
One of the possible type of variables.
Definition: variable.h:53
unsigned int GetVariableTypeN(unsigned int n, Tvariables *vs)
Gets the type of a particular variable.
Definition: variables.c:123