isl_multi_templ.c: extract out isl_multi_product_templ.c
[isl.git] / isl_map_subtract.c
blob1ef50ae5e70cf507b45dea40c968f0d9c27cfd99
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_add(vec->el[2 + dim + (*div_map)[j]],
191 vec->el[2 + dim + (*div_map)[j]],
192 bmap->div[i][2 + dim + j]);
193 for (j = 0; j < tab->bmap->n_div; ++j)
194 if (isl_seq_eq(tab->bmap->div[j],
195 vec->el, 2 + dim + tab->bmap->n_div))
196 break;
197 (*div_map)[i] = j;
198 if (j == tab->bmap->n_div) {
199 vec->size = 2 + dim + tab->bmap->n_div;
200 if (isl_tab_add_div(tab, vec) < 0)
201 goto error;
205 isl_vec_free(vec);
207 return isl_stat_ok;
208 error:
209 isl_vec_free(vec);
211 return isl_stat_error;
214 /* Freeze all constraints of tableau tab.
216 static int tab_freeze_constraints(struct isl_tab *tab)
218 int i;
220 for (i = 0; i < tab->n_con; ++i)
221 if (isl_tab_freeze_constraint(tab, i) < 0)
222 return -1;
224 return 0;
227 /* Check for redundant constraints starting at offset.
228 * Put the indices of the redundant constraints in index
229 * and return the number of redundant constraints.
231 static int n_non_redundant(isl_ctx *ctx, struct isl_tab *tab,
232 int offset, int **index)
234 int i, n;
235 int n_test = tab->n_con - offset;
237 if (isl_tab_detect_redundant(tab) < 0)
238 return -1;
240 if (n_test == 0)
241 return 0;
242 if (!*index)
243 *index = isl_alloc_array(ctx, int, n_test);
244 if (!*index)
245 return -1;
247 for (n = 0, i = 0; i < n_test; ++i) {
248 int r;
249 r = isl_tab_is_redundant(tab, offset + i);
250 if (r < 0)
251 return -1;
252 if (r)
253 continue;
254 (*index)[n++] = i;
257 return n;
260 /* basic_map_collect_diff calls add on each of the pieces of
261 * the set difference between bmap and map until the add method
262 * return a negative value.
264 struct isl_diff_collector {
265 isl_stat (*add)(struct isl_diff_collector *dc,
266 __isl_take isl_basic_map *bmap);
269 /* Compute the set difference between bmap and map and call
270 * dc->add on each of the piece until this function returns
271 * a negative value.
272 * Return 0 on success and -1 on error. dc->add returning
273 * a negative value is treated as an error, but the calling
274 * function can interpret the results based on the state of dc.
276 * Assumes that map has known divs.
278 * The difference is computed by a backtracking algorithm.
279 * Each level corresponds to a basic map in "map".
280 * When a node in entered for the first time, we check
281 * if the corresonding basic map intersects the current piece
282 * of "bmap". If not, we move to the next level.
283 * Otherwise, we split the current piece into as many
284 * pieces as there are non-redundant constraints of the current
285 * basic map in the intersection. Each of these pieces is
286 * handled by a child of the current node.
287 * In particular, if there are n non-redundant constraints,
288 * then for each 0 <= i < n, a piece is cut off by adding
289 * constraints 0 <= j < i and adding the opposite of constraint i.
290 * If there are no non-redundant constraints, meaning that the current
291 * piece is a subset of the current basic map, then we simply backtrack.
293 * In the leaves, we check if the remaining piece has any integer points
294 * and if so, pass it along to dc->add. As a special case, if nothing
295 * has been removed when we end up in a leaf, we simply pass along
296 * the original basic map.
298 static isl_stat basic_map_collect_diff(__isl_take isl_basic_map *bmap,
299 __isl_take isl_map *map, struct isl_diff_collector *dc)
301 int i;
302 int modified;
303 int level;
304 int init;
305 isl_bool empty;
306 isl_ctx *ctx;
307 struct isl_tab *tab = NULL;
308 struct isl_tab_undo **snap = NULL;
309 int *k = NULL;
310 int *n = NULL;
311 int **index = NULL;
312 int **div_map = NULL;
314 empty = isl_basic_map_is_empty(bmap);
315 if (empty) {
316 isl_basic_map_free(bmap);
317 isl_map_free(map);
318 return empty < 0 ? isl_stat_error : isl_stat_ok;
321 bmap = isl_basic_map_cow(bmap);
322 map = isl_map_cow(map);
324 if (!bmap || !map)
325 goto error;
327 ctx = map->ctx;
328 snap = isl_alloc_array(map->ctx, struct isl_tab_undo *, map->n);
329 k = isl_alloc_array(map->ctx, int, map->n);
330 n = isl_alloc_array(map->ctx, int, map->n);
331 index = isl_calloc_array(map->ctx, int *, map->n);
332 div_map = isl_calloc_array(map->ctx, int *, map->n);
333 if (!snap || !k || !n || !index || !div_map)
334 goto error;
336 bmap = isl_basic_map_order_divs(bmap);
337 map = isl_map_order_divs(map);
339 tab = isl_tab_from_basic_map(bmap, 1);
340 if (!tab)
341 goto error;
343 modified = 0;
344 level = 0;
345 init = 1;
347 while (level >= 0) {
348 if (level >= map->n) {
349 int empty;
350 struct isl_basic_map *bm;
351 if (!modified) {
352 if (dc->add(dc, isl_basic_map_copy(bmap)) < 0)
353 goto error;
354 break;
356 bm = isl_basic_map_copy(tab->bmap);
357 bm = isl_basic_map_cow(bm);
358 bm = isl_basic_map_update_from_tab(bm, tab);
359 bm = isl_basic_map_simplify(bm);
360 bm = isl_basic_map_finalize(bm);
361 empty = isl_basic_map_is_empty(bm);
362 if (empty)
363 isl_basic_map_free(bm);
364 else if (dc->add(dc, bm) < 0)
365 goto error;
366 if (empty < 0)
367 goto error;
368 level--;
369 init = 0;
370 continue;
372 if (init) {
373 int offset;
374 struct isl_tab_undo *snap2;
375 snap2 = isl_tab_snap(tab);
376 if (tab_add_divs(tab, map->p[level],
377 &div_map[level]) < 0)
378 goto error;
379 offset = tab->n_con;
380 snap[level] = isl_tab_snap(tab);
381 if (tab_freeze_constraints(tab) < 0)
382 goto error;
383 if (tab_add_constraints(tab, map->p[level],
384 div_map[level]) < 0)
385 goto error;
386 k[level] = 0;
387 n[level] = 0;
388 if (tab->empty) {
389 if (isl_tab_rollback(tab, snap2) < 0)
390 goto error;
391 level++;
392 continue;
394 modified = 1;
395 n[level] = n_non_redundant(ctx, tab, offset,
396 &index[level]);
397 if (n[level] < 0)
398 goto error;
399 if (n[level] == 0) {
400 level--;
401 init = 0;
402 continue;
404 if (isl_tab_rollback(tab, snap[level]) < 0)
405 goto error;
406 if (tab_add_constraint(tab, map->p[level],
407 div_map[level], index[level][0], 1) < 0)
408 goto error;
409 level++;
410 continue;
411 } else {
412 if (k[level] + 1 >= n[level]) {
413 level--;
414 continue;
416 if (isl_tab_rollback(tab, snap[level]) < 0)
417 goto error;
418 if (tab_add_constraint(tab, map->p[level],
419 div_map[level],
420 index[level][k[level]], 0) < 0)
421 goto error;
422 snap[level] = isl_tab_snap(tab);
423 k[level]++;
424 if (tab_add_constraint(tab, map->p[level],
425 div_map[level],
426 index[level][k[level]], 1) < 0)
427 goto error;
428 level++;
429 init = 1;
430 continue;
434 isl_tab_free(tab);
435 free(snap);
436 free(n);
437 free(k);
438 for (i = 0; index && i < map->n; ++i)
439 free(index[i]);
440 free(index);
441 for (i = 0; div_map && i < map->n; ++i)
442 free(div_map[i]);
443 free(div_map);
445 isl_basic_map_free(bmap);
446 isl_map_free(map);
448 return isl_stat_ok;
449 error:
450 isl_tab_free(tab);
451 free(snap);
452 free(n);
453 free(k);
454 for (i = 0; index && i < map->n; ++i)
455 free(index[i]);
456 free(index);
457 for (i = 0; div_map && i < map->n; ++i)
458 free(div_map[i]);
459 free(div_map);
460 isl_basic_map_free(bmap);
461 isl_map_free(map);
462 return isl_stat_error;
465 /* A diff collector that actually collects all parts of the
466 * set difference in the field diff.
468 struct isl_subtract_diff_collector {
469 struct isl_diff_collector dc;
470 struct isl_map *diff;
473 /* isl_subtract_diff_collector callback.
475 static isl_stat basic_map_subtract_add(struct isl_diff_collector *dc,
476 __isl_take isl_basic_map *bmap)
478 struct isl_subtract_diff_collector *sdc;
479 sdc = (struct isl_subtract_diff_collector *)dc;
481 sdc->diff = isl_map_union_disjoint(sdc->diff,
482 isl_map_from_basic_map(bmap));
484 return sdc->diff ? isl_stat_ok : isl_stat_error;
487 /* Return the set difference between bmap and map.
489 static __isl_give isl_map *basic_map_subtract(__isl_take isl_basic_map *bmap,
490 __isl_take isl_map *map)
492 struct isl_subtract_diff_collector sdc;
493 sdc.dc.add = &basic_map_subtract_add;
494 sdc.diff = isl_map_empty(isl_basic_map_get_space(bmap));
495 if (basic_map_collect_diff(bmap, map, &sdc.dc) < 0) {
496 isl_map_free(sdc.diff);
497 sdc.diff = NULL;
499 return sdc.diff;
502 /* Return an empty map living in the same space as "map1" and "map2".
504 static __isl_give isl_map *replace_pair_by_empty( __isl_take isl_map *map1,
505 __isl_take isl_map *map2)
507 isl_space *space;
509 space = isl_map_get_space(map1);
510 isl_map_free(map1);
511 isl_map_free(map2);
512 return isl_map_empty(space);
515 /* Return the set difference between map1 and map2.
516 * (U_i A_i) \ (U_j B_j) is computed as U_i (A_i \ (U_j B_j))
518 * If "map1" and "map2" are obviously equal to each other,
519 * then return an empty map in the same space.
521 * If "map1" and "map2" are disjoint, then simply return "map1".
523 static __isl_give isl_map *map_subtract( __isl_take isl_map *map1,
524 __isl_take isl_map *map2)
526 int i;
527 int equal, disjoint;
528 struct isl_map *diff;
530 if (!map1 || !map2)
531 goto error;
533 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
535 equal = isl_map_plain_is_equal(map1, map2);
536 if (equal < 0)
537 goto error;
538 if (equal)
539 return replace_pair_by_empty(map1, map2);
541 disjoint = isl_map_is_disjoint(map1, map2);
542 if (disjoint < 0)
543 goto error;
544 if (disjoint) {
545 isl_map_free(map2);
546 return map1;
549 map1 = isl_map_compute_divs(map1);
550 map2 = isl_map_compute_divs(map2);
551 if (!map1 || !map2)
552 goto error;
554 map1 = isl_map_remove_empty_parts(map1);
555 map2 = isl_map_remove_empty_parts(map2);
557 diff = isl_map_empty(isl_map_get_space(map1));
558 for (i = 0; i < map1->n; ++i) {
559 struct isl_map *d;
560 d = basic_map_subtract(isl_basic_map_copy(map1->p[i]),
561 isl_map_copy(map2));
562 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT))
563 diff = isl_map_union_disjoint(diff, d);
564 else
565 diff = isl_map_union(diff, d);
568 isl_map_free(map1);
569 isl_map_free(map2);
571 return diff;
572 error:
573 isl_map_free(map1);
574 isl_map_free(map2);
575 return NULL;
578 __isl_give isl_map *isl_map_subtract( __isl_take isl_map *map1,
579 __isl_take isl_map *map2)
581 return isl_map_align_params_map_map_and(map1, map2, &map_subtract);
584 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
586 return set_from_map(isl_map_subtract(set_to_map(set1),
587 set_to_map(set2)));
590 /* Remove the elements of "dom" from the domain of "map".
592 static __isl_give isl_map *map_subtract_domain(__isl_take isl_map *map,
593 __isl_take isl_set *dom)
595 isl_bool ok;
596 isl_map *ext_dom;
598 ok = isl_map_compatible_domain(map, dom);
599 if (ok < 0)
600 goto error;
601 if (!ok)
602 isl_die(isl_set_get_ctx(dom), isl_error_invalid,
603 "incompatible spaces", goto error);
605 ext_dom = isl_map_universe(isl_map_get_space(map));
606 ext_dom = isl_map_intersect_domain(ext_dom, dom);
607 return isl_map_subtract(map, ext_dom);
608 error:
609 isl_map_free(map);
610 isl_set_free(dom);
611 return NULL;
614 __isl_give isl_map *isl_map_subtract_domain(__isl_take isl_map *map,
615 __isl_take isl_set *dom)
617 return isl_map_align_params_map_map_and(map, dom, &map_subtract_domain);
620 /* Remove the elements of "dom" from the range of "map".
622 static __isl_give isl_map *map_subtract_range(__isl_take isl_map *map,
623 __isl_take isl_set *dom)
625 isl_bool ok;
626 isl_map *ext_dom;
628 ok = isl_map_compatible_range(map, dom);
629 if (ok < 0)
630 goto error;
631 if (!ok)
632 isl_die(isl_set_get_ctx(dom), isl_error_invalid,
633 "incompatible spaces", goto error);
635 ext_dom = isl_map_universe(isl_map_get_space(map));
636 ext_dom = isl_map_intersect_range(ext_dom, dom);
637 return isl_map_subtract(map, ext_dom);
638 error:
639 isl_map_free(map);
640 isl_set_free(dom);
641 return NULL;
644 __isl_give isl_map *isl_map_subtract_range(__isl_take isl_map *map,
645 __isl_take isl_set *dom)
647 return isl_map_align_params_map_map_and(map, dom, &map_subtract_range);
650 /* A diff collector that aborts as soon as its add function is called,
651 * setting empty to 0.
653 struct isl_is_empty_diff_collector {
654 struct isl_diff_collector dc;
655 isl_bool empty;
658 /* isl_is_empty_diff_collector callback.
660 static isl_stat basic_map_is_empty_add(struct isl_diff_collector *dc,
661 __isl_take isl_basic_map *bmap)
663 struct isl_is_empty_diff_collector *edc;
664 edc = (struct isl_is_empty_diff_collector *)dc;
666 edc->empty = 0;
668 isl_basic_map_free(bmap);
669 return isl_stat_error;
672 /* Check if bmap \ map is empty by computing this set difference
673 * and breaking off as soon as the difference is known to be non-empty.
675 static isl_bool basic_map_diff_is_empty(__isl_keep isl_basic_map *bmap,
676 __isl_keep isl_map *map)
678 isl_bool empty;
679 isl_stat r;
680 struct isl_is_empty_diff_collector edc;
682 empty = isl_basic_map_plain_is_empty(bmap);
683 if (empty)
684 return empty;
686 edc.dc.add = &basic_map_is_empty_add;
687 edc.empty = isl_bool_true;
688 r = basic_map_collect_diff(isl_basic_map_copy(bmap),
689 isl_map_copy(map), &edc.dc);
690 if (!edc.empty)
691 return isl_bool_false;
693 return r < 0 ? isl_bool_error : isl_bool_true;
696 /* Check if map1 \ map2 is empty by checking if the set difference is empty
697 * for each of the basic maps in map1.
699 static isl_bool map_diff_is_empty(__isl_keep isl_map *map1,
700 __isl_keep isl_map *map2)
702 int i;
703 isl_bool is_empty = isl_bool_true;
705 if (!map1 || !map2)
706 return isl_bool_error;
708 for (i = 0; i < map1->n; ++i) {
709 is_empty = basic_map_diff_is_empty(map1->p[i], map2);
710 if (is_empty < 0 || !is_empty)
711 break;
714 return is_empty;
717 /* Return true if "bmap" contains a single element.
719 isl_bool isl_basic_map_plain_is_singleton(__isl_keep isl_basic_map *bmap)
721 if (!bmap)
722 return isl_bool_error;
723 if (bmap->n_div)
724 return isl_bool_false;
725 if (bmap->n_ineq)
726 return isl_bool_false;
727 return bmap->n_eq == isl_basic_map_total_dim(bmap);
730 /* Return true if "map" contains a single element.
732 isl_bool isl_map_plain_is_singleton(__isl_keep isl_map *map)
734 if (!map)
735 return isl_bool_error;
736 if (map->n != 1)
737 return isl_bool_false;
739 return isl_basic_map_plain_is_singleton(map->p[0]);
742 /* Given a singleton basic map, extract the single element
743 * as an isl_point.
745 static __isl_give isl_point *singleton_extract_point(
746 __isl_keep isl_basic_map *bmap)
748 int j;
749 unsigned dim;
750 struct isl_vec *point;
751 isl_int m;
753 if (!bmap)
754 return NULL;
756 dim = isl_basic_map_total_dim(bmap);
757 isl_assert(bmap->ctx, bmap->n_eq == dim, return NULL);
758 point = isl_vec_alloc(bmap->ctx, 1 + dim);
759 if (!point)
760 return NULL;
762 isl_int_init(m);
764 isl_int_set_si(point->el[0], 1);
765 for (j = 0; j < bmap->n_eq; ++j) {
766 int i = dim - 1 - j;
767 isl_assert(bmap->ctx,
768 isl_seq_first_non_zero(bmap->eq[j] + 1, i) == -1,
769 goto error);
770 isl_assert(bmap->ctx,
771 isl_int_is_one(bmap->eq[j][1 + i]) ||
772 isl_int_is_negone(bmap->eq[j][1 + i]),
773 goto error);
774 isl_assert(bmap->ctx,
775 isl_seq_first_non_zero(bmap->eq[j]+1+i+1, dim-i-1) == -1,
776 goto error);
778 isl_int_gcd(m, point->el[0], bmap->eq[j][1 + i]);
779 isl_int_divexact(m, bmap->eq[j][1 + i], m);
780 isl_int_abs(m, m);
781 isl_seq_scale(point->el, point->el, m, 1 + i);
782 isl_int_divexact(m, point->el[0], bmap->eq[j][1 + i]);
783 isl_int_neg(m, m);
784 isl_int_mul(point->el[1 + i], m, bmap->eq[j][0]);
787 isl_int_clear(m);
788 return isl_point_alloc(isl_basic_map_get_space(bmap), point);
789 error:
790 isl_int_clear(m);
791 isl_vec_free(point);
792 return NULL;
795 /* Return isl_bool_true if the singleton map "map1" is a subset of "map2",
796 * i.e., if the single element of "map1" is also an element of "map2".
797 * Assumes "map2" has known divs.
799 static isl_bool map_is_singleton_subset(__isl_keep isl_map *map1,
800 __isl_keep isl_map *map2)
802 int i;
803 isl_bool is_subset = isl_bool_false;
804 struct isl_point *point;
806 if (!map1 || !map2)
807 return isl_bool_error;
808 if (map1->n != 1)
809 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
810 "expecting single-disjunct input",
811 return isl_bool_error);
813 point = singleton_extract_point(map1->p[0]);
814 if (!point)
815 return isl_bool_error;
817 for (i = 0; i < map2->n; ++i) {
818 is_subset = isl_basic_map_contains_point(map2->p[i], point);
819 if (is_subset)
820 break;
823 isl_point_free(point);
824 return is_subset;
827 static isl_bool map_is_subset(__isl_keep isl_map *map1,
828 __isl_keep isl_map *map2)
830 isl_bool is_subset = isl_bool_false;
831 isl_bool empty, single;
832 isl_bool rat1, rat2;
834 if (!map1 || !map2)
835 return isl_bool_error;
837 if (!isl_map_has_equal_space(map1, map2))
838 return isl_bool_false;
840 empty = isl_map_is_empty(map1);
841 if (empty < 0)
842 return isl_bool_error;
843 if (empty)
844 return isl_bool_true;
846 empty = isl_map_is_empty(map2);
847 if (empty < 0)
848 return isl_bool_error;
849 if (empty)
850 return isl_bool_false;
852 rat1 = isl_map_has_rational(map1);
853 rat2 = isl_map_has_rational(map2);
854 if (rat1 < 0 || rat2 < 0)
855 return isl_bool_error;
856 if (rat1 && !rat2)
857 return isl_bool_false;
859 if (isl_map_plain_is_universe(map2))
860 return isl_bool_true;
862 single = isl_map_plain_is_singleton(map1);
863 if (single < 0)
864 return isl_bool_error;
865 map2 = isl_map_compute_divs(isl_map_copy(map2));
866 if (single) {
867 is_subset = map_is_singleton_subset(map1, map2);
868 isl_map_free(map2);
869 return is_subset;
871 is_subset = map_diff_is_empty(map1, map2);
872 isl_map_free(map2);
874 return is_subset;
877 isl_bool isl_map_is_subset(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
879 return isl_map_align_params_map_map_and_test(map1, map2,
880 &map_is_subset);
883 isl_bool isl_set_is_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
885 return isl_map_is_subset(set_to_map(set1), set_to_map(set2));
888 __isl_give isl_map *isl_map_make_disjoint(__isl_take isl_map *map)
890 int i;
891 struct isl_subtract_diff_collector sdc;
892 sdc.dc.add = &basic_map_subtract_add;
894 if (!map)
895 return NULL;
896 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
897 return map;
898 if (map->n <= 1)
899 return map;
901 map = isl_map_compute_divs(map);
902 map = isl_map_remove_empty_parts(map);
904 if (!map || map->n <= 1)
905 return map;
907 sdc.diff = isl_map_from_basic_map(isl_basic_map_copy(map->p[0]));
909 for (i = 1; i < map->n; ++i) {
910 struct isl_basic_map *bmap = isl_basic_map_copy(map->p[i]);
911 struct isl_map *copy = isl_map_copy(sdc.diff);
912 if (basic_map_collect_diff(bmap, copy, &sdc.dc) < 0) {
913 isl_map_free(sdc.diff);
914 sdc.diff = NULL;
915 break;
919 isl_map_free(map);
921 return sdc.diff;
924 __isl_give isl_set *isl_set_make_disjoint(__isl_take isl_set *set)
926 return set_from_map(isl_map_make_disjoint(set_to_map(set)));
929 __isl_give isl_map *isl_map_complement(__isl_take isl_map *map)
931 isl_map *universe;
933 if (!map)
934 return NULL;
936 universe = isl_map_universe(isl_map_get_space(map));
938 return isl_map_subtract(universe, map);
941 __isl_give isl_set *isl_set_complement(__isl_take isl_set *set)
943 return isl_map_complement(set);