add isl_pw_aff_union_opt
[isl.git] / isl_union_map.c
blobec53f9381b0814a502e774775a3107132b5f7cd1
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 #include <isl_map_private.h>
12 #include <isl/ctx.h>
13 #include <isl/hash.h>
14 #include <isl/map.h>
15 #include <isl/set.h>
16 #include <isl_dim_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl/union_set.h>
20 static __isl_give isl_union_map *isl_union_map_alloc(__isl_take isl_dim *dim,
21 int size)
23 isl_union_map *umap;
25 if (!dim)
26 return NULL;
28 umap = isl_calloc_type(dim->ctx, isl_union_map);
29 if (!umap)
30 return NULL;
32 umap->ref = 1;
33 umap->dim = dim;
34 if (isl_hash_table_init(dim->ctx, &umap->table, size) < 0)
35 goto error;
37 return umap;
38 error:
39 isl_dim_free(dim);
40 isl_union_map_free(umap);
41 return NULL;
44 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_dim *dim)
46 return isl_union_map_alloc(dim, 16);
49 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_dim *dim)
51 return isl_union_map_empty(dim);
54 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
56 return umap ? umap->dim->ctx : NULL;
59 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
61 return uset ? uset->dim->ctx : NULL;
64 __isl_give isl_dim *isl_union_map_get_dim(__isl_keep isl_union_map *umap)
66 if (!umap)
67 return NULL;
68 return isl_dim_copy(umap->dim);
71 __isl_give isl_dim *isl_union_set_get_dim(__isl_keep isl_union_set *uset)
73 return isl_union_map_get_dim(uset);
76 static int free_umap_entry(void **entry, void *user)
78 isl_map *map = *entry;
79 isl_map_free(map);
80 return 0;
83 static int add_map(__isl_take isl_map *map, void *user)
85 isl_union_map **umap = (isl_union_map **)user;
87 *umap = isl_union_map_add_map(*umap, map);
89 return 0;
92 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
94 isl_union_map *dup;
96 if (!umap)
97 return NULL;
99 dup = isl_union_map_empty(isl_dim_copy(umap->dim));
100 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
101 goto error;
102 return dup;
103 error:
104 isl_union_map_free(dup);
105 return NULL;
108 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
110 if (!umap)
111 return NULL;
113 if (umap->ref == 1)
114 return umap;
115 umap->ref--;
116 return isl_union_map_dup(umap);
119 struct isl_union_align {
120 isl_reordering *exp;
121 isl_union_map *res;
124 static int align_entry(void **entry, void *user)
126 isl_map *map = *entry;
127 isl_reordering *exp;
128 struct isl_union_align *data = user;
130 exp = isl_reordering_extend_dim(isl_reordering_copy(data->exp),
131 isl_map_get_dim(map));
133 data->res = isl_union_map_add_map(data->res,
134 isl_map_realign(isl_map_copy(map), exp));
136 return 0;
139 /* Align the parameters of umap along those of model.
140 * The result has the parameters of model first, in the same order
141 * as they appear in model, followed by any remaining parameters of
142 * umap that do not appear in model.
144 __isl_give isl_union_map *isl_union_map_align_params(
145 __isl_take isl_union_map *umap, __isl_take isl_dim *model)
147 struct isl_union_align data = { NULL, NULL };
149 if (!umap || !model)
150 goto error;
152 if (isl_dim_match(umap->dim, isl_dim_param, model, isl_dim_param)) {
153 isl_dim_free(model);
154 return umap;
157 model = isl_dim_drop(model, isl_dim_in,
158 0, isl_dim_size(model, isl_dim_in));
159 model = isl_dim_drop(model, isl_dim_out,
160 0, isl_dim_size(model, isl_dim_out));
162 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
163 if (!data.exp)
164 goto error;
166 data.res = isl_union_map_alloc(isl_dim_copy(data.exp->dim),
167 umap->table.n);
168 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
169 &align_entry, &data) < 0)
170 goto error;
172 isl_reordering_free(data.exp);
173 isl_union_map_free(umap);
174 isl_dim_free(model);
175 return data.res;
176 error:
177 isl_reordering_free(data.exp);
178 isl_union_map_free(umap);
179 isl_union_map_free(data.res);
180 isl_dim_free(model);
181 return NULL;
184 __isl_give isl_union_set *isl_union_set_align_params(
185 __isl_take isl_union_set *uset, __isl_take isl_dim *model)
187 return isl_union_map_align_params(uset, model);
190 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
191 __isl_take isl_union_map *umap2)
193 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_dim(umap2));
194 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_dim(umap1));
196 umap1 = isl_union_map_cow(umap1);
198 if (!umap1 || !umap2)
199 goto error;
201 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
202 goto error;
204 isl_union_map_free(umap2);
206 return umap1;
207 error:
208 isl_union_map_free(umap1);
209 isl_union_map_free(umap2);
210 return NULL;
213 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
214 __isl_take isl_union_set *uset2)
216 return isl_union_map_union(uset1, uset2);
219 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
221 if (!umap)
222 return NULL;
224 umap->ref++;
225 return umap;
228 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
230 return isl_union_map_copy(uset);
233 void *isl_union_map_free(__isl_take isl_union_map *umap)
235 if (!umap)
236 return NULL;
238 if (--umap->ref > 0)
239 return NULL;
241 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
242 &free_umap_entry, NULL);
243 isl_hash_table_clear(&umap->table);
244 isl_dim_free(umap->dim);
245 free(umap);
246 return NULL;
249 void *isl_union_set_free(__isl_take isl_union_set *uset)
251 return isl_union_map_free(uset);
254 static int has_dim(const void *entry, const void *val)
256 isl_map *map = (isl_map *)entry;
257 isl_dim *dim = (isl_dim *)val;
259 return isl_dim_equal(map->dim, dim);
262 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
263 __isl_take isl_map *map)
265 uint32_t hash;
266 struct isl_hash_table_entry *entry;
268 if (!map || !umap)
269 goto error;
271 if (isl_map_plain_is_empty(map)) {
272 isl_map_free(map);
273 return umap;
276 if (!isl_dim_match(map->dim, isl_dim_param, umap->dim, isl_dim_param)) {
277 umap = isl_union_map_align_params(umap, isl_map_get_dim(map));
278 map = isl_map_align_params(map, isl_union_map_get_dim(umap));
281 umap = isl_union_map_cow(umap);
283 if (!map || !umap)
284 goto error;
286 hash = isl_dim_get_hash(map->dim);
287 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
288 &has_dim, map->dim, 1);
289 if (!entry)
290 goto error;
292 if (!entry->data)
293 entry->data = map;
294 else {
295 entry->data = isl_map_union(entry->data, isl_map_copy(map));
296 if (!entry->data)
297 goto error;
298 isl_map_free(map);
301 return umap;
302 error:
303 isl_map_free(map);
304 isl_union_map_free(umap);
305 return NULL;
308 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
309 __isl_take isl_set *set)
311 return isl_union_map_add_map(uset, (isl_map *)set);
314 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
316 isl_dim *dim;
317 isl_union_map *umap;
319 if (!map)
320 return NULL;
322 dim = isl_map_get_dim(map);
323 dim = isl_dim_drop(dim, isl_dim_in, 0, isl_dim_size(dim, isl_dim_in));
324 dim = isl_dim_drop(dim, isl_dim_out, 0, isl_dim_size(dim, isl_dim_out));
325 umap = isl_union_map_empty(dim);
326 umap = isl_union_map_add_map(umap, map);
328 return umap;
331 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
333 return isl_union_map_from_map((isl_map *)set);
336 struct isl_union_map_foreach_data
338 int (*fn)(__isl_take isl_map *map, void *user);
339 void *user;
342 static int call_on_copy(void **entry, void *user)
344 isl_map *map = *entry;
345 struct isl_union_map_foreach_data *data;
346 data = (struct isl_union_map_foreach_data *)user;
348 return data->fn(isl_map_copy(map), data->user);
351 int isl_union_map_n_map(__isl_keep isl_union_map *umap)
353 return umap ? umap->table.n : 0;
356 int isl_union_set_n_set(__isl_keep isl_union_set *uset)
358 return uset ? uset->table.n : 0;
361 int isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
362 int (*fn)(__isl_take isl_map *map, void *user), void *user)
364 struct isl_union_map_foreach_data data = { fn, user };
366 if (!umap)
367 return -1;
369 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
370 &call_on_copy, &data);
373 static int copy_map(void **entry, void *user)
375 isl_map *map = *entry;
376 isl_map **map_p = user;
378 *map_p = isl_map_copy(map);
380 return -1;
383 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
385 isl_ctx *ctx;
386 isl_map *map = NULL;
388 if (!umap)
389 return NULL;
390 ctx = isl_union_map_get_ctx(umap);
391 if (umap->table.n != 1)
392 isl_die(ctx, isl_error_invalid,
393 "union map needs to contain elements in exactly "
394 "one space", return isl_union_map_free(umap));
396 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
398 isl_union_map_free(umap);
400 return map;
403 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
405 return isl_map_from_union_map(uset);
408 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
409 __isl_take isl_dim *dim)
411 uint32_t hash;
412 struct isl_hash_table_entry *entry;
414 if (!umap || !dim)
415 goto error;
417 hash = isl_dim_get_hash(dim);
418 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
419 &has_dim, dim, 0);
420 if (!entry)
421 return isl_map_empty(dim);
422 isl_dim_free(dim);
423 return isl_map_copy(entry->data);
424 error:
425 isl_dim_free(dim);
426 return NULL;
429 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
430 __isl_take isl_dim *dim)
432 return (isl_set *)isl_union_map_extract_map(uset, dim);
435 /* Check if umap contains a map in the given space.
437 __isl_give int isl_union_map_contains(__isl_keep isl_union_map *umap,
438 __isl_keep isl_dim *dim)
440 uint32_t hash;
441 struct isl_hash_table_entry *entry;
443 if (!umap || !dim)
444 return -1;
446 hash = isl_dim_get_hash(dim);
447 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
448 &has_dim, dim, 0);
449 return !!entry;
452 __isl_give int isl_union_set_contains(__isl_keep isl_union_set *uset,
453 __isl_keep isl_dim *dim)
455 return isl_union_map_contains(uset, dim);
458 int isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
459 int (*fn)(__isl_take isl_set *set, void *user), void *user)
461 return isl_union_map_foreach_map(uset,
462 (int(*)(__isl_take isl_map *, void*))fn, user);
465 struct isl_union_set_foreach_point_data {
466 int (*fn)(__isl_take isl_point *pnt, void *user);
467 void *user;
470 static int foreach_point(__isl_take isl_set *set, void *user)
472 struct isl_union_set_foreach_point_data *data = user;
473 int r;
475 r = isl_set_foreach_point(set, data->fn, data->user);
476 isl_set_free(set);
478 return r;
481 int isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
482 int (*fn)(__isl_take isl_point *pnt, void *user), void *user)
484 struct isl_union_set_foreach_point_data data = { fn, user };
485 return isl_union_set_foreach_set(uset, &foreach_point, &data);
488 struct isl_union_map_gen_bin_data {
489 isl_union_map *umap2;
490 isl_union_map *res;
493 static int subtract_entry(void **entry, void *user)
495 struct isl_union_map_gen_bin_data *data = user;
496 uint32_t hash;
497 struct isl_hash_table_entry *entry2;
498 isl_map *map = *entry;
500 hash = isl_dim_get_hash(map->dim);
501 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
502 hash, &has_dim, map->dim, 0);
503 map = isl_map_copy(map);
504 if (entry2) {
505 int empty;
506 map = isl_map_subtract(map, isl_map_copy(entry2->data));
508 empty = isl_map_is_empty(map);
509 if (empty < 0) {
510 isl_map_free(map);
511 return -1;
513 if (empty) {
514 isl_map_free(map);
515 return 0;
518 data->res = isl_union_map_add_map(data->res, map);
520 return 0;
523 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
524 __isl_take isl_union_map *umap2, int (*fn)(void **, void *))
526 struct isl_union_map_gen_bin_data data = { NULL, NULL };
528 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_dim(umap2));
529 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_dim(umap1));
531 if (!umap1 || !umap2)
532 goto error;
534 data.umap2 = umap2;
535 data.res = isl_union_map_alloc(isl_dim_copy(umap1->dim),
536 umap1->table.n);
537 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
538 fn, &data) < 0)
539 goto error;
541 isl_union_map_free(umap1);
542 isl_union_map_free(umap2);
543 return data.res;
544 error:
545 isl_union_map_free(umap1);
546 isl_union_map_free(umap2);
547 isl_union_map_free(data.res);
548 return NULL;
551 __isl_give isl_union_map *isl_union_map_subtract(
552 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
554 return gen_bin_op(umap1, umap2, &subtract_entry);
557 __isl_give isl_union_set *isl_union_set_subtract(
558 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
560 return isl_union_map_subtract(uset1, uset2);
563 struct isl_union_map_match_bin_data {
564 isl_union_map *umap2;
565 isl_union_map *res;
566 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
569 static int match_bin_entry(void **entry, void *user)
571 struct isl_union_map_match_bin_data *data = user;
572 uint32_t hash;
573 struct isl_hash_table_entry *entry2;
574 isl_map *map = *entry;
575 int empty;
577 hash = isl_dim_get_hash(map->dim);
578 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
579 hash, &has_dim, map->dim, 0);
580 if (!entry2)
581 return 0;
583 map = isl_map_copy(map);
584 map = data->fn(map, isl_map_copy(entry2->data));
586 empty = isl_map_is_empty(map);
587 if (empty < 0) {
588 isl_map_free(map);
589 return -1;
591 if (empty) {
592 isl_map_free(map);
593 return 0;
596 data->res = isl_union_map_add_map(data->res, map);
598 return 0;
601 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
602 __isl_take isl_union_map *umap2,
603 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
605 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
607 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_dim(umap2));
608 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_dim(umap1));
610 if (!umap1 || !umap2)
611 goto error;
613 data.umap2 = umap2;
614 data.res = isl_union_map_alloc(isl_dim_copy(umap1->dim),
615 umap1->table.n);
616 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
617 &match_bin_entry, &data) < 0)
618 goto error;
620 isl_union_map_free(umap1);
621 isl_union_map_free(umap2);
622 return data.res;
623 error:
624 isl_union_map_free(umap1);
625 isl_union_map_free(umap2);
626 isl_union_map_free(data.res);
627 return NULL;
630 __isl_give isl_union_map *isl_union_map_intersect(
631 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
633 return match_bin_op(umap1, umap2, &isl_map_intersect);
636 __isl_give isl_union_set *isl_union_set_intersect(
637 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
639 return isl_union_map_intersect(uset1, uset2);
642 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
643 __isl_take isl_union_map *context)
645 return match_bin_op(umap, context, &isl_map_gist);
648 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
649 __isl_take isl_union_set *context)
651 return isl_union_map_gist(uset, context);
654 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
655 __isl_take isl_map *set2)
657 return isl_set_lex_le_set((isl_set *)set1, (isl_set *)set2);
660 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
661 __isl_take isl_map *set2)
663 return isl_set_lex_lt_set((isl_set *)set1, (isl_set *)set2);
666 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
667 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
669 return match_bin_op(uset1, uset2, &lex_lt_set);
672 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
673 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
675 return match_bin_op(uset1, uset2, &lex_le_set);
678 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
679 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
681 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
684 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
685 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
687 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
690 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
691 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
693 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
696 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
697 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
699 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
702 static int intersect_domain_entry(void **entry, void *user)
704 struct isl_union_map_gen_bin_data *data = user;
705 uint32_t hash;
706 struct isl_hash_table_entry *entry2;
707 isl_dim *dim;
708 isl_map *map = *entry;
709 int empty;
711 dim = isl_map_get_dim(map);
712 dim = isl_dim_domain(dim);
713 hash = isl_dim_get_hash(dim);
714 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
715 hash, &has_dim, dim, 0);
716 isl_dim_free(dim);
717 if (!entry2)
718 return 0;
720 map = isl_map_copy(map);
721 map = isl_map_intersect_domain(map, isl_set_copy(entry2->data));
723 empty = isl_map_is_empty(map);
724 if (empty < 0) {
725 isl_map_free(map);
726 return -1;
728 if (empty) {
729 isl_map_free(map);
730 return 0;
733 data->res = isl_union_map_add_map(data->res, map);
735 return 0;
738 __isl_give isl_union_map *isl_union_map_intersect_domain(
739 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
741 return gen_bin_op(umap, uset, &intersect_domain_entry);
744 static int intersect_range_entry(void **entry, void *user)
746 struct isl_union_map_gen_bin_data *data = user;
747 uint32_t hash;
748 struct isl_hash_table_entry *entry2;
749 isl_dim *dim;
750 isl_map *map = *entry;
751 int empty;
753 dim = isl_map_get_dim(map);
754 dim = isl_dim_range(dim);
755 hash = isl_dim_get_hash(dim);
756 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
757 hash, &has_dim, dim, 0);
758 isl_dim_free(dim);
759 if (!entry2)
760 return 0;
762 map = isl_map_copy(map);
763 map = isl_map_intersect_range(map, isl_set_copy(entry2->data));
765 empty = isl_map_is_empty(map);
766 if (empty < 0) {
767 isl_map_free(map);
768 return -1;
770 if (empty) {
771 isl_map_free(map);
772 return 0;
775 data->res = isl_union_map_add_map(data->res, map);
777 return 0;
780 __isl_give isl_union_map *isl_union_map_intersect_range(
781 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
783 return gen_bin_op(umap, uset, &intersect_range_entry);
786 struct isl_union_map_bin_data {
787 isl_union_map *umap2;
788 isl_union_map *res;
789 isl_map *map;
790 int (*fn)(void **entry, void *user);
793 static int apply_range_entry(void **entry, void *user)
795 struct isl_union_map_bin_data *data = user;
796 isl_map *map2 = *entry;
797 int empty;
799 if (!isl_dim_tuple_match(data->map->dim, isl_dim_out,
800 map2->dim, isl_dim_in))
801 return 0;
803 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
805 empty = isl_map_is_empty(map2);
806 if (empty < 0) {
807 isl_map_free(map2);
808 return -1;
810 if (empty) {
811 isl_map_free(map2);
812 return 0;
815 data->res = isl_union_map_add_map(data->res, map2);
817 return 0;
820 static int bin_entry(void **entry, void *user)
822 struct isl_union_map_bin_data *data = user;
823 isl_map *map = *entry;
825 data->map = map;
826 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
827 data->fn, data) < 0)
828 return -1;
830 return 0;
833 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
834 __isl_take isl_union_map *umap2, int (*fn)(void **entry, void *user))
836 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
838 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_dim(umap2));
839 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_dim(umap1));
841 if (!umap1 || !umap2)
842 goto error;
844 data.umap2 = umap2;
845 data.res = isl_union_map_alloc(isl_dim_copy(umap1->dim),
846 umap1->table.n);
847 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
848 &bin_entry, &data) < 0)
849 goto error;
851 isl_union_map_free(umap1);
852 isl_union_map_free(umap2);
853 return data.res;
854 error:
855 isl_union_map_free(umap1);
856 isl_union_map_free(umap2);
857 isl_union_map_free(data.res);
858 return NULL;
861 __isl_give isl_union_map *isl_union_map_apply_range(
862 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
864 return bin_op(umap1, umap2, &apply_range_entry);
867 __isl_give isl_union_map *isl_union_map_apply_domain(
868 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
870 umap1 = isl_union_map_reverse(umap1);
871 umap1 = isl_union_map_apply_range(umap1, umap2);
872 return isl_union_map_reverse(umap1);
875 __isl_give isl_union_set *isl_union_set_apply(
876 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
878 return isl_union_map_apply_range(uset, umap);
881 static int map_lex_lt_entry(void **entry, void *user)
883 struct isl_union_map_bin_data *data = user;
884 isl_map *map2 = *entry;
886 if (!isl_dim_tuple_match(data->map->dim, isl_dim_out,
887 map2->dim, isl_dim_out))
888 return 0;
890 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
892 data->res = isl_union_map_add_map(data->res, map2);
894 return 0;
897 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
898 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
900 return bin_op(umap1, umap2, &map_lex_lt_entry);
903 static int map_lex_le_entry(void **entry, void *user)
905 struct isl_union_map_bin_data *data = user;
906 isl_map *map2 = *entry;
908 if (!isl_dim_tuple_match(data->map->dim, isl_dim_out,
909 map2->dim, isl_dim_out))
910 return 0;
912 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
914 data->res = isl_union_map_add_map(data->res, map2);
916 return 0;
919 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
920 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
922 return bin_op(umap1, umap2, &map_lex_le_entry);
925 static int product_entry(void **entry, void *user)
927 struct isl_union_map_bin_data *data = user;
928 isl_map *map2 = *entry;
930 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
932 data->res = isl_union_map_add_map(data->res, map2);
934 return 0;
937 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
938 __isl_take isl_union_map *umap2)
940 return bin_op(umap1, umap2, &product_entry);
943 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
944 __isl_take isl_union_set *uset2)
946 return isl_union_map_product(uset1, uset2);
949 static int range_product_entry(void **entry, void *user)
951 struct isl_union_map_bin_data *data = user;
952 isl_map *map2 = *entry;
954 if (!isl_dim_tuple_match(data->map->dim, isl_dim_in,
955 map2->dim, isl_dim_in))
956 return 0;
958 map2 = isl_map_range_product(isl_map_copy(data->map),
959 isl_map_copy(map2));
961 data->res = isl_union_map_add_map(data->res, map2);
963 return 0;
966 __isl_give isl_union_map *isl_union_map_range_product(
967 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
969 return bin_op(umap1, umap2, &range_product_entry);
972 static int flat_range_product_entry(void **entry, void *user)
974 struct isl_union_map_bin_data *data = user;
975 isl_map *map2 = *entry;
977 if (!isl_dim_tuple_match(data->map->dim, isl_dim_in,
978 map2->dim, isl_dim_in))
979 return 0;
981 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
982 isl_map_copy(map2));
984 data->res = isl_union_map_add_map(data->res, map2);
986 return 0;
989 __isl_give isl_union_map *isl_union_map_flat_range_product(
990 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
992 return bin_op(umap1, umap2, &flat_range_product_entry);
995 __isl_give isl_union_map *isl_union_map_from_range(
996 __isl_take isl_union_set *uset)
998 return uset;
1001 __isl_give isl_union_map *isl_union_map_from_domain(
1002 __isl_take isl_union_set *uset)
1004 return isl_union_map_reverse(isl_union_map_from_range(uset));
1007 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1008 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1010 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1011 isl_union_map_from_range(range));
1014 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1015 int (*fn)(void **, void *))
1017 umap = isl_union_map_cow(umap);
1018 if (!umap)
1019 return NULL;
1021 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, NULL) < 0)
1022 goto error;
1024 return umap;
1025 error:
1026 isl_union_map_free(umap);
1027 return NULL;
1030 static int affine_entry(void **entry, void *user)
1032 isl_map **map = (isl_map **)entry;
1034 *map = isl_map_from_basic_map(isl_map_affine_hull(*map));
1036 return *map ? 0 : -1;
1039 __isl_give isl_union_map *isl_union_map_affine_hull(
1040 __isl_take isl_union_map *umap)
1042 return un_op(umap, &affine_entry);
1045 __isl_give isl_union_set *isl_union_set_affine_hull(
1046 __isl_take isl_union_set *uset)
1048 return isl_union_map_affine_hull(uset);
1051 static int polyhedral_entry(void **entry, void *user)
1053 isl_map **map = (isl_map **)entry;
1055 *map = isl_map_from_basic_map(isl_map_polyhedral_hull(*map));
1057 return *map ? 0 : -1;
1060 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1061 __isl_take isl_union_map *umap)
1063 return un_op(umap, &polyhedral_entry);
1066 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1067 __isl_take isl_union_set *uset)
1069 return isl_union_map_polyhedral_hull(uset);
1072 static int simple_entry(void **entry, void *user)
1074 isl_map **map = (isl_map **)entry;
1076 *map = isl_map_from_basic_map(isl_map_simple_hull(*map));
1078 return *map ? 0 : -1;
1081 __isl_give isl_union_map *isl_union_map_simple_hull(
1082 __isl_take isl_union_map *umap)
1084 return un_op(umap, &simple_entry);
1087 __isl_give isl_union_set *isl_union_set_simple_hull(
1088 __isl_take isl_union_set *uset)
1090 return isl_union_map_simple_hull(uset);
1093 static int inplace_entry(void **entry, void *user)
1095 __isl_give isl_map *(*fn)(__isl_take isl_map *);
1096 isl_map **map = (isl_map **)entry;
1097 isl_map *copy;
1099 fn = *(__isl_give isl_map *(**)(__isl_take isl_map *)) user;
1100 copy = fn(isl_map_copy(*map));
1101 if (!copy)
1102 return -1;
1104 isl_map_free(*map);
1105 *map = copy;
1107 return 0;
1110 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1111 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1113 if (!umap)
1114 return NULL;
1116 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1117 &inplace_entry, &fn) < 0)
1118 goto error;
1120 return umap;
1121 error:
1122 isl_union_map_free(umap);
1123 return NULL;
1126 __isl_give isl_union_map *isl_union_map_coalesce(
1127 __isl_take isl_union_map *umap)
1129 return inplace(umap, &isl_map_coalesce);
1132 __isl_give isl_union_set *isl_union_set_coalesce(
1133 __isl_take isl_union_set *uset)
1135 return isl_union_map_coalesce(uset);
1138 __isl_give isl_union_map *isl_union_map_detect_equalities(
1139 __isl_take isl_union_map *umap)
1141 return inplace(umap, &isl_map_detect_equalities);
1144 __isl_give isl_union_set *isl_union_set_detect_equalities(
1145 __isl_take isl_union_set *uset)
1147 return isl_union_map_detect_equalities(uset);
1150 __isl_give isl_union_map *isl_union_map_compute_divs(
1151 __isl_take isl_union_map *umap)
1153 return inplace(umap, &isl_map_compute_divs);
1156 __isl_give isl_union_set *isl_union_set_compute_divs(
1157 __isl_take isl_union_set *uset)
1159 return isl_union_map_compute_divs(uset);
1162 static int lexmin_entry(void **entry, void *user)
1164 isl_map **map = (isl_map **)entry;
1166 *map = isl_map_lexmin(*map);
1168 return *map ? 0 : -1;
1171 __isl_give isl_union_map *isl_union_map_lexmin(
1172 __isl_take isl_union_map *umap)
1174 return un_op(umap, &lexmin_entry);
1177 __isl_give isl_union_set *isl_union_set_lexmin(
1178 __isl_take isl_union_set *uset)
1180 return isl_union_map_lexmin(uset);
1183 static int lexmax_entry(void **entry, void *user)
1185 isl_map **map = (isl_map **)entry;
1187 *map = isl_map_lexmax(*map);
1189 return *map ? 0 : -1;
1192 __isl_give isl_union_map *isl_union_map_lexmax(
1193 __isl_take isl_union_map *umap)
1195 return un_op(umap, &lexmax_entry);
1198 __isl_give isl_union_set *isl_union_set_lexmax(
1199 __isl_take isl_union_set *uset)
1201 return isl_union_map_lexmax(uset);
1204 static __isl_give isl_union_set *cond_un_op(__isl_take isl_union_map *umap,
1205 int (*fn)(void **, void *))
1207 isl_union_set *res;
1209 if (!umap)
1210 return NULL;
1212 res = isl_union_map_alloc(isl_dim_copy(umap->dim), umap->table.n);
1213 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, &res) < 0)
1214 goto error;
1216 isl_union_map_free(umap);
1217 return res;
1218 error:
1219 isl_union_map_free(umap);
1220 isl_union_set_free(res);
1221 return NULL;
1224 static int universe_entry(void **entry, void *user)
1226 isl_map *map = *entry;
1227 isl_union_map **res = user;
1229 map = isl_map_universe(isl_map_get_dim(map));
1230 *res = isl_union_map_add_map(*res, map);
1232 return 0;
1235 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1237 return cond_un_op(umap, &universe_entry);
1240 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1242 return isl_union_map_universe(uset);
1245 static int reverse_entry(void **entry, void *user)
1247 isl_map *map = *entry;
1248 isl_union_map **res = user;
1250 *res = isl_union_map_add_map(*res, isl_map_reverse(isl_map_copy(map)));
1252 return 0;
1255 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1257 return cond_un_op(umap, &reverse_entry);
1260 static int domain_entry(void **entry, void *user)
1262 isl_map *map = *entry;
1263 isl_union_set **res = user;
1265 *res = isl_union_set_add_set(*res, isl_map_domain(isl_map_copy(map)));
1267 return 0;
1270 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1272 return cond_un_op(umap, &domain_entry);
1275 static int range_entry(void **entry, void *user)
1277 isl_map *map = *entry;
1278 isl_union_set **res = user;
1280 *res = isl_union_set_add_set(*res, isl_map_range(isl_map_copy(map)));
1282 return 0;
1285 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1287 return cond_un_op(umap, &range_entry);
1290 static int domain_map_entry(void **entry, void *user)
1292 isl_map *map = *entry;
1293 isl_union_set **res = user;
1295 *res = isl_union_map_add_map(*res,
1296 isl_map_domain_map(isl_map_copy(map)));
1298 return 0;
1301 __isl_give isl_union_map *isl_union_map_domain_map(
1302 __isl_take isl_union_map *umap)
1304 return cond_un_op(umap, &domain_map_entry);
1307 static int range_map_entry(void **entry, void *user)
1309 isl_map *map = *entry;
1310 isl_union_set **res = user;
1312 *res = isl_union_map_add_map(*res,
1313 isl_map_range_map(isl_map_copy(map)));
1315 return 0;
1318 __isl_give isl_union_map *isl_union_map_range_map(
1319 __isl_take isl_union_map *umap)
1321 return cond_un_op(umap, &range_map_entry);
1324 static int deltas_entry(void **entry, void *user)
1326 isl_map *map = *entry;
1327 isl_union_set **res = user;
1329 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1330 return 0;
1332 *res = isl_union_set_add_set(*res, isl_map_deltas(isl_map_copy(map)));
1334 return 0;
1337 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
1339 return cond_un_op(umap, &deltas_entry);
1342 static int deltas_map_entry(void **entry, void *user)
1344 isl_map *map = *entry;
1345 isl_union_map **res = user;
1347 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1348 return 0;
1350 *res = isl_union_map_add_map(*res,
1351 isl_map_deltas_map(isl_map_copy(map)));
1353 return 0;
1356 __isl_give isl_union_map *isl_union_map_deltas_map(
1357 __isl_take isl_union_map *umap)
1359 return cond_un_op(umap, &deltas_map_entry);
1362 static int identity_entry(void **entry, void *user)
1364 isl_set *set = *entry;
1365 isl_union_map **res = user;
1367 *res = isl_union_map_add_map(*res, isl_set_identity(isl_set_copy(set)));
1369 return 0;
1372 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
1374 return cond_un_op(uset, &identity_entry);
1377 static int unwrap_entry(void **entry, void *user)
1379 isl_set *set = *entry;
1380 isl_union_set **res = user;
1382 if (!isl_set_is_wrapping(set))
1383 return 0;
1385 *res = isl_union_map_add_map(*res, isl_set_unwrap(isl_set_copy(set)));
1387 return 0;
1390 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
1392 return cond_un_op(uset, &unwrap_entry);
1395 static int wrap_entry(void **entry, void *user)
1397 isl_map *map = *entry;
1398 isl_union_set **res = user;
1400 *res = isl_union_set_add_set(*res, isl_map_wrap(isl_map_copy(map)));
1402 return 0;
1405 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
1407 return cond_un_op(umap, &wrap_entry);
1410 struct isl_union_map_is_subset_data {
1411 isl_union_map *umap2;
1412 int is_subset;
1415 static int is_subset_entry(void **entry, void *user)
1417 struct isl_union_map_is_subset_data *data = user;
1418 uint32_t hash;
1419 struct isl_hash_table_entry *entry2;
1420 isl_map *map = *entry;
1422 hash = isl_dim_get_hash(map->dim);
1423 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1424 hash, &has_dim, map->dim, 0);
1425 if (!entry2) {
1426 data->is_subset = 0;
1427 return -1;
1430 data->is_subset = isl_map_is_subset(map, entry2->data);
1431 if (data->is_subset < 0 || !data->is_subset)
1432 return -1;
1434 return 0;
1437 int isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
1438 __isl_keep isl_union_map *umap2)
1440 struct isl_union_map_is_subset_data data = { NULL, 1 };
1442 umap1 = isl_union_map_copy(umap1);
1443 umap2 = isl_union_map_copy(umap2);
1444 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_dim(umap2));
1445 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_dim(umap1));
1447 if (!umap1 || !umap2)
1448 goto error;
1450 data.umap2 = umap2;
1451 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1452 &is_subset_entry, &data) < 0 &&
1453 data.is_subset)
1454 goto error;
1456 isl_union_map_free(umap1);
1457 isl_union_map_free(umap2);
1459 return data.is_subset;
1460 error:
1461 isl_union_map_free(umap1);
1462 isl_union_map_free(umap2);
1463 return -1;
1466 int isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
1467 __isl_keep isl_union_set *uset2)
1469 return isl_union_map_is_subset(uset1, uset2);
1472 int isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
1473 __isl_keep isl_union_map *umap2)
1475 int is_subset;
1477 if (!umap1 || !umap2)
1478 return -1;
1479 is_subset = isl_union_map_is_subset(umap1, umap2);
1480 if (is_subset != 1)
1481 return is_subset;
1482 is_subset = isl_union_map_is_subset(umap2, umap1);
1483 return is_subset;
1486 int isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
1487 __isl_keep isl_union_set *uset2)
1489 return isl_union_map_is_equal(uset1, uset2);
1492 int isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
1493 __isl_keep isl_union_map *umap2)
1495 int is_subset;
1497 if (!umap1 || !umap2)
1498 return -1;
1499 is_subset = isl_union_map_is_subset(umap1, umap2);
1500 if (is_subset != 1)
1501 return is_subset;
1502 is_subset = isl_union_map_is_subset(umap2, umap1);
1503 if (is_subset == -1)
1504 return is_subset;
1505 return !is_subset;
1508 int isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
1509 __isl_keep isl_union_set *uset2)
1511 return isl_union_map_is_strict_subset(uset1, uset2);
1514 static int sample_entry(void **entry, void *user)
1516 isl_basic_map **sample = (isl_basic_map **)user;
1517 isl_map *map = *entry;
1519 *sample = isl_map_sample(isl_map_copy(map));
1520 if (!*sample)
1521 return -1;
1522 if (!isl_basic_map_plain_is_empty(*sample))
1523 return -1;
1524 return 0;
1527 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
1529 isl_basic_map *sample = NULL;
1531 if (!umap)
1532 return NULL;
1534 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1535 &sample_entry, &sample) < 0 &&
1536 !sample)
1537 goto error;
1539 if (!sample)
1540 sample = isl_basic_map_empty(isl_union_map_get_dim(umap));
1542 isl_union_map_free(umap);
1544 return sample;
1545 error:
1546 isl_union_map_free(umap);
1547 return NULL;
1550 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
1552 return (isl_basic_set *)isl_union_map_sample(uset);
1555 struct isl_forall_data {
1556 int res;
1557 int (*fn)(__isl_keep isl_map *map);
1560 static int forall_entry(void **entry, void *user)
1562 struct isl_forall_data *data = user;
1563 isl_map *map = *entry;
1565 data->res = data->fn(map);
1566 if (data->res < 0)
1567 return -1;
1569 if (!data->res)
1570 return -1;
1572 return 0;
1575 static int union_map_forall(__isl_keep isl_union_map *umap,
1576 int (*fn)(__isl_keep isl_map *map))
1578 struct isl_forall_data data = { 1, fn };
1580 if (!umap)
1581 return -1;
1583 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1584 &forall_entry, &data) < 0 && data.res)
1585 return -1;
1587 return data.res;
1590 struct isl_forall_user_data {
1591 int res;
1592 int (*fn)(__isl_keep isl_map *map, void *user);
1593 void *user;
1596 static int forall_user_entry(void **entry, void *user)
1598 struct isl_forall_user_data *data = user;
1599 isl_map *map = *entry;
1601 data->res = data->fn(map, data->user);
1602 if (data->res < 0)
1603 return -1;
1605 if (!data->res)
1606 return -1;
1608 return 0;
1611 /* Check if fn(map, user) returns true for all maps "map" in umap.
1613 static int union_map_forall_user(__isl_keep isl_union_map *umap,
1614 int (*fn)(__isl_keep isl_map *map, void *user), void *user)
1616 struct isl_forall_user_data data = { 1, fn, user };
1618 if (!umap)
1619 return -1;
1621 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1622 &forall_user_entry, &data) < 0 && data.res)
1623 return -1;
1625 return data.res;
1628 int isl_union_map_is_empty(__isl_keep isl_union_map *umap)
1630 return union_map_forall(umap, &isl_map_is_empty);
1633 int isl_union_set_is_empty(__isl_keep isl_union_set *uset)
1635 return isl_union_map_is_empty(uset);
1638 static int is_subset_of_identity(__isl_keep isl_map *map)
1640 int is_subset;
1641 isl_dim *dim;
1642 isl_map *id;
1644 if (!map)
1645 return -1;
1647 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1648 return 0;
1650 dim = isl_map_get_dim(map);
1651 id = isl_map_identity(dim);
1653 is_subset = isl_map_is_subset(map, id);
1655 isl_map_free(id);
1657 return is_subset;
1660 /* Check if the given map is single-valued.
1661 * We simply compute
1663 * M \circ M^-1
1665 * and check if the result is a subset of the identity mapping.
1667 int isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
1669 isl_union_map *test;
1670 int sv;
1672 if (isl_union_map_n_map(umap) == 1) {
1673 isl_map *map;
1674 umap = isl_union_map_copy(umap);
1675 map = isl_map_from_union_map(umap);
1676 sv = isl_map_is_single_valued(map);
1677 isl_map_free(map);
1678 return sv;
1681 test = isl_union_map_reverse(isl_union_map_copy(umap));
1682 test = isl_union_map_apply_range(test, isl_union_map_copy(umap));
1684 sv = union_map_forall(test, &is_subset_of_identity);
1686 isl_union_map_free(test);
1688 return sv;
1691 int isl_union_map_is_injective(__isl_keep isl_union_map *umap)
1693 int in;
1695 umap = isl_union_map_copy(umap);
1696 umap = isl_union_map_reverse(umap);
1697 in = isl_union_map_is_single_valued(umap);
1698 isl_union_map_free(umap);
1700 return in;
1703 /* Represents a map that has a fixed value (v) for one of its
1704 * range dimensions.
1705 * The map in this structure is not reference counted, so it
1706 * is only valid while the isl_union_map from which it was
1707 * obtained is still alive.
1709 struct isl_fixed_map {
1710 isl_int v;
1711 isl_map *map;
1714 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
1715 int n)
1717 int i;
1718 struct isl_fixed_map *v;
1720 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
1721 if (!v)
1722 return NULL;
1723 for (i = 0; i < n; ++i)
1724 isl_int_init(v[i].v);
1725 return v;
1728 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
1730 int i;
1732 if (!v)
1733 return;
1734 for (i = 0; i < n; ++i)
1735 isl_int_clear(v[i].v);
1736 free(v);
1739 /* Compare the "v" field of two isl_fixed_map structs.
1741 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
1743 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
1744 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
1746 return isl_int_cmp(e1->v, e2->v);
1749 /* Internal data structure used while checking whether all maps
1750 * in a union_map have a fixed value for a given output dimension.
1751 * v is the list of maps, with the fixed value for the dimension
1752 * n is the number of maps considered so far
1753 * pos is the output dimension under investigation
1755 struct isl_fixed_dim_data {
1756 struct isl_fixed_map *v;
1757 int n;
1758 int pos;
1761 static int fixed_at_pos(__isl_keep isl_map *map, void *user)
1763 struct isl_fixed_dim_data *data = user;
1765 data->v[data->n].map = map;
1766 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
1767 &data->v[data->n++].v);
1770 static int plain_injective_on_range(__isl_take isl_union_map *umap,
1771 int first, int n_range);
1773 /* Given a list of the maps, with their fixed values at output dimension "pos",
1774 * check whether the ranges of the maps form an obvious partition.
1776 * We first sort the maps according to their fixed values.
1777 * If all maps have a different value, then we know the ranges form
1778 * a partition.
1779 * Otherwise, we collect the maps with the same fixed value and
1780 * check whether each such collection is obviously injective
1781 * based on later dimensions.
1783 static int separates(struct isl_fixed_map *v, int n,
1784 __isl_take isl_dim *dim, int pos, int n_range)
1786 int i;
1788 if (!v)
1789 goto error;
1791 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
1793 for (i = 0; i + 1 < n; ++i) {
1794 int j, k;
1795 isl_union_map *part;
1796 int injective;
1798 for (j = i + 1; j < n; ++j)
1799 if (isl_int_ne(v[i].v, v[j].v))
1800 break;
1802 if (j == i + 1)
1803 continue;
1805 part = isl_union_map_alloc(isl_dim_copy(dim), j - i);
1806 for (k = i; k < j; ++k)
1807 part = isl_union_map_add_map(part,
1808 isl_map_copy(v[k].map));
1810 injective = plain_injective_on_range(part, pos + 1, n_range);
1811 if (injective < 0)
1812 goto error;
1813 if (!injective)
1814 break;
1816 i = j - 1;
1819 isl_dim_free(dim);
1820 free_isl_fixed_map_array(v, n);
1821 return i + 1 >= n;
1822 error:
1823 isl_dim_free(dim);
1824 free_isl_fixed_map_array(v, n);
1825 return -1;
1828 /* Check whether the maps in umap have obviously distinct ranges.
1829 * In particular, check for an output dimension in the range
1830 * [first,n_range) for which all maps have a fixed value
1831 * and then check if these values, possibly along with fixed values
1832 * at later dimensions, entail distinct ranges.
1834 static int plain_injective_on_range(__isl_take isl_union_map *umap,
1835 int first, int n_range)
1837 isl_ctx *ctx;
1838 int n;
1839 struct isl_fixed_dim_data data = { NULL };
1841 ctx = isl_union_map_get_ctx(umap);
1843 if (!umap)
1844 goto error;
1846 n = isl_union_map_n_map(umap);
1847 if (n <= 1) {
1848 isl_union_map_free(umap);
1849 return 1;
1852 if (first >= n_range) {
1853 isl_union_map_free(umap);
1854 return 0;
1857 data.v = alloc_isl_fixed_map_array(ctx, n);
1858 if (!data.v)
1859 goto error;
1861 for (data.pos = first; data.pos < n_range; ++data.pos) {
1862 int fixed;
1863 int injective;
1864 isl_dim *dim;
1866 data.n = 0;
1867 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
1868 if (fixed < 0)
1869 goto error;
1870 if (!fixed)
1871 continue;
1872 dim = isl_union_map_get_dim(umap);
1873 injective = separates(data.v, n, dim, data.pos, n_range);
1874 isl_union_map_free(umap);
1875 return injective;
1878 free_isl_fixed_map_array(data.v, n);
1879 isl_union_map_free(umap);
1881 return 0;
1882 error:
1883 free_isl_fixed_map_array(data.v, n);
1884 isl_union_map_free(umap);
1885 return -1;
1888 /* Check whether the maps in umap that map to subsets of "ran"
1889 * have obviously distinct ranges.
1891 static int plain_injective_on_range_wrap(__isl_keep isl_set *ran, void *user)
1893 isl_union_map *umap = user;
1895 umap = isl_union_map_copy(umap);
1896 umap = isl_union_map_intersect_range(umap,
1897 isl_union_set_from_set(isl_set_copy(ran)));
1898 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
1901 /* Check if the given union_map is obviously injective.
1903 * In particular, we first check if all individual maps are obviously
1904 * injective and then check if all the ranges of these maps are
1905 * obviously disjoint.
1907 int isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
1909 int in;
1910 isl_union_map *univ;
1911 isl_union_set *ran;
1913 in = union_map_forall(umap, &isl_map_plain_is_injective);
1914 if (in < 0)
1915 return -1;
1916 if (!in)
1917 return 0;
1919 univ = isl_union_map_universe(isl_union_map_copy(umap));
1920 ran = isl_union_map_range(univ);
1922 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
1924 isl_union_set_free(ran);
1926 return in;
1929 int isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
1931 int sv;
1933 sv = isl_union_map_is_single_valued(umap);
1934 if (sv < 0 || !sv)
1935 return sv;
1937 return isl_union_map_is_injective(umap);
1940 static int zip_entry(void **entry, void *user)
1942 isl_map *map = *entry;
1943 isl_union_map **res = user;
1945 if (!isl_map_can_zip(map))
1946 return 0;
1948 *res = isl_union_map_add_map(*res, isl_map_zip(isl_map_copy(map)));
1950 return 0;
1953 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
1955 return cond_un_op(umap, &zip_entry);
1958 static int lift_entry(void **entry, void *user)
1960 isl_set *set = *entry;
1961 isl_union_set **res = user;
1963 *res = isl_union_set_add_set(*res, isl_set_lift(isl_set_copy(set)));
1965 return 0;
1968 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
1970 return cond_un_op(uset, &lift_entry);
1973 static int coefficients_entry(void **entry, void *user)
1975 isl_set *set = *entry;
1976 isl_union_set **res = user;
1978 set = isl_set_copy(set);
1979 set = isl_set_from_basic_set(isl_set_coefficients(set));
1980 *res = isl_union_set_add_set(*res, set);
1982 return 0;
1985 __isl_give isl_union_set *isl_union_set_coefficients(
1986 __isl_take isl_union_set *uset)
1988 isl_ctx *ctx;
1989 isl_dim *dim;
1990 isl_union_set *res;
1992 if (!uset)
1993 return NULL;
1995 ctx = isl_union_set_get_ctx(uset);
1996 dim = isl_dim_set_alloc(ctx, 0, 0);
1997 res = isl_union_map_alloc(dim, uset->table.n);
1998 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
1999 &coefficients_entry, &res) < 0)
2000 goto error;
2002 isl_union_set_free(uset);
2003 return res;
2004 error:
2005 isl_union_set_free(uset);
2006 isl_union_set_free(res);
2007 return NULL;
2010 static int solutions_entry(void **entry, void *user)
2012 isl_set *set = *entry;
2013 isl_union_set **res = user;
2015 set = isl_set_copy(set);
2016 set = isl_set_from_basic_set(isl_set_solutions(set));
2017 if (!*res)
2018 *res = isl_union_set_from_set(set);
2019 else
2020 *res = isl_union_set_add_set(*res, set);
2022 if (!*res)
2023 return -1;
2025 return 0;
2028 __isl_give isl_union_set *isl_union_set_solutions(
2029 __isl_take isl_union_set *uset)
2031 isl_union_set *res = NULL;
2033 if (!uset)
2034 return NULL;
2036 if (uset->table.n == 0) {
2037 res = isl_union_set_empty(isl_union_set_get_dim(uset));
2038 isl_union_set_free(uset);
2039 return res;
2042 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2043 &solutions_entry, &res) < 0)
2044 goto error;
2046 isl_union_set_free(uset);
2047 return res;
2048 error:
2049 isl_union_set_free(uset);
2050 isl_union_set_free(res);
2051 return NULL;