2 * Copyright 2010 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,
11 #include <isl_map_private.h>
16 #include <isl_dim_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl/union_set.h>
20 static __isl_give isl_union_map *isl_union_map_alloc(__isl_take isl_dim *dim,
28 umap = isl_calloc_type(dim->ctx, isl_union_map);
34 if (isl_hash_table_init(dim->ctx, &umap->table, size) < 0)
40 isl_union_map_free(umap);
44 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_dim *dim)
46 return isl_union_map_alloc(dim, 16);
49 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_dim *dim)
51 return isl_union_map_empty(dim);
54 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
56 return umap ? umap->dim->ctx : NULL;
59 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
61 return uset ? uset->dim->ctx : NULL;
64 __isl_give isl_dim *isl_union_map_get_dim(__isl_keep isl_union_map *umap)
68 return isl_dim_copy(umap->dim);
71 __isl_give isl_dim *isl_union_set_get_dim(__isl_keep isl_union_set *uset)
73 return isl_union_map_get_dim(uset);
76 static int free_umap_entry(void **entry, void *user)
78 isl_map *map = *entry;
83 static int add_map(__isl_take isl_map *map, void *user)
85 isl_union_map **umap = (isl_union_map **)user;
87 *umap = isl_union_map_add_map(*umap, map);
92 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
99 dup = isl_union_map_empty(isl_dim_copy(umap->dim));
100 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
104 isl_union_map_free(dup);
108 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
116 return isl_union_map_dup(umap);
119 struct isl_union_align {
124 static int align_entry(void **entry, void *user)
126 isl_map *map = *entry;
128 struct isl_union_align *data = user;
130 exp = isl_reordering_extend_dim(isl_reordering_copy(data->exp),
131 isl_map_get_dim(map));
133 data->res = isl_union_map_add_map(data->res,
134 isl_map_realign(isl_map_copy(map), exp));
139 /* Align the parameters of umap along those of model.
140 * The result has the parameters of model first, in the same order
141 * as they appear in model, followed by any remaining parameters of
142 * umap that do not appear in model.
144 __isl_give isl_union_map *isl_union_map_align_params(
145 __isl_take isl_union_map *umap, __isl_take isl_dim *model)
147 struct isl_union_align data = { NULL, NULL };
152 if (isl_dim_match(umap->dim, isl_dim_param, model, isl_dim_param)) {
157 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
161 data.res = isl_union_map_alloc(isl_dim_copy(data.exp->dim),
163 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
164 &align_entry, &data) < 0)
167 isl_reordering_free(data.exp);
168 isl_union_map_free(umap);
172 isl_reordering_free(data.exp);
173 isl_union_map_free(umap);
174 isl_union_map_free(data.res);
179 __isl_give isl_union_set *isl_union_set_align_params(
180 __isl_take isl_union_set *uset, __isl_take isl_dim *model)
182 return isl_union_map_align_params(uset, model);
185 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
186 __isl_take isl_union_map *umap2)
188 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_dim(umap2));
189 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_dim(umap1));
191 umap1 = isl_union_map_cow(umap1);
193 if (!umap1 || !umap2)
196 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
199 isl_union_map_free(umap2);
203 isl_union_map_free(umap1);
204 isl_union_map_free(umap2);
208 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
209 __isl_take isl_union_set *uset2)
211 return isl_union_map_union(uset1, uset2);
214 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
223 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
225 return isl_union_map_copy(uset);
228 void isl_union_map_free(__isl_take isl_union_map *umap)
236 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
237 &free_umap_entry, NULL);
238 isl_hash_table_clear(&umap->table);
239 isl_dim_free(umap->dim);
243 void isl_union_set_free(__isl_take isl_union_set *uset)
245 isl_union_map_free(uset);
248 static int has_dim(const void *entry, const void *val)
250 isl_map *map = (isl_map *)entry;
251 isl_dim *dim = (isl_dim *)val;
253 return isl_dim_equal(map->dim, dim);
256 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
257 __isl_take isl_map *map)
260 struct isl_hash_table_entry *entry;
262 if (isl_map_fast_is_empty(map)) {
267 umap = isl_union_map_cow(umap);
272 isl_assert(map->ctx, isl_dim_match(map->dim, isl_dim_param, umap->dim,
273 isl_dim_param), goto error);
275 hash = isl_dim_get_hash(map->dim);
276 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
277 &has_dim, map->dim, 1);
284 entry->data = isl_map_union(entry->data, isl_map_copy(map));
293 isl_union_map_free(umap);
297 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
298 __isl_take isl_set *set)
300 return isl_union_map_add_map(uset, (isl_map *)set);
303 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
311 dim = isl_map_get_dim(map);
312 dim = isl_dim_drop(dim, isl_dim_in, 0, isl_dim_size(dim, isl_dim_in));
313 dim = isl_dim_drop(dim, isl_dim_out, 0, isl_dim_size(dim, isl_dim_out));
314 umap = isl_union_map_empty(dim);
315 umap = isl_union_map_add_map(umap, map);
320 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
322 return isl_union_map_from_map((isl_map *)set);
325 struct isl_union_map_foreach_data
327 int (*fn)(__isl_take isl_map *map, void *user);
331 static int call_on_copy(void **entry, void *user)
333 isl_map *map = *entry;
334 struct isl_union_map_foreach_data *data;
335 data = (struct isl_union_map_foreach_data *)user;
337 return data->fn(isl_map_copy(map), data->user);
340 int isl_union_map_n_map(__isl_keep isl_union_map *umap)
342 return umap ? umap->table.n : 0;
345 int isl_union_set_n_set(__isl_keep isl_union_set *uset)
347 return uset ? uset->table.n : 0;
350 int isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
351 int (*fn)(__isl_take isl_map *map, void *user), void *user)
353 struct isl_union_map_foreach_data data = { fn, user };
358 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
359 &call_on_copy, &data);
362 static int copy_map(void **entry, void *user)
364 isl_map *map = *entry;
365 isl_map **map_p = user;
367 *map_p = isl_map_copy(map);
372 __isl_give isl_map *isl_union_map_copy_map(__isl_keep isl_union_map *umap)
376 if (!umap || umap->table.n == 0)
379 isl_hash_table_foreach(umap->dim->ctx, &umap->table, ©_map, &map);
384 __isl_give isl_set *isl_union_set_copy_set(__isl_keep isl_union_set *uset)
386 return isl_union_map_copy_map(uset);
389 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
390 __isl_take isl_dim *dim)
393 struct isl_hash_table_entry *entry;
398 hash = isl_dim_get_hash(dim);
399 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
402 return isl_map_empty(dim);
404 return isl_map_copy(entry->data);
410 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
411 __isl_take isl_dim *dim)
413 return (isl_set *)isl_union_map_extract_map(uset, dim);
416 /* Check if umap contains a map in the given space.
418 __isl_give int isl_union_map_contains(__isl_keep isl_union_map *umap,
419 __isl_keep isl_dim *dim)
422 struct isl_hash_table_entry *entry;
427 hash = isl_dim_get_hash(dim);
428 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
433 __isl_give int isl_union_set_contains(__isl_keep isl_union_set *uset,
434 __isl_keep isl_dim *dim)
436 return isl_union_map_contains(uset, dim);
439 int isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
440 int (*fn)(__isl_take isl_set *set, void *user), void *user)
442 return isl_union_map_foreach_map(uset,
443 (int(*)(__isl_take isl_map *, void*))fn, user);
446 struct isl_union_set_foreach_point_data {
447 int (*fn)(__isl_take isl_point *pnt, void *user);
451 static int foreach_point(__isl_take isl_set *set, void *user)
453 struct isl_union_set_foreach_point_data *data = user;
456 r = isl_set_foreach_point(set, data->fn, data->user);
462 int isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
463 int (*fn)(__isl_take isl_point *pnt, void *user), void *user)
465 struct isl_union_set_foreach_point_data data = { fn, user };
466 return isl_union_set_foreach_set(uset, &foreach_point, &data);
469 struct isl_union_map_gen_bin_data {
470 isl_union_map *umap2;
474 static int subtract_entry(void **entry, void *user)
476 struct isl_union_map_gen_bin_data *data = user;
478 struct isl_hash_table_entry *entry2;
479 isl_map *map = *entry;
481 hash = isl_dim_get_hash(map->dim);
482 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
483 hash, &has_dim, map->dim, 0);
484 map = isl_map_copy(map);
487 map = isl_map_subtract(map, isl_map_copy(entry2->data));
489 empty = isl_map_is_empty(map);
499 data->res = isl_union_map_add_map(data->res, map);
504 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
505 __isl_take isl_union_map *umap2, int (*fn)(void **, void *))
507 struct isl_union_map_gen_bin_data data = { NULL, NULL };
509 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_dim(umap2));
510 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_dim(umap1));
512 if (!umap1 || !umap2)
516 data.res = isl_union_map_alloc(isl_dim_copy(umap1->dim),
518 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
522 isl_union_map_free(umap1);
523 isl_union_map_free(umap2);
526 isl_union_map_free(umap1);
527 isl_union_map_free(umap2);
528 isl_union_map_free(data.res);
532 __isl_give isl_union_map *isl_union_map_subtract(
533 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
535 return gen_bin_op(umap1, umap2, &subtract_entry);
538 __isl_give isl_union_set *isl_union_set_subtract(
539 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
541 return isl_union_map_subtract(uset1, uset2);
544 struct isl_union_map_match_bin_data {
545 isl_union_map *umap2;
547 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
550 static int match_bin_entry(void **entry, void *user)
552 struct isl_union_map_match_bin_data *data = user;
554 struct isl_hash_table_entry *entry2;
555 isl_map *map = *entry;
558 hash = isl_dim_get_hash(map->dim);
559 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
560 hash, &has_dim, map->dim, 0);
564 map = isl_map_copy(map);
565 map = data->fn(map, isl_map_copy(entry2->data));
567 empty = isl_map_is_empty(map);
577 data->res = isl_union_map_add_map(data->res, map);
582 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
583 __isl_take isl_union_map *umap2,
584 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
586 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
588 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_dim(umap2));
589 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_dim(umap1));
591 if (!umap1 || !umap2)
595 data.res = isl_union_map_alloc(isl_dim_copy(umap1->dim),
597 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
598 &match_bin_entry, &data) < 0)
601 isl_union_map_free(umap1);
602 isl_union_map_free(umap2);
605 isl_union_map_free(umap1);
606 isl_union_map_free(umap2);
607 isl_union_map_free(data.res);
611 __isl_give isl_union_map *isl_union_map_intersect(
612 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
614 return match_bin_op(umap1, umap2, &isl_map_intersect);
617 __isl_give isl_union_set *isl_union_set_intersect(
618 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
620 return isl_union_map_intersect(uset1, uset2);
623 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
624 __isl_take isl_union_map *context)
626 return match_bin_op(umap, context, &isl_map_gist);
629 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
630 __isl_take isl_union_set *context)
632 return isl_union_map_gist(uset, context);
635 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
636 __isl_take isl_map *set2)
638 return isl_set_lex_le_set((isl_set *)set1, (isl_set *)set2);
641 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
642 __isl_take isl_map *set2)
644 return isl_set_lex_lt_set((isl_set *)set1, (isl_set *)set2);
647 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
648 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
650 return match_bin_op(uset1, uset2, &lex_lt_set);
653 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
654 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
656 return match_bin_op(uset1, uset2, &lex_le_set);
659 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
660 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
662 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
665 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
666 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
668 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
671 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
672 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
674 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
677 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
678 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
680 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
683 static int intersect_domain_entry(void **entry, void *user)
685 struct isl_union_map_gen_bin_data *data = user;
687 struct isl_hash_table_entry *entry2;
689 isl_map *map = *entry;
692 dim = isl_map_get_dim(map);
693 dim = isl_dim_domain(dim);
694 hash = isl_dim_get_hash(dim);
695 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
696 hash, &has_dim, dim, 0);
701 map = isl_map_copy(map);
702 map = isl_map_intersect_domain(map, isl_set_copy(entry2->data));
704 empty = isl_map_is_empty(map);
714 data->res = isl_union_map_add_map(data->res, map);
719 __isl_give isl_union_map *isl_union_map_intersect_domain(
720 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
722 return gen_bin_op(umap, uset, &intersect_domain_entry);
725 static int intersect_range_entry(void **entry, void *user)
727 struct isl_union_map_gen_bin_data *data = user;
729 struct isl_hash_table_entry *entry2;
731 isl_map *map = *entry;
734 dim = isl_map_get_dim(map);
735 dim = isl_dim_range(dim);
736 hash = isl_dim_get_hash(dim);
737 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
738 hash, &has_dim, dim, 0);
743 map = isl_map_copy(map);
744 map = isl_map_intersect_range(map, isl_set_copy(entry2->data));
746 empty = isl_map_is_empty(map);
756 data->res = isl_union_map_add_map(data->res, map);
761 __isl_give isl_union_map *isl_union_map_intersect_range(
762 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
764 return gen_bin_op(umap, uset, &intersect_range_entry);
767 struct isl_union_map_bin_data {
768 isl_union_map *umap2;
771 int (*fn)(void **entry, void *user);
774 static int apply_range_entry(void **entry, void *user)
776 struct isl_union_map_bin_data *data = user;
777 isl_map *map2 = *entry;
780 if (!isl_dim_tuple_match(data->map->dim, isl_dim_out,
781 map2->dim, isl_dim_in))
784 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
786 empty = isl_map_is_empty(map2);
796 data->res = isl_union_map_add_map(data->res, map2);
801 static int bin_entry(void **entry, void *user)
803 struct isl_union_map_bin_data *data = user;
804 isl_map *map = *entry;
807 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
814 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
815 __isl_take isl_union_map *umap2, int (*fn)(void **entry, void *user))
817 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
819 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_dim(umap2));
820 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_dim(umap1));
822 if (!umap1 || !umap2)
826 data.res = isl_union_map_alloc(isl_dim_copy(umap1->dim),
828 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
829 &bin_entry, &data) < 0)
832 isl_union_map_free(umap1);
833 isl_union_map_free(umap2);
836 isl_union_map_free(umap1);
837 isl_union_map_free(umap2);
838 isl_union_map_free(data.res);
842 __isl_give isl_union_map *isl_union_map_apply_range(
843 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
845 return bin_op(umap1, umap2, &apply_range_entry);
848 __isl_give isl_union_map *isl_union_map_apply_domain(
849 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
851 umap1 = isl_union_map_reverse(umap1);
852 umap1 = isl_union_map_apply_range(umap1, umap2);
853 return isl_union_map_reverse(umap1);
856 __isl_give isl_union_set *isl_union_set_apply(
857 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
859 return isl_union_map_apply_range(uset, umap);
862 static int map_lex_lt_entry(void **entry, void *user)
864 struct isl_union_map_bin_data *data = user;
865 isl_map *map2 = *entry;
867 if (!isl_dim_tuple_match(data->map->dim, isl_dim_out,
868 map2->dim, isl_dim_out))
871 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
873 data->res = isl_union_map_add_map(data->res, map2);
878 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
879 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
881 return bin_op(umap1, umap2, &map_lex_lt_entry);
884 static int map_lex_le_entry(void **entry, void *user)
886 struct isl_union_map_bin_data *data = user;
887 isl_map *map2 = *entry;
889 if (!isl_dim_tuple_match(data->map->dim, isl_dim_out,
890 map2->dim, isl_dim_out))
893 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
895 data->res = isl_union_map_add_map(data->res, map2);
900 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
901 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
903 return bin_op(umap1, umap2, &map_lex_le_entry);
906 static int product_entry(void **entry, void *user)
908 struct isl_union_map_bin_data *data = user;
909 isl_map *map2 = *entry;
911 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
913 data->res = isl_union_map_add_map(data->res, map2);
918 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
919 __isl_take isl_union_map *umap2)
921 return bin_op(umap1, umap2, &product_entry);
924 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
925 __isl_take isl_union_set *uset2)
927 return isl_union_map_product(uset1, uset2);
930 static int range_product_entry(void **entry, void *user)
932 struct isl_union_map_bin_data *data = user;
933 isl_map *map2 = *entry;
935 map2 = isl_map_range_product(isl_map_copy(data->map),
938 data->res = isl_union_map_add_map(data->res, map2);
943 __isl_give isl_union_map *isl_union_map_range_product(
944 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
946 return bin_op(umap1, umap2, &range_product_entry);
949 __isl_give isl_union_map *isl_union_map_from_range(
950 __isl_take isl_union_set *uset)
955 __isl_give isl_union_map *isl_union_map_from_domain(
956 __isl_take isl_union_set *uset)
958 return isl_union_map_reverse(isl_union_map_from_range(uset));
961 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
962 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
964 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
965 isl_union_map_from_range(range));
968 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
969 int (*fn)(void **, void *))
971 umap = isl_union_map_cow(umap);
975 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, NULL) < 0)
980 isl_union_map_free(umap);
984 static int affine_entry(void **entry, void *user)
986 isl_map **map = (isl_map **)entry;
988 *map = isl_map_from_basic_map(isl_map_affine_hull(*map));
990 return *map ? 0 : -1;
993 __isl_give isl_union_map *isl_union_map_affine_hull(
994 __isl_take isl_union_map *umap)
996 return un_op(umap, &affine_entry);
999 __isl_give isl_union_set *isl_union_set_affine_hull(
1000 __isl_take isl_union_set *uset)
1002 return isl_union_map_affine_hull(uset);
1005 static int polyhedral_entry(void **entry, void *user)
1007 isl_map **map = (isl_map **)entry;
1009 *map = isl_map_from_basic_map(isl_map_polyhedral_hull(*map));
1011 return *map ? 0 : -1;
1014 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1015 __isl_take isl_union_map *umap)
1017 return un_op(umap, &polyhedral_entry);
1020 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1021 __isl_take isl_union_set *uset)
1023 return isl_union_map_polyhedral_hull(uset);
1026 static int simple_entry(void **entry, void *user)
1028 isl_map **map = (isl_map **)entry;
1030 *map = isl_map_from_basic_map(isl_map_simple_hull(*map));
1032 return *map ? 0 : -1;
1035 __isl_give isl_union_map *isl_union_map_simple_hull(
1036 __isl_take isl_union_map *umap)
1038 return un_op(umap, &simple_entry);
1041 __isl_give isl_union_set *isl_union_set_simple_hull(
1042 __isl_take isl_union_set *uset)
1044 return isl_union_map_simple_hull(uset);
1047 static int inplace_entry(void **entry, void *user)
1049 __isl_give isl_map *(*fn)(__isl_take isl_map *);
1050 isl_map **map = (isl_map **)entry;
1053 fn = *(__isl_give isl_map *(**)(__isl_take isl_map *)) user;
1054 copy = fn(isl_map_copy(*map));
1064 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1065 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1070 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1071 &inplace_entry, &fn) < 0)
1076 isl_union_map_free(umap);
1080 __isl_give isl_union_map *isl_union_map_coalesce(
1081 __isl_take isl_union_map *umap)
1083 return inplace(umap, &isl_map_coalesce);
1086 __isl_give isl_union_set *isl_union_set_coalesce(
1087 __isl_take isl_union_set *uset)
1089 return isl_union_map_coalesce(uset);
1092 __isl_give isl_union_map *isl_union_map_detect_equalities(
1093 __isl_take isl_union_map *umap)
1095 return inplace(umap, &isl_map_detect_equalities);
1098 __isl_give isl_union_set *isl_union_set_detect_equalities(
1099 __isl_take isl_union_set *uset)
1101 return isl_union_map_detect_equalities(uset);
1104 __isl_give isl_union_map *isl_union_map_compute_divs(
1105 __isl_take isl_union_map *umap)
1107 return inplace(umap, &isl_map_compute_divs);
1110 __isl_give isl_union_set *isl_union_set_compute_divs(
1111 __isl_take isl_union_set *uset)
1113 return isl_union_map_compute_divs(uset);
1116 static int lexmin_entry(void **entry, void *user)
1118 isl_map **map = (isl_map **)entry;
1120 *map = isl_map_lexmin(*map);
1122 return *map ? 0 : -1;
1125 __isl_give isl_union_map *isl_union_map_lexmin(
1126 __isl_take isl_union_map *umap)
1128 return un_op(umap, &lexmin_entry);
1131 __isl_give isl_union_set *isl_union_set_lexmin(
1132 __isl_take isl_union_set *uset)
1134 return isl_union_map_lexmin(uset);
1137 static int lexmax_entry(void **entry, void *user)
1139 isl_map **map = (isl_map **)entry;
1141 *map = isl_map_lexmax(*map);
1143 return *map ? 0 : -1;
1146 __isl_give isl_union_map *isl_union_map_lexmax(
1147 __isl_take isl_union_map *umap)
1149 return un_op(umap, &lexmax_entry);
1152 __isl_give isl_union_set *isl_union_set_lexmax(
1153 __isl_take isl_union_set *uset)
1155 return isl_union_map_lexmax(uset);
1158 static __isl_give isl_union_set *cond_un_op(__isl_take isl_union_map *umap,
1159 int (*fn)(void **, void *))
1166 res = isl_union_map_alloc(isl_dim_copy(umap->dim), umap->table.n);
1167 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, &res) < 0)
1170 isl_union_map_free(umap);
1173 isl_union_map_free(umap);
1174 isl_union_set_free(res);
1178 static int reverse_entry(void **entry, void *user)
1180 isl_map *map = *entry;
1181 isl_union_map **res = user;
1183 *res = isl_union_map_add_map(*res, isl_map_reverse(isl_map_copy(map)));
1188 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1190 return cond_un_op(umap, &reverse_entry);
1193 static int domain_entry(void **entry, void *user)
1195 isl_map *map = *entry;
1196 isl_union_set **res = user;
1198 *res = isl_union_set_add_set(*res, isl_map_domain(isl_map_copy(map)));
1203 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1205 return cond_un_op(umap, &domain_entry);
1208 static int range_entry(void **entry, void *user)
1210 isl_map *map = *entry;
1211 isl_union_set **res = user;
1213 *res = isl_union_set_add_set(*res, isl_map_range(isl_map_copy(map)));
1218 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1220 return cond_un_op(umap, &range_entry);
1223 static int domain_map_entry(void **entry, void *user)
1225 isl_map *map = *entry;
1226 isl_union_set **res = user;
1228 *res = isl_union_map_add_map(*res,
1229 isl_map_domain_map(isl_map_copy(map)));
1234 __isl_give isl_union_map *isl_union_map_domain_map(
1235 __isl_take isl_union_map *umap)
1237 return cond_un_op(umap, &domain_map_entry);
1240 static int range_map_entry(void **entry, void *user)
1242 isl_map *map = *entry;
1243 isl_union_set **res = user;
1245 *res = isl_union_map_add_map(*res,
1246 isl_map_range_map(isl_map_copy(map)));
1251 __isl_give isl_union_map *isl_union_map_range_map(
1252 __isl_take isl_union_map *umap)
1254 return cond_un_op(umap, &range_map_entry);
1257 static int deltas_entry(void **entry, void *user)
1259 isl_map *map = *entry;
1260 isl_union_set **res = user;
1262 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1265 *res = isl_union_set_add_set(*res, isl_map_deltas(isl_map_copy(map)));
1270 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
1272 return cond_un_op(umap, &deltas_entry);
1275 static int deltas_map_entry(void **entry, void *user)
1277 isl_map *map = *entry;
1278 isl_union_map **res = user;
1280 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1283 *res = isl_union_map_add_map(*res,
1284 isl_map_deltas_map(isl_map_copy(map)));
1289 __isl_give isl_union_map *isl_union_map_deltas_map(
1290 __isl_take isl_union_map *umap)
1292 return cond_un_op(umap, &deltas_map_entry);
1295 static int identity_entry(void **entry, void *user)
1297 isl_set *set = *entry;
1298 isl_union_map **res = user;
1300 *res = isl_union_map_add_map(*res, isl_set_identity(isl_set_copy(set)));
1305 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
1307 return cond_un_op(uset, &identity_entry);
1310 static int unwrap_entry(void **entry, void *user)
1312 isl_set *set = *entry;
1313 isl_union_set **res = user;
1315 if (!isl_set_is_wrapping(set))
1318 *res = isl_union_map_add_map(*res, isl_set_unwrap(isl_set_copy(set)));
1323 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
1325 return cond_un_op(uset, &unwrap_entry);
1328 static int wrap_entry(void **entry, void *user)
1330 isl_map *map = *entry;
1331 isl_union_set **res = user;
1333 *res = isl_union_set_add_set(*res, isl_map_wrap(isl_map_copy(map)));
1338 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
1340 return cond_un_op(umap, &wrap_entry);
1343 struct isl_union_map_is_subset_data {
1344 isl_union_map *umap2;
1348 static int is_subset_entry(void **entry, void *user)
1350 struct isl_union_map_is_subset_data *data = user;
1352 struct isl_hash_table_entry *entry2;
1353 isl_map *map = *entry;
1355 hash = isl_dim_get_hash(map->dim);
1356 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1357 hash, &has_dim, map->dim, 0);
1359 data->is_subset = 0;
1363 data->is_subset = isl_map_is_subset(map, entry2->data);
1364 if (data->is_subset < 0 || !data->is_subset)
1370 int isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
1371 __isl_keep isl_union_map *umap2)
1373 struct isl_union_map_is_subset_data data = { NULL, 1 };
1375 umap1 = isl_union_map_copy(umap1);
1376 umap2 = isl_union_map_copy(umap2);
1377 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_dim(umap2));
1378 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_dim(umap1));
1380 if (!umap1 || !umap2)
1384 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1385 &is_subset_entry, &data) < 0 &&
1389 isl_union_map_free(umap1);
1390 isl_union_map_free(umap2);
1392 return data.is_subset;
1394 isl_union_map_free(umap1);
1395 isl_union_map_free(umap2);
1399 int isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
1400 __isl_keep isl_union_set *uset2)
1402 return isl_union_map_is_subset(uset1, uset2);
1405 int isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
1406 __isl_keep isl_union_map *umap2)
1410 if (!umap1 || !umap2)
1412 is_subset = isl_union_map_is_subset(umap1, umap2);
1415 is_subset = isl_union_map_is_subset(umap2, umap1);
1419 int isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
1420 __isl_keep isl_union_set *uset2)
1422 return isl_union_map_is_equal(uset1, uset2);
1425 int isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
1426 __isl_keep isl_union_map *umap2)
1430 if (!umap1 || !umap2)
1432 is_subset = isl_union_map_is_subset(umap1, umap2);
1435 is_subset = isl_union_map_is_subset(umap2, umap1);
1436 if (is_subset == -1)
1441 int isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
1442 __isl_keep isl_union_set *uset2)
1444 return isl_union_map_is_strict_subset(uset1, uset2);
1447 static int sample_entry(void **entry, void *user)
1449 isl_basic_map **sample = (isl_basic_map **)user;
1450 isl_map *map = *entry;
1452 *sample = isl_map_sample(isl_map_copy(map));
1455 if (!isl_basic_map_fast_is_empty(*sample))
1460 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
1462 isl_basic_map *sample = NULL;
1467 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1468 &sample_entry, &sample) < 0 &&
1473 sample = isl_basic_map_empty(isl_union_map_get_dim(umap));
1475 isl_union_map_free(umap);
1479 isl_union_map_free(umap);
1483 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
1485 return (isl_basic_set *)isl_union_map_sample(uset);
1488 struct isl_forall_data {
1490 int (*fn)(__isl_keep isl_map *map);
1493 static int forall_entry(void **entry, void *user)
1495 struct isl_forall_data *data = user;
1496 isl_map *map = *entry;
1498 data->res = data->fn(map);
1508 static int union_map_forall(__isl_keep isl_union_map *umap,
1509 int (*fn)(__isl_keep isl_map *map))
1511 struct isl_forall_data data = { 1, fn };
1516 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1517 &forall_entry, &data) < 0 && data.res)
1523 int isl_union_map_is_empty(__isl_keep isl_union_map *umap)
1525 return union_map_forall(umap, &isl_map_is_empty);
1528 int isl_union_set_is_empty(__isl_keep isl_union_set *uset)
1530 return isl_union_map_is_empty(uset);
1533 static int is_subset_of_identity(__isl_keep isl_map *map)
1542 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1545 dim = isl_map_get_dim(map);
1546 id = isl_map_identity(dim);
1548 is_subset = isl_map_is_subset(map, id);
1555 /* Check if the given map is single-valued.
1560 * and check if the result is a subset of the identity mapping.
1562 int isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
1564 isl_union_map *test;
1567 if (isl_union_map_n_map(umap) == 1) {
1568 isl_map *map = isl_union_map_copy_map(umap);
1569 sv = isl_map_is_single_valued(map);
1574 test = isl_union_map_reverse(isl_union_map_copy(umap));
1575 test = isl_union_map_apply_range(test, isl_union_map_copy(umap));
1577 sv = union_map_forall(test, &is_subset_of_identity);
1579 isl_union_map_free(test);
1584 int isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
1588 sv = isl_union_map_is_single_valued(umap);
1592 umap = isl_union_map_copy(umap);
1593 umap = isl_union_map_reverse(umap);
1594 sv = isl_union_map_is_single_valued(umap);
1595 isl_union_map_free(umap);
1600 static int zip_entry(void **entry, void *user)
1602 isl_map *map = *entry;
1603 isl_union_map **res = user;
1605 if (!isl_map_can_zip(map))
1608 *res = isl_union_map_add_map(*res, isl_map_zip(isl_map_copy(map)));
1613 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
1615 return cond_un_op(umap, &zip_entry);
1618 static int lift_entry(void **entry, void *user)
1620 isl_set *set = *entry;
1621 isl_union_set **res = user;
1623 *res = isl_union_set_add_set(*res, isl_set_lift(isl_set_copy(set)));
1628 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
1630 return cond_un_op(uset, &lift_entry);
1633 static int coefficients_entry(void **entry, void *user)
1635 isl_set *set = *entry;
1636 isl_union_set **res = user;
1638 set = isl_set_copy(set);
1639 set = isl_set_from_basic_set(isl_set_coefficients(set));
1640 *res = isl_union_set_add_set(*res, set);
1645 __isl_give isl_union_set *isl_union_set_coefficients(
1646 __isl_take isl_union_set *uset)
1655 ctx = isl_union_set_get_ctx(uset);
1656 dim = isl_dim_set_alloc(ctx, 0, 0);
1657 res = isl_union_map_alloc(dim, uset->table.n);
1658 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
1659 &coefficients_entry, &res) < 0)
1662 isl_union_set_free(uset);
1665 isl_union_set_free(uset);
1666 isl_union_set_free(res);
1670 static int solutions_entry(void **entry, void *user)
1672 isl_set *set = *entry;
1673 isl_union_set **res = user;
1675 set = isl_set_copy(set);
1676 set = isl_set_from_basic_set(isl_set_solutions(set));
1678 *res = isl_union_set_from_set(set);
1680 *res = isl_union_set_add_set(*res, set);
1688 __isl_give isl_union_set *isl_union_set_solutions(
1689 __isl_take isl_union_set *uset)
1693 isl_union_set *res = NULL;
1698 if (uset->table.n == 0) {
1699 res = isl_union_set_empty(isl_union_set_get_dim(uset));
1700 isl_union_set_free(uset);
1704 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
1705 &solutions_entry, &res) < 0)
1708 isl_union_set_free(uset);
1711 isl_union_set_free(uset);
1712 isl_union_set_free(res);