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 struct isl_union_map_match_bin_data
{
650 isl_union_map
*umap2
;
652 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*);
655 static int match_bin_entry(void **entry
, void *user
)
657 struct isl_union_map_match_bin_data
*data
= user
;
659 struct isl_hash_table_entry
*entry2
;
660 isl_map
*map
= *entry
;
663 hash
= isl_space_get_hash(map
->dim
);
664 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
665 hash
, &has_dim
, map
->dim
, 0);
669 map
= isl_map_copy(map
);
670 map
= data
->fn(map
, isl_map_copy(entry2
->data
));
672 empty
= isl_map_is_empty(map
);
682 data
->res
= isl_union_map_add_map(data
->res
, map
);
687 static __isl_give isl_union_map
*match_bin_op(__isl_take isl_union_map
*umap1
,
688 __isl_take isl_union_map
*umap2
,
689 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*))
691 struct isl_union_map_match_bin_data data
= { NULL
, NULL
, fn
};
693 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
694 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
696 if (!umap1
|| !umap2
)
700 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
702 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
703 &match_bin_entry
, &data
) < 0)
706 isl_union_map_free(umap1
);
707 isl_union_map_free(umap2
);
710 isl_union_map_free(umap1
);
711 isl_union_map_free(umap2
);
712 isl_union_map_free(data
.res
);
716 __isl_give isl_union_map
*isl_union_map_intersect(
717 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
719 return match_bin_op(umap1
, umap2
, &isl_map_intersect
);
722 /* Compute the intersection of the two union_sets.
723 * As a special case, if exactly one of the two union_sets
724 * is a parameter domain, then intersect the parameter domain
725 * of the other one with this set.
727 __isl_give isl_union_set
*isl_union_set_intersect(
728 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
732 p1
= isl_union_set_is_params(uset1
);
733 p2
= isl_union_set_is_params(uset2
);
734 if (p1
< 0 || p2
< 0)
737 return union_map_intersect_params(uset1
, uset2
);
739 return union_map_intersect_params(uset2
, uset1
);
740 return isl_union_map_intersect(uset1
, uset2
);
742 isl_union_set_free(uset1
);
743 isl_union_set_free(uset2
);
747 __isl_give isl_union_map
*isl_union_map_gist(__isl_take isl_union_map
*umap
,
748 __isl_take isl_union_map
*context
)
750 return match_bin_op(umap
, context
, &isl_map_gist
);
753 __isl_give isl_union_set
*isl_union_set_gist(__isl_take isl_union_set
*uset
,
754 __isl_take isl_union_set
*context
)
756 return isl_union_map_gist(uset
, context
);
759 static __isl_give isl_map
*lex_le_set(__isl_take isl_map
*set1
,
760 __isl_take isl_map
*set2
)
762 return isl_set_lex_le_set((isl_set
*)set1
, (isl_set
*)set2
);
765 static __isl_give isl_map
*lex_lt_set(__isl_take isl_map
*set1
,
766 __isl_take isl_map
*set2
)
768 return isl_set_lex_lt_set((isl_set
*)set1
, (isl_set
*)set2
);
771 __isl_give isl_union_map
*isl_union_set_lex_lt_union_set(
772 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
774 return match_bin_op(uset1
, uset2
, &lex_lt_set
);
777 __isl_give isl_union_map
*isl_union_set_lex_le_union_set(
778 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
780 return match_bin_op(uset1
, uset2
, &lex_le_set
);
783 __isl_give isl_union_map
*isl_union_set_lex_gt_union_set(
784 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
786 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2
, uset1
));
789 __isl_give isl_union_map
*isl_union_set_lex_ge_union_set(
790 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
792 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2
, uset1
));
795 __isl_give isl_union_map
*isl_union_map_lex_gt_union_map(
796 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
798 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2
, umap1
));
801 __isl_give isl_union_map
*isl_union_map_lex_ge_union_map(
802 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
804 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2
, umap1
));
807 static int intersect_domain_entry(void **entry
, void *user
)
809 struct isl_union_map_gen_bin_data
*data
= user
;
811 struct isl_hash_table_entry
*entry2
;
813 isl_map
*map
= *entry
;
816 dim
= isl_map_get_space(map
);
817 dim
= isl_space_domain(dim
);
818 hash
= isl_space_get_hash(dim
);
819 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
820 hash
, &has_dim
, dim
, 0);
825 map
= isl_map_copy(map
);
826 map
= isl_map_intersect_domain(map
, isl_set_copy(entry2
->data
));
828 empty
= isl_map_is_empty(map
);
838 data
->res
= isl_union_map_add_map(data
->res
, map
);
843 __isl_give isl_union_map
*isl_union_map_intersect_domain(
844 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
846 return gen_bin_op(umap
, uset
, &intersect_domain_entry
);
849 static int intersect_range_entry(void **entry
, void *user
)
851 struct isl_union_map_gen_bin_data
*data
= user
;
853 struct isl_hash_table_entry
*entry2
;
855 isl_map
*map
= *entry
;
858 dim
= isl_map_get_space(map
);
859 dim
= isl_space_range(dim
);
860 hash
= isl_space_get_hash(dim
);
861 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
862 hash
, &has_dim
, dim
, 0);
867 map
= isl_map_copy(map
);
868 map
= isl_map_intersect_range(map
, isl_set_copy(entry2
->data
));
870 empty
= isl_map_is_empty(map
);
880 data
->res
= isl_union_map_add_map(data
->res
, map
);
885 __isl_give isl_union_map
*isl_union_map_intersect_range(
886 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
888 return gen_bin_op(umap
, uset
, &intersect_range_entry
);
891 struct isl_union_map_bin_data
{
892 isl_union_map
*umap2
;
895 int (*fn
)(void **entry
, void *user
);
898 static int apply_range_entry(void **entry
, void *user
)
900 struct isl_union_map_bin_data
*data
= user
;
901 isl_map
*map2
= *entry
;
904 if (!isl_space_tuple_match(data
->map
->dim
, isl_dim_out
,
905 map2
->dim
, isl_dim_in
))
908 map2
= isl_map_apply_range(isl_map_copy(data
->map
), isl_map_copy(map2
));
910 empty
= isl_map_is_empty(map2
);
920 data
->res
= isl_union_map_add_map(data
->res
, map2
);
925 static int bin_entry(void **entry
, void *user
)
927 struct isl_union_map_bin_data
*data
= user
;
928 isl_map
*map
= *entry
;
931 if (isl_hash_table_foreach(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
938 static __isl_give isl_union_map
*bin_op(__isl_take isl_union_map
*umap1
,
939 __isl_take isl_union_map
*umap2
, int (*fn
)(void **entry
, void *user
))
941 struct isl_union_map_bin_data data
= { NULL
, NULL
, NULL
, fn
};
943 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
944 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
946 if (!umap1
|| !umap2
)
950 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
952 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
953 &bin_entry
, &data
) < 0)
956 isl_union_map_free(umap1
);
957 isl_union_map_free(umap2
);
960 isl_union_map_free(umap1
);
961 isl_union_map_free(umap2
);
962 isl_union_map_free(data
.res
);
966 __isl_give isl_union_map
*isl_union_map_apply_range(
967 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
969 return bin_op(umap1
, umap2
, &apply_range_entry
);
972 __isl_give isl_union_map
*isl_union_map_apply_domain(
973 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
975 umap1
= isl_union_map_reverse(umap1
);
976 umap1
= isl_union_map_apply_range(umap1
, umap2
);
977 return isl_union_map_reverse(umap1
);
980 __isl_give isl_union_set
*isl_union_set_apply(
981 __isl_take isl_union_set
*uset
, __isl_take isl_union_map
*umap
)
983 return isl_union_map_apply_range(uset
, umap
);
986 static int map_lex_lt_entry(void **entry
, void *user
)
988 struct isl_union_map_bin_data
*data
= user
;
989 isl_map
*map2
= *entry
;
991 if (!isl_space_tuple_match(data
->map
->dim
, isl_dim_out
,
992 map2
->dim
, isl_dim_out
))
995 map2
= isl_map_lex_lt_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
997 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1002 __isl_give isl_union_map
*isl_union_map_lex_lt_union_map(
1003 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1005 return bin_op(umap1
, umap2
, &map_lex_lt_entry
);
1008 static int map_lex_le_entry(void **entry
, void *user
)
1010 struct isl_union_map_bin_data
*data
= user
;
1011 isl_map
*map2
= *entry
;
1013 if (!isl_space_tuple_match(data
->map
->dim
, isl_dim_out
,
1014 map2
->dim
, isl_dim_out
))
1017 map2
= isl_map_lex_le_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1019 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1024 __isl_give isl_union_map
*isl_union_map_lex_le_union_map(
1025 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1027 return bin_op(umap1
, umap2
, &map_lex_le_entry
);
1030 static int product_entry(void **entry
, void *user
)
1032 struct isl_union_map_bin_data
*data
= user
;
1033 isl_map
*map2
= *entry
;
1035 map2
= isl_map_product(isl_map_copy(data
->map
), isl_map_copy(map2
));
1037 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1042 __isl_give isl_union_map
*isl_union_map_product(__isl_take isl_union_map
*umap1
,
1043 __isl_take isl_union_map
*umap2
)
1045 return bin_op(umap1
, umap2
, &product_entry
);
1048 __isl_give isl_union_set
*isl_union_set_product(__isl_take isl_union_set
*uset1
,
1049 __isl_take isl_union_set
*uset2
)
1051 return isl_union_map_product(uset1
, uset2
);
1054 static int range_product_entry(void **entry
, void *user
)
1056 struct isl_union_map_bin_data
*data
= user
;
1057 isl_map
*map2
= *entry
;
1059 if (!isl_space_tuple_match(data
->map
->dim
, isl_dim_in
,
1060 map2
->dim
, isl_dim_in
))
1063 map2
= isl_map_range_product(isl_map_copy(data
->map
),
1064 isl_map_copy(map2
));
1066 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1071 __isl_give isl_union_map
*isl_union_map_range_product(
1072 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1074 return bin_op(umap1
, umap2
, &range_product_entry
);
1077 static int flat_range_product_entry(void **entry
, void *user
)
1079 struct isl_union_map_bin_data
*data
= user
;
1080 isl_map
*map2
= *entry
;
1082 if (!isl_space_tuple_match(data
->map
->dim
, isl_dim_in
,
1083 map2
->dim
, isl_dim_in
))
1086 map2
= isl_map_flat_range_product(isl_map_copy(data
->map
),
1087 isl_map_copy(map2
));
1089 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1094 __isl_give isl_union_map
*isl_union_map_flat_range_product(
1095 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1097 return bin_op(umap1
, umap2
, &flat_range_product_entry
);
1100 static __isl_give isl_union_set
*cond_un_op(__isl_take isl_union_map
*umap
,
1101 int (*fn
)(void **, void *))
1108 res
= isl_union_map_alloc(isl_space_copy(umap
->dim
), umap
->table
.n
);
1109 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
, fn
, &res
) < 0)
1112 isl_union_map_free(umap
);
1115 isl_union_map_free(umap
);
1116 isl_union_set_free(res
);
1120 static int from_range_entry(void **entry
, void *user
)
1122 isl_map
*set
= *entry
;
1123 isl_union_set
**res
= user
;
1125 *res
= isl_union_map_add_map(*res
,
1126 isl_map_from_range(isl_set_copy(set
)));
1131 __isl_give isl_union_map
*isl_union_map_from_range(
1132 __isl_take isl_union_set
*uset
)
1134 return cond_un_op(uset
, &from_range_entry
);
1137 __isl_give isl_union_map
*isl_union_map_from_domain(
1138 __isl_take isl_union_set
*uset
)
1140 return isl_union_map_reverse(isl_union_map_from_range(uset
));
1143 __isl_give isl_union_map
*isl_union_map_from_domain_and_range(
1144 __isl_take isl_union_set
*domain
, __isl_take isl_union_set
*range
)
1146 return isl_union_map_apply_range(isl_union_map_from_domain(domain
),
1147 isl_union_map_from_range(range
));
1150 static __isl_give isl_union_map
*un_op(__isl_take isl_union_map
*umap
,
1151 int (*fn
)(void **, void *))
1153 umap
= isl_union_map_cow(umap
);
1157 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
, fn
, NULL
) < 0)
1162 isl_union_map_free(umap
);
1166 static int affine_entry(void **entry
, void *user
)
1168 isl_map
**map
= (isl_map
**)entry
;
1170 *map
= isl_map_from_basic_map(isl_map_affine_hull(*map
));
1172 return *map
? 0 : -1;
1175 __isl_give isl_union_map
*isl_union_map_affine_hull(
1176 __isl_take isl_union_map
*umap
)
1178 return un_op(umap
, &affine_entry
);
1181 __isl_give isl_union_set
*isl_union_set_affine_hull(
1182 __isl_take isl_union_set
*uset
)
1184 return isl_union_map_affine_hull(uset
);
1187 static int polyhedral_entry(void **entry
, void *user
)
1189 isl_map
**map
= (isl_map
**)entry
;
1191 *map
= isl_map_from_basic_map(isl_map_polyhedral_hull(*map
));
1193 return *map
? 0 : -1;
1196 __isl_give isl_union_map
*isl_union_map_polyhedral_hull(
1197 __isl_take isl_union_map
*umap
)
1199 return un_op(umap
, &polyhedral_entry
);
1202 __isl_give isl_union_set
*isl_union_set_polyhedral_hull(
1203 __isl_take isl_union_set
*uset
)
1205 return isl_union_map_polyhedral_hull(uset
);
1208 static int simple_entry(void **entry
, void *user
)
1210 isl_map
**map
= (isl_map
**)entry
;
1212 *map
= isl_map_from_basic_map(isl_map_simple_hull(*map
));
1214 return *map
? 0 : -1;
1217 __isl_give isl_union_map
*isl_union_map_simple_hull(
1218 __isl_take isl_union_map
*umap
)
1220 return un_op(umap
, &simple_entry
);
1223 __isl_give isl_union_set
*isl_union_set_simple_hull(
1224 __isl_take isl_union_set
*uset
)
1226 return isl_union_map_simple_hull(uset
);
1229 static int inplace_entry(void **entry
, void *user
)
1231 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*);
1232 isl_map
**map
= (isl_map
**)entry
;
1235 fn
= *(__isl_give isl_map
*(**)(__isl_take isl_map
*)) user
;
1236 copy
= fn(isl_map_copy(*map
));
1246 static __isl_give isl_union_map
*inplace(__isl_take isl_union_map
*umap
,
1247 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
1252 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1253 &inplace_entry
, &fn
) < 0)
1258 isl_union_map_free(umap
);
1262 __isl_give isl_union_map
*isl_union_map_coalesce(
1263 __isl_take isl_union_map
*umap
)
1265 return inplace(umap
, &isl_map_coalesce
);
1268 __isl_give isl_union_set
*isl_union_set_coalesce(
1269 __isl_take isl_union_set
*uset
)
1271 return isl_union_map_coalesce(uset
);
1274 __isl_give isl_union_map
*isl_union_map_detect_equalities(
1275 __isl_take isl_union_map
*umap
)
1277 return inplace(umap
, &isl_map_detect_equalities
);
1280 __isl_give isl_union_set
*isl_union_set_detect_equalities(
1281 __isl_take isl_union_set
*uset
)
1283 return isl_union_map_detect_equalities(uset
);
1286 __isl_give isl_union_map
*isl_union_map_compute_divs(
1287 __isl_take isl_union_map
*umap
)
1289 return inplace(umap
, &isl_map_compute_divs
);
1292 __isl_give isl_union_set
*isl_union_set_compute_divs(
1293 __isl_take isl_union_set
*uset
)
1295 return isl_union_map_compute_divs(uset
);
1298 static int lexmin_entry(void **entry
, void *user
)
1300 isl_map
**map
= (isl_map
**)entry
;
1302 *map
= isl_map_lexmin(*map
);
1304 return *map
? 0 : -1;
1307 __isl_give isl_union_map
*isl_union_map_lexmin(
1308 __isl_take isl_union_map
*umap
)
1310 return un_op(umap
, &lexmin_entry
);
1313 __isl_give isl_union_set
*isl_union_set_lexmin(
1314 __isl_take isl_union_set
*uset
)
1316 return isl_union_map_lexmin(uset
);
1319 static int lexmax_entry(void **entry
, void *user
)
1321 isl_map
**map
= (isl_map
**)entry
;
1323 *map
= isl_map_lexmax(*map
);
1325 return *map
? 0 : -1;
1328 __isl_give isl_union_map
*isl_union_map_lexmax(
1329 __isl_take isl_union_map
*umap
)
1331 return un_op(umap
, &lexmax_entry
);
1334 __isl_give isl_union_set
*isl_union_set_lexmax(
1335 __isl_take isl_union_set
*uset
)
1337 return isl_union_map_lexmax(uset
);
1340 static int universe_entry(void **entry
, void *user
)
1342 isl_map
*map
= *entry
;
1343 isl_union_map
**res
= user
;
1345 map
= isl_map_universe(isl_map_get_space(map
));
1346 *res
= isl_union_map_add_map(*res
, map
);
1351 __isl_give isl_union_map
*isl_union_map_universe(__isl_take isl_union_map
*umap
)
1353 return cond_un_op(umap
, &universe_entry
);
1356 __isl_give isl_union_set
*isl_union_set_universe(__isl_take isl_union_set
*uset
)
1358 return isl_union_map_universe(uset
);
1361 static int reverse_entry(void **entry
, void *user
)
1363 isl_map
*map
= *entry
;
1364 isl_union_map
**res
= user
;
1366 *res
= isl_union_map_add_map(*res
, isl_map_reverse(isl_map_copy(map
)));
1371 __isl_give isl_union_map
*isl_union_map_reverse(__isl_take isl_union_map
*umap
)
1373 return cond_un_op(umap
, &reverse_entry
);
1376 static int domain_entry(void **entry
, void *user
)
1378 isl_map
*map
= *entry
;
1379 isl_union_set
**res
= user
;
1381 *res
= isl_union_set_add_set(*res
, isl_map_domain(isl_map_copy(map
)));
1386 __isl_give isl_union_set
*isl_union_map_domain(__isl_take isl_union_map
*umap
)
1388 return cond_un_op(umap
, &domain_entry
);
1391 static int range_entry(void **entry
, void *user
)
1393 isl_map
*map
= *entry
;
1394 isl_union_set
**res
= user
;
1396 *res
= isl_union_set_add_set(*res
, isl_map_range(isl_map_copy(map
)));
1401 __isl_give isl_union_set
*isl_union_map_range(__isl_take isl_union_map
*umap
)
1403 return cond_un_op(umap
, &range_entry
);
1406 static int domain_map_entry(void **entry
, void *user
)
1408 isl_map
*map
= *entry
;
1409 isl_union_set
**res
= user
;
1411 *res
= isl_union_map_add_map(*res
,
1412 isl_map_domain_map(isl_map_copy(map
)));
1417 __isl_give isl_union_map
*isl_union_map_domain_map(
1418 __isl_take isl_union_map
*umap
)
1420 return cond_un_op(umap
, &domain_map_entry
);
1423 static int range_map_entry(void **entry
, void *user
)
1425 isl_map
*map
= *entry
;
1426 isl_union_set
**res
= user
;
1428 *res
= isl_union_map_add_map(*res
,
1429 isl_map_range_map(isl_map_copy(map
)));
1434 __isl_give isl_union_map
*isl_union_map_range_map(
1435 __isl_take isl_union_map
*umap
)
1437 return cond_un_op(umap
, &range_map_entry
);
1440 static int deltas_entry(void **entry
, void *user
)
1442 isl_map
*map
= *entry
;
1443 isl_union_set
**res
= user
;
1445 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
1448 *res
= isl_union_set_add_set(*res
, isl_map_deltas(isl_map_copy(map
)));
1453 __isl_give isl_union_set
*isl_union_map_deltas(__isl_take isl_union_map
*umap
)
1455 return cond_un_op(umap
, &deltas_entry
);
1458 static int deltas_map_entry(void **entry
, void *user
)
1460 isl_map
*map
= *entry
;
1461 isl_union_map
**res
= user
;
1463 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
1466 *res
= isl_union_map_add_map(*res
,
1467 isl_map_deltas_map(isl_map_copy(map
)));
1472 __isl_give isl_union_map
*isl_union_map_deltas_map(
1473 __isl_take isl_union_map
*umap
)
1475 return cond_un_op(umap
, &deltas_map_entry
);
1478 static int identity_entry(void **entry
, void *user
)
1480 isl_set
*set
= *entry
;
1481 isl_union_map
**res
= user
;
1483 *res
= isl_union_map_add_map(*res
, isl_set_identity(isl_set_copy(set
)));
1488 __isl_give isl_union_map
*isl_union_set_identity(__isl_take isl_union_set
*uset
)
1490 return cond_un_op(uset
, &identity_entry
);
1493 static int unwrap_entry(void **entry
, void *user
)
1495 isl_set
*set
= *entry
;
1496 isl_union_set
**res
= user
;
1498 if (!isl_set_is_wrapping(set
))
1501 *res
= isl_union_map_add_map(*res
, isl_set_unwrap(isl_set_copy(set
)));
1506 __isl_give isl_union_map
*isl_union_set_unwrap(__isl_take isl_union_set
*uset
)
1508 return cond_un_op(uset
, &unwrap_entry
);
1511 static int wrap_entry(void **entry
, void *user
)
1513 isl_map
*map
= *entry
;
1514 isl_union_set
**res
= user
;
1516 *res
= isl_union_set_add_set(*res
, isl_map_wrap(isl_map_copy(map
)));
1521 __isl_give isl_union_set
*isl_union_map_wrap(__isl_take isl_union_map
*umap
)
1523 return cond_un_op(umap
, &wrap_entry
);
1526 struct isl_union_map_is_subset_data
{
1527 isl_union_map
*umap2
;
1531 static int is_subset_entry(void **entry
, void *user
)
1533 struct isl_union_map_is_subset_data
*data
= user
;
1535 struct isl_hash_table_entry
*entry2
;
1536 isl_map
*map
= *entry
;
1538 hash
= isl_space_get_hash(map
->dim
);
1539 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1540 hash
, &has_dim
, map
->dim
, 0);
1542 data
->is_subset
= 0;
1546 data
->is_subset
= isl_map_is_subset(map
, entry2
->data
);
1547 if (data
->is_subset
< 0 || !data
->is_subset
)
1553 int isl_union_map_is_subset(__isl_keep isl_union_map
*umap1
,
1554 __isl_keep isl_union_map
*umap2
)
1556 struct isl_union_map_is_subset_data data
= { NULL
, 1 };
1558 umap1
= isl_union_map_copy(umap1
);
1559 umap2
= isl_union_map_copy(umap2
);
1560 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
1561 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1563 if (!umap1
|| !umap2
)
1567 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1568 &is_subset_entry
, &data
) < 0 &&
1572 isl_union_map_free(umap1
);
1573 isl_union_map_free(umap2
);
1575 return data
.is_subset
;
1577 isl_union_map_free(umap1
);
1578 isl_union_map_free(umap2
);
1582 int isl_union_set_is_subset(__isl_keep isl_union_set
*uset1
,
1583 __isl_keep isl_union_set
*uset2
)
1585 return isl_union_map_is_subset(uset1
, uset2
);
1588 int isl_union_map_is_equal(__isl_keep isl_union_map
*umap1
,
1589 __isl_keep isl_union_map
*umap2
)
1593 if (!umap1
|| !umap2
)
1595 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
1598 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
1602 int isl_union_set_is_equal(__isl_keep isl_union_set
*uset1
,
1603 __isl_keep isl_union_set
*uset2
)
1605 return isl_union_map_is_equal(uset1
, uset2
);
1608 int isl_union_map_is_strict_subset(__isl_keep isl_union_map
*umap1
,
1609 __isl_keep isl_union_map
*umap2
)
1613 if (!umap1
|| !umap2
)
1615 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
1618 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
1619 if (is_subset
== -1)
1624 int isl_union_set_is_strict_subset(__isl_keep isl_union_set
*uset1
,
1625 __isl_keep isl_union_set
*uset2
)
1627 return isl_union_map_is_strict_subset(uset1
, uset2
);
1630 static int sample_entry(void **entry
, void *user
)
1632 isl_basic_map
**sample
= (isl_basic_map
**)user
;
1633 isl_map
*map
= *entry
;
1635 *sample
= isl_map_sample(isl_map_copy(map
));
1638 if (!isl_basic_map_plain_is_empty(*sample
))
1643 __isl_give isl_basic_map
*isl_union_map_sample(__isl_take isl_union_map
*umap
)
1645 isl_basic_map
*sample
= NULL
;
1650 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1651 &sample_entry
, &sample
) < 0 &&
1656 sample
= isl_basic_map_empty(isl_union_map_get_space(umap
));
1658 isl_union_map_free(umap
);
1662 isl_union_map_free(umap
);
1666 __isl_give isl_basic_set
*isl_union_set_sample(__isl_take isl_union_set
*uset
)
1668 return (isl_basic_set
*)isl_union_map_sample(uset
);
1671 struct isl_forall_data
{
1673 int (*fn
)(__isl_keep isl_map
*map
);
1676 static int forall_entry(void **entry
, void *user
)
1678 struct isl_forall_data
*data
= user
;
1679 isl_map
*map
= *entry
;
1681 data
->res
= data
->fn(map
);
1691 static int union_map_forall(__isl_keep isl_union_map
*umap
,
1692 int (*fn
)(__isl_keep isl_map
*map
))
1694 struct isl_forall_data data
= { 1, fn
};
1699 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1700 &forall_entry
, &data
) < 0 && data
.res
)
1706 struct isl_forall_user_data
{
1708 int (*fn
)(__isl_keep isl_map
*map
, void *user
);
1712 static int forall_user_entry(void **entry
, void *user
)
1714 struct isl_forall_user_data
*data
= user
;
1715 isl_map
*map
= *entry
;
1717 data
->res
= data
->fn(map
, data
->user
);
1727 /* Check if fn(map, user) returns true for all maps "map" in umap.
1729 static int union_map_forall_user(__isl_keep isl_union_map
*umap
,
1730 int (*fn
)(__isl_keep isl_map
*map
, void *user
), void *user
)
1732 struct isl_forall_user_data data
= { 1, fn
, user
};
1737 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1738 &forall_user_entry
, &data
) < 0 && data
.res
)
1744 int isl_union_map_is_empty(__isl_keep isl_union_map
*umap
)
1746 return union_map_forall(umap
, &isl_map_is_empty
);
1749 int isl_union_set_is_empty(__isl_keep isl_union_set
*uset
)
1751 return isl_union_map_is_empty(uset
);
1754 static int is_subset_of_identity(__isl_keep isl_map
*map
)
1763 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
1766 dim
= isl_map_get_space(map
);
1767 id
= isl_map_identity(dim
);
1769 is_subset
= isl_map_is_subset(map
, id
);
1776 /* Check if the given map is single-valued.
1781 * and check if the result is a subset of the identity mapping.
1783 int isl_union_map_is_single_valued(__isl_keep isl_union_map
*umap
)
1785 isl_union_map
*test
;
1788 if (isl_union_map_n_map(umap
) == 1) {
1790 umap
= isl_union_map_copy(umap
);
1791 map
= isl_map_from_union_map(umap
);
1792 sv
= isl_map_is_single_valued(map
);
1797 test
= isl_union_map_reverse(isl_union_map_copy(umap
));
1798 test
= isl_union_map_apply_range(test
, isl_union_map_copy(umap
));
1800 sv
= union_map_forall(test
, &is_subset_of_identity
);
1802 isl_union_map_free(test
);
1807 int isl_union_map_is_injective(__isl_keep isl_union_map
*umap
)
1811 umap
= isl_union_map_copy(umap
);
1812 umap
= isl_union_map_reverse(umap
);
1813 in
= isl_union_map_is_single_valued(umap
);
1814 isl_union_map_free(umap
);
1819 /* Represents a map that has a fixed value (v) for one of its
1821 * The map in this structure is not reference counted, so it
1822 * is only valid while the isl_union_map from which it was
1823 * obtained is still alive.
1825 struct isl_fixed_map
{
1830 static struct isl_fixed_map
*alloc_isl_fixed_map_array(isl_ctx
*ctx
,
1834 struct isl_fixed_map
*v
;
1836 v
= isl_calloc_array(ctx
, struct isl_fixed_map
, n
);
1839 for (i
= 0; i
< n
; ++i
)
1840 isl_int_init(v
[i
].v
);
1844 static void free_isl_fixed_map_array(struct isl_fixed_map
*v
, int n
)
1850 for (i
= 0; i
< n
; ++i
)
1851 isl_int_clear(v
[i
].v
);
1855 /* Compare the "v" field of two isl_fixed_map structs.
1857 static int qsort_fixed_map_cmp(const void *p1
, const void *p2
)
1859 const struct isl_fixed_map
*e1
= (const struct isl_fixed_map
*) p1
;
1860 const struct isl_fixed_map
*e2
= (const struct isl_fixed_map
*) p2
;
1862 return isl_int_cmp(e1
->v
, e2
->v
);
1865 /* Internal data structure used while checking whether all maps
1866 * in a union_map have a fixed value for a given output dimension.
1867 * v is the list of maps, with the fixed value for the dimension
1868 * n is the number of maps considered so far
1869 * pos is the output dimension under investigation
1871 struct isl_fixed_dim_data
{
1872 struct isl_fixed_map
*v
;
1877 static int fixed_at_pos(__isl_keep isl_map
*map
, void *user
)
1879 struct isl_fixed_dim_data
*data
= user
;
1881 data
->v
[data
->n
].map
= map
;
1882 return isl_map_plain_is_fixed(map
, isl_dim_out
, data
->pos
,
1883 &data
->v
[data
->n
++].v
);
1886 static int plain_injective_on_range(__isl_take isl_union_map
*umap
,
1887 int first
, int n_range
);
1889 /* Given a list of the maps, with their fixed values at output dimension "pos",
1890 * check whether the ranges of the maps form an obvious partition.
1892 * We first sort the maps according to their fixed values.
1893 * If all maps have a different value, then we know the ranges form
1895 * Otherwise, we collect the maps with the same fixed value and
1896 * check whether each such collection is obviously injective
1897 * based on later dimensions.
1899 static int separates(struct isl_fixed_map
*v
, int n
,
1900 __isl_take isl_space
*dim
, int pos
, int n_range
)
1907 qsort(v
, n
, sizeof(*v
), &qsort_fixed_map_cmp
);
1909 for (i
= 0; i
+ 1 < n
; ++i
) {
1911 isl_union_map
*part
;
1914 for (j
= i
+ 1; j
< n
; ++j
)
1915 if (isl_int_ne(v
[i
].v
, v
[j
].v
))
1921 part
= isl_union_map_alloc(isl_space_copy(dim
), j
- i
);
1922 for (k
= i
; k
< j
; ++k
)
1923 part
= isl_union_map_add_map(part
,
1924 isl_map_copy(v
[k
].map
));
1926 injective
= plain_injective_on_range(part
, pos
+ 1, n_range
);
1935 isl_space_free(dim
);
1936 free_isl_fixed_map_array(v
, n
);
1939 isl_space_free(dim
);
1940 free_isl_fixed_map_array(v
, n
);
1944 /* Check whether the maps in umap have obviously distinct ranges.
1945 * In particular, check for an output dimension in the range
1946 * [first,n_range) for which all maps have a fixed value
1947 * and then check if these values, possibly along with fixed values
1948 * at later dimensions, entail distinct ranges.
1950 static int plain_injective_on_range(__isl_take isl_union_map
*umap
,
1951 int first
, int n_range
)
1955 struct isl_fixed_dim_data data
= { NULL
};
1957 ctx
= isl_union_map_get_ctx(umap
);
1962 n
= isl_union_map_n_map(umap
);
1964 isl_union_map_free(umap
);
1968 if (first
>= n_range
) {
1969 isl_union_map_free(umap
);
1973 data
.v
= alloc_isl_fixed_map_array(ctx
, n
);
1977 for (data
.pos
= first
; data
.pos
< n_range
; ++data
.pos
) {
1983 fixed
= union_map_forall_user(umap
, &fixed_at_pos
, &data
);
1988 dim
= isl_union_map_get_space(umap
);
1989 injective
= separates(data
.v
, n
, dim
, data
.pos
, n_range
);
1990 isl_union_map_free(umap
);
1994 free_isl_fixed_map_array(data
.v
, n
);
1995 isl_union_map_free(umap
);
1999 free_isl_fixed_map_array(data
.v
, n
);
2000 isl_union_map_free(umap
);
2004 /* Check whether the maps in umap that map to subsets of "ran"
2005 * have obviously distinct ranges.
2007 static int plain_injective_on_range_wrap(__isl_keep isl_set
*ran
, void *user
)
2009 isl_union_map
*umap
= user
;
2011 umap
= isl_union_map_copy(umap
);
2012 umap
= isl_union_map_intersect_range(umap
,
2013 isl_union_set_from_set(isl_set_copy(ran
)));
2014 return plain_injective_on_range(umap
, 0, isl_set_dim(ran
, isl_dim_set
));
2017 /* Check if the given union_map is obviously injective.
2019 * In particular, we first check if all individual maps are obviously
2020 * injective and then check if all the ranges of these maps are
2021 * obviously disjoint.
2023 int isl_union_map_plain_is_injective(__isl_keep isl_union_map
*umap
)
2026 isl_union_map
*univ
;
2029 in
= union_map_forall(umap
, &isl_map_plain_is_injective
);
2035 univ
= isl_union_map_universe(isl_union_map_copy(umap
));
2036 ran
= isl_union_map_range(univ
);
2038 in
= union_map_forall_user(ran
, &plain_injective_on_range_wrap
, umap
);
2040 isl_union_set_free(ran
);
2045 int isl_union_map_is_bijective(__isl_keep isl_union_map
*umap
)
2049 sv
= isl_union_map_is_single_valued(umap
);
2053 return isl_union_map_is_injective(umap
);
2056 static int zip_entry(void **entry
, void *user
)
2058 isl_map
*map
= *entry
;
2059 isl_union_map
**res
= user
;
2061 if (!isl_map_can_zip(map
))
2064 *res
= isl_union_map_add_map(*res
, isl_map_zip(isl_map_copy(map
)));
2069 __isl_give isl_union_map
*isl_union_map_zip(__isl_take isl_union_map
*umap
)
2071 return cond_un_op(umap
, &zip_entry
);
2074 static int lift_entry(void **entry
, void *user
)
2076 isl_set
*set
= *entry
;
2077 isl_union_set
**res
= user
;
2079 *res
= isl_union_set_add_set(*res
, isl_set_lift(isl_set_copy(set
)));
2084 __isl_give isl_union_set
*isl_union_set_lift(__isl_take isl_union_set
*uset
)
2086 return cond_un_op(uset
, &lift_entry
);
2089 static int coefficients_entry(void **entry
, void *user
)
2091 isl_set
*set
= *entry
;
2092 isl_union_set
**res
= user
;
2094 set
= isl_set_copy(set
);
2095 set
= isl_set_from_basic_set(isl_set_coefficients(set
));
2096 *res
= isl_union_set_add_set(*res
, set
);
2101 __isl_give isl_union_set
*isl_union_set_coefficients(
2102 __isl_take isl_union_set
*uset
)
2111 ctx
= isl_union_set_get_ctx(uset
);
2112 dim
= isl_space_set_alloc(ctx
, 0, 0);
2113 res
= isl_union_map_alloc(dim
, uset
->table
.n
);
2114 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
2115 &coefficients_entry
, &res
) < 0)
2118 isl_union_set_free(uset
);
2121 isl_union_set_free(uset
);
2122 isl_union_set_free(res
);
2126 static int solutions_entry(void **entry
, void *user
)
2128 isl_set
*set
= *entry
;
2129 isl_union_set
**res
= user
;
2131 set
= isl_set_copy(set
);
2132 set
= isl_set_from_basic_set(isl_set_solutions(set
));
2134 *res
= isl_union_set_from_set(set
);
2136 *res
= isl_union_set_add_set(*res
, set
);
2144 __isl_give isl_union_set
*isl_union_set_solutions(
2145 __isl_take isl_union_set
*uset
)
2147 isl_union_set
*res
= NULL
;
2152 if (uset
->table
.n
== 0) {
2153 res
= isl_union_set_empty(isl_union_set_get_space(uset
));
2154 isl_union_set_free(uset
);
2158 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
2159 &solutions_entry
, &res
) < 0)
2162 isl_union_set_free(uset
);
2165 isl_union_set_free(uset
);
2166 isl_union_set_free(res
);