isl_union_map_params: plug memory leak in case of empty input
[isl.git] / isl_point.c
bloba86dd67d29d02aff41dee5f2e2c5675d05d347df
1 #include <isl_map_private.h>
2 #include <isl_point_private.h>
3 #include <isl/set.h>
4 #include <isl_sample.h>
5 #include <isl_scan.h>
6 #include <isl/seq.h>
7 #include <isl_space_private.h>
8 #include <isl_val_private.h>
10 isl_ctx *isl_point_get_ctx(__isl_keep isl_point *pnt)
12 return pnt ? isl_space_get_ctx(pnt->dim) : NULL;
15 __isl_give isl_space *isl_point_get_space(__isl_keep isl_point *pnt)
17 return pnt ? isl_space_copy(pnt->dim) : NULL;
20 __isl_give isl_point *isl_point_alloc(__isl_take isl_space *dim,
21 __isl_take isl_vec *vec)
23 struct isl_point *pnt;
25 if (!dim || !vec)
26 goto error;
28 if (vec->size > 1 + isl_space_dim(dim, isl_dim_all)) {
29 vec = isl_vec_cow(vec);
30 if (!vec)
31 goto error;
32 vec->size = 1 + isl_space_dim(dim, isl_dim_all);
35 pnt = isl_alloc_type(dim->ctx, struct isl_point);
36 if (!pnt)
37 goto error;
39 pnt->ref = 1;
40 pnt->dim = dim;
41 pnt->vec = vec;
43 return pnt;
44 error:
45 isl_space_free(dim);
46 isl_vec_free(vec);
47 return NULL;
50 __isl_give isl_point *isl_point_zero(__isl_take isl_space *dim)
52 isl_vec *vec;
54 if (!dim)
55 return NULL;
56 vec = isl_vec_alloc(dim->ctx, 1 + isl_space_dim(dim, isl_dim_all));
57 if (!vec)
58 goto error;
59 isl_int_set_si(vec->el[0], 1);
60 isl_seq_clr(vec->el + 1, vec->size - 1);
61 return isl_point_alloc(dim, vec);
62 error:
63 isl_space_free(dim);
64 return NULL;
67 __isl_give isl_point *isl_point_dup(__isl_keep isl_point *pnt)
69 struct isl_point *pnt2;
71 if (!pnt)
72 return NULL;
73 pnt2 = isl_point_alloc(isl_space_copy(pnt->dim), isl_vec_copy(pnt->vec));
74 return pnt2;
77 __isl_give isl_point *isl_point_cow(__isl_take isl_point *pnt)
79 struct isl_point *pnt2;
80 if (!pnt)
81 return NULL;
83 if (pnt->ref == 1)
84 return pnt;
86 pnt2 = isl_point_dup(pnt);
87 isl_point_free(pnt);
88 return pnt2;
91 __isl_give isl_point *isl_point_copy(__isl_keep isl_point *pnt)
93 if (!pnt)
94 return NULL;
96 pnt->ref++;
97 return pnt;
100 void isl_point_free(__isl_take isl_point *pnt)
102 if (!pnt)
103 return;
105 if (--pnt->ref > 0)
106 return;
108 isl_space_free(pnt->dim);
109 isl_vec_free(pnt->vec);
110 free(pnt);
113 __isl_give isl_point *isl_point_void(__isl_take isl_space *dim)
115 if (!dim)
116 return NULL;
118 return isl_point_alloc(dim, isl_vec_alloc(dim->ctx, 0));
121 int isl_point_is_void(__isl_keep isl_point *pnt)
123 if (!pnt)
124 return -1;
126 return pnt->vec->size == 0;
129 int isl_point_get_coordinate(__isl_keep isl_point *pnt,
130 enum isl_dim_type type, int pos, isl_int *v)
132 if (!pnt || isl_point_is_void(pnt))
133 return -1;
135 if (pos < 0 || pos >= isl_space_dim(pnt->dim, type))
136 isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
137 "position out of bounds", return -1);
139 if (type == isl_dim_set)
140 pos += isl_space_dim(pnt->dim, isl_dim_param);
141 isl_int_set(*v, pnt->vec->el[1 + pos]);
143 return 0;
146 /* Return the value of coordinate "pos" of type "type" of "pnt".
148 __isl_give isl_val *isl_point_get_coordinate_val(__isl_keep isl_point *pnt,
149 enum isl_dim_type type, int pos)
151 isl_ctx *ctx;
152 isl_val *v;
154 if (!pnt)
155 return NULL;
157 ctx = isl_point_get_ctx(pnt);
158 if (isl_point_is_void(pnt))
159 isl_die(ctx, isl_error_invalid,
160 "void point does not have coordinates", return NULL);
161 if (pos < 0 || pos >= isl_space_dim(pnt->dim, type))
162 isl_die(ctx, isl_error_invalid,
163 "position out of bounds", return NULL);
165 if (type == isl_dim_set)
166 pos += isl_space_dim(pnt->dim, isl_dim_param);
168 v = isl_val_rat_from_isl_int(ctx, pnt->vec->el[1 + pos],
169 pnt->vec->el[0]);
170 return isl_val_normalize(v);
173 __isl_give isl_point *isl_point_set_coordinate(__isl_take isl_point *pnt,
174 enum isl_dim_type type, int pos, isl_int v)
176 if (!pnt || isl_point_is_void(pnt))
177 return pnt;
179 pnt = isl_point_cow(pnt);
180 if (!pnt)
181 return NULL;
182 pnt->vec = isl_vec_cow(pnt->vec);
183 if (!pnt->vec)
184 goto error;
186 if (type == isl_dim_set)
187 pos += isl_space_dim(pnt->dim, isl_dim_param);
189 isl_int_set(pnt->vec->el[1 + pos], v);
191 return pnt;
192 error:
193 isl_point_free(pnt);
194 return NULL;
197 /* Replace coordinate "pos" of type "type" of "pnt" by "v".
199 __isl_give isl_point *isl_point_set_coordinate_val(__isl_take isl_point *pnt,
200 enum isl_dim_type type, int pos, __isl_take isl_val *v)
202 if (!pnt || !v)
203 goto error;
204 if (isl_point_is_void(pnt))
205 isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
206 "void point does not have coordinates", goto error);
207 if (pos < 0 || pos >= isl_space_dim(pnt->dim, type))
208 isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
209 "position out of bounds", goto error);
210 if (!isl_val_is_rat(v))
211 isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
212 "expecting rational value", goto error);
214 if (isl_int_eq(pnt->vec->el[1 + pos], v->n) &&
215 isl_int_eq(pnt->vec->el[0], v->d)) {
216 isl_val_free(v);
217 return pnt;
220 pnt = isl_point_cow(pnt);
221 if (!pnt)
222 goto error;
223 pnt->vec = isl_vec_cow(pnt->vec);
224 if (!pnt->vec)
225 goto error;
227 if (isl_int_eq(pnt->vec->el[0], v->d)) {
228 isl_int_set(pnt->vec->el[1 + pos], v->n);
229 } else if (isl_int_is_one(v->d)) {
230 isl_int_mul(pnt->vec->el[1 + pos], pnt->vec->el[0], v->n);
231 } else {
232 isl_seq_scale(pnt->vec->el + 1,
233 pnt->vec->el + 1, v->d, pnt->vec->size - 1);
234 isl_int_mul(pnt->vec->el[1 + pos], pnt->vec->el[0], v->n);
235 isl_int_mul(pnt->vec->el[0], pnt->vec->el[0], v->d);
236 pnt->vec = isl_vec_normalize(pnt->vec);
237 if (!pnt->vec)
238 goto error;
241 isl_val_free(v);
242 return pnt;
243 error:
244 isl_val_free(v);
245 isl_point_free(pnt);
246 return NULL;
249 __isl_give isl_point *isl_point_add_ui(__isl_take isl_point *pnt,
250 enum isl_dim_type type, int pos, unsigned val)
252 if (!pnt || isl_point_is_void(pnt))
253 return pnt;
255 pnt = isl_point_cow(pnt);
256 if (!pnt)
257 return NULL;
258 pnt->vec = isl_vec_cow(pnt->vec);
259 if (!pnt->vec)
260 goto error;
262 if (type == isl_dim_set)
263 pos += isl_space_dim(pnt->dim, isl_dim_param);
265 isl_int_add_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
267 return pnt;
268 error:
269 isl_point_free(pnt);
270 return NULL;
273 __isl_give isl_point *isl_point_sub_ui(__isl_take isl_point *pnt,
274 enum isl_dim_type type, int pos, unsigned val)
276 if (!pnt || isl_point_is_void(pnt))
277 return pnt;
279 pnt = isl_point_cow(pnt);
280 if (!pnt)
281 return NULL;
282 pnt->vec = isl_vec_cow(pnt->vec);
283 if (!pnt->vec)
284 goto error;
286 if (type == isl_dim_set)
287 pos += isl_space_dim(pnt->dim, isl_dim_param);
289 isl_int_sub_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
291 return pnt;
292 error:
293 isl_point_free(pnt);
294 return NULL;
297 struct isl_foreach_point {
298 struct isl_scan_callback callback;
299 int (*fn)(__isl_take isl_point *pnt, void *user);
300 void *user;
301 isl_space *dim;
304 static int foreach_point(struct isl_scan_callback *cb, __isl_take isl_vec *sample)
306 struct isl_foreach_point *fp = (struct isl_foreach_point *)cb;
307 isl_point *pnt;
309 pnt = isl_point_alloc(isl_space_copy(fp->dim), sample);
311 return fp->fn(pnt, fp->user);
314 int isl_set_foreach_point(__isl_keep isl_set *set,
315 int (*fn)(__isl_take isl_point *pnt, void *user), void *user)
317 struct isl_foreach_point fp = { { &foreach_point }, fn, user };
318 int i;
320 if (!set)
321 return -1;
323 fp.dim = isl_set_get_space(set);
324 if (!fp.dim)
325 return -1;
327 set = isl_set_copy(set);
328 set = isl_set_cow(set);
329 set = isl_set_make_disjoint(set);
330 set = isl_set_compute_divs(set);
331 if (!set)
332 goto error;
334 for (i = 0; i < set->n; ++i)
335 if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
336 &fp.callback) < 0)
337 goto error;
339 isl_set_free(set);
340 isl_space_free(fp.dim);
342 return 0;
343 error:
344 isl_set_free(set);
345 isl_space_free(fp.dim);
346 return -1;
349 /* Return 1 if "bmap" contains the point "point".
350 * "bmap" is assumed to have known divs.
351 * The point is first extended with the divs and then passed
352 * to basic_map_contains.
354 int isl_basic_map_contains_point(__isl_keep isl_basic_map *bmap,
355 __isl_keep isl_point *point)
357 int i;
358 struct isl_vec *vec;
359 unsigned dim;
360 int contains;
362 if (!bmap || !point)
363 return -1;
364 isl_assert(bmap->ctx, isl_space_is_equal(bmap->dim, point->dim), return -1);
365 if (bmap->n_div == 0)
366 return isl_basic_map_contains(bmap, point->vec);
368 dim = isl_basic_map_total_dim(bmap) - bmap->n_div;
369 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
370 if (!vec)
371 return -1;
373 isl_seq_cpy(vec->el, point->vec->el, point->vec->size);
374 for (i = 0; i < bmap->n_div; ++i) {
375 isl_seq_inner_product(bmap->div[i] + 1, vec->el,
376 1 + dim + i, &vec->el[1+dim+i]);
377 isl_int_fdiv_q(vec->el[1+dim+i], vec->el[1+dim+i],
378 bmap->div[i][0]);
381 contains = isl_basic_map_contains(bmap, vec);
383 isl_vec_free(vec);
384 return contains;
387 int isl_map_contains_point(__isl_keep isl_map *map, __isl_keep isl_point *point)
389 int i;
390 int found = 0;
392 if (!map || !point)
393 return -1;
395 map = isl_map_copy(map);
396 map = isl_map_compute_divs(map);
397 if (!map)
398 return -1;
400 for (i = 0; i < map->n; ++i) {
401 found = isl_basic_map_contains_point(map->p[i], point);
402 if (found < 0)
403 goto error;
404 if (found)
405 break;
407 isl_map_free(map);
409 return found;
410 error:
411 isl_map_free(map);
412 return -1;
415 int isl_set_contains_point(__isl_keep isl_set *set, __isl_keep isl_point *point)
417 return isl_map_contains_point((isl_map *)set, point);
420 __isl_give isl_basic_set *isl_basic_set_from_point(__isl_take isl_point *pnt)
422 isl_basic_set *bset;
423 isl_basic_set *model;
425 model = isl_basic_set_empty(isl_space_copy(pnt->dim));
426 bset = isl_basic_set_from_vec(isl_vec_copy(pnt->vec));
427 bset = isl_basic_set_from_underlying_set(bset, model);
428 isl_point_free(pnt);
430 return bset;
433 __isl_give isl_set *isl_set_from_point(__isl_take isl_point *pnt)
435 isl_basic_set *bset;
436 bset = isl_basic_set_from_point(pnt);
437 return isl_set_from_basic_set(bset);
440 __isl_give isl_basic_set *isl_basic_set_box_from_points(
441 __isl_take isl_point *pnt1, __isl_take isl_point *pnt2)
443 isl_basic_set *bset;
444 unsigned total;
445 int i;
446 int k;
447 isl_int t;
449 isl_int_init(t);
451 if (!pnt1 || !pnt2)
452 goto error;
454 isl_assert(pnt1->dim->ctx,
455 isl_space_is_equal(pnt1->dim, pnt2->dim), goto error);
457 if (isl_point_is_void(pnt1) && isl_point_is_void(pnt2)) {
458 isl_space *dim = isl_space_copy(pnt1->dim);
459 isl_point_free(pnt1);
460 isl_point_free(pnt2);
461 isl_int_clear(t);
462 return isl_basic_set_empty(dim);
464 if (isl_point_is_void(pnt1)) {
465 isl_point_free(pnt1);
466 isl_int_clear(t);
467 return isl_basic_set_from_point(pnt2);
469 if (isl_point_is_void(pnt2)) {
470 isl_point_free(pnt2);
471 isl_int_clear(t);
472 return isl_basic_set_from_point(pnt1);
475 total = isl_space_dim(pnt1->dim, isl_dim_all);
476 bset = isl_basic_set_alloc_space(isl_space_copy(pnt1->dim), 0, 0, 2 * total);
478 for (i = 0; i < total; ++i) {
479 isl_int_mul(t, pnt1->vec->el[1 + i], pnt2->vec->el[0]);
480 isl_int_submul(t, pnt2->vec->el[1 + i], pnt1->vec->el[0]);
482 k = isl_basic_set_alloc_inequality(bset);
483 if (k < 0)
484 goto error;
485 isl_seq_clr(bset->ineq[k] + 1, total);
486 if (isl_int_is_pos(t)) {
487 isl_int_set_si(bset->ineq[k][1 + i], -1);
488 isl_int_set(bset->ineq[k][0], pnt1->vec->el[1 + i]);
489 } else {
490 isl_int_set_si(bset->ineq[k][1 + i], 1);
491 isl_int_neg(bset->ineq[k][0], pnt1->vec->el[1 + i]);
493 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt1->vec->el[0]);
495 k = isl_basic_set_alloc_inequality(bset);
496 if (k < 0)
497 goto error;
498 isl_seq_clr(bset->ineq[k] + 1, total);
499 if (isl_int_is_pos(t)) {
500 isl_int_set_si(bset->ineq[k][1 + i], 1);
501 isl_int_neg(bset->ineq[k][0], pnt2->vec->el[1 + i]);
502 } else {
503 isl_int_set_si(bset->ineq[k][1 + i], -1);
504 isl_int_set(bset->ineq[k][0], pnt2->vec->el[1 + i]);
506 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt2->vec->el[0]);
509 bset = isl_basic_set_finalize(bset);
511 isl_point_free(pnt1);
512 isl_point_free(pnt2);
514 isl_int_clear(t);
516 return bset;
517 error:
518 isl_point_free(pnt1);
519 isl_point_free(pnt2);
520 isl_int_clear(t);
521 return NULL;
524 __isl_give isl_set *isl_set_box_from_points(__isl_take isl_point *pnt1,
525 __isl_take isl_point *pnt2)
527 isl_basic_set *bset;
528 bset = isl_basic_set_box_from_points(pnt1, pnt2);
529 return isl_set_from_basic_set(bset);
532 __isl_give isl_printer *isl_printer_print_point(
533 __isl_take isl_printer *p, __isl_keep isl_point *pnt)
535 int i;
536 unsigned nparam;
537 unsigned dim;
539 if (!pnt)
540 return p;
541 if (isl_point_is_void(pnt)) {
542 p = isl_printer_print_str(p, "void");
543 return p;
546 nparam = isl_space_dim(pnt->dim, isl_dim_param);
547 dim = isl_space_dim(pnt->dim, isl_dim_set);
548 if (nparam > 0) {
549 p = isl_printer_print_str(p, "[");
550 for (i = 0; i < nparam; ++i) {
551 const char *name;
552 if (i)
553 p = isl_printer_print_str(p, ", ");
554 name = isl_space_get_dim_name(pnt->dim, isl_dim_param, i);
555 if (name) {
556 p = isl_printer_print_str(p, name);
557 p = isl_printer_print_str(p, " = ");
559 p = isl_printer_print_isl_int(p, pnt->vec->el[1 + i]);
560 if (!isl_int_is_one(pnt->vec->el[0])) {
561 p = isl_printer_print_str(p, "/");
562 p = isl_printer_print_isl_int(p, pnt->vec->el[0]);
565 p = isl_printer_print_str(p, "]");
566 p = isl_printer_print_str(p, " -> ");
568 p = isl_printer_print_str(p, "[");
569 for (i = 0; i < dim; ++i) {
570 if (i)
571 p = isl_printer_print_str(p, ", ");
572 p = isl_printer_print_isl_int(p, pnt->vec->el[1 + nparam + i]);
573 if (!isl_int_is_one(pnt->vec->el[0])) {
574 p = isl_printer_print_str(p, "/");
575 p = isl_printer_print_isl_int(p, pnt->vec->el[0]);
578 p = isl_printer_print_str(p, "]");
579 return p;