isl_aff_neg_basic_set: extract out isl_aff_pos_basic_set
[isl.git] / isl_map_subtract.c
blobdf16ea9e3034de3f4e035aabfc3ef790cd137d8c
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 #include <set_to_map.c>
19 #include <set_from_map.c>
21 /* Expand the constraint "c" into "v". The initial "dim" dimensions
22 * are the same, but "v" may have more divs than "c" and the divs of "c"
23 * may appear in different positions in "v".
24 * The number of divs in "c" is given by "n_div" and the mapping
25 * of divs in "c" to divs in "v" is given by "div_map".
27 * Although it shouldn't happen in practice, it is theoretically
28 * possible that two or more divs in "c" are mapped to the same div in "v".
29 * These divs are then necessarily the same, so we simply add their
30 * coefficients.
32 static void expand_constraint(isl_vec *v, unsigned dim,
33 isl_int *c, int *div_map, unsigned n_div)
35 int i;
37 isl_seq_cpy(v->el, c, 1 + dim);
38 isl_seq_clr(v->el + 1 + dim, v->size - (1 + dim));
40 for (i = 0; i < n_div; ++i) {
41 int pos = 1 + dim + div_map[i];
42 isl_int_add(v->el[pos], v->el[pos], c[1 + dim + i]);
46 /* Add all constraints of bmap to tab. The equalities of bmap
47 * are added as a pair of inequalities.
49 static isl_stat tab_add_constraints(struct isl_tab *tab,
50 __isl_keep isl_basic_map *bmap, int *div_map)
52 int i;
53 unsigned dim;
54 unsigned tab_total;
55 unsigned bmap_total;
56 isl_vec *v;
58 if (!tab || !bmap)
59 return isl_stat_error;
61 tab_total = isl_basic_map_total_dim(tab->bmap);
62 bmap_total = isl_basic_map_total_dim(bmap);
63 dim = isl_space_dim(tab->bmap->dim, isl_dim_all);
65 if (isl_tab_extend_cons(tab, 2 * bmap->n_eq + bmap->n_ineq) < 0)
66 return isl_stat_error;
68 v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
69 if (!v)
70 return isl_stat_error;
72 for (i = 0; i < bmap->n_eq; ++i) {
73 expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
74 if (isl_tab_add_ineq(tab, v->el) < 0)
75 goto error;
76 isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
77 expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
78 if (isl_tab_add_ineq(tab, v->el) < 0)
79 goto error;
80 isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
81 if (tab->empty)
82 break;
85 for (i = 0; i < bmap->n_ineq; ++i) {
86 expand_constraint(v, dim, bmap->ineq[i], div_map, bmap->n_div);
87 if (isl_tab_add_ineq(tab, v->el) < 0)
88 goto error;
89 if (tab->empty)
90 break;
93 isl_vec_free(v);
94 return isl_stat_ok;
95 error:
96 isl_vec_free(v);
97 return isl_stat_error;
100 /* Add a specific constraint of bmap (or its opposite) to tab.
101 * The position of the constraint is specified by "c", where
102 * the equalities of bmap are counted twice, once for the inequality
103 * that is equal to the equality, and once for its negation.
105 * Each of these constraints has been added to "tab" before by
106 * tab_add_constraints (and later removed again), so there should
107 * already be a row available for the constraint.
109 static isl_stat tab_add_constraint(struct isl_tab *tab,
110 __isl_keep isl_basic_map *bmap, int *div_map, int c, int oppose)
112 unsigned dim;
113 unsigned tab_total;
114 unsigned bmap_total;
115 isl_vec *v;
116 isl_stat r;
118 if (!tab || !bmap)
119 return isl_stat_error;
121 tab_total = isl_basic_map_total_dim(tab->bmap);
122 bmap_total = isl_basic_map_total_dim(bmap);
123 dim = isl_space_dim(tab->bmap->dim, isl_dim_all);
125 v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
126 if (!v)
127 return isl_stat_error;
129 if (c < 2 * bmap->n_eq) {
130 if ((c % 2) != oppose)
131 isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
132 1 + bmap_total);
133 if (oppose)
134 isl_int_sub_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
135 expand_constraint(v, dim, bmap->eq[c/2], div_map, bmap->n_div);
136 r = isl_tab_add_ineq(tab, v->el);
137 if (oppose)
138 isl_int_add_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
139 if ((c % 2) != oppose)
140 isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
141 1 + bmap_total);
142 } else {
143 c -= 2 * bmap->n_eq;
144 if (oppose) {
145 isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
146 1 + bmap_total);
147 isl_int_sub_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
149 expand_constraint(v, dim, bmap->ineq[c], div_map, bmap->n_div);
150 r = isl_tab_add_ineq(tab, v->el);
151 if (oppose) {
152 isl_int_add_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
153 isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
154 1 + bmap_total);
158 isl_vec_free(v);
159 return r;
162 static isl_stat tab_add_divs(struct isl_tab *tab,
163 __isl_keep isl_basic_map *bmap, int **div_map)
165 int i, j;
166 struct isl_vec *vec;
167 unsigned total;
168 unsigned dim;
170 if (!bmap)
171 return isl_stat_error;
172 if (!bmap->n_div)
173 return isl_stat_ok;
175 if (!*div_map)
176 *div_map = isl_alloc_array(bmap->ctx, int, bmap->n_div);
177 if (!*div_map)
178 return isl_stat_error;
180 total = isl_basic_map_total_dim(tab->bmap);
181 dim = total - tab->bmap->n_div;
182 vec = isl_vec_alloc(bmap->ctx, 2 + total + bmap->n_div);
183 if (!vec)
184 return isl_stat_error;
186 for (i = 0; i < bmap->n_div; ++i) {
187 isl_seq_cpy(vec->el, bmap->div[i], 2 + dim);
188 isl_seq_clr(vec->el + 2 + dim, tab->bmap->n_div);
189 for (j = 0; j < i; ++j)
190 isl_int_set(vec->el[2 + dim + (*div_map)[j]],
191 bmap->div[i][2 + dim + j]);
192 for (j = 0; j < tab->bmap->n_div; ++j)
193 if (isl_seq_eq(tab->bmap->div[j],
194 vec->el, 2 + dim + tab->bmap->n_div))
195 break;
196 (*div_map)[i] = j;
197 if (j == tab->bmap->n_div) {
198 vec->size = 2 + dim + tab->bmap->n_div;
199 if (isl_tab_add_div(tab, vec) < 0)
200 goto error;
204 isl_vec_free(vec);
206 return isl_stat_ok;
207 error:
208 isl_vec_free(vec);
210 return isl_stat_error;
213 /* Freeze all constraints of tableau tab.
215 static int tab_freeze_constraints(struct isl_tab *tab)
217 int i;
219 for (i = 0; i < tab->n_con; ++i)
220 if (isl_tab_freeze_constraint(tab, i) < 0)
221 return -1;
223 return 0;
226 /* Check for redundant constraints starting at offset.
227 * Put the indices of the redundant constraints in index
228 * and return the number of redundant constraints.
230 static int n_non_redundant(isl_ctx *ctx, struct isl_tab *tab,
231 int offset, int **index)
233 int i, n;
234 int n_test = tab->n_con - offset;
236 if (isl_tab_detect_redundant(tab) < 0)
237 return -1;
239 if (n_test == 0)
240 return 0;
241 if (!*index)
242 *index = isl_alloc_array(ctx, int, n_test);
243 if (!*index)
244 return -1;
246 for (n = 0, i = 0; i < n_test; ++i) {
247 int r;
248 r = isl_tab_is_redundant(tab, offset + i);
249 if (r < 0)
250 return -1;
251 if (r)
252 continue;
253 (*index)[n++] = i;
256 return n;
259 /* basic_map_collect_diff calls add on each of the pieces of
260 * the set difference between bmap and map until the add method
261 * return a negative value.
263 struct isl_diff_collector {
264 isl_stat (*add)(struct isl_diff_collector *dc,
265 __isl_take isl_basic_map *bmap);
268 /* Compute the set difference between bmap and map and call
269 * dc->add on each of the piece until this function returns
270 * a negative value.
271 * Return 0 on success and -1 on error. dc->add returning
272 * a negative value is treated as an error, but the calling
273 * function can interpret the results based on the state of dc.
275 * Assumes that map has known divs.
277 * The difference is computed by a backtracking algorithm.
278 * Each level corresponds to a basic map in "map".
279 * When a node in entered for the first time, we check
280 * if the corresonding basic map intersects the current piece
281 * of "bmap". If not, we move to the next level.
282 * Otherwise, we split the current piece into as many
283 * pieces as there are non-redundant constraints of the current
284 * basic map in the intersection. Each of these pieces is
285 * handled by a child of the current node.
286 * In particular, if there are n non-redundant constraints,
287 * then for each 0 <= i < n, a piece is cut off by adding
288 * constraints 0 <= j < i and adding the opposite of constraint i.
289 * If there are no non-redundant constraints, meaning that the current
290 * piece is a subset of the current basic map, then we simply backtrack.
292 * In the leaves, we check if the remaining piece has any integer points
293 * and if so, pass it along to dc->add. As a special case, if nothing
294 * has been removed when we end up in a leaf, we simply pass along
295 * the original basic map.
297 static isl_stat basic_map_collect_diff(__isl_take isl_basic_map *bmap,
298 __isl_take isl_map *map, struct isl_diff_collector *dc)
300 int i;
301 int modified;
302 int level;
303 int init;
304 isl_bool empty;
305 isl_ctx *ctx;
306 struct isl_tab *tab = NULL;
307 struct isl_tab_undo **snap = NULL;
308 int *k = NULL;
309 int *n = NULL;
310 int **index = NULL;
311 int **div_map = NULL;
313 empty = isl_basic_map_is_empty(bmap);
314 if (empty) {
315 isl_basic_map_free(bmap);
316 isl_map_free(map);
317 return empty < 0 ? isl_stat_error : isl_stat_ok;
320 bmap = isl_basic_map_cow(bmap);
321 map = isl_map_cow(map);
323 if (!bmap || !map)
324 goto error;
326 ctx = map->ctx;
327 snap = isl_alloc_array(map->ctx, struct isl_tab_undo *, map->n);
328 k = isl_alloc_array(map->ctx, int, map->n);
329 n = isl_alloc_array(map->ctx, int, map->n);
330 index = isl_calloc_array(map->ctx, int *, map->n);
331 div_map = isl_calloc_array(map->ctx, int *, map->n);
332 if (!snap || !k || !n || !index || !div_map)
333 goto error;
335 bmap = isl_basic_map_order_divs(bmap);
336 map = isl_map_order_divs(map);
338 tab = isl_tab_from_basic_map(bmap, 1);
339 if (!tab)
340 goto error;
342 modified = 0;
343 level = 0;
344 init = 1;
346 while (level >= 0) {
347 if (level >= map->n) {
348 int empty;
349 struct isl_basic_map *bm;
350 if (!modified) {
351 if (dc->add(dc, isl_basic_map_copy(bmap)) < 0)
352 goto error;
353 break;
355 bm = isl_basic_map_copy(tab->bmap);
356 bm = isl_basic_map_cow(bm);
357 bm = isl_basic_map_update_from_tab(bm, tab);
358 bm = isl_basic_map_simplify(bm);
359 bm = isl_basic_map_finalize(bm);
360 empty = isl_basic_map_is_empty(bm);
361 if (empty)
362 isl_basic_map_free(bm);
363 else if (dc->add(dc, bm) < 0)
364 goto error;
365 if (empty < 0)
366 goto error;
367 level--;
368 init = 0;
369 continue;
371 if (init) {
372 int offset;
373 struct isl_tab_undo *snap2;
374 snap2 = isl_tab_snap(tab);
375 if (tab_add_divs(tab, map->p[level],
376 &div_map[level]) < 0)
377 goto error;
378 offset = tab->n_con;
379 snap[level] = isl_tab_snap(tab);
380 if (tab_freeze_constraints(tab) < 0)
381 goto error;
382 if (tab_add_constraints(tab, map->p[level],
383 div_map[level]) < 0)
384 goto error;
385 k[level] = 0;
386 n[level] = 0;
387 if (tab->empty) {
388 if (isl_tab_rollback(tab, snap2) < 0)
389 goto error;
390 level++;
391 continue;
393 modified = 1;
394 n[level] = n_non_redundant(ctx, tab, offset,
395 &index[level]);
396 if (n[level] < 0)
397 goto error;
398 if (n[level] == 0) {
399 level--;
400 init = 0;
401 continue;
403 if (isl_tab_rollback(tab, snap[level]) < 0)
404 goto error;
405 if (tab_add_constraint(tab, map->p[level],
406 div_map[level], index[level][0], 1) < 0)
407 goto error;
408 level++;
409 continue;
410 } else {
411 if (k[level] + 1 >= n[level]) {
412 level--;
413 continue;
415 if (isl_tab_rollback(tab, snap[level]) < 0)
416 goto error;
417 if (tab_add_constraint(tab, map->p[level],
418 div_map[level],
419 index[level][k[level]], 0) < 0)
420 goto error;
421 snap[level] = isl_tab_snap(tab);
422 k[level]++;
423 if (tab_add_constraint(tab, map->p[level],
424 div_map[level],
425 index[level][k[level]], 1) < 0)
426 goto error;
427 level++;
428 init = 1;
429 continue;
433 isl_tab_free(tab);
434 free(snap);
435 free(n);
436 free(k);
437 for (i = 0; index && i < map->n; ++i)
438 free(index[i]);
439 free(index);
440 for (i = 0; div_map && i < map->n; ++i)
441 free(div_map[i]);
442 free(div_map);
444 isl_basic_map_free(bmap);
445 isl_map_free(map);
447 return isl_stat_ok;
448 error:
449 isl_tab_free(tab);
450 free(snap);
451 free(n);
452 free(k);
453 for (i = 0; index && i < map->n; ++i)
454 free(index[i]);
455 free(index);
456 for (i = 0; div_map && i < map->n; ++i)
457 free(div_map[i]);
458 free(div_map);
459 isl_basic_map_free(bmap);
460 isl_map_free(map);
461 return isl_stat_error;
464 /* A diff collector that actually collects all parts of the
465 * set difference in the field diff.
467 struct isl_subtract_diff_collector {
468 struct isl_diff_collector dc;
469 struct isl_map *diff;
472 /* isl_subtract_diff_collector callback.
474 static isl_stat basic_map_subtract_add(struct isl_diff_collector *dc,
475 __isl_take isl_basic_map *bmap)
477 struct isl_subtract_diff_collector *sdc;
478 sdc = (struct isl_subtract_diff_collector *)dc;
480 sdc->diff = isl_map_union_disjoint(sdc->diff,
481 isl_map_from_basic_map(bmap));
483 return sdc->diff ? isl_stat_ok : isl_stat_error;
486 /* Return the set difference between bmap and map.
488 static __isl_give isl_map *basic_map_subtract(__isl_take isl_basic_map *bmap,
489 __isl_take isl_map *map)
491 struct isl_subtract_diff_collector sdc;
492 sdc.dc.add = &basic_map_subtract_add;
493 sdc.diff = isl_map_empty(isl_basic_map_get_space(bmap));
494 if (basic_map_collect_diff(bmap, map, &sdc.dc) < 0) {
495 isl_map_free(sdc.diff);
496 sdc.diff = NULL;
498 return sdc.diff;
501 /* Return an empty map living in the same space as "map1" and "map2".
503 static __isl_give isl_map *replace_pair_by_empty( __isl_take isl_map *map1,
504 __isl_take isl_map *map2)
506 isl_space *space;
508 space = isl_map_get_space(map1);
509 isl_map_free(map1);
510 isl_map_free(map2);
511 return isl_map_empty(space);
514 /* Return the set difference between map1 and map2.
515 * (U_i A_i) \ (U_j B_j) is computed as U_i (A_i \ (U_j B_j))
517 * If "map1" and "map2" are obviously equal to each other,
518 * then return an empty map in the same space.
520 * If "map1" and "map2" are disjoint, then simply return "map1".
522 static __isl_give isl_map *map_subtract( __isl_take isl_map *map1,
523 __isl_take isl_map *map2)
525 int i;
526 int equal, disjoint;
527 struct isl_map *diff;
529 if (!map1 || !map2)
530 goto error;
532 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
534 equal = isl_map_plain_is_equal(map1, map2);
535 if (equal < 0)
536 goto error;
537 if (equal)
538 return replace_pair_by_empty(map1, map2);
540 disjoint = isl_map_is_disjoint(map1, map2);
541 if (disjoint < 0)
542 goto error;
543 if (disjoint) {
544 isl_map_free(map2);
545 return map1;
548 map1 = isl_map_compute_divs(map1);
549 map2 = isl_map_compute_divs(map2);
550 if (!map1 || !map2)
551 goto error;
553 map1 = isl_map_remove_empty_parts(map1);
554 map2 = isl_map_remove_empty_parts(map2);
556 diff = isl_map_empty(isl_map_get_space(map1));
557 for (i = 0; i < map1->n; ++i) {
558 struct isl_map *d;
559 d = basic_map_subtract(isl_basic_map_copy(map1->p[i]),
560 isl_map_copy(map2));
561 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT))
562 diff = isl_map_union_disjoint(diff, d);
563 else
564 diff = isl_map_union(diff, d);
567 isl_map_free(map1);
568 isl_map_free(map2);
570 return diff;
571 error:
572 isl_map_free(map1);
573 isl_map_free(map2);
574 return NULL;
577 __isl_give isl_map *isl_map_subtract( __isl_take isl_map *map1,
578 __isl_take isl_map *map2)
580 return isl_map_align_params_map_map_and(map1, map2, &map_subtract);
583 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
585 return set_from_map(isl_map_subtract(set_to_map(set1),
586 set_to_map(set2)));
589 /* Remove the elements of "dom" from the domain of "map".
591 static __isl_give isl_map *map_subtract_domain(__isl_take isl_map *map,
592 __isl_take isl_set *dom)
594 isl_bool ok;
595 isl_map *ext_dom;
597 ok = isl_map_compatible_domain(map, dom);
598 if (ok < 0)
599 goto error;
600 if (!ok)
601 isl_die(isl_set_get_ctx(dom), isl_error_invalid,
602 "incompatible spaces", goto error);
604 ext_dom = isl_map_universe(isl_map_get_space(map));
605 ext_dom = isl_map_intersect_domain(ext_dom, dom);
606 return isl_map_subtract(map, ext_dom);
607 error:
608 isl_map_free(map);
609 isl_set_free(dom);
610 return NULL;
613 __isl_give isl_map *isl_map_subtract_domain(__isl_take isl_map *map,
614 __isl_take isl_set *dom)
616 return isl_map_align_params_map_map_and(map, dom, &map_subtract_domain);
619 /* Remove the elements of "dom" from the range of "map".
621 static __isl_give isl_map *map_subtract_range(__isl_take isl_map *map,
622 __isl_take isl_set *dom)
624 isl_bool ok;
625 isl_map *ext_dom;
627 ok = isl_map_compatible_range(map, dom);
628 if (ok < 0)
629 goto error;
630 if (!ok)
631 isl_die(isl_set_get_ctx(dom), isl_error_invalid,
632 "incompatible spaces", goto error);
634 ext_dom = isl_map_universe(isl_map_get_space(map));
635 ext_dom = isl_map_intersect_range(ext_dom, dom);
636 return isl_map_subtract(map, ext_dom);
637 error:
638 isl_map_free(map);
639 isl_set_free(dom);
640 return NULL;
643 __isl_give isl_map *isl_map_subtract_range(__isl_take isl_map *map,
644 __isl_take isl_set *dom)
646 return isl_map_align_params_map_map_and(map, dom, &map_subtract_range);
649 /* A diff collector that aborts as soon as its add function is called,
650 * setting empty to 0.
652 struct isl_is_empty_diff_collector {
653 struct isl_diff_collector dc;
654 isl_bool empty;
657 /* isl_is_empty_diff_collector callback.
659 static isl_stat basic_map_is_empty_add(struct isl_diff_collector *dc,
660 __isl_take isl_basic_map *bmap)
662 struct isl_is_empty_diff_collector *edc;
663 edc = (struct isl_is_empty_diff_collector *)dc;
665 edc->empty = 0;
667 isl_basic_map_free(bmap);
668 return isl_stat_error;
671 /* Check if bmap \ map is empty by computing this set difference
672 * and breaking off as soon as the difference is known to be non-empty.
674 static isl_bool basic_map_diff_is_empty(__isl_keep isl_basic_map *bmap,
675 __isl_keep isl_map *map)
677 isl_bool empty;
678 isl_stat r;
679 struct isl_is_empty_diff_collector edc;
681 empty = isl_basic_map_plain_is_empty(bmap);
682 if (empty)
683 return empty;
685 edc.dc.add = &basic_map_is_empty_add;
686 edc.empty = isl_bool_true;
687 r = basic_map_collect_diff(isl_basic_map_copy(bmap),
688 isl_map_copy(map), &edc.dc);
689 if (!edc.empty)
690 return isl_bool_false;
692 return r < 0 ? isl_bool_error : isl_bool_true;
695 /* Check if map1 \ map2 is empty by checking if the set difference is empty
696 * for each of the basic maps in map1.
698 static isl_bool map_diff_is_empty(__isl_keep isl_map *map1,
699 __isl_keep isl_map *map2)
701 int i;
702 isl_bool is_empty = isl_bool_true;
704 if (!map1 || !map2)
705 return isl_bool_error;
707 for (i = 0; i < map1->n; ++i) {
708 is_empty = basic_map_diff_is_empty(map1->p[i], map2);
709 if (is_empty < 0 || !is_empty)
710 break;
713 return is_empty;
716 /* Return true if "bmap" contains a single element.
718 isl_bool isl_basic_map_plain_is_singleton(__isl_keep isl_basic_map *bmap)
720 if (!bmap)
721 return isl_bool_error;
722 if (bmap->n_div)
723 return isl_bool_false;
724 if (bmap->n_ineq)
725 return isl_bool_false;
726 return bmap->n_eq == isl_basic_map_total_dim(bmap);
729 /* Return true if "map" contains a single element.
731 isl_bool isl_map_plain_is_singleton(__isl_keep isl_map *map)
733 if (!map)
734 return isl_bool_error;
735 if (map->n != 1)
736 return isl_bool_false;
738 return isl_basic_map_plain_is_singleton(map->p[0]);
741 /* Given a singleton basic map, extract the single element
742 * as an isl_point.
744 static __isl_give isl_point *singleton_extract_point(
745 __isl_keep isl_basic_map *bmap)
747 int j;
748 unsigned dim;
749 struct isl_vec *point;
750 isl_int m;
752 if (!bmap)
753 return NULL;
755 dim = isl_basic_map_total_dim(bmap);
756 isl_assert(bmap->ctx, bmap->n_eq == dim, return NULL);
757 point = isl_vec_alloc(bmap->ctx, 1 + dim);
758 if (!point)
759 return NULL;
761 isl_int_init(m);
763 isl_int_set_si(point->el[0], 1);
764 for (j = 0; j < bmap->n_eq; ++j) {
765 int i = dim - 1 - j;
766 isl_assert(bmap->ctx,
767 isl_seq_first_non_zero(bmap->eq[j] + 1, i) == -1,
768 goto error);
769 isl_assert(bmap->ctx,
770 isl_int_is_one(bmap->eq[j][1 + i]) ||
771 isl_int_is_negone(bmap->eq[j][1 + i]),
772 goto error);
773 isl_assert(bmap->ctx,
774 isl_seq_first_non_zero(bmap->eq[j]+1+i+1, dim-i-1) == -1,
775 goto error);
777 isl_int_gcd(m, point->el[0], bmap->eq[j][1 + i]);
778 isl_int_divexact(m, bmap->eq[j][1 + i], m);
779 isl_int_abs(m, m);
780 isl_seq_scale(point->el, point->el, m, 1 + i);
781 isl_int_divexact(m, point->el[0], bmap->eq[j][1 + i]);
782 isl_int_neg(m, m);
783 isl_int_mul(point->el[1 + i], m, bmap->eq[j][0]);
786 isl_int_clear(m);
787 return isl_point_alloc(isl_basic_map_get_space(bmap), point);
788 error:
789 isl_int_clear(m);
790 isl_vec_free(point);
791 return NULL;
794 /* Return isl_bool_true if the singleton map "map1" is a subset of "map2",
795 * i.e., if the single element of "map1" is also an element of "map2".
796 * Assumes "map2" has known divs.
798 static isl_bool map_is_singleton_subset(__isl_keep isl_map *map1,
799 __isl_keep isl_map *map2)
801 int i;
802 isl_bool is_subset = isl_bool_false;
803 struct isl_point *point;
805 if (!map1 || !map2)
806 return isl_bool_error;
807 if (map1->n != 1)
808 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
809 "expecting single-disjunct input",
810 return isl_bool_error);
812 point = singleton_extract_point(map1->p[0]);
813 if (!point)
814 return isl_bool_error;
816 for (i = 0; i < map2->n; ++i) {
817 is_subset = isl_basic_map_contains_point(map2->p[i], point);
818 if (is_subset)
819 break;
822 isl_point_free(point);
823 return is_subset;
826 static isl_bool map_is_subset(__isl_keep isl_map *map1,
827 __isl_keep isl_map *map2)
829 isl_bool is_subset = isl_bool_false;
830 isl_bool empty, single;
831 isl_bool rat1, rat2;
833 if (!map1 || !map2)
834 return isl_bool_error;
836 if (!isl_map_has_equal_space(map1, map2))
837 return isl_bool_false;
839 empty = isl_map_is_empty(map1);
840 if (empty < 0)
841 return isl_bool_error;
842 if (empty)
843 return isl_bool_true;
845 empty = isl_map_is_empty(map2);
846 if (empty < 0)
847 return isl_bool_error;
848 if (empty)
849 return isl_bool_false;
851 rat1 = isl_map_has_rational(map1);
852 rat2 = isl_map_has_rational(map2);
853 if (rat1 < 0 || rat2 < 0)
854 return isl_bool_error;
855 if (rat1 && !rat2)
856 return isl_bool_false;
858 if (isl_map_plain_is_universe(map2))
859 return isl_bool_true;
861 single = isl_map_plain_is_singleton(map1);
862 if (single < 0)
863 return isl_bool_error;
864 map2 = isl_map_compute_divs(isl_map_copy(map2));
865 if (single) {
866 is_subset = map_is_singleton_subset(map1, map2);
867 isl_map_free(map2);
868 return is_subset;
870 is_subset = map_diff_is_empty(map1, map2);
871 isl_map_free(map2);
873 return is_subset;
876 isl_bool isl_map_is_subset(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
878 return isl_map_align_params_map_map_and_test(map1, map2,
879 &map_is_subset);
882 isl_bool isl_set_is_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
884 return isl_map_is_subset(set_to_map(set1), set_to_map(set2));
887 __isl_give isl_map *isl_map_make_disjoint(__isl_take isl_map *map)
889 int i;
890 struct isl_subtract_diff_collector sdc;
891 sdc.dc.add = &basic_map_subtract_add;
893 if (!map)
894 return NULL;
895 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
896 return map;
897 if (map->n <= 1)
898 return map;
900 map = isl_map_compute_divs(map);
901 map = isl_map_remove_empty_parts(map);
903 if (!map || map->n <= 1)
904 return map;
906 sdc.diff = isl_map_from_basic_map(isl_basic_map_copy(map->p[0]));
908 for (i = 1; i < map->n; ++i) {
909 struct isl_basic_map *bmap = isl_basic_map_copy(map->p[i]);
910 struct isl_map *copy = isl_map_copy(sdc.diff);
911 if (basic_map_collect_diff(bmap, copy, &sdc.dc) < 0) {
912 isl_map_free(sdc.diff);
913 sdc.diff = NULL;
914 break;
918 isl_map_free(map);
920 return sdc.diff;
923 __isl_give isl_set *isl_set_make_disjoint(__isl_take isl_set *set)
925 return set_from_map(isl_map_make_disjoint(set_to_map(set)));
928 __isl_give isl_map *isl_map_complement(__isl_take isl_map *map)
930 isl_map *universe;
932 if (!map)
933 return NULL;
935 universe = isl_map_universe(isl_map_get_space(map));
937 return isl_map_subtract(universe, map);
940 __isl_give isl_set *isl_set_complement(__isl_take isl_set *set)
942 return isl_map_complement(set);