PIPS
arguments.c
Go to the documentation of this file.
1 /*
2 
3  $Id: arguments.c 23065 2016-03-02 09:05:50Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS 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 General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 /* Functions dealing with entity lists
25  *
26  * Called "arguments" because the package was developped within the
27  * transformer library where entity lists were used to represent
28  * transformer arguments. No specific link with transformers. Now used
29  * here and there and moved into ri-util.
30  */
31 #ifdef HAVE_CONFIG_H
32  #include "pips_config.h"
33 #endif
34  /* package "arguments"
35  *
36  * Basic routines dealing with the arguments field of transformers
37  * (i.e. list of entities, so it should be put in ri-util like many such
38  * packages written for pips)
39  *
40  * Hash tables were not used because the argument lists are very short
41  *
42  * Francois Irigoin, April 1990
43  */
44 
45 #include <stdio.h>
46 
47 #include "linear.h"
48 #include "genC.h"
49 
50 #include "misc.h"
51 
52 #include "ri-util.h"
53 
55 {
56  if(ENDP(args))
57  (void) fprintf(stderr, "(nil)\n");
58  else {
59  MAPL(c, {entity e = ENTITY(CAR(c));
60  (void) fprintf(stderr,
61  c==args ? "%s" : ", %s",
62  e==entity_undefined? "entity_undefined" : variable_name(e));},
63  args);
64  (void) putc('\n',stderr);
65  }
66 }
67 
68 /* entity_name is a macro, hence the code replication */
69 void dump_arguments(args)
70 cons * args;
71 {
72  if(ENDP(args))
73  (void) fprintf(stderr, "(nil)\n");
74  else {
75  MAPL(c, {entity e = ENTITY(CAR(c));
76  (void) fprintf(stderr,
77  c==args ? "%s" : ", %s",
79  "entity_undefined" : entity_name(e));},
80  args);
81  (void) putc('\n',stderr);
82  }
83 }
84 
86 cons * a;
87 entity e;
88 {
89  if(!entity_is_argument_p(e, a))
90  a = gen_nconc(a, CONS(ENTITY, e, NIL));
91  return a;
92 }
93 
95 cons * a;
96 entity e;
97 {
98  if(entity_is_argument_p(e, a)) {
99  gen_remove(&a, e);
100  }
101  else {
102  pips_internal_error("entity %s is not in a",
103  entity_name(e));
104  }
105 
106  return a;
107 }
108 
109 /* cons * arguments_union(cons * a1, cons * a2): returns a = union(a1, a2)
110  * where a1 and a2 are lists of entities. A new list is allocated.
111  *
112  * Entities in a1 have the same rank wrt a1 and a. Entities in a2 are likely
113  * to have different ranks wrt a and a2. This might imply a transformer
114  * renaming.
115  */
117 cons * a1;
118 cons * a2;
119 {
120  cons * a;
121 
122  if(a1==a2) {
123  a = (cons *) gen_copy_seq(a1);
124  }
125  else {
126  a = (cons *) gen_copy_seq(a1);
127  MAPL(ce, {a = arguments_add_entity(a, ENTITY(CAR(ce)));}, a2);
128  }
129 
130  return a;
131 }
132 
133 /* Check the syntactic equality of lists a1 and a2
134  *
135  * To check the equality of a1 and a2 as sets, use argument
136  * intersection and a cardinal equality, assuming no entity occurs
137  * more than once in a1 or a2.
138  */
140 {
141  list ca1;
142  list ca2;
143 
144  for( ca1 = a1, ca2 = a2; !ENDP(ca1) && !ENDP(ca2); POP(ca1), POP(ca2))
145  if(ENTITY(CAR(ca1))!=ENTITY(CAR(ca2))) break;
146 
147  return ENDP(ca1) && ENDP(ca2);
148 }
149 
150 bool entity_is_argument_p(e, args)
151 entity e;
152 cons * args;
153 {
154  return gen_find_eq(e, args) != chunk_undefined;
155 }
156 
157 /* Build a new list with all entities occuring in both a1 and a2 */
159 {
160  list a = NIL;
161  FOREACH(ENTITY, e1, a1) {
162  if(entity_is_argument_p(e1, a2))
163  /* should gen_nconc be used ?!? Or is it only useful to
164  chain stuff at the end of a list? */
165  a = CONS(ENTITY, e1, a);
166  }
167 
168  return a;
169 }
170 
171 /* Set equality of lists a1 and a2. Check that all entities in a1 also
172  * occur in a2 and vice-versa.
173  *
174  * Might be faster to use the intersection and its cardinal...
175  *
176  * This algorithm is correct if an entity can appear several times in
177  * a list.
178  *
179  * Could be implemented with two calls to arguments_subset_p()
180  */
182 {
183  bool set_equal_p = true;
184 
185  FOREACH(ENTITY, e1, a1) {
186  if(!entity_is_argument_p(e1, a2)) {
187  set_equal_p = false;
188  break;
189  }
190  }
191  if(set_equal_p) {
192  FOREACH(ENTITY, e2, a2) {
193  if(!entity_is_argument_p(e2, a1)) {
194  set_equal_p = false;
195  break;
196  }
197  }
198  }
199 
200  return set_equal_p;
201 }
202 
203 /* Check if a1 is a subset of a2 */
205 {
206  bool subset_p = true;
207 
208  FOREACH(ENTITY, e1, a1) {
209  if(!entity_is_argument_p(e1, a2)) {
210  subset_p = false;
211  break;
212  }
213  }
214 
215  return subset_p;
216 }
217 
218 void free_arguments(args)
219 cons * args;
220 {
221  /* should be a macro later, but keep debugging in mind! */
222  gen_free_list(args);
223 }
224 
226 cons * args;
227 {
228  /* should be a macro later, but keep debugging in mind! */
229  return gen_copy_seq(args);
230 }
231 
232 /* set difference: a1 - a2 ; similar to set intersection */
234 cons * a1;
235 cons * a2;
236 {
237  cons * a = NIL;
238  MAPL(ca1, {
239  entity e1 = ENTITY(CAR(ca1));
240  if(!entity_is_argument_p(e1, a2))
241  /* should gen_nconc be used ?!? Or is it only useful to
242  chain stuff at the end of a list? */
243  a = CONS(ENTITY, e1, a);
244  },
245  a1);
246  return a;
247 }
248 
249 /* generate a Newgen list with all entities refered in vector b */
251 {
252  list el = NIL;
253  Pvecteur ev;
254 
255  for(ev = b; ev!=NULL; ev = ev->succ) {
256  entity e = (entity) vecteur_var(ev);
257  el = CONS(ENTITY, e, el);
258  }
259 
260  gen_nreverse(el);
261 
262  return el;
263 }
struct _newgen_struct_entity_ * entity
Definition: abc_private.h:14
cons * arguments_union(cons *a1, cons *a2)
cons * arguments_union(cons * a1, cons * a2): returns a = union(a1, a2) where a1 and a2 are lists of ...
Definition: arguments.c:116
void print_homogeneous_arguments(list args, const char *variable_name(entity))
Functions dealing with entity lists.
Definition: arguments.c:54
cons * dup_arguments(cons *args)
Definition: arguments.c:225
void free_arguments(cons *args)
Definition: arguments.c:218
bool arguments_set_equal_p(list a1, list a2)
Set equality of lists a1 and a2.
Definition: arguments.c:181
bool entity_is_argument_p(entity e, cons *args)
Definition: arguments.c:150
cons * arguments_add_entity(cons *a, entity e)
Definition: arguments.c:85
bool arguments_subset_p(list a1, list a2)
Check if a1 is a subset of a2.
Definition: arguments.c:204
cons * arguments_rm_entity(cons *a, entity e)
Definition: arguments.c:94
void dump_arguments(cons *args)
entity_name is a macro, hence the code replication
Definition: arguments.c:69
cons * arguments_difference(cons *a1, cons *a2)
set difference: a1 - a2 ; similar to set intersection
Definition: arguments.c:233
list base_to_entities(Pvecteur b)
generate a Newgen list with all entities refered in vector b
Definition: arguments.c:250
bool arguments_equal_p(list a1, list a2)
Check the syntactic equality of lists a1 and a2.
Definition: arguments.c:139
list arguments_intersection(list a1, list a2)
Build a new list with all entities occuring in both a1 and a2.
Definition: arguments.c:158
#define chunk_undefined
obsolete
Definition: genC.h:79
#define ENDP(l)
Test if a list is empty.
Definition: newgen_list.h:66
list gen_nreverse(list cp)
reverse a list in place
Definition: list.c:304
void gen_remove(list *cpp, const void *o)
remove all occurences of item o from list *cpp, which is thus modified.
Definition: list.c:685
#define POP(l)
Modify a list pointer to point on the next element of the list.
Definition: newgen_list.h:59
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
list gen_copy_seq(list l)
Copy a list structure.
Definition: list.c:501
#define CONS(_t_, _i_, _l_)
List element cell constructor (insert an element at the beginning of a list)
Definition: newgen_list.h:150
list gen_nconc(list cp1, list cp2)
physically concatenates CP1 and CP2 but do not duplicates the elements
Definition: list.c:344
#define CAR(pcons)
Get the value of the first element of a list.
Definition: newgen_list.h:92
void gen_free_list(list l)
free the spine of the list
Definition: list.c:327
#define FOREACH(_fe_CASTER, _fe_item, _fe_list)
Apply/map an instruction block on all the elements of a list.
Definition: newgen_list.h:179
void * gen_find_eq(const void *item, const list seq)
Definition: list.c:422
#define MAPL(_map_list_cp, _code, _l)
Apply some code on the addresses of all the elements of a list.
Definition: newgen_list.h:203
#define pips_internal_error
Definition: misc-local.h:149
bool set_equal_p(const set, const set)
returns whether s1 == s2
Definition: set.c:316
#define ENTITY(x)
ENTITY.
Definition: ri.h:2755
#define entity_undefined
Definition: ri.h:2761
#define entity_name(x)
Definition: ri.h:2790
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
char * variable_name(Variable v)
polynome_ri.c
Definition: polynome_ri.c:73
le type des coefficients dans les vecteurs: Value est defini dans le package arithmetique
Definition: vecteur-local.h:89
struct Svecteur * succ
Definition: vecteur-local.h:92
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
#define vecteur_var(v)