python interface: replace assert with explicit abort in print_callback
[isl.git] / isl_map_subtract.c
blob036737eb6ae5fe4e184231701a748de6d6ca12ad
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 * Each of these constraints has been added to "tab" before by
103 * tab_add_constraints (and later removed again), so there should
104 * already be a row available for the constraint.
106 static int tab_add_constraint(struct isl_tab *tab,
107 __isl_keep isl_basic_map *bmap, int *div_map, int c, int oppose)
109 unsigned dim;
110 unsigned tab_total;
111 unsigned bmap_total;
112 isl_vec *v;
113 int r;
115 if (!tab || !bmap)
116 return -1;
118 tab_total = isl_basic_map_total_dim(tab->bmap);
119 bmap_total = isl_basic_map_total_dim(bmap);
120 dim = isl_space_dim(tab->bmap->dim, isl_dim_all);
122 v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
123 if (!v)
124 return -1;
126 if (c < 2 * bmap->n_eq) {
127 if ((c % 2) != oppose)
128 isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
129 1 + bmap_total);
130 if (oppose)
131 isl_int_sub_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
132 expand_constraint(v, dim, bmap->eq[c/2], div_map, bmap->n_div);
133 r = isl_tab_add_ineq(tab, v->el);
134 if (oppose)
135 isl_int_add_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
136 if ((c % 2) != oppose)
137 isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
138 1 + bmap_total);
139 } else {
140 c -= 2 * bmap->n_eq;
141 if (oppose) {
142 isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
143 1 + bmap_total);
144 isl_int_sub_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
146 expand_constraint(v, dim, bmap->ineq[c], div_map, bmap->n_div);
147 r = isl_tab_add_ineq(tab, v->el);
148 if (oppose) {
149 isl_int_add_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
150 isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
151 1 + bmap_total);
155 isl_vec_free(v);
156 return r;
159 static int tab_add_divs(struct isl_tab *tab, __isl_keep isl_basic_map *bmap,
160 int **div_map)
162 int i, j;
163 struct isl_vec *vec;
164 unsigned total;
165 unsigned dim;
167 if (!bmap)
168 return -1;
169 if (!bmap->n_div)
170 return 0;
172 if (!*div_map)
173 *div_map = isl_alloc_array(bmap->ctx, int, bmap->n_div);
174 if (!*div_map)
175 return -1;
177 total = isl_basic_map_total_dim(tab->bmap);
178 dim = total - tab->bmap->n_div;
179 vec = isl_vec_alloc(bmap->ctx, 2 + total + bmap->n_div);
180 if (!vec)
181 return -1;
183 for (i = 0; i < bmap->n_div; ++i) {
184 isl_seq_cpy(vec->el, bmap->div[i], 2 + dim);
185 isl_seq_clr(vec->el + 2 + dim, tab->bmap->n_div);
186 for (j = 0; j < i; ++j)
187 isl_int_set(vec->el[2 + dim + (*div_map)[j]],
188 bmap->div[i][2 + dim + j]);
189 for (j = 0; j < tab->bmap->n_div; ++j)
190 if (isl_seq_eq(tab->bmap->div[j],
191 vec->el, 2 + dim + tab->bmap->n_div))
192 break;
193 (*div_map)[i] = j;
194 if (j == tab->bmap->n_div) {
195 vec->size = 2 + dim + tab->bmap->n_div;
196 if (isl_tab_add_div(tab, vec, NULL, NULL) < 0)
197 goto error;
201 isl_vec_free(vec);
203 return 0;
204 error:
205 isl_vec_free(vec);
207 return -1;
210 /* Freeze all constraints of tableau tab.
212 static int tab_freeze_constraints(struct isl_tab *tab)
214 int i;
216 for (i = 0; i < tab->n_con; ++i)
217 if (isl_tab_freeze_constraint(tab, i) < 0)
218 return -1;
220 return 0;
223 /* Check for redundant constraints starting at offset.
224 * Put the indices of the redundant constraints in index
225 * and return the number of redundant constraints.
227 static int n_non_redundant(isl_ctx *ctx, struct isl_tab *tab,
228 int offset, int **index)
230 int i, n;
231 int n_test = tab->n_con - offset;
233 if (isl_tab_detect_redundant(tab) < 0)
234 return -1;
236 if (n_test == 0)
237 return 0;
238 if (!*index)
239 *index = isl_alloc_array(ctx, int, n_test);
240 if (!*index)
241 return -1;
243 for (n = 0, i = 0; i < n_test; ++i) {
244 int r;
245 r = isl_tab_is_redundant(tab, offset + i);
246 if (r < 0)
247 return -1;
248 if (r)
249 continue;
250 (*index)[n++] = i;
253 return n;
256 /* basic_map_collect_diff calls add on each of the pieces of
257 * the set difference between bmap and map until the add method
258 * return a negative value.
260 struct isl_diff_collector {
261 int (*add)(struct isl_diff_collector *dc,
262 __isl_take isl_basic_map *bmap);
265 /* Compute the set difference between bmap and map and call
266 * dc->add on each of the piece until this function returns
267 * a negative value.
268 * Return 0 on success and -1 on error. dc->add returning
269 * a negative value is treated as an error, but the calling
270 * function can interpret the results based on the state of dc.
272 * Assumes that map has known divs.
274 * The difference is computed by a backtracking algorithm.
275 * Each level corresponds to a basic map in "map".
276 * When a node in entered for the first time, we check
277 * if the corresonding basic map intersects the current piece
278 * of "bmap". If not, we move to the next level.
279 * Otherwise, we split the current piece into as many
280 * pieces as there are non-redundant constraints of the current
281 * basic map in the intersection. Each of these pieces is
282 * handled by a child of the current node.
283 * In particular, if there are n non-redundant constraints,
284 * then for each 0 <= i < n, a piece is cut off by adding
285 * constraints 0 <= j < i and adding the opposite of constraint i.
286 * If there are no non-redundant constraints, meaning that the current
287 * piece is a subset of the current basic map, then we simply backtrack.
289 * In the leaves, we check if the remaining piece has any integer points
290 * and if so, pass it along to dc->add. As a special case, if nothing
291 * has been removed when we end up in a leaf, we simply pass along
292 * the original basic map.
294 static isl_stat basic_map_collect_diff(__isl_take isl_basic_map *bmap,
295 __isl_take isl_map *map, struct isl_diff_collector *dc)
297 int i;
298 int modified;
299 int level;
300 int init;
301 isl_bool empty;
302 isl_ctx *ctx;
303 struct isl_tab *tab = NULL;
304 struct isl_tab_undo **snap = NULL;
305 int *k = NULL;
306 int *n = NULL;
307 int **index = NULL;
308 int **div_map = NULL;
310 empty = isl_basic_map_is_empty(bmap);
311 if (empty) {
312 isl_basic_map_free(bmap);
313 isl_map_free(map);
314 return empty < 0 ? isl_stat_error : isl_stat_ok;
317 bmap = isl_basic_map_cow(bmap);
318 map = isl_map_cow(map);
320 if (!bmap || !map)
321 goto error;
323 ctx = map->ctx;
324 snap = isl_alloc_array(map->ctx, struct isl_tab_undo *, map->n);
325 k = isl_alloc_array(map->ctx, int, map->n);
326 n = isl_alloc_array(map->ctx, int, map->n);
327 index = isl_calloc_array(map->ctx, int *, map->n);
328 div_map = isl_calloc_array(map->ctx, int *, map->n);
329 if (!snap || !k || !n || !index || !div_map)
330 goto error;
332 bmap = isl_basic_map_order_divs(bmap);
333 map = isl_map_order_divs(map);
335 tab = isl_tab_from_basic_map(bmap, 1);
336 if (!tab)
337 goto error;
339 modified = 0;
340 level = 0;
341 init = 1;
343 while (level >= 0) {
344 if (level >= map->n) {
345 int empty;
346 struct isl_basic_map *bm;
347 if (!modified) {
348 if (dc->add(dc, isl_basic_map_copy(bmap)) < 0)
349 goto error;
350 break;
352 bm = isl_basic_map_copy(tab->bmap);
353 bm = isl_basic_map_cow(bm);
354 bm = isl_basic_map_update_from_tab(bm, tab);
355 bm = isl_basic_map_simplify(bm);
356 bm = isl_basic_map_finalize(bm);
357 empty = isl_basic_map_is_empty(bm);
358 if (empty)
359 isl_basic_map_free(bm);
360 else if (dc->add(dc, bm) < 0)
361 goto error;
362 if (empty < 0)
363 goto error;
364 level--;
365 init = 0;
366 continue;
368 if (init) {
369 int offset;
370 struct isl_tab_undo *snap2;
371 snap2 = isl_tab_snap(tab);
372 if (tab_add_divs(tab, map->p[level],
373 &div_map[level]) < 0)
374 goto error;
375 offset = tab->n_con;
376 snap[level] = isl_tab_snap(tab);
377 if (tab_freeze_constraints(tab) < 0)
378 goto error;
379 if (tab_add_constraints(tab, map->p[level],
380 div_map[level]) < 0)
381 goto error;
382 k[level] = 0;
383 n[level] = 0;
384 if (tab->empty) {
385 if (isl_tab_rollback(tab, snap2) < 0)
386 goto error;
387 level++;
388 continue;
390 modified = 1;
391 n[level] = n_non_redundant(ctx, tab, offset,
392 &index[level]);
393 if (n[level] < 0)
394 goto error;
395 if (n[level] == 0) {
396 level--;
397 init = 0;
398 continue;
400 if (isl_tab_rollback(tab, snap[level]) < 0)
401 goto error;
402 if (tab_add_constraint(tab, map->p[level],
403 div_map[level], index[level][0], 1) < 0)
404 goto error;
405 level++;
406 continue;
407 } else {
408 if (k[level] + 1 >= n[level]) {
409 level--;
410 continue;
412 if (isl_tab_rollback(tab, snap[level]) < 0)
413 goto error;
414 if (tab_add_constraint(tab, map->p[level],
415 div_map[level],
416 index[level][k[level]], 0) < 0)
417 goto error;
418 snap[level] = isl_tab_snap(tab);
419 k[level]++;
420 if (tab_add_constraint(tab, map->p[level],
421 div_map[level],
422 index[level][k[level]], 1) < 0)
423 goto error;
424 level++;
425 init = 1;
426 continue;
430 isl_tab_free(tab);
431 free(snap);
432 free(n);
433 free(k);
434 for (i = 0; index && i < map->n; ++i)
435 free(index[i]);
436 free(index);
437 for (i = 0; div_map && i < map->n; ++i)
438 free(div_map[i]);
439 free(div_map);
441 isl_basic_map_free(bmap);
442 isl_map_free(map);
444 return isl_stat_ok;
445 error:
446 isl_tab_free(tab);
447 free(snap);
448 free(n);
449 free(k);
450 for (i = 0; index && i < map->n; ++i)
451 free(index[i]);
452 free(index);
453 for (i = 0; div_map && i < map->n; ++i)
454 free(div_map[i]);
455 free(div_map);
456 isl_basic_map_free(bmap);
457 isl_map_free(map);
458 return isl_stat_error;
461 /* A diff collector that actually collects all parts of the
462 * set difference in the field diff.
464 struct isl_subtract_diff_collector {
465 struct isl_diff_collector dc;
466 struct isl_map *diff;
469 /* isl_subtract_diff_collector callback.
471 static int basic_map_subtract_add(struct isl_diff_collector *dc,
472 __isl_take isl_basic_map *bmap)
474 struct isl_subtract_diff_collector *sdc;
475 sdc = (struct isl_subtract_diff_collector *)dc;
477 sdc->diff = isl_map_union_disjoint(sdc->diff,
478 isl_map_from_basic_map(bmap));
480 return sdc->diff ? 0 : -1;
483 /* Return the set difference between bmap and map.
485 static __isl_give isl_map *basic_map_subtract(__isl_take isl_basic_map *bmap,
486 __isl_take isl_map *map)
488 struct isl_subtract_diff_collector sdc;
489 sdc.dc.add = &basic_map_subtract_add;
490 sdc.diff = isl_map_empty(isl_basic_map_get_space(bmap));
491 if (basic_map_collect_diff(bmap, map, &sdc.dc) < 0) {
492 isl_map_free(sdc.diff);
493 sdc.diff = NULL;
495 return sdc.diff;
498 /* Return an empty map living in the same space as "map1" and "map2".
500 static __isl_give isl_map *replace_pair_by_empty( __isl_take isl_map *map1,
501 __isl_take isl_map *map2)
503 isl_space *space;
505 space = isl_map_get_space(map1);
506 isl_map_free(map1);
507 isl_map_free(map2);
508 return isl_map_empty(space);
511 /* Return the set difference between map1 and map2.
512 * (U_i A_i) \ (U_j B_j) is computed as U_i (A_i \ (U_j B_j))
514 * If "map1" and "map2" are obviously equal to each other,
515 * then return an empty map in the same space.
517 * If "map1" and "map2" are disjoint, then simply return "map1".
519 static __isl_give isl_map *map_subtract( __isl_take isl_map *map1,
520 __isl_take isl_map *map2)
522 int i;
523 int equal, disjoint;
524 struct isl_map *diff;
526 if (!map1 || !map2)
527 goto error;
529 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
531 equal = isl_map_plain_is_equal(map1, map2);
532 if (equal < 0)
533 goto error;
534 if (equal)
535 return replace_pair_by_empty(map1, map2);
537 disjoint = isl_map_is_disjoint(map1, map2);
538 if (disjoint < 0)
539 goto error;
540 if (disjoint) {
541 isl_map_free(map2);
542 return map1;
545 map1 = isl_map_compute_divs(map1);
546 map2 = isl_map_compute_divs(map2);
547 if (!map1 || !map2)
548 goto error;
550 map1 = isl_map_remove_empty_parts(map1);
551 map2 = isl_map_remove_empty_parts(map2);
553 diff = isl_map_empty(isl_map_get_space(map1));
554 for (i = 0; i < map1->n; ++i) {
555 struct isl_map *d;
556 d = basic_map_subtract(isl_basic_map_copy(map1->p[i]),
557 isl_map_copy(map2));
558 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT))
559 diff = isl_map_union_disjoint(diff, d);
560 else
561 diff = isl_map_union(diff, d);
564 isl_map_free(map1);
565 isl_map_free(map2);
567 return diff;
568 error:
569 isl_map_free(map1);
570 isl_map_free(map2);
571 return NULL;
574 __isl_give isl_map *isl_map_subtract( __isl_take isl_map *map1,
575 __isl_take isl_map *map2)
577 return isl_map_align_params_map_map_and(map1, map2, &map_subtract);
580 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
582 return (struct isl_set *)
583 isl_map_subtract(
584 (struct isl_map *)set1, (struct isl_map *)set2);
587 /* Remove the elements of "dom" from the domain of "map".
589 static __isl_give isl_map *map_subtract_domain(__isl_take isl_map *map,
590 __isl_take isl_set *dom)
592 isl_map *ext_dom;
594 if (!isl_map_compatible_domain(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_domain(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_domain(__isl_take isl_map *map,
608 __isl_take isl_set *dom)
610 return isl_map_align_params_map_map_and(map, dom, &map_subtract_domain);
613 /* Remove the elements of "dom" from the range of "map".
615 static __isl_give isl_map *map_subtract_range(__isl_take isl_map *map,
616 __isl_take isl_set *dom)
618 isl_map *ext_dom;
620 if (!isl_map_compatible_range(map, dom))
621 isl_die(isl_set_get_ctx(dom), isl_error_invalid,
622 "incompatible spaces", goto error);
624 ext_dom = isl_map_universe(isl_map_get_space(map));
625 ext_dom = isl_map_intersect_range(ext_dom, dom);
626 return isl_map_subtract(map, ext_dom);
627 error:
628 isl_map_free(map);
629 isl_set_free(dom);
630 return NULL;
633 __isl_give isl_map *isl_map_subtract_range(__isl_take isl_map *map,
634 __isl_take isl_set *dom)
636 return isl_map_align_params_map_map_and(map, dom, &map_subtract_range);
639 /* A diff collector that aborts as soon as its add function is called,
640 * setting empty to 0.
642 struct isl_is_empty_diff_collector {
643 struct isl_diff_collector dc;
644 isl_bool empty;
647 /* isl_is_empty_diff_collector callback.
649 static int basic_map_is_empty_add(struct isl_diff_collector *dc,
650 __isl_take isl_basic_map *bmap)
652 struct isl_is_empty_diff_collector *edc;
653 edc = (struct isl_is_empty_diff_collector *)dc;
655 edc->empty = 0;
657 isl_basic_map_free(bmap);
658 return -1;
661 /* Check if bmap \ map is empty by computing this set difference
662 * and breaking off as soon as the difference is known to be non-empty.
664 static isl_bool basic_map_diff_is_empty(__isl_keep isl_basic_map *bmap,
665 __isl_keep isl_map *map)
667 isl_bool empty;
668 isl_stat r;
669 struct isl_is_empty_diff_collector edc;
671 empty = isl_basic_map_plain_is_empty(bmap);
672 if (empty)
673 return empty;
675 edc.dc.add = &basic_map_is_empty_add;
676 edc.empty = isl_bool_true;
677 r = basic_map_collect_diff(isl_basic_map_copy(bmap),
678 isl_map_copy(map), &edc.dc);
679 if (!edc.empty)
680 return isl_bool_false;
682 return r < 0 ? isl_bool_error : isl_bool_true;
685 /* Check if map1 \ map2 is empty by checking if the set difference is empty
686 * for each of the basic maps in map1.
688 static isl_bool map_diff_is_empty(__isl_keep isl_map *map1,
689 __isl_keep isl_map *map2)
691 int i;
692 isl_bool is_empty = isl_bool_true;
694 if (!map1 || !map2)
695 return isl_bool_error;
697 for (i = 0; i < map1->n; ++i) {
698 is_empty = basic_map_diff_is_empty(map1->p[i], map2);
699 if (is_empty < 0 || !is_empty)
700 break;
703 return is_empty;
706 /* Return 1 if "bmap" contains a single element.
708 int isl_basic_map_plain_is_singleton(__isl_keep isl_basic_map *bmap)
710 if (!bmap)
711 return -1;
712 if (bmap->n_div)
713 return 0;
714 if (bmap->n_ineq)
715 return 0;
716 return bmap->n_eq == isl_basic_map_total_dim(bmap);
719 /* Return 1 if "map" contains a single element.
721 int isl_map_plain_is_singleton(__isl_keep isl_map *map)
723 if (!map)
724 return -1;
725 if (map->n != 1)
726 return 0;
728 return isl_basic_map_plain_is_singleton(map->p[0]);
731 /* Given a singleton basic map, extract the single element
732 * as an isl_point.
734 static __isl_give isl_point *singleton_extract_point(
735 __isl_keep isl_basic_map *bmap)
737 int j;
738 unsigned dim;
739 struct isl_vec *point;
740 isl_int m;
742 if (!bmap)
743 return NULL;
745 dim = isl_basic_map_total_dim(bmap);
746 isl_assert(bmap->ctx, bmap->n_eq == dim, return NULL);
747 point = isl_vec_alloc(bmap->ctx, 1 + dim);
748 if (!point)
749 return NULL;
751 isl_int_init(m);
753 isl_int_set_si(point->el[0], 1);
754 for (j = 0; j < bmap->n_eq; ++j) {
755 int i = dim - 1 - j;
756 isl_assert(bmap->ctx,
757 isl_seq_first_non_zero(bmap->eq[j] + 1, i) == -1,
758 goto error);
759 isl_assert(bmap->ctx,
760 isl_int_is_one(bmap->eq[j][1 + i]) ||
761 isl_int_is_negone(bmap->eq[j][1 + i]),
762 goto error);
763 isl_assert(bmap->ctx,
764 isl_seq_first_non_zero(bmap->eq[j]+1+i+1, dim-i-1) == -1,
765 goto error);
767 isl_int_gcd(m, point->el[0], bmap->eq[j][1 + i]);
768 isl_int_divexact(m, bmap->eq[j][1 + i], m);
769 isl_int_abs(m, m);
770 isl_seq_scale(point->el, point->el, m, 1 + i);
771 isl_int_divexact(m, point->el[0], bmap->eq[j][1 + i]);
772 isl_int_neg(m, m);
773 isl_int_mul(point->el[1 + i], m, bmap->eq[j][0]);
776 isl_int_clear(m);
777 return isl_point_alloc(isl_basic_map_get_space(bmap), point);
778 error:
779 isl_int_clear(m);
780 isl_vec_free(point);
781 return NULL;
784 /* Return isl_bool_true if the singleton map "map1" is a subset of "map2",
785 * i.e., if the single element of "map1" is also an element of "map2".
786 * Assumes "map2" has known divs.
788 static isl_bool map_is_singleton_subset(__isl_keep isl_map *map1,
789 __isl_keep isl_map *map2)
791 int i;
792 isl_bool is_subset = isl_bool_false;
793 struct isl_point *point;
795 if (!map1 || !map2)
796 return isl_bool_error;
797 if (map1->n != 1)
798 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
799 "expecting single-disjunct input",
800 return isl_bool_error);
802 point = singleton_extract_point(map1->p[0]);
803 if (!point)
804 return isl_bool_error;
806 for (i = 0; i < map2->n; ++i) {
807 is_subset = isl_basic_map_contains_point(map2->p[i], point);
808 if (is_subset)
809 break;
812 isl_point_free(point);
813 return is_subset;
816 static isl_bool map_is_subset(__isl_keep isl_map *map1,
817 __isl_keep isl_map *map2)
819 isl_bool is_subset = isl_bool_false;
820 isl_bool empty;
821 int rat1, rat2;
823 if (!map1 || !map2)
824 return isl_bool_error;
826 if (!isl_map_has_equal_space(map1, map2))
827 return isl_bool_false;
829 empty = isl_map_is_empty(map1);
830 if (empty < 0)
831 return isl_bool_error;
832 if (empty)
833 return isl_bool_true;
835 empty = isl_map_is_empty(map2);
836 if (empty < 0)
837 return isl_bool_error;
838 if (empty)
839 return isl_bool_false;
841 rat1 = isl_map_has_rational(map1);
842 rat2 = isl_map_has_rational(map2);
843 if (rat1 < 0 || rat2 < 0)
844 return isl_bool_error;
845 if (rat1 && !rat2)
846 return isl_bool_false;
848 if (isl_map_plain_is_universe(map2))
849 return isl_bool_true;
851 map2 = isl_map_compute_divs(isl_map_copy(map2));
852 if (isl_map_plain_is_singleton(map1)) {
853 is_subset = map_is_singleton_subset(map1, map2);
854 isl_map_free(map2);
855 return is_subset;
857 is_subset = map_diff_is_empty(map1, map2);
858 isl_map_free(map2);
860 return is_subset;
863 isl_bool isl_map_is_subset(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
865 return isl_map_align_params_map_map_and_test(map1, map2,
866 &map_is_subset);
869 isl_bool isl_set_is_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
871 return isl_map_is_subset(
872 (struct isl_map *)set1, (struct isl_map *)set2);
875 __isl_give isl_map *isl_map_make_disjoint(__isl_take isl_map *map)
877 int i;
878 struct isl_subtract_diff_collector sdc;
879 sdc.dc.add = &basic_map_subtract_add;
881 if (!map)
882 return NULL;
883 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
884 return map;
885 if (map->n <= 1)
886 return map;
888 map = isl_map_compute_divs(map);
889 map = isl_map_remove_empty_parts(map);
891 if (!map || map->n <= 1)
892 return map;
894 sdc.diff = isl_map_from_basic_map(isl_basic_map_copy(map->p[0]));
896 for (i = 1; i < map->n; ++i) {
897 struct isl_basic_map *bmap = isl_basic_map_copy(map->p[i]);
898 struct isl_map *copy = isl_map_copy(sdc.diff);
899 if (basic_map_collect_diff(bmap, copy, &sdc.dc) < 0) {
900 isl_map_free(sdc.diff);
901 sdc.diff = NULL;
902 break;
906 isl_map_free(map);
908 return sdc.diff;
911 __isl_give isl_set *isl_set_make_disjoint(__isl_take isl_set *set)
913 return (struct isl_set *)isl_map_make_disjoint((struct isl_map *)set);
916 __isl_give isl_map *isl_map_complement(__isl_take isl_map *map)
918 isl_map *universe;
920 if (!map)
921 return NULL;
923 universe = isl_map_universe(isl_map_get_space(map));
925 return isl_map_subtract(universe, map);
928 __isl_give isl_set *isl_set_complement(__isl_take isl_set *set)
930 return isl_map_complement(set);