add isl_set_wrapped_domain_map
[isl.git] / isl_map_subtract.c
blob4d10cab807db52fdcd63b35acab1566c6e937480
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include <isl_map_private.h>
11 #include <isl_seq.h>
12 #include <isl/set.h>
13 #include <isl/map.h>
14 #include "isl_tab.h"
15 #include <isl_point_private.h>
16 #include <isl_vec_private.h>
18 /* Expand the constraint "c" into "v". The initial "dim" dimensions
19 * are the same, but "v" may have more divs than "c" and the divs of "c"
20 * may appear in different positions in "v".
21 * The number of divs in "c" is given by "n_div" and the mapping
22 * of divs in "c" to divs in "v" is given by "div_map".
24 * Although it shouldn't happen in practice, it is theoretically
25 * possible that two or more divs in "c" are mapped to the same div in "v".
26 * These divs are then necessarily the same, so we simply add their
27 * coefficients.
29 static void expand_constraint(isl_vec *v, unsigned dim,
30 isl_int *c, int *div_map, unsigned n_div)
32 int i;
34 isl_seq_cpy(v->el, c, 1 + dim);
35 isl_seq_clr(v->el + 1 + dim, v->size - (1 + dim));
37 for (i = 0; i < n_div; ++i) {
38 int pos = 1 + dim + div_map[i];
39 isl_int_add(v->el[pos], v->el[pos], c[1 + dim + i]);
43 /* Add all constraints of bmap to tab. The equalities of bmap
44 * are added as a pair of inequalities.
46 static int tab_add_constraints(struct isl_tab *tab,
47 __isl_keep isl_basic_map *bmap, int *div_map)
49 int i;
50 unsigned dim;
51 unsigned tab_total;
52 unsigned bmap_total;
53 isl_vec *v;
55 if (!tab || !bmap)
56 return -1;
58 tab_total = isl_basic_map_total_dim(tab->bmap);
59 bmap_total = isl_basic_map_total_dim(bmap);
60 dim = isl_space_dim(tab->bmap->dim, isl_dim_all);
62 if (isl_tab_extend_cons(tab, 2 * bmap->n_eq + bmap->n_ineq) < 0)
63 return -1;
65 v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
66 if (!v)
67 return -1;
69 for (i = 0; i < bmap->n_eq; ++i) {
70 expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
71 if (isl_tab_add_ineq(tab, v->el) < 0)
72 goto error;
73 isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
74 expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
75 if (isl_tab_add_ineq(tab, v->el) < 0)
76 goto error;
77 isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
78 if (tab->empty)
79 break;
82 for (i = 0; i < bmap->n_ineq; ++i) {
83 expand_constraint(v, dim, bmap->ineq[i], div_map, bmap->n_div);
84 if (isl_tab_add_ineq(tab, v->el) < 0)
85 goto error;
86 if (tab->empty)
87 break;
90 isl_vec_free(v);
91 return 0;
92 error:
93 isl_vec_free(v);
94 return -1;
97 /* Add a specific constraint of bmap (or its opposite) to tab.
98 * The position of the constraint is specified by "c", where
99 * the equalities of bmap are counted twice, once for the inequality
100 * that is equal to the equality, and once for its negation.
102 static int tab_add_constraint(struct isl_tab *tab,
103 __isl_keep isl_basic_map *bmap, int *div_map, int c, int oppose)
105 unsigned dim;
106 unsigned tab_total;
107 unsigned bmap_total;
108 isl_vec *v;
109 int r;
111 if (!tab || !bmap)
112 return -1;
114 tab_total = isl_basic_map_total_dim(tab->bmap);
115 bmap_total = isl_basic_map_total_dim(bmap);
116 dim = isl_space_dim(tab->bmap->dim, isl_dim_all);
118 v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
119 if (!v)
120 return -1;
122 if (c < 2 * bmap->n_eq) {
123 if ((c % 2) != oppose)
124 isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
125 1 + bmap_total);
126 if (oppose)
127 isl_int_sub_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
128 expand_constraint(v, dim, bmap->eq[c/2], div_map, bmap->n_div);
129 r = isl_tab_add_ineq(tab, v->el);
130 if (oppose)
131 isl_int_add_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
132 if ((c % 2) != oppose)
133 isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
134 1 + bmap_total);
135 } else {
136 c -= 2 * bmap->n_eq;
137 if (oppose) {
138 isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
139 1 + bmap_total);
140 isl_int_sub_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
142 expand_constraint(v, dim, bmap->ineq[c], div_map, bmap->n_div);
143 r = isl_tab_add_ineq(tab, v->el);
144 if (oppose) {
145 isl_int_add_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
146 isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
147 1 + bmap_total);
151 isl_vec_free(v);
152 return r;
155 static int tab_add_divs(struct isl_tab *tab, __isl_keep isl_basic_map *bmap,
156 int **div_map)
158 int i, j;
159 struct isl_vec *vec;
160 unsigned total;
161 unsigned dim;
163 if (!bmap)
164 return -1;
165 if (!bmap->n_div)
166 return 0;
168 if (!*div_map)
169 *div_map = isl_alloc_array(bmap->ctx, int, bmap->n_div);
170 if (!*div_map)
171 return -1;
173 total = isl_basic_map_total_dim(tab->bmap);
174 dim = total - tab->bmap->n_div;
175 vec = isl_vec_alloc(bmap->ctx, 2 + total + bmap->n_div);
176 if (!vec)
177 return -1;
179 for (i = 0; i < bmap->n_div; ++i) {
180 isl_seq_cpy(vec->el, bmap->div[i], 2 + dim);
181 isl_seq_clr(vec->el + 2 + dim, tab->bmap->n_div);
182 for (j = 0; j < i; ++j)
183 isl_int_set(vec->el[2 + dim + (*div_map)[j]],
184 bmap->div[i][2 + dim + j]);
185 for (j = 0; j < tab->bmap->n_div; ++j)
186 if (isl_seq_eq(tab->bmap->div[j],
187 vec->el, 2 + dim + tab->bmap->n_div))
188 break;
189 (*div_map)[i] = j;
190 if (j == tab->bmap->n_div) {
191 vec->size = 2 + dim + tab->bmap->n_div;
192 if (isl_tab_add_div(tab, vec, NULL, NULL) < 0)
193 goto error;
197 isl_vec_free(vec);
199 return 0;
200 error:
201 isl_vec_free(vec);
203 return -1;
206 /* Freeze all constraints of tableau tab.
208 static int tab_freeze_constraints(struct isl_tab *tab)
210 int i;
212 for (i = 0; i < tab->n_con; ++i)
213 if (isl_tab_freeze_constraint(tab, i) < 0)
214 return -1;
216 return 0;
219 /* Check for redundant constraints starting at offset.
220 * Put the indices of the redundant constraints in index
221 * and return the number of redundant constraints.
223 static int n_non_redundant(isl_ctx *ctx, struct isl_tab *tab,
224 int offset, int **index)
226 int i, n;
227 int n_test = tab->n_con - offset;
229 if (isl_tab_detect_redundant(tab) < 0)
230 return -1;
232 if (n_test == 0)
233 return 0;
234 if (!*index)
235 *index = isl_alloc_array(ctx, int, n_test);
236 if (!*index)
237 return -1;
239 for (n = 0, i = 0; i < n_test; ++i) {
240 int r;
241 r = isl_tab_is_redundant(tab, offset + i);
242 if (r < 0)
243 return -1;
244 if (r)
245 continue;
246 (*index)[n++] = i;
249 return n;
252 /* basic_map_collect_diff calls add on each of the pieces of
253 * the set difference between bmap and map until the add method
254 * return a negative value.
256 struct isl_diff_collector {
257 int (*add)(struct isl_diff_collector *dc,
258 __isl_take isl_basic_map *bmap);
261 /* Compute the set difference between bmap and map and call
262 * dc->add on each of the piece until this function returns
263 * a negative value.
264 * Return 0 on success and -1 on error. dc->add returning
265 * a negative value is treated as an error, but the calling
266 * function can interpret the results based on the state of dc.
268 * Assumes that map has known divs.
270 * The difference is computed by a backtracking algorithm.
271 * Each level corresponds to a basic map in "map".
272 * When a node in entered for the first time, we check
273 * if the corresonding basic map intersects the current piece
274 * of "bmap". If not, we move to the next level.
275 * Otherwise, we split the current piece into as many
276 * pieces as there are non-redundant constraints of the current
277 * basic map in the intersection. Each of these pieces is
278 * handled by a child of the current node.
279 * In particular, if there are n non-redundant constraints,
280 * then for each 0 <= i < n, a piece is cut off by adding
281 * constraints 0 <= j < i and adding the opposite of constraint i.
282 * If there are no non-redundant constraints, meaning that the current
283 * piece is a subset of the current basic map, then we simply backtrack.
285 * In the leaves, we check if the remaining piece has any integer points
286 * and if so, pass it along to dc->add. As a special case, if nothing
287 * has been removed when we end up in a leaf, we simply pass along
288 * the original basic map.
290 static int basic_map_collect_diff(__isl_take isl_basic_map *bmap,
291 __isl_take isl_map *map, struct isl_diff_collector *dc)
293 int i;
294 int modified;
295 int level;
296 int init;
297 int empty;
298 isl_ctx *ctx;
299 struct isl_tab *tab = NULL;
300 struct isl_tab_undo **snap = NULL;
301 int *k = NULL;
302 int *n = NULL;
303 int **index = NULL;
304 int **div_map = NULL;
306 empty = isl_basic_map_is_empty(bmap);
307 if (empty) {
308 isl_basic_map_free(bmap);
309 isl_map_free(map);
310 return empty < 0 ? -1 : 0;
313 bmap = isl_basic_map_cow(bmap);
314 map = isl_map_cow(map);
316 if (!bmap || !map)
317 goto error;
319 ctx = map->ctx;
320 snap = isl_alloc_array(map->ctx, struct isl_tab_undo *, map->n);
321 k = isl_alloc_array(map->ctx, int, map->n);
322 n = isl_alloc_array(map->ctx, int, map->n);
323 index = isl_calloc_array(map->ctx, int *, map->n);
324 div_map = isl_calloc_array(map->ctx, int *, map->n);
325 if (!snap || !k || !n || !index || !div_map)
326 goto error;
328 bmap = isl_basic_map_order_divs(bmap);
329 map = isl_map_order_divs(map);
331 tab = isl_tab_from_basic_map(bmap, 1);
332 if (!tab)
333 goto error;
335 modified = 0;
336 level = 0;
337 init = 1;
339 while (level >= 0) {
340 if (level >= map->n) {
341 int empty;
342 struct isl_basic_map *bm;
343 if (!modified) {
344 if (dc->add(dc, isl_basic_map_copy(bmap)) < 0)
345 goto error;
346 break;
348 bm = isl_basic_map_copy(tab->bmap);
349 bm = isl_basic_map_cow(bm);
350 bm = isl_basic_map_update_from_tab(bm, tab);
351 bm = isl_basic_map_simplify(bm);
352 bm = isl_basic_map_finalize(bm);
353 empty = isl_basic_map_is_empty(bm);
354 if (empty)
355 isl_basic_map_free(bm);
356 else if (dc->add(dc, bm) < 0)
357 goto error;
358 if (empty < 0)
359 goto error;
360 level--;
361 init = 0;
362 continue;
364 if (init) {
365 int offset;
366 struct isl_tab_undo *snap2;
367 snap2 = isl_tab_snap(tab);
368 if (tab_add_divs(tab, map->p[level],
369 &div_map[level]) < 0)
370 goto error;
371 offset = tab->n_con;
372 snap[level] = isl_tab_snap(tab);
373 if (tab_freeze_constraints(tab) < 0)
374 goto error;
375 if (tab_add_constraints(tab, map->p[level],
376 div_map[level]) < 0)
377 goto error;
378 k[level] = 0;
379 n[level] = 0;
380 if (tab->empty) {
381 if (isl_tab_rollback(tab, snap2) < 0)
382 goto error;
383 level++;
384 continue;
386 modified = 1;
387 n[level] = n_non_redundant(ctx, tab, offset,
388 &index[level]);
389 if (n[level] < 0)
390 goto error;
391 if (n[level] == 0) {
392 level--;
393 init = 0;
394 continue;
396 if (isl_tab_rollback(tab, snap[level]) < 0)
397 goto error;
398 if (tab_add_constraint(tab, map->p[level],
399 div_map[level], index[level][0], 1) < 0)
400 goto error;
401 level++;
402 continue;
403 } else {
404 if (k[level] + 1 >= n[level]) {
405 level--;
406 continue;
408 if (isl_tab_rollback(tab, snap[level]) < 0)
409 goto error;
410 if (tab_add_constraint(tab, map->p[level],
411 div_map[level],
412 index[level][k[level]], 0) < 0)
413 goto error;
414 snap[level] = isl_tab_snap(tab);
415 k[level]++;
416 if (tab_add_constraint(tab, map->p[level],
417 div_map[level],
418 index[level][k[level]], 1) < 0)
419 goto error;
420 level++;
421 init = 1;
422 continue;
426 isl_tab_free(tab);
427 free(snap);
428 free(n);
429 free(k);
430 for (i = 0; index && i < map->n; ++i)
431 free(index[i]);
432 free(index);
433 for (i = 0; div_map && i < map->n; ++i)
434 free(div_map[i]);
435 free(div_map);
437 isl_basic_map_free(bmap);
438 isl_map_free(map);
440 return 0;
441 error:
442 isl_tab_free(tab);
443 free(snap);
444 free(n);
445 free(k);
446 for (i = 0; index && i < map->n; ++i)
447 free(index[i]);
448 free(index);
449 for (i = 0; div_map && i < map->n; ++i)
450 free(div_map[i]);
451 free(div_map);
452 isl_basic_map_free(bmap);
453 isl_map_free(map);
454 return -1;
457 /* A diff collector that actually collects all parts of the
458 * set difference in the field diff.
460 struct isl_subtract_diff_collector {
461 struct isl_diff_collector dc;
462 struct isl_map *diff;
465 /* isl_subtract_diff_collector callback.
467 static int basic_map_subtract_add(struct isl_diff_collector *dc,
468 __isl_take isl_basic_map *bmap)
470 struct isl_subtract_diff_collector *sdc;
471 sdc = (struct isl_subtract_diff_collector *)dc;
473 sdc->diff = isl_map_union_disjoint(sdc->diff,
474 isl_map_from_basic_map(bmap));
476 return sdc->diff ? 0 : -1;
479 /* Return the set difference between bmap and map.
481 static __isl_give isl_map *basic_map_subtract(__isl_take isl_basic_map *bmap,
482 __isl_take isl_map *map)
484 struct isl_subtract_diff_collector sdc;
485 sdc.dc.add = &basic_map_subtract_add;
486 sdc.diff = isl_map_empty_like_basic_map(bmap);
487 if (basic_map_collect_diff(bmap, map, &sdc.dc) < 0) {
488 isl_map_free(sdc.diff);
489 sdc.diff = NULL;
491 return sdc.diff;
494 /* Return the set difference between map1 and map2.
495 * (U_i A_i) \ (U_j B_j) is computed as U_i (A_i \ (U_j B_j))
497 * If "map1" and "map2" are disjoint, then simply return "map1".
499 static __isl_give isl_map *map_subtract( __isl_take isl_map *map1,
500 __isl_take isl_map *map2)
502 int i;
503 int disjoint;
504 struct isl_map *diff;
506 if (!map1 || !map2)
507 goto error;
509 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
511 disjoint = isl_map_is_disjoint(map1, map2);
512 if (disjoint < 0)
513 goto error;
514 if (disjoint) {
515 isl_map_free(map2);
516 return map1;
519 map1 = isl_map_compute_divs(map1);
520 map2 = isl_map_compute_divs(map2);
521 if (!map1 || !map2)
522 goto error;
524 map1 = isl_map_remove_empty_parts(map1);
525 map2 = isl_map_remove_empty_parts(map2);
527 diff = isl_map_empty_like(map1);
528 for (i = 0; i < map1->n; ++i) {
529 struct isl_map *d;
530 d = basic_map_subtract(isl_basic_map_copy(map1->p[i]),
531 isl_map_copy(map2));
532 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT))
533 diff = isl_map_union_disjoint(diff, d);
534 else
535 diff = isl_map_union(diff, d);
538 isl_map_free(map1);
539 isl_map_free(map2);
541 return diff;
542 error:
543 isl_map_free(map1);
544 isl_map_free(map2);
545 return NULL;
548 __isl_give isl_map *isl_map_subtract( __isl_take isl_map *map1,
549 __isl_take isl_map *map2)
551 return isl_map_align_params_map_map_and(map1, map2, &map_subtract);
554 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
556 return (struct isl_set *)
557 isl_map_subtract(
558 (struct isl_map *)set1, (struct isl_map *)set2);
561 /* Remove the elements of "dom" from the domain of "map".
563 static __isl_give isl_map *map_subtract_domain(__isl_take isl_map *map,
564 __isl_take isl_set *dom)
566 isl_map *ext_dom;
568 if (!isl_map_compatible_domain(map, dom))
569 isl_die(isl_set_get_ctx(dom), isl_error_invalid,
570 "incompatible spaces", goto error);
572 ext_dom = isl_map_universe(isl_map_get_space(map));
573 ext_dom = isl_map_intersect_domain(ext_dom, dom);
574 return isl_map_subtract(map, ext_dom);
575 error:
576 isl_map_free(map);
577 isl_set_free(dom);
578 return NULL;
581 __isl_give isl_map *isl_map_subtract_domain(__isl_take isl_map *map,
582 __isl_take isl_set *dom)
584 return isl_map_align_params_map_map_and(map, dom, &map_subtract_domain);
587 /* Remove the elements of "dom" from the range of "map".
589 static __isl_give isl_map *map_subtract_range(__isl_take isl_map *map,
590 __isl_take isl_set *dom)
592 isl_map *ext_dom;
594 if (!isl_map_compatible_range(map, dom))
595 isl_die(isl_set_get_ctx(dom), isl_error_invalid,
596 "incompatible spaces", goto error);
598 ext_dom = isl_map_universe(isl_map_get_space(map));
599 ext_dom = isl_map_intersect_range(ext_dom, dom);
600 return isl_map_subtract(map, ext_dom);
601 error:
602 isl_map_free(map);
603 isl_set_free(dom);
604 return NULL;
607 __isl_give isl_map *isl_map_subtract_range(__isl_take isl_map *map,
608 __isl_take isl_set *dom)
610 return isl_map_align_params_map_map_and(map, dom, &map_subtract_range);
613 /* A diff collector that aborts as soon as its add function is called,
614 * setting empty to 0.
616 struct isl_is_empty_diff_collector {
617 struct isl_diff_collector dc;
618 int empty;
621 /* isl_is_empty_diff_collector callback.
623 static int basic_map_is_empty_add(struct isl_diff_collector *dc,
624 __isl_take isl_basic_map *bmap)
626 struct isl_is_empty_diff_collector *edc;
627 edc = (struct isl_is_empty_diff_collector *)dc;
629 edc->empty = 0;
631 isl_basic_map_free(bmap);
632 return -1;
635 /* Check if bmap \ map is empty by computing this set difference
636 * and breaking off as soon as the difference is known to be non-empty.
638 static int basic_map_diff_is_empty(__isl_keep isl_basic_map *bmap,
639 __isl_keep isl_map *map)
641 int r;
642 struct isl_is_empty_diff_collector edc;
644 r = isl_basic_map_plain_is_empty(bmap);
645 if (r)
646 return r;
648 edc.dc.add = &basic_map_is_empty_add;
649 edc.empty = 1;
650 r = basic_map_collect_diff(isl_basic_map_copy(bmap),
651 isl_map_copy(map), &edc.dc);
652 if (!edc.empty)
653 return 0;
655 return r < 0 ? -1 : 1;
658 /* Check if map1 \ map2 is empty by checking if the set difference is empty
659 * for each of the basic maps in map1.
661 static int map_diff_is_empty(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
663 int i;
664 int is_empty = 1;
666 if (!map1 || !map2)
667 return -1;
669 for (i = 0; i < map1->n; ++i) {
670 is_empty = basic_map_diff_is_empty(map1->p[i], map2);
671 if (is_empty < 0 || !is_empty)
672 break;
675 return is_empty;
678 /* Return 1 if "bmap" contains a single element.
680 int isl_basic_map_plain_is_singleton(__isl_keep isl_basic_map *bmap)
682 if (!bmap)
683 return -1;
684 if (bmap->n_div)
685 return 0;
686 if (bmap->n_ineq)
687 return 0;
688 return bmap->n_eq == isl_basic_map_total_dim(bmap);
691 /* Return 1 if "map" contains a single element.
693 int isl_map_plain_is_singleton(__isl_keep isl_map *map)
695 if (!map)
696 return -1;
697 if (map->n != 1)
698 return 0;
700 return isl_basic_map_plain_is_singleton(map->p[0]);
703 /* Given a singleton basic map, extract the single element
704 * as an isl_point.
706 static __isl_give isl_point *singleton_extract_point(
707 __isl_keep isl_basic_map *bmap)
709 int j;
710 unsigned dim;
711 struct isl_vec *point;
712 isl_int m;
714 if (!bmap)
715 return NULL;
717 dim = isl_basic_map_total_dim(bmap);
718 isl_assert(bmap->ctx, bmap->n_eq == dim, return NULL);
719 point = isl_vec_alloc(bmap->ctx, 1 + dim);
720 if (!point)
721 return NULL;
723 isl_int_init(m);
725 isl_int_set_si(point->el[0], 1);
726 for (j = 0; j < bmap->n_eq; ++j) {
727 int i = dim - 1 - j;
728 isl_assert(bmap->ctx,
729 isl_seq_first_non_zero(bmap->eq[j] + 1, i) == -1,
730 goto error);
731 isl_assert(bmap->ctx,
732 isl_int_is_one(bmap->eq[j][1 + i]) ||
733 isl_int_is_negone(bmap->eq[j][1 + i]),
734 goto error);
735 isl_assert(bmap->ctx,
736 isl_seq_first_non_zero(bmap->eq[j]+1+i+1, dim-i-1) == -1,
737 goto error);
739 isl_int_gcd(m, point->el[0], bmap->eq[j][1 + i]);
740 isl_int_divexact(m, bmap->eq[j][1 + i], m);
741 isl_int_abs(m, m);
742 isl_seq_scale(point->el, point->el, m, 1 + i);
743 isl_int_divexact(m, point->el[0], bmap->eq[j][1 + i]);
744 isl_int_neg(m, m);
745 isl_int_mul(point->el[1 + i], m, bmap->eq[j][0]);
748 isl_int_clear(m);
749 return isl_point_alloc(isl_basic_map_get_space(bmap), point);
750 error:
751 isl_int_clear(m);
752 isl_vec_free(point);
753 return NULL;
756 /* Return 1 is the singleton map "map1" is a subset of "map2",
757 * i.e., if the single element of "map1" is also an element of "map2".
758 * Assumes "map2" has known divs.
760 static int map_is_singleton_subset(__isl_keep isl_map *map1,
761 __isl_keep isl_map *map2)
763 int i;
764 int is_subset = 0;
765 struct isl_point *point;
767 if (!map1 || !map2)
768 return -1;
769 if (map1->n != 1)
770 return -1;
772 point = singleton_extract_point(map1->p[0]);
773 if (!point)
774 return -1;
776 for (i = 0; i < map2->n; ++i) {
777 is_subset = isl_basic_map_contains_point(map2->p[i], point);
778 if (is_subset)
779 break;
782 isl_point_free(point);
783 return is_subset;
786 static int map_is_subset(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
788 int is_subset = 0;
789 int empty;
790 int rat1, rat2;
792 if (!map1 || !map2)
793 return -1;
795 if (!isl_map_has_equal_space(map1, map2))
796 return 0;
798 empty = isl_map_is_empty(map1);
799 if (empty < 0)
800 return -1;
801 if (empty)
802 return 1;
804 empty = isl_map_is_empty(map2);
805 if (empty < 0)
806 return -1;
807 if (empty)
808 return 0;
810 rat1 = isl_map_has_rational(map1);
811 rat2 = isl_map_has_rational(map2);
812 if (rat1 < 0 || rat2 < 0)
813 return -1;
814 if (rat1 && !rat2)
815 return 0;
817 if (isl_map_plain_is_universe(map2))
818 return 1;
820 map2 = isl_map_compute_divs(isl_map_copy(map2));
821 if (isl_map_plain_is_singleton(map1)) {
822 is_subset = map_is_singleton_subset(map1, map2);
823 isl_map_free(map2);
824 return is_subset;
826 is_subset = map_diff_is_empty(map1, map2);
827 isl_map_free(map2);
829 return is_subset;
832 int isl_map_is_subset(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
834 return isl_map_align_params_map_map_and_test(map1, map2,
835 &map_is_subset);
838 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
840 return isl_map_is_subset(
841 (struct isl_map *)set1, (struct isl_map *)set2);
844 __isl_give isl_map *isl_map_make_disjoint(__isl_take isl_map *map)
846 int i;
847 struct isl_subtract_diff_collector sdc;
848 sdc.dc.add = &basic_map_subtract_add;
850 if (!map)
851 return NULL;
852 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
853 return map;
854 if (map->n <= 1)
855 return map;
857 map = isl_map_compute_divs(map);
858 map = isl_map_remove_empty_parts(map);
860 if (!map || map->n <= 1)
861 return map;
863 sdc.diff = isl_map_from_basic_map(isl_basic_map_copy(map->p[0]));
865 for (i = 1; i < map->n; ++i) {
866 struct isl_basic_map *bmap = isl_basic_map_copy(map->p[i]);
867 struct isl_map *copy = isl_map_copy(sdc.diff);
868 if (basic_map_collect_diff(bmap, copy, &sdc.dc) < 0) {
869 isl_map_free(sdc.diff);
870 sdc.diff = NULL;
871 break;
875 isl_map_free(map);
877 return sdc.diff;
880 __isl_give isl_set *isl_set_make_disjoint(__isl_take isl_set *set)
882 return (struct isl_set *)isl_map_make_disjoint((struct isl_map *)set);
885 __isl_give isl_map *isl_map_complement(__isl_take isl_map *map)
887 isl_map *universe;
889 if (!map)
890 return NULL;
892 universe = isl_map_universe(isl_map_get_space(map));
894 return isl_map_subtract(universe, map);
897 __isl_give isl_set *isl_set_complement(__isl_take isl_set *set)
899 return isl_map_complement(set);