document *lower_bound_si functions
[isl.git] / isl_union_map.c
blob445d4febee53d38bf37349038ab8893de214a9b5
1 /*
2 * Copyright 2010-2011 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
11 #define ISL_DIM_H
12 #include <isl_map_private.h>
13 #include <isl/ctx.h>
14 #include <isl/hash.h>
15 #include <isl/map.h>
16 #include <isl/set.h>
17 #include <isl_space_private.h>
18 #include <isl_union_map_private.h>
19 #include <isl/union_set.h>
21 /* Is this union set a parameter domain?
23 int isl_union_set_is_params(__isl_keep isl_union_set *uset)
25 isl_set *set;
26 int params;
28 if (!uset)
29 return -1;
30 if (uset->table.n != 1)
31 return 0;
33 set = isl_set_from_union_set(isl_union_set_copy(uset));
34 params = isl_set_is_params(set);
35 isl_set_free(set);
36 return params;
39 static __isl_give isl_union_map *isl_union_map_alloc(__isl_take isl_space *dim,
40 int size)
42 isl_union_map *umap;
44 if (!dim)
45 return NULL;
47 umap = isl_calloc_type(dim->ctx, isl_union_map);
48 if (!umap)
49 return NULL;
51 umap->ref = 1;
52 umap->dim = dim;
53 if (isl_hash_table_init(dim->ctx, &umap->table, size) < 0)
54 goto error;
56 return umap;
57 error:
58 isl_space_free(dim);
59 isl_union_map_free(umap);
60 return NULL;
63 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *dim)
65 return isl_union_map_alloc(dim, 16);
68 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *dim)
70 return isl_union_map_empty(dim);
73 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
75 return umap ? umap->dim->ctx : NULL;
78 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
80 return uset ? uset->dim->ctx : NULL;
83 __isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
85 if (!umap)
86 return NULL;
87 return isl_space_copy(umap->dim);
90 __isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
92 return isl_union_map_get_space(uset);
95 static int free_umap_entry(void **entry, void *user)
97 isl_map *map = *entry;
98 isl_map_free(map);
99 return 0;
102 static int add_map(__isl_take isl_map *map, void *user)
104 isl_union_map **umap = (isl_union_map **)user;
106 *umap = isl_union_map_add_map(*umap, map);
108 return 0;
111 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
113 isl_union_map *dup;
115 if (!umap)
116 return NULL;
118 dup = isl_union_map_empty(isl_space_copy(umap->dim));
119 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
120 goto error;
121 return dup;
122 error:
123 isl_union_map_free(dup);
124 return NULL;
127 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
129 if (!umap)
130 return NULL;
132 if (umap->ref == 1)
133 return umap;
134 umap->ref--;
135 return isl_union_map_dup(umap);
138 struct isl_union_align {
139 isl_reordering *exp;
140 isl_union_map *res;
143 static int align_entry(void **entry, void *user)
145 isl_map *map = *entry;
146 isl_reordering *exp;
147 struct isl_union_align *data = user;
149 exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
150 isl_map_get_space(map));
152 data->res = isl_union_map_add_map(data->res,
153 isl_map_realign(isl_map_copy(map), exp));
155 return 0;
158 /* Align the parameters of umap along those of model.
159 * The result has the parameters of model first, in the same order
160 * as they appear in model, followed by any remaining parameters of
161 * umap that do not appear in model.
163 __isl_give isl_union_map *isl_union_map_align_params(
164 __isl_take isl_union_map *umap, __isl_take isl_space *model)
166 struct isl_union_align data = { NULL, NULL };
168 if (!umap || !model)
169 goto error;
171 if (isl_space_match(umap->dim, isl_dim_param, model, isl_dim_param)) {
172 isl_space_free(model);
173 return umap;
176 model = isl_space_params(model);
177 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
178 if (!data.exp)
179 goto error;
181 data.res = isl_union_map_alloc(isl_space_copy(data.exp->dim),
182 umap->table.n);
183 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
184 &align_entry, &data) < 0)
185 goto error;
187 isl_reordering_free(data.exp);
188 isl_union_map_free(umap);
189 isl_space_free(model);
190 return data.res;
191 error:
192 isl_reordering_free(data.exp);
193 isl_union_map_free(umap);
194 isl_union_map_free(data.res);
195 isl_space_free(model);
196 return NULL;
199 __isl_give isl_union_set *isl_union_set_align_params(
200 __isl_take isl_union_set *uset, __isl_take isl_space *model)
202 return isl_union_map_align_params(uset, model);
205 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
206 __isl_take isl_union_map *umap2)
208 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
209 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
211 umap1 = isl_union_map_cow(umap1);
213 if (!umap1 || !umap2)
214 goto error;
216 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
217 goto error;
219 isl_union_map_free(umap2);
221 return umap1;
222 error:
223 isl_union_map_free(umap1);
224 isl_union_map_free(umap2);
225 return NULL;
228 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
229 __isl_take isl_union_set *uset2)
231 return isl_union_map_union(uset1, uset2);
234 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
236 if (!umap)
237 return NULL;
239 umap->ref++;
240 return umap;
243 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
245 return isl_union_map_copy(uset);
248 void *isl_union_map_free(__isl_take isl_union_map *umap)
250 if (!umap)
251 return NULL;
253 if (--umap->ref > 0)
254 return NULL;
256 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
257 &free_umap_entry, NULL);
258 isl_hash_table_clear(&umap->table);
259 isl_space_free(umap->dim);
260 free(umap);
261 return NULL;
264 void *isl_union_set_free(__isl_take isl_union_set *uset)
266 return isl_union_map_free(uset);
269 static int has_dim(const void *entry, const void *val)
271 isl_map *map = (isl_map *)entry;
272 isl_space *dim = (isl_space *)val;
274 return isl_space_is_equal(map->dim, dim);
277 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
278 __isl_take isl_map *map)
280 uint32_t hash;
281 struct isl_hash_table_entry *entry;
283 if (!map || !umap)
284 goto error;
286 if (isl_map_plain_is_empty(map)) {
287 isl_map_free(map);
288 return umap;
291 if (!isl_space_match(map->dim, isl_dim_param, umap->dim, isl_dim_param)) {
292 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
293 map = isl_map_align_params(map, isl_union_map_get_space(umap));
296 umap = isl_union_map_cow(umap);
298 if (!map || !umap)
299 goto error;
301 hash = isl_space_get_hash(map->dim);
302 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
303 &has_dim, map->dim, 1);
304 if (!entry)
305 goto error;
307 if (!entry->data)
308 entry->data = map;
309 else {
310 entry->data = isl_map_union(entry->data, isl_map_copy(map));
311 if (!entry->data)
312 goto error;
313 isl_map_free(map);
316 return umap;
317 error:
318 isl_map_free(map);
319 isl_union_map_free(umap);
320 return NULL;
323 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
324 __isl_take isl_set *set)
326 return isl_union_map_add_map(uset, (isl_map *)set);
329 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
331 isl_space *dim;
332 isl_union_map *umap;
334 if (!map)
335 return NULL;
337 dim = isl_map_get_space(map);
338 dim = isl_space_params(dim);
339 umap = isl_union_map_empty(dim);
340 umap = isl_union_map_add_map(umap, map);
342 return umap;
345 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
347 return isl_union_map_from_map((isl_map *)set);
350 struct isl_union_map_foreach_data
352 int (*fn)(__isl_take isl_map *map, void *user);
353 void *user;
356 static int call_on_copy(void **entry, void *user)
358 isl_map *map = *entry;
359 struct isl_union_map_foreach_data *data;
360 data = (struct isl_union_map_foreach_data *)user;
362 return data->fn(isl_map_copy(map), data->user);
365 int isl_union_map_n_map(__isl_keep isl_union_map *umap)
367 return umap ? umap->table.n : 0;
370 int isl_union_set_n_set(__isl_keep isl_union_set *uset)
372 return uset ? uset->table.n : 0;
375 int isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
376 int (*fn)(__isl_take isl_map *map, void *user), void *user)
378 struct isl_union_map_foreach_data data = { fn, user };
380 if (!umap)
381 return -1;
383 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
384 &call_on_copy, &data);
387 static int copy_map(void **entry, void *user)
389 isl_map *map = *entry;
390 isl_map **map_p = user;
392 *map_p = isl_map_copy(map);
394 return -1;
397 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
399 isl_ctx *ctx;
400 isl_map *map = NULL;
402 if (!umap)
403 return NULL;
404 ctx = isl_union_map_get_ctx(umap);
405 if (umap->table.n != 1)
406 isl_die(ctx, isl_error_invalid,
407 "union map needs to contain elements in exactly "
408 "one space", return isl_union_map_free(umap));
410 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
412 isl_union_map_free(umap);
414 return map;
417 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
419 return isl_map_from_union_map(uset);
422 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
423 __isl_take isl_space *dim)
425 uint32_t hash;
426 struct isl_hash_table_entry *entry;
428 if (!umap || !dim)
429 goto error;
431 hash = isl_space_get_hash(dim);
432 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
433 &has_dim, dim, 0);
434 if (!entry)
435 return isl_map_empty(dim);
436 isl_space_free(dim);
437 return isl_map_copy(entry->data);
438 error:
439 isl_space_free(dim);
440 return NULL;
443 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
444 __isl_take isl_space *dim)
446 return (isl_set *)isl_union_map_extract_map(uset, dim);
449 /* Check if umap contains a map in the given space.
451 __isl_give int isl_union_map_contains(__isl_keep isl_union_map *umap,
452 __isl_keep isl_space *dim)
454 uint32_t hash;
455 struct isl_hash_table_entry *entry;
457 if (!umap || !dim)
458 return -1;
460 hash = isl_space_get_hash(dim);
461 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
462 &has_dim, dim, 0);
463 return !!entry;
466 __isl_give int isl_union_set_contains(__isl_keep isl_union_set *uset,
467 __isl_keep isl_space *dim)
469 return isl_union_map_contains(uset, dim);
472 int isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
473 int (*fn)(__isl_take isl_set *set, void *user), void *user)
475 return isl_union_map_foreach_map(uset,
476 (int(*)(__isl_take isl_map *, void*))fn, user);
479 struct isl_union_set_foreach_point_data {
480 int (*fn)(__isl_take isl_point *pnt, void *user);
481 void *user;
484 static int foreach_point(__isl_take isl_set *set, void *user)
486 struct isl_union_set_foreach_point_data *data = user;
487 int r;
489 r = isl_set_foreach_point(set, data->fn, data->user);
490 isl_set_free(set);
492 return r;
495 int isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
496 int (*fn)(__isl_take isl_point *pnt, void *user), void *user)
498 struct isl_union_set_foreach_point_data data = { fn, user };
499 return isl_union_set_foreach_set(uset, &foreach_point, &data);
502 struct isl_union_map_gen_bin_data {
503 isl_union_map *umap2;
504 isl_union_map *res;
507 static int subtract_entry(void **entry, void *user)
509 struct isl_union_map_gen_bin_data *data = user;
510 uint32_t hash;
511 struct isl_hash_table_entry *entry2;
512 isl_map *map = *entry;
514 hash = isl_space_get_hash(map->dim);
515 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
516 hash, &has_dim, map->dim, 0);
517 map = isl_map_copy(map);
518 if (entry2) {
519 int empty;
520 map = isl_map_subtract(map, isl_map_copy(entry2->data));
522 empty = isl_map_is_empty(map);
523 if (empty < 0) {
524 isl_map_free(map);
525 return -1;
527 if (empty) {
528 isl_map_free(map);
529 return 0;
532 data->res = isl_union_map_add_map(data->res, map);
534 return 0;
537 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
538 __isl_take isl_union_map *umap2, int (*fn)(void **, void *))
540 struct isl_union_map_gen_bin_data data = { NULL, NULL };
542 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
543 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
545 if (!umap1 || !umap2)
546 goto error;
548 data.umap2 = umap2;
549 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
550 umap1->table.n);
551 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
552 fn, &data) < 0)
553 goto error;
555 isl_union_map_free(umap1);
556 isl_union_map_free(umap2);
557 return data.res;
558 error:
559 isl_union_map_free(umap1);
560 isl_union_map_free(umap2);
561 isl_union_map_free(data.res);
562 return NULL;
565 __isl_give isl_union_map *isl_union_map_subtract(
566 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
568 return gen_bin_op(umap1, umap2, &subtract_entry);
571 __isl_give isl_union_set *isl_union_set_subtract(
572 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
574 return isl_union_map_subtract(uset1, uset2);
577 struct isl_union_map_gen_bin_set_data {
578 isl_set *set;
579 isl_union_map *res;
582 static int intersect_params_entry(void **entry, void *user)
584 struct isl_union_map_gen_bin_set_data *data = user;
585 isl_map *map = *entry;
586 int empty;
588 map = isl_map_copy(map);
589 map = isl_map_intersect_params(map, isl_set_copy(data->set));
591 empty = isl_map_is_empty(map);
592 if (empty < 0) {
593 isl_map_free(map);
594 return -1;
597 data->res = isl_union_map_add_map(data->res, map);
599 return 0;
602 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
603 __isl_take isl_set *set, int (*fn)(void **, void *))
605 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
607 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
608 set = isl_set_align_params(set, isl_union_map_get_space(umap));
610 if (!umap || !set)
611 goto error;
613 data.set = set;
614 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
615 umap->table.n);
616 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
617 fn, &data) < 0)
618 goto error;
620 isl_union_map_free(umap);
621 isl_set_free(set);
622 return data.res;
623 error:
624 isl_union_map_free(umap);
625 isl_set_free(set);
626 isl_union_map_free(data.res);
627 return NULL;
630 __isl_give isl_union_map *isl_union_map_intersect_params(
631 __isl_take isl_union_map *umap, __isl_take isl_set *set)
633 return gen_bin_set_op(umap, set, &intersect_params_entry);
636 __isl_give isl_union_set *isl_union_set_intersect_params(
637 __isl_take isl_union_set *uset, __isl_take isl_set *set)
639 return isl_union_map_intersect_params(uset, set);
642 static __isl_give isl_union_map *union_map_intersect_params(
643 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
645 return isl_union_map_intersect_params(umap,
646 isl_set_from_union_set(uset));
649 static __isl_give isl_union_map *union_map_gist_params(
650 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
652 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
655 struct isl_union_map_match_bin_data {
656 isl_union_map *umap2;
657 isl_union_map *res;
658 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
661 static int match_bin_entry(void **entry, void *user)
663 struct isl_union_map_match_bin_data *data = user;
664 uint32_t hash;
665 struct isl_hash_table_entry *entry2;
666 isl_map *map = *entry;
667 int empty;
669 hash = isl_space_get_hash(map->dim);
670 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
671 hash, &has_dim, map->dim, 0);
672 if (!entry2)
673 return 0;
675 map = isl_map_copy(map);
676 map = data->fn(map, isl_map_copy(entry2->data));
678 empty = isl_map_is_empty(map);
679 if (empty < 0) {
680 isl_map_free(map);
681 return -1;
683 if (empty) {
684 isl_map_free(map);
685 return 0;
688 data->res = isl_union_map_add_map(data->res, map);
690 return 0;
693 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
694 __isl_take isl_union_map *umap2,
695 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
697 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
699 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
700 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
702 if (!umap1 || !umap2)
703 goto error;
705 data.umap2 = umap2;
706 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
707 umap1->table.n);
708 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
709 &match_bin_entry, &data) < 0)
710 goto error;
712 isl_union_map_free(umap1);
713 isl_union_map_free(umap2);
714 return data.res;
715 error:
716 isl_union_map_free(umap1);
717 isl_union_map_free(umap2);
718 isl_union_map_free(data.res);
719 return NULL;
722 __isl_give isl_union_map *isl_union_map_intersect(
723 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
725 return match_bin_op(umap1, umap2, &isl_map_intersect);
728 /* Compute the intersection of the two union_sets.
729 * As a special case, if exactly one of the two union_sets
730 * is a parameter domain, then intersect the parameter domain
731 * of the other one with this set.
733 __isl_give isl_union_set *isl_union_set_intersect(
734 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
736 int p1, p2;
738 p1 = isl_union_set_is_params(uset1);
739 p2 = isl_union_set_is_params(uset2);
740 if (p1 < 0 || p2 < 0)
741 goto error;
742 if (!p1 && p2)
743 return union_map_intersect_params(uset1, uset2);
744 if (p1 && !p2)
745 return union_map_intersect_params(uset2, uset1);
746 return isl_union_map_intersect(uset1, uset2);
747 error:
748 isl_union_set_free(uset1);
749 isl_union_set_free(uset2);
750 return NULL;
753 static int gist_params_entry(void **entry, void *user)
755 struct isl_union_map_gen_bin_set_data *data = user;
756 isl_map *map = *entry;
757 int empty;
759 map = isl_map_copy(map);
760 map = isl_map_gist_params(map, isl_set_copy(data->set));
762 empty = isl_map_is_empty(map);
763 if (empty < 0) {
764 isl_map_free(map);
765 return -1;
768 data->res = isl_union_map_add_map(data->res, map);
770 return 0;
773 __isl_give isl_union_map *isl_union_map_gist_params(
774 __isl_take isl_union_map *umap, __isl_take isl_set *set)
776 return gen_bin_set_op(umap, set, &gist_params_entry);
779 __isl_give isl_union_set *isl_union_set_gist_params(
780 __isl_take isl_union_set *uset, __isl_take isl_set *set)
782 return isl_union_map_gist_params(uset, set);
785 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
786 __isl_take isl_union_map *context)
788 return match_bin_op(umap, context, &isl_map_gist);
791 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
792 __isl_take isl_union_set *context)
794 if (isl_union_set_is_params(context))
795 return union_map_gist_params(uset, context);
796 return isl_union_map_gist(uset, context);
799 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
800 __isl_take isl_map *set2)
802 return isl_set_lex_le_set((isl_set *)set1, (isl_set *)set2);
805 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
806 __isl_take isl_map *set2)
808 return isl_set_lex_lt_set((isl_set *)set1, (isl_set *)set2);
811 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
812 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
814 return match_bin_op(uset1, uset2, &lex_lt_set);
817 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
818 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
820 return match_bin_op(uset1, uset2, &lex_le_set);
823 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
824 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
826 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
829 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
830 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
832 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
835 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
836 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
838 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
841 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
842 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
844 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
847 static int intersect_domain_entry(void **entry, void *user)
849 struct isl_union_map_gen_bin_data *data = user;
850 uint32_t hash;
851 struct isl_hash_table_entry *entry2;
852 isl_space *dim;
853 isl_map *map = *entry;
854 int empty;
856 dim = isl_map_get_space(map);
857 dim = isl_space_domain(dim);
858 hash = isl_space_get_hash(dim);
859 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
860 hash, &has_dim, dim, 0);
861 isl_space_free(dim);
862 if (!entry2)
863 return 0;
865 map = isl_map_copy(map);
866 map = isl_map_intersect_domain(map, isl_set_copy(entry2->data));
868 empty = isl_map_is_empty(map);
869 if (empty < 0) {
870 isl_map_free(map);
871 return -1;
873 if (empty) {
874 isl_map_free(map);
875 return 0;
878 data->res = isl_union_map_add_map(data->res, map);
880 return 0;
883 /* Intersect the domain of "umap" with "uset".
884 * If "uset" is a parameters domain, then intersect the parameter
885 * domain of "umap" with this set.
887 __isl_give isl_union_map *isl_union_map_intersect_domain(
888 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
890 if (isl_union_set_is_params(uset))
891 return union_map_intersect_params(umap, uset);
892 return gen_bin_op(umap, uset, &intersect_domain_entry);
895 static int gist_domain_entry(void **entry, void *user)
897 struct isl_union_map_gen_bin_data *data = user;
898 uint32_t hash;
899 struct isl_hash_table_entry *entry2;
900 isl_space *dim;
901 isl_map *map = *entry;
902 int empty;
904 dim = isl_map_get_space(map);
905 dim = isl_space_domain(dim);
906 hash = isl_space_get_hash(dim);
907 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
908 hash, &has_dim, dim, 0);
909 isl_space_free(dim);
910 if (!entry2)
911 return 0;
913 map = isl_map_copy(map);
914 map = isl_map_gist_domain(map, isl_set_copy(entry2->data));
916 empty = isl_map_is_empty(map);
917 if (empty < 0) {
918 isl_map_free(map);
919 return -1;
922 data->res = isl_union_map_add_map(data->res, map);
924 return 0;
927 /* Compute the gist of "umap" with respect to the domain "uset".
928 * If "uset" is a parameters domain, then compute the gist
929 * with respect to this parameter domain.
931 __isl_give isl_union_map *isl_union_map_gist_domain(
932 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
934 if (isl_union_set_is_params(uset))
935 return union_map_gist_params(umap, uset);
936 return gen_bin_op(umap, uset, &gist_domain_entry);
939 static int intersect_range_entry(void **entry, void *user)
941 struct isl_union_map_gen_bin_data *data = user;
942 uint32_t hash;
943 struct isl_hash_table_entry *entry2;
944 isl_space *dim;
945 isl_map *map = *entry;
946 int empty;
948 dim = isl_map_get_space(map);
949 dim = isl_space_range(dim);
950 hash = isl_space_get_hash(dim);
951 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
952 hash, &has_dim, dim, 0);
953 isl_space_free(dim);
954 if (!entry2)
955 return 0;
957 map = isl_map_copy(map);
958 map = isl_map_intersect_range(map, isl_set_copy(entry2->data));
960 empty = isl_map_is_empty(map);
961 if (empty < 0) {
962 isl_map_free(map);
963 return -1;
965 if (empty) {
966 isl_map_free(map);
967 return 0;
970 data->res = isl_union_map_add_map(data->res, map);
972 return 0;
975 __isl_give isl_union_map *isl_union_map_intersect_range(
976 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
978 return gen_bin_op(umap, uset, &intersect_range_entry);
981 struct isl_union_map_bin_data {
982 isl_union_map *umap2;
983 isl_union_map *res;
984 isl_map *map;
985 int (*fn)(void **entry, void *user);
988 static int apply_range_entry(void **entry, void *user)
990 struct isl_union_map_bin_data *data = user;
991 isl_map *map2 = *entry;
992 int empty;
994 if (!isl_space_tuple_match(data->map->dim, isl_dim_out,
995 map2->dim, isl_dim_in))
996 return 0;
998 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1000 empty = isl_map_is_empty(map2);
1001 if (empty < 0) {
1002 isl_map_free(map2);
1003 return -1;
1005 if (empty) {
1006 isl_map_free(map2);
1007 return 0;
1010 data->res = isl_union_map_add_map(data->res, map2);
1012 return 0;
1015 static int bin_entry(void **entry, void *user)
1017 struct isl_union_map_bin_data *data = user;
1018 isl_map *map = *entry;
1020 data->map = map;
1021 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1022 data->fn, data) < 0)
1023 return -1;
1025 return 0;
1028 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1029 __isl_take isl_union_map *umap2, int (*fn)(void **entry, void *user))
1031 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1033 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1034 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1036 if (!umap1 || !umap2)
1037 goto error;
1039 data.umap2 = umap2;
1040 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1041 umap1->table.n);
1042 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1043 &bin_entry, &data) < 0)
1044 goto error;
1046 isl_union_map_free(umap1);
1047 isl_union_map_free(umap2);
1048 return data.res;
1049 error:
1050 isl_union_map_free(umap1);
1051 isl_union_map_free(umap2);
1052 isl_union_map_free(data.res);
1053 return NULL;
1056 __isl_give isl_union_map *isl_union_map_apply_range(
1057 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1059 return bin_op(umap1, umap2, &apply_range_entry);
1062 __isl_give isl_union_map *isl_union_map_apply_domain(
1063 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1065 umap1 = isl_union_map_reverse(umap1);
1066 umap1 = isl_union_map_apply_range(umap1, umap2);
1067 return isl_union_map_reverse(umap1);
1070 __isl_give isl_union_set *isl_union_set_apply(
1071 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1073 return isl_union_map_apply_range(uset, umap);
1076 static int map_lex_lt_entry(void **entry, void *user)
1078 struct isl_union_map_bin_data *data = user;
1079 isl_map *map2 = *entry;
1081 if (!isl_space_tuple_match(data->map->dim, isl_dim_out,
1082 map2->dim, isl_dim_out))
1083 return 0;
1085 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1087 data->res = isl_union_map_add_map(data->res, map2);
1089 return 0;
1092 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1093 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1095 return bin_op(umap1, umap2, &map_lex_lt_entry);
1098 static int map_lex_le_entry(void **entry, void *user)
1100 struct isl_union_map_bin_data *data = user;
1101 isl_map *map2 = *entry;
1103 if (!isl_space_tuple_match(data->map->dim, isl_dim_out,
1104 map2->dim, isl_dim_out))
1105 return 0;
1107 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1109 data->res = isl_union_map_add_map(data->res, map2);
1111 return 0;
1114 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1115 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1117 return bin_op(umap1, umap2, &map_lex_le_entry);
1120 static int product_entry(void **entry, void *user)
1122 struct isl_union_map_bin_data *data = user;
1123 isl_map *map2 = *entry;
1125 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1127 data->res = isl_union_map_add_map(data->res, map2);
1129 return 0;
1132 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1133 __isl_take isl_union_map *umap2)
1135 return bin_op(umap1, umap2, &product_entry);
1138 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1139 __isl_take isl_union_set *uset2)
1141 return isl_union_map_product(uset1, uset2);
1144 static int range_product_entry(void **entry, void *user)
1146 struct isl_union_map_bin_data *data = user;
1147 isl_map *map2 = *entry;
1149 if (!isl_space_tuple_match(data->map->dim, isl_dim_in,
1150 map2->dim, isl_dim_in))
1151 return 0;
1153 map2 = isl_map_range_product(isl_map_copy(data->map),
1154 isl_map_copy(map2));
1156 data->res = isl_union_map_add_map(data->res, map2);
1158 return 0;
1161 __isl_give isl_union_map *isl_union_map_range_product(
1162 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1164 return bin_op(umap1, umap2, &range_product_entry);
1167 static int flat_range_product_entry(void **entry, void *user)
1169 struct isl_union_map_bin_data *data = user;
1170 isl_map *map2 = *entry;
1172 if (!isl_space_tuple_match(data->map->dim, isl_dim_in,
1173 map2->dim, isl_dim_in))
1174 return 0;
1176 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1177 isl_map_copy(map2));
1179 data->res = isl_union_map_add_map(data->res, map2);
1181 return 0;
1184 __isl_give isl_union_map *isl_union_map_flat_range_product(
1185 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1187 return bin_op(umap1, umap2, &flat_range_product_entry);
1190 static __isl_give isl_union_set *cond_un_op(__isl_take isl_union_map *umap,
1191 int (*fn)(void **, void *))
1193 isl_union_set *res;
1195 if (!umap)
1196 return NULL;
1198 res = isl_union_map_alloc(isl_space_copy(umap->dim), umap->table.n);
1199 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, &res) < 0)
1200 goto error;
1202 isl_union_map_free(umap);
1203 return res;
1204 error:
1205 isl_union_map_free(umap);
1206 isl_union_set_free(res);
1207 return NULL;
1210 static int from_range_entry(void **entry, void *user)
1212 isl_map *set = *entry;
1213 isl_union_set **res = user;
1215 *res = isl_union_map_add_map(*res,
1216 isl_map_from_range(isl_set_copy(set)));
1218 return 0;
1221 __isl_give isl_union_map *isl_union_map_from_range(
1222 __isl_take isl_union_set *uset)
1224 return cond_un_op(uset, &from_range_entry);
1227 __isl_give isl_union_map *isl_union_map_from_domain(
1228 __isl_take isl_union_set *uset)
1230 return isl_union_map_reverse(isl_union_map_from_range(uset));
1233 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1234 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1236 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1237 isl_union_map_from_range(range));
1240 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1241 int (*fn)(void **, void *))
1243 umap = isl_union_map_cow(umap);
1244 if (!umap)
1245 return NULL;
1247 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, NULL) < 0)
1248 goto error;
1250 return umap;
1251 error:
1252 isl_union_map_free(umap);
1253 return NULL;
1256 static int affine_entry(void **entry, void *user)
1258 isl_map **map = (isl_map **)entry;
1260 *map = isl_map_from_basic_map(isl_map_affine_hull(*map));
1262 return *map ? 0 : -1;
1265 __isl_give isl_union_map *isl_union_map_affine_hull(
1266 __isl_take isl_union_map *umap)
1268 return un_op(umap, &affine_entry);
1271 __isl_give isl_union_set *isl_union_set_affine_hull(
1272 __isl_take isl_union_set *uset)
1274 return isl_union_map_affine_hull(uset);
1277 static int polyhedral_entry(void **entry, void *user)
1279 isl_map **map = (isl_map **)entry;
1281 *map = isl_map_from_basic_map(isl_map_polyhedral_hull(*map));
1283 return *map ? 0 : -1;
1286 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1287 __isl_take isl_union_map *umap)
1289 return un_op(umap, &polyhedral_entry);
1292 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1293 __isl_take isl_union_set *uset)
1295 return isl_union_map_polyhedral_hull(uset);
1298 static int simple_entry(void **entry, void *user)
1300 isl_map **map = (isl_map **)entry;
1302 *map = isl_map_from_basic_map(isl_map_simple_hull(*map));
1304 return *map ? 0 : -1;
1307 __isl_give isl_union_map *isl_union_map_simple_hull(
1308 __isl_take isl_union_map *umap)
1310 return un_op(umap, &simple_entry);
1313 __isl_give isl_union_set *isl_union_set_simple_hull(
1314 __isl_take isl_union_set *uset)
1316 return isl_union_map_simple_hull(uset);
1319 static int inplace_entry(void **entry, void *user)
1321 __isl_give isl_map *(*fn)(__isl_take isl_map *);
1322 isl_map **map = (isl_map **)entry;
1323 isl_map *copy;
1325 fn = *(__isl_give isl_map *(**)(__isl_take isl_map *)) user;
1326 copy = fn(isl_map_copy(*map));
1327 if (!copy)
1328 return -1;
1330 isl_map_free(*map);
1331 *map = copy;
1333 return 0;
1336 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1337 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1339 if (!umap)
1340 return NULL;
1342 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1343 &inplace_entry, &fn) < 0)
1344 goto error;
1346 return umap;
1347 error:
1348 isl_union_map_free(umap);
1349 return NULL;
1352 __isl_give isl_union_map *isl_union_map_coalesce(
1353 __isl_take isl_union_map *umap)
1355 return inplace(umap, &isl_map_coalesce);
1358 __isl_give isl_union_set *isl_union_set_coalesce(
1359 __isl_take isl_union_set *uset)
1361 return isl_union_map_coalesce(uset);
1364 __isl_give isl_union_map *isl_union_map_detect_equalities(
1365 __isl_take isl_union_map *umap)
1367 return inplace(umap, &isl_map_detect_equalities);
1370 __isl_give isl_union_set *isl_union_set_detect_equalities(
1371 __isl_take isl_union_set *uset)
1373 return isl_union_map_detect_equalities(uset);
1376 __isl_give isl_union_map *isl_union_map_compute_divs(
1377 __isl_take isl_union_map *umap)
1379 return inplace(umap, &isl_map_compute_divs);
1382 __isl_give isl_union_set *isl_union_set_compute_divs(
1383 __isl_take isl_union_set *uset)
1385 return isl_union_map_compute_divs(uset);
1388 static int lexmin_entry(void **entry, void *user)
1390 isl_map **map = (isl_map **)entry;
1392 *map = isl_map_lexmin(*map);
1394 return *map ? 0 : -1;
1397 __isl_give isl_union_map *isl_union_map_lexmin(
1398 __isl_take isl_union_map *umap)
1400 return un_op(umap, &lexmin_entry);
1403 __isl_give isl_union_set *isl_union_set_lexmin(
1404 __isl_take isl_union_set *uset)
1406 return isl_union_map_lexmin(uset);
1409 static int lexmax_entry(void **entry, void *user)
1411 isl_map **map = (isl_map **)entry;
1413 *map = isl_map_lexmax(*map);
1415 return *map ? 0 : -1;
1418 __isl_give isl_union_map *isl_union_map_lexmax(
1419 __isl_take isl_union_map *umap)
1421 return un_op(umap, &lexmax_entry);
1424 __isl_give isl_union_set *isl_union_set_lexmax(
1425 __isl_take isl_union_set *uset)
1427 return isl_union_map_lexmax(uset);
1430 static int universe_entry(void **entry, void *user)
1432 isl_map *map = *entry;
1433 isl_union_map **res = user;
1435 map = isl_map_universe(isl_map_get_space(map));
1436 *res = isl_union_map_add_map(*res, map);
1438 return 0;
1441 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1443 return cond_un_op(umap, &universe_entry);
1446 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1448 return isl_union_map_universe(uset);
1451 static int reverse_entry(void **entry, void *user)
1453 isl_map *map = *entry;
1454 isl_union_map **res = user;
1456 *res = isl_union_map_add_map(*res, isl_map_reverse(isl_map_copy(map)));
1458 return 0;
1461 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1463 return cond_un_op(umap, &reverse_entry);
1466 static int params_entry(void **entry, void *user)
1468 isl_map *map = *entry;
1469 isl_union_set **res = user;
1471 *res = isl_union_set_add_set(*res, isl_map_params(isl_map_copy(map)));
1473 return 0;
1476 /* Compute the parameter domain of the given union map.
1478 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1480 int empty;
1482 empty = isl_union_map_is_empty(umap);
1483 if (empty < 0)
1484 return isl_union_map_free(umap);
1485 if (empty)
1486 return isl_set_empty(isl_union_map_get_space(umap));
1487 return isl_set_from_union_set(cond_un_op(umap, &params_entry));
1490 /* Compute the parameter domain of the given union set.
1492 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
1494 return isl_union_map_params(uset);
1497 static int domain_entry(void **entry, void *user)
1499 isl_map *map = *entry;
1500 isl_union_set **res = user;
1502 *res = isl_union_set_add_set(*res, isl_map_domain(isl_map_copy(map)));
1504 return 0;
1507 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1509 return cond_un_op(umap, &domain_entry);
1512 static int range_entry(void **entry, void *user)
1514 isl_map *map = *entry;
1515 isl_union_set **res = user;
1517 *res = isl_union_set_add_set(*res, isl_map_range(isl_map_copy(map)));
1519 return 0;
1522 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1524 return cond_un_op(umap, &range_entry);
1527 static int domain_map_entry(void **entry, void *user)
1529 isl_map *map = *entry;
1530 isl_union_set **res = user;
1532 *res = isl_union_map_add_map(*res,
1533 isl_map_domain_map(isl_map_copy(map)));
1535 return 0;
1538 __isl_give isl_union_map *isl_union_map_domain_map(
1539 __isl_take isl_union_map *umap)
1541 return cond_un_op(umap, &domain_map_entry);
1544 static int range_map_entry(void **entry, void *user)
1546 isl_map *map = *entry;
1547 isl_union_set **res = user;
1549 *res = isl_union_map_add_map(*res,
1550 isl_map_range_map(isl_map_copy(map)));
1552 return 0;
1555 __isl_give isl_union_map *isl_union_map_range_map(
1556 __isl_take isl_union_map *umap)
1558 return cond_un_op(umap, &range_map_entry);
1561 static int deltas_entry(void **entry, void *user)
1563 isl_map *map = *entry;
1564 isl_union_set **res = user;
1566 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1567 return 0;
1569 *res = isl_union_set_add_set(*res, isl_map_deltas(isl_map_copy(map)));
1571 return 0;
1574 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
1576 return cond_un_op(umap, &deltas_entry);
1579 static int deltas_map_entry(void **entry, void *user)
1581 isl_map *map = *entry;
1582 isl_union_map **res = user;
1584 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1585 return 0;
1587 *res = isl_union_map_add_map(*res,
1588 isl_map_deltas_map(isl_map_copy(map)));
1590 return 0;
1593 __isl_give isl_union_map *isl_union_map_deltas_map(
1594 __isl_take isl_union_map *umap)
1596 return cond_un_op(umap, &deltas_map_entry);
1599 static int identity_entry(void **entry, void *user)
1601 isl_set *set = *entry;
1602 isl_union_map **res = user;
1604 *res = isl_union_map_add_map(*res, isl_set_identity(isl_set_copy(set)));
1606 return 0;
1609 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
1611 return cond_un_op(uset, &identity_entry);
1614 static int unwrap_entry(void **entry, void *user)
1616 isl_set *set = *entry;
1617 isl_union_set **res = user;
1619 if (!isl_set_is_wrapping(set))
1620 return 0;
1622 *res = isl_union_map_add_map(*res, isl_set_unwrap(isl_set_copy(set)));
1624 return 0;
1627 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
1629 return cond_un_op(uset, &unwrap_entry);
1632 static int wrap_entry(void **entry, void *user)
1634 isl_map *map = *entry;
1635 isl_union_set **res = user;
1637 *res = isl_union_set_add_set(*res, isl_map_wrap(isl_map_copy(map)));
1639 return 0;
1642 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
1644 return cond_un_op(umap, &wrap_entry);
1647 struct isl_union_map_is_subset_data {
1648 isl_union_map *umap2;
1649 int is_subset;
1652 static int is_subset_entry(void **entry, void *user)
1654 struct isl_union_map_is_subset_data *data = user;
1655 uint32_t hash;
1656 struct isl_hash_table_entry *entry2;
1657 isl_map *map = *entry;
1659 hash = isl_space_get_hash(map->dim);
1660 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1661 hash, &has_dim, map->dim, 0);
1662 if (!entry2) {
1663 data->is_subset = 0;
1664 return -1;
1667 data->is_subset = isl_map_is_subset(map, entry2->data);
1668 if (data->is_subset < 0 || !data->is_subset)
1669 return -1;
1671 return 0;
1674 int isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
1675 __isl_keep isl_union_map *umap2)
1677 struct isl_union_map_is_subset_data data = { NULL, 1 };
1679 umap1 = isl_union_map_copy(umap1);
1680 umap2 = isl_union_map_copy(umap2);
1681 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1682 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1684 if (!umap1 || !umap2)
1685 goto error;
1687 data.umap2 = umap2;
1688 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1689 &is_subset_entry, &data) < 0 &&
1690 data.is_subset)
1691 goto error;
1693 isl_union_map_free(umap1);
1694 isl_union_map_free(umap2);
1696 return data.is_subset;
1697 error:
1698 isl_union_map_free(umap1);
1699 isl_union_map_free(umap2);
1700 return -1;
1703 int isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
1704 __isl_keep isl_union_set *uset2)
1706 return isl_union_map_is_subset(uset1, uset2);
1709 int isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
1710 __isl_keep isl_union_map *umap2)
1712 int is_subset;
1714 if (!umap1 || !umap2)
1715 return -1;
1716 is_subset = isl_union_map_is_subset(umap1, umap2);
1717 if (is_subset != 1)
1718 return is_subset;
1719 is_subset = isl_union_map_is_subset(umap2, umap1);
1720 return is_subset;
1723 int isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
1724 __isl_keep isl_union_set *uset2)
1726 return isl_union_map_is_equal(uset1, uset2);
1729 int isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
1730 __isl_keep isl_union_map *umap2)
1732 int is_subset;
1734 if (!umap1 || !umap2)
1735 return -1;
1736 is_subset = isl_union_map_is_subset(umap1, umap2);
1737 if (is_subset != 1)
1738 return is_subset;
1739 is_subset = isl_union_map_is_subset(umap2, umap1);
1740 if (is_subset == -1)
1741 return is_subset;
1742 return !is_subset;
1745 int isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
1746 __isl_keep isl_union_set *uset2)
1748 return isl_union_map_is_strict_subset(uset1, uset2);
1751 static int sample_entry(void **entry, void *user)
1753 isl_basic_map **sample = (isl_basic_map **)user;
1754 isl_map *map = *entry;
1756 *sample = isl_map_sample(isl_map_copy(map));
1757 if (!*sample)
1758 return -1;
1759 if (!isl_basic_map_plain_is_empty(*sample))
1760 return -1;
1761 return 0;
1764 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
1766 isl_basic_map *sample = NULL;
1768 if (!umap)
1769 return NULL;
1771 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1772 &sample_entry, &sample) < 0 &&
1773 !sample)
1774 goto error;
1776 if (!sample)
1777 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
1779 isl_union_map_free(umap);
1781 return sample;
1782 error:
1783 isl_union_map_free(umap);
1784 return NULL;
1787 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
1789 return (isl_basic_set *)isl_union_map_sample(uset);
1792 struct isl_forall_data {
1793 int res;
1794 int (*fn)(__isl_keep isl_map *map);
1797 static int forall_entry(void **entry, void *user)
1799 struct isl_forall_data *data = user;
1800 isl_map *map = *entry;
1802 data->res = data->fn(map);
1803 if (data->res < 0)
1804 return -1;
1806 if (!data->res)
1807 return -1;
1809 return 0;
1812 static int union_map_forall(__isl_keep isl_union_map *umap,
1813 int (*fn)(__isl_keep isl_map *map))
1815 struct isl_forall_data data = { 1, fn };
1817 if (!umap)
1818 return -1;
1820 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1821 &forall_entry, &data) < 0 && data.res)
1822 return -1;
1824 return data.res;
1827 struct isl_forall_user_data {
1828 int res;
1829 int (*fn)(__isl_keep isl_map *map, void *user);
1830 void *user;
1833 static int forall_user_entry(void **entry, void *user)
1835 struct isl_forall_user_data *data = user;
1836 isl_map *map = *entry;
1838 data->res = data->fn(map, data->user);
1839 if (data->res < 0)
1840 return -1;
1842 if (!data->res)
1843 return -1;
1845 return 0;
1848 /* Check if fn(map, user) returns true for all maps "map" in umap.
1850 static int union_map_forall_user(__isl_keep isl_union_map *umap,
1851 int (*fn)(__isl_keep isl_map *map, void *user), void *user)
1853 struct isl_forall_user_data data = { 1, fn, user };
1855 if (!umap)
1856 return -1;
1858 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1859 &forall_user_entry, &data) < 0 && data.res)
1860 return -1;
1862 return data.res;
1865 int isl_union_map_is_empty(__isl_keep isl_union_map *umap)
1867 return union_map_forall(umap, &isl_map_is_empty);
1870 int isl_union_set_is_empty(__isl_keep isl_union_set *uset)
1872 return isl_union_map_is_empty(uset);
1875 static int is_subset_of_identity(__isl_keep isl_map *map)
1877 int is_subset;
1878 isl_space *dim;
1879 isl_map *id;
1881 if (!map)
1882 return -1;
1884 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1885 return 0;
1887 dim = isl_map_get_space(map);
1888 id = isl_map_identity(dim);
1890 is_subset = isl_map_is_subset(map, id);
1892 isl_map_free(id);
1894 return is_subset;
1897 /* Check if the given map is single-valued.
1898 * We simply compute
1900 * M \circ M^-1
1902 * and check if the result is a subset of the identity mapping.
1904 int isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
1906 isl_union_map *test;
1907 int sv;
1909 if (isl_union_map_n_map(umap) == 1) {
1910 isl_map *map;
1911 umap = isl_union_map_copy(umap);
1912 map = isl_map_from_union_map(umap);
1913 sv = isl_map_is_single_valued(map);
1914 isl_map_free(map);
1915 return sv;
1918 test = isl_union_map_reverse(isl_union_map_copy(umap));
1919 test = isl_union_map_apply_range(test, isl_union_map_copy(umap));
1921 sv = union_map_forall(test, &is_subset_of_identity);
1923 isl_union_map_free(test);
1925 return sv;
1928 int isl_union_map_is_injective(__isl_keep isl_union_map *umap)
1930 int in;
1932 umap = isl_union_map_copy(umap);
1933 umap = isl_union_map_reverse(umap);
1934 in = isl_union_map_is_single_valued(umap);
1935 isl_union_map_free(umap);
1937 return in;
1940 /* Represents a map that has a fixed value (v) for one of its
1941 * range dimensions.
1942 * The map in this structure is not reference counted, so it
1943 * is only valid while the isl_union_map from which it was
1944 * obtained is still alive.
1946 struct isl_fixed_map {
1947 isl_int v;
1948 isl_map *map;
1951 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
1952 int n)
1954 int i;
1955 struct isl_fixed_map *v;
1957 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
1958 if (!v)
1959 return NULL;
1960 for (i = 0; i < n; ++i)
1961 isl_int_init(v[i].v);
1962 return v;
1965 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
1967 int i;
1969 if (!v)
1970 return;
1971 for (i = 0; i < n; ++i)
1972 isl_int_clear(v[i].v);
1973 free(v);
1976 /* Compare the "v" field of two isl_fixed_map structs.
1978 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
1980 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
1981 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
1983 return isl_int_cmp(e1->v, e2->v);
1986 /* Internal data structure used while checking whether all maps
1987 * in a union_map have a fixed value for a given output dimension.
1988 * v is the list of maps, with the fixed value for the dimension
1989 * n is the number of maps considered so far
1990 * pos is the output dimension under investigation
1992 struct isl_fixed_dim_data {
1993 struct isl_fixed_map *v;
1994 int n;
1995 int pos;
1998 static int fixed_at_pos(__isl_keep isl_map *map, void *user)
2000 struct isl_fixed_dim_data *data = user;
2002 data->v[data->n].map = map;
2003 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2004 &data->v[data->n++].v);
2007 static int plain_injective_on_range(__isl_take isl_union_map *umap,
2008 int first, int n_range);
2010 /* Given a list of the maps, with their fixed values at output dimension "pos",
2011 * check whether the ranges of the maps form an obvious partition.
2013 * We first sort the maps according to their fixed values.
2014 * If all maps have a different value, then we know the ranges form
2015 * a partition.
2016 * Otherwise, we collect the maps with the same fixed value and
2017 * check whether each such collection is obviously injective
2018 * based on later dimensions.
2020 static int separates(struct isl_fixed_map *v, int n,
2021 __isl_take isl_space *dim, int pos, int n_range)
2023 int i;
2025 if (!v)
2026 goto error;
2028 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2030 for (i = 0; i + 1 < n; ++i) {
2031 int j, k;
2032 isl_union_map *part;
2033 int injective;
2035 for (j = i + 1; j < n; ++j)
2036 if (isl_int_ne(v[i].v, v[j].v))
2037 break;
2039 if (j == i + 1)
2040 continue;
2042 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2043 for (k = i; k < j; ++k)
2044 part = isl_union_map_add_map(part,
2045 isl_map_copy(v[k].map));
2047 injective = plain_injective_on_range(part, pos + 1, n_range);
2048 if (injective < 0)
2049 goto error;
2050 if (!injective)
2051 break;
2053 i = j - 1;
2056 isl_space_free(dim);
2057 free_isl_fixed_map_array(v, n);
2058 return i + 1 >= n;
2059 error:
2060 isl_space_free(dim);
2061 free_isl_fixed_map_array(v, n);
2062 return -1;
2065 /* Check whether the maps in umap have obviously distinct ranges.
2066 * In particular, check for an output dimension in the range
2067 * [first,n_range) for which all maps have a fixed value
2068 * and then check if these values, possibly along with fixed values
2069 * at later dimensions, entail distinct ranges.
2071 static int plain_injective_on_range(__isl_take isl_union_map *umap,
2072 int first, int n_range)
2074 isl_ctx *ctx;
2075 int n;
2076 struct isl_fixed_dim_data data = { NULL };
2078 ctx = isl_union_map_get_ctx(umap);
2080 if (!umap)
2081 goto error;
2083 n = isl_union_map_n_map(umap);
2084 if (n <= 1) {
2085 isl_union_map_free(umap);
2086 return 1;
2089 if (first >= n_range) {
2090 isl_union_map_free(umap);
2091 return 0;
2094 data.v = alloc_isl_fixed_map_array(ctx, n);
2095 if (!data.v)
2096 goto error;
2098 for (data.pos = first; data.pos < n_range; ++data.pos) {
2099 int fixed;
2100 int injective;
2101 isl_space *dim;
2103 data.n = 0;
2104 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2105 if (fixed < 0)
2106 goto error;
2107 if (!fixed)
2108 continue;
2109 dim = isl_union_map_get_space(umap);
2110 injective = separates(data.v, n, dim, data.pos, n_range);
2111 isl_union_map_free(umap);
2112 return injective;
2115 free_isl_fixed_map_array(data.v, n);
2116 isl_union_map_free(umap);
2118 return 0;
2119 error:
2120 free_isl_fixed_map_array(data.v, n);
2121 isl_union_map_free(umap);
2122 return -1;
2125 /* Check whether the maps in umap that map to subsets of "ran"
2126 * have obviously distinct ranges.
2128 static int plain_injective_on_range_wrap(__isl_keep isl_set *ran, void *user)
2130 isl_union_map *umap = user;
2132 umap = isl_union_map_copy(umap);
2133 umap = isl_union_map_intersect_range(umap,
2134 isl_union_set_from_set(isl_set_copy(ran)));
2135 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2138 /* Check if the given union_map is obviously injective.
2140 * In particular, we first check if all individual maps are obviously
2141 * injective and then check if all the ranges of these maps are
2142 * obviously disjoint.
2144 int isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2146 int in;
2147 isl_union_map *univ;
2148 isl_union_set *ran;
2150 in = union_map_forall(umap, &isl_map_plain_is_injective);
2151 if (in < 0)
2152 return -1;
2153 if (!in)
2154 return 0;
2156 univ = isl_union_map_universe(isl_union_map_copy(umap));
2157 ran = isl_union_map_range(univ);
2159 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
2161 isl_union_set_free(ran);
2163 return in;
2166 int isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
2168 int sv;
2170 sv = isl_union_map_is_single_valued(umap);
2171 if (sv < 0 || !sv)
2172 return sv;
2174 return isl_union_map_is_injective(umap);
2177 static int zip_entry(void **entry, void *user)
2179 isl_map *map = *entry;
2180 isl_union_map **res = user;
2182 if (!isl_map_can_zip(map))
2183 return 0;
2185 *res = isl_union_map_add_map(*res, isl_map_zip(isl_map_copy(map)));
2187 return 0;
2190 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
2192 return cond_un_op(umap, &zip_entry);
2195 static int lift_entry(void **entry, void *user)
2197 isl_set *set = *entry;
2198 isl_union_set **res = user;
2200 *res = isl_union_set_add_set(*res, isl_set_lift(isl_set_copy(set)));
2202 return 0;
2205 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
2207 return cond_un_op(uset, &lift_entry);
2210 static int coefficients_entry(void **entry, void *user)
2212 isl_set *set = *entry;
2213 isl_union_set **res = user;
2215 set = isl_set_copy(set);
2216 set = isl_set_from_basic_set(isl_set_coefficients(set));
2217 *res = isl_union_set_add_set(*res, set);
2219 return 0;
2222 __isl_give isl_union_set *isl_union_set_coefficients(
2223 __isl_take isl_union_set *uset)
2225 isl_ctx *ctx;
2226 isl_space *dim;
2227 isl_union_set *res;
2229 if (!uset)
2230 return NULL;
2232 ctx = isl_union_set_get_ctx(uset);
2233 dim = isl_space_set_alloc(ctx, 0, 0);
2234 res = isl_union_map_alloc(dim, uset->table.n);
2235 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2236 &coefficients_entry, &res) < 0)
2237 goto error;
2239 isl_union_set_free(uset);
2240 return res;
2241 error:
2242 isl_union_set_free(uset);
2243 isl_union_set_free(res);
2244 return NULL;
2247 static int solutions_entry(void **entry, void *user)
2249 isl_set *set = *entry;
2250 isl_union_set **res = user;
2252 set = isl_set_copy(set);
2253 set = isl_set_from_basic_set(isl_set_solutions(set));
2254 if (!*res)
2255 *res = isl_union_set_from_set(set);
2256 else
2257 *res = isl_union_set_add_set(*res, set);
2259 if (!*res)
2260 return -1;
2262 return 0;
2265 __isl_give isl_union_set *isl_union_set_solutions(
2266 __isl_take isl_union_set *uset)
2268 isl_union_set *res = NULL;
2270 if (!uset)
2271 return NULL;
2273 if (uset->table.n == 0) {
2274 res = isl_union_set_empty(isl_union_set_get_space(uset));
2275 isl_union_set_free(uset);
2276 return res;
2279 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2280 &solutions_entry, &res) < 0)
2281 goto error;
2283 isl_union_set_free(uset);
2284 return res;
2285 error:
2286 isl_union_set_free(uset);
2287 isl_union_set_free(res);
2288 return NULL;