PIPS
listes.c
Go to the documentation of this file.
1 /*
2 
3  $Id: listes.c 1671 2019-06-26 19:14:11Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of Linear/C3 Library.
8 
9  Linear/C3 Library is free software: you can redistribute it and/or modify it
10  under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  Linear/C3 Library is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with Linear/C3 Library. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 
25  /* package contrainte - operations sur les listes de contraintes
26  */
27 
28 #ifdef HAVE_CONFIG_H
29  #include "config.h"
30 #endif
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include "linear_assert.h"
35 
36 #include "boolean.h"
37 #include "arithmetique.h"
38 #include "vecteur.h"
39 #include "contrainte.h"
40 
41 
42 /* bool contrainte_in_liste(Pcontrainte c, Pcontrainte lc): test de
43  * l'appartenance d'une contrainte c a une liste de contrainte lc
44  *
45  * Les contrainte sont supposees normalisees (coefficients reduits par leur
46  * PGCD, y compris les termes constants).
47  *
48  * On considere que la contrainte nulle, qui ne represente aucune contrainte
49  * (elle est toujours verifiee) appartient a toutes les listes de contraintes.
50  *
51  */
53 {
54  Pcontrainte c1;
55 
57 
58  if (CONTRAINTE_NULLE_P(c))
59  return true;
60 
61  for (c1=lc; !CONTRAINTE_UNDEFINED_P(c1); c1=c1->succ) {
62  if (vect_equal((c1->vecteur),(c->vecteur))) {
63  return true;
64  }
65  }
66  return false;
67 }
68 
69 /* Return the rank of constraint c in constraint list lc. 1 for the
70  * first element, and so on, Fortran style. 0 when c is not in lc.
71  *
72  * The comparisons are based on the pointers, not on the values of the
73  * constraints. It is mainly useful to detect cycles in constraint list.
74  */
76 {
77  Pcontrainte cc;
78  int rank = 0;
79 
81 
82  if (CONTRAINTE_NULLE_P(c))
83  ;
84  else {
85  for (cc=lc; !CONTRAINTE_UNDEFINED_P(cc); cc=cc->succ) {
86  rank++;
87  // This would be useful to detect duplicate constraints
88  // if (vect_equal((c1->vecteur),(c->vecteur))) {
89 
90  // Physical check
91  if(cc==c)
92  break;
93  }
94  }
95 
96  return rank;
97 }
98 
99 /* bool egalite_in_liste(Pcontrainte eg, Pcontrainte leg): test si une
100  * egalite appartient a une liste d'egalites
101  *
102  * Une egalite peut avoir ete multipliee par moins 1 mais ses coefficients,
103  * comme les coefficients des egalites de la liste, doivent avoir ete
104  * reduits par leur PGCD auparavant
105  *
106  * Ancien nom: vect_in_liste1()
107  */
109 {
110  Pcontrainte v1;
111 
112  if (v->vecteur == NULL) return(true);
113  for (v1=listev;v1!=NULL;v1=v1->succ) {
114  if (vect_equal((v1->vecteur),(v->vecteur)) ||
115  vect_oppos((v1->vecteur),(v->vecteur))) {
116  return true;
117  }
118  }
119  return false;
120 }
121 
122 /* int nb_elems_list(Pcontrainte list): nombre de contraintes se trouvant
123  * dans une liste de contraintes
124  *
125  * Ancien nom: nb_elems_eq()
126  *
127  * Cycles are not detected
128  */
130 {
131  int i;
132  Pcontrainte c;
133 
134  for(i=0, c=list;c!=NULL;i++,c=c->succ)
135  ;
136 
137  return i;
138 }
139 
140 /* Check if list l contains a cycle */
142 {
143  int i;
144  Pcontrainte c;
145  bool cyclic_p = false;
146 
147  for(i=0, c=l; c!=NULL; i++, c=c->succ) {
148  int r = constraint_rank(c, l);
149  if(r>0 && r<i) {
150  cyclic_p = true;
151  break;
152  }
153  }
154 
155  return cyclic_p;
156 }
157 
158 /* Compute the number of elements in the list if it is less than n. n
159  is assumed positive. A negative value is returned if the number of
160  elements is strictly greater than n, for instance because the list
161  is cyclic. */
163 {
164  int i;
165  Pcontrainte c;
166 
167  assert(n>=0);
168 
169  for(i=0, c=list;c!=NULL && i<=n ;i++,c=c->succ)
170  ;
171 
172  return i<=n? i : -1;
173 }
174 
175 /* @return a constraint list without constraint with large coeffs
176  * @param lc list of constraint, which is modified
177  * @param val maximum value allowed for coefficients
178  */
180 {
181  linear_assert("value must be positive", value_posz_p(val));
182 
183  Pcontrainte first = lc, previous = NULL;
184 
185  if (value_zero_p(val)) // nothing to do
186  return lc;
187 
188  while (lc!=NULL)
189  {
190  if (vect_larger_coef_p(lc->vecteur, val))
191  {
192  // unlink and free
193  Pcontrainte next = lc->succ;
194  lc->succ = NULL;
195  contrainte_free(lc);
196  if (lc==first) first = next;
197  if (previous) previous->succ = next;
198  // "previous" constraint itself is unchanged
199  lc = next;
200  }
201  else
202  {
203  previous = lc;
204  lc = lc->succ;
205  }
206  }
207  return first;
208 }
#define value_zero_p(val)
int Value
#define value_posz_p(val)
#define CONTRAINTE_UNDEFINED_P(c)
#define CONTRAINTE_NULLE_P(c)
contrainte nulle (non contrainte 0 == 0 ou 0 <= 0)
Pcontrainte contrainte_free(Pcontrainte c)
Pcontrainte contrainte_free(Pcontrainte c): liberation de l'espace memoire alloue a la contrainte c a...
Definition: alloc.c:184
bool vect_equal(Pvecteur v1, Pvecteur v2)
bool vect_equal(Pvecteur v1, Pvecteur v2): test a egalite de deux vecteurs
Definition: reductions.c:278
bool vect_oppos(Pvecteur v1, Pvecteur v2)
bool vect_oppos(Pvecteur v1, Pvecteur v2): test de l'opposition de deux vecteurs
Definition: reductions.c:360
bool vect_larger_coef_p(Pvecteur v, Value val)
Definition: reductions.c:564
#define linear_assert(msg, ex)
Definition: linear_assert.h:51
bool egalite_in_liste(Pcontrainte v, Pcontrainte listev)
bool egalite_in_liste(Pcontrainte eg, Pcontrainte leg): test si une egalite appartient a une liste d'...
Definition: listes.c:108
int nb_elems_list(Pcontrainte list)
int nb_elems_list(Pcontrainte list): nombre de contraintes se trouvant dans une liste de contraintes
Definition: listes.c:129
bool contrainte_in_liste(Pcontrainte c, Pcontrainte lc)
package contrainte - operations sur les listes de contraintes
Definition: listes.c:52
Pcontrainte contrainte_remove_large_coef(Pcontrainte lc, Value val)
Definition: listes.c:179
int constraint_rank(Pcontrainte c, Pcontrainte lc)
Return the rank of constraint c in constraint list lc.
Definition: listes.c:75
int safe_nb_elems_list(Pcontrainte list, int n)
Compute the number of elements in the list if it is less than n.
Definition: listes.c:162
bool cyclic_constraint_list_p(Pcontrainte l)
Check if list l contains a cycle.
Definition: listes.c:141
static entity rank
#define assert(ex)
Definition: newgen_assert.h:41
Pvecteur vecteur
struct Scontrainte * succ
The structure used to build lists in NewGen.
Definition: newgen_list.h:41