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