isl_space_set_tuple_id: rename "dim" argument to "space"
[isl.git] / isl_point.c
blob3382e10b42121252d24ecab5ea71ed67b5f317fb
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_val_private.h>
10 #include <isl_vec_private.h>
11 #include <isl_output_private.h>
13 #include <set_to_map.c>
15 isl_ctx *isl_point_get_ctx(__isl_keep isl_point *pnt)
17 return pnt ? isl_space_get_ctx(pnt->dim) : NULL;
20 /* Return the space of "pnt".
22 __isl_keep isl_space *isl_point_peek_space(__isl_keep isl_point *pnt)
24 return pnt ? pnt->dim : NULL;
27 __isl_give isl_space *isl_point_get_space(__isl_keep isl_point *pnt)
29 return isl_space_copy(isl_point_peek_space(pnt));
32 __isl_give isl_point *isl_point_alloc(__isl_take isl_space *space,
33 __isl_take isl_vec *vec)
35 struct isl_point *pnt;
37 if (!space || !vec)
38 goto error;
40 if (vec->size > 1 + isl_space_dim(space, isl_dim_all)) {
41 vec = isl_vec_cow(vec);
42 if (!vec)
43 goto error;
44 vec->size = 1 + isl_space_dim(space, isl_dim_all);
47 pnt = isl_alloc_type(space->ctx, struct isl_point);
48 if (!pnt)
49 goto error;
51 pnt->ref = 1;
52 pnt->dim = space;
53 pnt->vec = vec;
55 return pnt;
56 error:
57 isl_space_free(space);
58 isl_vec_free(vec);
59 return NULL;
62 __isl_give isl_point *isl_point_zero(__isl_take isl_space *space)
64 isl_vec *vec;
66 if (!space)
67 return NULL;
68 vec = isl_vec_alloc(space->ctx, 1 + isl_space_dim(space, isl_dim_all));
69 if (!vec)
70 goto error;
71 isl_int_set_si(vec->el[0], 1);
72 isl_seq_clr(vec->el + 1, vec->size - 1);
73 return isl_point_alloc(space, vec);
74 error:
75 isl_space_free(space);
76 return NULL;
79 __isl_give isl_point *isl_point_dup(__isl_keep isl_point *pnt)
81 struct isl_point *pnt2;
83 if (!pnt)
84 return NULL;
85 pnt2 = isl_point_alloc(isl_space_copy(pnt->dim), isl_vec_copy(pnt->vec));
86 return pnt2;
89 __isl_give isl_point *isl_point_cow(__isl_take isl_point *pnt)
91 struct isl_point *pnt2;
92 if (!pnt)
93 return NULL;
95 if (pnt->ref == 1)
96 return pnt;
98 pnt2 = isl_point_dup(pnt);
99 isl_point_free(pnt);
100 return pnt2;
103 __isl_give isl_point *isl_point_copy(__isl_keep isl_point *pnt)
105 if (!pnt)
106 return NULL;
108 pnt->ref++;
109 return pnt;
112 __isl_null isl_point *isl_point_free(__isl_take isl_point *pnt)
114 if (!pnt)
115 return NULL;
117 if (--pnt->ref > 0)
118 return NULL;
120 isl_space_free(pnt->dim);
121 isl_vec_free(pnt->vec);
122 free(pnt);
123 return NULL;
126 __isl_give isl_point *isl_point_void(__isl_take isl_space *space)
128 if (!space)
129 return NULL;
131 return isl_point_alloc(space, isl_vec_alloc(space->ctx, 0));
134 isl_bool isl_point_is_void(__isl_keep isl_point *pnt)
136 if (!pnt)
137 return isl_bool_error;
139 return pnt->vec->size == 0;
142 /* Return the space of "pnt".
143 * This may be either a copy or the space itself
144 * if there is only one reference to "pnt".
145 * This allows the space to be modified inplace
146 * if both the point and its space have only a single reference.
147 * The caller is not allowed to modify "pnt" between this call and
148 * a subsequent call to isl_point_restore_space.
149 * The only exception is that isl_point_free can be called instead.
151 __isl_give isl_space *isl_point_take_space(__isl_keep isl_point *pnt)
153 isl_space *space;
155 if (!pnt)
156 return NULL;
157 if (pnt->ref != 1)
158 return isl_point_get_space(pnt);
159 space = pnt->dim;
160 pnt->dim = NULL;
161 return space;
164 /* Set the space of "pnt" to "space", where the space of "pnt" may be missing
165 * due to a preceding call to isl_point_take_space.
166 * However, in this case, "pnt" only has a single reference and
167 * then the call to isl_point_cow has no effect.
169 __isl_give isl_point *isl_point_restore_space(__isl_take isl_point *pnt,
170 __isl_take isl_space *space)
172 if (!pnt || !space)
173 goto error;
175 if (pnt->dim == space) {
176 isl_space_free(space);
177 return pnt;
180 pnt = isl_point_cow(pnt);
181 if (!pnt)
182 goto error;
183 isl_space_free(pnt->dim);
184 pnt->dim = space;
186 return pnt;
187 error:
188 isl_point_free(pnt);
189 isl_space_free(space);
190 return NULL;
193 /* Return the coordinate vector of "pnt".
195 __isl_keep isl_vec *isl_point_peek_vec(__isl_keep isl_point *pnt)
197 return pnt ? pnt->vec : NULL;
200 /* Return a copy of the coordinate vector of "pnt".
202 __isl_give isl_vec *isl_point_get_vec(__isl_keep isl_point *pnt)
204 return isl_vec_copy(isl_point_peek_vec(pnt));
207 /* Return the coordinate vector of "pnt".
208 * This may be either a copy or the coordinate vector itself
209 * if there is only one reference to "pnt".
210 * This allows the coordinate vector to be modified inplace
211 * if both the point and its coordinate vector have only a single reference.
212 * The caller is not allowed to modify "pnt" between this call and
213 * a subsequent call to isl_point_restore_vec.
214 * The only exception is that isl_point_free can be called instead.
216 __isl_give isl_vec *isl_point_take_vec(__isl_keep isl_point *pnt)
218 isl_vec *vec;
220 if (!pnt)
221 return NULL;
222 if (pnt->ref != 1)
223 return isl_point_get_vec(pnt);
224 vec = pnt->vec;
225 pnt->vec = NULL;
226 return vec;
229 /* Set the coordinate vector of "pnt" to "vec",
230 * where the coordinate vector of "pnt" may be missing
231 * due to a preceding call to isl_point_take_vec.
232 * However, in this case, "pnt" only has a single reference and
233 * then the call to isl_point_cow has no effect.
235 __isl_give isl_point *isl_point_restore_vec(__isl_take isl_point *pnt,
236 __isl_take isl_vec *vec)
238 if (!pnt || !vec)
239 goto error;
241 if (pnt->vec == vec) {
242 isl_vec_free(vec);
243 return pnt;
246 pnt = isl_point_cow(pnt);
247 if (!pnt)
248 goto error;
249 isl_vec_free(pnt->vec);
250 pnt->vec = vec;
252 return pnt;
253 error:
254 isl_point_free(pnt);
255 isl_vec_free(vec);
256 return NULL;
259 /* Return the value of coordinate "pos" of type "type" of "pnt".
261 __isl_give isl_val *isl_point_get_coordinate_val(__isl_keep isl_point *pnt,
262 enum isl_dim_type type, int pos)
264 isl_ctx *ctx;
265 isl_val *v;
267 if (!pnt)
268 return NULL;
270 ctx = isl_point_get_ctx(pnt);
271 if (isl_point_is_void(pnt))
272 isl_die(ctx, isl_error_invalid,
273 "void point does not have coordinates", return NULL);
274 if (pos < 0 || pos >= isl_space_dim(pnt->dim, type))
275 isl_die(ctx, isl_error_invalid,
276 "position out of bounds", return NULL);
278 if (type == isl_dim_set)
279 pos += isl_space_dim(pnt->dim, isl_dim_param);
281 v = isl_val_rat_from_isl_int(ctx, pnt->vec->el[1 + pos],
282 pnt->vec->el[0]);
283 return isl_val_normalize(v);
286 /* Replace coordinate "pos" of type "type" of "pnt" by "v".
288 __isl_give isl_point *isl_point_set_coordinate_val(__isl_take isl_point *pnt,
289 enum isl_dim_type type, int pos, __isl_take isl_val *v)
291 if (!pnt || !v)
292 goto error;
293 if (isl_point_is_void(pnt))
294 isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
295 "void point does not have coordinates", goto error);
296 if (pos < 0 || pos >= isl_space_dim(pnt->dim, type))
297 isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
298 "position out of bounds", goto error);
299 if (!isl_val_is_rat(v))
300 isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
301 "expecting rational value", goto error);
303 if (isl_int_eq(pnt->vec->el[1 + pos], v->n) &&
304 isl_int_eq(pnt->vec->el[0], v->d)) {
305 isl_val_free(v);
306 return pnt;
309 pnt = isl_point_cow(pnt);
310 if (!pnt)
311 goto error;
312 pnt->vec = isl_vec_cow(pnt->vec);
313 if (!pnt->vec)
314 goto error;
316 if (isl_int_eq(pnt->vec->el[0], v->d)) {
317 isl_int_set(pnt->vec->el[1 + pos], v->n);
318 } else if (isl_int_is_one(v->d)) {
319 isl_int_mul(pnt->vec->el[1 + pos], pnt->vec->el[0], v->n);
320 } else {
321 isl_seq_scale(pnt->vec->el + 1,
322 pnt->vec->el + 1, v->d, pnt->vec->size - 1);
323 isl_int_mul(pnt->vec->el[1 + pos], pnt->vec->el[0], v->n);
324 isl_int_mul(pnt->vec->el[0], pnt->vec->el[0], v->d);
325 pnt->vec = isl_vec_normalize(pnt->vec);
326 if (!pnt->vec)
327 goto error;
330 isl_val_free(v);
331 return pnt;
332 error:
333 isl_val_free(v);
334 isl_point_free(pnt);
335 return NULL;
338 __isl_give isl_point *isl_point_add_ui(__isl_take isl_point *pnt,
339 enum isl_dim_type type, int pos, unsigned val)
341 if (!pnt || isl_point_is_void(pnt))
342 return pnt;
344 pnt = isl_point_cow(pnt);
345 if (!pnt)
346 return NULL;
347 pnt->vec = isl_vec_cow(pnt->vec);
348 if (!pnt->vec)
349 goto error;
351 if (type == isl_dim_set)
352 pos += isl_space_dim(pnt->dim, isl_dim_param);
354 isl_int_add_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
356 return pnt;
357 error:
358 isl_point_free(pnt);
359 return NULL;
362 __isl_give isl_point *isl_point_sub_ui(__isl_take isl_point *pnt,
363 enum isl_dim_type type, int pos, unsigned val)
365 if (!pnt || isl_point_is_void(pnt))
366 return pnt;
368 pnt = isl_point_cow(pnt);
369 if (!pnt)
370 return NULL;
371 pnt->vec = isl_vec_cow(pnt->vec);
372 if (!pnt->vec)
373 goto error;
375 if (type == isl_dim_set)
376 pos += isl_space_dim(pnt->dim, isl_dim_param);
378 isl_int_sub_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
380 return pnt;
381 error:
382 isl_point_free(pnt);
383 return NULL;
386 struct isl_foreach_point {
387 struct isl_scan_callback callback;
388 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
389 void *user;
390 isl_space *dim;
393 static isl_stat foreach_point(struct isl_scan_callback *cb,
394 __isl_take isl_vec *sample)
396 struct isl_foreach_point *fp = (struct isl_foreach_point *)cb;
397 isl_point *pnt;
399 pnt = isl_point_alloc(isl_space_copy(fp->dim), sample);
401 return fp->fn(pnt, fp->user);
404 isl_stat isl_set_foreach_point(__isl_keep isl_set *set,
405 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
407 struct isl_foreach_point fp = { { &foreach_point }, fn, user };
408 int i;
410 if (!set)
411 return isl_stat_error;
413 fp.dim = isl_set_get_space(set);
414 if (!fp.dim)
415 return isl_stat_error;
417 set = isl_set_copy(set);
418 set = isl_set_cow(set);
419 set = isl_set_make_disjoint(set);
420 set = isl_set_compute_divs(set);
421 if (!set)
422 goto error;
424 for (i = 0; i < set->n; ++i)
425 if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
426 &fp.callback) < 0)
427 goto error;
429 isl_set_free(set);
430 isl_space_free(fp.dim);
432 return isl_stat_ok;
433 error:
434 isl_set_free(set);
435 isl_space_free(fp.dim);
436 return isl_stat_error;
439 /* Return 1 if "bmap" contains the point "point".
440 * "bmap" is assumed to have known divs.
441 * The point is first extended with the divs and then passed
442 * to basic_map_contains.
444 isl_bool isl_basic_map_contains_point(__isl_keep isl_basic_map *bmap,
445 __isl_keep isl_point *point)
447 int i;
448 struct isl_vec *vec;
449 unsigned dim;
450 isl_bool contains;
452 if (!bmap || !point)
453 return isl_bool_error;
454 isl_assert(bmap->ctx, isl_space_is_equal(bmap->dim, point->dim),
455 return isl_bool_error);
456 if (bmap->n_div == 0)
457 return isl_basic_map_contains(bmap, point->vec);
459 dim = isl_basic_map_total_dim(bmap) - bmap->n_div;
460 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
461 if (!vec)
462 return isl_bool_error;
464 isl_seq_cpy(vec->el, point->vec->el, point->vec->size);
465 for (i = 0; i < bmap->n_div; ++i) {
466 isl_seq_inner_product(bmap->div[i] + 1, vec->el,
467 1 + dim + i, &vec->el[1+dim+i]);
468 isl_int_fdiv_q(vec->el[1+dim+i], vec->el[1+dim+i],
469 bmap->div[i][0]);
472 contains = isl_basic_map_contains(bmap, vec);
474 isl_vec_free(vec);
475 return contains;
478 isl_bool isl_map_contains_point(__isl_keep isl_map *map,
479 __isl_keep isl_point *point)
481 int i;
482 isl_bool found = isl_bool_false;
484 if (!map || !point)
485 return isl_bool_error;
487 map = isl_map_copy(map);
488 map = isl_map_compute_divs(map);
489 if (!map)
490 return isl_bool_error;
492 for (i = 0; i < map->n; ++i) {
493 found = isl_basic_map_contains_point(map->p[i], point);
494 if (found < 0)
495 goto error;
496 if (found)
497 break;
499 isl_map_free(map);
501 return found;
502 error:
503 isl_map_free(map);
504 return isl_bool_error;
507 isl_bool isl_set_contains_point(__isl_keep isl_set *set,
508 __isl_keep isl_point *point)
510 return isl_map_contains_point(set_to_map(set), point);
513 __isl_give isl_basic_set *isl_basic_set_from_point(__isl_take isl_point *pnt)
515 isl_basic_set *bset;
516 isl_basic_set *model;
518 if (!pnt)
519 return NULL;
521 model = isl_basic_set_empty(isl_space_copy(pnt->dim));
522 bset = isl_basic_set_from_vec(isl_vec_copy(pnt->vec));
523 bset = isl_basic_set_from_underlying_set(bset, model);
524 isl_point_free(pnt);
526 return bset;
529 __isl_give isl_set *isl_set_from_point(__isl_take isl_point *pnt)
531 isl_basic_set *bset;
532 bset = isl_basic_set_from_point(pnt);
533 return isl_set_from_basic_set(bset);
536 /* Construct a union set, containing the single element "pnt".
537 * If "pnt" is void, then return an empty union set.
539 __isl_give isl_union_set *isl_union_set_from_point(__isl_take isl_point *pnt)
541 if (!pnt)
542 return NULL;
543 if (isl_point_is_void(pnt)) {
544 isl_space *space;
546 space = isl_point_get_space(pnt);
547 isl_point_free(pnt);
548 return isl_union_set_empty(space);
551 return isl_union_set_from_set(isl_set_from_point(pnt));
554 __isl_give isl_basic_set *isl_basic_set_box_from_points(
555 __isl_take isl_point *pnt1, __isl_take isl_point *pnt2)
557 isl_basic_set *bset = NULL;
558 unsigned total;
559 int i;
560 int k;
561 isl_int t;
563 isl_int_init(t);
565 if (!pnt1 || !pnt2)
566 goto error;
568 isl_assert(pnt1->dim->ctx,
569 isl_space_is_equal(pnt1->dim, pnt2->dim), goto error);
571 if (isl_point_is_void(pnt1) && isl_point_is_void(pnt2)) {
572 isl_space *dim = isl_space_copy(pnt1->dim);
573 isl_point_free(pnt1);
574 isl_point_free(pnt2);
575 isl_int_clear(t);
576 return isl_basic_set_empty(dim);
578 if (isl_point_is_void(pnt1)) {
579 isl_point_free(pnt1);
580 isl_int_clear(t);
581 return isl_basic_set_from_point(pnt2);
583 if (isl_point_is_void(pnt2)) {
584 isl_point_free(pnt2);
585 isl_int_clear(t);
586 return isl_basic_set_from_point(pnt1);
589 total = isl_space_dim(pnt1->dim, isl_dim_all);
590 bset = isl_basic_set_alloc_space(isl_space_copy(pnt1->dim), 0, 0, 2 * total);
592 for (i = 0; i < total; ++i) {
593 isl_int_mul(t, pnt1->vec->el[1 + i], pnt2->vec->el[0]);
594 isl_int_submul(t, pnt2->vec->el[1 + i], pnt1->vec->el[0]);
596 k = isl_basic_set_alloc_inequality(bset);
597 if (k < 0)
598 goto error;
599 isl_seq_clr(bset->ineq[k] + 1, total);
600 if (isl_int_is_pos(t)) {
601 isl_int_set_si(bset->ineq[k][1 + i], -1);
602 isl_int_set(bset->ineq[k][0], pnt1->vec->el[1 + i]);
603 } else {
604 isl_int_set_si(bset->ineq[k][1 + i], 1);
605 isl_int_neg(bset->ineq[k][0], pnt1->vec->el[1 + i]);
607 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt1->vec->el[0]);
609 k = isl_basic_set_alloc_inequality(bset);
610 if (k < 0)
611 goto error;
612 isl_seq_clr(bset->ineq[k] + 1, total);
613 if (isl_int_is_pos(t)) {
614 isl_int_set_si(bset->ineq[k][1 + i], 1);
615 isl_int_neg(bset->ineq[k][0], pnt2->vec->el[1 + i]);
616 } else {
617 isl_int_set_si(bset->ineq[k][1 + i], -1);
618 isl_int_set(bset->ineq[k][0], pnt2->vec->el[1 + i]);
620 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt2->vec->el[0]);
623 bset = isl_basic_set_finalize(bset);
625 isl_point_free(pnt1);
626 isl_point_free(pnt2);
628 isl_int_clear(t);
630 return bset;
631 error:
632 isl_point_free(pnt1);
633 isl_point_free(pnt2);
634 isl_int_clear(t);
635 isl_basic_set_free(bset);
636 return NULL;
639 __isl_give isl_set *isl_set_box_from_points(__isl_take isl_point *pnt1,
640 __isl_take isl_point *pnt2)
642 isl_basic_set *bset;
643 bset = isl_basic_set_box_from_points(pnt1, pnt2);
644 return isl_set_from_basic_set(bset);
647 /* Print the coordinate at position "pos" of the point "pnt".
649 static __isl_give isl_printer *print_coordinate(__isl_take isl_printer *p,
650 struct isl_print_space_data *data, unsigned pos)
652 isl_point *pnt = data->user;
654 p = isl_printer_print_isl_int(p, pnt->vec->el[1 + pos]);
655 if (!isl_int_is_one(pnt->vec->el[0])) {
656 p = isl_printer_print_str(p, "/");
657 p = isl_printer_print_isl_int(p, pnt->vec->el[0]);
660 return p;
663 __isl_give isl_printer *isl_printer_print_point(
664 __isl_take isl_printer *p, __isl_keep isl_point *pnt)
666 struct isl_print_space_data data = { 0 };
667 int i;
668 unsigned nparam;
670 if (!pnt)
671 return p;
672 if (isl_point_is_void(pnt)) {
673 p = isl_printer_print_str(p, "void");
674 return p;
677 nparam = isl_space_dim(pnt->dim, isl_dim_param);
678 if (nparam > 0) {
679 p = isl_printer_print_str(p, "[");
680 for (i = 0; i < nparam; ++i) {
681 const char *name;
682 if (i)
683 p = isl_printer_print_str(p, ", ");
684 name = isl_space_get_dim_name(pnt->dim, isl_dim_param, i);
685 if (name) {
686 p = isl_printer_print_str(p, name);
687 p = isl_printer_print_str(p, " = ");
689 p = isl_printer_print_isl_int(p, pnt->vec->el[1 + i]);
690 if (!isl_int_is_one(pnt->vec->el[0])) {
691 p = isl_printer_print_str(p, "/");
692 p = isl_printer_print_isl_int(p, pnt->vec->el[0]);
695 p = isl_printer_print_str(p, "]");
696 p = isl_printer_print_str(p, " -> ");
698 data.print_dim = &print_coordinate;
699 data.user = pnt;
700 p = isl_printer_print_str(p, "{ ");
701 p = isl_print_space(pnt->dim, p, 0, &data);
702 p = isl_printer_print_str(p, " }");
703 return p;