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,
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 model
= isl_dim_drop(model
, isl_dim_in
,
158 0, isl_dim_size(model
, isl_dim_in
));
159 model
= isl_dim_drop(model
, isl_dim_out
,
160 0, isl_dim_size(model
, isl_dim_out
));
162 data
.exp
= isl_parameter_alignment_reordering(umap
->dim
, model
);
166 data
.res
= isl_union_map_alloc(isl_dim_copy(data
.exp
->dim
),
168 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
169 &align_entry
, &data
) < 0)
172 isl_reordering_free(data
.exp
);
173 isl_union_map_free(umap
);
177 isl_reordering_free(data
.exp
);
178 isl_union_map_free(umap
);
179 isl_union_map_free(data
.res
);
184 __isl_give isl_union_set
*isl_union_set_align_params(
185 __isl_take isl_union_set
*uset
, __isl_take isl_dim
*model
)
187 return isl_union_map_align_params(uset
, model
);
190 __isl_give isl_union_map
*isl_union_map_union(__isl_take isl_union_map
*umap1
,
191 __isl_take isl_union_map
*umap2
)
193 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_dim(umap2
));
194 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_dim(umap1
));
196 umap1
= isl_union_map_cow(umap1
);
198 if (!umap1
|| !umap2
)
201 if (isl_union_map_foreach_map(umap2
, &add_map
, &umap1
) < 0)
204 isl_union_map_free(umap2
);
208 isl_union_map_free(umap1
);
209 isl_union_map_free(umap2
);
213 __isl_give isl_union_set
*isl_union_set_union(__isl_take isl_union_set
*uset1
,
214 __isl_take isl_union_set
*uset2
)
216 return isl_union_map_union(uset1
, uset2
);
219 __isl_give isl_union_map
*isl_union_map_copy(__isl_keep isl_union_map
*umap
)
228 __isl_give isl_union_set
*isl_union_set_copy(__isl_keep isl_union_set
*uset
)
230 return isl_union_map_copy(uset
);
233 void *isl_union_map_free(__isl_take isl_union_map
*umap
)
241 isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
242 &free_umap_entry
, NULL
);
243 isl_hash_table_clear(&umap
->table
);
244 isl_dim_free(umap
->dim
);
249 void *isl_union_set_free(__isl_take isl_union_set
*uset
)
251 return isl_union_map_free(uset
);
254 static int has_dim(const void *entry
, const void *val
)
256 isl_map
*map
= (isl_map
*)entry
;
257 isl_dim
*dim
= (isl_dim
*)val
;
259 return isl_dim_equal(map
->dim
, dim
);
262 __isl_give isl_union_map
*isl_union_map_add_map(__isl_take isl_union_map
*umap
,
263 __isl_take isl_map
*map
)
266 struct isl_hash_table_entry
*entry
;
271 if (isl_map_plain_is_empty(map
)) {
276 if (!isl_dim_match(map
->dim
, isl_dim_param
, umap
->dim
, isl_dim_param
)) {
277 umap
= isl_union_map_align_params(umap
, isl_map_get_dim(map
));
278 map
= isl_map_align_params(map
, isl_union_map_get_dim(umap
));
281 umap
= isl_union_map_cow(umap
);
286 hash
= isl_dim_get_hash(map
->dim
);
287 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
288 &has_dim
, map
->dim
, 1);
295 entry
->data
= isl_map_union(entry
->data
, isl_map_copy(map
));
304 isl_union_map_free(umap
);
308 __isl_give isl_union_set
*isl_union_set_add_set(__isl_take isl_union_set
*uset
,
309 __isl_take isl_set
*set
)
311 return isl_union_map_add_map(uset
, (isl_map
*)set
);
314 __isl_give isl_union_map
*isl_union_map_from_map(__isl_take isl_map
*map
)
322 dim
= isl_map_get_dim(map
);
323 dim
= isl_dim_drop(dim
, isl_dim_in
, 0, isl_dim_size(dim
, isl_dim_in
));
324 dim
= isl_dim_drop(dim
, isl_dim_out
, 0, isl_dim_size(dim
, isl_dim_out
));
325 umap
= isl_union_map_empty(dim
);
326 umap
= isl_union_map_add_map(umap
, map
);
331 __isl_give isl_union_set
*isl_union_set_from_set(__isl_take isl_set
*set
)
333 return isl_union_map_from_map((isl_map
*)set
);
336 struct isl_union_map_foreach_data
338 int (*fn
)(__isl_take isl_map
*map
, void *user
);
342 static int call_on_copy(void **entry
, void *user
)
344 isl_map
*map
= *entry
;
345 struct isl_union_map_foreach_data
*data
;
346 data
= (struct isl_union_map_foreach_data
*)user
;
348 return data
->fn(isl_map_copy(map
), data
->user
);
351 int isl_union_map_n_map(__isl_keep isl_union_map
*umap
)
353 return umap
? umap
->table
.n
: 0;
356 int isl_union_set_n_set(__isl_keep isl_union_set
*uset
)
358 return uset
? uset
->table
.n
: 0;
361 int isl_union_map_foreach_map(__isl_keep isl_union_map
*umap
,
362 int (*fn
)(__isl_take isl_map
*map
, void *user
), void *user
)
364 struct isl_union_map_foreach_data data
= { fn
, user
};
369 return isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
370 &call_on_copy
, &data
);
373 static int copy_map(void **entry
, void *user
)
375 isl_map
*map
= *entry
;
376 isl_map
**map_p
= user
;
378 *map_p
= isl_map_copy(map
);
383 __isl_give isl_map
*isl_map_from_union_map(__isl_take isl_union_map
*umap
)
390 ctx
= isl_union_map_get_ctx(umap
);
391 if (umap
->table
.n
!= 1)
392 isl_die(ctx
, isl_error_invalid
,
393 "union map needs to contain elements in exactly "
394 "one space", return isl_union_map_free(umap
));
396 isl_hash_table_foreach(ctx
, &umap
->table
, ©_map
, &map
);
398 isl_union_map_free(umap
);
403 __isl_give isl_set
*isl_set_from_union_set(__isl_take isl_union_set
*uset
)
405 return isl_map_from_union_map(uset
);
408 __isl_give isl_map
*isl_union_map_extract_map(__isl_keep isl_union_map
*umap
,
409 __isl_take isl_dim
*dim
)
412 struct isl_hash_table_entry
*entry
;
417 hash
= isl_dim_get_hash(dim
);
418 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
421 return isl_map_empty(dim
);
423 return isl_map_copy(entry
->data
);
429 __isl_give isl_set
*isl_union_set_extract_set(__isl_keep isl_union_set
*uset
,
430 __isl_take isl_dim
*dim
)
432 return (isl_set
*)isl_union_map_extract_map(uset
, dim
);
435 /* Check if umap contains a map in the given space.
437 __isl_give
int isl_union_map_contains(__isl_keep isl_union_map
*umap
,
438 __isl_keep isl_dim
*dim
)
441 struct isl_hash_table_entry
*entry
;
446 hash
= isl_dim_get_hash(dim
);
447 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
452 __isl_give
int isl_union_set_contains(__isl_keep isl_union_set
*uset
,
453 __isl_keep isl_dim
*dim
)
455 return isl_union_map_contains(uset
, dim
);
458 int isl_union_set_foreach_set(__isl_keep isl_union_set
*uset
,
459 int (*fn
)(__isl_take isl_set
*set
, void *user
), void *user
)
461 return isl_union_map_foreach_map(uset
,
462 (int(*)(__isl_take isl_map
*, void*))fn
, user
);
465 struct isl_union_set_foreach_point_data
{
466 int (*fn
)(__isl_take isl_point
*pnt
, void *user
);
470 static int foreach_point(__isl_take isl_set
*set
, void *user
)
472 struct isl_union_set_foreach_point_data
*data
= user
;
475 r
= isl_set_foreach_point(set
, data
->fn
, data
->user
);
481 int isl_union_set_foreach_point(__isl_keep isl_union_set
*uset
,
482 int (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
484 struct isl_union_set_foreach_point_data data
= { fn
, user
};
485 return isl_union_set_foreach_set(uset
, &foreach_point
, &data
);
488 struct isl_union_map_gen_bin_data
{
489 isl_union_map
*umap2
;
493 static int subtract_entry(void **entry
, void *user
)
495 struct isl_union_map_gen_bin_data
*data
= user
;
497 struct isl_hash_table_entry
*entry2
;
498 isl_map
*map
= *entry
;
500 hash
= isl_dim_get_hash(map
->dim
);
501 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
502 hash
, &has_dim
, map
->dim
, 0);
503 map
= isl_map_copy(map
);
506 map
= isl_map_subtract(map
, isl_map_copy(entry2
->data
));
508 empty
= isl_map_is_empty(map
);
518 data
->res
= isl_union_map_add_map(data
->res
, map
);
523 static __isl_give isl_union_map
*gen_bin_op(__isl_take isl_union_map
*umap1
,
524 __isl_take isl_union_map
*umap2
, int (*fn
)(void **, void *))
526 struct isl_union_map_gen_bin_data data
= { NULL
, NULL
};
528 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_dim(umap2
));
529 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_dim(umap1
));
531 if (!umap1
|| !umap2
)
535 data
.res
= isl_union_map_alloc(isl_dim_copy(umap1
->dim
),
537 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
541 isl_union_map_free(umap1
);
542 isl_union_map_free(umap2
);
545 isl_union_map_free(umap1
);
546 isl_union_map_free(umap2
);
547 isl_union_map_free(data
.res
);
551 __isl_give isl_union_map
*isl_union_map_subtract(
552 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
554 return gen_bin_op(umap1
, umap2
, &subtract_entry
);
557 __isl_give isl_union_set
*isl_union_set_subtract(
558 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
560 return isl_union_map_subtract(uset1
, uset2
);
563 struct isl_union_map_match_bin_data
{
564 isl_union_map
*umap2
;
566 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*);
569 static int match_bin_entry(void **entry
, void *user
)
571 struct isl_union_map_match_bin_data
*data
= user
;
573 struct isl_hash_table_entry
*entry2
;
574 isl_map
*map
= *entry
;
577 hash
= isl_dim_get_hash(map
->dim
);
578 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
579 hash
, &has_dim
, map
->dim
, 0);
583 map
= isl_map_copy(map
);
584 map
= data
->fn(map
, isl_map_copy(entry2
->data
));
586 empty
= isl_map_is_empty(map
);
596 data
->res
= isl_union_map_add_map(data
->res
, map
);
601 static __isl_give isl_union_map
*match_bin_op(__isl_take isl_union_map
*umap1
,
602 __isl_take isl_union_map
*umap2
,
603 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*))
605 struct isl_union_map_match_bin_data data
= { NULL
, NULL
, fn
};
607 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_dim(umap2
));
608 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_dim(umap1
));
610 if (!umap1
|| !umap2
)
614 data
.res
= isl_union_map_alloc(isl_dim_copy(umap1
->dim
),
616 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
617 &match_bin_entry
, &data
) < 0)
620 isl_union_map_free(umap1
);
621 isl_union_map_free(umap2
);
624 isl_union_map_free(umap1
);
625 isl_union_map_free(umap2
);
626 isl_union_map_free(data
.res
);
630 __isl_give isl_union_map
*isl_union_map_intersect(
631 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
633 return match_bin_op(umap1
, umap2
, &isl_map_intersect
);
636 __isl_give isl_union_set
*isl_union_set_intersect(
637 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
639 return isl_union_map_intersect(uset1
, uset2
);
642 __isl_give isl_union_map
*isl_union_map_gist(__isl_take isl_union_map
*umap
,
643 __isl_take isl_union_map
*context
)
645 return match_bin_op(umap
, context
, &isl_map_gist
);
648 __isl_give isl_union_set
*isl_union_set_gist(__isl_take isl_union_set
*uset
,
649 __isl_take isl_union_set
*context
)
651 return isl_union_map_gist(uset
, context
);
654 static __isl_give isl_map
*lex_le_set(__isl_take isl_map
*set1
,
655 __isl_take isl_map
*set2
)
657 return isl_set_lex_le_set((isl_set
*)set1
, (isl_set
*)set2
);
660 static __isl_give isl_map
*lex_lt_set(__isl_take isl_map
*set1
,
661 __isl_take isl_map
*set2
)
663 return isl_set_lex_lt_set((isl_set
*)set1
, (isl_set
*)set2
);
666 __isl_give isl_union_map
*isl_union_set_lex_lt_union_set(
667 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
669 return match_bin_op(uset1
, uset2
, &lex_lt_set
);
672 __isl_give isl_union_map
*isl_union_set_lex_le_union_set(
673 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
675 return match_bin_op(uset1
, uset2
, &lex_le_set
);
678 __isl_give isl_union_map
*isl_union_set_lex_gt_union_set(
679 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
681 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2
, uset1
));
684 __isl_give isl_union_map
*isl_union_set_lex_ge_union_set(
685 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
687 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2
, uset1
));
690 __isl_give isl_union_map
*isl_union_map_lex_gt_union_map(
691 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
693 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2
, umap1
));
696 __isl_give isl_union_map
*isl_union_map_lex_ge_union_map(
697 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
699 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2
, umap1
));
702 static int intersect_domain_entry(void **entry
, void *user
)
704 struct isl_union_map_gen_bin_data
*data
= user
;
706 struct isl_hash_table_entry
*entry2
;
708 isl_map
*map
= *entry
;
711 dim
= isl_map_get_dim(map
);
712 dim
= isl_dim_domain(dim
);
713 hash
= isl_dim_get_hash(dim
);
714 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
715 hash
, &has_dim
, dim
, 0);
720 map
= isl_map_copy(map
);
721 map
= isl_map_intersect_domain(map
, isl_set_copy(entry2
->data
));
723 empty
= isl_map_is_empty(map
);
733 data
->res
= isl_union_map_add_map(data
->res
, map
);
738 __isl_give isl_union_map
*isl_union_map_intersect_domain(
739 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
741 return gen_bin_op(umap
, uset
, &intersect_domain_entry
);
744 static int intersect_range_entry(void **entry
, void *user
)
746 struct isl_union_map_gen_bin_data
*data
= user
;
748 struct isl_hash_table_entry
*entry2
;
750 isl_map
*map
= *entry
;
753 dim
= isl_map_get_dim(map
);
754 dim
= isl_dim_range(dim
);
755 hash
= isl_dim_get_hash(dim
);
756 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
757 hash
, &has_dim
, dim
, 0);
762 map
= isl_map_copy(map
);
763 map
= isl_map_intersect_range(map
, isl_set_copy(entry2
->data
));
765 empty
= isl_map_is_empty(map
);
775 data
->res
= isl_union_map_add_map(data
->res
, map
);
780 __isl_give isl_union_map
*isl_union_map_intersect_range(
781 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
783 return gen_bin_op(umap
, uset
, &intersect_range_entry
);
786 struct isl_union_map_bin_data
{
787 isl_union_map
*umap2
;
790 int (*fn
)(void **entry
, void *user
);
793 static int apply_range_entry(void **entry
, void *user
)
795 struct isl_union_map_bin_data
*data
= user
;
796 isl_map
*map2
= *entry
;
799 if (!isl_dim_tuple_match(data
->map
->dim
, isl_dim_out
,
800 map2
->dim
, isl_dim_in
))
803 map2
= isl_map_apply_range(isl_map_copy(data
->map
), isl_map_copy(map2
));
805 empty
= isl_map_is_empty(map2
);
815 data
->res
= isl_union_map_add_map(data
->res
, map2
);
820 static int bin_entry(void **entry
, void *user
)
822 struct isl_union_map_bin_data
*data
= user
;
823 isl_map
*map
= *entry
;
826 if (isl_hash_table_foreach(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
833 static __isl_give isl_union_map
*bin_op(__isl_take isl_union_map
*umap1
,
834 __isl_take isl_union_map
*umap2
, int (*fn
)(void **entry
, void *user
))
836 struct isl_union_map_bin_data data
= { NULL
, NULL
, NULL
, fn
};
838 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_dim(umap2
));
839 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_dim(umap1
));
841 if (!umap1
|| !umap2
)
845 data
.res
= isl_union_map_alloc(isl_dim_copy(umap1
->dim
),
847 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
848 &bin_entry
, &data
) < 0)
851 isl_union_map_free(umap1
);
852 isl_union_map_free(umap2
);
855 isl_union_map_free(umap1
);
856 isl_union_map_free(umap2
);
857 isl_union_map_free(data
.res
);
861 __isl_give isl_union_map
*isl_union_map_apply_range(
862 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
864 return bin_op(umap1
, umap2
, &apply_range_entry
);
867 __isl_give isl_union_map
*isl_union_map_apply_domain(
868 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
870 umap1
= isl_union_map_reverse(umap1
);
871 umap1
= isl_union_map_apply_range(umap1
, umap2
);
872 return isl_union_map_reverse(umap1
);
875 __isl_give isl_union_set
*isl_union_set_apply(
876 __isl_take isl_union_set
*uset
, __isl_take isl_union_map
*umap
)
878 return isl_union_map_apply_range(uset
, umap
);
881 static int map_lex_lt_entry(void **entry
, void *user
)
883 struct isl_union_map_bin_data
*data
= user
;
884 isl_map
*map2
= *entry
;
886 if (!isl_dim_tuple_match(data
->map
->dim
, isl_dim_out
,
887 map2
->dim
, isl_dim_out
))
890 map2
= isl_map_lex_lt_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
892 data
->res
= isl_union_map_add_map(data
->res
, map2
);
897 __isl_give isl_union_map
*isl_union_map_lex_lt_union_map(
898 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
900 return bin_op(umap1
, umap2
, &map_lex_lt_entry
);
903 static int map_lex_le_entry(void **entry
, void *user
)
905 struct isl_union_map_bin_data
*data
= user
;
906 isl_map
*map2
= *entry
;
908 if (!isl_dim_tuple_match(data
->map
->dim
, isl_dim_out
,
909 map2
->dim
, isl_dim_out
))
912 map2
= isl_map_lex_le_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
914 data
->res
= isl_union_map_add_map(data
->res
, map2
);
919 __isl_give isl_union_map
*isl_union_map_lex_le_union_map(
920 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
922 return bin_op(umap1
, umap2
, &map_lex_le_entry
);
925 static int product_entry(void **entry
, void *user
)
927 struct isl_union_map_bin_data
*data
= user
;
928 isl_map
*map2
= *entry
;
930 map2
= isl_map_product(isl_map_copy(data
->map
), isl_map_copy(map2
));
932 data
->res
= isl_union_map_add_map(data
->res
, map2
);
937 __isl_give isl_union_map
*isl_union_map_product(__isl_take isl_union_map
*umap1
,
938 __isl_take isl_union_map
*umap2
)
940 return bin_op(umap1
, umap2
, &product_entry
);
943 __isl_give isl_union_set
*isl_union_set_product(__isl_take isl_union_set
*uset1
,
944 __isl_take isl_union_set
*uset2
)
946 return isl_union_map_product(uset1
, uset2
);
949 static int range_product_entry(void **entry
, void *user
)
951 struct isl_union_map_bin_data
*data
= user
;
952 isl_map
*map2
= *entry
;
954 if (!isl_dim_tuple_match(data
->map
->dim
, isl_dim_in
,
955 map2
->dim
, isl_dim_in
))
958 map2
= isl_map_range_product(isl_map_copy(data
->map
),
961 data
->res
= isl_union_map_add_map(data
->res
, map2
);
966 __isl_give isl_union_map
*isl_union_map_range_product(
967 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
969 return bin_op(umap1
, umap2
, &range_product_entry
);
972 static int flat_range_product_entry(void **entry
, void *user
)
974 struct isl_union_map_bin_data
*data
= user
;
975 isl_map
*map2
= *entry
;
977 if (!isl_dim_tuple_match(data
->map
->dim
, isl_dim_in
,
978 map2
->dim
, isl_dim_in
))
981 map2
= isl_map_flat_range_product(isl_map_copy(data
->map
),
984 data
->res
= isl_union_map_add_map(data
->res
, map2
);
989 __isl_give isl_union_map
*isl_union_map_flat_range_product(
990 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
992 return bin_op(umap1
, umap2
, &flat_range_product_entry
);
995 __isl_give isl_union_map
*isl_union_map_from_range(
996 __isl_take isl_union_set
*uset
)
1001 __isl_give isl_union_map
*isl_union_map_from_domain(
1002 __isl_take isl_union_set
*uset
)
1004 return isl_union_map_reverse(isl_union_map_from_range(uset
));
1007 __isl_give isl_union_map
*isl_union_map_from_domain_and_range(
1008 __isl_take isl_union_set
*domain
, __isl_take isl_union_set
*range
)
1010 return isl_union_map_apply_range(isl_union_map_from_domain(domain
),
1011 isl_union_map_from_range(range
));
1014 static __isl_give isl_union_map
*un_op(__isl_take isl_union_map
*umap
,
1015 int (*fn
)(void **, void *))
1017 umap
= isl_union_map_cow(umap
);
1021 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
, fn
, NULL
) < 0)
1026 isl_union_map_free(umap
);
1030 static int affine_entry(void **entry
, void *user
)
1032 isl_map
**map
= (isl_map
**)entry
;
1034 *map
= isl_map_from_basic_map(isl_map_affine_hull(*map
));
1036 return *map
? 0 : -1;
1039 __isl_give isl_union_map
*isl_union_map_affine_hull(
1040 __isl_take isl_union_map
*umap
)
1042 return un_op(umap
, &affine_entry
);
1045 __isl_give isl_union_set
*isl_union_set_affine_hull(
1046 __isl_take isl_union_set
*uset
)
1048 return isl_union_map_affine_hull(uset
);
1051 static int polyhedral_entry(void **entry
, void *user
)
1053 isl_map
**map
= (isl_map
**)entry
;
1055 *map
= isl_map_from_basic_map(isl_map_polyhedral_hull(*map
));
1057 return *map
? 0 : -1;
1060 __isl_give isl_union_map
*isl_union_map_polyhedral_hull(
1061 __isl_take isl_union_map
*umap
)
1063 return un_op(umap
, &polyhedral_entry
);
1066 __isl_give isl_union_set
*isl_union_set_polyhedral_hull(
1067 __isl_take isl_union_set
*uset
)
1069 return isl_union_map_polyhedral_hull(uset
);
1072 static int simple_entry(void **entry
, void *user
)
1074 isl_map
**map
= (isl_map
**)entry
;
1076 *map
= isl_map_from_basic_map(isl_map_simple_hull(*map
));
1078 return *map
? 0 : -1;
1081 __isl_give isl_union_map
*isl_union_map_simple_hull(
1082 __isl_take isl_union_map
*umap
)
1084 return un_op(umap
, &simple_entry
);
1087 __isl_give isl_union_set
*isl_union_set_simple_hull(
1088 __isl_take isl_union_set
*uset
)
1090 return isl_union_map_simple_hull(uset
);
1093 static int inplace_entry(void **entry
, void *user
)
1095 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*);
1096 isl_map
**map
= (isl_map
**)entry
;
1099 fn
= *(__isl_give isl_map
*(**)(__isl_take isl_map
*)) user
;
1100 copy
= fn(isl_map_copy(*map
));
1110 static __isl_give isl_union_map
*inplace(__isl_take isl_union_map
*umap
,
1111 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
1116 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1117 &inplace_entry
, &fn
) < 0)
1122 isl_union_map_free(umap
);
1126 __isl_give isl_union_map
*isl_union_map_coalesce(
1127 __isl_take isl_union_map
*umap
)
1129 return inplace(umap
, &isl_map_coalesce
);
1132 __isl_give isl_union_set
*isl_union_set_coalesce(
1133 __isl_take isl_union_set
*uset
)
1135 return isl_union_map_coalesce(uset
);
1138 __isl_give isl_union_map
*isl_union_map_detect_equalities(
1139 __isl_take isl_union_map
*umap
)
1141 return inplace(umap
, &isl_map_detect_equalities
);
1144 __isl_give isl_union_set
*isl_union_set_detect_equalities(
1145 __isl_take isl_union_set
*uset
)
1147 return isl_union_map_detect_equalities(uset
);
1150 __isl_give isl_union_map
*isl_union_map_compute_divs(
1151 __isl_take isl_union_map
*umap
)
1153 return inplace(umap
, &isl_map_compute_divs
);
1156 __isl_give isl_union_set
*isl_union_set_compute_divs(
1157 __isl_take isl_union_set
*uset
)
1159 return isl_union_map_compute_divs(uset
);
1162 static int lexmin_entry(void **entry
, void *user
)
1164 isl_map
**map
= (isl_map
**)entry
;
1166 *map
= isl_map_lexmin(*map
);
1168 return *map
? 0 : -1;
1171 __isl_give isl_union_map
*isl_union_map_lexmin(
1172 __isl_take isl_union_map
*umap
)
1174 return un_op(umap
, &lexmin_entry
);
1177 __isl_give isl_union_set
*isl_union_set_lexmin(
1178 __isl_take isl_union_set
*uset
)
1180 return isl_union_map_lexmin(uset
);
1183 static int lexmax_entry(void **entry
, void *user
)
1185 isl_map
**map
= (isl_map
**)entry
;
1187 *map
= isl_map_lexmax(*map
);
1189 return *map
? 0 : -1;
1192 __isl_give isl_union_map
*isl_union_map_lexmax(
1193 __isl_take isl_union_map
*umap
)
1195 return un_op(umap
, &lexmax_entry
);
1198 __isl_give isl_union_set
*isl_union_set_lexmax(
1199 __isl_take isl_union_set
*uset
)
1201 return isl_union_map_lexmax(uset
);
1204 static __isl_give isl_union_set
*cond_un_op(__isl_take isl_union_map
*umap
,
1205 int (*fn
)(void **, void *))
1212 res
= isl_union_map_alloc(isl_dim_copy(umap
->dim
), umap
->table
.n
);
1213 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
, fn
, &res
) < 0)
1216 isl_union_map_free(umap
);
1219 isl_union_map_free(umap
);
1220 isl_union_set_free(res
);
1224 static int universe_entry(void **entry
, void *user
)
1226 isl_map
*map
= *entry
;
1227 isl_union_map
**res
= user
;
1229 map
= isl_map_universe(isl_map_get_dim(map
));
1230 *res
= isl_union_map_add_map(*res
, map
);
1235 __isl_give isl_union_map
*isl_union_map_universe(__isl_take isl_union_map
*umap
)
1237 return cond_un_op(umap
, &universe_entry
);
1240 __isl_give isl_union_set
*isl_union_set_universe(__isl_take isl_union_set
*uset
)
1242 return isl_union_map_universe(uset
);
1245 static int reverse_entry(void **entry
, void *user
)
1247 isl_map
*map
= *entry
;
1248 isl_union_map
**res
= user
;
1250 *res
= isl_union_map_add_map(*res
, isl_map_reverse(isl_map_copy(map
)));
1255 __isl_give isl_union_map
*isl_union_map_reverse(__isl_take isl_union_map
*umap
)
1257 return cond_un_op(umap
, &reverse_entry
);
1260 static int domain_entry(void **entry
, void *user
)
1262 isl_map
*map
= *entry
;
1263 isl_union_set
**res
= user
;
1265 *res
= isl_union_set_add_set(*res
, isl_map_domain(isl_map_copy(map
)));
1270 __isl_give isl_union_set
*isl_union_map_domain(__isl_take isl_union_map
*umap
)
1272 return cond_un_op(umap
, &domain_entry
);
1275 static int range_entry(void **entry
, void *user
)
1277 isl_map
*map
= *entry
;
1278 isl_union_set
**res
= user
;
1280 *res
= isl_union_set_add_set(*res
, isl_map_range(isl_map_copy(map
)));
1285 __isl_give isl_union_set
*isl_union_map_range(__isl_take isl_union_map
*umap
)
1287 return cond_un_op(umap
, &range_entry
);
1290 static int domain_map_entry(void **entry
, void *user
)
1292 isl_map
*map
= *entry
;
1293 isl_union_set
**res
= user
;
1295 *res
= isl_union_map_add_map(*res
,
1296 isl_map_domain_map(isl_map_copy(map
)));
1301 __isl_give isl_union_map
*isl_union_map_domain_map(
1302 __isl_take isl_union_map
*umap
)
1304 return cond_un_op(umap
, &domain_map_entry
);
1307 static int range_map_entry(void **entry
, void *user
)
1309 isl_map
*map
= *entry
;
1310 isl_union_set
**res
= user
;
1312 *res
= isl_union_map_add_map(*res
,
1313 isl_map_range_map(isl_map_copy(map
)));
1318 __isl_give isl_union_map
*isl_union_map_range_map(
1319 __isl_take isl_union_map
*umap
)
1321 return cond_un_op(umap
, &range_map_entry
);
1324 static int deltas_entry(void **entry
, void *user
)
1326 isl_map
*map
= *entry
;
1327 isl_union_set
**res
= user
;
1329 if (!isl_dim_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
1332 *res
= isl_union_set_add_set(*res
, isl_map_deltas(isl_map_copy(map
)));
1337 __isl_give isl_union_set
*isl_union_map_deltas(__isl_take isl_union_map
*umap
)
1339 return cond_un_op(umap
, &deltas_entry
);
1342 static int deltas_map_entry(void **entry
, void *user
)
1344 isl_map
*map
= *entry
;
1345 isl_union_map
**res
= user
;
1347 if (!isl_dim_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
1350 *res
= isl_union_map_add_map(*res
,
1351 isl_map_deltas_map(isl_map_copy(map
)));
1356 __isl_give isl_union_map
*isl_union_map_deltas_map(
1357 __isl_take isl_union_map
*umap
)
1359 return cond_un_op(umap
, &deltas_map_entry
);
1362 static int identity_entry(void **entry
, void *user
)
1364 isl_set
*set
= *entry
;
1365 isl_union_map
**res
= user
;
1367 *res
= isl_union_map_add_map(*res
, isl_set_identity(isl_set_copy(set
)));
1372 __isl_give isl_union_map
*isl_union_set_identity(__isl_take isl_union_set
*uset
)
1374 return cond_un_op(uset
, &identity_entry
);
1377 static int unwrap_entry(void **entry
, void *user
)
1379 isl_set
*set
= *entry
;
1380 isl_union_set
**res
= user
;
1382 if (!isl_set_is_wrapping(set
))
1385 *res
= isl_union_map_add_map(*res
, isl_set_unwrap(isl_set_copy(set
)));
1390 __isl_give isl_union_map
*isl_union_set_unwrap(__isl_take isl_union_set
*uset
)
1392 return cond_un_op(uset
, &unwrap_entry
);
1395 static int wrap_entry(void **entry
, void *user
)
1397 isl_map
*map
= *entry
;
1398 isl_union_set
**res
= user
;
1400 *res
= isl_union_set_add_set(*res
, isl_map_wrap(isl_map_copy(map
)));
1405 __isl_give isl_union_set
*isl_union_map_wrap(__isl_take isl_union_map
*umap
)
1407 return cond_un_op(umap
, &wrap_entry
);
1410 struct isl_union_map_is_subset_data
{
1411 isl_union_map
*umap2
;
1415 static int is_subset_entry(void **entry
, void *user
)
1417 struct isl_union_map_is_subset_data
*data
= user
;
1419 struct isl_hash_table_entry
*entry2
;
1420 isl_map
*map
= *entry
;
1422 hash
= isl_dim_get_hash(map
->dim
);
1423 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1424 hash
, &has_dim
, map
->dim
, 0);
1426 data
->is_subset
= 0;
1430 data
->is_subset
= isl_map_is_subset(map
, entry2
->data
);
1431 if (data
->is_subset
< 0 || !data
->is_subset
)
1437 int isl_union_map_is_subset(__isl_keep isl_union_map
*umap1
,
1438 __isl_keep isl_union_map
*umap2
)
1440 struct isl_union_map_is_subset_data data
= { NULL
, 1 };
1442 umap1
= isl_union_map_copy(umap1
);
1443 umap2
= isl_union_map_copy(umap2
);
1444 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_dim(umap2
));
1445 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_dim(umap1
));
1447 if (!umap1
|| !umap2
)
1451 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1452 &is_subset_entry
, &data
) < 0 &&
1456 isl_union_map_free(umap1
);
1457 isl_union_map_free(umap2
);
1459 return data
.is_subset
;
1461 isl_union_map_free(umap1
);
1462 isl_union_map_free(umap2
);
1466 int isl_union_set_is_subset(__isl_keep isl_union_set
*uset1
,
1467 __isl_keep isl_union_set
*uset2
)
1469 return isl_union_map_is_subset(uset1
, uset2
);
1472 int isl_union_map_is_equal(__isl_keep isl_union_map
*umap1
,
1473 __isl_keep isl_union_map
*umap2
)
1477 if (!umap1
|| !umap2
)
1479 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
1482 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
1486 int isl_union_set_is_equal(__isl_keep isl_union_set
*uset1
,
1487 __isl_keep isl_union_set
*uset2
)
1489 return isl_union_map_is_equal(uset1
, uset2
);
1492 int isl_union_map_is_strict_subset(__isl_keep isl_union_map
*umap1
,
1493 __isl_keep isl_union_map
*umap2
)
1497 if (!umap1
|| !umap2
)
1499 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
1502 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
1503 if (is_subset
== -1)
1508 int isl_union_set_is_strict_subset(__isl_keep isl_union_set
*uset1
,
1509 __isl_keep isl_union_set
*uset2
)
1511 return isl_union_map_is_strict_subset(uset1
, uset2
);
1514 static int sample_entry(void **entry
, void *user
)
1516 isl_basic_map
**sample
= (isl_basic_map
**)user
;
1517 isl_map
*map
= *entry
;
1519 *sample
= isl_map_sample(isl_map_copy(map
));
1522 if (!isl_basic_map_plain_is_empty(*sample
))
1527 __isl_give isl_basic_map
*isl_union_map_sample(__isl_take isl_union_map
*umap
)
1529 isl_basic_map
*sample
= NULL
;
1534 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1535 &sample_entry
, &sample
) < 0 &&
1540 sample
= isl_basic_map_empty(isl_union_map_get_dim(umap
));
1542 isl_union_map_free(umap
);
1546 isl_union_map_free(umap
);
1550 __isl_give isl_basic_set
*isl_union_set_sample(__isl_take isl_union_set
*uset
)
1552 return (isl_basic_set
*)isl_union_map_sample(uset
);
1555 struct isl_forall_data
{
1557 int (*fn
)(__isl_keep isl_map
*map
);
1560 static int forall_entry(void **entry
, void *user
)
1562 struct isl_forall_data
*data
= user
;
1563 isl_map
*map
= *entry
;
1565 data
->res
= data
->fn(map
);
1575 static int union_map_forall(__isl_keep isl_union_map
*umap
,
1576 int (*fn
)(__isl_keep isl_map
*map
))
1578 struct isl_forall_data data
= { 1, fn
};
1583 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1584 &forall_entry
, &data
) < 0 && data
.res
)
1590 struct isl_forall_user_data
{
1592 int (*fn
)(__isl_keep isl_map
*map
, void *user
);
1596 static int forall_user_entry(void **entry
, void *user
)
1598 struct isl_forall_user_data
*data
= user
;
1599 isl_map
*map
= *entry
;
1601 data
->res
= data
->fn(map
, data
->user
);
1611 /* Check if fn(map, user) returns true for all maps "map" in umap.
1613 static int union_map_forall_user(__isl_keep isl_union_map
*umap
,
1614 int (*fn
)(__isl_keep isl_map
*map
, void *user
), void *user
)
1616 struct isl_forall_user_data data
= { 1, fn
, user
};
1621 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1622 &forall_user_entry
, &data
) < 0 && data
.res
)
1628 int isl_union_map_is_empty(__isl_keep isl_union_map
*umap
)
1630 return union_map_forall(umap
, &isl_map_is_empty
);
1633 int isl_union_set_is_empty(__isl_keep isl_union_set
*uset
)
1635 return isl_union_map_is_empty(uset
);
1638 static int is_subset_of_identity(__isl_keep isl_map
*map
)
1647 if (!isl_dim_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
1650 dim
= isl_map_get_dim(map
);
1651 id
= isl_map_identity(dim
);
1653 is_subset
= isl_map_is_subset(map
, id
);
1660 /* Check if the given map is single-valued.
1665 * and check if the result is a subset of the identity mapping.
1667 int isl_union_map_is_single_valued(__isl_keep isl_union_map
*umap
)
1669 isl_union_map
*test
;
1672 if (isl_union_map_n_map(umap
) == 1) {
1674 umap
= isl_union_map_copy(umap
);
1675 map
= isl_map_from_union_map(umap
);
1676 sv
= isl_map_is_single_valued(map
);
1681 test
= isl_union_map_reverse(isl_union_map_copy(umap
));
1682 test
= isl_union_map_apply_range(test
, isl_union_map_copy(umap
));
1684 sv
= union_map_forall(test
, &is_subset_of_identity
);
1686 isl_union_map_free(test
);
1691 int isl_union_map_is_injective(__isl_keep isl_union_map
*umap
)
1695 umap
= isl_union_map_copy(umap
);
1696 umap
= isl_union_map_reverse(umap
);
1697 in
= isl_union_map_is_single_valued(umap
);
1698 isl_union_map_free(umap
);
1703 /* Represents a map that has a fixed value (v) for one of its
1705 * The map in this structure is not reference counted, so it
1706 * is only valid while the isl_union_map from which it was
1707 * obtained is still alive.
1709 struct isl_fixed_map
{
1714 static struct isl_fixed_map
*alloc_isl_fixed_map_array(isl_ctx
*ctx
,
1718 struct isl_fixed_map
*v
;
1720 v
= isl_calloc_array(ctx
, struct isl_fixed_map
, n
);
1723 for (i
= 0; i
< n
; ++i
)
1724 isl_int_init(v
[i
].v
);
1728 static void free_isl_fixed_map_array(struct isl_fixed_map
*v
, int n
)
1734 for (i
= 0; i
< n
; ++i
)
1735 isl_int_clear(v
[i
].v
);
1739 /* Compare the "v" field of two isl_fixed_map structs.
1741 static int qsort_fixed_map_cmp(const void *p1
, const void *p2
)
1743 const struct isl_fixed_map
*e1
= (const struct isl_fixed_map
*) p1
;
1744 const struct isl_fixed_map
*e2
= (const struct isl_fixed_map
*) p2
;
1746 return isl_int_cmp(e1
->v
, e2
->v
);
1749 /* Internal data structure used while checking whether all maps
1750 * in a union_map have a fixed value for a given output dimension.
1751 * v is the list of maps, with the fixed value for the dimension
1752 * n is the number of maps considered so far
1753 * pos is the output dimension under investigation
1755 struct isl_fixed_dim_data
{
1756 struct isl_fixed_map
*v
;
1761 static int fixed_at_pos(__isl_keep isl_map
*map
, void *user
)
1763 struct isl_fixed_dim_data
*data
= user
;
1765 data
->v
[data
->n
].map
= map
;
1766 return isl_map_plain_is_fixed(map
, isl_dim_out
, data
->pos
,
1767 &data
->v
[data
->n
++].v
);
1770 static int plain_injective_on_range(__isl_take isl_union_map
*umap
,
1771 int first
, int n_range
);
1773 /* Given a list of the maps, with their fixed values at output dimension "pos",
1774 * check whether the ranges of the maps form an obvious partition.
1776 * We first sort the maps according to their fixed values.
1777 * If all maps have a different value, then we know the ranges form
1779 * Otherwise, we collect the maps with the same fixed value and
1780 * check whether each such collection is obviously injective
1781 * based on later dimensions.
1783 static int separates(struct isl_fixed_map
*v
, int n
,
1784 __isl_take isl_dim
*dim
, int pos
, int n_range
)
1791 qsort(v
, n
, sizeof(*v
), &qsort_fixed_map_cmp
);
1793 for (i
= 0; i
+ 1 < n
; ++i
) {
1795 isl_union_map
*part
;
1798 for (j
= i
+ 1; j
< n
; ++j
)
1799 if (isl_int_ne(v
[i
].v
, v
[j
].v
))
1805 part
= isl_union_map_alloc(isl_dim_copy(dim
), j
- i
);
1806 for (k
= i
; k
< j
; ++k
)
1807 part
= isl_union_map_add_map(part
,
1808 isl_map_copy(v
[k
].map
));
1810 injective
= plain_injective_on_range(part
, pos
+ 1, n_range
);
1820 free_isl_fixed_map_array(v
, n
);
1824 free_isl_fixed_map_array(v
, n
);
1828 /* Check whether the maps in umap have obviously distinct ranges.
1829 * In particular, check for an output dimension in the range
1830 * [first,n_range) for which all maps have a fixed value
1831 * and then check if these values, possibly along with fixed values
1832 * at later dimensions, entail distinct ranges.
1834 static int plain_injective_on_range(__isl_take isl_union_map
*umap
,
1835 int first
, int n_range
)
1839 struct isl_fixed_dim_data data
= { NULL
};
1841 ctx
= isl_union_map_get_ctx(umap
);
1846 n
= isl_union_map_n_map(umap
);
1848 isl_union_map_free(umap
);
1852 if (first
>= n_range
) {
1853 isl_union_map_free(umap
);
1857 data
.v
= alloc_isl_fixed_map_array(ctx
, n
);
1861 for (data
.pos
= first
; data
.pos
< n_range
; ++data
.pos
) {
1867 fixed
= union_map_forall_user(umap
, &fixed_at_pos
, &data
);
1872 dim
= isl_union_map_get_dim(umap
);
1873 injective
= separates(data
.v
, n
, dim
, data
.pos
, n_range
);
1874 isl_union_map_free(umap
);
1878 free_isl_fixed_map_array(data
.v
, n
);
1879 isl_union_map_free(umap
);
1883 free_isl_fixed_map_array(data
.v
, n
);
1884 isl_union_map_free(umap
);
1888 /* Check whether the maps in umap that map to subsets of "ran"
1889 * have obviously distinct ranges.
1891 static int plain_injective_on_range_wrap(__isl_keep isl_set
*ran
, void *user
)
1893 isl_union_map
*umap
= user
;
1895 umap
= isl_union_map_copy(umap
);
1896 umap
= isl_union_map_intersect_range(umap
,
1897 isl_union_set_from_set(isl_set_copy(ran
)));
1898 return plain_injective_on_range(umap
, 0, isl_set_dim(ran
, isl_dim_set
));
1901 /* Check if the given union_map is obviously injective.
1903 * In particular, we first check if all individual maps are obviously
1904 * injective and then check if all the ranges of these maps are
1905 * obviously disjoint.
1907 int isl_union_map_plain_is_injective(__isl_keep isl_union_map
*umap
)
1910 isl_union_map
*univ
;
1913 in
= union_map_forall(umap
, &isl_map_plain_is_injective
);
1919 univ
= isl_union_map_universe(isl_union_map_copy(umap
));
1920 ran
= isl_union_map_range(univ
);
1922 in
= union_map_forall_user(ran
, &plain_injective_on_range_wrap
, umap
);
1924 isl_union_set_free(ran
);
1929 int isl_union_map_is_bijective(__isl_keep isl_union_map
*umap
)
1933 sv
= isl_union_map_is_single_valued(umap
);
1937 return isl_union_map_is_injective(umap
);
1940 static int zip_entry(void **entry
, void *user
)
1942 isl_map
*map
= *entry
;
1943 isl_union_map
**res
= user
;
1945 if (!isl_map_can_zip(map
))
1948 *res
= isl_union_map_add_map(*res
, isl_map_zip(isl_map_copy(map
)));
1953 __isl_give isl_union_map
*isl_union_map_zip(__isl_take isl_union_map
*umap
)
1955 return cond_un_op(umap
, &zip_entry
);
1958 static int lift_entry(void **entry
, void *user
)
1960 isl_set
*set
= *entry
;
1961 isl_union_set
**res
= user
;
1963 *res
= isl_union_set_add_set(*res
, isl_set_lift(isl_set_copy(set
)));
1968 __isl_give isl_union_set
*isl_union_set_lift(__isl_take isl_union_set
*uset
)
1970 return cond_un_op(uset
, &lift_entry
);
1973 static int coefficients_entry(void **entry
, void *user
)
1975 isl_set
*set
= *entry
;
1976 isl_union_set
**res
= user
;
1978 set
= isl_set_copy(set
);
1979 set
= isl_set_from_basic_set(isl_set_coefficients(set
));
1980 *res
= isl_union_set_add_set(*res
, set
);
1985 __isl_give isl_union_set
*isl_union_set_coefficients(
1986 __isl_take isl_union_set
*uset
)
1995 ctx
= isl_union_set_get_ctx(uset
);
1996 dim
= isl_dim_set_alloc(ctx
, 0, 0);
1997 res
= isl_union_map_alloc(dim
, uset
->table
.n
);
1998 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
1999 &coefficients_entry
, &res
) < 0)
2002 isl_union_set_free(uset
);
2005 isl_union_set_free(uset
);
2006 isl_union_set_free(res
);
2010 static int solutions_entry(void **entry
, void *user
)
2012 isl_set
*set
= *entry
;
2013 isl_union_set
**res
= user
;
2015 set
= isl_set_copy(set
);
2016 set
= isl_set_from_basic_set(isl_set_solutions(set
));
2018 *res
= isl_union_set_from_set(set
);
2020 *res
= isl_union_set_add_set(*res
, set
);
2028 __isl_give isl_union_set
*isl_union_set_solutions(
2029 __isl_take isl_union_set
*uset
)
2031 isl_union_set
*res
= NULL
;
2036 if (uset
->table
.n
== 0) {
2037 res
= isl_union_set_empty(isl_union_set_get_dim(uset
));
2038 isl_union_set_free(uset
);
2042 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
2043 &solutions_entry
, &res
) < 0)
2046 isl_union_set_free(uset
);
2049 isl_union_set_free(uset
);
2050 isl_union_set_free(res
);