hide isl_block
[isl.git] / isl_map_subtract.c
blob9de5beb8145a5a82a052e88ec80fd8618523c38e
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 (!*index)
233 *index = isl_alloc_array(ctx, int, n_test);
234 if (!*index)
235 return -1;
237 for (n = 0, i = 0; i < n_test; ++i) {
238 int r;
239 r = isl_tab_is_redundant(tab, offset + i);
240 if (r < 0)
241 return -1;
242 if (r)
243 continue;
244 (*index)[n++] = i;
247 return n;
250 /* basic_map_collect_diff calls add on each of the pieces of
251 * the set difference between bmap and map until the add method
252 * return a negative value.
254 struct isl_diff_collector {
255 int (*add)(struct isl_diff_collector *dc,
256 __isl_take isl_basic_map *bmap);
259 /* Compute the set difference between bmap and map and call
260 * dc->add on each of the piece until this function returns
261 * a negative value.
262 * Return 0 on success and -1 on error. dc->add returning
263 * a negative value is treated as an error, but the calling
264 * function can interpret the results based on the state of dc.
266 * Assumes that map has known divs.
268 * The difference is computed by a backtracking algorithm.
269 * Each level corresponds to a basic map in "map".
270 * When a node in entered for the first time, we check
271 * if the corresonding basic map intersects the current piece
272 * of "bmap". If not, we move to the next level.
273 * Otherwise, we split the current piece into as many
274 * pieces as there are non-redundant constraints of the current
275 * basic map in the intersection. Each of these pieces is
276 * handled by a child of the current node.
277 * In particular, if there are n non-redundant constraints,
278 * then for each 0 <= i < n, a piece is cut off by adding
279 * constraints 0 <= j < i and adding the opposite of constraint i.
280 * If there are no non-redundant constraints, meaning that the current
281 * piece is a subset of the current basic map, then we simply backtrack.
283 * In the leaves, we check if the remaining piece has any integer points
284 * and if so, pass it along to dc->add. As a special case, if nothing
285 * has been removed when we end up in a leaf, we simply pass along
286 * the original basic map.
288 static int basic_map_collect_diff(__isl_take isl_basic_map *bmap,
289 __isl_take isl_map *map, struct isl_diff_collector *dc)
291 int i;
292 int modified;
293 int level;
294 int init;
295 int empty;
296 isl_ctx *ctx;
297 struct isl_tab *tab = NULL;
298 struct isl_tab_undo **snap = NULL;
299 int *k = NULL;
300 int *n = NULL;
301 int **index = NULL;
302 int **div_map = NULL;
304 empty = isl_basic_map_is_empty(bmap);
305 if (empty) {
306 isl_basic_map_free(bmap);
307 isl_map_free(map);
308 return empty < 0 ? -1 : 0;
311 bmap = isl_basic_map_cow(bmap);
312 map = isl_map_cow(map);
314 if (!bmap || !map)
315 goto error;
317 ctx = map->ctx;
318 snap = isl_alloc_array(map->ctx, struct isl_tab_undo *, map->n);
319 k = isl_alloc_array(map->ctx, int, map->n);
320 n = isl_alloc_array(map->ctx, int, map->n);
321 index = isl_calloc_array(map->ctx, int *, map->n);
322 div_map = isl_calloc_array(map->ctx, int *, map->n);
323 if (!snap || !k || !n || !index || !div_map)
324 goto error;
326 bmap = isl_basic_map_order_divs(bmap);
327 map = isl_map_order_divs(map);
329 tab = isl_tab_from_basic_map(bmap, 1);
330 if (!tab)
331 goto error;
333 modified = 0;
334 level = 0;
335 init = 1;
337 while (level >= 0) {
338 if (level >= map->n) {
339 int empty;
340 struct isl_basic_map *bm;
341 if (!modified) {
342 if (dc->add(dc, isl_basic_map_copy(bmap)) < 0)
343 goto error;
344 break;
346 bm = isl_basic_map_copy(tab->bmap);
347 bm = isl_basic_map_cow(bm);
348 bm = isl_basic_map_update_from_tab(bm, tab);
349 bm = isl_basic_map_simplify(bm);
350 bm = isl_basic_map_finalize(bm);
351 empty = isl_basic_map_is_empty(bm);
352 if (empty)
353 isl_basic_map_free(bm);
354 else if (dc->add(dc, bm) < 0)
355 goto error;
356 if (empty < 0)
357 goto error;
358 level--;
359 init = 0;
360 continue;
362 if (init) {
363 int offset;
364 struct isl_tab_undo *snap2;
365 snap2 = isl_tab_snap(tab);
366 if (tab_add_divs(tab, map->p[level],
367 &div_map[level]) < 0)
368 goto error;
369 offset = tab->n_con;
370 snap[level] = isl_tab_snap(tab);
371 if (tab_freeze_constraints(tab) < 0)
372 goto error;
373 if (tab_add_constraints(tab, map->p[level],
374 div_map[level]) < 0)
375 goto error;
376 k[level] = 0;
377 n[level] = 0;
378 if (tab->empty) {
379 if (isl_tab_rollback(tab, snap2) < 0)
380 goto error;
381 level++;
382 continue;
384 modified = 1;
385 n[level] = n_non_redundant(ctx, tab, offset,
386 &index[level]);
387 if (n[level] < 0)
388 goto error;
389 if (n[level] == 0) {
390 level--;
391 init = 0;
392 continue;
394 if (isl_tab_rollback(tab, snap[level]) < 0)
395 goto error;
396 if (tab_add_constraint(tab, map->p[level],
397 div_map[level], index[level][0], 1) < 0)
398 goto error;
399 level++;
400 continue;
401 } else {
402 if (k[level] + 1 >= n[level]) {
403 level--;
404 continue;
406 if (isl_tab_rollback(tab, snap[level]) < 0)
407 goto error;
408 if (tab_add_constraint(tab, map->p[level],
409 div_map[level],
410 index[level][k[level]], 0) < 0)
411 goto error;
412 snap[level] = isl_tab_snap(tab);
413 k[level]++;
414 if (tab_add_constraint(tab, map->p[level],
415 div_map[level],
416 index[level][k[level]], 1) < 0)
417 goto error;
418 level++;
419 init = 1;
420 continue;
424 isl_tab_free(tab);
425 free(snap);
426 free(n);
427 free(k);
428 for (i = 0; index && i < map->n; ++i)
429 free(index[i]);
430 free(index);
431 for (i = 0; div_map && i < map->n; ++i)
432 free(div_map[i]);
433 free(div_map);
435 isl_basic_map_free(bmap);
436 isl_map_free(map);
438 return 0;
439 error:
440 isl_tab_free(tab);
441 free(snap);
442 free(n);
443 free(k);
444 for (i = 0; index && i < map->n; ++i)
445 free(index[i]);
446 free(index);
447 for (i = 0; div_map && i < map->n; ++i)
448 free(div_map[i]);
449 free(div_map);
450 isl_basic_map_free(bmap);
451 isl_map_free(map);
452 return -1;
455 /* A diff collector that actually collects all parts of the
456 * set difference in the field diff.
458 struct isl_subtract_diff_collector {
459 struct isl_diff_collector dc;
460 struct isl_map *diff;
463 /* isl_subtract_diff_collector callback.
465 static int basic_map_subtract_add(struct isl_diff_collector *dc,
466 __isl_take isl_basic_map *bmap)
468 struct isl_subtract_diff_collector *sdc;
469 sdc = (struct isl_subtract_diff_collector *)dc;
471 sdc->diff = isl_map_union_disjoint(sdc->diff,
472 isl_map_from_basic_map(bmap));
474 return sdc->diff ? 0 : -1;
477 /* Return the set difference between bmap and map.
479 static __isl_give isl_map *basic_map_subtract(__isl_take isl_basic_map *bmap,
480 __isl_take isl_map *map)
482 struct isl_subtract_diff_collector sdc;
483 sdc.dc.add = &basic_map_subtract_add;
484 sdc.diff = isl_map_empty_like_basic_map(bmap);
485 if (basic_map_collect_diff(bmap, map, &sdc.dc) < 0) {
486 isl_map_free(sdc.diff);
487 sdc.diff = NULL;
489 return sdc.diff;
492 /* Return the set difference between map1 and map2.
493 * (U_i A_i) \ (U_j B_j) is computed as U_i (A_i \ (U_j B_j))
495 static __isl_give isl_map *map_subtract( __isl_take isl_map *map1,
496 __isl_take isl_map *map2)
498 int i;
499 struct isl_map *diff;
501 if (!map1 || !map2)
502 goto error;
504 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
506 if (isl_map_is_empty(map2)) {
507 isl_map_free(map2);
508 return map1;
511 map1 = isl_map_compute_divs(map1);
512 map2 = isl_map_compute_divs(map2);
513 if (!map1 || !map2)
514 goto error;
516 map1 = isl_map_remove_empty_parts(map1);
517 map2 = isl_map_remove_empty_parts(map2);
519 diff = isl_map_empty_like(map1);
520 for (i = 0; i < map1->n; ++i) {
521 struct isl_map *d;
522 d = basic_map_subtract(isl_basic_map_copy(map1->p[i]),
523 isl_map_copy(map2));
524 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT))
525 diff = isl_map_union_disjoint(diff, d);
526 else
527 diff = isl_map_union(diff, d);
530 isl_map_free(map1);
531 isl_map_free(map2);
533 return diff;
534 error:
535 isl_map_free(map1);
536 isl_map_free(map2);
537 return NULL;
540 __isl_give isl_map *isl_map_subtract( __isl_take isl_map *map1,
541 __isl_take isl_map *map2)
543 return isl_map_align_params_map_map_and(map1, map2, &map_subtract);
546 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
548 return (struct isl_set *)
549 isl_map_subtract(
550 (struct isl_map *)set1, (struct isl_map *)set2);
553 /* Remove the elements of "dom" from the domain of "map".
555 static __isl_give isl_map *map_subtract_domain(__isl_take isl_map *map,
556 __isl_take isl_set *dom)
558 isl_map *ext_dom;
560 if (!isl_map_compatible_domain(map, dom))
561 isl_die(isl_set_get_ctx(dom), isl_error_invalid,
562 "incompatible spaces", goto error);
564 ext_dom = isl_map_universe(isl_map_get_space(map));
565 ext_dom = isl_map_intersect_domain(ext_dom, dom);
566 return isl_map_subtract(map, ext_dom);
567 error:
568 isl_map_free(map);
569 isl_set_free(dom);
570 return NULL;
573 __isl_give isl_map *isl_map_subtract_domain(__isl_take isl_map *map,
574 __isl_take isl_set *dom)
576 return isl_map_align_params_map_map_and(map, dom, &map_subtract_domain);
579 /* Remove the elements of "dom" from the range of "map".
581 static __isl_give isl_map *map_subtract_range(__isl_take isl_map *map,
582 __isl_take isl_set *dom)
584 isl_map *ext_dom;
586 if (!isl_map_compatible_range(map, dom))
587 isl_die(isl_set_get_ctx(dom), isl_error_invalid,
588 "incompatible spaces", goto error);
590 ext_dom = isl_map_universe(isl_map_get_space(map));
591 ext_dom = isl_map_intersect_range(ext_dom, dom);
592 return isl_map_subtract(map, ext_dom);
593 error:
594 isl_map_free(map);
595 isl_set_free(dom);
596 return NULL;
599 __isl_give isl_map *isl_map_subtract_range(__isl_take isl_map *map,
600 __isl_take isl_set *dom)
602 return isl_map_align_params_map_map_and(map, dom, &map_subtract_range);
605 /* A diff collector that aborts as soon as its add function is called,
606 * setting empty to 0.
608 struct isl_is_empty_diff_collector {
609 struct isl_diff_collector dc;
610 int empty;
613 /* isl_is_empty_diff_collector callback.
615 static int basic_map_is_empty_add(struct isl_diff_collector *dc,
616 __isl_take isl_basic_map *bmap)
618 struct isl_is_empty_diff_collector *edc;
619 edc = (struct isl_is_empty_diff_collector *)dc;
621 edc->empty = 0;
623 isl_basic_map_free(bmap);
624 return -1;
627 /* Check if bmap \ map is empty by computing this set difference
628 * and breaking off as soon as the difference is known to be non-empty.
630 static int basic_map_diff_is_empty(__isl_keep isl_basic_map *bmap,
631 __isl_keep isl_map *map)
633 int r;
634 struct isl_is_empty_diff_collector edc;
636 r = isl_basic_map_plain_is_empty(bmap);
637 if (r)
638 return r;
640 edc.dc.add = &basic_map_is_empty_add;
641 edc.empty = 1;
642 r = basic_map_collect_diff(isl_basic_map_copy(bmap),
643 isl_map_copy(map), &edc.dc);
644 if (!edc.empty)
645 return 0;
647 return r < 0 ? -1 : 1;
650 /* Check if map1 \ map2 is empty by checking if the set difference is empty
651 * for each of the basic maps in map1.
653 static int map_diff_is_empty(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
655 int i;
656 int is_empty = 1;
658 if (!map1 || !map2)
659 return -1;
661 for (i = 0; i < map1->n; ++i) {
662 is_empty = basic_map_diff_is_empty(map1->p[i], map2);
663 if (is_empty < 0 || !is_empty)
664 break;
667 return is_empty;
670 /* Return 1 if "bmap" contains a single element.
672 int isl_basic_map_plain_is_singleton(__isl_keep isl_basic_map *bmap)
674 if (!bmap)
675 return -1;
676 if (bmap->n_div)
677 return 0;
678 if (bmap->n_ineq)
679 return 0;
680 return bmap->n_eq == isl_basic_map_total_dim(bmap);
683 /* Return 1 if "map" contains a single element.
685 int isl_map_plain_is_singleton(__isl_keep isl_map *map)
687 if (!map)
688 return -1;
689 if (map->n != 1)
690 return 0;
692 return isl_basic_map_plain_is_singleton(map->p[0]);
695 /* Given a singleton basic map, extract the single element
696 * as an isl_point.
698 static __isl_give isl_point *singleton_extract_point(
699 __isl_keep isl_basic_map *bmap)
701 int j;
702 unsigned dim;
703 struct isl_vec *point;
704 isl_int m;
706 if (!bmap)
707 return NULL;
709 dim = isl_basic_map_total_dim(bmap);
710 isl_assert(bmap->ctx, bmap->n_eq == dim, return NULL);
711 point = isl_vec_alloc(bmap->ctx, 1 + dim);
712 if (!point)
713 return NULL;
715 isl_int_init(m);
717 isl_int_set_si(point->el[0], 1);
718 for (j = 0; j < bmap->n_eq; ++j) {
719 int i = dim - 1 - j;
720 isl_assert(bmap->ctx,
721 isl_seq_first_non_zero(bmap->eq[j] + 1, i) == -1,
722 goto error);
723 isl_assert(bmap->ctx,
724 isl_int_is_one(bmap->eq[j][1 + i]) ||
725 isl_int_is_negone(bmap->eq[j][1 + i]),
726 goto error);
727 isl_assert(bmap->ctx,
728 isl_seq_first_non_zero(bmap->eq[j]+1+i+1, dim-i-1) == -1,
729 goto error);
731 isl_int_gcd(m, point->el[0], bmap->eq[j][1 + i]);
732 isl_int_divexact(m, bmap->eq[j][1 + i], m);
733 isl_int_abs(m, m);
734 isl_seq_scale(point->el, point->el, m, 1 + i);
735 isl_int_divexact(m, point->el[0], bmap->eq[j][1 + i]);
736 isl_int_neg(m, m);
737 isl_int_mul(point->el[1 + i], m, bmap->eq[j][0]);
740 isl_int_clear(m);
741 return isl_point_alloc(isl_basic_map_get_space(bmap), point);
742 error:
743 isl_int_clear(m);
744 isl_vec_free(point);
745 return NULL;
748 /* Return 1 is the singleton map "map1" is a subset of "map2",
749 * i.e., if the single element of "map1" is also an element of "map2".
750 * Assumes "map2" has known divs.
752 static int map_is_singleton_subset(__isl_keep isl_map *map1,
753 __isl_keep isl_map *map2)
755 int i;
756 int is_subset = 0;
757 struct isl_point *point;
759 if (!map1 || !map2)
760 return -1;
761 if (map1->n != 1)
762 return -1;
764 point = singleton_extract_point(map1->p[0]);
765 if (!point)
766 return -1;
768 for (i = 0; i < map2->n; ++i) {
769 is_subset = isl_basic_map_contains_point(map2->p[i], point);
770 if (is_subset)
771 break;
774 isl_point_free(point);
775 return is_subset;
778 static int map_is_subset(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
780 int is_subset = 0;
781 int empty;
782 int rat1, rat2;
784 if (!map1 || !map2)
785 return -1;
787 if (!isl_map_has_equal_space(map1, map2))
788 return 0;
790 empty = isl_map_is_empty(map1);
791 if (empty < 0)
792 return -1;
793 if (empty)
794 return 1;
796 empty = isl_map_is_empty(map2);
797 if (empty < 0)
798 return -1;
799 if (empty)
800 return 0;
802 rat1 = isl_map_has_rational(map1);
803 rat2 = isl_map_has_rational(map2);
804 if (rat1 < 0 || rat2 < 0)
805 return -1;
806 if (rat1 && !rat2)
807 return 0;
809 if (isl_map_plain_is_universe(map2))
810 return 1;
812 map2 = isl_map_compute_divs(isl_map_copy(map2));
813 if (isl_map_plain_is_singleton(map1)) {
814 is_subset = map_is_singleton_subset(map1, map2);
815 isl_map_free(map2);
816 return is_subset;
818 is_subset = map_diff_is_empty(map1, map2);
819 isl_map_free(map2);
821 return is_subset;
824 int isl_map_is_subset(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
826 return isl_map_align_params_map_map_and_test(map1, map2,
827 &map_is_subset);
830 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
832 return isl_map_is_subset(
833 (struct isl_map *)set1, (struct isl_map *)set2);
836 __isl_give isl_map *isl_map_make_disjoint(__isl_take isl_map *map)
838 int i;
839 struct isl_subtract_diff_collector sdc;
840 sdc.dc.add = &basic_map_subtract_add;
842 if (!map)
843 return NULL;
844 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
845 return map;
846 if (map->n <= 1)
847 return map;
849 map = isl_map_compute_divs(map);
850 map = isl_map_remove_empty_parts(map);
852 if (!map || map->n <= 1)
853 return map;
855 sdc.diff = isl_map_from_basic_map(isl_basic_map_copy(map->p[0]));
857 for (i = 1; i < map->n; ++i) {
858 struct isl_basic_map *bmap = isl_basic_map_copy(map->p[i]);
859 struct isl_map *copy = isl_map_copy(sdc.diff);
860 if (basic_map_collect_diff(bmap, copy, &sdc.dc) < 0) {
861 isl_map_free(sdc.diff);
862 sdc.diff = NULL;
863 break;
867 isl_map_free(map);
869 return sdc.diff;
872 __isl_give isl_set *isl_set_make_disjoint(__isl_take isl_set *set)
874 return (struct isl_set *)isl_map_make_disjoint((struct isl_map *)set);
877 __isl_give isl_map *isl_map_complement(__isl_take isl_map *map)
879 isl_map *universe;
881 if (!map)
882 return NULL;
884 universe = isl_map_universe(isl_map_get_space(map));
886 return isl_map_subtract(universe, map);
889 __isl_give isl_set *isl_set_complement(__isl_take isl_set *set)
891 return isl_map_complement(set);