c02b6d7310329270b9a15145649be6a2346067d1
[platform/upstream/isl.git] / isl_union_map.c
1 /*
2  * Copyright 2010-2011 INRIA Saclay
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
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  */
10
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>
20
21 /* Is this union set a parameter domain?
22  */
23 int isl_union_set_is_params(__isl_keep isl_union_set *uset)
24 {
25         isl_set *set;
26         int params;
27
28         if (!uset)
29                 return -1;
30         if (uset->table.n != 1)
31                 return 0;
32
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;
37 }
38
39 static __isl_give isl_union_map *isl_union_map_alloc(__isl_take isl_space *dim,
40         int size)
41 {
42         isl_union_map *umap;
43
44         if (!dim)
45                 return NULL;
46
47         umap = isl_calloc_type(dim->ctx, isl_union_map);
48         if (!umap)
49                 return NULL;
50
51         umap->ref = 1;
52         umap->dim = dim;
53         if (isl_hash_table_init(dim->ctx, &umap->table, size) < 0)
54                 goto error;
55
56         return umap;
57 error:
58         isl_space_free(dim);
59         isl_union_map_free(umap);
60         return NULL;
61 }
62
63 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *dim)
64 {
65         return isl_union_map_alloc(dim, 16);
66 }
67
68 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *dim)
69 {
70         return isl_union_map_empty(dim);
71 }
72
73 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
74 {
75         return umap ? umap->dim->ctx : NULL;
76 }
77
78 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
79 {
80         return uset ? uset->dim->ctx : NULL;
81 }
82
83 __isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
84 {
85         if (!umap)
86                 return NULL;
87         return isl_space_copy(umap->dim);
88 }
89
90 __isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
91 {
92         return isl_union_map_get_space(uset);
93 }
94
95 static int free_umap_entry(void **entry, void *user)
96 {
97         isl_map *map = *entry;
98         isl_map_free(map);
99         return 0;
100 }
101
102 static int add_map(__isl_take isl_map *map, void *user)
103 {
104         isl_union_map **umap = (isl_union_map **)user;
105
106         *umap = isl_union_map_add_map(*umap, map);
107
108         return 0;
109 }
110
111 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
112 {
113         isl_union_map *dup;
114
115         if (!umap)
116                 return NULL;
117
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;
125 }
126
127 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
128 {
129         if (!umap)
130                 return NULL;
131
132         if (umap->ref == 1)
133                 return umap;
134         umap->ref--;
135         return isl_union_map_dup(umap);
136 }
137
138 struct isl_union_align {
139         isl_reordering *exp;
140         isl_union_map *res;
141 };
142
143 static int align_entry(void **entry, void *user)
144 {
145         isl_map *map = *entry;
146         isl_reordering *exp;
147         struct isl_union_align *data = user;
148
149         exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
150                                     isl_map_get_space(map));
151
152         data->res = isl_union_map_add_map(data->res,
153                                         isl_map_realign(isl_map_copy(map), exp));
154
155         return 0;
156 }
157
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.
162  */
163 __isl_give isl_union_map *isl_union_map_align_params(
164         __isl_take isl_union_map *umap, __isl_take isl_space *model)
165 {
166         struct isl_union_align data = { NULL, NULL };
167
168         if (!umap || !model)
169                 goto error;
170
171         if (isl_space_match(umap->dim, isl_dim_param, model, isl_dim_param)) {
172                 isl_space_free(model);
173                 return umap;
174         }
175
176         model = isl_space_params(model);
177         data.exp = isl_parameter_alignment_reordering(umap->dim, model);
178         if (!data.exp)
179                 goto error;
180
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;
186
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;
197 }
198
199 __isl_give isl_union_set *isl_union_set_align_params(
200         __isl_take isl_union_set *uset, __isl_take isl_space *model)
201 {
202         return isl_union_map_align_params(uset, model);
203 }
204
205 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
206         __isl_take isl_union_map *umap2)
207 {
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));
210
211         umap1 = isl_union_map_cow(umap1);
212
213         if (!umap1 || !umap2)
214                 goto error;
215
216         if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
217                 goto error;
218
219         isl_union_map_free(umap2);
220
221         return umap1;
222 error:
223         isl_union_map_free(umap1);
224         isl_union_map_free(umap2);
225         return NULL;
226 }
227
228 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
229         __isl_take isl_union_set *uset2)
230 {
231         return isl_union_map_union(uset1, uset2);
232 }
233
234 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
235 {
236         if (!umap)
237                 return NULL;
238
239         umap->ref++;
240         return umap;
241 }
242
243 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
244 {
245         return isl_union_map_copy(uset);
246 }
247
248 void *isl_union_map_free(__isl_take isl_union_map *umap)
249 {
250         if (!umap)
251                 return NULL;
252
253         if (--umap->ref > 0)
254                 return NULL;
255
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;
262 }
263
264 void *isl_union_set_free(__isl_take isl_union_set *uset)
265 {
266         return isl_union_map_free(uset);
267 }
268
269 static int has_dim(const void *entry, const void *val)
270 {
271         isl_map *map = (isl_map *)entry;
272         isl_space *dim = (isl_space *)val;
273
274         return isl_space_is_equal(map->dim, dim);
275 }
276
277 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
278         __isl_take isl_map *map)
279 {
280         uint32_t hash;
281         struct isl_hash_table_entry *entry;
282
283         if (!map || !umap)
284                 goto error;
285
286         if (isl_map_plain_is_empty(map)) {
287                 isl_map_free(map);
288                 return umap;
289         }
290
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));
294         }
295
296         umap = isl_union_map_cow(umap);
297
298         if (!map || !umap)
299                 goto error;
300
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;
306
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);
314         }
315
316         return umap;
317 error:
318         isl_map_free(map);
319         isl_union_map_free(umap);
320         return NULL;
321 }
322
323 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
324         __isl_take isl_set *set)
325 {
326         return isl_union_map_add_map(uset, (isl_map *)set);
327 }
328
329 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
330 {
331         isl_space *dim;
332         isl_union_map *umap;
333
334         if (!map)
335                 return NULL;
336
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);
341
342         return umap;
343 }
344
345 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
346 {
347         return isl_union_map_from_map((isl_map *)set);
348 }
349
350 __isl_give isl_union_map *isl_union_map_from_basic_map(
351         __isl_take isl_basic_map *bmap)
352 {
353         return isl_union_map_from_map(isl_map_from_basic_map(bmap));
354 }
355
356 __isl_give isl_union_set *isl_union_set_from_basic_set(
357         __isl_take isl_basic_set *bset)
358 {
359         return isl_union_map_from_basic_map(bset);
360 }
361
362 struct isl_union_map_foreach_data
363 {
364         int (*fn)(__isl_take isl_map *map, void *user);
365         void *user;
366 };
367
368 static int call_on_copy(void **entry, void *user)
369 {
370         isl_map *map = *entry;
371         struct isl_union_map_foreach_data *data;
372         data = (struct isl_union_map_foreach_data *)user;
373
374         return data->fn(isl_map_copy(map), data->user);
375 }
376
377 int isl_union_map_n_map(__isl_keep isl_union_map *umap)
378 {
379         return umap ? umap->table.n : 0;
380 }
381
382 int isl_union_set_n_set(__isl_keep isl_union_set *uset)
383 {
384         return uset ? uset->table.n : 0;
385 }
386
387 int isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
388         int (*fn)(__isl_take isl_map *map, void *user), void *user)
389 {
390         struct isl_union_map_foreach_data data = { fn, user };
391
392         if (!umap)
393                 return -1;
394
395         return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
396                                       &call_on_copy, &data);
397 }
398
399 static int copy_map(void **entry, void *user)
400 {
401         isl_map *map = *entry;
402         isl_map **map_p = user;
403
404         *map_p = isl_map_copy(map);
405
406         return -1;
407 }
408
409 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
410 {
411         isl_ctx *ctx;
412         isl_map *map = NULL;
413
414         if (!umap)
415                 return NULL;
416         ctx = isl_union_map_get_ctx(umap);
417         if (umap->table.n != 1)
418                 isl_die(ctx, isl_error_invalid,
419                         "union map needs to contain elements in exactly "
420                         "one space", return isl_union_map_free(umap));
421
422         isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
423
424         isl_union_map_free(umap);
425
426         return map;
427 }
428
429 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
430 {
431         return isl_map_from_union_map(uset);
432 }
433
434 /* Extract the map in "umap" that lives in the given space (ignoring
435  * parameters).
436  */
437 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
438         __isl_take isl_space *space)
439 {
440         uint32_t hash;
441         struct isl_hash_table_entry *entry;
442
443         space = isl_space_drop_dims(space, isl_dim_param,
444                                         0, isl_space_dim(space, isl_dim_param));
445         space = isl_space_align_params(space, isl_union_map_get_space(umap));
446         if (!umap || !space)
447                 goto error;
448
449         hash = isl_space_get_hash(space);
450         entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
451                                     &has_dim, space, 0);
452         if (!entry)
453                 return isl_map_empty(space);
454         isl_space_free(space);
455         return isl_map_copy(entry->data);
456 error:
457         isl_space_free(space);
458         return NULL;
459 }
460
461 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
462         __isl_take isl_space *dim)
463 {
464         return (isl_set *)isl_union_map_extract_map(uset, dim);
465 }
466
467 /* Check if umap contains a map in the given space.
468  */
469 __isl_give int isl_union_map_contains(__isl_keep isl_union_map *umap,
470         __isl_keep isl_space *dim)
471 {
472         uint32_t hash;
473         struct isl_hash_table_entry *entry;
474
475         if (!umap || !dim)
476                 return -1;
477
478         hash = isl_space_get_hash(dim);
479         entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
480                                     &has_dim, dim, 0);
481         return !!entry;
482 }
483
484 __isl_give int isl_union_set_contains(__isl_keep isl_union_set *uset,
485         __isl_keep isl_space *dim)
486 {
487         return isl_union_map_contains(uset, dim);
488 }
489
490 int isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
491         int (*fn)(__isl_take isl_set *set, void *user), void *user)
492 {
493         return isl_union_map_foreach_map(uset,
494                 (int(*)(__isl_take isl_map *, void*))fn, user);
495 }
496
497 struct isl_union_set_foreach_point_data {
498         int (*fn)(__isl_take isl_point *pnt, void *user);
499         void *user;
500 };
501
502 static int foreach_point(__isl_take isl_set *set, void *user)
503 {
504         struct isl_union_set_foreach_point_data *data = user;
505         int r;
506
507         r = isl_set_foreach_point(set, data->fn, data->user);
508         isl_set_free(set);
509
510         return r;
511 }
512
513 int isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
514         int (*fn)(__isl_take isl_point *pnt, void *user), void *user)
515 {
516         struct isl_union_set_foreach_point_data data = { fn, user };
517         return isl_union_set_foreach_set(uset, &foreach_point, &data);
518 }
519
520 struct isl_union_map_gen_bin_data {
521         isl_union_map *umap2;
522         isl_union_map *res;
523 };
524
525 static int subtract_entry(void **entry, void *user)
526 {
527         struct isl_union_map_gen_bin_data *data = user;
528         uint32_t hash;
529         struct isl_hash_table_entry *entry2;
530         isl_map *map = *entry;
531
532         hash = isl_space_get_hash(map->dim);
533         entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
534                                      hash, &has_dim, map->dim, 0);
535         map = isl_map_copy(map);
536         if (entry2) {
537                 int empty;
538                 map = isl_map_subtract(map, isl_map_copy(entry2->data));
539
540                 empty = isl_map_is_empty(map);
541                 if (empty < 0) {
542                         isl_map_free(map);
543                         return -1;
544                 }
545                 if (empty) {
546                         isl_map_free(map);
547                         return 0;
548                 }
549         }
550         data->res = isl_union_map_add_map(data->res, map);
551
552         return 0;
553 }
554
555 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
556         __isl_take isl_union_map *umap2, int (*fn)(void **, void *))
557 {
558         struct isl_union_map_gen_bin_data data = { NULL, NULL };
559
560         umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
561         umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
562
563         if (!umap1 || !umap2)
564                 goto error;
565
566         data.umap2 = umap2;
567         data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
568                                        umap1->table.n);
569         if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
570                                    fn, &data) < 0)
571                 goto error;
572
573         isl_union_map_free(umap1);
574         isl_union_map_free(umap2);
575         return data.res;
576 error:
577         isl_union_map_free(umap1);
578         isl_union_map_free(umap2);
579         isl_union_map_free(data.res);
580         return NULL;
581 }
582
583 __isl_give isl_union_map *isl_union_map_subtract(
584         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
585 {
586         return gen_bin_op(umap1, umap2, &subtract_entry);
587 }
588
589 __isl_give isl_union_set *isl_union_set_subtract(
590         __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
591 {
592         return isl_union_map_subtract(uset1, uset2);
593 }
594
595 struct isl_union_map_gen_bin_set_data {
596         isl_set *set;
597         isl_union_map *res;
598 };
599
600 static int intersect_params_entry(void **entry, void *user)
601 {
602         struct isl_union_map_gen_bin_set_data *data = user;
603         isl_map *map = *entry;
604         int empty;
605
606         map = isl_map_copy(map);
607         map = isl_map_intersect_params(map, isl_set_copy(data->set));
608
609         empty = isl_map_is_empty(map);
610         if (empty < 0) {
611                 isl_map_free(map);
612                 return -1;
613         }
614
615         data->res = isl_union_map_add_map(data->res, map);
616
617         return 0;
618 }
619
620 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
621         __isl_take isl_set *set, int (*fn)(void **, void *))
622 {
623         struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
624
625         umap = isl_union_map_align_params(umap, isl_set_get_space(set));
626         set = isl_set_align_params(set, isl_union_map_get_space(umap));
627
628         if (!umap || !set)
629                 goto error;
630
631         data.set = set;
632         data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
633                                        umap->table.n);
634         if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
635                                    fn, &data) < 0)
636                 goto error;
637
638         isl_union_map_free(umap);
639         isl_set_free(set);
640         return data.res;
641 error:
642         isl_union_map_free(umap);
643         isl_set_free(set);
644         isl_union_map_free(data.res);
645         return NULL;
646 }
647
648 __isl_give isl_union_map *isl_union_map_intersect_params(
649         __isl_take isl_union_map *umap, __isl_take isl_set *set)
650 {
651         return gen_bin_set_op(umap, set, &intersect_params_entry);
652 }
653
654 __isl_give isl_union_set *isl_union_set_intersect_params(
655         __isl_take isl_union_set *uset, __isl_take isl_set *set)
656 {
657         return isl_union_map_intersect_params(uset, set);
658 }
659
660 static __isl_give isl_union_map *union_map_intersect_params(
661         __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
662 {
663         return isl_union_map_intersect_params(umap,
664                                                 isl_set_from_union_set(uset));
665 }
666
667 static __isl_give isl_union_map *union_map_gist_params(
668         __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
669 {
670         return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
671 }
672
673 struct isl_union_map_match_bin_data {
674         isl_union_map *umap2;
675         isl_union_map *res;
676         __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
677 };
678
679 static int match_bin_entry(void **entry, void *user)
680 {
681         struct isl_union_map_match_bin_data *data = user;
682         uint32_t hash;
683         struct isl_hash_table_entry *entry2;
684         isl_map *map = *entry;
685         int empty;
686
687         hash = isl_space_get_hash(map->dim);
688         entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
689                                      hash, &has_dim, map->dim, 0);
690         if (!entry2)
691                 return 0;
692
693         map = isl_map_copy(map);
694         map = data->fn(map, isl_map_copy(entry2->data));
695
696         empty = isl_map_is_empty(map);
697         if (empty < 0) {
698                 isl_map_free(map);
699                 return -1;
700         }
701         if (empty) {
702                 isl_map_free(map);
703                 return 0;
704         }
705
706         data->res = isl_union_map_add_map(data->res, map);
707
708         return 0;
709 }
710
711 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
712         __isl_take isl_union_map *umap2,
713         __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
714 {
715         struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
716
717         umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
718         umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
719
720         if (!umap1 || !umap2)
721                 goto error;
722
723         data.umap2 = umap2;
724         data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
725                                        umap1->table.n);
726         if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
727                                    &match_bin_entry, &data) < 0)
728                 goto error;
729
730         isl_union_map_free(umap1);
731         isl_union_map_free(umap2);
732         return data.res;
733 error:
734         isl_union_map_free(umap1);
735         isl_union_map_free(umap2);
736         isl_union_map_free(data.res);
737         return NULL;
738 }
739
740 __isl_give isl_union_map *isl_union_map_intersect(
741         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
742 {
743         return match_bin_op(umap1, umap2, &isl_map_intersect);
744 }
745
746 /* Compute the intersection of the two union_sets.
747  * As a special case, if exactly one of the two union_sets
748  * is a parameter domain, then intersect the parameter domain
749  * of the other one with this set.
750  */
751 __isl_give isl_union_set *isl_union_set_intersect(
752         __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
753 {
754         int p1, p2;
755
756         p1 = isl_union_set_is_params(uset1);
757         p2 = isl_union_set_is_params(uset2);
758         if (p1 < 0 || p2 < 0)
759                 goto error;
760         if (!p1 && p2)
761                 return union_map_intersect_params(uset1, uset2);
762         if (p1 && !p2)
763                 return union_map_intersect_params(uset2, uset1);
764         return isl_union_map_intersect(uset1, uset2);
765 error:
766         isl_union_set_free(uset1);
767         isl_union_set_free(uset2);
768         return NULL;
769 }
770
771 static int gist_params_entry(void **entry, void *user)
772 {
773         struct isl_union_map_gen_bin_set_data *data = user;
774         isl_map *map = *entry;
775         int empty;
776
777         map = isl_map_copy(map);
778         map = isl_map_gist_params(map, isl_set_copy(data->set));
779
780         empty = isl_map_is_empty(map);
781         if (empty < 0) {
782                 isl_map_free(map);
783                 return -1;
784         }
785
786         data->res = isl_union_map_add_map(data->res, map);
787
788         return 0;
789 }
790
791 __isl_give isl_union_map *isl_union_map_gist_params(
792         __isl_take isl_union_map *umap, __isl_take isl_set *set)
793 {
794         return gen_bin_set_op(umap, set, &gist_params_entry);
795 }
796
797 __isl_give isl_union_set *isl_union_set_gist_params(
798         __isl_take isl_union_set *uset, __isl_take isl_set *set)
799 {
800         return isl_union_map_gist_params(uset, set);
801 }
802
803 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
804         __isl_take isl_union_map *context)
805 {
806         return match_bin_op(umap, context, &isl_map_gist);
807 }
808
809 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
810         __isl_take isl_union_set *context)
811 {
812         if (isl_union_set_is_params(context))
813                 return union_map_gist_params(uset, context);
814         return isl_union_map_gist(uset, context);
815 }
816
817 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
818         __isl_take isl_map *set2)
819 {
820         return isl_set_lex_le_set((isl_set *)set1, (isl_set *)set2);
821 }
822
823 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
824         __isl_take isl_map *set2)
825 {
826         return isl_set_lex_lt_set((isl_set *)set1, (isl_set *)set2);
827 }
828
829 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
830         __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
831 {
832         return match_bin_op(uset1, uset2, &lex_lt_set);
833 }
834
835 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
836         __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
837 {
838         return match_bin_op(uset1, uset2, &lex_le_set);
839 }
840
841 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
842         __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
843 {
844         return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
845 }
846
847 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
848         __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
849 {
850         return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
851 }
852
853 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
854         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
855 {
856         return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
857 }
858
859 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
860         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
861 {
862         return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
863 }
864
865 static int intersect_domain_entry(void **entry, void *user)
866 {
867         struct isl_union_map_gen_bin_data *data = user;
868         uint32_t hash;
869         struct isl_hash_table_entry *entry2;
870         isl_space *dim;
871         isl_map *map = *entry;
872         int empty;
873
874         dim = isl_map_get_space(map);
875         dim = isl_space_domain(dim);
876         hash = isl_space_get_hash(dim);
877         entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
878                                      hash, &has_dim, dim, 0);
879         isl_space_free(dim);
880         if (!entry2)
881                 return 0;
882
883         map = isl_map_copy(map);
884         map = isl_map_intersect_domain(map, isl_set_copy(entry2->data));
885
886         empty = isl_map_is_empty(map);
887         if (empty < 0) {
888                 isl_map_free(map);
889                 return -1;
890         }
891         if (empty) {
892                 isl_map_free(map);
893                 return 0;
894         }
895
896         data->res = isl_union_map_add_map(data->res, map);
897
898         return 0;
899 }
900
901 /* Intersect the domain of "umap" with "uset".
902  * If "uset" is a parameters domain, then intersect the parameter
903  * domain of "umap" with this set.
904  */
905 __isl_give isl_union_map *isl_union_map_intersect_domain(
906         __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
907 {
908         if (isl_union_set_is_params(uset))
909                 return union_map_intersect_params(umap, uset);
910         return gen_bin_op(umap, uset, &intersect_domain_entry);
911 }
912
913 static int gist_domain_entry(void **entry, void *user)
914 {
915         struct isl_union_map_gen_bin_data *data = user;
916         uint32_t hash;
917         struct isl_hash_table_entry *entry2;
918         isl_space *dim;
919         isl_map *map = *entry;
920         int empty;
921
922         dim = isl_map_get_space(map);
923         dim = isl_space_domain(dim);
924         hash = isl_space_get_hash(dim);
925         entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
926                                      hash, &has_dim, dim, 0);
927         isl_space_free(dim);
928         if (!entry2)
929                 return 0;
930
931         map = isl_map_copy(map);
932         map = isl_map_gist_domain(map, isl_set_copy(entry2->data));
933
934         empty = isl_map_is_empty(map);
935         if (empty < 0) {
936                 isl_map_free(map);
937                 return -1;
938         }
939
940         data->res = isl_union_map_add_map(data->res, map);
941
942         return 0;
943 }
944
945 /* Compute the gist of "umap" with respect to the domain "uset".
946  * If "uset" is a parameters domain, then compute the gist
947  * with respect to this parameter domain.
948  */
949 __isl_give isl_union_map *isl_union_map_gist_domain(
950         __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
951 {
952         if (isl_union_set_is_params(uset))
953                 return union_map_gist_params(umap, uset);
954         return gen_bin_op(umap, uset, &gist_domain_entry);
955 }
956
957 static int gist_range_entry(void **entry, void *user)
958 {
959         struct isl_union_map_gen_bin_data *data = user;
960         uint32_t hash;
961         struct isl_hash_table_entry *entry2;
962         isl_space *space;
963         isl_map *map = *entry;
964         int empty;
965
966         space = isl_map_get_space(map);
967         space = isl_space_range(space);
968         hash = isl_space_get_hash(space);
969         entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
970                                      hash, &has_dim, space, 0);
971         isl_space_free(space);
972         if (!entry2)
973                 return 0;
974
975         map = isl_map_copy(map);
976         map = isl_map_gist_range(map, isl_set_copy(entry2->data));
977
978         empty = isl_map_is_empty(map);
979         if (empty < 0) {
980                 isl_map_free(map);
981                 return -1;
982         }
983
984         data->res = isl_union_map_add_map(data->res, map);
985
986         return 0;
987 }
988
989 /* Compute the gist of "umap" with respect to the range "uset".
990  */
991 __isl_give isl_union_map *isl_union_map_gist_range(
992         __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
993 {
994         return gen_bin_op(umap, uset, &gist_range_entry);
995 }
996
997 static int intersect_range_entry(void **entry, void *user)
998 {
999         struct isl_union_map_gen_bin_data *data = user;
1000         uint32_t hash;
1001         struct isl_hash_table_entry *entry2;
1002         isl_space *dim;
1003         isl_map *map = *entry;
1004         int empty;
1005
1006         dim = isl_map_get_space(map);
1007         dim = isl_space_range(dim);
1008         hash = isl_space_get_hash(dim);
1009         entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1010                                      hash, &has_dim, dim, 0);
1011         isl_space_free(dim);
1012         if (!entry2)
1013                 return 0;
1014
1015         map = isl_map_copy(map);
1016         map = isl_map_intersect_range(map, isl_set_copy(entry2->data));
1017
1018         empty = isl_map_is_empty(map);
1019         if (empty < 0) {
1020                 isl_map_free(map);
1021                 return -1;
1022         }
1023         if (empty) {
1024                 isl_map_free(map);
1025                 return 0;
1026         }
1027
1028         data->res = isl_union_map_add_map(data->res, map);
1029
1030         return 0;
1031 }
1032
1033 __isl_give isl_union_map *isl_union_map_intersect_range(
1034         __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1035 {
1036         return gen_bin_op(umap, uset, &intersect_range_entry);
1037 }
1038
1039 struct isl_union_map_bin_data {
1040         isl_union_map *umap2;
1041         isl_union_map *res;
1042         isl_map *map;
1043         int (*fn)(void **entry, void *user);
1044 };
1045
1046 static int apply_range_entry(void **entry, void *user)
1047 {
1048         struct isl_union_map_bin_data *data = user;
1049         isl_map *map2 = *entry;
1050         int empty;
1051
1052         if (!isl_space_tuple_match(data->map->dim, isl_dim_out,
1053                                  map2->dim, isl_dim_in))
1054                 return 0;
1055
1056         map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1057
1058         empty = isl_map_is_empty(map2);
1059         if (empty < 0) {
1060                 isl_map_free(map2);
1061                 return -1;
1062         }
1063         if (empty) {
1064                 isl_map_free(map2);
1065                 return 0;
1066         }
1067
1068         data->res = isl_union_map_add_map(data->res, map2);
1069
1070         return 0;
1071 }
1072
1073 static int bin_entry(void **entry, void *user)
1074 {
1075         struct isl_union_map_bin_data *data = user;
1076         isl_map *map = *entry;
1077
1078         data->map = map;
1079         if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1080                                    data->fn, data) < 0)
1081                 return -1;
1082
1083         return 0;
1084 }
1085
1086 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1087         __isl_take isl_union_map *umap2, int (*fn)(void **entry, void *user))
1088 {
1089         struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1090
1091         umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1092         umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1093
1094         if (!umap1 || !umap2)
1095                 goto error;
1096
1097         data.umap2 = umap2;
1098         data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1099                                        umap1->table.n);
1100         if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1101                                    &bin_entry, &data) < 0)
1102                 goto error;
1103
1104         isl_union_map_free(umap1);
1105         isl_union_map_free(umap2);
1106         return data.res;
1107 error:
1108         isl_union_map_free(umap1);
1109         isl_union_map_free(umap2);
1110         isl_union_map_free(data.res);
1111         return NULL;
1112 }
1113
1114 __isl_give isl_union_map *isl_union_map_apply_range(
1115         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1116 {
1117         return bin_op(umap1, umap2, &apply_range_entry);
1118 }
1119
1120 __isl_give isl_union_map *isl_union_map_apply_domain(
1121         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1122 {
1123         umap1 = isl_union_map_reverse(umap1);
1124         umap1 = isl_union_map_apply_range(umap1, umap2);
1125         return isl_union_map_reverse(umap1);
1126 }
1127
1128 __isl_give isl_union_set *isl_union_set_apply(
1129         __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1130 {
1131         return isl_union_map_apply_range(uset, umap);
1132 }
1133
1134 static int map_lex_lt_entry(void **entry, void *user)
1135 {
1136         struct isl_union_map_bin_data *data = user;
1137         isl_map *map2 = *entry;
1138
1139         if (!isl_space_tuple_match(data->map->dim, isl_dim_out,
1140                                  map2->dim, isl_dim_out))
1141                 return 0;
1142
1143         map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1144
1145         data->res = isl_union_map_add_map(data->res, map2);
1146
1147         return 0;
1148 }
1149
1150 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1151         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1152 {
1153         return bin_op(umap1, umap2, &map_lex_lt_entry);
1154 }
1155
1156 static int map_lex_le_entry(void **entry, void *user)
1157 {
1158         struct isl_union_map_bin_data *data = user;
1159         isl_map *map2 = *entry;
1160
1161         if (!isl_space_tuple_match(data->map->dim, isl_dim_out,
1162                                  map2->dim, isl_dim_out))
1163                 return 0;
1164
1165         map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1166
1167         data->res = isl_union_map_add_map(data->res, map2);
1168
1169         return 0;
1170 }
1171
1172 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1173         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1174 {
1175         return bin_op(umap1, umap2, &map_lex_le_entry);
1176 }
1177
1178 static int product_entry(void **entry, void *user)
1179 {
1180         struct isl_union_map_bin_data *data = user;
1181         isl_map *map2 = *entry;
1182
1183         map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1184
1185         data->res = isl_union_map_add_map(data->res, map2);
1186
1187         return 0;
1188 }
1189
1190 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1191         __isl_take isl_union_map *umap2)
1192 {
1193         return bin_op(umap1, umap2, &product_entry);
1194 }
1195
1196 static int set_product_entry(void **entry, void *user)
1197 {
1198         struct isl_union_map_bin_data *data = user;
1199         isl_set *set2 = *entry;
1200
1201         set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1202
1203         data->res = isl_union_set_add_set(data->res, set2);
1204
1205         return 0;
1206 }
1207
1208 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1209         __isl_take isl_union_set *uset2)
1210 {
1211         return bin_op(uset1, uset2, &set_product_entry);
1212 }
1213
1214 static int domain_product_entry(void **entry, void *user)
1215 {
1216         struct isl_union_map_bin_data *data = user;
1217         isl_map *map2 = *entry;
1218
1219         if (!isl_space_tuple_match(data->map->dim, isl_dim_out,
1220                                  map2->dim, isl_dim_out))
1221                 return 0;
1222
1223         map2 = isl_map_domain_product(isl_map_copy(data->map),
1224                                      isl_map_copy(map2));
1225
1226         data->res = isl_union_map_add_map(data->res, map2);
1227
1228         return 0;
1229 }
1230
1231 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1232  */
1233 __isl_give isl_union_map *isl_union_map_domain_product(
1234         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1235 {
1236         return bin_op(umap1, umap2, &domain_product_entry);
1237 }
1238
1239 static int range_product_entry(void **entry, void *user)
1240 {
1241         struct isl_union_map_bin_data *data = user;
1242         isl_map *map2 = *entry;
1243
1244         if (!isl_space_tuple_match(data->map->dim, isl_dim_in,
1245                                  map2->dim, isl_dim_in))
1246                 return 0;
1247
1248         map2 = isl_map_range_product(isl_map_copy(data->map),
1249                                      isl_map_copy(map2));
1250
1251         data->res = isl_union_map_add_map(data->res, map2);
1252
1253         return 0;
1254 }
1255
1256 __isl_give isl_union_map *isl_union_map_range_product(
1257         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1258 {
1259         return bin_op(umap1, umap2, &range_product_entry);
1260 }
1261
1262 static int flat_range_product_entry(void **entry, void *user)
1263 {
1264         struct isl_union_map_bin_data *data = user;
1265         isl_map *map2 = *entry;
1266
1267         if (!isl_space_tuple_match(data->map->dim, isl_dim_in,
1268                                  map2->dim, isl_dim_in))
1269                 return 0;
1270
1271         map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1272                                           isl_map_copy(map2));
1273
1274         data->res = isl_union_map_add_map(data->res, map2);
1275
1276         return 0;
1277 }
1278
1279 __isl_give isl_union_map *isl_union_map_flat_range_product(
1280         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1281 {
1282         return bin_op(umap1, umap2, &flat_range_product_entry);
1283 }
1284
1285 static __isl_give isl_union_set *cond_un_op(__isl_take isl_union_map *umap,
1286         int (*fn)(void **, void *))
1287 {
1288         isl_union_set *res;
1289
1290         if (!umap)
1291                 return NULL;
1292
1293         res = isl_union_map_alloc(isl_space_copy(umap->dim), umap->table.n);
1294         if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, &res) < 0)
1295                 goto error;
1296
1297         isl_union_map_free(umap);
1298         return res;
1299 error:
1300         isl_union_map_free(umap);
1301         isl_union_set_free(res);
1302         return NULL;
1303 }
1304
1305 static int from_range_entry(void **entry, void *user)
1306 {
1307         isl_map *set = *entry;
1308         isl_union_set **res = user;
1309
1310         *res = isl_union_map_add_map(*res,
1311                                         isl_map_from_range(isl_set_copy(set)));
1312
1313         return 0;
1314 }
1315
1316 __isl_give isl_union_map *isl_union_map_from_range(
1317         __isl_take isl_union_set *uset)
1318 {
1319         return cond_un_op(uset, &from_range_entry);
1320 }
1321
1322 __isl_give isl_union_map *isl_union_map_from_domain(
1323         __isl_take isl_union_set *uset)
1324 {
1325         return isl_union_map_reverse(isl_union_map_from_range(uset));
1326 }
1327
1328 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1329         __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1330 {
1331         return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1332                                          isl_union_map_from_range(range));
1333 }
1334
1335 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1336         int (*fn)(void **, void *))
1337 {
1338         umap = isl_union_map_cow(umap);
1339         if (!umap)
1340                 return NULL;
1341
1342         if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, NULL) < 0)
1343                 goto error;
1344
1345         return umap;
1346 error:
1347         isl_union_map_free(umap);
1348         return NULL;
1349 }
1350
1351 static int affine_entry(void **entry, void *user)
1352 {
1353         isl_map **map = (isl_map **)entry;
1354
1355         *map = isl_map_from_basic_map(isl_map_affine_hull(*map));
1356
1357         return *map ? 0 : -1;
1358 }
1359
1360 __isl_give isl_union_map *isl_union_map_affine_hull(
1361         __isl_take isl_union_map *umap)
1362 {
1363         return un_op(umap, &affine_entry);
1364 }
1365
1366 __isl_give isl_union_set *isl_union_set_affine_hull(
1367         __isl_take isl_union_set *uset)
1368 {
1369         return isl_union_map_affine_hull(uset);
1370 }
1371
1372 static int polyhedral_entry(void **entry, void *user)
1373 {
1374         isl_map **map = (isl_map **)entry;
1375
1376         *map = isl_map_from_basic_map(isl_map_polyhedral_hull(*map));
1377
1378         return *map ? 0 : -1;
1379 }
1380
1381 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1382         __isl_take isl_union_map *umap)
1383 {
1384         return un_op(umap, &polyhedral_entry);
1385 }
1386
1387 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1388         __isl_take isl_union_set *uset)
1389 {
1390         return isl_union_map_polyhedral_hull(uset);
1391 }
1392
1393 static int simple_entry(void **entry, void *user)
1394 {
1395         isl_map **map = (isl_map **)entry;
1396
1397         *map = isl_map_from_basic_map(isl_map_simple_hull(*map));
1398
1399         return *map ? 0 : -1;
1400 }
1401
1402 __isl_give isl_union_map *isl_union_map_simple_hull(
1403         __isl_take isl_union_map *umap)
1404 {
1405         return un_op(umap, &simple_entry);
1406 }
1407
1408 __isl_give isl_union_set *isl_union_set_simple_hull(
1409         __isl_take isl_union_set *uset)
1410 {
1411         return isl_union_map_simple_hull(uset);
1412 }
1413
1414 static int inplace_entry(void **entry, void *user)
1415 {
1416         __isl_give isl_map *(*fn)(__isl_take isl_map *);
1417         isl_map **map = (isl_map **)entry;
1418         isl_map *copy;
1419
1420         fn = *(__isl_give isl_map *(**)(__isl_take isl_map *)) user;
1421         copy = fn(isl_map_copy(*map));
1422         if (!copy)
1423                 return -1;
1424
1425         isl_map_free(*map);
1426         *map = copy;
1427
1428         return 0;
1429 }
1430
1431 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1432         __isl_give isl_map *(*fn)(__isl_take isl_map *))
1433 {
1434         if (!umap)
1435                 return NULL;
1436
1437         if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1438                                     &inplace_entry, &fn) < 0)
1439                 goto error;
1440
1441         return umap;
1442 error:
1443         isl_union_map_free(umap);
1444         return NULL;
1445 }
1446
1447 __isl_give isl_union_map *isl_union_map_coalesce(
1448         __isl_take isl_union_map *umap)
1449 {
1450         return inplace(umap, &isl_map_coalesce);
1451 }
1452
1453 __isl_give isl_union_set *isl_union_set_coalesce(
1454         __isl_take isl_union_set *uset)
1455 {
1456         return isl_union_map_coalesce(uset);
1457 }
1458
1459 __isl_give isl_union_map *isl_union_map_detect_equalities(
1460         __isl_take isl_union_map *umap)
1461 {
1462         return inplace(umap, &isl_map_detect_equalities);
1463 }
1464
1465 __isl_give isl_union_set *isl_union_set_detect_equalities(
1466         __isl_take isl_union_set *uset)
1467 {
1468         return isl_union_map_detect_equalities(uset);
1469 }
1470
1471 __isl_give isl_union_map *isl_union_map_compute_divs(
1472         __isl_take isl_union_map *umap)
1473 {
1474         return inplace(umap, &isl_map_compute_divs);
1475 }
1476
1477 __isl_give isl_union_set *isl_union_set_compute_divs(
1478         __isl_take isl_union_set *uset)
1479 {
1480         return isl_union_map_compute_divs(uset);
1481 }
1482
1483 static int lexmin_entry(void **entry, void *user)
1484 {
1485         isl_map **map = (isl_map **)entry;
1486
1487         *map = isl_map_lexmin(*map);
1488
1489         return *map ? 0 : -1;
1490 }
1491
1492 __isl_give isl_union_map *isl_union_map_lexmin(
1493         __isl_take isl_union_map *umap)
1494 {
1495         return un_op(umap, &lexmin_entry);
1496 }
1497
1498 __isl_give isl_union_set *isl_union_set_lexmin(
1499         __isl_take isl_union_set *uset)
1500 {
1501         return isl_union_map_lexmin(uset);
1502 }
1503
1504 static int lexmax_entry(void **entry, void *user)
1505 {
1506         isl_map **map = (isl_map **)entry;
1507
1508         *map = isl_map_lexmax(*map);
1509
1510         return *map ? 0 : -1;
1511 }
1512
1513 __isl_give isl_union_map *isl_union_map_lexmax(
1514         __isl_take isl_union_map *umap)
1515 {
1516         return un_op(umap, &lexmax_entry);
1517 }
1518
1519 __isl_give isl_union_set *isl_union_set_lexmax(
1520         __isl_take isl_union_set *uset)
1521 {
1522         return isl_union_map_lexmax(uset);
1523 }
1524
1525 static int universe_entry(void **entry, void *user)
1526 {
1527         isl_map *map = *entry;
1528         isl_union_map **res = user;
1529
1530         map = isl_map_universe(isl_map_get_space(map));
1531         *res = isl_union_map_add_map(*res, map);
1532
1533         return 0;
1534 }
1535
1536 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1537 {
1538         return cond_un_op(umap, &universe_entry);
1539 }
1540
1541 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1542 {
1543         return isl_union_map_universe(uset);
1544 }
1545
1546 static int reverse_entry(void **entry, void *user)
1547 {
1548         isl_map *map = *entry;
1549         isl_union_map **res = user;
1550
1551         *res = isl_union_map_add_map(*res, isl_map_reverse(isl_map_copy(map)));
1552
1553         return 0;
1554 }
1555
1556 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1557 {
1558         return cond_un_op(umap, &reverse_entry);
1559 }
1560
1561 static int params_entry(void **entry, void *user)
1562 {
1563         isl_map *map = *entry;
1564         isl_union_set **res = user;
1565
1566         *res = isl_union_set_add_set(*res, isl_map_params(isl_map_copy(map)));
1567
1568         return 0;
1569 }
1570
1571 /* Compute the parameter domain of the given union map.
1572  */
1573 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1574 {
1575         int empty;
1576
1577         empty = isl_union_map_is_empty(umap);
1578         if (empty < 0)
1579                 return isl_union_map_free(umap);
1580         if (empty)
1581                 return isl_set_empty(isl_union_map_get_space(umap));
1582         return isl_set_from_union_set(cond_un_op(umap, &params_entry));
1583 }
1584
1585 /* Compute the parameter domain of the given union set.
1586  */
1587 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
1588 {
1589         return isl_union_map_params(uset);
1590 }
1591
1592 static int domain_entry(void **entry, void *user)
1593 {
1594         isl_map *map = *entry;
1595         isl_union_set **res = user;
1596
1597         *res = isl_union_set_add_set(*res, isl_map_domain(isl_map_copy(map)));
1598
1599         return 0;
1600 }
1601
1602 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1603 {
1604         return cond_un_op(umap, &domain_entry);
1605 }
1606
1607 static int range_entry(void **entry, void *user)
1608 {
1609         isl_map *map = *entry;
1610         isl_union_set **res = user;
1611
1612         *res = isl_union_set_add_set(*res, isl_map_range(isl_map_copy(map)));
1613
1614         return 0;
1615 }
1616
1617 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1618 {
1619         return cond_un_op(umap, &range_entry);
1620 }
1621
1622 static int domain_map_entry(void **entry, void *user)
1623 {
1624         isl_map *map = *entry;
1625         isl_union_set **res = user;
1626
1627         *res = isl_union_map_add_map(*res,
1628                                         isl_map_domain_map(isl_map_copy(map)));
1629
1630         return 0;
1631 }
1632
1633 __isl_give isl_union_map *isl_union_map_domain_map(
1634         __isl_take isl_union_map *umap)
1635 {
1636         return cond_un_op(umap, &domain_map_entry);
1637 }
1638
1639 static int range_map_entry(void **entry, void *user)
1640 {
1641         isl_map *map = *entry;
1642         isl_union_set **res = user;
1643
1644         *res = isl_union_map_add_map(*res,
1645                                         isl_map_range_map(isl_map_copy(map)));
1646
1647         return 0;
1648 }
1649
1650 __isl_give isl_union_map *isl_union_map_range_map(
1651         __isl_take isl_union_map *umap)
1652 {
1653         return cond_un_op(umap, &range_map_entry);
1654 }
1655
1656 static int deltas_entry(void **entry, void *user)
1657 {
1658         isl_map *map = *entry;
1659         isl_union_set **res = user;
1660
1661         if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1662                 return 0;
1663
1664         *res = isl_union_set_add_set(*res, isl_map_deltas(isl_map_copy(map)));
1665
1666         return 0;
1667 }
1668
1669 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
1670 {
1671         return cond_un_op(umap, &deltas_entry);
1672 }
1673
1674 static int deltas_map_entry(void **entry, void *user)
1675 {
1676         isl_map *map = *entry;
1677         isl_union_map **res = user;
1678
1679         if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1680                 return 0;
1681
1682         *res = isl_union_map_add_map(*res,
1683                                      isl_map_deltas_map(isl_map_copy(map)));
1684
1685         return 0;
1686 }
1687
1688 __isl_give isl_union_map *isl_union_map_deltas_map(
1689         __isl_take isl_union_map *umap)
1690 {
1691         return cond_un_op(umap, &deltas_map_entry);
1692 }
1693
1694 static int identity_entry(void **entry, void *user)
1695 {
1696         isl_set *set = *entry;
1697         isl_union_map **res = user;
1698
1699         *res = isl_union_map_add_map(*res, isl_set_identity(isl_set_copy(set)));
1700
1701         return 0;
1702 }
1703
1704 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
1705 {
1706         return cond_un_op(uset, &identity_entry);
1707 }
1708
1709 static int unwrap_entry(void **entry, void *user)
1710 {
1711         isl_set *set = *entry;
1712         isl_union_set **res = user;
1713
1714         if (!isl_set_is_wrapping(set))
1715                 return 0;
1716
1717         *res = isl_union_map_add_map(*res, isl_set_unwrap(isl_set_copy(set)));
1718
1719         return 0;
1720 }
1721
1722 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
1723 {
1724         return cond_un_op(uset, &unwrap_entry);
1725 }
1726
1727 static int wrap_entry(void **entry, void *user)
1728 {
1729         isl_map *map = *entry;
1730         isl_union_set **res = user;
1731
1732         *res = isl_union_set_add_set(*res, isl_map_wrap(isl_map_copy(map)));
1733
1734         return 0;
1735 }
1736
1737 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
1738 {
1739         return cond_un_op(umap, &wrap_entry);
1740 }
1741
1742 struct isl_union_map_is_subset_data {
1743         isl_union_map *umap2;
1744         int is_subset;
1745 };
1746
1747 static int is_subset_entry(void **entry, void *user)
1748 {
1749         struct isl_union_map_is_subset_data *data = user;
1750         uint32_t hash;
1751         struct isl_hash_table_entry *entry2;
1752         isl_map *map = *entry;
1753
1754         hash = isl_space_get_hash(map->dim);
1755         entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1756                                      hash, &has_dim, map->dim, 0);
1757         if (!entry2) {
1758                 int empty = isl_map_is_empty(map);
1759                 if (empty < 0)
1760                         return -1;
1761                 if (empty)
1762                         return 0;
1763                 data->is_subset = 0;
1764                 return -1;
1765         }
1766
1767         data->is_subset = isl_map_is_subset(map, entry2->data);
1768         if (data->is_subset < 0 || !data->is_subset)
1769                 return -1;
1770
1771         return 0;
1772 }
1773
1774 int isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
1775         __isl_keep isl_union_map *umap2)
1776 {
1777         struct isl_union_map_is_subset_data data = { NULL, 1 };
1778
1779         umap1 = isl_union_map_copy(umap1);
1780         umap2 = isl_union_map_copy(umap2);
1781         umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1782         umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1783
1784         if (!umap1 || !umap2)
1785                 goto error;
1786
1787         data.umap2 = umap2;
1788         if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1789                                    &is_subset_entry, &data) < 0 &&
1790             data.is_subset)
1791                 goto error;
1792
1793         isl_union_map_free(umap1);
1794         isl_union_map_free(umap2);
1795
1796         return data.is_subset;
1797 error:
1798         isl_union_map_free(umap1);
1799         isl_union_map_free(umap2);
1800         return -1;
1801 }
1802
1803 int isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
1804         __isl_keep isl_union_set *uset2)
1805 {
1806         return isl_union_map_is_subset(uset1, uset2);
1807 }
1808
1809 int isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
1810         __isl_keep isl_union_map *umap2)
1811 {
1812         int is_subset;
1813
1814         if (!umap1 || !umap2)
1815                 return -1;
1816         is_subset = isl_union_map_is_subset(umap1, umap2);
1817         if (is_subset != 1)
1818                 return is_subset;
1819         is_subset = isl_union_map_is_subset(umap2, umap1);
1820         return is_subset;
1821 }
1822
1823 int isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
1824         __isl_keep isl_union_set *uset2)
1825 {
1826         return isl_union_map_is_equal(uset1, uset2);
1827 }
1828
1829 int isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
1830         __isl_keep isl_union_map *umap2)
1831 {
1832         int is_subset;
1833
1834         if (!umap1 || !umap2)
1835                 return -1;
1836         is_subset = isl_union_map_is_subset(umap1, umap2);
1837         if (is_subset != 1)
1838                 return is_subset;
1839         is_subset = isl_union_map_is_subset(umap2, umap1);
1840         if (is_subset == -1)
1841                 return is_subset;
1842         return !is_subset;
1843 }
1844
1845 int isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
1846         __isl_keep isl_union_set *uset2)
1847 {
1848         return isl_union_map_is_strict_subset(uset1, uset2);
1849 }
1850
1851 static int sample_entry(void **entry, void *user)
1852 {
1853         isl_basic_map **sample = (isl_basic_map **)user;
1854         isl_map *map = *entry;
1855
1856         *sample = isl_map_sample(isl_map_copy(map));
1857         if (!*sample)
1858                 return -1;
1859         if (!isl_basic_map_plain_is_empty(*sample))
1860                 return -1;
1861         return 0;
1862 }
1863
1864 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
1865 {
1866         isl_basic_map *sample = NULL;
1867
1868         if (!umap)
1869                 return NULL;
1870
1871         if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1872                                    &sample_entry, &sample) < 0 &&
1873             !sample)
1874                 goto error;
1875
1876         if (!sample)
1877                 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
1878
1879         isl_union_map_free(umap);
1880
1881         return sample;
1882 error:
1883         isl_union_map_free(umap);
1884         return NULL;
1885 }
1886
1887 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
1888 {
1889         return (isl_basic_set *)isl_union_map_sample(uset);
1890 }
1891
1892 struct isl_forall_data {
1893         int res;
1894         int (*fn)(__isl_keep isl_map *map);
1895 };
1896
1897 static int forall_entry(void **entry, void *user)
1898 {
1899         struct isl_forall_data *data = user;
1900         isl_map *map = *entry;
1901
1902         data->res = data->fn(map);
1903         if (data->res < 0)
1904                 return -1;
1905
1906         if (!data->res)
1907                 return -1;
1908
1909         return 0;
1910 }
1911
1912 static int union_map_forall(__isl_keep isl_union_map *umap,
1913         int (*fn)(__isl_keep isl_map *map))
1914 {
1915         struct isl_forall_data data = { 1, fn };
1916
1917         if (!umap)
1918                 return -1;
1919
1920         if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1921                                    &forall_entry, &data) < 0 && data.res)
1922                 return -1;
1923
1924         return data.res;
1925 }
1926
1927 struct isl_forall_user_data {
1928         int res;
1929         int (*fn)(__isl_keep isl_map *map, void *user);
1930         void *user;
1931 };
1932
1933 static int forall_user_entry(void **entry, void *user)
1934 {
1935         struct isl_forall_user_data *data = user;
1936         isl_map *map = *entry;
1937
1938         data->res = data->fn(map, data->user);
1939         if (data->res < 0)
1940                 return -1;
1941
1942         if (!data->res)
1943                 return -1;
1944
1945         return 0;
1946 }
1947
1948 /* Check if fn(map, user) returns true for all maps "map" in umap.
1949  */
1950 static int union_map_forall_user(__isl_keep isl_union_map *umap,
1951         int (*fn)(__isl_keep isl_map *map, void *user), void *user)
1952 {
1953         struct isl_forall_user_data data = { 1, fn, user };
1954
1955         if (!umap)
1956                 return -1;
1957
1958         if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1959                                    &forall_user_entry, &data) < 0 && data.res)
1960                 return -1;
1961
1962         return data.res;
1963 }
1964
1965 int isl_union_map_is_empty(__isl_keep isl_union_map *umap)
1966 {
1967         return union_map_forall(umap, &isl_map_is_empty);
1968 }
1969
1970 int isl_union_set_is_empty(__isl_keep isl_union_set *uset)
1971 {
1972         return isl_union_map_is_empty(uset);
1973 }
1974
1975 static int is_subset_of_identity(__isl_keep isl_map *map)
1976 {
1977         int is_subset;
1978         isl_space *dim;
1979         isl_map *id;
1980
1981         if (!map)
1982                 return -1;
1983
1984         if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1985                 return 0;
1986
1987         dim = isl_map_get_space(map);
1988         id = isl_map_identity(dim);
1989
1990         is_subset = isl_map_is_subset(map, id);
1991
1992         isl_map_free(id);
1993
1994         return is_subset;
1995 }
1996
1997 /* Check if the given map is single-valued.
1998  * We simply compute
1999  *
2000  *      M \circ M^-1
2001  *
2002  * and check if the result is a subset of the identity mapping.
2003  */
2004 int isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2005 {
2006         isl_union_map *test;
2007         int sv;
2008
2009         if (isl_union_map_n_map(umap) == 1) {
2010                 isl_map *map;
2011                 umap = isl_union_map_copy(umap);
2012                 map = isl_map_from_union_map(umap);
2013                 sv = isl_map_is_single_valued(map);
2014                 isl_map_free(map);
2015                 return sv;
2016         }
2017
2018         test = isl_union_map_reverse(isl_union_map_copy(umap));
2019         test = isl_union_map_apply_range(test, isl_union_map_copy(umap));
2020
2021         sv = union_map_forall(test, &is_subset_of_identity);
2022
2023         isl_union_map_free(test);
2024
2025         return sv;
2026 }
2027
2028 int isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2029 {
2030         int in;
2031
2032         umap = isl_union_map_copy(umap);
2033         umap = isl_union_map_reverse(umap);
2034         in = isl_union_map_is_single_valued(umap);
2035         isl_union_map_free(umap);
2036
2037         return in;
2038 }
2039
2040 /* Represents a map that has a fixed value (v) for one of its
2041  * range dimensions.
2042  * The map in this structure is not reference counted, so it
2043  * is only valid while the isl_union_map from which it was
2044  * obtained is still alive.
2045  */
2046 struct isl_fixed_map {
2047         isl_int v;
2048         isl_map *map;
2049 };
2050
2051 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2052         int n)
2053 {
2054         int i;
2055         struct isl_fixed_map *v;
2056
2057         v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2058         if (!v)
2059                 return NULL;
2060         for (i = 0; i < n; ++i)
2061                 isl_int_init(v[i].v);
2062         return v;
2063 }
2064
2065 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2066 {
2067         int i;
2068
2069         if (!v)
2070                 return;
2071         for (i = 0; i < n; ++i)
2072                 isl_int_clear(v[i].v);
2073         free(v);
2074 }
2075
2076 /* Compare the "v" field of two isl_fixed_map structs.
2077  */
2078 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2079 {
2080         const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2081         const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2082
2083         return isl_int_cmp(e1->v, e2->v);
2084 }
2085
2086 /* Internal data structure used while checking whether all maps
2087  * in a union_map have a fixed value for a given output dimension.
2088  * v is the list of maps, with the fixed value for the dimension
2089  * n is the number of maps considered so far
2090  * pos is the output dimension under investigation
2091  */
2092 struct isl_fixed_dim_data {
2093         struct isl_fixed_map *v;
2094         int n;
2095         int pos;
2096 };
2097
2098 static int fixed_at_pos(__isl_keep isl_map *map, void *user)
2099 {
2100         struct isl_fixed_dim_data *data = user;
2101
2102         data->v[data->n].map = map;
2103         return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2104                                       &data->v[data->n++].v);
2105 }
2106
2107 static int plain_injective_on_range(__isl_take isl_union_map *umap,
2108         int first, int n_range);
2109
2110 /* Given a list of the maps, with their fixed values at output dimension "pos",
2111  * check whether the ranges of the maps form an obvious partition.
2112  *
2113  * We first sort the maps according to their fixed values.
2114  * If all maps have a different value, then we know the ranges form
2115  * a partition.
2116  * Otherwise, we collect the maps with the same fixed value and
2117  * check whether each such collection is obviously injective
2118  * based on later dimensions.
2119  */
2120 static int separates(struct isl_fixed_map *v, int n,
2121         __isl_take isl_space *dim, int pos, int n_range)
2122 {
2123         int i;
2124
2125         if (!v)
2126                 goto error;
2127
2128         qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2129
2130         for (i = 0; i + 1 < n; ++i) {
2131                 int j, k;
2132                 isl_union_map *part;
2133                 int injective;
2134
2135                 for (j = i + 1; j < n; ++j)
2136                         if (isl_int_ne(v[i].v, v[j].v))
2137                                 break;
2138
2139                 if (j == i + 1)
2140                         continue;
2141
2142                 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2143                 for (k = i; k < j; ++k)
2144                         part = isl_union_map_add_map(part,
2145                                                      isl_map_copy(v[k].map));
2146
2147                 injective = plain_injective_on_range(part, pos + 1, n_range);
2148                 if (injective < 0)
2149                         goto error;
2150                 if (!injective)
2151                         break;
2152
2153                 i = j - 1;
2154         }
2155
2156         isl_space_free(dim);
2157         free_isl_fixed_map_array(v, n);
2158         return i + 1 >= n;
2159 error:
2160         isl_space_free(dim);
2161         free_isl_fixed_map_array(v, n);
2162         return -1;
2163 }
2164
2165 /* Check whether the maps in umap have obviously distinct ranges.
2166  * In particular, check for an output dimension in the range
2167  * [first,n_range) for which all maps have a fixed value
2168  * and then check if these values, possibly along with fixed values
2169  * at later dimensions, entail distinct ranges.
2170  */
2171 static int plain_injective_on_range(__isl_take isl_union_map *umap,
2172         int first, int n_range)
2173 {
2174         isl_ctx *ctx;
2175         int n;
2176         struct isl_fixed_dim_data data = { NULL };
2177
2178         ctx = isl_union_map_get_ctx(umap);
2179
2180         if (!umap)
2181                 goto error;
2182
2183         n = isl_union_map_n_map(umap);
2184         if (n <= 1) {
2185                 isl_union_map_free(umap);
2186                 return 1;
2187         }
2188
2189         if (first >= n_range) {
2190                 isl_union_map_free(umap);
2191                 return 0;
2192         }
2193
2194         data.v = alloc_isl_fixed_map_array(ctx, n);
2195         if (!data.v)
2196                 goto error;
2197
2198         for (data.pos = first; data.pos < n_range; ++data.pos) {
2199                 int fixed;
2200                 int injective;
2201                 isl_space *dim;
2202
2203                 data.n = 0;
2204                 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2205                 if (fixed < 0)
2206                         goto error;
2207                 if (!fixed)
2208                         continue;
2209                 dim = isl_union_map_get_space(umap);
2210                 injective = separates(data.v, n, dim, data.pos, n_range);
2211                 isl_union_map_free(umap);
2212                 return injective;
2213         }
2214
2215         free_isl_fixed_map_array(data.v, n);
2216         isl_union_map_free(umap);
2217
2218         return 0;
2219 error:
2220         free_isl_fixed_map_array(data.v, n);
2221         isl_union_map_free(umap);
2222         return -1;
2223 }
2224
2225 /* Check whether the maps in umap that map to subsets of "ran"
2226  * have obviously distinct ranges.
2227  */
2228 static int plain_injective_on_range_wrap(__isl_keep isl_set *ran, void *user)
2229 {
2230         isl_union_map *umap = user;
2231
2232         umap = isl_union_map_copy(umap);
2233         umap = isl_union_map_intersect_range(umap,
2234                         isl_union_set_from_set(isl_set_copy(ran)));
2235         return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2236 }
2237
2238 /* Check if the given union_map is obviously injective.
2239  *
2240  * In particular, we first check if all individual maps are obviously
2241  * injective and then check if all the ranges of these maps are
2242  * obviously disjoint.
2243  */
2244 int isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2245 {
2246         int in;
2247         isl_union_map *univ;
2248         isl_union_set *ran;
2249
2250         in = union_map_forall(umap, &isl_map_plain_is_injective);
2251         if (in < 0)
2252                 return -1;
2253         if (!in)
2254                 return 0;
2255
2256         univ = isl_union_map_universe(isl_union_map_copy(umap));
2257         ran = isl_union_map_range(univ);
2258
2259         in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
2260
2261         isl_union_set_free(ran);
2262
2263         return in;
2264 }
2265
2266 int isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
2267 {
2268         int sv;
2269
2270         sv = isl_union_map_is_single_valued(umap);
2271         if (sv < 0 || !sv)
2272                 return sv;
2273
2274         return isl_union_map_is_injective(umap);
2275 }
2276
2277 static int zip_entry(void **entry, void *user)
2278 {
2279         isl_map *map = *entry;
2280         isl_union_map **res = user;
2281
2282         if (!isl_map_can_zip(map))
2283                 return 0;
2284
2285         *res = isl_union_map_add_map(*res, isl_map_zip(isl_map_copy(map)));
2286
2287         return 0;
2288 }
2289
2290 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
2291 {
2292         return cond_un_op(umap, &zip_entry);
2293 }
2294
2295 static int curry_entry(void **entry, void *user)
2296 {
2297         isl_map *map = *entry;
2298         isl_union_map **res = user;
2299
2300         if (!isl_map_can_curry(map))
2301                 return 0;
2302
2303         *res = isl_union_map_add_map(*res, isl_map_curry(isl_map_copy(map)));
2304
2305         return 0;
2306 }
2307
2308 /* Given a union map, take the maps of the form (A -> B) -> C and
2309  * return the union of the corresponding maps A -> (B -> C).
2310  */
2311 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
2312 {
2313         return cond_un_op(umap, &curry_entry);
2314 }
2315
2316 static int lift_entry(void **entry, void *user)
2317 {
2318         isl_set *set = *entry;
2319         isl_union_set **res = user;
2320
2321         *res = isl_union_set_add_set(*res, isl_set_lift(isl_set_copy(set)));
2322
2323         return 0;
2324 }
2325
2326 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
2327 {
2328         return cond_un_op(uset, &lift_entry);
2329 }
2330
2331 static int coefficients_entry(void **entry, void *user)
2332 {
2333         isl_set *set = *entry;
2334         isl_union_set **res = user;
2335
2336         set = isl_set_copy(set);
2337         set = isl_set_from_basic_set(isl_set_coefficients(set));
2338         *res = isl_union_set_add_set(*res, set);
2339
2340         return 0;
2341 }
2342
2343 __isl_give isl_union_set *isl_union_set_coefficients(
2344         __isl_take isl_union_set *uset)
2345 {
2346         isl_ctx *ctx;
2347         isl_space *dim;
2348         isl_union_set *res;
2349
2350         if (!uset)
2351                 return NULL;
2352
2353         ctx = isl_union_set_get_ctx(uset);
2354         dim = isl_space_set_alloc(ctx, 0, 0);
2355         res = isl_union_map_alloc(dim, uset->table.n);
2356         if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2357                                    &coefficients_entry, &res) < 0)
2358                 goto error;
2359
2360         isl_union_set_free(uset);
2361         return res;
2362 error:
2363         isl_union_set_free(uset);
2364         isl_union_set_free(res);
2365         return NULL;
2366 }
2367
2368 static int solutions_entry(void **entry, void *user)
2369 {
2370         isl_set *set = *entry;
2371         isl_union_set **res = user;
2372
2373         set = isl_set_copy(set);
2374         set = isl_set_from_basic_set(isl_set_solutions(set));
2375         if (!*res)
2376                 *res = isl_union_set_from_set(set);
2377         else
2378                 *res = isl_union_set_add_set(*res, set);
2379
2380         if (!*res)
2381                 return -1;
2382
2383         return 0;
2384 }
2385
2386 __isl_give isl_union_set *isl_union_set_solutions(
2387         __isl_take isl_union_set *uset)
2388 {
2389         isl_union_set *res = NULL;
2390
2391         if (!uset)
2392                 return NULL;
2393
2394         if (uset->table.n == 0) {
2395                 res = isl_union_set_empty(isl_union_set_get_space(uset));
2396                 isl_union_set_free(uset);
2397                 return res;
2398         }
2399
2400         if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2401                                    &solutions_entry, &res) < 0)
2402                 goto error;
2403
2404         isl_union_set_free(uset);
2405         return res;
2406 error:
2407         isl_union_set_free(uset);
2408         isl_union_set_free(res);
2409         return NULL;
2410 }