-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
364 lines (327 loc) · 7.99 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "def.h"
int I, J, p;
int **w, **t, *h;
double *f_i, *f_j;
int *z; //node index of hubs
int *y; //arch index of hub archs
extern int **sp;
FEDGE **edges;
void initialize_local_memory(void)
{
z = create_and_initialize_int_vector(I, 0);
y = create_and_initialize_int_vector(I * I, 0);
sp = create_int_matrix(I, I);
h = create_int_vector(p);
}
void free_local_memory(void)
{
//limpiar memoria de los vectores usados
for (int i = 0; i < I; i++)
{
free(w[i]);
free(t[i]);
free(sp[i]);
free(edges[i]);
}
free(w);
free(t);
free(f_i);
free(f_j);
free(sp);
free(edges);
}
int **create_int_matrix(int rows, int Columns) // funcion te devuelve un array two dimensions de enteros , tb hace y lo mas importante
{
int i;
int **ptr;
//reserva memoria //aqui estoy creando la fila pero no tiene columna en el **ptr
if ((ptr = (int **)calloc(rows, sizeof(int *))) == NULL)
{ //importante esta funcion tb reserva de bloques de memoria, en este caso reserva cantidad d filas d memoria
printf("\nError: Insuficient memory \n"); // (int *) reservando espacio pa arreglos de #'s
exit(8);
} //creando el arreglo
for (i = 0; i < rows; i++) //aqui por cada fila voy creando las columnas y pa esto uso la funcion create_int_vector(Columns)
ptr[i] = create_int_vector(Columns); //esta funcion crea otra funcion, creando columnas x cada fila
return ptr;
} //finalmente creo mi arreglo bidimensional, para esto llamo a la funcion create_int_vector (column) para crear la dimension columna
int **create_and_initialize_int_matrix(int rows, int Columns, int val) // funcion te devuelve un array two dimensions de enteros , tb hace y lo mas importante
{
int i;
int **ptr;
//reserva memoria //aqui estoy creando la fila pero no tiene columna en el **ptr
if ((ptr = (int **)calloc(rows, sizeof(int *))) == NULL)
{ //importante esta funcion tb reserva de bloques de memoria, en este caso reserva cantidad d filas d memoria
printf("\nError: Insuficient memory \n"); // (int *) reservando espacio pa arreglos de #'s
exit(8);
} //creando el arreglo
for (i = 0; i < rows; i++) //aqui por cada fila voy creando las columnas y pa esto uso la funcion create_int_vector(Columns)
ptr[i] = create_and_initialize_int_vector(Columns, val); //esta funcion crea otra funcion, creando columnas x cada fila
return ptr;
}
double **create_double_matrix(int rows, int Columns)
{
int i;
double **ptr;
if ((ptr = (double **)calloc(rows, sizeof(double *))) == NULL)
{
printf("\nError: Insuficient memory \n");
exit(8);
}
for (i = 0; i < rows; i++)
{
ptr[i] = create_double_vector(Columns);
}
return ptr;
}
double **create_and_initialize_double_matrix(int rows, int Columns, double val)
{
int i;
double **ptr;
if ((ptr = (double **)calloc(rows, sizeof(double *))) == NULL)
{
printf("\nError: Insuficient memory \n");
exit(8);
}
for (i = 0; i < rows; i++)
{
ptr[i] = create_and_initialize_double_vector(Columns, val);
}
return ptr;
}
//esta funcion es reservar la memoria y crea un arreglo unidimensional
int *create_int_vector(int dim)
{
int *ptr;
if ((ptr = (int *)calloc(dim, sizeof(int))) == NULL)
{
printf("\nError: Insuficient memory \n");
exit(8); //8 es un codigo de error de salida
}
return ptr;
}
int *create_and_initialize_int_vector(int dim, int val)
{
int *ptr;
if ((ptr = (int *)calloc(dim, sizeof(int))) == NULL)
{
printf("\nError: Insuficient memory \n");
exit(8); //8 es un codigo de error de salida
}
for (int i = 0; i < dim; i++)
{
ptr[i] = val;
}
return ptr;
}
double *create_double_vector(int dim)
{
double *ptr;
if ((ptr = (double *)calloc(dim, sizeof(double))) == NULL)
{
printf("\nError: Insuficient memory \n");
exit(8);
}
return ptr;
}
double *create_and_initialize_double_vector(int dim, double val)
{
double *ptr;
if ((ptr = (double *)calloc(dim, sizeof(double))) == NULL)
{
printf("\nError: Insuficient memory \n");
exit(8);
}
for (int i = 0; i < dim; i++)
{
ptr[i] = val;
}
return ptr;
}
// CPLEX functions to allocate memeory to arrays
void i_vector(int **vector, int n, char *s)
{
if ((*vector = (int *)calloc(n, sizeof(int))) == NULL)
//error(s);
printf("Error: Insuficient memory \n");
return;
}
void d_vector(double **vector, int n, char *s)
{
if ((*vector = (double *)calloc(n, sizeof(double))) == NULL)
// error(s);
printf("Error: Insuficient memory \n");
return;
}
void c_vector(char **vector, int n, char *s)
{
if ((*vector = (char *)calloc(n, sizeof(char))) == NULL)
//error(s);
printf("Error: Insuficient memory \n");
return;
}
void printMatrix(int **matrix, int a, int b, char *label)
{
printf("%s\n", label);
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
printf("%5d", matrix[i][j]);
printf("\n");
}
}
void printDoubleMatrix(double **matrix, int a, int b, char *label)
{
printf("%s\n", label);
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
printf("%5.2f", matrix[i][j]);
printf("\n");
}
}
void printVector(int *vector, int l, char *label)
{
printf("%s\n", label);
for (int i = 0; i < l; i++)
{
printf("%5d", vector[i]);
}
printf("\n");
}
int **copy_int_matrix(int **matrix, int i, int j)
{
int **res = create_int_matrix(i, j);
for (int x = 0; x < i; x++)
{
res[x] = copy_int_vector(matrix[x], j);
}
return res;
}
int *copy_int_vector(int *vector, int n)
{
int *res = create_int_vector(n);
for (int i = 0; i < n; i++)
res[i] = vector[i];
return res;
}
double **copy_double_matrix(double **matrix, int i, int j)
{
double **res = create_double_matrix(i, j);
for (int x = 0; x < i; x++)
{
res[x] = copy_double_vector(matrix[x], j);
}
return res;
}
double *copy_double_vector(double *vector, int n)
{
double *res = create_double_vector(n);
for (int i = 0; i < n; i++)
res[i] = vector[i];
return res;
}
// stack utils
STACK create_stack(int listLen)
{
STACK stack;
stack.top = -1;
stack.list = create_int_vector(listLen);
return stack;
}
void clean_stack(STACK *stack)
{
free(stack->list);
}
bool in_stack(STACK *stack, int elem)
{
bool res = false;
for (int i = 0; i < stack->top; i++)
{
if (stack->list[i] == elem)
{
res = true;
break;
}
}
return res;
}
void push(int elem, STACK *stack)
{
if (!in_stack(stack, elem))
{
stack->top++;
stack->list[stack->top] = elem;
}
}
int pop(STACK *stack)
{
int res = -1;
if (stack->top >= 0)
{
res = stack->list[stack->top];
stack->top--;
}
return res;
}
void free_int_matrix(int **matrix, int rows)
{
for (int i = 0; i < rows; i++)
free(matrix[i]);
free(matrix);
}
void remove_from_list(int *list, int len, int pos)
{
int *new_list;
int j = 0;
new_list = create_and_initialize_int_vector(len, -1);
for (int i = 0; i < len; i++)
{
if (i != pos)
new_list[j++] = list[i];
}
list = new_list;
free(new_list);
}
void shift_in_list(int *list, int len, int pos, int elem)
{
for (int i = 0; i < len; i++)
{
if (i == pos)
list[i] = elem;
}
}
// ease the time analysis
double elapsed_time(clock_t start, clock_t end)
{
return (double)(end - start) / (double)CLOCKS_PER_SEC;
}
void clone_int_vector(int *origin, int *target, int len)
{
for (int i = 0; i < len; i++)
target[i] = origin[i];
}
void clone_int_matrix(int **origin, int **target, int n, int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
target[i][j] = origin[i][j];
}
}
}
void set_int_vector(int *vector, int len, int value)
{
for (int i = 0; i < len; i++)
vector[i] = value;
}
void set_int_matrix(int **vector, int m, int n, int value)
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
vector[i][j] = value;
}
}
}