isl_ctx_reset_error: handle NULL input
[isl.git] / isl_point.c
blob9e8e24b56d0a153e1e0c0d1ace74a2187ee37e2a
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>
12 #include <isl/deprecated/point_int.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 __isl_give isl_space *isl_point_get_space(__isl_keep isl_point *pnt)
23 return pnt ? isl_space_copy(pnt->dim) : NULL;
26 __isl_give isl_point *isl_point_alloc(__isl_take isl_space *dim,
27 __isl_take isl_vec *vec)
29 struct isl_point *pnt;
31 if (!dim || !vec)
32 goto error;
34 if (vec->size > 1 + isl_space_dim(dim, isl_dim_all)) {
35 vec = isl_vec_cow(vec);
36 if (!vec)
37 goto error;
38 vec->size = 1 + isl_space_dim(dim, isl_dim_all);
41 pnt = isl_alloc_type(dim->ctx, struct isl_point);
42 if (!pnt)
43 goto error;
45 pnt->ref = 1;
46 pnt->dim = dim;
47 pnt->vec = vec;
49 return pnt;
50 error:
51 isl_space_free(dim);
52 isl_vec_free(vec);
53 return NULL;
56 __isl_give isl_point *isl_point_zero(__isl_take isl_space *dim)
58 isl_vec *vec;
60 if (!dim)
61 return NULL;
62 vec = isl_vec_alloc(dim->ctx, 1 + isl_space_dim(dim, isl_dim_all));
63 if (!vec)
64 goto error;
65 isl_int_set_si(vec->el[0], 1);
66 isl_seq_clr(vec->el + 1, vec->size - 1);
67 return isl_point_alloc(dim, vec);
68 error:
69 isl_space_free(dim);
70 return NULL;
73 __isl_give isl_point *isl_point_dup(__isl_keep isl_point *pnt)
75 struct isl_point *pnt2;
77 if (!pnt)
78 return NULL;
79 pnt2 = isl_point_alloc(isl_space_copy(pnt->dim), isl_vec_copy(pnt->vec));
80 return pnt2;
83 __isl_give isl_point *isl_point_cow(__isl_take isl_point *pnt)
85 struct isl_point *pnt2;
86 if (!pnt)
87 return NULL;
89 if (pnt->ref == 1)
90 return pnt;
92 pnt2 = isl_point_dup(pnt);
93 isl_point_free(pnt);
94 return pnt2;
97 __isl_give isl_point *isl_point_copy(__isl_keep isl_point *pnt)
99 if (!pnt)
100 return NULL;
102 pnt->ref++;
103 return pnt;
106 void isl_point_free(__isl_take isl_point *pnt)
108 if (!pnt)
109 return;
111 if (--pnt->ref > 0)
112 return;
114 isl_space_free(pnt->dim);
115 isl_vec_free(pnt->vec);
116 free(pnt);
119 __isl_give isl_point *isl_point_void(__isl_take isl_space *dim)
121 if (!dim)
122 return NULL;
124 return isl_point_alloc(dim, isl_vec_alloc(dim->ctx, 0));
127 isl_bool isl_point_is_void(__isl_keep isl_point *pnt)
129 if (!pnt)
130 return isl_bool_error;
132 return pnt->vec->size == 0;
135 int isl_point_get_coordinate(__isl_keep isl_point *pnt,
136 enum isl_dim_type type, int pos, isl_int *v)
138 if (!pnt || isl_point_is_void(pnt))
139 return -1;
141 if (pos < 0 || pos >= isl_space_dim(pnt->dim, type))
142 isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
143 "position out of bounds", return -1);
145 if (type == isl_dim_set)
146 pos += isl_space_dim(pnt->dim, isl_dim_param);
147 isl_int_set(*v, pnt->vec->el[1 + pos]);
149 return 0;
152 /* Return the value of coordinate "pos" of type "type" of "pnt".
154 __isl_give isl_val *isl_point_get_coordinate_val(__isl_keep isl_point *pnt,
155 enum isl_dim_type type, int pos)
157 isl_ctx *ctx;
158 isl_val *v;
160 if (!pnt)
161 return NULL;
163 ctx = isl_point_get_ctx(pnt);
164 if (isl_point_is_void(pnt))
165 isl_die(ctx, isl_error_invalid,
166 "void point does not have coordinates", return NULL);
167 if (pos < 0 || pos >= isl_space_dim(pnt->dim, type))
168 isl_die(ctx, isl_error_invalid,
169 "position out of bounds", return NULL);
171 if (type == isl_dim_set)
172 pos += isl_space_dim(pnt->dim, isl_dim_param);
174 v = isl_val_rat_from_isl_int(ctx, pnt->vec->el[1 + pos],
175 pnt->vec->el[0]);
176 return isl_val_normalize(v);
179 __isl_give isl_point *isl_point_set_coordinate(__isl_take isl_point *pnt,
180 enum isl_dim_type type, int pos, isl_int v)
182 if (!pnt || isl_point_is_void(pnt))
183 return pnt;
185 pnt = isl_point_cow(pnt);
186 if (!pnt)
187 return NULL;
188 pnt->vec = isl_vec_cow(pnt->vec);
189 if (!pnt->vec)
190 goto error;
192 if (type == isl_dim_set)
193 pos += isl_space_dim(pnt->dim, isl_dim_param);
195 isl_int_set(pnt->vec->el[1 + pos], v);
197 return pnt;
198 error:
199 isl_point_free(pnt);
200 return NULL;
203 /* Replace coordinate "pos" of type "type" of "pnt" by "v".
205 __isl_give isl_point *isl_point_set_coordinate_val(__isl_take isl_point *pnt,
206 enum isl_dim_type type, int pos, __isl_take isl_val *v)
208 if (!pnt || !v)
209 goto error;
210 if (isl_point_is_void(pnt))
211 isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
212 "void point does not have coordinates", goto error);
213 if (pos < 0 || pos >= isl_space_dim(pnt->dim, type))
214 isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
215 "position out of bounds", goto error);
216 if (!isl_val_is_rat(v))
217 isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
218 "expecting rational value", goto error);
220 if (isl_int_eq(pnt->vec->el[1 + pos], v->n) &&
221 isl_int_eq(pnt->vec->el[0], v->d)) {
222 isl_val_free(v);
223 return pnt;
226 pnt = isl_point_cow(pnt);
227 if (!pnt)
228 goto error;
229 pnt->vec = isl_vec_cow(pnt->vec);
230 if (!pnt->vec)
231 goto error;
233 if (isl_int_eq(pnt->vec->el[0], v->d)) {
234 isl_int_set(pnt->vec->el[1 + pos], v->n);
235 } else if (isl_int_is_one(v->d)) {
236 isl_int_mul(pnt->vec->el[1 + pos], pnt->vec->el[0], v->n);
237 } else {
238 isl_seq_scale(pnt->vec->el + 1,
239 pnt->vec->el + 1, v->d, pnt->vec->size - 1);
240 isl_int_mul(pnt->vec->el[1 + pos], pnt->vec->el[0], v->n);
241 isl_int_mul(pnt->vec->el[0], pnt->vec->el[0], v->d);
242 pnt->vec = isl_vec_normalize(pnt->vec);
243 if (!pnt->vec)
244 goto error;
247 isl_val_free(v);
248 return pnt;
249 error:
250 isl_val_free(v);
251 isl_point_free(pnt);
252 return NULL;
255 __isl_give isl_point *isl_point_add_ui(__isl_take isl_point *pnt,
256 enum isl_dim_type type, int pos, unsigned val)
258 if (!pnt || isl_point_is_void(pnt))
259 return pnt;
261 pnt = isl_point_cow(pnt);
262 if (!pnt)
263 return NULL;
264 pnt->vec = isl_vec_cow(pnt->vec);
265 if (!pnt->vec)
266 goto error;
268 if (type == isl_dim_set)
269 pos += isl_space_dim(pnt->dim, isl_dim_param);
271 isl_int_add_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
273 return pnt;
274 error:
275 isl_point_free(pnt);
276 return NULL;
279 __isl_give isl_point *isl_point_sub_ui(__isl_take isl_point *pnt,
280 enum isl_dim_type type, int pos, unsigned val)
282 if (!pnt || isl_point_is_void(pnt))
283 return pnt;
285 pnt = isl_point_cow(pnt);
286 if (!pnt)
287 return NULL;
288 pnt->vec = isl_vec_cow(pnt->vec);
289 if (!pnt->vec)
290 goto error;
292 if (type == isl_dim_set)
293 pos += isl_space_dim(pnt->dim, isl_dim_param);
295 isl_int_sub_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
297 return pnt;
298 error:
299 isl_point_free(pnt);
300 return NULL;
303 struct isl_foreach_point {
304 struct isl_scan_callback callback;
305 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
306 void *user;
307 isl_space *dim;
310 static isl_stat foreach_point(struct isl_scan_callback *cb,
311 __isl_take isl_vec *sample)
313 struct isl_foreach_point *fp = (struct isl_foreach_point *)cb;
314 isl_point *pnt;
316 pnt = isl_point_alloc(isl_space_copy(fp->dim), sample);
318 return fp->fn(pnt, fp->user);
321 isl_stat isl_set_foreach_point(__isl_keep isl_set *set,
322 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
324 struct isl_foreach_point fp = { { &foreach_point }, fn, user };
325 int i;
327 if (!set)
328 return isl_stat_error;
330 fp.dim = isl_set_get_space(set);
331 if (!fp.dim)
332 return isl_stat_error;
334 set = isl_set_copy(set);
335 set = isl_set_cow(set);
336 set = isl_set_make_disjoint(set);
337 set = isl_set_compute_divs(set);
338 if (!set)
339 goto error;
341 for (i = 0; i < set->n; ++i)
342 if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
343 &fp.callback) < 0)
344 goto error;
346 isl_set_free(set);
347 isl_space_free(fp.dim);
349 return isl_stat_ok;
350 error:
351 isl_set_free(set);
352 isl_space_free(fp.dim);
353 return isl_stat_error;
356 /* Return 1 if "bmap" contains the point "point".
357 * "bmap" is assumed to have known divs.
358 * The point is first extended with the divs and then passed
359 * to basic_map_contains.
361 isl_bool isl_basic_map_contains_point(__isl_keep isl_basic_map *bmap,
362 __isl_keep isl_point *point)
364 int i;
365 struct isl_vec *vec;
366 unsigned dim;
367 isl_bool contains;
369 if (!bmap || !point)
370 return isl_bool_error;
371 isl_assert(bmap->ctx, isl_space_is_equal(bmap->dim, point->dim),
372 return isl_bool_error);
373 if (bmap->n_div == 0)
374 return isl_basic_map_contains(bmap, point->vec);
376 dim = isl_basic_map_total_dim(bmap) - bmap->n_div;
377 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
378 if (!vec)
379 return isl_bool_error;
381 isl_seq_cpy(vec->el, point->vec->el, point->vec->size);
382 for (i = 0; i < bmap->n_div; ++i) {
383 isl_seq_inner_product(bmap->div[i] + 1, vec->el,
384 1 + dim + i, &vec->el[1+dim+i]);
385 isl_int_fdiv_q(vec->el[1+dim+i], vec->el[1+dim+i],
386 bmap->div[i][0]);
389 contains = isl_basic_map_contains(bmap, vec);
391 isl_vec_free(vec);
392 return contains;
395 int isl_map_contains_point(__isl_keep isl_map *map, __isl_keep isl_point *point)
397 int i;
398 int found = 0;
400 if (!map || !point)
401 return -1;
403 map = isl_map_copy(map);
404 map = isl_map_compute_divs(map);
405 if (!map)
406 return -1;
408 for (i = 0; i < map->n; ++i) {
409 found = isl_basic_map_contains_point(map->p[i], point);
410 if (found < 0)
411 goto error;
412 if (found)
413 break;
415 isl_map_free(map);
417 return found;
418 error:
419 isl_map_free(map);
420 return -1;
423 isl_bool isl_set_contains_point(__isl_keep isl_set *set,
424 __isl_keep isl_point *point)
426 return isl_map_contains_point(set_to_map(set), point);
429 __isl_give isl_basic_set *isl_basic_set_from_point(__isl_take isl_point *pnt)
431 isl_basic_set *bset;
432 isl_basic_set *model;
434 if (!pnt)
435 return NULL;
437 model = isl_basic_set_empty(isl_space_copy(pnt->dim));
438 bset = isl_basic_set_from_vec(isl_vec_copy(pnt->vec));
439 bset = isl_basic_set_from_underlying_set(bset, model);
440 isl_point_free(pnt);
442 return bset;
445 __isl_give isl_set *isl_set_from_point(__isl_take isl_point *pnt)
447 isl_basic_set *bset;
448 bset = isl_basic_set_from_point(pnt);
449 return isl_set_from_basic_set(bset);
452 /* Construct a union set, containing the single element "pnt".
453 * If "pnt" is void, then return an empty union set.
455 __isl_give isl_union_set *isl_union_set_from_point(__isl_take isl_point *pnt)
457 if (!pnt)
458 return NULL;
459 if (isl_point_is_void(pnt)) {
460 isl_space *space;
462 space = isl_point_get_space(pnt);
463 isl_point_free(pnt);
464 return isl_union_set_empty(space);
467 return isl_union_set_from_set(isl_set_from_point(pnt));
470 __isl_give isl_basic_set *isl_basic_set_box_from_points(
471 __isl_take isl_point *pnt1, __isl_take isl_point *pnt2)
473 isl_basic_set *bset = NULL;
474 unsigned total;
475 int i;
476 int k;
477 isl_int t;
479 isl_int_init(t);
481 if (!pnt1 || !pnt2)
482 goto error;
484 isl_assert(pnt1->dim->ctx,
485 isl_space_is_equal(pnt1->dim, pnt2->dim), goto error);
487 if (isl_point_is_void(pnt1) && isl_point_is_void(pnt2)) {
488 isl_space *dim = isl_space_copy(pnt1->dim);
489 isl_point_free(pnt1);
490 isl_point_free(pnt2);
491 isl_int_clear(t);
492 return isl_basic_set_empty(dim);
494 if (isl_point_is_void(pnt1)) {
495 isl_point_free(pnt1);
496 isl_int_clear(t);
497 return isl_basic_set_from_point(pnt2);
499 if (isl_point_is_void(pnt2)) {
500 isl_point_free(pnt2);
501 isl_int_clear(t);
502 return isl_basic_set_from_point(pnt1);
505 total = isl_space_dim(pnt1->dim, isl_dim_all);
506 bset = isl_basic_set_alloc_space(isl_space_copy(pnt1->dim), 0, 0, 2 * total);
508 for (i = 0; i < total; ++i) {
509 isl_int_mul(t, pnt1->vec->el[1 + i], pnt2->vec->el[0]);
510 isl_int_submul(t, pnt2->vec->el[1 + i], pnt1->vec->el[0]);
512 k = isl_basic_set_alloc_inequality(bset);
513 if (k < 0)
514 goto error;
515 isl_seq_clr(bset->ineq[k] + 1, total);
516 if (isl_int_is_pos(t)) {
517 isl_int_set_si(bset->ineq[k][1 + i], -1);
518 isl_int_set(bset->ineq[k][0], pnt1->vec->el[1 + i]);
519 } else {
520 isl_int_set_si(bset->ineq[k][1 + i], 1);
521 isl_int_neg(bset->ineq[k][0], pnt1->vec->el[1 + i]);
523 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt1->vec->el[0]);
525 k = isl_basic_set_alloc_inequality(bset);
526 if (k < 0)
527 goto error;
528 isl_seq_clr(bset->ineq[k] + 1, total);
529 if (isl_int_is_pos(t)) {
530 isl_int_set_si(bset->ineq[k][1 + i], 1);
531 isl_int_neg(bset->ineq[k][0], pnt2->vec->el[1 + i]);
532 } else {
533 isl_int_set_si(bset->ineq[k][1 + i], -1);
534 isl_int_set(bset->ineq[k][0], pnt2->vec->el[1 + i]);
536 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt2->vec->el[0]);
539 bset = isl_basic_set_finalize(bset);
541 isl_point_free(pnt1);
542 isl_point_free(pnt2);
544 isl_int_clear(t);
546 return bset;
547 error:
548 isl_point_free(pnt1);
549 isl_point_free(pnt2);
550 isl_int_clear(t);
551 isl_basic_set_free(bset);
552 return NULL;
555 __isl_give isl_set *isl_set_box_from_points(__isl_take isl_point *pnt1,
556 __isl_take isl_point *pnt2)
558 isl_basic_set *bset;
559 bset = isl_basic_set_box_from_points(pnt1, pnt2);
560 return isl_set_from_basic_set(bset);
563 /* Print the coordinate at position "pos" of the point "pnt".
565 static __isl_give isl_printer *print_coordinate(__isl_take isl_printer *p,
566 struct isl_print_space_data *data, unsigned pos)
568 isl_point *pnt = data->user;
570 p = isl_printer_print_isl_int(p, pnt->vec->el[1 + pos]);
571 if (!isl_int_is_one(pnt->vec->el[0])) {
572 p = isl_printer_print_str(p, "/");
573 p = isl_printer_print_isl_int(p, pnt->vec->el[0]);
576 return p;
579 __isl_give isl_printer *isl_printer_print_point(
580 __isl_take isl_printer *p, __isl_keep isl_point *pnt)
582 struct isl_print_space_data data = { 0 };
583 int i;
584 unsigned nparam;
585 unsigned dim;
587 if (!pnt)
588 return p;
589 if (isl_point_is_void(pnt)) {
590 p = isl_printer_print_str(p, "void");
591 return p;
594 nparam = isl_space_dim(pnt->dim, isl_dim_param);
595 dim = isl_space_dim(pnt->dim, isl_dim_set);
596 if (nparam > 0) {
597 p = isl_printer_print_str(p, "[");
598 for (i = 0; i < nparam; ++i) {
599 const char *name;
600 if (i)
601 p = isl_printer_print_str(p, ", ");
602 name = isl_space_get_dim_name(pnt->dim, isl_dim_param, i);
603 if (name) {
604 p = isl_printer_print_str(p, name);
605 p = isl_printer_print_str(p, " = ");
607 p = isl_printer_print_isl_int(p, pnt->vec->el[1 + i]);
608 if (!isl_int_is_one(pnt->vec->el[0])) {
609 p = isl_printer_print_str(p, "/");
610 p = isl_printer_print_isl_int(p, pnt->vec->el[0]);
613 p = isl_printer_print_str(p, "]");
614 p = isl_printer_print_str(p, " -> ");
616 data.print_dim = &print_coordinate;
617 data.user = pnt;
618 p = isl_printer_print_str(p, "{ ");
619 p = isl_print_space(pnt->dim, p, 0, &data);
620 p = isl_printer_print_str(p, " }");
621 return p;