add isl_union_set_is_params
[isl.git] / isl_union_map.c
blobc044e5b5d94bcb8e1fca8d8b8f2430ccc2ce6b44
1 /*
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,
8 * 91893 Orsay, France
9 */
11 #define ISL_DIM_H
12 #include <isl_map_private.h>
13 #include <isl/ctx.h>
14 #include <isl/hash.h>
15 #include <isl/map.h>
16 #include <isl/set.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)
25 isl_set *set;
26 int params;
28 if (!uset)
29 return -1;
30 if (uset->table.n != 1)
31 return 0;
33 set = isl_set_from_union_set(isl_union_set_copy(uset));
34 params = isl_set_is_params(set);
35 isl_set_free(set);
36 return params;
39 static __isl_give isl_union_map *isl_union_map_alloc(__isl_take isl_space *dim,
40 int size)
42 isl_union_map *umap;
44 if (!dim)
45 return NULL;
47 umap = isl_calloc_type(dim->ctx, isl_union_map);
48 if (!umap)
49 return NULL;
51 umap->ref = 1;
52 umap->dim = dim;
53 if (isl_hash_table_init(dim->ctx, &umap->table, size) < 0)
54 goto error;
56 return umap;
57 error:
58 isl_space_free(dim);
59 isl_union_map_free(umap);
60 return NULL;
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)
85 if (!umap)
86 return NULL;
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;
98 isl_map_free(map);
99 return 0;
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);
108 return 0;
111 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
113 isl_union_map *dup;
115 if (!umap)
116 return NULL;
118 dup = isl_union_map_empty(isl_space_copy(umap->dim));
119 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
120 goto error;
121 return dup;
122 error:
123 isl_union_map_free(dup);
124 return NULL;
127 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
129 if (!umap)
130 return NULL;
132 if (umap->ref == 1)
133 return umap;
134 umap->ref--;
135 return isl_union_map_dup(umap);
138 struct isl_union_align {
139 isl_reordering *exp;
140 isl_union_map *res;
143 static int align_entry(void **entry, void *user)
145 isl_map *map = *entry;
146 isl_reordering *exp;
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));
155 return 0;
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 };
168 if (!umap || !model)
169 goto error;
171 if (isl_space_match(umap->dim, isl_dim_param, model, isl_dim_param)) {
172 isl_space_free(model);
173 return umap;
176 model = isl_space_params(model);
177 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
178 if (!data.exp)
179 goto error;
181 data.res = isl_union_map_alloc(isl_space_copy(data.exp->dim),
182 umap->table.n);
183 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
184 &align_entry, &data) < 0)
185 goto error;
187 isl_reordering_free(data.exp);
188 isl_union_map_free(umap);
189 isl_space_free(model);
190 return data.res;
191 error:
192 isl_reordering_free(data.exp);
193 isl_union_map_free(umap);
194 isl_union_map_free(data.res);
195 isl_space_free(model);
196 return NULL;
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)
214 goto error;
216 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
217 goto error;
219 isl_union_map_free(umap2);
221 return umap1;
222 error:
223 isl_union_map_free(umap1);
224 isl_union_map_free(umap2);
225 return NULL;
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)
236 if (!umap)
237 return NULL;
239 umap->ref++;
240 return 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)
250 if (!umap)
251 return NULL;
253 if (--umap->ref > 0)
254 return NULL;
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);
260 free(umap);
261 return NULL;
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)
280 uint32_t hash;
281 struct isl_hash_table_entry *entry;
283 if (!map || !umap)
284 goto error;
286 if (isl_map_plain_is_empty(map)) {
287 isl_map_free(map);
288 return umap;
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);
298 if (!map || !umap)
299 goto error;
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);
304 if (!entry)
305 goto error;
307 if (!entry->data)
308 entry->data = map;
309 else {
310 entry->data = isl_map_union(entry->data, isl_map_copy(map));
311 if (!entry->data)
312 goto error;
313 isl_map_free(map);
316 return umap;
317 error:
318 isl_map_free(map);
319 isl_union_map_free(umap);
320 return NULL;
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)
331 isl_space *dim;
332 isl_union_map *umap;
334 if (!map)
335 return NULL;
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);
342 return umap;
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);
353 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 };
380 if (!umap)
381 return -1;
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);
394 return -1;
397 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
399 isl_ctx *ctx;
400 isl_map *map = NULL;
402 if (!umap)
403 return NULL;
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, &copy_map, &map);
412 isl_union_map_free(umap);
414 return map;
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)
425 uint32_t hash;
426 struct isl_hash_table_entry *entry;
428 if (!umap || !dim)
429 goto error;
431 hash = isl_space_get_hash(dim);
432 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
433 &has_dim, dim, 0);
434 if (!entry)
435 return isl_map_empty(dim);
436 isl_space_free(dim);
437 return isl_map_copy(entry->data);
438 error:
439 isl_space_free(dim);
440 return NULL;
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)
454 uint32_t hash;
455 struct isl_hash_table_entry *entry;
457 if (!umap || !dim)
458 return -1;
460 hash = isl_space_get_hash(dim);
461 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
462 &has_dim, dim, 0);
463 return !!entry;
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);
481 void *user;
484 static int foreach_point(__isl_take isl_set *set, void *user)
486 struct isl_union_set_foreach_point_data *data = user;
487 int r;
489 r = isl_set_foreach_point(set, data->fn, data->user);
490 isl_set_free(set);
492 return r;
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;
504 isl_union_map *res;
507 static int subtract_entry(void **entry, void *user)
509 struct isl_union_map_gen_bin_data *data = user;
510 uint32_t hash;
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);
518 if (entry2) {
519 int empty;
520 map = isl_map_subtract(map, isl_map_copy(entry2->data));
522 empty = isl_map_is_empty(map);
523 if (empty < 0) {
524 isl_map_free(map);
525 return -1;
527 if (empty) {
528 isl_map_free(map);
529 return 0;
532 data->res = isl_union_map_add_map(data->res, map);
534 return 0;
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)
546 goto error;
548 data.umap2 = umap2;
549 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
550 umap1->table.n);
551 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
552 fn, &data) < 0)
553 goto error;
555 isl_union_map_free(umap1);
556 isl_union_map_free(umap2);
557 return data.res;
558 error:
559 isl_union_map_free(umap1);
560 isl_union_map_free(umap2);
561 isl_union_map_free(data.res);
562 return NULL;
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_match_bin_data {
578 isl_union_map *umap2;
579 isl_union_map *res;
580 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
583 static int match_bin_entry(void **entry, void *user)
585 struct isl_union_map_match_bin_data *data = user;
586 uint32_t hash;
587 struct isl_hash_table_entry *entry2;
588 isl_map *map = *entry;
589 int empty;
591 hash = isl_space_get_hash(map->dim);
592 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
593 hash, &has_dim, map->dim, 0);
594 if (!entry2)
595 return 0;
597 map = isl_map_copy(map);
598 map = data->fn(map, isl_map_copy(entry2->data));
600 empty = isl_map_is_empty(map);
601 if (empty < 0) {
602 isl_map_free(map);
603 return -1;
605 if (empty) {
606 isl_map_free(map);
607 return 0;
610 data->res = isl_union_map_add_map(data->res, map);
612 return 0;
615 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
616 __isl_take isl_union_map *umap2,
617 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
619 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
621 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
622 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
624 if (!umap1 || !umap2)
625 goto error;
627 data.umap2 = umap2;
628 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
629 umap1->table.n);
630 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
631 &match_bin_entry, &data) < 0)
632 goto error;
634 isl_union_map_free(umap1);
635 isl_union_map_free(umap2);
636 return data.res;
637 error:
638 isl_union_map_free(umap1);
639 isl_union_map_free(umap2);
640 isl_union_map_free(data.res);
641 return NULL;
644 __isl_give isl_union_map *isl_union_map_intersect(
645 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
647 return match_bin_op(umap1, umap2, &isl_map_intersect);
650 __isl_give isl_union_set *isl_union_set_intersect(
651 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
653 return isl_union_map_intersect(uset1, uset2);
656 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
657 __isl_take isl_union_map *context)
659 return match_bin_op(umap, context, &isl_map_gist);
662 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
663 __isl_take isl_union_set *context)
665 return isl_union_map_gist(uset, context);
668 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
669 __isl_take isl_map *set2)
671 return isl_set_lex_le_set((isl_set *)set1, (isl_set *)set2);
674 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
675 __isl_take isl_map *set2)
677 return isl_set_lex_lt_set((isl_set *)set1, (isl_set *)set2);
680 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
681 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
683 return match_bin_op(uset1, uset2, &lex_lt_set);
686 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
687 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
689 return match_bin_op(uset1, uset2, &lex_le_set);
692 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
693 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
695 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
698 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
699 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
701 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
704 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
705 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
707 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
710 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
711 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
713 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
716 static int intersect_domain_entry(void **entry, void *user)
718 struct isl_union_map_gen_bin_data *data = user;
719 uint32_t hash;
720 struct isl_hash_table_entry *entry2;
721 isl_space *dim;
722 isl_map *map = *entry;
723 int empty;
725 dim = isl_map_get_space(map);
726 dim = isl_space_domain(dim);
727 hash = isl_space_get_hash(dim);
728 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
729 hash, &has_dim, dim, 0);
730 isl_space_free(dim);
731 if (!entry2)
732 return 0;
734 map = isl_map_copy(map);
735 map = isl_map_intersect_domain(map, isl_set_copy(entry2->data));
737 empty = isl_map_is_empty(map);
738 if (empty < 0) {
739 isl_map_free(map);
740 return -1;
742 if (empty) {
743 isl_map_free(map);
744 return 0;
747 data->res = isl_union_map_add_map(data->res, map);
749 return 0;
752 __isl_give isl_union_map *isl_union_map_intersect_domain(
753 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
755 return gen_bin_op(umap, uset, &intersect_domain_entry);
758 static int intersect_range_entry(void **entry, void *user)
760 struct isl_union_map_gen_bin_data *data = user;
761 uint32_t hash;
762 struct isl_hash_table_entry *entry2;
763 isl_space *dim;
764 isl_map *map = *entry;
765 int empty;
767 dim = isl_map_get_space(map);
768 dim = isl_space_range(dim);
769 hash = isl_space_get_hash(dim);
770 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
771 hash, &has_dim, dim, 0);
772 isl_space_free(dim);
773 if (!entry2)
774 return 0;
776 map = isl_map_copy(map);
777 map = isl_map_intersect_range(map, isl_set_copy(entry2->data));
779 empty = isl_map_is_empty(map);
780 if (empty < 0) {
781 isl_map_free(map);
782 return -1;
784 if (empty) {
785 isl_map_free(map);
786 return 0;
789 data->res = isl_union_map_add_map(data->res, map);
791 return 0;
794 __isl_give isl_union_map *isl_union_map_intersect_range(
795 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
797 return gen_bin_op(umap, uset, &intersect_range_entry);
800 struct isl_union_map_bin_data {
801 isl_union_map *umap2;
802 isl_union_map *res;
803 isl_map *map;
804 int (*fn)(void **entry, void *user);
807 static int apply_range_entry(void **entry, void *user)
809 struct isl_union_map_bin_data *data = user;
810 isl_map *map2 = *entry;
811 int empty;
813 if (!isl_space_tuple_match(data->map->dim, isl_dim_out,
814 map2->dim, isl_dim_in))
815 return 0;
817 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
819 empty = isl_map_is_empty(map2);
820 if (empty < 0) {
821 isl_map_free(map2);
822 return -1;
824 if (empty) {
825 isl_map_free(map2);
826 return 0;
829 data->res = isl_union_map_add_map(data->res, map2);
831 return 0;
834 static int bin_entry(void **entry, void *user)
836 struct isl_union_map_bin_data *data = user;
837 isl_map *map = *entry;
839 data->map = map;
840 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
841 data->fn, data) < 0)
842 return -1;
844 return 0;
847 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
848 __isl_take isl_union_map *umap2, int (*fn)(void **entry, void *user))
850 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
852 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
853 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
855 if (!umap1 || !umap2)
856 goto error;
858 data.umap2 = umap2;
859 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
860 umap1->table.n);
861 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
862 &bin_entry, &data) < 0)
863 goto error;
865 isl_union_map_free(umap1);
866 isl_union_map_free(umap2);
867 return data.res;
868 error:
869 isl_union_map_free(umap1);
870 isl_union_map_free(umap2);
871 isl_union_map_free(data.res);
872 return NULL;
875 __isl_give isl_union_map *isl_union_map_apply_range(
876 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
878 return bin_op(umap1, umap2, &apply_range_entry);
881 __isl_give isl_union_map *isl_union_map_apply_domain(
882 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
884 umap1 = isl_union_map_reverse(umap1);
885 umap1 = isl_union_map_apply_range(umap1, umap2);
886 return isl_union_map_reverse(umap1);
889 __isl_give isl_union_set *isl_union_set_apply(
890 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
892 return isl_union_map_apply_range(uset, umap);
895 static int map_lex_lt_entry(void **entry, void *user)
897 struct isl_union_map_bin_data *data = user;
898 isl_map *map2 = *entry;
900 if (!isl_space_tuple_match(data->map->dim, isl_dim_out,
901 map2->dim, isl_dim_out))
902 return 0;
904 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
906 data->res = isl_union_map_add_map(data->res, map2);
908 return 0;
911 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
912 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
914 return bin_op(umap1, umap2, &map_lex_lt_entry);
917 static int map_lex_le_entry(void **entry, void *user)
919 struct isl_union_map_bin_data *data = user;
920 isl_map *map2 = *entry;
922 if (!isl_space_tuple_match(data->map->dim, isl_dim_out,
923 map2->dim, isl_dim_out))
924 return 0;
926 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
928 data->res = isl_union_map_add_map(data->res, map2);
930 return 0;
933 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
934 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
936 return bin_op(umap1, umap2, &map_lex_le_entry);
939 static int product_entry(void **entry, void *user)
941 struct isl_union_map_bin_data *data = user;
942 isl_map *map2 = *entry;
944 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
946 data->res = isl_union_map_add_map(data->res, map2);
948 return 0;
951 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
952 __isl_take isl_union_map *umap2)
954 return bin_op(umap1, umap2, &product_entry);
957 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
958 __isl_take isl_union_set *uset2)
960 return isl_union_map_product(uset1, uset2);
963 static int range_product_entry(void **entry, void *user)
965 struct isl_union_map_bin_data *data = user;
966 isl_map *map2 = *entry;
968 if (!isl_space_tuple_match(data->map->dim, isl_dim_in,
969 map2->dim, isl_dim_in))
970 return 0;
972 map2 = isl_map_range_product(isl_map_copy(data->map),
973 isl_map_copy(map2));
975 data->res = isl_union_map_add_map(data->res, map2);
977 return 0;
980 __isl_give isl_union_map *isl_union_map_range_product(
981 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
983 return bin_op(umap1, umap2, &range_product_entry);
986 static int flat_range_product_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_in,
992 map2->dim, isl_dim_in))
993 return 0;
995 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
996 isl_map_copy(map2));
998 data->res = isl_union_map_add_map(data->res, map2);
1000 return 0;
1003 __isl_give isl_union_map *isl_union_map_flat_range_product(
1004 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1006 return bin_op(umap1, umap2, &flat_range_product_entry);
1009 static __isl_give isl_union_set *cond_un_op(__isl_take isl_union_map *umap,
1010 int (*fn)(void **, void *))
1012 isl_union_set *res;
1014 if (!umap)
1015 return NULL;
1017 res = isl_union_map_alloc(isl_space_copy(umap->dim), umap->table.n);
1018 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, &res) < 0)
1019 goto error;
1021 isl_union_map_free(umap);
1022 return res;
1023 error:
1024 isl_union_map_free(umap);
1025 isl_union_set_free(res);
1026 return NULL;
1029 static int from_range_entry(void **entry, void *user)
1031 isl_map *set = *entry;
1032 isl_union_set **res = user;
1034 *res = isl_union_map_add_map(*res,
1035 isl_map_from_range(isl_set_copy(set)));
1037 return 0;
1040 __isl_give isl_union_map *isl_union_map_from_range(
1041 __isl_take isl_union_set *uset)
1043 return cond_un_op(uset, &from_range_entry);
1046 __isl_give isl_union_map *isl_union_map_from_domain(
1047 __isl_take isl_union_set *uset)
1049 return isl_union_map_reverse(isl_union_map_from_range(uset));
1052 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1053 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1055 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1056 isl_union_map_from_range(range));
1059 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1060 int (*fn)(void **, void *))
1062 umap = isl_union_map_cow(umap);
1063 if (!umap)
1064 return NULL;
1066 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, NULL) < 0)
1067 goto error;
1069 return umap;
1070 error:
1071 isl_union_map_free(umap);
1072 return NULL;
1075 static int affine_entry(void **entry, void *user)
1077 isl_map **map = (isl_map **)entry;
1079 *map = isl_map_from_basic_map(isl_map_affine_hull(*map));
1081 return *map ? 0 : -1;
1084 __isl_give isl_union_map *isl_union_map_affine_hull(
1085 __isl_take isl_union_map *umap)
1087 return un_op(umap, &affine_entry);
1090 __isl_give isl_union_set *isl_union_set_affine_hull(
1091 __isl_take isl_union_set *uset)
1093 return isl_union_map_affine_hull(uset);
1096 static int polyhedral_entry(void **entry, void *user)
1098 isl_map **map = (isl_map **)entry;
1100 *map = isl_map_from_basic_map(isl_map_polyhedral_hull(*map));
1102 return *map ? 0 : -1;
1105 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1106 __isl_take isl_union_map *umap)
1108 return un_op(umap, &polyhedral_entry);
1111 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1112 __isl_take isl_union_set *uset)
1114 return isl_union_map_polyhedral_hull(uset);
1117 static int simple_entry(void **entry, void *user)
1119 isl_map **map = (isl_map **)entry;
1121 *map = isl_map_from_basic_map(isl_map_simple_hull(*map));
1123 return *map ? 0 : -1;
1126 __isl_give isl_union_map *isl_union_map_simple_hull(
1127 __isl_take isl_union_map *umap)
1129 return un_op(umap, &simple_entry);
1132 __isl_give isl_union_set *isl_union_set_simple_hull(
1133 __isl_take isl_union_set *uset)
1135 return isl_union_map_simple_hull(uset);
1138 static int inplace_entry(void **entry, void *user)
1140 __isl_give isl_map *(*fn)(__isl_take isl_map *);
1141 isl_map **map = (isl_map **)entry;
1142 isl_map *copy;
1144 fn = *(__isl_give isl_map *(**)(__isl_take isl_map *)) user;
1145 copy = fn(isl_map_copy(*map));
1146 if (!copy)
1147 return -1;
1149 isl_map_free(*map);
1150 *map = copy;
1152 return 0;
1155 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1156 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1158 if (!umap)
1159 return NULL;
1161 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1162 &inplace_entry, &fn) < 0)
1163 goto error;
1165 return umap;
1166 error:
1167 isl_union_map_free(umap);
1168 return NULL;
1171 __isl_give isl_union_map *isl_union_map_coalesce(
1172 __isl_take isl_union_map *umap)
1174 return inplace(umap, &isl_map_coalesce);
1177 __isl_give isl_union_set *isl_union_set_coalesce(
1178 __isl_take isl_union_set *uset)
1180 return isl_union_map_coalesce(uset);
1183 __isl_give isl_union_map *isl_union_map_detect_equalities(
1184 __isl_take isl_union_map *umap)
1186 return inplace(umap, &isl_map_detect_equalities);
1189 __isl_give isl_union_set *isl_union_set_detect_equalities(
1190 __isl_take isl_union_set *uset)
1192 return isl_union_map_detect_equalities(uset);
1195 __isl_give isl_union_map *isl_union_map_compute_divs(
1196 __isl_take isl_union_map *umap)
1198 return inplace(umap, &isl_map_compute_divs);
1201 __isl_give isl_union_set *isl_union_set_compute_divs(
1202 __isl_take isl_union_set *uset)
1204 return isl_union_map_compute_divs(uset);
1207 static int lexmin_entry(void **entry, void *user)
1209 isl_map **map = (isl_map **)entry;
1211 *map = isl_map_lexmin(*map);
1213 return *map ? 0 : -1;
1216 __isl_give isl_union_map *isl_union_map_lexmin(
1217 __isl_take isl_union_map *umap)
1219 return un_op(umap, &lexmin_entry);
1222 __isl_give isl_union_set *isl_union_set_lexmin(
1223 __isl_take isl_union_set *uset)
1225 return isl_union_map_lexmin(uset);
1228 static int lexmax_entry(void **entry, void *user)
1230 isl_map **map = (isl_map **)entry;
1232 *map = isl_map_lexmax(*map);
1234 return *map ? 0 : -1;
1237 __isl_give isl_union_map *isl_union_map_lexmax(
1238 __isl_take isl_union_map *umap)
1240 return un_op(umap, &lexmax_entry);
1243 __isl_give isl_union_set *isl_union_set_lexmax(
1244 __isl_take isl_union_set *uset)
1246 return isl_union_map_lexmax(uset);
1249 static int universe_entry(void **entry, void *user)
1251 isl_map *map = *entry;
1252 isl_union_map **res = user;
1254 map = isl_map_universe(isl_map_get_space(map));
1255 *res = isl_union_map_add_map(*res, map);
1257 return 0;
1260 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1262 return cond_un_op(umap, &universe_entry);
1265 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1267 return isl_union_map_universe(uset);
1270 static int reverse_entry(void **entry, void *user)
1272 isl_map *map = *entry;
1273 isl_union_map **res = user;
1275 *res = isl_union_map_add_map(*res, isl_map_reverse(isl_map_copy(map)));
1277 return 0;
1280 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1282 return cond_un_op(umap, &reverse_entry);
1285 static int domain_entry(void **entry, void *user)
1287 isl_map *map = *entry;
1288 isl_union_set **res = user;
1290 *res = isl_union_set_add_set(*res, isl_map_domain(isl_map_copy(map)));
1292 return 0;
1295 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1297 return cond_un_op(umap, &domain_entry);
1300 static int range_entry(void **entry, void *user)
1302 isl_map *map = *entry;
1303 isl_union_set **res = user;
1305 *res = isl_union_set_add_set(*res, isl_map_range(isl_map_copy(map)));
1307 return 0;
1310 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1312 return cond_un_op(umap, &range_entry);
1315 static int domain_map_entry(void **entry, void *user)
1317 isl_map *map = *entry;
1318 isl_union_set **res = user;
1320 *res = isl_union_map_add_map(*res,
1321 isl_map_domain_map(isl_map_copy(map)));
1323 return 0;
1326 __isl_give isl_union_map *isl_union_map_domain_map(
1327 __isl_take isl_union_map *umap)
1329 return cond_un_op(umap, &domain_map_entry);
1332 static int range_map_entry(void **entry, void *user)
1334 isl_map *map = *entry;
1335 isl_union_set **res = user;
1337 *res = isl_union_map_add_map(*res,
1338 isl_map_range_map(isl_map_copy(map)));
1340 return 0;
1343 __isl_give isl_union_map *isl_union_map_range_map(
1344 __isl_take isl_union_map *umap)
1346 return cond_un_op(umap, &range_map_entry);
1349 static int deltas_entry(void **entry, void *user)
1351 isl_map *map = *entry;
1352 isl_union_set **res = user;
1354 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1355 return 0;
1357 *res = isl_union_set_add_set(*res, isl_map_deltas(isl_map_copy(map)));
1359 return 0;
1362 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
1364 return cond_un_op(umap, &deltas_entry);
1367 static int deltas_map_entry(void **entry, void *user)
1369 isl_map *map = *entry;
1370 isl_union_map **res = user;
1372 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1373 return 0;
1375 *res = isl_union_map_add_map(*res,
1376 isl_map_deltas_map(isl_map_copy(map)));
1378 return 0;
1381 __isl_give isl_union_map *isl_union_map_deltas_map(
1382 __isl_take isl_union_map *umap)
1384 return cond_un_op(umap, &deltas_map_entry);
1387 static int identity_entry(void **entry, void *user)
1389 isl_set *set = *entry;
1390 isl_union_map **res = user;
1392 *res = isl_union_map_add_map(*res, isl_set_identity(isl_set_copy(set)));
1394 return 0;
1397 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
1399 return cond_un_op(uset, &identity_entry);
1402 static int unwrap_entry(void **entry, void *user)
1404 isl_set *set = *entry;
1405 isl_union_set **res = user;
1407 if (!isl_set_is_wrapping(set))
1408 return 0;
1410 *res = isl_union_map_add_map(*res, isl_set_unwrap(isl_set_copy(set)));
1412 return 0;
1415 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
1417 return cond_un_op(uset, &unwrap_entry);
1420 static int wrap_entry(void **entry, void *user)
1422 isl_map *map = *entry;
1423 isl_union_set **res = user;
1425 *res = isl_union_set_add_set(*res, isl_map_wrap(isl_map_copy(map)));
1427 return 0;
1430 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
1432 return cond_un_op(umap, &wrap_entry);
1435 struct isl_union_map_is_subset_data {
1436 isl_union_map *umap2;
1437 int is_subset;
1440 static int is_subset_entry(void **entry, void *user)
1442 struct isl_union_map_is_subset_data *data = user;
1443 uint32_t hash;
1444 struct isl_hash_table_entry *entry2;
1445 isl_map *map = *entry;
1447 hash = isl_space_get_hash(map->dim);
1448 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1449 hash, &has_dim, map->dim, 0);
1450 if (!entry2) {
1451 data->is_subset = 0;
1452 return -1;
1455 data->is_subset = isl_map_is_subset(map, entry2->data);
1456 if (data->is_subset < 0 || !data->is_subset)
1457 return -1;
1459 return 0;
1462 int isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
1463 __isl_keep isl_union_map *umap2)
1465 struct isl_union_map_is_subset_data data = { NULL, 1 };
1467 umap1 = isl_union_map_copy(umap1);
1468 umap2 = isl_union_map_copy(umap2);
1469 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1470 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1472 if (!umap1 || !umap2)
1473 goto error;
1475 data.umap2 = umap2;
1476 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1477 &is_subset_entry, &data) < 0 &&
1478 data.is_subset)
1479 goto error;
1481 isl_union_map_free(umap1);
1482 isl_union_map_free(umap2);
1484 return data.is_subset;
1485 error:
1486 isl_union_map_free(umap1);
1487 isl_union_map_free(umap2);
1488 return -1;
1491 int isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
1492 __isl_keep isl_union_set *uset2)
1494 return isl_union_map_is_subset(uset1, uset2);
1497 int isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
1498 __isl_keep isl_union_map *umap2)
1500 int is_subset;
1502 if (!umap1 || !umap2)
1503 return -1;
1504 is_subset = isl_union_map_is_subset(umap1, umap2);
1505 if (is_subset != 1)
1506 return is_subset;
1507 is_subset = isl_union_map_is_subset(umap2, umap1);
1508 return is_subset;
1511 int isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
1512 __isl_keep isl_union_set *uset2)
1514 return isl_union_map_is_equal(uset1, uset2);
1517 int isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
1518 __isl_keep isl_union_map *umap2)
1520 int is_subset;
1522 if (!umap1 || !umap2)
1523 return -1;
1524 is_subset = isl_union_map_is_subset(umap1, umap2);
1525 if (is_subset != 1)
1526 return is_subset;
1527 is_subset = isl_union_map_is_subset(umap2, umap1);
1528 if (is_subset == -1)
1529 return is_subset;
1530 return !is_subset;
1533 int isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
1534 __isl_keep isl_union_set *uset2)
1536 return isl_union_map_is_strict_subset(uset1, uset2);
1539 static int sample_entry(void **entry, void *user)
1541 isl_basic_map **sample = (isl_basic_map **)user;
1542 isl_map *map = *entry;
1544 *sample = isl_map_sample(isl_map_copy(map));
1545 if (!*sample)
1546 return -1;
1547 if (!isl_basic_map_plain_is_empty(*sample))
1548 return -1;
1549 return 0;
1552 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
1554 isl_basic_map *sample = NULL;
1556 if (!umap)
1557 return NULL;
1559 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1560 &sample_entry, &sample) < 0 &&
1561 !sample)
1562 goto error;
1564 if (!sample)
1565 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
1567 isl_union_map_free(umap);
1569 return sample;
1570 error:
1571 isl_union_map_free(umap);
1572 return NULL;
1575 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
1577 return (isl_basic_set *)isl_union_map_sample(uset);
1580 struct isl_forall_data {
1581 int res;
1582 int (*fn)(__isl_keep isl_map *map);
1585 static int forall_entry(void **entry, void *user)
1587 struct isl_forall_data *data = user;
1588 isl_map *map = *entry;
1590 data->res = data->fn(map);
1591 if (data->res < 0)
1592 return -1;
1594 if (!data->res)
1595 return -1;
1597 return 0;
1600 static int union_map_forall(__isl_keep isl_union_map *umap,
1601 int (*fn)(__isl_keep isl_map *map))
1603 struct isl_forall_data data = { 1, fn };
1605 if (!umap)
1606 return -1;
1608 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1609 &forall_entry, &data) < 0 && data.res)
1610 return -1;
1612 return data.res;
1615 struct isl_forall_user_data {
1616 int res;
1617 int (*fn)(__isl_keep isl_map *map, void *user);
1618 void *user;
1621 static int forall_user_entry(void **entry, void *user)
1623 struct isl_forall_user_data *data = user;
1624 isl_map *map = *entry;
1626 data->res = data->fn(map, data->user);
1627 if (data->res < 0)
1628 return -1;
1630 if (!data->res)
1631 return -1;
1633 return 0;
1636 /* Check if fn(map, user) returns true for all maps "map" in umap.
1638 static int union_map_forall_user(__isl_keep isl_union_map *umap,
1639 int (*fn)(__isl_keep isl_map *map, void *user), void *user)
1641 struct isl_forall_user_data data = { 1, fn, user };
1643 if (!umap)
1644 return -1;
1646 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1647 &forall_user_entry, &data) < 0 && data.res)
1648 return -1;
1650 return data.res;
1653 int isl_union_map_is_empty(__isl_keep isl_union_map *umap)
1655 return union_map_forall(umap, &isl_map_is_empty);
1658 int isl_union_set_is_empty(__isl_keep isl_union_set *uset)
1660 return isl_union_map_is_empty(uset);
1663 static int is_subset_of_identity(__isl_keep isl_map *map)
1665 int is_subset;
1666 isl_space *dim;
1667 isl_map *id;
1669 if (!map)
1670 return -1;
1672 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1673 return 0;
1675 dim = isl_map_get_space(map);
1676 id = isl_map_identity(dim);
1678 is_subset = isl_map_is_subset(map, id);
1680 isl_map_free(id);
1682 return is_subset;
1685 /* Check if the given map is single-valued.
1686 * We simply compute
1688 * M \circ M^-1
1690 * and check if the result is a subset of the identity mapping.
1692 int isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
1694 isl_union_map *test;
1695 int sv;
1697 if (isl_union_map_n_map(umap) == 1) {
1698 isl_map *map;
1699 umap = isl_union_map_copy(umap);
1700 map = isl_map_from_union_map(umap);
1701 sv = isl_map_is_single_valued(map);
1702 isl_map_free(map);
1703 return sv;
1706 test = isl_union_map_reverse(isl_union_map_copy(umap));
1707 test = isl_union_map_apply_range(test, isl_union_map_copy(umap));
1709 sv = union_map_forall(test, &is_subset_of_identity);
1711 isl_union_map_free(test);
1713 return sv;
1716 int isl_union_map_is_injective(__isl_keep isl_union_map *umap)
1718 int in;
1720 umap = isl_union_map_copy(umap);
1721 umap = isl_union_map_reverse(umap);
1722 in = isl_union_map_is_single_valued(umap);
1723 isl_union_map_free(umap);
1725 return in;
1728 /* Represents a map that has a fixed value (v) for one of its
1729 * range dimensions.
1730 * The map in this structure is not reference counted, so it
1731 * is only valid while the isl_union_map from which it was
1732 * obtained is still alive.
1734 struct isl_fixed_map {
1735 isl_int v;
1736 isl_map *map;
1739 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
1740 int n)
1742 int i;
1743 struct isl_fixed_map *v;
1745 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
1746 if (!v)
1747 return NULL;
1748 for (i = 0; i < n; ++i)
1749 isl_int_init(v[i].v);
1750 return v;
1753 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
1755 int i;
1757 if (!v)
1758 return;
1759 for (i = 0; i < n; ++i)
1760 isl_int_clear(v[i].v);
1761 free(v);
1764 /* Compare the "v" field of two isl_fixed_map structs.
1766 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
1768 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
1769 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
1771 return isl_int_cmp(e1->v, e2->v);
1774 /* Internal data structure used while checking whether all maps
1775 * in a union_map have a fixed value for a given output dimension.
1776 * v is the list of maps, with the fixed value for the dimension
1777 * n is the number of maps considered so far
1778 * pos is the output dimension under investigation
1780 struct isl_fixed_dim_data {
1781 struct isl_fixed_map *v;
1782 int n;
1783 int pos;
1786 static int fixed_at_pos(__isl_keep isl_map *map, void *user)
1788 struct isl_fixed_dim_data *data = user;
1790 data->v[data->n].map = map;
1791 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
1792 &data->v[data->n++].v);
1795 static int plain_injective_on_range(__isl_take isl_union_map *umap,
1796 int first, int n_range);
1798 /* Given a list of the maps, with their fixed values at output dimension "pos",
1799 * check whether the ranges of the maps form an obvious partition.
1801 * We first sort the maps according to their fixed values.
1802 * If all maps have a different value, then we know the ranges form
1803 * a partition.
1804 * Otherwise, we collect the maps with the same fixed value and
1805 * check whether each such collection is obviously injective
1806 * based on later dimensions.
1808 static int separates(struct isl_fixed_map *v, int n,
1809 __isl_take isl_space *dim, int pos, int n_range)
1811 int i;
1813 if (!v)
1814 goto error;
1816 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
1818 for (i = 0; i + 1 < n; ++i) {
1819 int j, k;
1820 isl_union_map *part;
1821 int injective;
1823 for (j = i + 1; j < n; ++j)
1824 if (isl_int_ne(v[i].v, v[j].v))
1825 break;
1827 if (j == i + 1)
1828 continue;
1830 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
1831 for (k = i; k < j; ++k)
1832 part = isl_union_map_add_map(part,
1833 isl_map_copy(v[k].map));
1835 injective = plain_injective_on_range(part, pos + 1, n_range);
1836 if (injective < 0)
1837 goto error;
1838 if (!injective)
1839 break;
1841 i = j - 1;
1844 isl_space_free(dim);
1845 free_isl_fixed_map_array(v, n);
1846 return i + 1 >= n;
1847 error:
1848 isl_space_free(dim);
1849 free_isl_fixed_map_array(v, n);
1850 return -1;
1853 /* Check whether the maps in umap have obviously distinct ranges.
1854 * In particular, check for an output dimension in the range
1855 * [first,n_range) for which all maps have a fixed value
1856 * and then check if these values, possibly along with fixed values
1857 * at later dimensions, entail distinct ranges.
1859 static int plain_injective_on_range(__isl_take isl_union_map *umap,
1860 int first, int n_range)
1862 isl_ctx *ctx;
1863 int n;
1864 struct isl_fixed_dim_data data = { NULL };
1866 ctx = isl_union_map_get_ctx(umap);
1868 if (!umap)
1869 goto error;
1871 n = isl_union_map_n_map(umap);
1872 if (n <= 1) {
1873 isl_union_map_free(umap);
1874 return 1;
1877 if (first >= n_range) {
1878 isl_union_map_free(umap);
1879 return 0;
1882 data.v = alloc_isl_fixed_map_array(ctx, n);
1883 if (!data.v)
1884 goto error;
1886 for (data.pos = first; data.pos < n_range; ++data.pos) {
1887 int fixed;
1888 int injective;
1889 isl_space *dim;
1891 data.n = 0;
1892 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
1893 if (fixed < 0)
1894 goto error;
1895 if (!fixed)
1896 continue;
1897 dim = isl_union_map_get_space(umap);
1898 injective = separates(data.v, n, dim, data.pos, n_range);
1899 isl_union_map_free(umap);
1900 return injective;
1903 free_isl_fixed_map_array(data.v, n);
1904 isl_union_map_free(umap);
1906 return 0;
1907 error:
1908 free_isl_fixed_map_array(data.v, n);
1909 isl_union_map_free(umap);
1910 return -1;
1913 /* Check whether the maps in umap that map to subsets of "ran"
1914 * have obviously distinct ranges.
1916 static int plain_injective_on_range_wrap(__isl_keep isl_set *ran, void *user)
1918 isl_union_map *umap = user;
1920 umap = isl_union_map_copy(umap);
1921 umap = isl_union_map_intersect_range(umap,
1922 isl_union_set_from_set(isl_set_copy(ran)));
1923 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
1926 /* Check if the given union_map is obviously injective.
1928 * In particular, we first check if all individual maps are obviously
1929 * injective and then check if all the ranges of these maps are
1930 * obviously disjoint.
1932 int isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
1934 int in;
1935 isl_union_map *univ;
1936 isl_union_set *ran;
1938 in = union_map_forall(umap, &isl_map_plain_is_injective);
1939 if (in < 0)
1940 return -1;
1941 if (!in)
1942 return 0;
1944 univ = isl_union_map_universe(isl_union_map_copy(umap));
1945 ran = isl_union_map_range(univ);
1947 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
1949 isl_union_set_free(ran);
1951 return in;
1954 int isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
1956 int sv;
1958 sv = isl_union_map_is_single_valued(umap);
1959 if (sv < 0 || !sv)
1960 return sv;
1962 return isl_union_map_is_injective(umap);
1965 static int zip_entry(void **entry, void *user)
1967 isl_map *map = *entry;
1968 isl_union_map **res = user;
1970 if (!isl_map_can_zip(map))
1971 return 0;
1973 *res = isl_union_map_add_map(*res, isl_map_zip(isl_map_copy(map)));
1975 return 0;
1978 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
1980 return cond_un_op(umap, &zip_entry);
1983 static int lift_entry(void **entry, void *user)
1985 isl_set *set = *entry;
1986 isl_union_set **res = user;
1988 *res = isl_union_set_add_set(*res, isl_set_lift(isl_set_copy(set)));
1990 return 0;
1993 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
1995 return cond_un_op(uset, &lift_entry);
1998 static int coefficients_entry(void **entry, void *user)
2000 isl_set *set = *entry;
2001 isl_union_set **res = user;
2003 set = isl_set_copy(set);
2004 set = isl_set_from_basic_set(isl_set_coefficients(set));
2005 *res = isl_union_set_add_set(*res, set);
2007 return 0;
2010 __isl_give isl_union_set *isl_union_set_coefficients(
2011 __isl_take isl_union_set *uset)
2013 isl_ctx *ctx;
2014 isl_space *dim;
2015 isl_union_set *res;
2017 if (!uset)
2018 return NULL;
2020 ctx = isl_union_set_get_ctx(uset);
2021 dim = isl_space_set_alloc(ctx, 0, 0);
2022 res = isl_union_map_alloc(dim, uset->table.n);
2023 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2024 &coefficients_entry, &res) < 0)
2025 goto error;
2027 isl_union_set_free(uset);
2028 return res;
2029 error:
2030 isl_union_set_free(uset);
2031 isl_union_set_free(res);
2032 return NULL;
2035 static int solutions_entry(void **entry, void *user)
2037 isl_set *set = *entry;
2038 isl_union_set **res = user;
2040 set = isl_set_copy(set);
2041 set = isl_set_from_basic_set(isl_set_solutions(set));
2042 if (!*res)
2043 *res = isl_union_set_from_set(set);
2044 else
2045 *res = isl_union_set_add_set(*res, set);
2047 if (!*res)
2048 return -1;
2050 return 0;
2053 __isl_give isl_union_set *isl_union_set_solutions(
2054 __isl_take isl_union_set *uset)
2056 isl_union_set *res = NULL;
2058 if (!uset)
2059 return NULL;
2061 if (uset->table.n == 0) {
2062 res = isl_union_set_empty(isl_union_set_get_space(uset));
2063 isl_union_set_free(uset);
2064 return res;
2067 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2068 &solutions_entry, &res) < 0)
2069 goto error;
2071 isl_union_set_free(uset);
2072 return res;
2073 error:
2074 isl_union_set_free(uset);
2075 isl_union_set_free(res);
2076 return NULL;