add isl_union_map_intersect_params
[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 struct isl_union_map_match_bin_data {
643         isl_union_map *umap2;
644         isl_union_map *res;
645         __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
646 };
647
648 static int match_bin_entry(void **entry, void *user)
649 {
650         struct isl_union_map_match_bin_data *data = user;
651         uint32_t hash;
652         struct isl_hash_table_entry *entry2;
653         isl_map *map = *entry;
654         int empty;
655
656         hash = isl_space_get_hash(map->dim);
657         entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
658                                      hash, &has_dim, map->dim, 0);
659         if (!entry2)
660                 return 0;
661
662         map = isl_map_copy(map);
663         map = data->fn(map, isl_map_copy(entry2->data));
664
665         empty = isl_map_is_empty(map);
666         if (empty < 0) {
667                 isl_map_free(map);
668                 return -1;
669         }
670         if (empty) {
671                 isl_map_free(map);
672                 return 0;
673         }
674
675         data->res = isl_union_map_add_map(data->res, map);
676
677         return 0;
678 }
679
680 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
681         __isl_take isl_union_map *umap2,
682         __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
683 {
684         struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
685
686         umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
687         umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
688
689         if (!umap1 || !umap2)
690                 goto error;
691
692         data.umap2 = umap2;
693         data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
694                                        umap1->table.n);
695         if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
696                                    &match_bin_entry, &data) < 0)
697                 goto error;
698
699         isl_union_map_free(umap1);
700         isl_union_map_free(umap2);
701         return data.res;
702 error:
703         isl_union_map_free(umap1);
704         isl_union_map_free(umap2);
705         isl_union_map_free(data.res);
706         return NULL;
707 }
708
709 __isl_give isl_union_map *isl_union_map_intersect(
710         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
711 {
712         return match_bin_op(umap1, umap2, &isl_map_intersect);
713 }
714
715 __isl_give isl_union_set *isl_union_set_intersect(
716         __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
717 {
718         return isl_union_map_intersect(uset1, uset2);
719 }
720
721 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
722         __isl_take isl_union_map *context)
723 {
724         return match_bin_op(umap, context, &isl_map_gist);
725 }
726
727 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
728         __isl_take isl_union_set *context)
729 {
730         return isl_union_map_gist(uset, context);
731 }
732
733 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
734         __isl_take isl_map *set2)
735 {
736         return isl_set_lex_le_set((isl_set *)set1, (isl_set *)set2);
737 }
738
739 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
740         __isl_take isl_map *set2)
741 {
742         return isl_set_lex_lt_set((isl_set *)set1, (isl_set *)set2);
743 }
744
745 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
746         __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
747 {
748         return match_bin_op(uset1, uset2, &lex_lt_set);
749 }
750
751 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
752         __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
753 {
754         return match_bin_op(uset1, uset2, &lex_le_set);
755 }
756
757 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
758         __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
759 {
760         return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
761 }
762
763 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
764         __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
765 {
766         return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
767 }
768
769 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
770         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
771 {
772         return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
773 }
774
775 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
776         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
777 {
778         return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
779 }
780
781 static int intersect_domain_entry(void **entry, void *user)
782 {
783         struct isl_union_map_gen_bin_data *data = user;
784         uint32_t hash;
785         struct isl_hash_table_entry *entry2;
786         isl_space *dim;
787         isl_map *map = *entry;
788         int empty;
789
790         dim = isl_map_get_space(map);
791         dim = isl_space_domain(dim);
792         hash = isl_space_get_hash(dim);
793         entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
794                                      hash, &has_dim, dim, 0);
795         isl_space_free(dim);
796         if (!entry2)
797                 return 0;
798
799         map = isl_map_copy(map);
800         map = isl_map_intersect_domain(map, isl_set_copy(entry2->data));
801
802         empty = isl_map_is_empty(map);
803         if (empty < 0) {
804                 isl_map_free(map);
805                 return -1;
806         }
807         if (empty) {
808                 isl_map_free(map);
809                 return 0;
810         }
811
812         data->res = isl_union_map_add_map(data->res, map);
813
814         return 0;
815 }
816
817 __isl_give isl_union_map *isl_union_map_intersect_domain(
818         __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
819 {
820         return gen_bin_op(umap, uset, &intersect_domain_entry);
821 }
822
823 static int intersect_range_entry(void **entry, void *user)
824 {
825         struct isl_union_map_gen_bin_data *data = user;
826         uint32_t hash;
827         struct isl_hash_table_entry *entry2;
828         isl_space *dim;
829         isl_map *map = *entry;
830         int empty;
831
832         dim = isl_map_get_space(map);
833         dim = isl_space_range(dim);
834         hash = isl_space_get_hash(dim);
835         entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
836                                      hash, &has_dim, dim, 0);
837         isl_space_free(dim);
838         if (!entry2)
839                 return 0;
840
841         map = isl_map_copy(map);
842         map = isl_map_intersect_range(map, isl_set_copy(entry2->data));
843
844         empty = isl_map_is_empty(map);
845         if (empty < 0) {
846                 isl_map_free(map);
847                 return -1;
848         }
849         if (empty) {
850                 isl_map_free(map);
851                 return 0;
852         }
853
854         data->res = isl_union_map_add_map(data->res, map);
855
856         return 0;
857 }
858
859 __isl_give isl_union_map *isl_union_map_intersect_range(
860         __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
861 {
862         return gen_bin_op(umap, uset, &intersect_range_entry);
863 }
864
865 struct isl_union_map_bin_data {
866         isl_union_map *umap2;
867         isl_union_map *res;
868         isl_map *map;
869         int (*fn)(void **entry, void *user);
870 };
871
872 static int apply_range_entry(void **entry, void *user)
873 {
874         struct isl_union_map_bin_data *data = user;
875         isl_map *map2 = *entry;
876         int empty;
877
878         if (!isl_space_tuple_match(data->map->dim, isl_dim_out,
879                                  map2->dim, isl_dim_in))
880                 return 0;
881
882         map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
883
884         empty = isl_map_is_empty(map2);
885         if (empty < 0) {
886                 isl_map_free(map2);
887                 return -1;
888         }
889         if (empty) {
890                 isl_map_free(map2);
891                 return 0;
892         }
893
894         data->res = isl_union_map_add_map(data->res, map2);
895
896         return 0;
897 }
898
899 static int bin_entry(void **entry, void *user)
900 {
901         struct isl_union_map_bin_data *data = user;
902         isl_map *map = *entry;
903
904         data->map = map;
905         if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
906                                    data->fn, data) < 0)
907                 return -1;
908
909         return 0;
910 }
911
912 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
913         __isl_take isl_union_map *umap2, int (*fn)(void **entry, void *user))
914 {
915         struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
916
917         umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
918         umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
919
920         if (!umap1 || !umap2)
921                 goto error;
922
923         data.umap2 = umap2;
924         data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
925                                        umap1->table.n);
926         if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
927                                    &bin_entry, &data) < 0)
928                 goto error;
929
930         isl_union_map_free(umap1);
931         isl_union_map_free(umap2);
932         return data.res;
933 error:
934         isl_union_map_free(umap1);
935         isl_union_map_free(umap2);
936         isl_union_map_free(data.res);
937         return NULL;
938 }
939
940 __isl_give isl_union_map *isl_union_map_apply_range(
941         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
942 {
943         return bin_op(umap1, umap2, &apply_range_entry);
944 }
945
946 __isl_give isl_union_map *isl_union_map_apply_domain(
947         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
948 {
949         umap1 = isl_union_map_reverse(umap1);
950         umap1 = isl_union_map_apply_range(umap1, umap2);
951         return isl_union_map_reverse(umap1);
952 }
953
954 __isl_give isl_union_set *isl_union_set_apply(
955         __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
956 {
957         return isl_union_map_apply_range(uset, umap);
958 }
959
960 static int map_lex_lt_entry(void **entry, void *user)
961 {
962         struct isl_union_map_bin_data *data = user;
963         isl_map *map2 = *entry;
964
965         if (!isl_space_tuple_match(data->map->dim, isl_dim_out,
966                                  map2->dim, isl_dim_out))
967                 return 0;
968
969         map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
970
971         data->res = isl_union_map_add_map(data->res, map2);
972
973         return 0;
974 }
975
976 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
977         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
978 {
979         return bin_op(umap1, umap2, &map_lex_lt_entry);
980 }
981
982 static int map_lex_le_entry(void **entry, void *user)
983 {
984         struct isl_union_map_bin_data *data = user;
985         isl_map *map2 = *entry;
986
987         if (!isl_space_tuple_match(data->map->dim, isl_dim_out,
988                                  map2->dim, isl_dim_out))
989                 return 0;
990
991         map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
992
993         data->res = isl_union_map_add_map(data->res, map2);
994
995         return 0;
996 }
997
998 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
999         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1000 {
1001         return bin_op(umap1, umap2, &map_lex_le_entry);
1002 }
1003
1004 static int product_entry(void **entry, void *user)
1005 {
1006         struct isl_union_map_bin_data *data = user;
1007         isl_map *map2 = *entry;
1008
1009         map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1010
1011         data->res = isl_union_map_add_map(data->res, map2);
1012
1013         return 0;
1014 }
1015
1016 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1017         __isl_take isl_union_map *umap2)
1018 {
1019         return bin_op(umap1, umap2, &product_entry);
1020 }
1021
1022 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1023         __isl_take isl_union_set *uset2)
1024 {
1025         return isl_union_map_product(uset1, uset2);
1026 }
1027
1028 static int range_product_entry(void **entry, void *user)
1029 {
1030         struct isl_union_map_bin_data *data = user;
1031         isl_map *map2 = *entry;
1032
1033         if (!isl_space_tuple_match(data->map->dim, isl_dim_in,
1034                                  map2->dim, isl_dim_in))
1035                 return 0;
1036
1037         map2 = isl_map_range_product(isl_map_copy(data->map),
1038                                      isl_map_copy(map2));
1039
1040         data->res = isl_union_map_add_map(data->res, map2);
1041
1042         return 0;
1043 }
1044
1045 __isl_give isl_union_map *isl_union_map_range_product(
1046         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1047 {
1048         return bin_op(umap1, umap2, &range_product_entry);
1049 }
1050
1051 static int flat_range_product_entry(void **entry, void *user)
1052 {
1053         struct isl_union_map_bin_data *data = user;
1054         isl_map *map2 = *entry;
1055
1056         if (!isl_space_tuple_match(data->map->dim, isl_dim_in,
1057                                  map2->dim, isl_dim_in))
1058                 return 0;
1059
1060         map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1061                                           isl_map_copy(map2));
1062
1063         data->res = isl_union_map_add_map(data->res, map2);
1064
1065         return 0;
1066 }
1067
1068 __isl_give isl_union_map *isl_union_map_flat_range_product(
1069         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1070 {
1071         return bin_op(umap1, umap2, &flat_range_product_entry);
1072 }
1073
1074 static __isl_give isl_union_set *cond_un_op(__isl_take isl_union_map *umap,
1075         int (*fn)(void **, void *))
1076 {
1077         isl_union_set *res;
1078
1079         if (!umap)
1080                 return NULL;
1081
1082         res = isl_union_map_alloc(isl_space_copy(umap->dim), umap->table.n);
1083         if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, &res) < 0)
1084                 goto error;
1085
1086         isl_union_map_free(umap);
1087         return res;
1088 error:
1089         isl_union_map_free(umap);
1090         isl_union_set_free(res);
1091         return NULL;
1092 }
1093
1094 static int from_range_entry(void **entry, void *user)
1095 {
1096         isl_map *set = *entry;
1097         isl_union_set **res = user;
1098
1099         *res = isl_union_map_add_map(*res,
1100                                         isl_map_from_range(isl_set_copy(set)));
1101
1102         return 0;
1103 }
1104
1105 __isl_give isl_union_map *isl_union_map_from_range(
1106         __isl_take isl_union_set *uset)
1107 {
1108         return cond_un_op(uset, &from_range_entry);
1109 }
1110
1111 __isl_give isl_union_map *isl_union_map_from_domain(
1112         __isl_take isl_union_set *uset)
1113 {
1114         return isl_union_map_reverse(isl_union_map_from_range(uset));
1115 }
1116
1117 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1118         __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1119 {
1120         return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1121                                          isl_union_map_from_range(range));
1122 }
1123
1124 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1125         int (*fn)(void **, void *))
1126 {
1127         umap = isl_union_map_cow(umap);
1128         if (!umap)
1129                 return NULL;
1130
1131         if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, NULL) < 0)
1132                 goto error;
1133
1134         return umap;
1135 error:
1136         isl_union_map_free(umap);
1137         return NULL;
1138 }
1139
1140 static int affine_entry(void **entry, void *user)
1141 {
1142         isl_map **map = (isl_map **)entry;
1143
1144         *map = isl_map_from_basic_map(isl_map_affine_hull(*map));
1145
1146         return *map ? 0 : -1;
1147 }
1148
1149 __isl_give isl_union_map *isl_union_map_affine_hull(
1150         __isl_take isl_union_map *umap)
1151 {
1152         return un_op(umap, &affine_entry);
1153 }
1154
1155 __isl_give isl_union_set *isl_union_set_affine_hull(
1156         __isl_take isl_union_set *uset)
1157 {
1158         return isl_union_map_affine_hull(uset);
1159 }
1160
1161 static int polyhedral_entry(void **entry, void *user)
1162 {
1163         isl_map **map = (isl_map **)entry;
1164
1165         *map = isl_map_from_basic_map(isl_map_polyhedral_hull(*map));
1166
1167         return *map ? 0 : -1;
1168 }
1169
1170 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1171         __isl_take isl_union_map *umap)
1172 {
1173         return un_op(umap, &polyhedral_entry);
1174 }
1175
1176 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1177         __isl_take isl_union_set *uset)
1178 {
1179         return isl_union_map_polyhedral_hull(uset);
1180 }
1181
1182 static int simple_entry(void **entry, void *user)
1183 {
1184         isl_map **map = (isl_map **)entry;
1185
1186         *map = isl_map_from_basic_map(isl_map_simple_hull(*map));
1187
1188         return *map ? 0 : -1;
1189 }
1190
1191 __isl_give isl_union_map *isl_union_map_simple_hull(
1192         __isl_take isl_union_map *umap)
1193 {
1194         return un_op(umap, &simple_entry);
1195 }
1196
1197 __isl_give isl_union_set *isl_union_set_simple_hull(
1198         __isl_take isl_union_set *uset)
1199 {
1200         return isl_union_map_simple_hull(uset);
1201 }
1202
1203 static int inplace_entry(void **entry, void *user)
1204 {
1205         __isl_give isl_map *(*fn)(__isl_take isl_map *);
1206         isl_map **map = (isl_map **)entry;
1207         isl_map *copy;
1208
1209         fn = *(__isl_give isl_map *(**)(__isl_take isl_map *)) user;
1210         copy = fn(isl_map_copy(*map));
1211         if (!copy)
1212                 return -1;
1213
1214         isl_map_free(*map);
1215         *map = copy;
1216
1217         return 0;
1218 }
1219
1220 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1221         __isl_give isl_map *(*fn)(__isl_take isl_map *))
1222 {
1223         if (!umap)
1224                 return NULL;
1225
1226         if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1227                                     &inplace_entry, &fn) < 0)
1228                 goto error;
1229
1230         return umap;
1231 error:
1232         isl_union_map_free(umap);
1233         return NULL;
1234 }
1235
1236 __isl_give isl_union_map *isl_union_map_coalesce(
1237         __isl_take isl_union_map *umap)
1238 {
1239         return inplace(umap, &isl_map_coalesce);
1240 }
1241
1242 __isl_give isl_union_set *isl_union_set_coalesce(
1243         __isl_take isl_union_set *uset)
1244 {
1245         return isl_union_map_coalesce(uset);
1246 }
1247
1248 __isl_give isl_union_map *isl_union_map_detect_equalities(
1249         __isl_take isl_union_map *umap)
1250 {
1251         return inplace(umap, &isl_map_detect_equalities);
1252 }
1253
1254 __isl_give isl_union_set *isl_union_set_detect_equalities(
1255         __isl_take isl_union_set *uset)
1256 {
1257         return isl_union_map_detect_equalities(uset);
1258 }
1259
1260 __isl_give isl_union_map *isl_union_map_compute_divs(
1261         __isl_take isl_union_map *umap)
1262 {
1263         return inplace(umap, &isl_map_compute_divs);
1264 }
1265
1266 __isl_give isl_union_set *isl_union_set_compute_divs(
1267         __isl_take isl_union_set *uset)
1268 {
1269         return isl_union_map_compute_divs(uset);
1270 }
1271
1272 static int lexmin_entry(void **entry, void *user)
1273 {
1274         isl_map **map = (isl_map **)entry;
1275
1276         *map = isl_map_lexmin(*map);
1277
1278         return *map ? 0 : -1;
1279 }
1280
1281 __isl_give isl_union_map *isl_union_map_lexmin(
1282         __isl_take isl_union_map *umap)
1283 {
1284         return un_op(umap, &lexmin_entry);
1285 }
1286
1287 __isl_give isl_union_set *isl_union_set_lexmin(
1288         __isl_take isl_union_set *uset)
1289 {
1290         return isl_union_map_lexmin(uset);
1291 }
1292
1293 static int lexmax_entry(void **entry, void *user)
1294 {
1295         isl_map **map = (isl_map **)entry;
1296
1297         *map = isl_map_lexmax(*map);
1298
1299         return *map ? 0 : -1;
1300 }
1301
1302 __isl_give isl_union_map *isl_union_map_lexmax(
1303         __isl_take isl_union_map *umap)
1304 {
1305         return un_op(umap, &lexmax_entry);
1306 }
1307
1308 __isl_give isl_union_set *isl_union_set_lexmax(
1309         __isl_take isl_union_set *uset)
1310 {
1311         return isl_union_map_lexmax(uset);
1312 }
1313
1314 static int universe_entry(void **entry, void *user)
1315 {
1316         isl_map *map = *entry;
1317         isl_union_map **res = user;
1318
1319         map = isl_map_universe(isl_map_get_space(map));
1320         *res = isl_union_map_add_map(*res, map);
1321
1322         return 0;
1323 }
1324
1325 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1326 {
1327         return cond_un_op(umap, &universe_entry);
1328 }
1329
1330 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1331 {
1332         return isl_union_map_universe(uset);
1333 }
1334
1335 static int reverse_entry(void **entry, void *user)
1336 {
1337         isl_map *map = *entry;
1338         isl_union_map **res = user;
1339
1340         *res = isl_union_map_add_map(*res, isl_map_reverse(isl_map_copy(map)));
1341
1342         return 0;
1343 }
1344
1345 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1346 {
1347         return cond_un_op(umap, &reverse_entry);
1348 }
1349
1350 static int domain_entry(void **entry, void *user)
1351 {
1352         isl_map *map = *entry;
1353         isl_union_set **res = user;
1354
1355         *res = isl_union_set_add_set(*res, isl_map_domain(isl_map_copy(map)));
1356
1357         return 0;
1358 }
1359
1360 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1361 {
1362         return cond_un_op(umap, &domain_entry);
1363 }
1364
1365 static int range_entry(void **entry, void *user)
1366 {
1367         isl_map *map = *entry;
1368         isl_union_set **res = user;
1369
1370         *res = isl_union_set_add_set(*res, isl_map_range(isl_map_copy(map)));
1371
1372         return 0;
1373 }
1374
1375 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1376 {
1377         return cond_un_op(umap, &range_entry);
1378 }
1379
1380 static int domain_map_entry(void **entry, void *user)
1381 {
1382         isl_map *map = *entry;
1383         isl_union_set **res = user;
1384
1385         *res = isl_union_map_add_map(*res,
1386                                         isl_map_domain_map(isl_map_copy(map)));
1387
1388         return 0;
1389 }
1390
1391 __isl_give isl_union_map *isl_union_map_domain_map(
1392         __isl_take isl_union_map *umap)
1393 {
1394         return cond_un_op(umap, &domain_map_entry);
1395 }
1396
1397 static int range_map_entry(void **entry, void *user)
1398 {
1399         isl_map *map = *entry;
1400         isl_union_set **res = user;
1401
1402         *res = isl_union_map_add_map(*res,
1403                                         isl_map_range_map(isl_map_copy(map)));
1404
1405         return 0;
1406 }
1407
1408 __isl_give isl_union_map *isl_union_map_range_map(
1409         __isl_take isl_union_map *umap)
1410 {
1411         return cond_un_op(umap, &range_map_entry);
1412 }
1413
1414 static int deltas_entry(void **entry, void *user)
1415 {
1416         isl_map *map = *entry;
1417         isl_union_set **res = user;
1418
1419         if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1420                 return 0;
1421
1422         *res = isl_union_set_add_set(*res, isl_map_deltas(isl_map_copy(map)));
1423
1424         return 0;
1425 }
1426
1427 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
1428 {
1429         return cond_un_op(umap, &deltas_entry);
1430 }
1431
1432 static int deltas_map_entry(void **entry, void *user)
1433 {
1434         isl_map *map = *entry;
1435         isl_union_map **res = user;
1436
1437         if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1438                 return 0;
1439
1440         *res = isl_union_map_add_map(*res,
1441                                      isl_map_deltas_map(isl_map_copy(map)));
1442
1443         return 0;
1444 }
1445
1446 __isl_give isl_union_map *isl_union_map_deltas_map(
1447         __isl_take isl_union_map *umap)
1448 {
1449         return cond_un_op(umap, &deltas_map_entry);
1450 }
1451
1452 static int identity_entry(void **entry, void *user)
1453 {
1454         isl_set *set = *entry;
1455         isl_union_map **res = user;
1456
1457         *res = isl_union_map_add_map(*res, isl_set_identity(isl_set_copy(set)));
1458
1459         return 0;
1460 }
1461
1462 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
1463 {
1464         return cond_un_op(uset, &identity_entry);
1465 }
1466
1467 static int unwrap_entry(void **entry, void *user)
1468 {
1469         isl_set *set = *entry;
1470         isl_union_set **res = user;
1471
1472         if (!isl_set_is_wrapping(set))
1473                 return 0;
1474
1475         *res = isl_union_map_add_map(*res, isl_set_unwrap(isl_set_copy(set)));
1476
1477         return 0;
1478 }
1479
1480 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
1481 {
1482         return cond_un_op(uset, &unwrap_entry);
1483 }
1484
1485 static int wrap_entry(void **entry, void *user)
1486 {
1487         isl_map *map = *entry;
1488         isl_union_set **res = user;
1489
1490         *res = isl_union_set_add_set(*res, isl_map_wrap(isl_map_copy(map)));
1491
1492         return 0;
1493 }
1494
1495 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
1496 {
1497         return cond_un_op(umap, &wrap_entry);
1498 }
1499
1500 struct isl_union_map_is_subset_data {
1501         isl_union_map *umap2;
1502         int is_subset;
1503 };
1504
1505 static int is_subset_entry(void **entry, void *user)
1506 {
1507         struct isl_union_map_is_subset_data *data = user;
1508         uint32_t hash;
1509         struct isl_hash_table_entry *entry2;
1510         isl_map *map = *entry;
1511
1512         hash = isl_space_get_hash(map->dim);
1513         entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1514                                      hash, &has_dim, map->dim, 0);
1515         if (!entry2) {
1516                 data->is_subset = 0;
1517                 return -1;
1518         }
1519
1520         data->is_subset = isl_map_is_subset(map, entry2->data);
1521         if (data->is_subset < 0 || !data->is_subset)
1522                 return -1;
1523
1524         return 0;
1525 }
1526
1527 int isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
1528         __isl_keep isl_union_map *umap2)
1529 {
1530         struct isl_union_map_is_subset_data data = { NULL, 1 };
1531
1532         umap1 = isl_union_map_copy(umap1);
1533         umap2 = isl_union_map_copy(umap2);
1534         umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1535         umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1536
1537         if (!umap1 || !umap2)
1538                 goto error;
1539
1540         data.umap2 = umap2;
1541         if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1542                                    &is_subset_entry, &data) < 0 &&
1543             data.is_subset)
1544                 goto error;
1545
1546         isl_union_map_free(umap1);
1547         isl_union_map_free(umap2);
1548
1549         return data.is_subset;
1550 error:
1551         isl_union_map_free(umap1);
1552         isl_union_map_free(umap2);
1553         return -1;
1554 }
1555
1556 int isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
1557         __isl_keep isl_union_set *uset2)
1558 {
1559         return isl_union_map_is_subset(uset1, uset2);
1560 }
1561
1562 int isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
1563         __isl_keep isl_union_map *umap2)
1564 {
1565         int is_subset;
1566
1567         if (!umap1 || !umap2)
1568                 return -1;
1569         is_subset = isl_union_map_is_subset(umap1, umap2);
1570         if (is_subset != 1)
1571                 return is_subset;
1572         is_subset = isl_union_map_is_subset(umap2, umap1);
1573         return is_subset;
1574 }
1575
1576 int isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
1577         __isl_keep isl_union_set *uset2)
1578 {
1579         return isl_union_map_is_equal(uset1, uset2);
1580 }
1581
1582 int isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
1583         __isl_keep isl_union_map *umap2)
1584 {
1585         int is_subset;
1586
1587         if (!umap1 || !umap2)
1588                 return -1;
1589         is_subset = isl_union_map_is_subset(umap1, umap2);
1590         if (is_subset != 1)
1591                 return is_subset;
1592         is_subset = isl_union_map_is_subset(umap2, umap1);
1593         if (is_subset == -1)
1594                 return is_subset;
1595         return !is_subset;
1596 }
1597
1598 int isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
1599         __isl_keep isl_union_set *uset2)
1600 {
1601         return isl_union_map_is_strict_subset(uset1, uset2);
1602 }
1603
1604 static int sample_entry(void **entry, void *user)
1605 {
1606         isl_basic_map **sample = (isl_basic_map **)user;
1607         isl_map *map = *entry;
1608
1609         *sample = isl_map_sample(isl_map_copy(map));
1610         if (!*sample)
1611                 return -1;
1612         if (!isl_basic_map_plain_is_empty(*sample))
1613                 return -1;
1614         return 0;
1615 }
1616
1617 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
1618 {
1619         isl_basic_map *sample = NULL;
1620
1621         if (!umap)
1622                 return NULL;
1623
1624         if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1625                                    &sample_entry, &sample) < 0 &&
1626             !sample)
1627                 goto error;
1628
1629         if (!sample)
1630                 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
1631
1632         isl_union_map_free(umap);
1633
1634         return sample;
1635 error:
1636         isl_union_map_free(umap);
1637         return NULL;
1638 }
1639
1640 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
1641 {
1642         return (isl_basic_set *)isl_union_map_sample(uset);
1643 }
1644
1645 struct isl_forall_data {
1646         int res;
1647         int (*fn)(__isl_keep isl_map *map);
1648 };
1649
1650 static int forall_entry(void **entry, void *user)
1651 {
1652         struct isl_forall_data *data = user;
1653         isl_map *map = *entry;
1654
1655         data->res = data->fn(map);
1656         if (data->res < 0)
1657                 return -1;
1658
1659         if (!data->res)
1660                 return -1;
1661
1662         return 0;
1663 }
1664
1665 static int union_map_forall(__isl_keep isl_union_map *umap,
1666         int (*fn)(__isl_keep isl_map *map))
1667 {
1668         struct isl_forall_data data = { 1, fn };
1669
1670         if (!umap)
1671                 return -1;
1672
1673         if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1674                                    &forall_entry, &data) < 0 && data.res)
1675                 return -1;
1676
1677         return data.res;
1678 }
1679
1680 struct isl_forall_user_data {
1681         int res;
1682         int (*fn)(__isl_keep isl_map *map, void *user);
1683         void *user;
1684 };
1685
1686 static int forall_user_entry(void **entry, void *user)
1687 {
1688         struct isl_forall_user_data *data = user;
1689         isl_map *map = *entry;
1690
1691         data->res = data->fn(map, data->user);
1692         if (data->res < 0)
1693                 return -1;
1694
1695         if (!data->res)
1696                 return -1;
1697
1698         return 0;
1699 }
1700
1701 /* Check if fn(map, user) returns true for all maps "map" in umap.
1702  */
1703 static int union_map_forall_user(__isl_keep isl_union_map *umap,
1704         int (*fn)(__isl_keep isl_map *map, void *user), void *user)
1705 {
1706         struct isl_forall_user_data data = { 1, fn, user };
1707
1708         if (!umap)
1709                 return -1;
1710
1711         if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1712                                    &forall_user_entry, &data) < 0 && data.res)
1713                 return -1;
1714
1715         return data.res;
1716 }
1717
1718 int isl_union_map_is_empty(__isl_keep isl_union_map *umap)
1719 {
1720         return union_map_forall(umap, &isl_map_is_empty);
1721 }
1722
1723 int isl_union_set_is_empty(__isl_keep isl_union_set *uset)
1724 {
1725         return isl_union_map_is_empty(uset);
1726 }
1727
1728 static int is_subset_of_identity(__isl_keep isl_map *map)
1729 {
1730         int is_subset;
1731         isl_space *dim;
1732         isl_map *id;
1733
1734         if (!map)
1735                 return -1;
1736
1737         if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1738                 return 0;
1739
1740         dim = isl_map_get_space(map);
1741         id = isl_map_identity(dim);
1742
1743         is_subset = isl_map_is_subset(map, id);
1744
1745         isl_map_free(id);
1746
1747         return is_subset;
1748 }
1749
1750 /* Check if the given map is single-valued.
1751  * We simply compute
1752  *
1753  *      M \circ M^-1
1754  *
1755  * and check if the result is a subset of the identity mapping.
1756  */
1757 int isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
1758 {
1759         isl_union_map *test;
1760         int sv;
1761
1762         if (isl_union_map_n_map(umap) == 1) {
1763                 isl_map *map;
1764                 umap = isl_union_map_copy(umap);
1765                 map = isl_map_from_union_map(umap);
1766                 sv = isl_map_is_single_valued(map);
1767                 isl_map_free(map);
1768                 return sv;
1769         }
1770
1771         test = isl_union_map_reverse(isl_union_map_copy(umap));
1772         test = isl_union_map_apply_range(test, isl_union_map_copy(umap));
1773
1774         sv = union_map_forall(test, &is_subset_of_identity);
1775
1776         isl_union_map_free(test);
1777
1778         return sv;
1779 }
1780
1781 int isl_union_map_is_injective(__isl_keep isl_union_map *umap)
1782 {
1783         int in;
1784
1785         umap = isl_union_map_copy(umap);
1786         umap = isl_union_map_reverse(umap);
1787         in = isl_union_map_is_single_valued(umap);
1788         isl_union_map_free(umap);
1789
1790         return in;
1791 }
1792
1793 /* Represents a map that has a fixed value (v) for one of its
1794  * range dimensions.
1795  * The map in this structure is not reference counted, so it
1796  * is only valid while the isl_union_map from which it was
1797  * obtained is still alive.
1798  */
1799 struct isl_fixed_map {
1800         isl_int v;
1801         isl_map *map;
1802 };
1803
1804 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
1805         int n)
1806 {
1807         int i;
1808         struct isl_fixed_map *v;
1809
1810         v = isl_calloc_array(ctx, struct isl_fixed_map, n);
1811         if (!v)
1812                 return NULL;
1813         for (i = 0; i < n; ++i)
1814                 isl_int_init(v[i].v);
1815         return v;
1816 }
1817
1818 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
1819 {
1820         int i;
1821
1822         if (!v)
1823                 return;
1824         for (i = 0; i < n; ++i)
1825                 isl_int_clear(v[i].v);
1826         free(v);
1827 }
1828
1829 /* Compare the "v" field of two isl_fixed_map structs.
1830  */
1831 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
1832 {
1833         const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
1834         const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
1835
1836         return isl_int_cmp(e1->v, e2->v);
1837 }
1838
1839 /* Internal data structure used while checking whether all maps
1840  * in a union_map have a fixed value for a given output dimension.
1841  * v is the list of maps, with the fixed value for the dimension
1842  * n is the number of maps considered so far
1843  * pos is the output dimension under investigation
1844  */
1845 struct isl_fixed_dim_data {
1846         struct isl_fixed_map *v;
1847         int n;
1848         int pos;
1849 };
1850
1851 static int fixed_at_pos(__isl_keep isl_map *map, void *user)
1852 {
1853         struct isl_fixed_dim_data *data = user;
1854
1855         data->v[data->n].map = map;
1856         return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
1857                                       &data->v[data->n++].v);
1858 }
1859
1860 static int plain_injective_on_range(__isl_take isl_union_map *umap,
1861         int first, int n_range);
1862
1863 /* Given a list of the maps, with their fixed values at output dimension "pos",
1864  * check whether the ranges of the maps form an obvious partition.
1865  *
1866  * We first sort the maps according to their fixed values.
1867  * If all maps have a different value, then we know the ranges form
1868  * a partition.
1869  * Otherwise, we collect the maps with the same fixed value and
1870  * check whether each such collection is obviously injective
1871  * based on later dimensions.
1872  */
1873 static int separates(struct isl_fixed_map *v, int n,
1874         __isl_take isl_space *dim, int pos, int n_range)
1875 {
1876         int i;
1877
1878         if (!v)
1879                 goto error;
1880
1881         qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
1882
1883         for (i = 0; i + 1 < n; ++i) {
1884                 int j, k;
1885                 isl_union_map *part;
1886                 int injective;
1887
1888                 for (j = i + 1; j < n; ++j)
1889                         if (isl_int_ne(v[i].v, v[j].v))
1890                                 break;
1891
1892                 if (j == i + 1)
1893                         continue;
1894
1895                 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
1896                 for (k = i; k < j; ++k)
1897                         part = isl_union_map_add_map(part,
1898                                                      isl_map_copy(v[k].map));
1899
1900                 injective = plain_injective_on_range(part, pos + 1, n_range);
1901                 if (injective < 0)
1902                         goto error;
1903                 if (!injective)
1904                         break;
1905
1906                 i = j - 1;
1907         }
1908
1909         isl_space_free(dim);
1910         free_isl_fixed_map_array(v, n);
1911         return i + 1 >= n;
1912 error:
1913         isl_space_free(dim);
1914         free_isl_fixed_map_array(v, n);
1915         return -1;
1916 }
1917
1918 /* Check whether the maps in umap have obviously distinct ranges.
1919  * In particular, check for an output dimension in the range
1920  * [first,n_range) for which all maps have a fixed value
1921  * and then check if these values, possibly along with fixed values
1922  * at later dimensions, entail distinct ranges.
1923  */
1924 static int plain_injective_on_range(__isl_take isl_union_map *umap,
1925         int first, int n_range)
1926 {
1927         isl_ctx *ctx;
1928         int n;
1929         struct isl_fixed_dim_data data = { NULL };
1930
1931         ctx = isl_union_map_get_ctx(umap);
1932
1933         if (!umap)
1934                 goto error;
1935
1936         n = isl_union_map_n_map(umap);
1937         if (n <= 1) {
1938                 isl_union_map_free(umap);
1939                 return 1;
1940         }
1941
1942         if (first >= n_range) {
1943                 isl_union_map_free(umap);
1944                 return 0;
1945         }
1946
1947         data.v = alloc_isl_fixed_map_array(ctx, n);
1948         if (!data.v)
1949                 goto error;
1950
1951         for (data.pos = first; data.pos < n_range; ++data.pos) {
1952                 int fixed;
1953                 int injective;
1954                 isl_space *dim;
1955
1956                 data.n = 0;
1957                 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
1958                 if (fixed < 0)
1959                         goto error;
1960                 if (!fixed)
1961                         continue;
1962                 dim = isl_union_map_get_space(umap);
1963                 injective = separates(data.v, n, dim, data.pos, n_range);
1964                 isl_union_map_free(umap);
1965                 return injective;
1966         }
1967
1968         free_isl_fixed_map_array(data.v, n);
1969         isl_union_map_free(umap);
1970
1971         return 0;
1972 error:
1973         free_isl_fixed_map_array(data.v, n);
1974         isl_union_map_free(umap);
1975         return -1;
1976 }
1977
1978 /* Check whether the maps in umap that map to subsets of "ran"
1979  * have obviously distinct ranges.
1980  */
1981 static int plain_injective_on_range_wrap(__isl_keep isl_set *ran, void *user)
1982 {
1983         isl_union_map *umap = user;
1984
1985         umap = isl_union_map_copy(umap);
1986         umap = isl_union_map_intersect_range(umap,
1987                         isl_union_set_from_set(isl_set_copy(ran)));
1988         return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
1989 }
1990
1991 /* Check if the given union_map is obviously injective.
1992  *
1993  * In particular, we first check if all individual maps are obviously
1994  * injective and then check if all the ranges of these maps are
1995  * obviously disjoint.
1996  */
1997 int isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
1998 {
1999         int in;
2000         isl_union_map *univ;
2001         isl_union_set *ran;
2002
2003         in = union_map_forall(umap, &isl_map_plain_is_injective);
2004         if (in < 0)
2005                 return -1;
2006         if (!in)
2007                 return 0;
2008
2009         univ = isl_union_map_universe(isl_union_map_copy(umap));
2010         ran = isl_union_map_range(univ);
2011
2012         in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
2013
2014         isl_union_set_free(ran);
2015
2016         return in;
2017 }
2018
2019 int isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
2020 {
2021         int sv;
2022
2023         sv = isl_union_map_is_single_valued(umap);
2024         if (sv < 0 || !sv)
2025                 return sv;
2026
2027         return isl_union_map_is_injective(umap);
2028 }
2029
2030 static int zip_entry(void **entry, void *user)
2031 {
2032         isl_map *map = *entry;
2033         isl_union_map **res = user;
2034
2035         if (!isl_map_can_zip(map))
2036                 return 0;
2037
2038         *res = isl_union_map_add_map(*res, isl_map_zip(isl_map_copy(map)));
2039
2040         return 0;
2041 }
2042
2043 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
2044 {
2045         return cond_un_op(umap, &zip_entry);
2046 }
2047
2048 static int lift_entry(void **entry, void *user)
2049 {
2050         isl_set *set = *entry;
2051         isl_union_set **res = user;
2052
2053         *res = isl_union_set_add_set(*res, isl_set_lift(isl_set_copy(set)));
2054
2055         return 0;
2056 }
2057
2058 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
2059 {
2060         return cond_un_op(uset, &lift_entry);
2061 }
2062
2063 static int coefficients_entry(void **entry, void *user)
2064 {
2065         isl_set *set = *entry;
2066         isl_union_set **res = user;
2067
2068         set = isl_set_copy(set);
2069         set = isl_set_from_basic_set(isl_set_coefficients(set));
2070         *res = isl_union_set_add_set(*res, set);
2071
2072         return 0;
2073 }
2074
2075 __isl_give isl_union_set *isl_union_set_coefficients(
2076         __isl_take isl_union_set *uset)
2077 {
2078         isl_ctx *ctx;
2079         isl_space *dim;
2080         isl_union_set *res;
2081
2082         if (!uset)
2083                 return NULL;
2084
2085         ctx = isl_union_set_get_ctx(uset);
2086         dim = isl_space_set_alloc(ctx, 0, 0);
2087         res = isl_union_map_alloc(dim, uset->table.n);
2088         if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2089                                    &coefficients_entry, &res) < 0)
2090                 goto error;
2091
2092         isl_union_set_free(uset);
2093         return res;
2094 error:
2095         isl_union_set_free(uset);
2096         isl_union_set_free(res);
2097         return NULL;
2098 }
2099
2100 static int solutions_entry(void **entry, void *user)
2101 {
2102         isl_set *set = *entry;
2103         isl_union_set **res = user;
2104
2105         set = isl_set_copy(set);
2106         set = isl_set_from_basic_set(isl_set_solutions(set));
2107         if (!*res)
2108                 *res = isl_union_set_from_set(set);
2109         else
2110                 *res = isl_union_set_add_set(*res, set);
2111
2112         if (!*res)
2113                 return -1;
2114
2115         return 0;
2116 }
2117
2118 __isl_give isl_union_set *isl_union_set_solutions(
2119         __isl_take isl_union_set *uset)
2120 {
2121         isl_union_set *res = NULL;
2122
2123         if (!uset)
2124                 return NULL;
2125
2126         if (uset->table.n == 0) {
2127                 res = isl_union_set_empty(isl_union_set_get_space(uset));
2128                 isl_union_set_free(uset);
2129                 return res;
2130         }
2131
2132         if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2133                                    &solutions_entry, &res) < 0)
2134                 goto error;
2135
2136         isl_union_set_free(uset);
2137         return res;
2138 error:
2139         isl_union_set_free(uset);
2140         isl_union_set_free(res);
2141         return NULL;
2142 }