add isl::checked::boolean::error()
[isl.git] / isl_point.c
blob423507e045f48feefb59db62d335ebaa851009f7
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;
37 isl_size dim;
39 dim = isl_space_dim(space, isl_dim_all);
40 if (dim < 0 || !vec)
41 goto error;
43 if (vec->size > 1 + dim) {
44 vec = isl_vec_cow(vec);
45 if (!vec)
46 goto error;
47 vec->size = 1 + dim;
50 pnt = isl_alloc_type(space->ctx, struct isl_point);
51 if (!pnt)
52 goto error;
54 pnt->ref = 1;
55 pnt->dim = space;
56 pnt->vec = vec;
58 return pnt;
59 error:
60 isl_space_free(space);
61 isl_vec_free(vec);
62 return NULL;
65 __isl_give isl_point *isl_point_zero(__isl_take isl_space *space)
67 isl_vec *vec;
68 isl_size dim;
70 dim = isl_space_dim(space, isl_dim_all);
71 if (dim < 0)
72 goto error;
73 vec = isl_vec_alloc(space->ctx, 1 + dim);
74 if (!vec)
75 goto error;
76 isl_int_set_si(vec->el[0], 1);
77 isl_seq_clr(vec->el + 1, vec->size - 1);
78 return isl_point_alloc(space, vec);
79 error:
80 isl_space_free(space);
81 return NULL;
84 __isl_give isl_point *isl_point_dup(__isl_keep isl_point *pnt)
86 struct isl_point *pnt2;
88 if (!pnt)
89 return NULL;
90 pnt2 = isl_point_alloc(isl_space_copy(pnt->dim), isl_vec_copy(pnt->vec));
91 return pnt2;
94 __isl_give isl_point *isl_point_cow(__isl_take isl_point *pnt)
96 struct isl_point *pnt2;
97 if (!pnt)
98 return NULL;
100 if (pnt->ref == 1)
101 return pnt;
103 pnt2 = isl_point_dup(pnt);
104 isl_point_free(pnt);
105 return pnt2;
108 __isl_give isl_point *isl_point_copy(__isl_keep isl_point *pnt)
110 if (!pnt)
111 return NULL;
113 pnt->ref++;
114 return pnt;
117 __isl_null isl_point *isl_point_free(__isl_take isl_point *pnt)
119 if (!pnt)
120 return NULL;
122 if (--pnt->ref > 0)
123 return NULL;
125 isl_space_free(pnt->dim);
126 isl_vec_free(pnt->vec);
127 free(pnt);
128 return NULL;
131 __isl_give isl_point *isl_point_void(__isl_take isl_space *space)
133 if (!space)
134 return NULL;
136 return isl_point_alloc(space, isl_vec_alloc(space->ctx, 0));
139 isl_bool isl_point_is_void(__isl_keep isl_point *pnt)
141 if (!pnt)
142 return isl_bool_error;
144 return pnt->vec->size == 0;
147 /* Return the space of "pnt".
148 * This may be either a copy or the space itself
149 * if there is only one reference to "pnt".
150 * This allows the space to be modified inplace
151 * if both the point and its space have only a single reference.
152 * The caller is not allowed to modify "pnt" between this call and
153 * a subsequent call to isl_point_restore_space.
154 * The only exception is that isl_point_free can be called instead.
156 __isl_give isl_space *isl_point_take_space(__isl_keep isl_point *pnt)
158 isl_space *space;
160 if (!pnt)
161 return NULL;
162 if (pnt->ref != 1)
163 return isl_point_get_space(pnt);
164 space = pnt->dim;
165 pnt->dim = NULL;
166 return space;
169 /* Set the space of "pnt" to "space", where the space of "pnt" may be missing
170 * due to a preceding call to isl_point_take_space.
171 * However, in this case, "pnt" only has a single reference and
172 * then the call to isl_point_cow has no effect.
174 __isl_give isl_point *isl_point_restore_space(__isl_take isl_point *pnt,
175 __isl_take isl_space *space)
177 if (!pnt || !space)
178 goto error;
180 if (pnt->dim == space) {
181 isl_space_free(space);
182 return pnt;
185 pnt = isl_point_cow(pnt);
186 if (!pnt)
187 goto error;
188 isl_space_free(pnt->dim);
189 pnt->dim = space;
191 return pnt;
192 error:
193 isl_point_free(pnt);
194 isl_space_free(space);
195 return NULL;
198 /* Return the coordinate vector of "pnt".
200 __isl_keep isl_vec *isl_point_peek_vec(__isl_keep isl_point *pnt)
202 return pnt ? pnt->vec : NULL;
205 /* Return a copy of the coordinate vector of "pnt".
207 __isl_give isl_vec *isl_point_get_vec(__isl_keep isl_point *pnt)
209 return isl_vec_copy(isl_point_peek_vec(pnt));
212 /* Return the coordinate vector of "pnt".
213 * This may be either a copy or the coordinate vector itself
214 * if there is only one reference to "pnt".
215 * This allows the coordinate vector to be modified inplace
216 * if both the point and its coordinate vector have only a single reference.
217 * The caller is not allowed to modify "pnt" between this call and
218 * a subsequent call to isl_point_restore_vec.
219 * The only exception is that isl_point_free can be called instead.
221 __isl_give isl_vec *isl_point_take_vec(__isl_keep isl_point *pnt)
223 isl_vec *vec;
225 if (!pnt)
226 return NULL;
227 if (pnt->ref != 1)
228 return isl_point_get_vec(pnt);
229 vec = pnt->vec;
230 pnt->vec = NULL;
231 return vec;
234 /* Set the coordinate vector of "pnt" to "vec",
235 * where the coordinate vector of "pnt" may be missing
236 * due to a preceding call to isl_point_take_vec.
237 * However, in this case, "pnt" only has a single reference and
238 * then the call to isl_point_cow has no effect.
240 __isl_give isl_point *isl_point_restore_vec(__isl_take isl_point *pnt,
241 __isl_take isl_vec *vec)
243 if (!pnt || !vec)
244 goto error;
246 if (pnt->vec == vec) {
247 isl_vec_free(vec);
248 return pnt;
251 pnt = isl_point_cow(pnt);
252 if (!pnt)
253 goto error;
254 isl_vec_free(pnt->vec);
255 pnt->vec = vec;
257 return pnt;
258 error:
259 isl_point_free(pnt);
260 isl_vec_free(vec);
261 return NULL;
264 /* Return the number of variables of the given type.
266 static isl_size isl_point_dim(__isl_keep isl_point *pnt, enum isl_dim_type type)
268 return isl_space_dim(isl_point_peek_space(pnt), type);
271 /* Return the position of the coordinates of the given type
272 * within the sequence of coordinates of "pnt".
274 static isl_size isl_point_var_offset(__isl_keep isl_point *pnt,
275 enum isl_dim_type type)
277 return pnt ? isl_space_offset(pnt->dim, type) : isl_size_error;
280 #undef TYPE
281 #define TYPE isl_point
282 static
283 #include "check_type_range_templ.c"
285 /* Return the value of coordinate "pos" of type "type" of "pnt".
287 __isl_give isl_val *isl_point_get_coordinate_val(__isl_keep isl_point *pnt,
288 enum isl_dim_type type, int pos)
290 isl_ctx *ctx;
291 isl_val *v;
292 isl_size off;
294 if (!pnt)
295 return NULL;
297 ctx = isl_point_get_ctx(pnt);
298 if (isl_point_is_void(pnt))
299 isl_die(ctx, isl_error_invalid,
300 "void point does not have coordinates", return NULL);
301 if (isl_point_check_range(pnt, type, pos, 1) < 0)
302 return NULL;
304 off = isl_point_var_offset(pnt, type);
305 if (off < 0)
306 return NULL;
307 pos += off;
309 v = isl_val_rat_from_isl_int(ctx, pnt->vec->el[1 + pos],
310 pnt->vec->el[0]);
311 return isl_val_normalize(v);
314 /* Replace coordinate "pos" of type "type" of "pnt" by "v".
316 __isl_give isl_point *isl_point_set_coordinate_val(__isl_take isl_point *pnt,
317 enum isl_dim_type type, int pos, __isl_take isl_val *v)
319 if (!pnt || !v)
320 goto error;
321 if (isl_point_is_void(pnt))
322 isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
323 "void point does not have coordinates", goto error);
324 if (isl_point_check_range(pnt, type, pos, 1) < 0)
325 goto error;
326 if (!isl_val_is_rat(v))
327 isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
328 "expecting rational value", goto error);
330 if (isl_int_eq(pnt->vec->el[1 + pos], v->n) &&
331 isl_int_eq(pnt->vec->el[0], v->d)) {
332 isl_val_free(v);
333 return pnt;
336 pnt = isl_point_cow(pnt);
337 if (!pnt)
338 goto error;
339 pnt->vec = isl_vec_cow(pnt->vec);
340 if (!pnt->vec)
341 goto error;
343 if (isl_int_eq(pnt->vec->el[0], v->d)) {
344 isl_int_set(pnt->vec->el[1 + pos], v->n);
345 } else if (isl_int_is_one(v->d)) {
346 isl_int_mul(pnt->vec->el[1 + pos], pnt->vec->el[0], v->n);
347 } else {
348 isl_seq_scale(pnt->vec->el + 1,
349 pnt->vec->el + 1, v->d, pnt->vec->size - 1);
350 isl_int_mul(pnt->vec->el[1 + pos], pnt->vec->el[0], v->n);
351 isl_int_mul(pnt->vec->el[0], pnt->vec->el[0], v->d);
352 pnt->vec = isl_vec_normalize(pnt->vec);
353 if (!pnt->vec)
354 goto error;
357 isl_val_free(v);
358 return pnt;
359 error:
360 isl_val_free(v);
361 isl_point_free(pnt);
362 return NULL;
365 __isl_give isl_point *isl_point_add_ui(__isl_take isl_point *pnt,
366 enum isl_dim_type type, int pos, unsigned val)
368 isl_size off;
370 if (!pnt || isl_point_is_void(pnt))
371 return pnt;
373 pnt = isl_point_cow(pnt);
374 if (!pnt)
375 return NULL;
376 pnt->vec = isl_vec_cow(pnt->vec);
377 if (!pnt->vec)
378 goto error;
380 off = isl_point_var_offset(pnt, type);
381 if (off < 0)
382 goto error;
383 pos += off;
385 isl_int_add_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
387 return pnt;
388 error:
389 isl_point_free(pnt);
390 return NULL;
393 __isl_give isl_point *isl_point_sub_ui(__isl_take isl_point *pnt,
394 enum isl_dim_type type, int pos, unsigned val)
396 isl_size off;
398 if (!pnt || isl_point_is_void(pnt))
399 return pnt;
401 pnt = isl_point_cow(pnt);
402 if (!pnt)
403 return NULL;
404 pnt->vec = isl_vec_cow(pnt->vec);
405 if (!pnt->vec)
406 goto error;
408 off = isl_point_var_offset(pnt, type);
409 if (off < 0)
410 goto error;
411 pos += off;
413 isl_int_sub_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
415 return pnt;
416 error:
417 isl_point_free(pnt);
418 return NULL;
421 struct isl_foreach_point {
422 struct isl_scan_callback callback;
423 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
424 void *user;
425 isl_space *dim;
428 static isl_stat foreach_point(struct isl_scan_callback *cb,
429 __isl_take isl_vec *sample)
431 struct isl_foreach_point *fp = (struct isl_foreach_point *)cb;
432 isl_point *pnt;
434 pnt = isl_point_alloc(isl_space_copy(fp->dim), sample);
436 return fp->fn(pnt, fp->user);
439 isl_stat isl_set_foreach_point(__isl_keep isl_set *set,
440 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
442 struct isl_foreach_point fp = { { &foreach_point }, fn, user };
443 int i;
445 if (!set)
446 return isl_stat_error;
448 fp.dim = isl_set_get_space(set);
449 if (!fp.dim)
450 return isl_stat_error;
452 set = isl_set_copy(set);
453 set = isl_set_cow(set);
454 set = isl_set_make_disjoint(set);
455 set = isl_set_compute_divs(set);
456 if (!set)
457 goto error;
459 for (i = 0; i < set->n; ++i)
460 if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
461 &fp.callback) < 0)
462 goto error;
464 isl_set_free(set);
465 isl_space_free(fp.dim);
467 return isl_stat_ok;
468 error:
469 isl_set_free(set);
470 isl_space_free(fp.dim);
471 return isl_stat_error;
474 /* Return 1 if "bmap" contains the point "point".
475 * "bmap" is assumed to have known divs.
476 * The point is first extended with the divs and then passed
477 * to basic_map_contains.
479 isl_bool isl_basic_map_contains_point(__isl_keep isl_basic_map *bmap,
480 __isl_keep isl_point *point)
482 isl_local *local;
483 isl_vec *vec;
484 isl_bool contains;
486 if (!bmap || !point)
487 return isl_bool_error;
488 isl_assert(bmap->ctx, isl_space_is_equal(bmap->dim, point->dim),
489 return isl_bool_error);
490 if (bmap->n_div == 0)
491 return isl_basic_map_contains(bmap, point->vec);
493 local = isl_local_alloc_from_mat(isl_basic_map_get_divs(bmap));
494 vec = isl_point_get_vec(point);
495 vec = isl_local_extend_point_vec(local, vec);
496 isl_local_free(local);
498 contains = isl_basic_map_contains(bmap, vec);
500 isl_vec_free(vec);
501 return contains;
504 isl_bool isl_map_contains_point(__isl_keep isl_map *map,
505 __isl_keep isl_point *point)
507 int i;
508 isl_bool found = isl_bool_false;
510 if (!map || !point)
511 return isl_bool_error;
513 map = isl_map_copy(map);
514 map = isl_map_compute_divs(map);
515 if (!map)
516 return isl_bool_error;
518 for (i = 0; i < map->n; ++i) {
519 found = isl_basic_map_contains_point(map->p[i], point);
520 if (found < 0)
521 goto error;
522 if (found)
523 break;
525 isl_map_free(map);
527 return found;
528 error:
529 isl_map_free(map);
530 return isl_bool_error;
533 isl_bool isl_set_contains_point(__isl_keep isl_set *set,
534 __isl_keep isl_point *point)
536 return isl_map_contains_point(set_to_map(set), point);
539 __isl_give isl_basic_set *isl_basic_set_from_point(__isl_take isl_point *pnt)
541 isl_basic_set *bset;
542 isl_basic_set *model;
544 if (!pnt)
545 return NULL;
547 model = isl_basic_set_empty(isl_space_copy(pnt->dim));
548 bset = isl_basic_set_from_vec(isl_vec_copy(pnt->vec));
549 bset = isl_basic_set_from_underlying_set(bset, model);
550 isl_point_free(pnt);
552 return bset;
555 __isl_give isl_set *isl_set_from_point(__isl_take isl_point *pnt)
557 isl_basic_set *bset;
558 bset = isl_basic_set_from_point(pnt);
559 return isl_set_from_basic_set(bset);
562 /* Construct a union set, containing the single element "pnt".
563 * If "pnt" is void, then return an empty union set.
565 __isl_give isl_union_set *isl_union_set_from_point(__isl_take isl_point *pnt)
567 if (!pnt)
568 return NULL;
569 if (isl_point_is_void(pnt)) {
570 isl_space *space;
572 space = isl_point_get_space(pnt);
573 isl_point_free(pnt);
574 return isl_union_set_empty(space);
577 return isl_union_set_from_set(isl_set_from_point(pnt));
580 __isl_give isl_basic_set *isl_basic_set_box_from_points(
581 __isl_take isl_point *pnt1, __isl_take isl_point *pnt2)
583 isl_basic_set *bset = NULL;
584 isl_size total;
585 int i;
586 int k;
587 isl_int t;
589 isl_int_init(t);
591 if (!pnt1 || !pnt2)
592 goto error;
594 isl_assert(pnt1->dim->ctx,
595 isl_space_is_equal(pnt1->dim, pnt2->dim), goto error);
597 if (isl_point_is_void(pnt1) && isl_point_is_void(pnt2)) {
598 isl_space *dim = isl_space_copy(pnt1->dim);
599 isl_point_free(pnt1);
600 isl_point_free(pnt2);
601 isl_int_clear(t);
602 return isl_basic_set_empty(dim);
604 if (isl_point_is_void(pnt1)) {
605 isl_point_free(pnt1);
606 isl_int_clear(t);
607 return isl_basic_set_from_point(pnt2);
609 if (isl_point_is_void(pnt2)) {
610 isl_point_free(pnt2);
611 isl_int_clear(t);
612 return isl_basic_set_from_point(pnt1);
615 total = isl_point_dim(pnt1, isl_dim_all);
616 if (total < 0)
617 goto error;
618 bset = isl_basic_set_alloc_space(isl_space_copy(pnt1->dim), 0, 0, 2 * total);
620 for (i = 0; i < total; ++i) {
621 isl_int_mul(t, pnt1->vec->el[1 + i], pnt2->vec->el[0]);
622 isl_int_submul(t, pnt2->vec->el[1 + i], pnt1->vec->el[0]);
624 k = isl_basic_set_alloc_inequality(bset);
625 if (k < 0)
626 goto error;
627 isl_seq_clr(bset->ineq[k] + 1, total);
628 if (isl_int_is_pos(t)) {
629 isl_int_set_si(bset->ineq[k][1 + i], -1);
630 isl_int_set(bset->ineq[k][0], pnt1->vec->el[1 + i]);
631 } else {
632 isl_int_set_si(bset->ineq[k][1 + i], 1);
633 isl_int_neg(bset->ineq[k][0], pnt1->vec->el[1 + i]);
635 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt1->vec->el[0]);
637 k = isl_basic_set_alloc_inequality(bset);
638 if (k < 0)
639 goto error;
640 isl_seq_clr(bset->ineq[k] + 1, total);
641 if (isl_int_is_pos(t)) {
642 isl_int_set_si(bset->ineq[k][1 + i], 1);
643 isl_int_neg(bset->ineq[k][0], pnt2->vec->el[1 + i]);
644 } else {
645 isl_int_set_si(bset->ineq[k][1 + i], -1);
646 isl_int_set(bset->ineq[k][0], pnt2->vec->el[1 + i]);
648 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt2->vec->el[0]);
651 bset = isl_basic_set_finalize(bset);
653 isl_point_free(pnt1);
654 isl_point_free(pnt2);
656 isl_int_clear(t);
658 return bset;
659 error:
660 isl_point_free(pnt1);
661 isl_point_free(pnt2);
662 isl_int_clear(t);
663 isl_basic_set_free(bset);
664 return NULL;
667 __isl_give isl_set *isl_set_box_from_points(__isl_take isl_point *pnt1,
668 __isl_take isl_point *pnt2)
670 isl_basic_set *bset;
671 bset = isl_basic_set_box_from_points(pnt1, pnt2);
672 return isl_set_from_basic_set(bset);
675 /* Print the coordinate at position "pos" of the point "pnt".
677 static __isl_give isl_printer *print_coordinate(__isl_take isl_printer *p,
678 struct isl_print_space_data *data, unsigned pos)
680 isl_point *pnt = data->user;
682 p = isl_printer_print_isl_int(p, pnt->vec->el[1 + pos]);
683 if (!isl_int_is_one(pnt->vec->el[0])) {
684 p = isl_printer_print_str(p, "/");
685 p = isl_printer_print_isl_int(p, pnt->vec->el[0]);
688 return p;
691 __isl_give isl_printer *isl_printer_print_point(
692 __isl_take isl_printer *p, __isl_keep isl_point *pnt)
694 struct isl_print_space_data data = { 0 };
695 int i;
696 isl_size nparam;
698 if (!pnt)
699 return p;
700 if (isl_point_is_void(pnt)) {
701 p = isl_printer_print_str(p, "void");
702 return p;
705 nparam = isl_point_dim(pnt, isl_dim_param);
706 if (nparam < 0)
707 return isl_printer_free(p);
708 if (nparam > 0) {
709 p = isl_printer_print_str(p, "[");
710 for (i = 0; i < nparam; ++i) {
711 const char *name;
712 if (i)
713 p = isl_printer_print_str(p, ", ");
714 name = isl_space_get_dim_name(pnt->dim, isl_dim_param, i);
715 if (name) {
716 p = isl_printer_print_str(p, name);
717 p = isl_printer_print_str(p, " = ");
719 p = isl_printer_print_isl_int(p, pnt->vec->el[1 + i]);
720 if (!isl_int_is_one(pnt->vec->el[0])) {
721 p = isl_printer_print_str(p, "/");
722 p = isl_printer_print_isl_int(p, pnt->vec->el[0]);
725 p = isl_printer_print_str(p, "]");
726 p = isl_printer_print_str(p, " -> ");
728 data.print_dim = &print_coordinate;
729 data.user = pnt;
730 p = isl_printer_print_str(p, "{ ");
731 p = isl_print_space(pnt->dim, p, 0, &data);
732 p = isl_printer_print_str(p, " }");
733 return p;