fix typos in user manual
[isl.git] / isl_union_templ.c
blobb2ad551ed9194b524ad332491a8c72c7fb2ecf43
1 /*
2 * Copyright 2010 INRIA Saclay
3 * Copyright 2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9 * 91893 Orsay, France
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 __isl_give UNION *FN(UNION,cow)(__isl_take UNION *u);
15 isl_ctx *FN(UNION,get_ctx)(__isl_keep UNION *u)
17 return u ? u->space->ctx : NULL;
20 __isl_give isl_space *FN(UNION,get_space)(__isl_keep UNION *u)
22 if (!u)
23 return NULL;
24 return isl_space_copy(u->space);
27 /* Return the number of parameters of "u", where "type"
28 * is required to be set to isl_dim_param.
30 unsigned FN(UNION,dim)(__isl_keep UNION *u, enum isl_dim_type type)
32 if (!u)
33 return 0;
35 if (type != isl_dim_param)
36 isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
37 "can only reference parameters", return 0);
39 return isl_space_dim(u->space, type);
42 /* Return the position of the parameter with the given name
43 * in "u".
44 * Return -1 if no such dimension can be found.
46 int FN(UNION,find_dim_by_name)(__isl_keep UNION *u, enum isl_dim_type type,
47 const char *name)
49 if (!u)
50 return -1;
51 return isl_space_find_dim_by_name(u->space, type, name);
54 #ifdef HAS_TYPE
55 static __isl_give UNION *FN(UNION,alloc)(__isl_take isl_space *dim,
56 enum isl_fold type, int size)
57 #else
58 static __isl_give UNION *FN(UNION,alloc)(__isl_take isl_space *dim, int size)
59 #endif
61 UNION *u;
63 dim = isl_space_params(dim);
64 if (!dim)
65 return NULL;
67 u = isl_calloc_type(dim->ctx, UNION);
68 if (!u)
69 goto error;
71 u->ref = 1;
72 #ifdef HAS_TYPE
73 u->type = type;
74 #endif
75 u->space = dim;
76 if (isl_hash_table_init(dim->ctx, &u->table, size) < 0)
77 return FN(UNION,free)(u);
79 return u;
80 error:
81 isl_space_free(dim);
82 return NULL;
85 #ifdef HAS_TYPE
86 __isl_give UNION *FN(UNION,ZERO)(__isl_take isl_space *dim, enum isl_fold type)
88 return FN(UNION,alloc)(dim, type, 16);
90 #else
91 __isl_give UNION *FN(UNION,ZERO)(__isl_take isl_space *dim)
93 return FN(UNION,alloc)(dim, 16);
95 #endif
97 __isl_give UNION *FN(UNION,copy)(__isl_keep UNION *u)
99 if (!u)
100 return NULL;
102 u->ref++;
103 return u;
106 /* Extract the element of "u" living in "space" (ignoring parameters).
108 * Return the ZERO element if "u" does not contain any element
109 * living in "space".
111 __isl_give PART *FN(FN(UNION,extract),PARTS)(__isl_keep UNION *u,
112 __isl_take isl_space *space)
114 struct isl_hash_table_entry *entry;
115 isl_bool equal_params;
117 if (!u || !space)
118 goto error;
119 equal_params = isl_space_has_equal_params(u->space, space);
120 if (equal_params < 0)
121 goto error;
122 if (!equal_params) {
123 space = isl_space_drop_dims(space, isl_dim_param,
124 0, isl_space_dim(space, isl_dim_param));
125 space = isl_space_align_params(space,
126 FN(UNION,get_space)(u));
127 if (!space)
128 goto error;
131 entry = FN(UNION,find_part_entry)(u, space, 0);
132 if (!entry)
133 goto error;
134 if (entry == isl_hash_table_entry_none)
135 #ifdef HAS_TYPE
136 return FN(PART,ZERO)(space, u->type);
137 #else
138 return FN(PART,ZERO)(space);
139 #endif
140 isl_space_free(space);
141 return FN(PART,copy)(entry->data);
142 error:
143 isl_space_free(space);
144 return NULL;
147 /* Add "part" to "u".
148 * If "disjoint" is set, then "u" is not allowed to already have
149 * a part that is defined over a domain that overlaps with the domain
150 * of "part".
151 * Otherwise, compute the union sum of "part" and the part in "u"
152 * defined on the same space.
154 static __isl_give UNION *FN(UNION,add_part_generic)(__isl_take UNION *u,
155 __isl_take PART *part, int disjoint)
157 int empty;
158 struct isl_hash_table_entry *entry;
160 if (!part)
161 goto error;
163 empty = FN(PART,IS_ZERO)(part);
164 if (empty < 0)
165 goto error;
166 if (empty) {
167 FN(PART,free)(part);
168 return u;
171 u = FN(UNION,align_params)(u, FN(PART,get_space)(part));
172 part = FN(PART,align_params)(part, FN(UNION,get_space)(u));
174 u = FN(UNION,cow)(u);
176 if (!u)
177 goto error;
179 if (FN(UNION,check_disjoint_domain_other)(u, part) < 0)
180 goto error;
181 entry = FN(UNION,find_part_entry)(u, part->dim, 1);
182 if (!entry)
183 goto error;
185 if (!entry->data)
186 entry->data = part;
187 else {
188 if (disjoint &&
189 FN(UNION,check_disjoint_domain)(entry->data, part) < 0)
190 goto error;
191 entry->data = FN(PART,union_add_)(entry->data,
192 FN(PART,copy)(part));
193 if (!entry->data)
194 goto error;
195 empty = FN(PART,IS_ZERO)(part);
196 if (empty < 0)
197 goto error;
198 if (empty)
199 u = FN(UNION,remove_part_entry)(u, entry);
200 FN(PART,free)(part);
203 return u;
204 error:
205 FN(PART,free)(part);
206 FN(UNION,free)(u);
207 return NULL;
210 /* Add "part" to "u", where "u" is assumed not to already have
211 * a part that is defined on the same space as "part".
213 __isl_give UNION *FN(FN(UNION,add),PARTS)(__isl_take UNION *u,
214 __isl_take PART *part)
216 return FN(UNION,add_part_generic)(u, part, 1);
219 #ifdef HAS_TYPE
220 /* Allocate a UNION with the same type and the same size as "u" and
221 * with space "space".
223 static __isl_give UNION *FN(UNION,alloc_same_size_on_space)(__isl_keep UNION *u,
224 __isl_take isl_space *space)
226 if (!u)
227 goto error;
228 return FN(UNION,alloc)(space, u->type, u->table.n);
229 error:
230 isl_space_free(space);
231 return NULL;
233 #else
234 /* Allocate a UNION with the same size as "u" and with space "space".
236 static __isl_give UNION *FN(UNION,alloc_same_size_on_space)(__isl_keep UNION *u,
237 __isl_take isl_space *space)
239 if (!u)
240 goto error;
241 return FN(UNION,alloc)(space, u->table.n);
242 error:
243 isl_space_free(space);
244 return NULL;
246 #endif
248 /* Allocate a UNION with the same space, the same type (if any) and
249 * the same size as "u".
251 static __isl_give UNION *FN(UNION,alloc_same_size)(__isl_keep UNION *u)
253 return FN(UNION,alloc_same_size_on_space)(u, FN(UNION,get_space)(u));
256 /* Internal data structure for isl_union_*_transform_space.
257 * "fn' is applied to each entry in the input.
258 * "res" collects the results.
260 S(UNION,transform_data)
262 __isl_give PART *(*fn)(__isl_take PART *part, void *user);
263 void *user;
265 UNION *res;
268 /* Apply data->fn to "part" and add the result to data->res.
270 static isl_stat FN(UNION,transform_entry)(__isl_take PART *part, void *user)
272 S(UNION,transform_data) *data = (S(UNION,transform_data) *)user;
274 part = data->fn(part, data->user);
275 data->res = FN(FN(UNION,add),PARTS)(data->res, part);
276 if (!data->res)
277 return isl_stat_error;
279 return isl_stat_ok;
282 /* Return a UNION living in "space" that is obtained by applying "fn"
283 * to each of the entries in "u".
285 static __isl_give UNION *FN(UNION,transform_space)(__isl_take UNION *u,
286 isl_space *space,
287 __isl_give PART *(*fn)(__isl_take PART *part, void *user), void *user)
289 S(UNION,transform_data) data = { fn, user };
291 data.res = FN(UNION,alloc_same_size_on_space)(u, space);
292 if (FN(FN(UNION,foreach),PARTS)(u,
293 &FN(UNION,transform_entry), &data) < 0)
294 data.res = FN(UNION,free)(data.res);
295 FN(UNION,free)(u);
296 return data.res;
299 /* Return a UNION that lives in the same space as "u" and that is obtained
300 * by applying "fn" to each of the entries in "u".
302 static __isl_give UNION *FN(UNION,transform)(__isl_take UNION *u,
303 __isl_give PART *(*fn)(__isl_take PART *part, void *user), void *user)
305 return FN(UNION,transform_space)(u, FN(UNION,get_space)(u), fn, user);
308 /* Apply data->fn to *part and store the result back into *part.
310 static isl_stat FN(UNION,transform_inplace_entry)(void **part, void *user)
312 S(UNION,transform_data) *data = (S(UNION,transform_data) *) user;
314 *part = data->fn(*part, data->user);
315 if (!*part)
316 return isl_stat_error;
317 return isl_stat_ok;
320 /* Update "u" by applying "fn" to each entry.
321 * This operation is assumed not to change the number of entries nor
322 * the spaces of the entries.
324 * If there is only one reference to "u", then change "u" inplace.
325 * Otherwise, create a new UNION from "u" and discard the original.
327 static __isl_give UNION *FN(UNION,transform_inplace)(__isl_take UNION *u,
328 __isl_give PART *(*fn)(__isl_take PART *part, void *user), void *user)
330 isl_bool single_ref;
332 single_ref = FN(UNION,has_single_reference)(u);
333 if (single_ref < 0)
334 return FN(UNION,free)(u);
335 if (single_ref) {
336 S(UNION,transform_data) data = { fn, user };
337 if (FN(UNION,foreach_inplace)(u,
338 &FN(UNION,transform_inplace_entry), &data) < 0)
339 return FN(UNION,free)(u);
340 return u;
342 return FN(UNION,transform)(u, fn, user);
345 /* An isl_union_*_transform callback for use in isl_union_*_dup
346 * that simply returns "part".
348 static __isl_give PART *FN(UNION,copy_part)(__isl_take PART *part, void *user)
350 return part;
353 __isl_give UNION *FN(UNION,dup)(__isl_keep UNION *u)
355 u = FN(UNION,copy)(u);
356 return FN(UNION,transform)(u, &FN(UNION,copy_part), NULL);
359 __isl_give UNION *FN(UNION,cow)(__isl_take UNION *u)
361 if (!u)
362 return NULL;
364 if (u->ref == 1)
365 return u;
366 u->ref--;
367 return FN(UNION,dup)(u);
370 __isl_null UNION *FN(UNION,free)(__isl_take UNION *u)
372 if (!u)
373 return NULL;
375 if (--u->ref > 0)
376 return NULL;
378 isl_hash_table_foreach(u->space->ctx, &u->table,
379 &FN(UNION,free_u_entry), NULL);
380 isl_hash_table_clear(&u->table);
381 isl_space_free(u->space);
382 free(u);
383 return NULL;
386 static __isl_give PART *FN(UNION,align_entry)(__isl_take PART *part, void *user)
388 isl_reordering *exp = user;
390 exp = isl_reordering_extend_space(isl_reordering_copy(exp),
391 FN(PART,get_domain_space)(part));
392 return FN(PART,realign_domain)(part, exp);
395 /* Reorder the parameters of "u" according to the given reordering.
397 static __isl_give UNION *FN(UNION,realign_domain)(__isl_take UNION *u,
398 __isl_take isl_reordering *r)
400 isl_space *space;
402 if (!u || !r)
403 goto error;
405 space = isl_space_copy(r->dim);
406 u = FN(UNION,transform_space)(u, space, &FN(UNION,align_entry), r);
407 isl_reordering_free(r);
408 return u;
409 error:
410 FN(UNION,free)(u);
411 isl_reordering_free(r);
412 return NULL;
415 /* Align the parameters of "u" to those of "model".
417 __isl_give UNION *FN(UNION,align_params)(__isl_take UNION *u,
418 __isl_take isl_space *model)
420 isl_bool equal_params;
421 isl_reordering *r;
423 if (!u || !model)
424 goto error;
426 equal_params = isl_space_has_equal_params(u->space, model);
427 if (equal_params < 0)
428 goto error;
429 if (equal_params) {
430 isl_space_free(model);
431 return u;
434 model = isl_space_params(model);
435 r = isl_parameter_alignment_reordering(u->space, model);
436 isl_space_free(model);
438 return FN(UNION,realign_domain)(u, r);
439 error:
440 isl_space_free(model);
441 FN(UNION,free)(u);
442 return NULL;
445 /* Add "part" to *u, taking the union sum if "u" already has
446 * a part defined on the same space as "part".
448 static isl_stat FN(UNION,union_add_part)(__isl_take PART *part, void *user)
450 UNION **u = (UNION **)user;
452 *u = FN(UNION,add_part_generic)(*u, part, 0);
454 return isl_stat_ok;
457 /* Compute the sum of "u1" and "u2" on the union of their domains,
458 * with the actual sum on the shared domain and
459 * the defined expression on the symmetric difference of the domains.
461 * This is an internal function that is exposed under different
462 * names depending on whether the base expressions have a zero default
463 * value.
464 * If they do, then this function is called "add".
465 * Otherwise, it is called "union_add".
467 static __isl_give UNION *FN(UNION,union_add_)(__isl_take UNION *u1,
468 __isl_take UNION *u2)
470 u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
471 u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
473 u1 = FN(UNION,cow)(u1);
475 if (!u1 || !u2)
476 goto error;
478 if (FN(FN(UNION,foreach),PARTS)(u2, &FN(UNION,union_add_part), &u1) < 0)
479 goto error;
481 FN(UNION,free)(u2);
483 return u1;
484 error:
485 FN(UNION,free)(u1);
486 FN(UNION,free)(u2);
487 return NULL;
490 __isl_give UNION *FN(FN(UNION,from),PARTS)(__isl_take PART *part)
492 isl_space *dim;
493 UNION *u;
495 if (!part)
496 return NULL;
498 dim = FN(PART,get_space)(part);
499 dim = isl_space_drop_dims(dim, isl_dim_in, 0, isl_space_dim(dim, isl_dim_in));
500 dim = isl_space_drop_dims(dim, isl_dim_out, 0, isl_space_dim(dim, isl_dim_out));
501 #ifdef HAS_TYPE
502 u = FN(UNION,ZERO)(dim, part->type);
503 #else
504 u = FN(UNION,ZERO)(dim);
505 #endif
506 u = FN(FN(UNION,add),PARTS)(u, part);
508 return u;
511 S(UNION,match_bin_data) {
512 UNION *u2;
513 UNION *res;
514 __isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *);
517 /* Check if data->u2 has an element living in the same space as "part".
518 * If so, call data->fn on the two elements and add the result to
519 * data->res.
521 static isl_stat FN(UNION,match_bin_entry)(__isl_take PART *part, void *user)
523 S(UNION,match_bin_data) *data = user;
524 struct isl_hash_table_entry *entry2;
525 isl_space *space;
526 PART *part2;
528 space = FN(PART,get_space)(part);
529 entry2 = FN(UNION,find_part_entry)(data->u2, space, 0);
530 isl_space_free(space);
531 if (!entry2)
532 goto error;
533 if (entry2 == isl_hash_table_entry_none) {
534 FN(PART,free)(part);
535 return isl_stat_ok;
538 part2 = entry2->data;
539 if (!isl_space_tuple_is_equal(part->dim, isl_dim_out,
540 part2->dim, isl_dim_out))
541 isl_die(FN(UNION,get_ctx)(data->u2), isl_error_invalid,
542 "entries should have the same range space",
543 goto error);
545 part = data->fn(part, FN(PART, copy)(entry2->data));
547 data->res = FN(FN(UNION,add),PARTS)(data->res, part);
548 if (!data->res)
549 return isl_stat_error;
551 return isl_stat_ok;
552 error:
553 FN(PART,free)(part);
554 return isl_stat_error;
557 /* This function is currently only used from isl_polynomial.c
558 * and not from isl_fold.c.
560 static __isl_give UNION *FN(UNION,match_bin_op)(__isl_take UNION *u1,
561 __isl_take UNION *u2,
562 __isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *))
563 __attribute__ ((unused));
564 /* For each pair of elements in "u1" and "u2" living in the same space,
565 * call "fn" and collect the results.
567 static __isl_give UNION *FN(UNION,match_bin_op)(__isl_take UNION *u1,
568 __isl_take UNION *u2,
569 __isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *))
571 S(UNION,match_bin_data) data = { NULL, NULL, fn };
573 u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
574 u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
576 if (!u1 || !u2)
577 goto error;
579 data.u2 = u2;
580 data.res = FN(UNION,alloc_same_size)(u1);
581 if (FN(FN(UNION,foreach),PARTS)(u1,
582 &FN(UNION,match_bin_entry), &data) < 0)
583 goto error;
585 FN(UNION,free)(u1);
586 FN(UNION,free)(u2);
587 return data.res;
588 error:
589 FN(UNION,free)(u1);
590 FN(UNION,free)(u2);
591 FN(UNION,free)(data.res);
592 return NULL;
595 /* Compute the sum of "u1" and "u2".
597 * If the base expressions have a default zero value, then the sum
598 * is computed on the union of the domains of "u1" and "u2".
599 * Otherwise, it is computed on their shared domains.
601 __isl_give UNION *FN(UNION,add)(__isl_take UNION *u1, __isl_take UNION *u2)
603 #if DEFAULT_IS_ZERO
604 return FN(UNION,union_add_)(u1, u2);
605 #else
606 return FN(UNION,match_bin_op)(u1, u2, &FN(PART,add));
607 #endif
610 #ifndef NO_SUB
611 /* Subtract "u2" from "u1" and return the result.
613 __isl_give UNION *FN(UNION,sub)(__isl_take UNION *u1, __isl_take UNION *u2)
615 return FN(UNION,match_bin_op)(u1, u2, &FN(PART,sub));
617 #endif
619 S(UNION,any_set_data) {
620 isl_set *set;
621 __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*);
624 static __isl_give PART *FN(UNION,any_set_entry)(__isl_take PART *part,
625 void *user)
627 S(UNION,any_set_data) *data = user;
629 return data->fn(part, isl_set_copy(data->set));
632 /* Update each element of "u" by calling "fn" on the element and "set".
634 static __isl_give UNION *FN(UNION,any_set_op)(__isl_take UNION *u,
635 __isl_take isl_set *set,
636 __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*))
638 S(UNION,any_set_data) data = { NULL, fn };
640 u = FN(UNION,align_params)(u, isl_set_get_space(set));
641 set = isl_set_align_params(set, FN(UNION,get_space)(u));
643 if (!u || !set)
644 goto error;
646 data.set = set;
647 u = FN(UNION,transform)(u, &FN(UNION,any_set_entry), &data);
648 isl_set_free(set);
649 return u;
650 error:
651 FN(UNION,free)(u);
652 isl_set_free(set);
653 return NULL;
656 /* Intersect the domain of "u" with the parameter domain "context".
658 __isl_give UNION *FN(UNION,intersect_params)(__isl_take UNION *u,
659 __isl_take isl_set *set)
661 return FN(UNION,any_set_op)(u, set, &FN(PW,intersect_params));
664 /* Compute the gist of the domain of "u" with respect to
665 * the parameter domain "context".
667 __isl_give UNION *FN(UNION,gist_params)(__isl_take UNION *u,
668 __isl_take isl_set *set)
670 return FN(UNION,any_set_op)(u, set, &FN(PW,gist_params));
673 S(UNION,match_domain_data) {
674 isl_union_set *uset;
675 UNION *res;
676 __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*);
679 static int FN(UNION,set_has_dim)(const void *entry, const void *val)
681 isl_set *set = (isl_set *)entry;
682 isl_space *dim = (isl_space *)val;
684 return isl_space_is_equal(set->dim, dim);
687 /* Find the set in data->uset that lives in the same space as the domain
688 * of "part", apply data->fn to *entry and this set (if any), and add
689 * the result to data->res.
691 static isl_stat FN(UNION,match_domain_entry)(__isl_take PART *part, void *user)
693 S(UNION,match_domain_data) *data = user;
694 uint32_t hash;
695 struct isl_hash_table_entry *entry2;
696 isl_space *space;
698 space = FN(PART,get_domain_space)(part);
699 hash = isl_space_get_hash(space);
700 entry2 = isl_hash_table_find(data->uset->dim->ctx, &data->uset->table,
701 hash, &FN(UNION,set_has_dim), space, 0);
702 isl_space_free(space);
703 if (!entry2) {
704 FN(PART,free)(part);
705 return isl_stat_ok;
708 part = data->fn(part, isl_set_copy(entry2->data));
710 data->res = FN(FN(UNION,add),PARTS)(data->res, part);
711 if (!data->res)
712 return isl_stat_error;
714 return isl_stat_ok;
717 /* Apply fn to each pair of PW in u and set in uset such that
718 * the set lives in the same space as the domain of PW
719 * and collect the results.
721 static __isl_give UNION *FN(UNION,match_domain_op)(__isl_take UNION *u,
722 __isl_take isl_union_set *uset,
723 __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*))
725 S(UNION,match_domain_data) data = { NULL, NULL, fn };
727 u = FN(UNION,align_params)(u, isl_union_set_get_space(uset));
728 uset = isl_union_set_align_params(uset, FN(UNION,get_space)(u));
730 if (!u || !uset)
731 goto error;
733 data.uset = uset;
734 data.res = FN(UNION,alloc_same_size)(u);
735 if (FN(FN(UNION,foreach),PARTS)(u,
736 &FN(UNION,match_domain_entry), &data) < 0)
737 goto error;
739 FN(UNION,free)(u);
740 isl_union_set_free(uset);
741 return data.res;
742 error:
743 FN(UNION,free)(u);
744 isl_union_set_free(uset);
745 FN(UNION,free)(data.res);
746 return NULL;
749 /* Intersect the domain of "u" with "uset".
750 * If "uset" is a parameters domain, then intersect the parameter
751 * domain of "u" with this set.
753 __isl_give UNION *FN(UNION,intersect_domain)(__isl_take UNION *u,
754 __isl_take isl_union_set *uset)
756 if (isl_union_set_is_params(uset))
757 return FN(UNION,intersect_params)(u,
758 isl_set_from_union_set(uset));
759 return FN(UNION,match_domain_op)(u, uset, &FN(PW,intersect_domain));
762 /* Take the set (which may be empty) in data->uset that lives
763 * in the same space as the domain of "pw", subtract it from the domain
764 * of "part" and return the result.
766 static __isl_give PART *FN(UNION,subtract_domain_entry)(__isl_take PART *part,
767 void *user)
769 isl_union_set *uset = user;
770 isl_space *space;
771 isl_set *set;
773 space = FN(PART,get_domain_space)(part);
774 set = isl_union_set_extract_set(uset, space);
775 return FN(PART,subtract_domain)(part, set);
778 /* Subtract "uset' from the domain of "u".
780 __isl_give UNION *FN(UNION,subtract_domain)(__isl_take UNION *u,
781 __isl_take isl_union_set *uset)
783 u = FN(UNION,transform)(u, &FN(UNION,subtract_domain_entry), uset);
784 isl_union_set_free(uset);
785 return u;
788 __isl_give UNION *FN(UNION,gist)(__isl_take UNION *u,
789 __isl_take isl_union_set *uset)
791 if (isl_union_set_is_params(uset))
792 return FN(UNION,gist_params)(u, isl_set_from_union_set(uset));
793 return FN(UNION,match_domain_op)(u, uset, &FN(PW,gist));
796 /* Coalesce an entry in a UNION. Coalescing is performed in-place.
797 * Since the UNION may have several references, the entry is only
798 * replaced if the coalescing is successful.
800 static isl_stat FN(UNION,coalesce_entry)(void **entry, void *user)
802 PART **part_p = (PART **) entry;
803 PART *part;
805 part = FN(PART,copy)(*part_p);
806 part = FN(PW,coalesce)(part);
807 if (!part)
808 return isl_stat_error;
809 FN(PART,free)(*part_p);
810 *part_p = part;
812 return isl_stat_ok;
815 __isl_give UNION *FN(UNION,coalesce)(__isl_take UNION *u)
817 if (FN(UNION,foreach_inplace)(u, &FN(UNION,coalesce_entry), NULL) < 0)
818 goto error;
820 return u;
821 error:
822 FN(UNION,free)(u);
823 return NULL;
826 static isl_stat FN(UNION,domain_entry)(__isl_take PART *part, void *user)
828 isl_union_set **uset = (isl_union_set **)user;
830 *uset = isl_union_set_add_set(*uset, FN(PART,domain)(part));
832 return isl_stat_ok;
835 __isl_give isl_union_set *FN(UNION,domain)(__isl_take UNION *u)
837 isl_union_set *uset;
839 uset = isl_union_set_empty(FN(UNION,get_space)(u));
840 if (FN(FN(UNION,foreach),PARTS)(u, &FN(UNION,domain_entry), &uset) < 0)
841 goto error;
843 FN(UNION,free)(u);
845 return uset;
846 error:
847 isl_union_set_free(uset);
848 FN(UNION,free)(u);
849 return NULL;
852 #ifdef HAS_TYPE
853 /* Negate the type of "u".
855 static __isl_give UNION *FN(UNION,negate_type)(__isl_take UNION *u)
857 u = FN(UNION,cow)(u);
858 if (!u)
859 return NULL;
860 u->type = isl_fold_type_negate(u->type);
861 return u;
863 #else
864 /* Negate the type of "u".
865 * Since "u" does not have a type, do nothing.
867 static __isl_give UNION *FN(UNION,negate_type)(__isl_take UNION *u)
869 return u;
871 #endif
873 /* Multiply "part" by the isl_val "user" and return the result.
875 static __isl_give PART *FN(UNION,scale_val_entry)(__isl_take PART *part,
876 void *user)
878 isl_val *v = user;
880 return FN(PART,scale_val)(part, isl_val_copy(v));
883 /* Multiply "u" by "v" and return the result.
885 __isl_give UNION *FN(UNION,scale_val)(__isl_take UNION *u,
886 __isl_take isl_val *v)
888 if (!u || !v)
889 goto error;
890 if (isl_val_is_one(v)) {
891 isl_val_free(v);
892 return u;
895 if (DEFAULT_IS_ZERO && u && isl_val_is_zero(v)) {
896 UNION *zero;
897 isl_space *space = FN(UNION,get_space)(u);
898 #ifdef HAS_TYPE
899 zero = FN(UNION,ZERO)(space, u->type);
900 #else
901 zero = FN(UNION,ZERO)(space);
902 #endif
903 FN(UNION,free)(u);
904 isl_val_free(v);
905 return zero;
908 if (!isl_val_is_rat(v))
909 isl_die(isl_val_get_ctx(v), isl_error_invalid,
910 "expecting rational factor", goto error);
912 u = FN(UNION,transform_inplace)(u, &FN(UNION,scale_val_entry), v);
913 if (isl_val_is_neg(v))
914 u = FN(UNION,negate_type)(u);
916 isl_val_free(v);
917 return u;
918 error:
919 isl_val_free(v);
920 FN(UNION,free)(u);
921 return NULL;
924 /* Divide "part" by the isl_val "user" and return the result.
926 static __isl_give PART *FN(UNION,scale_down_val_entry)(__isl_take PART *part,
927 void *user)
929 isl_val *v = user;
931 return FN(PART,scale_down_val)(part, isl_val_copy(v));
934 /* Divide "u" by "v" and return the result.
936 __isl_give UNION *FN(UNION,scale_down_val)(__isl_take UNION *u,
937 __isl_take isl_val *v)
939 if (!u || !v)
940 goto error;
941 if (isl_val_is_one(v)) {
942 isl_val_free(v);
943 return u;
946 if (!isl_val_is_rat(v))
947 isl_die(isl_val_get_ctx(v), isl_error_invalid,
948 "expecting rational factor", goto error);
949 if (isl_val_is_zero(v))
950 isl_die(isl_val_get_ctx(v), isl_error_invalid,
951 "cannot scale down by zero", goto error);
953 u = FN(UNION,transform_inplace)(u, &FN(UNION,scale_down_val_entry), v);
954 if (isl_val_is_neg(v))
955 u = FN(UNION,negate_type)(u);
957 isl_val_free(v);
958 return u;
959 error:
960 isl_val_free(v);
961 FN(UNION,free)(u);
962 return NULL;
965 S(UNION,plain_is_equal_data)
967 UNION *u2;
968 isl_bool is_equal;
971 static isl_stat FN(UNION,plain_is_equal_entry)(void **entry, void *user)
973 S(UNION,plain_is_equal_data) *data = user;
974 struct isl_hash_table_entry *entry2;
975 PW *pw = *entry;
977 entry2 = FN(UNION,find_part_entry)(data->u2, pw->dim, 0);
978 if (!entry2 || entry2 == isl_hash_table_entry_none) {
979 if (!entry2)
980 data->is_equal = isl_bool_error;
981 else
982 data->is_equal = isl_bool_false;
983 return isl_stat_error;
986 data->is_equal = FN(PW,plain_is_equal)(pw, entry2->data);
987 if (data->is_equal < 0 || !data->is_equal)
988 return isl_stat_error;
990 return isl_stat_ok;
993 isl_bool FN(UNION,plain_is_equal)(__isl_keep UNION *u1, __isl_keep UNION *u2)
995 S(UNION,plain_is_equal_data) data = { NULL, isl_bool_true };
996 int n1, n2;
998 if (!u1 || !u2)
999 return isl_bool_error;
1000 if (u1 == u2)
1001 return isl_bool_true;
1002 if (u1->table.n != u2->table.n)
1003 return isl_bool_false;
1004 n1 = FN(FN(UNION,n),PARTS)(u1);
1005 n2 = FN(FN(UNION,n),PARTS)(u2);
1006 if (n1 < 0 || n2 < 0)
1007 return isl_bool_error;
1008 if (n1 != n2)
1009 return isl_bool_false;
1011 u1 = FN(UNION,copy)(u1);
1012 u2 = FN(UNION,copy)(u2);
1013 u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
1014 u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
1015 if (!u1 || !u2)
1016 goto error;
1018 data.u2 = u2;
1019 if (FN(UNION,foreach_inplace)(u1,
1020 &FN(UNION,plain_is_equal_entry), &data) < 0 &&
1021 data.is_equal)
1022 goto error;
1024 FN(UNION,free)(u1);
1025 FN(UNION,free)(u2);
1027 return data.is_equal;
1028 error:
1029 FN(UNION,free)(u1);
1030 FN(UNION,free)(u2);
1031 return isl_bool_error;
1034 /* Check whether the element that "entry" points to involves any NaNs and
1035 * store the result in *nan.
1036 * Abort as soon as one such element has been found.
1038 static isl_stat FN(UNION,involves_nan_entry)(void **entry, void *user)
1040 isl_bool *nan = user;
1041 PW *pw = *entry;
1043 *nan = FN(PW,involves_nan)(pw);
1044 if (*nan < 0 || !nan)
1045 return isl_stat_error;
1047 return isl_stat_ok;
1050 /* Does "u" involve any NaNs?
1052 isl_bool FN(UNION,involves_nan)(__isl_keep UNION *u)
1054 isl_bool nan = isl_bool_false;
1056 if (!u)
1057 return isl_bool_error;
1059 if (FN(UNION,foreach_inplace)(u,
1060 &FN(UNION,involves_nan_entry), &nan) < 0 &&
1061 !nan)
1062 return isl_bool_error;
1064 return nan;
1067 /* Internal data structure for isl_union_*_drop_dims.
1068 * type, first and n are passed to isl_*_drop_dims.
1070 S(UNION,drop_dims_data) {
1071 enum isl_dim_type type;
1072 unsigned first;
1073 unsigned n;
1076 /* Drop the parameters specified by "data" from "part" and return the result.
1078 static __isl_give PART *FN(UNION,drop_dims_entry)(__isl_take PART *part,
1079 void *user)
1081 S(UNION,drop_dims_data) *data = user;
1083 return FN(PART,drop_dims)(part, data->type, data->first, data->n);
1086 /* Drop the specified parameters from "u".
1087 * That is, type is required to be isl_dim_param.
1089 __isl_give UNION *FN(UNION,drop_dims)( __isl_take UNION *u,
1090 enum isl_dim_type type, unsigned first, unsigned n)
1092 isl_space *space;
1093 S(UNION,drop_dims_data) data = { type, first, n };
1095 if (!u)
1096 return NULL;
1098 if (type != isl_dim_param)
1099 isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
1100 "can only project out parameters",
1101 return FN(UNION,free)(u));
1103 space = FN(UNION,get_space)(u);
1104 space = isl_space_drop_dims(space, type, first, n);
1105 return FN(UNION,transform_space)(u, space, &FN(UNION,drop_dims_entry),
1106 &data);
1109 /* Internal data structure for isl_union_*_set_dim_name.
1110 * pos is the position of the parameter that needs to be renamed.
1111 * s is the new name.
1113 S(UNION,set_dim_name_data) {
1114 unsigned pos;
1115 const char *s;
1118 /* Change the name of the parameter at position data->pos of "part" to data->s
1119 * and return the result.
1121 static __isl_give PART *FN(UNION,set_dim_name_entry)(__isl_take PART *part,
1122 void *user)
1124 S(UNION,set_dim_name_data) *data = user;
1126 return FN(PART,set_dim_name)(part, isl_dim_param, data->pos, data->s);
1129 /* Change the name of the parameter at position "pos" to "s".
1130 * That is, type is required to be isl_dim_param.
1132 __isl_give UNION *FN(UNION,set_dim_name)(__isl_take UNION *u,
1133 enum isl_dim_type type, unsigned pos, const char *s)
1135 S(UNION,set_dim_name_data) data = { pos, s };
1136 isl_space *space;
1138 if (!u)
1139 return NULL;
1141 if (type != isl_dim_param)
1142 isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
1143 "can only set parameter names",
1144 return FN(UNION,free)(u));
1146 space = FN(UNION,get_space)(u);
1147 space = isl_space_set_dim_name(space, type, pos, s);
1148 return FN(UNION,transform_space)(u, space,
1149 &FN(UNION,set_dim_name_entry), &data);
1152 /* Reset the user pointer on all identifiers of parameters and tuples
1153 * of the space of "part" and return the result.
1155 static __isl_give PART *FN(UNION,reset_user_entry)(__isl_take PART *part,
1156 void *user)
1158 return FN(PART,reset_user)(part);
1161 /* Reset the user pointer on all identifiers of parameters and tuples
1162 * of the spaces of "u".
1164 __isl_give UNION *FN(UNION,reset_user)(__isl_take UNION *u)
1166 isl_space *space;
1168 space = FN(UNION,get_space)(u);
1169 space = isl_space_reset_user(space);
1170 return FN(UNION,transform_space)(u, space, &FN(UNION,reset_user_entry),
1171 NULL);