1 /******************************************************************************
2 * PIP : Parametric Integer Programming *
3 ******************************************************************************
5 ******************************************************************************
7 * Copyright Paul Feautrier, 1988, 1993, 1994, 1996, 2002 *
9 * This is free software; you can redistribute it and/or modify it under the *
10 * terms of the GNU General Public License as published by the Free Software *
11 * Foundation; either version 2 of the License, or (at your option) any later *
14 * This software is distributed in the hope that it will be useful, but *
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
19 * You should have received a copy of the GNU General Public License along *
20 * with software; if not, write to the Free Software Foundation, Inc., *
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
23 * Written by Paul Feautrier and Cedric Bastoul *
25 ******************************************************************************/
31 #include <piplib/piplib.h>
33 #define TAB_CHUNK 4096*sizeof(Entier)
35 static char *tab_free
, *tab_top
;
36 static struct A
*tab_base
;
38 extern int allocation
;
39 extern long int cross_product
, limit
;
40 static int chunk_count
;
43 #if defined(LINEAR_VALUE_IS_MP)
44 int dscanf(FILE *, int *);
46 int dscanf(FILE *, Entier
*);
53 tab_free
= malloc(sizeof (struct A
));
55 {fprintf(stderr
, "Your computer doesn't have enough memory\n");
59 tab_top
= tab_free
+ sizeof (struct A
);
60 tab_base
= (struct A
*)tab_free
;
61 tab_free
+= sizeof(struct A
);
62 tab_base
->precedent
= NULL
;
63 tab_base
->bout
= tab_top
;
67 struct high_water_mark
tab_hwm(void)
68 {struct high_water_mark p
;
69 p
.chunk
= chunk_count
;
75 #if defined(LINEAR_VALUE_IS_MP)
76 /* the clear_tab routine clears the GMP objects which may be referenced
79 void tab_clear(Tableau
*tp
)
82 /* clear the determinant */
83 mpz_clear(tp
->determinant
);
85 for(i
=0; i
<tp
->height
; i
++){
86 /* clear the denominator */
87 mpz_clear(Denom(tp
, i
));
88 if((Flag(tp
, i
) & Unit
) == 0)
89 for(j
=0; j
<tp
->width
; j
++)
90 mpz_clear(Index(tp
,i
,j
));
95 void tab_reset(struct high_water_mark by_the_mark
)
99 while(chunk_count
> by_the_mark
.chunk
)
101 g
= tab_base
->precedent
;
103 #if defined(LINEAR_VALUE_IS_MP)
104 /* Before actually freeing the memory, one has to clear the
105 * included Tableaux. If this is not done, the GMP objects
106 * referenced in the Tableaux will be orphaned.
109 /* Enumerate the included tableaux. */
110 p
= (char *)tab_base
+ sizeof(struct A
);
121 tab_top
= tab_base
->bout
;
124 if(chunk_count
> 0) tab_free
= by_the_mark
.top
;
126 fprintf(stderr
, "Syserr: tab_reset : error in memory allocation\n");
131 Tableau
* tab_alloc(int h
, int w
, int n
)
133 /* h : le nombre de ligne reelles;
134 n : le nombre de lignes virtuelles
137 char *p
; Tableau
*tp
;
139 unsigned long taille
;
141 taille
= sizeof(struct T
) + (h
+n
-1) * sizeof (struct L
)
142 + h
* w
* sizeof (Entier
);
143 if(tab_free
+ taille
>= tab_top
)
146 d
= taille
+ sizeof(struct A
);
147 if(d
< TAB_CHUNK
) d
= TAB_CHUNK
;
148 tab_free
= malloc(d
);
150 {printf("Memory overflow\n");
154 g
= (struct A
*)tab_free
;
155 g
->precedent
= tab_base
;
156 tab_top
= tab_free
+ d
;
157 tab_free
+= sizeof(struct A
);
164 q
= (Entier
*)(p
+ sizeof(struct T
) + (h
+n
-1) * sizeof (struct L
));
165 #if defined(LINEAR_VALUE_IS_MP)
166 mpz_init_set_ui(tp
->determinant
,1);
168 tp
->determinant
[0] = (Entier
) 1;
169 tp
->l_determinant
= 1;
171 for(i
= 0; i
<n
; i
++){
172 tp
->row
[i
].flags
= Unit
;
173 tp
->row
[i
].objet
.unit
= i
;
174 #if defined(LINEAR_VALUE_IS_MP)
175 mpz_init_set_ui(Denom(tp
, i
), 1);
180 for(i
= n
; i
< (h
+n
); i
++){
181 tp
->row
[i
].flags
= 0;
182 tp
->row
[i
].objet
.val
= q
;
183 for(j
= 0; j
< w
; j
++)
184 #if defined(LINEAR_VALUE_IS_MP)
185 mpz_init_set_ui(*q
++, 0); /* loop body. */
186 mpz_init_set_ui(Denom(tp
, i
), 0);
188 *q
++ = 0; /* loop body. */
189 Denom(tp
, i
) = ZERO
;
192 tp
->height
= h
+ n
; tp
->width
= w
;
193 #if defined(LINEAR_VALUE_IS_MP)
194 tp
->taille
= taille
;
200 Tableau
* tab_get(foo
, h
, w
, n
)
206 #if defined(LINEAR_VALUE_IS_MP)
212 p
= tab_alloc(h
, w
, n
);
213 while((c
= dgetc(foo
)) != EOF
)
215 for(i
= n
; i
<h
+n
; i
++)
216 {p
->row
[i
].flags
= Unknown
;
217 #if defined(LINEAR_VALUE_IS_MP)
218 mpz_set_ui(Denom(p
, i
), 1);
222 while((c
= dgetc(foo
)) != EOF
)if(c
== '[')break;
223 for(j
= 0; j
<w
; j
++){
224 if(dscanf(foo
, &x
) < 0)
227 #if defined(LINEAR_VALUE_IS_MP)
228 mpz_set_si(p
->row
[i
].objet
.val
[j
], x
);
230 p
->row
[i
].objet
.val
[j
] = x
;
234 while((c
= dgetc(foo
)) != EOF
)if(c
== ']')break;
240 /* Fonction tab_Matrix2Tableau :
241 * Cette fonction effectue la conversion du format de matrice de la polylib
242 * vers le format de traitement de Pip. matrix est la matrice a convertir.
243 * Nineq est le nombre d'inequations necessaires (dans le format de la
244 * polylib, le premier element d'une ligne indique si l'equation decrite
245 * est une inequation ou une egalite. Pip ne gere que les inequations. On
246 * compte donc le nombre d'inequations total pour reserver la place
247 * necessaire, et on scinde toute egalite p(x)=0 en p(x)>=0 et -p(x)>=0).
248 * Nv est le nombre de variables dans la premiere serie de variables (c'est
249 * a dire que si les premiers coefficients dans les lignes de la matrice
250 * sont ceux des inconnues, Nv est le nombre d'inconnues, resp. parametres).
251 * n est le nombre de lignes 'virtuelles' contenues dans la matrice (c'est
252 * a dire en fait le nombre d'inconnues).
253 * 27 juillet 2001 : Premiere version, Ced.
254 * 30 juillet 2001 : Nombreuses modifications. Le calcul du nombre total
255 * d'inequations (Nineq) se fait a present a l'exterieur.
256 * 3 octobre 2001 : Pas mal d'ameliorations.
258 Tableau
* tab_Matrix2Tableau(PipMatrix
* matrix
, int Nineq
, int Nv
, int n
)
260 unsigned i
, j
, end
, current
, new, nb_columns
, decal
=0 ;
261 Entier
* entier
, inequality
;
263 #if defined(LINEAR_VALUE_IS_MP)
264 mpz_init(inequality
) ;
266 nb_columns
= matrix
->NbColumns
- 1 ;
267 p
= tab_alloc(Nineq
,nb_columns
,n
) ;
269 /* La variable decal sert a prendre en compte les lignes supplementaires
270 * issues des egalites.
272 end
= matrix
->NbRows
+ n
;
274 { current
= i
+ decal
;
275 Flag(p
,current
) = Unknown
;
276 #if defined(LINEAR_VALUE_IS_MP)
277 mpz_set_ui(Denom(p
,current
),1) ;
279 Denom(p
,current
) = UN
;
281 entier
= *(matrix
->p
+ i
- n
) ;
282 /* Pour passer l'indicateur d'egalite/inegalite. */
283 #if defined(LINEAR_VALUE_IS_MP)
284 mpz_set(inequality
,*entier
) ;
286 inequality
= *entier
;
290 /* Dans le format de la polylib, l'element constant est place en
291 * dernier. Dans le format de Pip, il se trouve apres la premiere
292 * serie de variables (inconnues ou parametres). On remet donc les
293 * choses dans l'ordre de Pip. Ici pour p(x) >= 0.
296 #if defined(LINEAR_VALUE_IS_MP)
297 mpz_set(*(p
->row
[current
].objet
.val
+ j
),*entier
++) ;
299 *(p
->row
[current
].objet
.val
+ j
) = *entier
++ ;
301 for (j
=Nv
+1;j
<nb_columns
;j
++)
302 #if defined(LINEAR_VALUE_IS_MP)
303 mpz_set(*(p
->row
[current
].objet
.val
+ j
),*entier
++) ;
304 mpz_set(*(p
->row
[current
].objet
.val
+ Nv
),*entier
) ;
306 *(p
->row
[current
].objet
.val
+ j
) = *entier
++ ;
307 *(p
->row
[current
].objet
.val
+ Nv
) = *entier
;
310 /* Et ici lors de l'ajout de -p(x) >= 0 quand on traite une egalite. */
314 Flag(p
,new)= Unknown
;
315 #if defined(LINEAR_VALUE_IS_MP)
316 mpz_set(Denom(p
,new),UN
) ;
321 for (j
=0;j
<nb_columns
;j
++)
322 #if defined(LINEAR_VALUE_IS_MP)
323 mpz_neg(*(p
->row
[new].objet
.val
+ j
),*(p
->row
[current
].objet
.val
+ j
)) ;
325 *(p
->row
[new].objet
.val
+ j
) = -(*(p
->row
[current
].objet
.val
+ j
)) ;
329 #if defined(LINEAR_VALUE_IS_MP)
330 mpz_clear(inequality
);
336 char *Attr
[] = {"Unit", "+", "-", "0", "*", "?"};
338 void tab_display(p
, foo
)
343 int i
, j
, ff
, fff
, n
;
345 #if defined(LINEAR_VALUE_IS_MP)
349 fprintf(foo
, "%ld/[%d * %d]\n", cross_product
, p
->height
, p
->width
);
350 for(i
= 0; i
<p
->height
; i
++){
351 fff
= ff
= p
->row
[i
].flags
;
352 /* if(fff ==0) continue; */
353 #if defined(LINEAR_VALUE_IS_MP)
354 mpz_set(d
, Denom(p
, i
));
360 if(fff
& 1) fprintf(foo
, "%s ",Attr
[n
]);
363 fprintf(foo
, "%f #[", p
->row
[i
].size
);
365 for(j
= 0; j
<p
->width
; j
++)
366 fprintf(foo
, " /%d/",(j
== p
->row
[i
].objet
.unit
)? 1: 0);
368 for(j
= 0; j
<p
->width
; j
++){
369 #if defined(LINEAR_VALUE_IS_MP)
370 mpz_out_str(foo
, 10, Index(p
, i
, j
));
374 fprintf(foo
, FORMAT
, x
);
379 #if defined(LINEAR_VALUE_IS_MP)
380 mpz_out_str(foo
, 10, d
);
382 fprintf(foo
, "%d", (int)d
);
386 #if defined(LINEAR_VALUE_IS_MP)