isl_transitive_closure.c: is_acyclic: return isl_bool
[isl.git] / isl_point.c
blob92ea9e69987a659889f389f05be3c86fd0cd5e58
1 #include <isl_map_private.h>
2 #include <isl_point_private.h>
3 #include <isl/set.h>
4 #include <isl/union_set.h>
5 #include <isl_sample.h>
6 #include <isl_scan.h>
7 #include <isl_seq.h>
8 #include <isl_space_private.h>
9 #include <isl_local_private.h>
10 #include <isl_val_private.h>
11 #include <isl_vec_private.h>
12 #include <isl_output_private.h>
14 #include <set_to_map.c>
16 isl_ctx *isl_point_get_ctx(__isl_keep isl_point *pnt)
18 return pnt ? isl_space_get_ctx(pnt->dim) : NULL;
21 /* Return the space of "pnt".
23 __isl_keep isl_space *isl_point_peek_space(__isl_keep isl_point *pnt)
25 return pnt ? pnt->dim : NULL;
28 __isl_give isl_space *isl_point_get_space(__isl_keep isl_point *pnt)
30 return isl_space_copy(isl_point_peek_space(pnt));
33 __isl_give isl_point *isl_point_alloc(__isl_take isl_space *space,
34 __isl_take isl_vec *vec)
36 struct isl_point *pnt;
38 if (!space || !vec)
39 goto error;
41 if (vec->size > 1 + isl_space_dim(space, isl_dim_all)) {
42 vec = isl_vec_cow(vec);
43 if (!vec)
44 goto error;
45 vec->size = 1 + isl_space_dim(space, isl_dim_all);
48 pnt = isl_alloc_type(space->ctx, struct isl_point);
49 if (!pnt)
50 goto error;
52 pnt->ref = 1;
53 pnt->dim = space;
54 pnt->vec = vec;
56 return pnt;
57 error:
58 isl_space_free(space);
59 isl_vec_free(vec);
60 return NULL;
63 __isl_give isl_point *isl_point_zero(__isl_take isl_space *space)
65 isl_vec *vec;
67 if (!space)
68 return NULL;
69 vec = isl_vec_alloc(space->ctx, 1 + isl_space_dim(space, isl_dim_all));
70 if (!vec)
71 goto error;
72 isl_int_set_si(vec->el[0], 1);
73 isl_seq_clr(vec->el + 1, vec->size - 1);
74 return isl_point_alloc(space, vec);
75 error:
76 isl_space_free(space);
77 return NULL;
80 __isl_give isl_point *isl_point_dup(__isl_keep isl_point *pnt)
82 struct isl_point *pnt2;
84 if (!pnt)
85 return NULL;
86 pnt2 = isl_point_alloc(isl_space_copy(pnt->dim), isl_vec_copy(pnt->vec));
87 return pnt2;
90 __isl_give isl_point *isl_point_cow(__isl_take isl_point *pnt)
92 struct isl_point *pnt2;
93 if (!pnt)
94 return NULL;
96 if (pnt->ref == 1)
97 return pnt;
99 pnt2 = isl_point_dup(pnt);
100 isl_point_free(pnt);
101 return pnt2;
104 __isl_give isl_point *isl_point_copy(__isl_keep isl_point *pnt)
106 if (!pnt)
107 return NULL;
109 pnt->ref++;
110 return pnt;
113 __isl_null isl_point *isl_point_free(__isl_take isl_point *pnt)
115 if (!pnt)
116 return NULL;
118 if (--pnt->ref > 0)
119 return NULL;
121 isl_space_free(pnt->dim);
122 isl_vec_free(pnt->vec);
123 free(pnt);
124 return NULL;
127 __isl_give isl_point *isl_point_void(__isl_take isl_space *space)
129 if (!space)
130 return NULL;
132 return isl_point_alloc(space, isl_vec_alloc(space->ctx, 0));
135 isl_bool isl_point_is_void(__isl_keep isl_point *pnt)
137 if (!pnt)
138 return isl_bool_error;
140 return pnt->vec->size == 0;
143 /* Return the space of "pnt".
144 * This may be either a copy or the space itself
145 * if there is only one reference to "pnt".
146 * This allows the space to be modified inplace
147 * if both the point and its space have only a single reference.
148 * The caller is not allowed to modify "pnt" between this call and
149 * a subsequent call to isl_point_restore_space.
150 * The only exception is that isl_point_free can be called instead.
152 __isl_give isl_space *isl_point_take_space(__isl_keep isl_point *pnt)
154 isl_space *space;
156 if (!pnt)
157 return NULL;
158 if (pnt->ref != 1)
159 return isl_point_get_space(pnt);
160 space = pnt->dim;
161 pnt->dim = NULL;
162 return space;
165 /* Set the space of "pnt" to "space", where the space of "pnt" may be missing
166 * due to a preceding call to isl_point_take_space.
167 * However, in this case, "pnt" only has a single reference and
168 * then the call to isl_point_cow has no effect.
170 __isl_give isl_point *isl_point_restore_space(__isl_take isl_point *pnt,
171 __isl_take isl_space *space)
173 if (!pnt || !space)
174 goto error;
176 if (pnt->dim == space) {
177 isl_space_free(space);
178 return pnt;
181 pnt = isl_point_cow(pnt);
182 if (!pnt)
183 goto error;
184 isl_space_free(pnt->dim);
185 pnt->dim = space;
187 return pnt;
188 error:
189 isl_point_free(pnt);
190 isl_space_free(space);
191 return NULL;
194 /* Return the coordinate vector of "pnt".
196 __isl_keep isl_vec *isl_point_peek_vec(__isl_keep isl_point *pnt)
198 return pnt ? pnt->vec : NULL;
201 /* Return a copy of the coordinate vector of "pnt".
203 __isl_give isl_vec *isl_point_get_vec(__isl_keep isl_point *pnt)
205 return isl_vec_copy(isl_point_peek_vec(pnt));
208 /* Return the coordinate vector of "pnt".
209 * This may be either a copy or the coordinate vector itself
210 * if there is only one reference to "pnt".
211 * This allows the coordinate vector to be modified inplace
212 * if both the point and its coordinate vector have only a single reference.
213 * The caller is not allowed to modify "pnt" between this call and
214 * a subsequent call to isl_point_restore_vec.
215 * The only exception is that isl_point_free can be called instead.
217 __isl_give isl_vec *isl_point_take_vec(__isl_keep isl_point *pnt)
219 isl_vec *vec;
221 if (!pnt)
222 return NULL;
223 if (pnt->ref != 1)
224 return isl_point_get_vec(pnt);
225 vec = pnt->vec;
226 pnt->vec = NULL;
227 return vec;
230 /* Set the coordinate vector of "pnt" to "vec",
231 * where the coordinate vector of "pnt" may be missing
232 * due to a preceding call to isl_point_take_vec.
233 * However, in this case, "pnt" only has a single reference and
234 * then the call to isl_point_cow has no effect.
236 __isl_give isl_point *isl_point_restore_vec(__isl_take isl_point *pnt,
237 __isl_take isl_vec *vec)
239 if (!pnt || !vec)
240 goto error;
242 if (pnt->vec == vec) {
243 isl_vec_free(vec);
244 return pnt;
247 pnt = isl_point_cow(pnt);
248 if (!pnt)
249 goto error;
250 isl_vec_free(pnt->vec);
251 pnt->vec = vec;
253 return pnt;
254 error:
255 isl_point_free(pnt);
256 isl_vec_free(vec);
257 return NULL;
260 /* Return the number of variables of the given type.
262 static unsigned isl_point_dim(__isl_keep isl_point *pnt, enum isl_dim_type type)
264 return pnt ? isl_space_dim(pnt->dim, type) : 0;
267 /* Return the position of the coordinates of the given type
268 * within the sequence of coordinates of "pnt".
270 static int isl_point_var_offset(__isl_keep isl_point *pnt,
271 enum isl_dim_type type)
273 return pnt ? isl_space_offset(pnt->dim, type) : -1;
276 #undef TYPE
277 #define TYPE isl_point
278 static
279 #include "check_type_range_templ.c"
281 /* Return the value of coordinate "pos" of type "type" of "pnt".
283 __isl_give isl_val *isl_point_get_coordinate_val(__isl_keep isl_point *pnt,
284 enum isl_dim_type type, int pos)
286 isl_ctx *ctx;
287 isl_val *v;
288 int off;
290 if (!pnt)
291 return NULL;
293 ctx = isl_point_get_ctx(pnt);
294 if (isl_point_is_void(pnt))
295 isl_die(ctx, isl_error_invalid,
296 "void point does not have coordinates", return NULL);
297 if (isl_point_check_range(pnt, type, pos, 1) < 0)
298 return NULL;
300 off = isl_point_var_offset(pnt, type);
301 if (off < 0)
302 return NULL;
303 pos += off;
305 v = isl_val_rat_from_isl_int(ctx, pnt->vec->el[1 + pos],
306 pnt->vec->el[0]);
307 return isl_val_normalize(v);
310 /* Replace coordinate "pos" of type "type" of "pnt" by "v".
312 __isl_give isl_point *isl_point_set_coordinate_val(__isl_take isl_point *pnt,
313 enum isl_dim_type type, int pos, __isl_take isl_val *v)
315 if (!pnt || !v)
316 goto error;
317 if (isl_point_is_void(pnt))
318 isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
319 "void point does not have coordinates", goto error);
320 if (isl_point_check_range(pnt, type, pos, 1) < 0)
321 goto error;
322 if (!isl_val_is_rat(v))
323 isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
324 "expecting rational value", goto error);
326 if (isl_int_eq(pnt->vec->el[1 + pos], v->n) &&
327 isl_int_eq(pnt->vec->el[0], v->d)) {
328 isl_val_free(v);
329 return pnt;
332 pnt = isl_point_cow(pnt);
333 if (!pnt)
334 goto error;
335 pnt->vec = isl_vec_cow(pnt->vec);
336 if (!pnt->vec)
337 goto error;
339 if (isl_int_eq(pnt->vec->el[0], v->d)) {
340 isl_int_set(pnt->vec->el[1 + pos], v->n);
341 } else if (isl_int_is_one(v->d)) {
342 isl_int_mul(pnt->vec->el[1 + pos], pnt->vec->el[0], v->n);
343 } else {
344 isl_seq_scale(pnt->vec->el + 1,
345 pnt->vec->el + 1, v->d, pnt->vec->size - 1);
346 isl_int_mul(pnt->vec->el[1 + pos], pnt->vec->el[0], v->n);
347 isl_int_mul(pnt->vec->el[0], pnt->vec->el[0], v->d);
348 pnt->vec = isl_vec_normalize(pnt->vec);
349 if (!pnt->vec)
350 goto error;
353 isl_val_free(v);
354 return pnt;
355 error:
356 isl_val_free(v);
357 isl_point_free(pnt);
358 return NULL;
361 __isl_give isl_point *isl_point_add_ui(__isl_take isl_point *pnt,
362 enum isl_dim_type type, int pos, unsigned val)
364 int off;
366 if (!pnt || isl_point_is_void(pnt))
367 return pnt;
369 pnt = isl_point_cow(pnt);
370 if (!pnt)
371 return NULL;
372 pnt->vec = isl_vec_cow(pnt->vec);
373 if (!pnt->vec)
374 goto error;
376 off = isl_point_var_offset(pnt, type);
377 if (off < 0)
378 goto error;
379 pos += off;
381 isl_int_add_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
383 return pnt;
384 error:
385 isl_point_free(pnt);
386 return NULL;
389 __isl_give isl_point *isl_point_sub_ui(__isl_take isl_point *pnt,
390 enum isl_dim_type type, int pos, unsigned val)
392 int off;
394 if (!pnt || isl_point_is_void(pnt))
395 return pnt;
397 pnt = isl_point_cow(pnt);
398 if (!pnt)
399 return NULL;
400 pnt->vec = isl_vec_cow(pnt->vec);
401 if (!pnt->vec)
402 goto error;
404 off = isl_point_var_offset(pnt, type);
405 if (off < 0)
406 goto error;
407 pos += off;
409 isl_int_sub_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
411 return pnt;
412 error:
413 isl_point_free(pnt);
414 return NULL;
417 struct isl_foreach_point {
418 struct isl_scan_callback callback;
419 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
420 void *user;
421 isl_space *dim;
424 static isl_stat foreach_point(struct isl_scan_callback *cb,
425 __isl_take isl_vec *sample)
427 struct isl_foreach_point *fp = (struct isl_foreach_point *)cb;
428 isl_point *pnt;
430 pnt = isl_point_alloc(isl_space_copy(fp->dim), sample);
432 return fp->fn(pnt, fp->user);
435 isl_stat isl_set_foreach_point(__isl_keep isl_set *set,
436 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
438 struct isl_foreach_point fp = { { &foreach_point }, fn, user };
439 int i;
441 if (!set)
442 return isl_stat_error;
444 fp.dim = isl_set_get_space(set);
445 if (!fp.dim)
446 return isl_stat_error;
448 set = isl_set_copy(set);
449 set = isl_set_cow(set);
450 set = isl_set_make_disjoint(set);
451 set = isl_set_compute_divs(set);
452 if (!set)
453 goto error;
455 for (i = 0; i < set->n; ++i)
456 if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
457 &fp.callback) < 0)
458 goto error;
460 isl_set_free(set);
461 isl_space_free(fp.dim);
463 return isl_stat_ok;
464 error:
465 isl_set_free(set);
466 isl_space_free(fp.dim);
467 return isl_stat_error;
470 /* Return 1 if "bmap" contains the point "point".
471 * "bmap" is assumed to have known divs.
472 * The point is first extended with the divs and then passed
473 * to basic_map_contains.
475 isl_bool isl_basic_map_contains_point(__isl_keep isl_basic_map *bmap,
476 __isl_keep isl_point *point)
478 isl_local *local;
479 isl_vec *vec;
480 isl_bool contains;
482 if (!bmap || !point)
483 return isl_bool_error;
484 isl_assert(bmap->ctx, isl_space_is_equal(bmap->dim, point->dim),
485 return isl_bool_error);
486 if (bmap->n_div == 0)
487 return isl_basic_map_contains(bmap, point->vec);
489 local = isl_local_alloc_from_mat(isl_basic_map_get_divs(bmap));
490 vec = isl_point_get_vec(point);
491 vec = isl_local_extend_point_vec(local, vec);
492 isl_local_free(local);
494 contains = isl_basic_map_contains(bmap, vec);
496 isl_vec_free(vec);
497 return contains;
500 isl_bool isl_map_contains_point(__isl_keep isl_map *map,
501 __isl_keep isl_point *point)
503 int i;
504 isl_bool found = isl_bool_false;
506 if (!map || !point)
507 return isl_bool_error;
509 map = isl_map_copy(map);
510 map = isl_map_compute_divs(map);
511 if (!map)
512 return isl_bool_error;
514 for (i = 0; i < map->n; ++i) {
515 found = isl_basic_map_contains_point(map->p[i], point);
516 if (found < 0)
517 goto error;
518 if (found)
519 break;
521 isl_map_free(map);
523 return found;
524 error:
525 isl_map_free(map);
526 return isl_bool_error;
529 isl_bool isl_set_contains_point(__isl_keep isl_set *set,
530 __isl_keep isl_point *point)
532 return isl_map_contains_point(set_to_map(set), point);
535 __isl_give isl_basic_set *isl_basic_set_from_point(__isl_take isl_point *pnt)
537 isl_basic_set *bset;
538 isl_basic_set *model;
540 if (!pnt)
541 return NULL;
543 model = isl_basic_set_empty(isl_space_copy(pnt->dim));
544 bset = isl_basic_set_from_vec(isl_vec_copy(pnt->vec));
545 bset = isl_basic_set_from_underlying_set(bset, model);
546 isl_point_free(pnt);
548 return bset;
551 __isl_give isl_set *isl_set_from_point(__isl_take isl_point *pnt)
553 isl_basic_set *bset;
554 bset = isl_basic_set_from_point(pnt);
555 return isl_set_from_basic_set(bset);
558 /* Construct a union set, containing the single element "pnt".
559 * If "pnt" is void, then return an empty union set.
561 __isl_give isl_union_set *isl_union_set_from_point(__isl_take isl_point *pnt)
563 if (!pnt)
564 return NULL;
565 if (isl_point_is_void(pnt)) {
566 isl_space *space;
568 space = isl_point_get_space(pnt);
569 isl_point_free(pnt);
570 return isl_union_set_empty(space);
573 return isl_union_set_from_set(isl_set_from_point(pnt));
576 __isl_give isl_basic_set *isl_basic_set_box_from_points(
577 __isl_take isl_point *pnt1, __isl_take isl_point *pnt2)
579 isl_basic_set *bset = NULL;
580 unsigned total;
581 int i;
582 int k;
583 isl_int t;
585 isl_int_init(t);
587 if (!pnt1 || !pnt2)
588 goto error;
590 isl_assert(pnt1->dim->ctx,
591 isl_space_is_equal(pnt1->dim, pnt2->dim), goto error);
593 if (isl_point_is_void(pnt1) && isl_point_is_void(pnt2)) {
594 isl_space *dim = isl_space_copy(pnt1->dim);
595 isl_point_free(pnt1);
596 isl_point_free(pnt2);
597 isl_int_clear(t);
598 return isl_basic_set_empty(dim);
600 if (isl_point_is_void(pnt1)) {
601 isl_point_free(pnt1);
602 isl_int_clear(t);
603 return isl_basic_set_from_point(pnt2);
605 if (isl_point_is_void(pnt2)) {
606 isl_point_free(pnt2);
607 isl_int_clear(t);
608 return isl_basic_set_from_point(pnt1);
611 total = isl_point_dim(pnt1, isl_dim_all);
612 bset = isl_basic_set_alloc_space(isl_space_copy(pnt1->dim), 0, 0, 2 * total);
614 for (i = 0; i < total; ++i) {
615 isl_int_mul(t, pnt1->vec->el[1 + i], pnt2->vec->el[0]);
616 isl_int_submul(t, pnt2->vec->el[1 + i], pnt1->vec->el[0]);
618 k = isl_basic_set_alloc_inequality(bset);
619 if (k < 0)
620 goto error;
621 isl_seq_clr(bset->ineq[k] + 1, total);
622 if (isl_int_is_pos(t)) {
623 isl_int_set_si(bset->ineq[k][1 + i], -1);
624 isl_int_set(bset->ineq[k][0], pnt1->vec->el[1 + i]);
625 } else {
626 isl_int_set_si(bset->ineq[k][1 + i], 1);
627 isl_int_neg(bset->ineq[k][0], pnt1->vec->el[1 + i]);
629 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt1->vec->el[0]);
631 k = isl_basic_set_alloc_inequality(bset);
632 if (k < 0)
633 goto error;
634 isl_seq_clr(bset->ineq[k] + 1, total);
635 if (isl_int_is_pos(t)) {
636 isl_int_set_si(bset->ineq[k][1 + i], 1);
637 isl_int_neg(bset->ineq[k][0], pnt2->vec->el[1 + i]);
638 } else {
639 isl_int_set_si(bset->ineq[k][1 + i], -1);
640 isl_int_set(bset->ineq[k][0], pnt2->vec->el[1 + i]);
642 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt2->vec->el[0]);
645 bset = isl_basic_set_finalize(bset);
647 isl_point_free(pnt1);
648 isl_point_free(pnt2);
650 isl_int_clear(t);
652 return bset;
653 error:
654 isl_point_free(pnt1);
655 isl_point_free(pnt2);
656 isl_int_clear(t);
657 isl_basic_set_free(bset);
658 return NULL;
661 __isl_give isl_set *isl_set_box_from_points(__isl_take isl_point *pnt1,
662 __isl_take isl_point *pnt2)
664 isl_basic_set *bset;
665 bset = isl_basic_set_box_from_points(pnt1, pnt2);
666 return isl_set_from_basic_set(bset);
669 /* Print the coordinate at position "pos" of the point "pnt".
671 static __isl_give isl_printer *print_coordinate(__isl_take isl_printer *p,
672 struct isl_print_space_data *data, unsigned pos)
674 isl_point *pnt = data->user;
676 p = isl_printer_print_isl_int(p, pnt->vec->el[1 + pos]);
677 if (!isl_int_is_one(pnt->vec->el[0])) {
678 p = isl_printer_print_str(p, "/");
679 p = isl_printer_print_isl_int(p, pnt->vec->el[0]);
682 return p;
685 __isl_give isl_printer *isl_printer_print_point(
686 __isl_take isl_printer *p, __isl_keep isl_point *pnt)
688 struct isl_print_space_data data = { 0 };
689 int i;
690 unsigned nparam;
692 if (!pnt)
693 return p;
694 if (isl_point_is_void(pnt)) {
695 p = isl_printer_print_str(p, "void");
696 return p;
699 nparam = isl_point_dim(pnt, isl_dim_param);
700 if (nparam > 0) {
701 p = isl_printer_print_str(p, "[");
702 for (i = 0; i < nparam; ++i) {
703 const char *name;
704 if (i)
705 p = isl_printer_print_str(p, ", ");
706 name = isl_space_get_dim_name(pnt->dim, isl_dim_param, i);
707 if (name) {
708 p = isl_printer_print_str(p, name);
709 p = isl_printer_print_str(p, " = ");
711 p = isl_printer_print_isl_int(p, pnt->vec->el[1 + i]);
712 if (!isl_int_is_one(pnt->vec->el[0])) {
713 p = isl_printer_print_str(p, "/");
714 p = isl_printer_print_isl_int(p, pnt->vec->el[0]);
717 p = isl_printer_print_str(p, "]");
718 p = isl_printer_print_str(p, " -> ");
720 data.print_dim = &print_coordinate;
721 data.user = pnt;
722 p = isl_printer_print_str(p, "{ ");
723 p = isl_print_space(pnt->dim, p, 0, &data);
724 p = isl_printer_print_str(p, " }");
725 return p;