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,
12 #include <isl_map_private.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)
30 if (uset->table.n != 1)
33 set = isl_set_from_union_set(isl_union_set_copy(uset));
34 params = isl_set_is_params(set);
39 static __isl_give isl_union_map *isl_union_map_alloc(__isl_take isl_space *dim,
47 umap = isl_calloc_type(dim->ctx, isl_union_map);
53 if (isl_hash_table_init(dim->ctx, &umap->table, size) < 0)
59 isl_union_map_free(umap);
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)
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;
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);
111 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
118 dup = isl_union_map_empty(isl_space_copy(umap->dim));
119 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
123 isl_union_map_free(dup);
127 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
135 return isl_union_map_dup(umap);
138 struct isl_union_align {
143 static int align_entry(void **entry, void *user)
145 isl_map *map = *entry;
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));
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 };
171 if (isl_space_match(umap->dim, isl_dim_param, model, isl_dim_param)) {
172 isl_space_free(model);
176 model = isl_space_params(model);
177 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
181 data.res = isl_union_map_alloc(isl_space_copy(data.exp->dim),
183 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
184 &align_entry, &data) < 0)
187 isl_reordering_free(data.exp);
188 isl_union_map_free(umap);
189 isl_space_free(model);
192 isl_reordering_free(data.exp);
193 isl_union_map_free(umap);
194 isl_union_map_free(data.res);
195 isl_space_free(model);
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)
216 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
219 isl_union_map_free(umap2);
223 isl_union_map_free(umap1);
224 isl_union_map_free(umap2);
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)
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)
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);
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)
281 struct isl_hash_table_entry *entry;
286 if (isl_map_plain_is_empty(map)) {
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);
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);
310 entry->data = isl_map_union(entry->data, isl_map_copy(map));
319 isl_union_map_free(umap);
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)
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);
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);
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 };
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);
397 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
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, ©_map, &map);
412 isl_union_map_free(umap);
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)
426 struct isl_hash_table_entry *entry;
431 hash = isl_space_get_hash(dim);
432 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
435 return isl_map_empty(dim);
437 return isl_map_copy(entry->data);
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)
455 struct isl_hash_table_entry *entry;
460 hash = isl_space_get_hash(dim);
461 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
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);
484 static int foreach_point(__isl_take isl_set *set, void *user)
486 struct isl_union_set_foreach_point_data *data = user;
489 r = isl_set_foreach_point(set, data->fn, data->user);
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;
507 static int subtract_entry(void **entry, void *user)
509 struct isl_union_map_gen_bin_data *data = user;
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);
520 map = isl_map_subtract(map, isl_map_copy(entry2->data));
522 empty = isl_map_is_empty(map);
532 data->res = isl_union_map_add_map(data->res, map);
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)
549 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
551 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
555 isl_union_map_free(umap1);
556 isl_union_map_free(umap2);
559 isl_union_map_free(umap1);
560 isl_union_map_free(umap2);
561 isl_union_map_free(data.res);
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 {
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;
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);
597 data->res = isl_union_map_add_map(data->res, map);
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));
614 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
616 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
620 isl_union_map_free(umap);
624 isl_union_map_free(umap);
626 isl_union_map_free(data.res);
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;
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;
665 struct isl_hash_table_entry *entry2;
666 isl_map *map = *entry;
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);
675 map = isl_map_copy(map);
676 map = data->fn(map, isl_map_copy(entry2->data));
678 empty = isl_map_is_empty(map);
688 data->res = isl_union_map_add_map(data->res, map);
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)
706 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
708 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
709 &match_bin_entry, &data) < 0)
712 isl_union_map_free(umap1);
713 isl_union_map_free(umap2);
716 isl_union_map_free(umap1);
717 isl_union_map_free(umap2);
718 isl_union_map_free(data.res);
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)
738 p1 = isl_union_set_is_params(uset1);
739 p2 = isl_union_set_is_params(uset2);
740 if (p1 < 0 || p2 < 0)
743 return union_map_intersect_params(uset1, uset2);
745 return union_map_intersect_params(uset2, uset1);
746 return isl_union_map_intersect(uset1, uset2);
748 isl_union_set_free(uset1);
749 isl_union_set_free(uset2);
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;
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);
768 data->res = isl_union_map_add_map(data->res, map);
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;
851 struct isl_hash_table_entry *entry2;
853 isl_map *map = *entry;
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);
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);
878 data->res = isl_union_map_add_map(data->res, map);
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;
899 struct isl_hash_table_entry *entry2;
901 isl_map *map = *entry;
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);
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);
922 data->res = isl_union_map_add_map(data->res, map);
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 gist_range_entry(void **entry, void *user)
941 struct isl_union_map_gen_bin_data *data = user;
943 struct isl_hash_table_entry *entry2;
945 isl_map *map = *entry;
948 space = isl_map_get_space(map);
949 space = isl_space_range(space);
950 hash = isl_space_get_hash(space);
951 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
952 hash, &has_dim, space, 0);
953 isl_space_free(space);
957 map = isl_map_copy(map);
958 map = isl_map_gist_range(map, isl_set_copy(entry2->data));
960 empty = isl_map_is_empty(map);
966 data->res = isl_union_map_add_map(data->res, map);
971 /* Compute the gist of "umap" with respect to the range "uset".
973 __isl_give isl_union_map *isl_union_map_gist_range(
974 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
976 return gen_bin_op(umap, uset, &gist_range_entry);
979 static int intersect_range_entry(void **entry, void *user)
981 struct isl_union_map_gen_bin_data *data = user;
983 struct isl_hash_table_entry *entry2;
985 isl_map *map = *entry;
988 dim = isl_map_get_space(map);
989 dim = isl_space_range(dim);
990 hash = isl_space_get_hash(dim);
991 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
992 hash, &has_dim, dim, 0);
997 map = isl_map_copy(map);
998 map = isl_map_intersect_range(map, isl_set_copy(entry2->data));
1000 empty = isl_map_is_empty(map);
1010 data->res = isl_union_map_add_map(data->res, map);
1015 __isl_give isl_union_map *isl_union_map_intersect_range(
1016 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1018 return gen_bin_op(umap, uset, &intersect_range_entry);
1021 struct isl_union_map_bin_data {
1022 isl_union_map *umap2;
1025 int (*fn)(void **entry, void *user);
1028 static int apply_range_entry(void **entry, void *user)
1030 struct isl_union_map_bin_data *data = user;
1031 isl_map *map2 = *entry;
1034 if (!isl_space_tuple_match(data->map->dim, isl_dim_out,
1035 map2->dim, isl_dim_in))
1038 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1040 empty = isl_map_is_empty(map2);
1050 data->res = isl_union_map_add_map(data->res, map2);
1055 static int bin_entry(void **entry, void *user)
1057 struct isl_union_map_bin_data *data = user;
1058 isl_map *map = *entry;
1061 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1062 data->fn, data) < 0)
1068 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1069 __isl_take isl_union_map *umap2, int (*fn)(void **entry, void *user))
1071 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1073 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1074 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1076 if (!umap1 || !umap2)
1080 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1082 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1083 &bin_entry, &data) < 0)
1086 isl_union_map_free(umap1);
1087 isl_union_map_free(umap2);
1090 isl_union_map_free(umap1);
1091 isl_union_map_free(umap2);
1092 isl_union_map_free(data.res);
1096 __isl_give isl_union_map *isl_union_map_apply_range(
1097 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1099 return bin_op(umap1, umap2, &apply_range_entry);
1102 __isl_give isl_union_map *isl_union_map_apply_domain(
1103 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1105 umap1 = isl_union_map_reverse(umap1);
1106 umap1 = isl_union_map_apply_range(umap1, umap2);
1107 return isl_union_map_reverse(umap1);
1110 __isl_give isl_union_set *isl_union_set_apply(
1111 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1113 return isl_union_map_apply_range(uset, umap);
1116 static int map_lex_lt_entry(void **entry, void *user)
1118 struct isl_union_map_bin_data *data = user;
1119 isl_map *map2 = *entry;
1121 if (!isl_space_tuple_match(data->map->dim, isl_dim_out,
1122 map2->dim, isl_dim_out))
1125 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1127 data->res = isl_union_map_add_map(data->res, map2);
1132 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1133 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1135 return bin_op(umap1, umap2, &map_lex_lt_entry);
1138 static int map_lex_le_entry(void **entry, void *user)
1140 struct isl_union_map_bin_data *data = user;
1141 isl_map *map2 = *entry;
1143 if (!isl_space_tuple_match(data->map->dim, isl_dim_out,
1144 map2->dim, isl_dim_out))
1147 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1149 data->res = isl_union_map_add_map(data->res, map2);
1154 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1155 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1157 return bin_op(umap1, umap2, &map_lex_le_entry);
1160 static int product_entry(void **entry, void *user)
1162 struct isl_union_map_bin_data *data = user;
1163 isl_map *map2 = *entry;
1165 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1167 data->res = isl_union_map_add_map(data->res, map2);
1172 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1173 __isl_take isl_union_map *umap2)
1175 return bin_op(umap1, umap2, &product_entry);
1178 static int set_product_entry(void **entry, void *user)
1180 struct isl_union_map_bin_data *data = user;
1181 isl_set *set2 = *entry;
1183 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1185 data->res = isl_union_set_add_set(data->res, set2);
1190 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1191 __isl_take isl_union_set *uset2)
1193 return bin_op(uset1, uset2, &set_product_entry);
1196 static int range_product_entry(void **entry, void *user)
1198 struct isl_union_map_bin_data *data = user;
1199 isl_map *map2 = *entry;
1201 if (!isl_space_tuple_match(data->map->dim, isl_dim_in,
1202 map2->dim, isl_dim_in))
1205 map2 = isl_map_range_product(isl_map_copy(data->map),
1206 isl_map_copy(map2));
1208 data->res = isl_union_map_add_map(data->res, map2);
1213 __isl_give isl_union_map *isl_union_map_range_product(
1214 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1216 return bin_op(umap1, umap2, &range_product_entry);
1219 static int flat_range_product_entry(void **entry, void *user)
1221 struct isl_union_map_bin_data *data = user;
1222 isl_map *map2 = *entry;
1224 if (!isl_space_tuple_match(data->map->dim, isl_dim_in,
1225 map2->dim, isl_dim_in))
1228 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1229 isl_map_copy(map2));
1231 data->res = isl_union_map_add_map(data->res, map2);
1236 __isl_give isl_union_map *isl_union_map_flat_range_product(
1237 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1239 return bin_op(umap1, umap2, &flat_range_product_entry);
1242 static __isl_give isl_union_set *cond_un_op(__isl_take isl_union_map *umap,
1243 int (*fn)(void **, void *))
1250 res = isl_union_map_alloc(isl_space_copy(umap->dim), umap->table.n);
1251 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, &res) < 0)
1254 isl_union_map_free(umap);
1257 isl_union_map_free(umap);
1258 isl_union_set_free(res);
1262 static int from_range_entry(void **entry, void *user)
1264 isl_map *set = *entry;
1265 isl_union_set **res = user;
1267 *res = isl_union_map_add_map(*res,
1268 isl_map_from_range(isl_set_copy(set)));
1273 __isl_give isl_union_map *isl_union_map_from_range(
1274 __isl_take isl_union_set *uset)
1276 return cond_un_op(uset, &from_range_entry);
1279 __isl_give isl_union_map *isl_union_map_from_domain(
1280 __isl_take isl_union_set *uset)
1282 return isl_union_map_reverse(isl_union_map_from_range(uset));
1285 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1286 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1288 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1289 isl_union_map_from_range(range));
1292 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1293 int (*fn)(void **, void *))
1295 umap = isl_union_map_cow(umap);
1299 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, NULL) < 0)
1304 isl_union_map_free(umap);
1308 static int affine_entry(void **entry, void *user)
1310 isl_map **map = (isl_map **)entry;
1312 *map = isl_map_from_basic_map(isl_map_affine_hull(*map));
1314 return *map ? 0 : -1;
1317 __isl_give isl_union_map *isl_union_map_affine_hull(
1318 __isl_take isl_union_map *umap)
1320 return un_op(umap, &affine_entry);
1323 __isl_give isl_union_set *isl_union_set_affine_hull(
1324 __isl_take isl_union_set *uset)
1326 return isl_union_map_affine_hull(uset);
1329 static int polyhedral_entry(void **entry, void *user)
1331 isl_map **map = (isl_map **)entry;
1333 *map = isl_map_from_basic_map(isl_map_polyhedral_hull(*map));
1335 return *map ? 0 : -1;
1338 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1339 __isl_take isl_union_map *umap)
1341 return un_op(umap, &polyhedral_entry);
1344 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1345 __isl_take isl_union_set *uset)
1347 return isl_union_map_polyhedral_hull(uset);
1350 static int simple_entry(void **entry, void *user)
1352 isl_map **map = (isl_map **)entry;
1354 *map = isl_map_from_basic_map(isl_map_simple_hull(*map));
1356 return *map ? 0 : -1;
1359 __isl_give isl_union_map *isl_union_map_simple_hull(
1360 __isl_take isl_union_map *umap)
1362 return un_op(umap, &simple_entry);
1365 __isl_give isl_union_set *isl_union_set_simple_hull(
1366 __isl_take isl_union_set *uset)
1368 return isl_union_map_simple_hull(uset);
1371 static int inplace_entry(void **entry, void *user)
1373 __isl_give isl_map *(*fn)(__isl_take isl_map *);
1374 isl_map **map = (isl_map **)entry;
1377 fn = *(__isl_give isl_map *(**)(__isl_take isl_map *)) user;
1378 copy = fn(isl_map_copy(*map));
1388 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1389 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1394 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1395 &inplace_entry, &fn) < 0)
1400 isl_union_map_free(umap);
1404 __isl_give isl_union_map *isl_union_map_coalesce(
1405 __isl_take isl_union_map *umap)
1407 return inplace(umap, &isl_map_coalesce);
1410 __isl_give isl_union_set *isl_union_set_coalesce(
1411 __isl_take isl_union_set *uset)
1413 return isl_union_map_coalesce(uset);
1416 __isl_give isl_union_map *isl_union_map_detect_equalities(
1417 __isl_take isl_union_map *umap)
1419 return inplace(umap, &isl_map_detect_equalities);
1422 __isl_give isl_union_set *isl_union_set_detect_equalities(
1423 __isl_take isl_union_set *uset)
1425 return isl_union_map_detect_equalities(uset);
1428 __isl_give isl_union_map *isl_union_map_compute_divs(
1429 __isl_take isl_union_map *umap)
1431 return inplace(umap, &isl_map_compute_divs);
1434 __isl_give isl_union_set *isl_union_set_compute_divs(
1435 __isl_take isl_union_set *uset)
1437 return isl_union_map_compute_divs(uset);
1440 static int lexmin_entry(void **entry, void *user)
1442 isl_map **map = (isl_map **)entry;
1444 *map = isl_map_lexmin(*map);
1446 return *map ? 0 : -1;
1449 __isl_give isl_union_map *isl_union_map_lexmin(
1450 __isl_take isl_union_map *umap)
1452 return un_op(umap, &lexmin_entry);
1455 __isl_give isl_union_set *isl_union_set_lexmin(
1456 __isl_take isl_union_set *uset)
1458 return isl_union_map_lexmin(uset);
1461 static int lexmax_entry(void **entry, void *user)
1463 isl_map **map = (isl_map **)entry;
1465 *map = isl_map_lexmax(*map);
1467 return *map ? 0 : -1;
1470 __isl_give isl_union_map *isl_union_map_lexmax(
1471 __isl_take isl_union_map *umap)
1473 return un_op(umap, &lexmax_entry);
1476 __isl_give isl_union_set *isl_union_set_lexmax(
1477 __isl_take isl_union_set *uset)
1479 return isl_union_map_lexmax(uset);
1482 static int universe_entry(void **entry, void *user)
1484 isl_map *map = *entry;
1485 isl_union_map **res = user;
1487 map = isl_map_universe(isl_map_get_space(map));
1488 *res = isl_union_map_add_map(*res, map);
1493 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1495 return cond_un_op(umap, &universe_entry);
1498 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1500 return isl_union_map_universe(uset);
1503 static int reverse_entry(void **entry, void *user)
1505 isl_map *map = *entry;
1506 isl_union_map **res = user;
1508 *res = isl_union_map_add_map(*res, isl_map_reverse(isl_map_copy(map)));
1513 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1515 return cond_un_op(umap, &reverse_entry);
1518 static int params_entry(void **entry, void *user)
1520 isl_map *map = *entry;
1521 isl_union_set **res = user;
1523 *res = isl_union_set_add_set(*res, isl_map_params(isl_map_copy(map)));
1528 /* Compute the parameter domain of the given union map.
1530 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1534 empty = isl_union_map_is_empty(umap);
1536 return isl_union_map_free(umap);
1538 return isl_set_empty(isl_union_map_get_space(umap));
1539 return isl_set_from_union_set(cond_un_op(umap, ¶ms_entry));
1542 /* Compute the parameter domain of the given union set.
1544 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
1546 return isl_union_map_params(uset);
1549 static int domain_entry(void **entry, void *user)
1551 isl_map *map = *entry;
1552 isl_union_set **res = user;
1554 *res = isl_union_set_add_set(*res, isl_map_domain(isl_map_copy(map)));
1559 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1561 return cond_un_op(umap, &domain_entry);
1564 static int range_entry(void **entry, void *user)
1566 isl_map *map = *entry;
1567 isl_union_set **res = user;
1569 *res = isl_union_set_add_set(*res, isl_map_range(isl_map_copy(map)));
1574 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1576 return cond_un_op(umap, &range_entry);
1579 static int domain_map_entry(void **entry, void *user)
1581 isl_map *map = *entry;
1582 isl_union_set **res = user;
1584 *res = isl_union_map_add_map(*res,
1585 isl_map_domain_map(isl_map_copy(map)));
1590 __isl_give isl_union_map *isl_union_map_domain_map(
1591 __isl_take isl_union_map *umap)
1593 return cond_un_op(umap, &domain_map_entry);
1596 static int range_map_entry(void **entry, void *user)
1598 isl_map *map = *entry;
1599 isl_union_set **res = user;
1601 *res = isl_union_map_add_map(*res,
1602 isl_map_range_map(isl_map_copy(map)));
1607 __isl_give isl_union_map *isl_union_map_range_map(
1608 __isl_take isl_union_map *umap)
1610 return cond_un_op(umap, &range_map_entry);
1613 static int deltas_entry(void **entry, void *user)
1615 isl_map *map = *entry;
1616 isl_union_set **res = user;
1618 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1621 *res = isl_union_set_add_set(*res, isl_map_deltas(isl_map_copy(map)));
1626 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
1628 return cond_un_op(umap, &deltas_entry);
1631 static int deltas_map_entry(void **entry, void *user)
1633 isl_map *map = *entry;
1634 isl_union_map **res = user;
1636 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1639 *res = isl_union_map_add_map(*res,
1640 isl_map_deltas_map(isl_map_copy(map)));
1645 __isl_give isl_union_map *isl_union_map_deltas_map(
1646 __isl_take isl_union_map *umap)
1648 return cond_un_op(umap, &deltas_map_entry);
1651 static int identity_entry(void **entry, void *user)
1653 isl_set *set = *entry;
1654 isl_union_map **res = user;
1656 *res = isl_union_map_add_map(*res, isl_set_identity(isl_set_copy(set)));
1661 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
1663 return cond_un_op(uset, &identity_entry);
1666 static int unwrap_entry(void **entry, void *user)
1668 isl_set *set = *entry;
1669 isl_union_set **res = user;
1671 if (!isl_set_is_wrapping(set))
1674 *res = isl_union_map_add_map(*res, isl_set_unwrap(isl_set_copy(set)));
1679 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
1681 return cond_un_op(uset, &unwrap_entry);
1684 static int wrap_entry(void **entry, void *user)
1686 isl_map *map = *entry;
1687 isl_union_set **res = user;
1689 *res = isl_union_set_add_set(*res, isl_map_wrap(isl_map_copy(map)));
1694 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
1696 return cond_un_op(umap, &wrap_entry);
1699 struct isl_union_map_is_subset_data {
1700 isl_union_map *umap2;
1704 static int is_subset_entry(void **entry, void *user)
1706 struct isl_union_map_is_subset_data *data = user;
1708 struct isl_hash_table_entry *entry2;
1709 isl_map *map = *entry;
1711 hash = isl_space_get_hash(map->dim);
1712 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1713 hash, &has_dim, map->dim, 0);
1715 data->is_subset = 0;
1719 data->is_subset = isl_map_is_subset(map, entry2->data);
1720 if (data->is_subset < 0 || !data->is_subset)
1726 int isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
1727 __isl_keep isl_union_map *umap2)
1729 struct isl_union_map_is_subset_data data = { NULL, 1 };
1731 umap1 = isl_union_map_copy(umap1);
1732 umap2 = isl_union_map_copy(umap2);
1733 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1734 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1736 if (!umap1 || !umap2)
1740 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1741 &is_subset_entry, &data) < 0 &&
1745 isl_union_map_free(umap1);
1746 isl_union_map_free(umap2);
1748 return data.is_subset;
1750 isl_union_map_free(umap1);
1751 isl_union_map_free(umap2);
1755 int isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
1756 __isl_keep isl_union_set *uset2)
1758 return isl_union_map_is_subset(uset1, uset2);
1761 int isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
1762 __isl_keep isl_union_map *umap2)
1766 if (!umap1 || !umap2)
1768 is_subset = isl_union_map_is_subset(umap1, umap2);
1771 is_subset = isl_union_map_is_subset(umap2, umap1);
1775 int isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
1776 __isl_keep isl_union_set *uset2)
1778 return isl_union_map_is_equal(uset1, uset2);
1781 int isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
1782 __isl_keep isl_union_map *umap2)
1786 if (!umap1 || !umap2)
1788 is_subset = isl_union_map_is_subset(umap1, umap2);
1791 is_subset = isl_union_map_is_subset(umap2, umap1);
1792 if (is_subset == -1)
1797 int isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
1798 __isl_keep isl_union_set *uset2)
1800 return isl_union_map_is_strict_subset(uset1, uset2);
1803 static int sample_entry(void **entry, void *user)
1805 isl_basic_map **sample = (isl_basic_map **)user;
1806 isl_map *map = *entry;
1808 *sample = isl_map_sample(isl_map_copy(map));
1811 if (!isl_basic_map_plain_is_empty(*sample))
1816 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
1818 isl_basic_map *sample = NULL;
1823 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1824 &sample_entry, &sample) < 0 &&
1829 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
1831 isl_union_map_free(umap);
1835 isl_union_map_free(umap);
1839 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
1841 return (isl_basic_set *)isl_union_map_sample(uset);
1844 struct isl_forall_data {
1846 int (*fn)(__isl_keep isl_map *map);
1849 static int forall_entry(void **entry, void *user)
1851 struct isl_forall_data *data = user;
1852 isl_map *map = *entry;
1854 data->res = data->fn(map);
1864 static int union_map_forall(__isl_keep isl_union_map *umap,
1865 int (*fn)(__isl_keep isl_map *map))
1867 struct isl_forall_data data = { 1, fn };
1872 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1873 &forall_entry, &data) < 0 && data.res)
1879 struct isl_forall_user_data {
1881 int (*fn)(__isl_keep isl_map *map, void *user);
1885 static int forall_user_entry(void **entry, void *user)
1887 struct isl_forall_user_data *data = user;
1888 isl_map *map = *entry;
1890 data->res = data->fn(map, data->user);
1900 /* Check if fn(map, user) returns true for all maps "map" in umap.
1902 static int union_map_forall_user(__isl_keep isl_union_map *umap,
1903 int (*fn)(__isl_keep isl_map *map, void *user), void *user)
1905 struct isl_forall_user_data data = { 1, fn, user };
1910 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1911 &forall_user_entry, &data) < 0 && data.res)
1917 int isl_union_map_is_empty(__isl_keep isl_union_map *umap)
1919 return union_map_forall(umap, &isl_map_is_empty);
1922 int isl_union_set_is_empty(__isl_keep isl_union_set *uset)
1924 return isl_union_map_is_empty(uset);
1927 static int is_subset_of_identity(__isl_keep isl_map *map)
1936 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1939 dim = isl_map_get_space(map);
1940 id = isl_map_identity(dim);
1942 is_subset = isl_map_is_subset(map, id);
1949 /* Check if the given map is single-valued.
1954 * and check if the result is a subset of the identity mapping.
1956 int isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
1958 isl_union_map *test;
1961 if (isl_union_map_n_map(umap) == 1) {
1963 umap = isl_union_map_copy(umap);
1964 map = isl_map_from_union_map(umap);
1965 sv = isl_map_is_single_valued(map);
1970 test = isl_union_map_reverse(isl_union_map_copy(umap));
1971 test = isl_union_map_apply_range(test, isl_union_map_copy(umap));
1973 sv = union_map_forall(test, &is_subset_of_identity);
1975 isl_union_map_free(test);
1980 int isl_union_map_is_injective(__isl_keep isl_union_map *umap)
1984 umap = isl_union_map_copy(umap);
1985 umap = isl_union_map_reverse(umap);
1986 in = isl_union_map_is_single_valued(umap);
1987 isl_union_map_free(umap);
1992 /* Represents a map that has a fixed value (v) for one of its
1994 * The map in this structure is not reference counted, so it
1995 * is only valid while the isl_union_map from which it was
1996 * obtained is still alive.
1998 struct isl_fixed_map {
2003 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2007 struct isl_fixed_map *v;
2009 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2012 for (i = 0; i < n; ++i)
2013 isl_int_init(v[i].v);
2017 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2023 for (i = 0; i < n; ++i)
2024 isl_int_clear(v[i].v);
2028 /* Compare the "v" field of two isl_fixed_map structs.
2030 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2032 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2033 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2035 return isl_int_cmp(e1->v, e2->v);
2038 /* Internal data structure used while checking whether all maps
2039 * in a union_map have a fixed value for a given output dimension.
2040 * v is the list of maps, with the fixed value for the dimension
2041 * n is the number of maps considered so far
2042 * pos is the output dimension under investigation
2044 struct isl_fixed_dim_data {
2045 struct isl_fixed_map *v;
2050 static int fixed_at_pos(__isl_keep isl_map *map, void *user)
2052 struct isl_fixed_dim_data *data = user;
2054 data->v[data->n].map = map;
2055 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2056 &data->v[data->n++].v);
2059 static int plain_injective_on_range(__isl_take isl_union_map *umap,
2060 int first, int n_range);
2062 /* Given a list of the maps, with their fixed values at output dimension "pos",
2063 * check whether the ranges of the maps form an obvious partition.
2065 * We first sort the maps according to their fixed values.
2066 * If all maps have a different value, then we know the ranges form
2068 * Otherwise, we collect the maps with the same fixed value and
2069 * check whether each such collection is obviously injective
2070 * based on later dimensions.
2072 static int separates(struct isl_fixed_map *v, int n,
2073 __isl_take isl_space *dim, int pos, int n_range)
2080 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2082 for (i = 0; i + 1 < n; ++i) {
2084 isl_union_map *part;
2087 for (j = i + 1; j < n; ++j)
2088 if (isl_int_ne(v[i].v, v[j].v))
2094 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2095 for (k = i; k < j; ++k)
2096 part = isl_union_map_add_map(part,
2097 isl_map_copy(v[k].map));
2099 injective = plain_injective_on_range(part, pos + 1, n_range);
2108 isl_space_free(dim);
2109 free_isl_fixed_map_array(v, n);
2112 isl_space_free(dim);
2113 free_isl_fixed_map_array(v, n);
2117 /* Check whether the maps in umap have obviously distinct ranges.
2118 * In particular, check for an output dimension in the range
2119 * [first,n_range) for which all maps have a fixed value
2120 * and then check if these values, possibly along with fixed values
2121 * at later dimensions, entail distinct ranges.
2123 static int plain_injective_on_range(__isl_take isl_union_map *umap,
2124 int first, int n_range)
2128 struct isl_fixed_dim_data data = { NULL };
2130 ctx = isl_union_map_get_ctx(umap);
2135 n = isl_union_map_n_map(umap);
2137 isl_union_map_free(umap);
2141 if (first >= n_range) {
2142 isl_union_map_free(umap);
2146 data.v = alloc_isl_fixed_map_array(ctx, n);
2150 for (data.pos = first; data.pos < n_range; ++data.pos) {
2156 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2161 dim = isl_union_map_get_space(umap);
2162 injective = separates(data.v, n, dim, data.pos, n_range);
2163 isl_union_map_free(umap);
2167 free_isl_fixed_map_array(data.v, n);
2168 isl_union_map_free(umap);
2172 free_isl_fixed_map_array(data.v, n);
2173 isl_union_map_free(umap);
2177 /* Check whether the maps in umap that map to subsets of "ran"
2178 * have obviously distinct ranges.
2180 static int plain_injective_on_range_wrap(__isl_keep isl_set *ran, void *user)
2182 isl_union_map *umap = user;
2184 umap = isl_union_map_copy(umap);
2185 umap = isl_union_map_intersect_range(umap,
2186 isl_union_set_from_set(isl_set_copy(ran)));
2187 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2190 /* Check if the given union_map is obviously injective.
2192 * In particular, we first check if all individual maps are obviously
2193 * injective and then check if all the ranges of these maps are
2194 * obviously disjoint.
2196 int isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2199 isl_union_map *univ;
2202 in = union_map_forall(umap, &isl_map_plain_is_injective);
2208 univ = isl_union_map_universe(isl_union_map_copy(umap));
2209 ran = isl_union_map_range(univ);
2211 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
2213 isl_union_set_free(ran);
2218 int isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
2222 sv = isl_union_map_is_single_valued(umap);
2226 return isl_union_map_is_injective(umap);
2229 static int zip_entry(void **entry, void *user)
2231 isl_map *map = *entry;
2232 isl_union_map **res = user;
2234 if (!isl_map_can_zip(map))
2237 *res = isl_union_map_add_map(*res, isl_map_zip(isl_map_copy(map)));
2242 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
2244 return cond_un_op(umap, &zip_entry);
2247 static int lift_entry(void **entry, void *user)
2249 isl_set *set = *entry;
2250 isl_union_set **res = user;
2252 *res = isl_union_set_add_set(*res, isl_set_lift(isl_set_copy(set)));
2257 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
2259 return cond_un_op(uset, &lift_entry);
2262 static int coefficients_entry(void **entry, void *user)
2264 isl_set *set = *entry;
2265 isl_union_set **res = user;
2267 set = isl_set_copy(set);
2268 set = isl_set_from_basic_set(isl_set_coefficients(set));
2269 *res = isl_union_set_add_set(*res, set);
2274 __isl_give isl_union_set *isl_union_set_coefficients(
2275 __isl_take isl_union_set *uset)
2284 ctx = isl_union_set_get_ctx(uset);
2285 dim = isl_space_set_alloc(ctx, 0, 0);
2286 res = isl_union_map_alloc(dim, uset->table.n);
2287 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2288 &coefficients_entry, &res) < 0)
2291 isl_union_set_free(uset);
2294 isl_union_set_free(uset);
2295 isl_union_set_free(res);
2299 static int solutions_entry(void **entry, void *user)
2301 isl_set *set = *entry;
2302 isl_union_set **res = user;
2304 set = isl_set_copy(set);
2305 set = isl_set_from_basic_set(isl_set_solutions(set));
2307 *res = isl_union_set_from_set(set);
2309 *res = isl_union_set_add_set(*res, set);
2317 __isl_give isl_union_set *isl_union_set_solutions(
2318 __isl_take isl_union_set *uset)
2320 isl_union_set *res = NULL;
2325 if (uset->table.n == 0) {
2326 res = isl_union_set_empty(isl_union_set_get_space(uset));
2327 isl_union_set_free(uset);
2331 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2332 &solutions_entry, &res) < 0)
2335 isl_union_set_free(uset);
2338 isl_union_set_free(uset);
2339 isl_union_set_free(res);