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 static __isl_give isl_union_map
*isl_union_map_alloc(__isl_take isl_space
*dim
,
29 umap
= isl_calloc_type(dim
->ctx
, isl_union_map
);
35 if (isl_hash_table_init(dim
->ctx
, &umap
->table
, size
) < 0)
41 isl_union_map_free(umap
);
45 __isl_give isl_union_map
*isl_union_map_empty(__isl_take isl_space
*dim
)
47 return isl_union_map_alloc(dim
, 16);
50 __isl_give isl_union_set
*isl_union_set_empty(__isl_take isl_space
*dim
)
52 return isl_union_map_empty(dim
);
55 isl_ctx
*isl_union_map_get_ctx(__isl_keep isl_union_map
*umap
)
57 return umap
? umap
->dim
->ctx
: NULL
;
60 isl_ctx
*isl_union_set_get_ctx(__isl_keep isl_union_set
*uset
)
62 return uset
? uset
->dim
->ctx
: NULL
;
65 __isl_give isl_space
*isl_union_map_get_space(__isl_keep isl_union_map
*umap
)
69 return isl_space_copy(umap
->dim
);
72 __isl_give isl_space
*isl_union_set_get_space(__isl_keep isl_union_set
*uset
)
74 return isl_union_map_get_space(uset
);
77 static int free_umap_entry(void **entry
, void *user
)
79 isl_map
*map
= *entry
;
84 static int add_map(__isl_take isl_map
*map
, void *user
)
86 isl_union_map
**umap
= (isl_union_map
**)user
;
88 *umap
= isl_union_map_add_map(*umap
, map
);
93 __isl_give isl_union_map
*isl_union_map_dup(__isl_keep isl_union_map
*umap
)
100 dup
= isl_union_map_empty(isl_space_copy(umap
->dim
));
101 if (isl_union_map_foreach_map(umap
, &add_map
, &dup
) < 0)
105 isl_union_map_free(dup
);
109 __isl_give isl_union_map
*isl_union_map_cow(__isl_take isl_union_map
*umap
)
117 return isl_union_map_dup(umap
);
120 struct isl_union_align
{
125 static int align_entry(void **entry
, void *user
)
127 isl_map
*map
= *entry
;
129 struct isl_union_align
*data
= user
;
131 exp
= isl_reordering_extend_space(isl_reordering_copy(data
->exp
),
132 isl_map_get_space(map
));
134 data
->res
= isl_union_map_add_map(data
->res
,
135 isl_map_realign(isl_map_copy(map
), exp
));
140 /* Align the parameters of umap along those of model.
141 * The result has the parameters of model first, in the same order
142 * as they appear in model, followed by any remaining parameters of
143 * umap that do not appear in model.
145 __isl_give isl_union_map
*isl_union_map_align_params(
146 __isl_take isl_union_map
*umap
, __isl_take isl_space
*model
)
148 struct isl_union_align data
= { NULL
, NULL
};
153 if (isl_space_match(umap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
154 isl_space_free(model
);
158 model
= isl_space_params(model
);
159 data
.exp
= isl_parameter_alignment_reordering(umap
->dim
, model
);
163 data
.res
= isl_union_map_alloc(isl_space_copy(data
.exp
->dim
),
165 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
166 &align_entry
, &data
) < 0)
169 isl_reordering_free(data
.exp
);
170 isl_union_map_free(umap
);
171 isl_space_free(model
);
174 isl_reordering_free(data
.exp
);
175 isl_union_map_free(umap
);
176 isl_union_map_free(data
.res
);
177 isl_space_free(model
);
181 __isl_give isl_union_set
*isl_union_set_align_params(
182 __isl_take isl_union_set
*uset
, __isl_take isl_space
*model
)
184 return isl_union_map_align_params(uset
, model
);
187 __isl_give isl_union_map
*isl_union_map_union(__isl_take isl_union_map
*umap1
,
188 __isl_take isl_union_map
*umap2
)
190 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
191 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
193 umap1
= isl_union_map_cow(umap1
);
195 if (!umap1
|| !umap2
)
198 if (isl_union_map_foreach_map(umap2
, &add_map
, &umap1
) < 0)
201 isl_union_map_free(umap2
);
205 isl_union_map_free(umap1
);
206 isl_union_map_free(umap2
);
210 __isl_give isl_union_set
*isl_union_set_union(__isl_take isl_union_set
*uset1
,
211 __isl_take isl_union_set
*uset2
)
213 return isl_union_map_union(uset1
, uset2
);
216 __isl_give isl_union_map
*isl_union_map_copy(__isl_keep isl_union_map
*umap
)
225 __isl_give isl_union_set
*isl_union_set_copy(__isl_keep isl_union_set
*uset
)
227 return isl_union_map_copy(uset
);
230 void *isl_union_map_free(__isl_take isl_union_map
*umap
)
238 isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
239 &free_umap_entry
, NULL
);
240 isl_hash_table_clear(&umap
->table
);
241 isl_space_free(umap
->dim
);
246 void *isl_union_set_free(__isl_take isl_union_set
*uset
)
248 return isl_union_map_free(uset
);
251 static int has_dim(const void *entry
, const void *val
)
253 isl_map
*map
= (isl_map
*)entry
;
254 isl_space
*dim
= (isl_space
*)val
;
256 return isl_space_is_equal(map
->dim
, dim
);
259 __isl_give isl_union_map
*isl_union_map_add_map(__isl_take isl_union_map
*umap
,
260 __isl_take isl_map
*map
)
263 struct isl_hash_table_entry
*entry
;
268 if (isl_map_plain_is_empty(map
)) {
273 if (!isl_space_match(map
->dim
, isl_dim_param
, umap
->dim
, isl_dim_param
)) {
274 umap
= isl_union_map_align_params(umap
, isl_map_get_space(map
));
275 map
= isl_map_align_params(map
, isl_union_map_get_space(umap
));
278 umap
= isl_union_map_cow(umap
);
283 hash
= isl_space_get_hash(map
->dim
);
284 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
285 &has_dim
, map
->dim
, 1);
292 entry
->data
= isl_map_union(entry
->data
, isl_map_copy(map
));
301 isl_union_map_free(umap
);
305 __isl_give isl_union_set
*isl_union_set_add_set(__isl_take isl_union_set
*uset
,
306 __isl_take isl_set
*set
)
308 return isl_union_map_add_map(uset
, (isl_map
*)set
);
311 __isl_give isl_union_map
*isl_union_map_from_map(__isl_take isl_map
*map
)
319 dim
= isl_map_get_space(map
);
320 dim
= isl_space_params(dim
);
321 umap
= isl_union_map_empty(dim
);
322 umap
= isl_union_map_add_map(umap
, map
);
327 __isl_give isl_union_set
*isl_union_set_from_set(__isl_take isl_set
*set
)
329 return isl_union_map_from_map((isl_map
*)set
);
332 struct isl_union_map_foreach_data
334 int (*fn
)(__isl_take isl_map
*map
, void *user
);
338 static int call_on_copy(void **entry
, void *user
)
340 isl_map
*map
= *entry
;
341 struct isl_union_map_foreach_data
*data
;
342 data
= (struct isl_union_map_foreach_data
*)user
;
344 return data
->fn(isl_map_copy(map
), data
->user
);
347 int isl_union_map_n_map(__isl_keep isl_union_map
*umap
)
349 return umap
? umap
->table
.n
: 0;
352 int isl_union_set_n_set(__isl_keep isl_union_set
*uset
)
354 return uset
? uset
->table
.n
: 0;
357 int isl_union_map_foreach_map(__isl_keep isl_union_map
*umap
,
358 int (*fn
)(__isl_take isl_map
*map
, void *user
), void *user
)
360 struct isl_union_map_foreach_data data
= { fn
, user
};
365 return isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
366 &call_on_copy
, &data
);
369 static int copy_map(void **entry
, void *user
)
371 isl_map
*map
= *entry
;
372 isl_map
**map_p
= user
;
374 *map_p
= isl_map_copy(map
);
379 __isl_give isl_map
*isl_map_from_union_map(__isl_take isl_union_map
*umap
)
386 ctx
= isl_union_map_get_ctx(umap
);
387 if (umap
->table
.n
!= 1)
388 isl_die(ctx
, isl_error_invalid
,
389 "union map needs to contain elements in exactly "
390 "one space", return isl_union_map_free(umap
));
392 isl_hash_table_foreach(ctx
, &umap
->table
, ©_map
, &map
);
394 isl_union_map_free(umap
);
399 __isl_give isl_set
*isl_set_from_union_set(__isl_take isl_union_set
*uset
)
401 return isl_map_from_union_map(uset
);
404 __isl_give isl_map
*isl_union_map_extract_map(__isl_keep isl_union_map
*umap
,
405 __isl_take isl_space
*dim
)
408 struct isl_hash_table_entry
*entry
;
413 hash
= isl_space_get_hash(dim
);
414 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
417 return isl_map_empty(dim
);
419 return isl_map_copy(entry
->data
);
425 __isl_give isl_set
*isl_union_set_extract_set(__isl_keep isl_union_set
*uset
,
426 __isl_take isl_space
*dim
)
428 return (isl_set
*)isl_union_map_extract_map(uset
, dim
);
431 /* Check if umap contains a map in the given space.
433 __isl_give
int isl_union_map_contains(__isl_keep isl_union_map
*umap
,
434 __isl_keep isl_space
*dim
)
437 struct isl_hash_table_entry
*entry
;
442 hash
= isl_space_get_hash(dim
);
443 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
448 __isl_give
int isl_union_set_contains(__isl_keep isl_union_set
*uset
,
449 __isl_keep isl_space
*dim
)
451 return isl_union_map_contains(uset
, dim
);
454 int isl_union_set_foreach_set(__isl_keep isl_union_set
*uset
,
455 int (*fn
)(__isl_take isl_set
*set
, void *user
), void *user
)
457 return isl_union_map_foreach_map(uset
,
458 (int(*)(__isl_take isl_map
*, void*))fn
, user
);
461 struct isl_union_set_foreach_point_data
{
462 int (*fn
)(__isl_take isl_point
*pnt
, void *user
);
466 static int foreach_point(__isl_take isl_set
*set
, void *user
)
468 struct isl_union_set_foreach_point_data
*data
= user
;
471 r
= isl_set_foreach_point(set
, data
->fn
, data
->user
);
477 int isl_union_set_foreach_point(__isl_keep isl_union_set
*uset
,
478 int (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
480 struct isl_union_set_foreach_point_data data
= { fn
, user
};
481 return isl_union_set_foreach_set(uset
, &foreach_point
, &data
);
484 struct isl_union_map_gen_bin_data
{
485 isl_union_map
*umap2
;
489 static int subtract_entry(void **entry
, void *user
)
491 struct isl_union_map_gen_bin_data
*data
= user
;
493 struct isl_hash_table_entry
*entry2
;
494 isl_map
*map
= *entry
;
496 hash
= isl_space_get_hash(map
->dim
);
497 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
498 hash
, &has_dim
, map
->dim
, 0);
499 map
= isl_map_copy(map
);
502 map
= isl_map_subtract(map
, isl_map_copy(entry2
->data
));
504 empty
= isl_map_is_empty(map
);
514 data
->res
= isl_union_map_add_map(data
->res
, map
);
519 static __isl_give isl_union_map
*gen_bin_op(__isl_take isl_union_map
*umap1
,
520 __isl_take isl_union_map
*umap2
, int (*fn
)(void **, void *))
522 struct isl_union_map_gen_bin_data data
= { NULL
, NULL
};
524 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
525 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
527 if (!umap1
|| !umap2
)
531 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
533 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
537 isl_union_map_free(umap1
);
538 isl_union_map_free(umap2
);
541 isl_union_map_free(umap1
);
542 isl_union_map_free(umap2
);
543 isl_union_map_free(data
.res
);
547 __isl_give isl_union_map
*isl_union_map_subtract(
548 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
550 return gen_bin_op(umap1
, umap2
, &subtract_entry
);
553 __isl_give isl_union_set
*isl_union_set_subtract(
554 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
556 return isl_union_map_subtract(uset1
, uset2
);
559 struct isl_union_map_match_bin_data
{
560 isl_union_map
*umap2
;
562 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*);
565 static int match_bin_entry(void **entry
, void *user
)
567 struct isl_union_map_match_bin_data
*data
= user
;
569 struct isl_hash_table_entry
*entry2
;
570 isl_map
*map
= *entry
;
573 hash
= isl_space_get_hash(map
->dim
);
574 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
575 hash
, &has_dim
, map
->dim
, 0);
579 map
= isl_map_copy(map
);
580 map
= data
->fn(map
, isl_map_copy(entry2
->data
));
582 empty
= isl_map_is_empty(map
);
592 data
->res
= isl_union_map_add_map(data
->res
, map
);
597 static __isl_give isl_union_map
*match_bin_op(__isl_take isl_union_map
*umap1
,
598 __isl_take isl_union_map
*umap2
,
599 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*))
601 struct isl_union_map_match_bin_data data
= { NULL
, NULL
, fn
};
603 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
604 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
606 if (!umap1
|| !umap2
)
610 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
612 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
613 &match_bin_entry
, &data
) < 0)
616 isl_union_map_free(umap1
);
617 isl_union_map_free(umap2
);
620 isl_union_map_free(umap1
);
621 isl_union_map_free(umap2
);
622 isl_union_map_free(data
.res
);
626 __isl_give isl_union_map
*isl_union_map_intersect(
627 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
629 return match_bin_op(umap1
, umap2
, &isl_map_intersect
);
632 __isl_give isl_union_set
*isl_union_set_intersect(
633 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
635 return isl_union_map_intersect(uset1
, uset2
);
638 __isl_give isl_union_map
*isl_union_map_gist(__isl_take isl_union_map
*umap
,
639 __isl_take isl_union_map
*context
)
641 return match_bin_op(umap
, context
, &isl_map_gist
);
644 __isl_give isl_union_set
*isl_union_set_gist(__isl_take isl_union_set
*uset
,
645 __isl_take isl_union_set
*context
)
647 return isl_union_map_gist(uset
, context
);
650 static __isl_give isl_map
*lex_le_set(__isl_take isl_map
*set1
,
651 __isl_take isl_map
*set2
)
653 return isl_set_lex_le_set((isl_set
*)set1
, (isl_set
*)set2
);
656 static __isl_give isl_map
*lex_lt_set(__isl_take isl_map
*set1
,
657 __isl_take isl_map
*set2
)
659 return isl_set_lex_lt_set((isl_set
*)set1
, (isl_set
*)set2
);
662 __isl_give isl_union_map
*isl_union_set_lex_lt_union_set(
663 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
665 return match_bin_op(uset1
, uset2
, &lex_lt_set
);
668 __isl_give isl_union_map
*isl_union_set_lex_le_union_set(
669 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
671 return match_bin_op(uset1
, uset2
, &lex_le_set
);
674 __isl_give isl_union_map
*isl_union_set_lex_gt_union_set(
675 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
677 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2
, uset1
));
680 __isl_give isl_union_map
*isl_union_set_lex_ge_union_set(
681 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
683 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2
, uset1
));
686 __isl_give isl_union_map
*isl_union_map_lex_gt_union_map(
687 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
689 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2
, umap1
));
692 __isl_give isl_union_map
*isl_union_map_lex_ge_union_map(
693 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
695 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2
, umap1
));
698 static int intersect_domain_entry(void **entry
, void *user
)
700 struct isl_union_map_gen_bin_data
*data
= user
;
702 struct isl_hash_table_entry
*entry2
;
704 isl_map
*map
= *entry
;
707 dim
= isl_map_get_space(map
);
708 dim
= isl_space_domain(dim
);
709 hash
= isl_space_get_hash(dim
);
710 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
711 hash
, &has_dim
, dim
, 0);
716 map
= isl_map_copy(map
);
717 map
= isl_map_intersect_domain(map
, isl_set_copy(entry2
->data
));
719 empty
= isl_map_is_empty(map
);
729 data
->res
= isl_union_map_add_map(data
->res
, map
);
734 __isl_give isl_union_map
*isl_union_map_intersect_domain(
735 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
737 return gen_bin_op(umap
, uset
, &intersect_domain_entry
);
740 static int intersect_range_entry(void **entry
, void *user
)
742 struct isl_union_map_gen_bin_data
*data
= user
;
744 struct isl_hash_table_entry
*entry2
;
746 isl_map
*map
= *entry
;
749 dim
= isl_map_get_space(map
);
750 dim
= isl_space_range(dim
);
751 hash
= isl_space_get_hash(dim
);
752 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
753 hash
, &has_dim
, dim
, 0);
758 map
= isl_map_copy(map
);
759 map
= isl_map_intersect_range(map
, isl_set_copy(entry2
->data
));
761 empty
= isl_map_is_empty(map
);
771 data
->res
= isl_union_map_add_map(data
->res
, map
);
776 __isl_give isl_union_map
*isl_union_map_intersect_range(
777 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
779 return gen_bin_op(umap
, uset
, &intersect_range_entry
);
782 struct isl_union_map_bin_data
{
783 isl_union_map
*umap2
;
786 int (*fn
)(void **entry
, void *user
);
789 static int apply_range_entry(void **entry
, void *user
)
791 struct isl_union_map_bin_data
*data
= user
;
792 isl_map
*map2
= *entry
;
795 if (!isl_space_tuple_match(data
->map
->dim
, isl_dim_out
,
796 map2
->dim
, isl_dim_in
))
799 map2
= isl_map_apply_range(isl_map_copy(data
->map
), isl_map_copy(map2
));
801 empty
= isl_map_is_empty(map2
);
811 data
->res
= isl_union_map_add_map(data
->res
, map2
);
816 static int bin_entry(void **entry
, void *user
)
818 struct isl_union_map_bin_data
*data
= user
;
819 isl_map
*map
= *entry
;
822 if (isl_hash_table_foreach(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
829 static __isl_give isl_union_map
*bin_op(__isl_take isl_union_map
*umap1
,
830 __isl_take isl_union_map
*umap2
, int (*fn
)(void **entry
, void *user
))
832 struct isl_union_map_bin_data data
= { NULL
, NULL
, NULL
, fn
};
834 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
835 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
837 if (!umap1
|| !umap2
)
841 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
843 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
844 &bin_entry
, &data
) < 0)
847 isl_union_map_free(umap1
);
848 isl_union_map_free(umap2
);
851 isl_union_map_free(umap1
);
852 isl_union_map_free(umap2
);
853 isl_union_map_free(data
.res
);
857 __isl_give isl_union_map
*isl_union_map_apply_range(
858 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
860 return bin_op(umap1
, umap2
, &apply_range_entry
);
863 __isl_give isl_union_map
*isl_union_map_apply_domain(
864 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
866 umap1
= isl_union_map_reverse(umap1
);
867 umap1
= isl_union_map_apply_range(umap1
, umap2
);
868 return isl_union_map_reverse(umap1
);
871 __isl_give isl_union_set
*isl_union_set_apply(
872 __isl_take isl_union_set
*uset
, __isl_take isl_union_map
*umap
)
874 return isl_union_map_apply_range(uset
, umap
);
877 static int map_lex_lt_entry(void **entry
, void *user
)
879 struct isl_union_map_bin_data
*data
= user
;
880 isl_map
*map2
= *entry
;
882 if (!isl_space_tuple_match(data
->map
->dim
, isl_dim_out
,
883 map2
->dim
, isl_dim_out
))
886 map2
= isl_map_lex_lt_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
888 data
->res
= isl_union_map_add_map(data
->res
, map2
);
893 __isl_give isl_union_map
*isl_union_map_lex_lt_union_map(
894 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
896 return bin_op(umap1
, umap2
, &map_lex_lt_entry
);
899 static int map_lex_le_entry(void **entry
, void *user
)
901 struct isl_union_map_bin_data
*data
= user
;
902 isl_map
*map2
= *entry
;
904 if (!isl_space_tuple_match(data
->map
->dim
, isl_dim_out
,
905 map2
->dim
, isl_dim_out
))
908 map2
= isl_map_lex_le_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
910 data
->res
= isl_union_map_add_map(data
->res
, map2
);
915 __isl_give isl_union_map
*isl_union_map_lex_le_union_map(
916 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
918 return bin_op(umap1
, umap2
, &map_lex_le_entry
);
921 static int product_entry(void **entry
, void *user
)
923 struct isl_union_map_bin_data
*data
= user
;
924 isl_map
*map2
= *entry
;
926 map2
= isl_map_product(isl_map_copy(data
->map
), isl_map_copy(map2
));
928 data
->res
= isl_union_map_add_map(data
->res
, map2
);
933 __isl_give isl_union_map
*isl_union_map_product(__isl_take isl_union_map
*umap1
,
934 __isl_take isl_union_map
*umap2
)
936 return bin_op(umap1
, umap2
, &product_entry
);
939 __isl_give isl_union_set
*isl_union_set_product(__isl_take isl_union_set
*uset1
,
940 __isl_take isl_union_set
*uset2
)
942 return isl_union_map_product(uset1
, uset2
);
945 static int range_product_entry(void **entry
, void *user
)
947 struct isl_union_map_bin_data
*data
= user
;
948 isl_map
*map2
= *entry
;
950 if (!isl_space_tuple_match(data
->map
->dim
, isl_dim_in
,
951 map2
->dim
, isl_dim_in
))
954 map2
= isl_map_range_product(isl_map_copy(data
->map
),
957 data
->res
= isl_union_map_add_map(data
->res
, map2
);
962 __isl_give isl_union_map
*isl_union_map_range_product(
963 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
965 return bin_op(umap1
, umap2
, &range_product_entry
);
968 static int flat_range_product_entry(void **entry
, void *user
)
970 struct isl_union_map_bin_data
*data
= user
;
971 isl_map
*map2
= *entry
;
973 if (!isl_space_tuple_match(data
->map
->dim
, isl_dim_in
,
974 map2
->dim
, isl_dim_in
))
977 map2
= isl_map_flat_range_product(isl_map_copy(data
->map
),
980 data
->res
= isl_union_map_add_map(data
->res
, map2
);
985 __isl_give isl_union_map
*isl_union_map_flat_range_product(
986 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
988 return bin_op(umap1
, umap2
, &flat_range_product_entry
);
991 static __isl_give isl_union_set
*cond_un_op(__isl_take isl_union_map
*umap
,
992 int (*fn
)(void **, void *))
999 res
= isl_union_map_alloc(isl_space_copy(umap
->dim
), umap
->table
.n
);
1000 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
, fn
, &res
) < 0)
1003 isl_union_map_free(umap
);
1006 isl_union_map_free(umap
);
1007 isl_union_set_free(res
);
1011 static int from_range_entry(void **entry
, void *user
)
1013 isl_map
*set
= *entry
;
1014 isl_union_set
**res
= user
;
1016 *res
= isl_union_map_add_map(*res
,
1017 isl_map_from_range(isl_set_copy(set
)));
1022 __isl_give isl_union_map
*isl_union_map_from_range(
1023 __isl_take isl_union_set
*uset
)
1025 return cond_un_op(uset
, &from_range_entry
);
1028 __isl_give isl_union_map
*isl_union_map_from_domain(
1029 __isl_take isl_union_set
*uset
)
1031 return isl_union_map_reverse(isl_union_map_from_range(uset
));
1034 __isl_give isl_union_map
*isl_union_map_from_domain_and_range(
1035 __isl_take isl_union_set
*domain
, __isl_take isl_union_set
*range
)
1037 return isl_union_map_apply_range(isl_union_map_from_domain(domain
),
1038 isl_union_map_from_range(range
));
1041 static __isl_give isl_union_map
*un_op(__isl_take isl_union_map
*umap
,
1042 int (*fn
)(void **, void *))
1044 umap
= isl_union_map_cow(umap
);
1048 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
, fn
, NULL
) < 0)
1053 isl_union_map_free(umap
);
1057 static int affine_entry(void **entry
, void *user
)
1059 isl_map
**map
= (isl_map
**)entry
;
1061 *map
= isl_map_from_basic_map(isl_map_affine_hull(*map
));
1063 return *map
? 0 : -1;
1066 __isl_give isl_union_map
*isl_union_map_affine_hull(
1067 __isl_take isl_union_map
*umap
)
1069 return un_op(umap
, &affine_entry
);
1072 __isl_give isl_union_set
*isl_union_set_affine_hull(
1073 __isl_take isl_union_set
*uset
)
1075 return isl_union_map_affine_hull(uset
);
1078 static int polyhedral_entry(void **entry
, void *user
)
1080 isl_map
**map
= (isl_map
**)entry
;
1082 *map
= isl_map_from_basic_map(isl_map_polyhedral_hull(*map
));
1084 return *map
? 0 : -1;
1087 __isl_give isl_union_map
*isl_union_map_polyhedral_hull(
1088 __isl_take isl_union_map
*umap
)
1090 return un_op(umap
, &polyhedral_entry
);
1093 __isl_give isl_union_set
*isl_union_set_polyhedral_hull(
1094 __isl_take isl_union_set
*uset
)
1096 return isl_union_map_polyhedral_hull(uset
);
1099 static int simple_entry(void **entry
, void *user
)
1101 isl_map
**map
= (isl_map
**)entry
;
1103 *map
= isl_map_from_basic_map(isl_map_simple_hull(*map
));
1105 return *map
? 0 : -1;
1108 __isl_give isl_union_map
*isl_union_map_simple_hull(
1109 __isl_take isl_union_map
*umap
)
1111 return un_op(umap
, &simple_entry
);
1114 __isl_give isl_union_set
*isl_union_set_simple_hull(
1115 __isl_take isl_union_set
*uset
)
1117 return isl_union_map_simple_hull(uset
);
1120 static int inplace_entry(void **entry
, void *user
)
1122 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*);
1123 isl_map
**map
= (isl_map
**)entry
;
1126 fn
= *(__isl_give isl_map
*(**)(__isl_take isl_map
*)) user
;
1127 copy
= fn(isl_map_copy(*map
));
1137 static __isl_give isl_union_map
*inplace(__isl_take isl_union_map
*umap
,
1138 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
1143 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1144 &inplace_entry
, &fn
) < 0)
1149 isl_union_map_free(umap
);
1153 __isl_give isl_union_map
*isl_union_map_coalesce(
1154 __isl_take isl_union_map
*umap
)
1156 return inplace(umap
, &isl_map_coalesce
);
1159 __isl_give isl_union_set
*isl_union_set_coalesce(
1160 __isl_take isl_union_set
*uset
)
1162 return isl_union_map_coalesce(uset
);
1165 __isl_give isl_union_map
*isl_union_map_detect_equalities(
1166 __isl_take isl_union_map
*umap
)
1168 return inplace(umap
, &isl_map_detect_equalities
);
1171 __isl_give isl_union_set
*isl_union_set_detect_equalities(
1172 __isl_take isl_union_set
*uset
)
1174 return isl_union_map_detect_equalities(uset
);
1177 __isl_give isl_union_map
*isl_union_map_compute_divs(
1178 __isl_take isl_union_map
*umap
)
1180 return inplace(umap
, &isl_map_compute_divs
);
1183 __isl_give isl_union_set
*isl_union_set_compute_divs(
1184 __isl_take isl_union_set
*uset
)
1186 return isl_union_map_compute_divs(uset
);
1189 static int lexmin_entry(void **entry
, void *user
)
1191 isl_map
**map
= (isl_map
**)entry
;
1193 *map
= isl_map_lexmin(*map
);
1195 return *map
? 0 : -1;
1198 __isl_give isl_union_map
*isl_union_map_lexmin(
1199 __isl_take isl_union_map
*umap
)
1201 return un_op(umap
, &lexmin_entry
);
1204 __isl_give isl_union_set
*isl_union_set_lexmin(
1205 __isl_take isl_union_set
*uset
)
1207 return isl_union_map_lexmin(uset
);
1210 static int lexmax_entry(void **entry
, void *user
)
1212 isl_map
**map
= (isl_map
**)entry
;
1214 *map
= isl_map_lexmax(*map
);
1216 return *map
? 0 : -1;
1219 __isl_give isl_union_map
*isl_union_map_lexmax(
1220 __isl_take isl_union_map
*umap
)
1222 return un_op(umap
, &lexmax_entry
);
1225 __isl_give isl_union_set
*isl_union_set_lexmax(
1226 __isl_take isl_union_set
*uset
)
1228 return isl_union_map_lexmax(uset
);
1231 static int universe_entry(void **entry
, void *user
)
1233 isl_map
*map
= *entry
;
1234 isl_union_map
**res
= user
;
1236 map
= isl_map_universe(isl_map_get_space(map
));
1237 *res
= isl_union_map_add_map(*res
, map
);
1242 __isl_give isl_union_map
*isl_union_map_universe(__isl_take isl_union_map
*umap
)
1244 return cond_un_op(umap
, &universe_entry
);
1247 __isl_give isl_union_set
*isl_union_set_universe(__isl_take isl_union_set
*uset
)
1249 return isl_union_map_universe(uset
);
1252 static int reverse_entry(void **entry
, void *user
)
1254 isl_map
*map
= *entry
;
1255 isl_union_map
**res
= user
;
1257 *res
= isl_union_map_add_map(*res
, isl_map_reverse(isl_map_copy(map
)));
1262 __isl_give isl_union_map
*isl_union_map_reverse(__isl_take isl_union_map
*umap
)
1264 return cond_un_op(umap
, &reverse_entry
);
1267 static int domain_entry(void **entry
, void *user
)
1269 isl_map
*map
= *entry
;
1270 isl_union_set
**res
= user
;
1272 *res
= isl_union_set_add_set(*res
, isl_map_domain(isl_map_copy(map
)));
1277 __isl_give isl_union_set
*isl_union_map_domain(__isl_take isl_union_map
*umap
)
1279 return cond_un_op(umap
, &domain_entry
);
1282 static int range_entry(void **entry
, void *user
)
1284 isl_map
*map
= *entry
;
1285 isl_union_set
**res
= user
;
1287 *res
= isl_union_set_add_set(*res
, isl_map_range(isl_map_copy(map
)));
1292 __isl_give isl_union_set
*isl_union_map_range(__isl_take isl_union_map
*umap
)
1294 return cond_un_op(umap
, &range_entry
);
1297 static int domain_map_entry(void **entry
, void *user
)
1299 isl_map
*map
= *entry
;
1300 isl_union_set
**res
= user
;
1302 *res
= isl_union_map_add_map(*res
,
1303 isl_map_domain_map(isl_map_copy(map
)));
1308 __isl_give isl_union_map
*isl_union_map_domain_map(
1309 __isl_take isl_union_map
*umap
)
1311 return cond_un_op(umap
, &domain_map_entry
);
1314 static int range_map_entry(void **entry
, void *user
)
1316 isl_map
*map
= *entry
;
1317 isl_union_set
**res
= user
;
1319 *res
= isl_union_map_add_map(*res
,
1320 isl_map_range_map(isl_map_copy(map
)));
1325 __isl_give isl_union_map
*isl_union_map_range_map(
1326 __isl_take isl_union_map
*umap
)
1328 return cond_un_op(umap
, &range_map_entry
);
1331 static int deltas_entry(void **entry
, void *user
)
1333 isl_map
*map
= *entry
;
1334 isl_union_set
**res
= user
;
1336 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
1339 *res
= isl_union_set_add_set(*res
, isl_map_deltas(isl_map_copy(map
)));
1344 __isl_give isl_union_set
*isl_union_map_deltas(__isl_take isl_union_map
*umap
)
1346 return cond_un_op(umap
, &deltas_entry
);
1349 static int deltas_map_entry(void **entry
, void *user
)
1351 isl_map
*map
= *entry
;
1352 isl_union_map
**res
= user
;
1354 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
1357 *res
= isl_union_map_add_map(*res
,
1358 isl_map_deltas_map(isl_map_copy(map
)));
1363 __isl_give isl_union_map
*isl_union_map_deltas_map(
1364 __isl_take isl_union_map
*umap
)
1366 return cond_un_op(umap
, &deltas_map_entry
);
1369 static int identity_entry(void **entry
, void *user
)
1371 isl_set
*set
= *entry
;
1372 isl_union_map
**res
= user
;
1374 *res
= isl_union_map_add_map(*res
, isl_set_identity(isl_set_copy(set
)));
1379 __isl_give isl_union_map
*isl_union_set_identity(__isl_take isl_union_set
*uset
)
1381 return cond_un_op(uset
, &identity_entry
);
1384 static int unwrap_entry(void **entry
, void *user
)
1386 isl_set
*set
= *entry
;
1387 isl_union_set
**res
= user
;
1389 if (!isl_set_is_wrapping(set
))
1392 *res
= isl_union_map_add_map(*res
, isl_set_unwrap(isl_set_copy(set
)));
1397 __isl_give isl_union_map
*isl_union_set_unwrap(__isl_take isl_union_set
*uset
)
1399 return cond_un_op(uset
, &unwrap_entry
);
1402 static int wrap_entry(void **entry
, void *user
)
1404 isl_map
*map
= *entry
;
1405 isl_union_set
**res
= user
;
1407 *res
= isl_union_set_add_set(*res
, isl_map_wrap(isl_map_copy(map
)));
1412 __isl_give isl_union_set
*isl_union_map_wrap(__isl_take isl_union_map
*umap
)
1414 return cond_un_op(umap
, &wrap_entry
);
1417 struct isl_union_map_is_subset_data
{
1418 isl_union_map
*umap2
;
1422 static int is_subset_entry(void **entry
, void *user
)
1424 struct isl_union_map_is_subset_data
*data
= user
;
1426 struct isl_hash_table_entry
*entry2
;
1427 isl_map
*map
= *entry
;
1429 hash
= isl_space_get_hash(map
->dim
);
1430 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1431 hash
, &has_dim
, map
->dim
, 0);
1433 data
->is_subset
= 0;
1437 data
->is_subset
= isl_map_is_subset(map
, entry2
->data
);
1438 if (data
->is_subset
< 0 || !data
->is_subset
)
1444 int isl_union_map_is_subset(__isl_keep isl_union_map
*umap1
,
1445 __isl_keep isl_union_map
*umap2
)
1447 struct isl_union_map_is_subset_data data
= { NULL
, 1 };
1449 umap1
= isl_union_map_copy(umap1
);
1450 umap2
= isl_union_map_copy(umap2
);
1451 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
1452 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1454 if (!umap1
|| !umap2
)
1458 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1459 &is_subset_entry
, &data
) < 0 &&
1463 isl_union_map_free(umap1
);
1464 isl_union_map_free(umap2
);
1466 return data
.is_subset
;
1468 isl_union_map_free(umap1
);
1469 isl_union_map_free(umap2
);
1473 int isl_union_set_is_subset(__isl_keep isl_union_set
*uset1
,
1474 __isl_keep isl_union_set
*uset2
)
1476 return isl_union_map_is_subset(uset1
, uset2
);
1479 int isl_union_map_is_equal(__isl_keep isl_union_map
*umap1
,
1480 __isl_keep isl_union_map
*umap2
)
1484 if (!umap1
|| !umap2
)
1486 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
1489 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
1493 int isl_union_set_is_equal(__isl_keep isl_union_set
*uset1
,
1494 __isl_keep isl_union_set
*uset2
)
1496 return isl_union_map_is_equal(uset1
, uset2
);
1499 int isl_union_map_is_strict_subset(__isl_keep isl_union_map
*umap1
,
1500 __isl_keep isl_union_map
*umap2
)
1504 if (!umap1
|| !umap2
)
1506 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
1509 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
1510 if (is_subset
== -1)
1515 int isl_union_set_is_strict_subset(__isl_keep isl_union_set
*uset1
,
1516 __isl_keep isl_union_set
*uset2
)
1518 return isl_union_map_is_strict_subset(uset1
, uset2
);
1521 static int sample_entry(void **entry
, void *user
)
1523 isl_basic_map
**sample
= (isl_basic_map
**)user
;
1524 isl_map
*map
= *entry
;
1526 *sample
= isl_map_sample(isl_map_copy(map
));
1529 if (!isl_basic_map_plain_is_empty(*sample
))
1534 __isl_give isl_basic_map
*isl_union_map_sample(__isl_take isl_union_map
*umap
)
1536 isl_basic_map
*sample
= NULL
;
1541 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1542 &sample_entry
, &sample
) < 0 &&
1547 sample
= isl_basic_map_empty(isl_union_map_get_space(umap
));
1549 isl_union_map_free(umap
);
1553 isl_union_map_free(umap
);
1557 __isl_give isl_basic_set
*isl_union_set_sample(__isl_take isl_union_set
*uset
)
1559 return (isl_basic_set
*)isl_union_map_sample(uset
);
1562 struct isl_forall_data
{
1564 int (*fn
)(__isl_keep isl_map
*map
);
1567 static int forall_entry(void **entry
, void *user
)
1569 struct isl_forall_data
*data
= user
;
1570 isl_map
*map
= *entry
;
1572 data
->res
= data
->fn(map
);
1582 static int union_map_forall(__isl_keep isl_union_map
*umap
,
1583 int (*fn
)(__isl_keep isl_map
*map
))
1585 struct isl_forall_data data
= { 1, fn
};
1590 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1591 &forall_entry
, &data
) < 0 && data
.res
)
1597 struct isl_forall_user_data
{
1599 int (*fn
)(__isl_keep isl_map
*map
, void *user
);
1603 static int forall_user_entry(void **entry
, void *user
)
1605 struct isl_forall_user_data
*data
= user
;
1606 isl_map
*map
= *entry
;
1608 data
->res
= data
->fn(map
, data
->user
);
1618 /* Check if fn(map, user) returns true for all maps "map" in umap.
1620 static int union_map_forall_user(__isl_keep isl_union_map
*umap
,
1621 int (*fn
)(__isl_keep isl_map
*map
, void *user
), void *user
)
1623 struct isl_forall_user_data data
= { 1, fn
, user
};
1628 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1629 &forall_user_entry
, &data
) < 0 && data
.res
)
1635 int isl_union_map_is_empty(__isl_keep isl_union_map
*umap
)
1637 return union_map_forall(umap
, &isl_map_is_empty
);
1640 int isl_union_set_is_empty(__isl_keep isl_union_set
*uset
)
1642 return isl_union_map_is_empty(uset
);
1645 static int is_subset_of_identity(__isl_keep isl_map
*map
)
1654 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
1657 dim
= isl_map_get_space(map
);
1658 id
= isl_map_identity(dim
);
1660 is_subset
= isl_map_is_subset(map
, id
);
1667 /* Check if the given map is single-valued.
1672 * and check if the result is a subset of the identity mapping.
1674 int isl_union_map_is_single_valued(__isl_keep isl_union_map
*umap
)
1676 isl_union_map
*test
;
1679 if (isl_union_map_n_map(umap
) == 1) {
1681 umap
= isl_union_map_copy(umap
);
1682 map
= isl_map_from_union_map(umap
);
1683 sv
= isl_map_is_single_valued(map
);
1688 test
= isl_union_map_reverse(isl_union_map_copy(umap
));
1689 test
= isl_union_map_apply_range(test
, isl_union_map_copy(umap
));
1691 sv
= union_map_forall(test
, &is_subset_of_identity
);
1693 isl_union_map_free(test
);
1698 int isl_union_map_is_injective(__isl_keep isl_union_map
*umap
)
1702 umap
= isl_union_map_copy(umap
);
1703 umap
= isl_union_map_reverse(umap
);
1704 in
= isl_union_map_is_single_valued(umap
);
1705 isl_union_map_free(umap
);
1710 /* Represents a map that has a fixed value (v) for one of its
1712 * The map in this structure is not reference counted, so it
1713 * is only valid while the isl_union_map from which it was
1714 * obtained is still alive.
1716 struct isl_fixed_map
{
1721 static struct isl_fixed_map
*alloc_isl_fixed_map_array(isl_ctx
*ctx
,
1725 struct isl_fixed_map
*v
;
1727 v
= isl_calloc_array(ctx
, struct isl_fixed_map
, n
);
1730 for (i
= 0; i
< n
; ++i
)
1731 isl_int_init(v
[i
].v
);
1735 static void free_isl_fixed_map_array(struct isl_fixed_map
*v
, int n
)
1741 for (i
= 0; i
< n
; ++i
)
1742 isl_int_clear(v
[i
].v
);
1746 /* Compare the "v" field of two isl_fixed_map structs.
1748 static int qsort_fixed_map_cmp(const void *p1
, const void *p2
)
1750 const struct isl_fixed_map
*e1
= (const struct isl_fixed_map
*) p1
;
1751 const struct isl_fixed_map
*e2
= (const struct isl_fixed_map
*) p2
;
1753 return isl_int_cmp(e1
->v
, e2
->v
);
1756 /* Internal data structure used while checking whether all maps
1757 * in a union_map have a fixed value for a given output dimension.
1758 * v is the list of maps, with the fixed value for the dimension
1759 * n is the number of maps considered so far
1760 * pos is the output dimension under investigation
1762 struct isl_fixed_dim_data
{
1763 struct isl_fixed_map
*v
;
1768 static int fixed_at_pos(__isl_keep isl_map
*map
, void *user
)
1770 struct isl_fixed_dim_data
*data
= user
;
1772 data
->v
[data
->n
].map
= map
;
1773 return isl_map_plain_is_fixed(map
, isl_dim_out
, data
->pos
,
1774 &data
->v
[data
->n
++].v
);
1777 static int plain_injective_on_range(__isl_take isl_union_map
*umap
,
1778 int first
, int n_range
);
1780 /* Given a list of the maps, with their fixed values at output dimension "pos",
1781 * check whether the ranges of the maps form an obvious partition.
1783 * We first sort the maps according to their fixed values.
1784 * If all maps have a different value, then we know the ranges form
1786 * Otherwise, we collect the maps with the same fixed value and
1787 * check whether each such collection is obviously injective
1788 * based on later dimensions.
1790 static int separates(struct isl_fixed_map
*v
, int n
,
1791 __isl_take isl_space
*dim
, int pos
, int n_range
)
1798 qsort(v
, n
, sizeof(*v
), &qsort_fixed_map_cmp
);
1800 for (i
= 0; i
+ 1 < n
; ++i
) {
1802 isl_union_map
*part
;
1805 for (j
= i
+ 1; j
< n
; ++j
)
1806 if (isl_int_ne(v
[i
].v
, v
[j
].v
))
1812 part
= isl_union_map_alloc(isl_space_copy(dim
), j
- i
);
1813 for (k
= i
; k
< j
; ++k
)
1814 part
= isl_union_map_add_map(part
,
1815 isl_map_copy(v
[k
].map
));
1817 injective
= plain_injective_on_range(part
, pos
+ 1, n_range
);
1826 isl_space_free(dim
);
1827 free_isl_fixed_map_array(v
, n
);
1830 isl_space_free(dim
);
1831 free_isl_fixed_map_array(v
, n
);
1835 /* Check whether the maps in umap have obviously distinct ranges.
1836 * In particular, check for an output dimension in the range
1837 * [first,n_range) for which all maps have a fixed value
1838 * and then check if these values, possibly along with fixed values
1839 * at later dimensions, entail distinct ranges.
1841 static int plain_injective_on_range(__isl_take isl_union_map
*umap
,
1842 int first
, int n_range
)
1846 struct isl_fixed_dim_data data
= { NULL
};
1848 ctx
= isl_union_map_get_ctx(umap
);
1853 n
= isl_union_map_n_map(umap
);
1855 isl_union_map_free(umap
);
1859 if (first
>= n_range
) {
1860 isl_union_map_free(umap
);
1864 data
.v
= alloc_isl_fixed_map_array(ctx
, n
);
1868 for (data
.pos
= first
; data
.pos
< n_range
; ++data
.pos
) {
1874 fixed
= union_map_forall_user(umap
, &fixed_at_pos
, &data
);
1879 dim
= isl_union_map_get_space(umap
);
1880 injective
= separates(data
.v
, n
, dim
, data
.pos
, n_range
);
1881 isl_union_map_free(umap
);
1885 free_isl_fixed_map_array(data
.v
, n
);
1886 isl_union_map_free(umap
);
1890 free_isl_fixed_map_array(data
.v
, n
);
1891 isl_union_map_free(umap
);
1895 /* Check whether the maps in umap that map to subsets of "ran"
1896 * have obviously distinct ranges.
1898 static int plain_injective_on_range_wrap(__isl_keep isl_set
*ran
, void *user
)
1900 isl_union_map
*umap
= user
;
1902 umap
= isl_union_map_copy(umap
);
1903 umap
= isl_union_map_intersect_range(umap
,
1904 isl_union_set_from_set(isl_set_copy(ran
)));
1905 return plain_injective_on_range(umap
, 0, isl_set_dim(ran
, isl_dim_set
));
1908 /* Check if the given union_map is obviously injective.
1910 * In particular, we first check if all individual maps are obviously
1911 * injective and then check if all the ranges of these maps are
1912 * obviously disjoint.
1914 int isl_union_map_plain_is_injective(__isl_keep isl_union_map
*umap
)
1917 isl_union_map
*univ
;
1920 in
= union_map_forall(umap
, &isl_map_plain_is_injective
);
1926 univ
= isl_union_map_universe(isl_union_map_copy(umap
));
1927 ran
= isl_union_map_range(univ
);
1929 in
= union_map_forall_user(ran
, &plain_injective_on_range_wrap
, umap
);
1931 isl_union_set_free(ran
);
1936 int isl_union_map_is_bijective(__isl_keep isl_union_map
*umap
)
1940 sv
= isl_union_map_is_single_valued(umap
);
1944 return isl_union_map_is_injective(umap
);
1947 static int zip_entry(void **entry
, void *user
)
1949 isl_map
*map
= *entry
;
1950 isl_union_map
**res
= user
;
1952 if (!isl_map_can_zip(map
))
1955 *res
= isl_union_map_add_map(*res
, isl_map_zip(isl_map_copy(map
)));
1960 __isl_give isl_union_map
*isl_union_map_zip(__isl_take isl_union_map
*umap
)
1962 return cond_un_op(umap
, &zip_entry
);
1965 static int lift_entry(void **entry
, void *user
)
1967 isl_set
*set
= *entry
;
1968 isl_union_set
**res
= user
;
1970 *res
= isl_union_set_add_set(*res
, isl_set_lift(isl_set_copy(set
)));
1975 __isl_give isl_union_set
*isl_union_set_lift(__isl_take isl_union_set
*uset
)
1977 return cond_un_op(uset
, &lift_entry
);
1980 static int coefficients_entry(void **entry
, void *user
)
1982 isl_set
*set
= *entry
;
1983 isl_union_set
**res
= user
;
1985 set
= isl_set_copy(set
);
1986 set
= isl_set_from_basic_set(isl_set_coefficients(set
));
1987 *res
= isl_union_set_add_set(*res
, set
);
1992 __isl_give isl_union_set
*isl_union_set_coefficients(
1993 __isl_take isl_union_set
*uset
)
2002 ctx
= isl_union_set_get_ctx(uset
);
2003 dim
= isl_space_set_alloc(ctx
, 0, 0);
2004 res
= isl_union_map_alloc(dim
, uset
->table
.n
);
2005 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
2006 &coefficients_entry
, &res
) < 0)
2009 isl_union_set_free(uset
);
2012 isl_union_set_free(uset
);
2013 isl_union_set_free(res
);
2017 static int solutions_entry(void **entry
, void *user
)
2019 isl_set
*set
= *entry
;
2020 isl_union_set
**res
= user
;
2022 set
= isl_set_copy(set
);
2023 set
= isl_set_from_basic_set(isl_set_solutions(set
));
2025 *res
= isl_union_set_from_set(set
);
2027 *res
= isl_union_set_add_set(*res
, set
);
2035 __isl_give isl_union_set
*isl_union_set_solutions(
2036 __isl_take isl_union_set
*uset
)
2038 isl_union_set
*res
= NULL
;
2043 if (uset
->table
.n
== 0) {
2044 res
= isl_union_set_empty(isl_union_set_get_space(uset
));
2045 isl_union_set_free(uset
);
2049 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
2050 &solutions_entry
, &res
) < 0)
2053 isl_union_set_free(uset
);
2056 isl_union_set_free(uset
);
2057 isl_union_set_free(res
);