add isl_union_set_every_set
[isl.git] / isl_vec.c
blob029162057b4aa2e05fccb399d8d592d6cc92a545
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 #include <isl_ctx_private.h>
13 #include <isl_seq.h>
14 #include <isl_val_private.h>
15 #include <isl_vec_private.h>
17 isl_ctx *isl_vec_get_ctx(__isl_keep isl_vec *vec)
19 return vec ? vec->ctx : NULL;
22 /* Return a hash value that digests "vec".
24 uint32_t isl_vec_get_hash(__isl_keep isl_vec *vec)
26 if (!vec)
27 return 0;
29 return isl_seq_get_hash(vec->el, vec->size);
32 __isl_give isl_vec *isl_vec_alloc(struct isl_ctx *ctx, unsigned size)
34 struct isl_vec *vec;
36 vec = isl_alloc_type(ctx, struct isl_vec);
37 if (!vec)
38 return NULL;
40 vec->block = isl_blk_alloc(ctx, size);
41 if (isl_blk_is_error(vec->block))
42 goto error;
44 vec->ctx = ctx;
45 isl_ctx_ref(ctx);
46 vec->ref = 1;
47 vec->size = size;
48 vec->el = vec->block.data;
50 return vec;
51 error:
52 isl_blk_free(ctx, vec->block);
53 free(vec);
54 return NULL;
57 __isl_give isl_vec *isl_vec_extend(__isl_take isl_vec *vec, unsigned size)
59 if (!vec)
60 return NULL;
61 if (size <= vec->size)
62 return vec;
64 vec = isl_vec_cow(vec);
65 if (!vec)
66 return NULL;
68 vec->block = isl_blk_extend(vec->ctx, vec->block, size);
69 if (!vec->block.data)
70 goto error;
72 vec->size = size;
73 vec->el = vec->block.data;
75 return vec;
76 error:
77 isl_vec_free(vec);
78 return NULL;
81 /* Apply the expansion specified by "exp" to the "n" elements starting at "pos".
82 * "expanded" it the number of elements that need to replace those "n"
83 * elements. The entries in "exp" have increasing values between
84 * 0 and "expanded".
86 __isl_give isl_vec *isl_vec_expand(__isl_take isl_vec *vec, int pos, int n,
87 int *exp, int expanded)
89 int i, j;
90 int old_size, extra;
92 if (!vec)
93 return NULL;
94 if (expanded < n)
95 isl_die(isl_vec_get_ctx(vec), isl_error_invalid,
96 "not an expansion", return isl_vec_free(vec));
97 if (expanded == n)
98 return vec;
99 if (pos < 0 || n < 0 || pos + n > vec->size)
100 isl_die(isl_vec_get_ctx(vec), isl_error_invalid,
101 "position out of bounds", return isl_vec_free(vec));
103 old_size = vec->size;
104 extra = expanded - n;
105 vec = isl_vec_extend(vec, old_size + extra);
106 vec = isl_vec_cow(vec);
107 if (!vec)
108 return NULL;
110 for (i = old_size - 1; i >= pos + n; --i)
111 isl_int_set(vec->el[i + extra], vec->el[i]);
113 j = n - 1;
114 for (i = expanded - 1; i >= 0; --i) {
115 if (j >= 0 && exp[j] == i) {
116 if (i != j)
117 isl_int_swap(vec->el[pos + i],
118 vec->el[pos + j]);
119 j--;
120 } else {
121 isl_int_set_si(vec->el[pos + i], 0);
125 return vec;
128 /* Create a vector of size "size" with zero-valued elements.
130 __isl_give isl_vec *isl_vec_zero(isl_ctx *ctx, unsigned size)
132 isl_vec *vec;
134 vec = isl_vec_alloc(ctx, size);
135 if (!vec)
136 return NULL;
137 isl_seq_clr(vec->el, size);
138 return vec;
141 __isl_give isl_vec *isl_vec_zero_extend(__isl_take isl_vec *vec, unsigned size)
143 int extra;
145 if (!vec)
146 return NULL;
147 if (size <= vec->size)
148 return vec;
150 vec = isl_vec_cow(vec);
151 if (!vec)
152 return NULL;
154 extra = size - vec->size;
155 vec = isl_vec_extend(vec, size);
156 if (!vec)
157 return NULL;
159 isl_seq_clr(vec->el + size - extra, extra);
161 return vec;
164 /* Return a vector containing the elements of "vec1" followed by
165 * those of "vec2".
167 __isl_give isl_vec *isl_vec_concat(__isl_take isl_vec *vec1,
168 __isl_take isl_vec *vec2)
170 if (!vec1 || !vec2)
171 goto error;
173 if (vec2->size == 0) {
174 isl_vec_free(vec2);
175 return vec1;
178 if (vec1->size == 0) {
179 isl_vec_free(vec1);
180 return vec2;
183 vec1 = isl_vec_extend(vec1, vec1->size + vec2->size);
184 if (!vec1)
185 goto error;
187 isl_seq_cpy(vec1->el + vec1->size - vec2->size, vec2->el, vec2->size);
189 isl_vec_free(vec2);
190 return vec1;
191 error:
192 isl_vec_free(vec1);
193 isl_vec_free(vec2);
194 return NULL;
197 struct isl_vec *isl_vec_copy(struct isl_vec *vec)
199 if (!vec)
200 return NULL;
202 vec->ref++;
203 return vec;
206 struct isl_vec *isl_vec_dup(struct isl_vec *vec)
208 struct isl_vec *vec2;
210 if (!vec)
211 return NULL;
212 vec2 = isl_vec_alloc(vec->ctx, vec->size);
213 if (!vec2)
214 return NULL;
215 isl_seq_cpy(vec2->el, vec->el, vec->size);
216 return vec2;
219 struct isl_vec *isl_vec_cow(struct isl_vec *vec)
221 struct isl_vec *vec2;
222 if (!vec)
223 return NULL;
225 if (vec->ref == 1)
226 return vec;
228 vec2 = isl_vec_dup(vec);
229 isl_vec_free(vec);
230 return vec2;
233 __isl_null isl_vec *isl_vec_free(__isl_take isl_vec *vec)
235 if (!vec)
236 return NULL;
238 if (--vec->ref > 0)
239 return NULL;
241 isl_ctx_deref(vec->ctx);
242 isl_blk_free(vec->ctx, vec->block);
243 free(vec);
245 return NULL;
248 int isl_vec_size(__isl_keep isl_vec *vec)
250 return vec ? vec->size : -1;
253 /* Extract the element at position "pos" of "vec".
255 __isl_give isl_val *isl_vec_get_element_val(__isl_keep isl_vec *vec, int pos)
257 isl_ctx *ctx;
259 if (!vec)
260 return NULL;
261 ctx = isl_vec_get_ctx(vec);
262 if (pos < 0 || pos >= vec->size)
263 isl_die(ctx, isl_error_invalid, "position out of range",
264 return NULL);
265 return isl_val_int_from_isl_int(ctx, vec->el[pos]);
268 __isl_give isl_vec *isl_vec_set_element(__isl_take isl_vec *vec,
269 int pos, isl_int v)
271 vec = isl_vec_cow(vec);
272 if (!vec)
273 return NULL;
274 if (pos < 0 || pos >= vec->size)
275 isl_die(vec->ctx, isl_error_invalid, "position out of range",
276 goto error);
277 isl_int_set(vec->el[pos], v);
278 return vec;
279 error:
280 isl_vec_free(vec);
281 return NULL;
284 __isl_give isl_vec *isl_vec_set_element_si(__isl_take isl_vec *vec,
285 int pos, int v)
287 vec = isl_vec_cow(vec);
288 if (!vec)
289 return NULL;
290 if (pos < 0 || pos >= vec->size)
291 isl_die(vec->ctx, isl_error_invalid, "position out of range",
292 goto error);
293 isl_int_set_si(vec->el[pos], v);
294 return vec;
295 error:
296 isl_vec_free(vec);
297 return NULL;
300 /* Replace the element at position "pos" of "vec" by "v".
302 __isl_give isl_vec *isl_vec_set_element_val(__isl_take isl_vec *vec,
303 int pos, __isl_take isl_val *v)
305 if (!v)
306 return isl_vec_free(vec);
307 if (!isl_val_is_int(v))
308 isl_die(isl_val_get_ctx(v), isl_error_invalid,
309 "expecting integer value", goto error);
310 vec = isl_vec_set_element(vec, pos, v->n);
311 isl_val_free(v);
312 return vec;
313 error:
314 isl_val_free(v);
315 return isl_vec_free(vec);
318 /* Compare the elements of "vec1" and "vec2" at position "pos".
320 int isl_vec_cmp_element(__isl_keep isl_vec *vec1, __isl_keep isl_vec *vec2,
321 int pos)
323 if (!vec1 || !vec2)
324 return 0;
325 if (pos < 0 || pos >= vec1->size || pos >= vec2->size)
326 isl_die(isl_vec_get_ctx(vec1), isl_error_invalid,
327 "position out of range", return 0);
328 return isl_int_cmp(vec1->el[pos], vec2->el[pos]);
331 /* Does "vec" contain only zero elements?
333 isl_bool isl_vec_is_zero(__isl_keep isl_vec *vec)
335 if (!vec)
336 return isl_bool_error;
337 return isl_seq_first_non_zero(vec->el, vec->size) < 0;
340 isl_bool isl_vec_is_equal(__isl_keep isl_vec *vec1, __isl_keep isl_vec *vec2)
342 if (!vec1 || !vec2)
343 return isl_bool_error;
345 if (vec1->size != vec2->size)
346 return isl_bool_false;
348 return isl_seq_eq(vec1->el, vec2->el, vec1->size);
351 __isl_give isl_printer *isl_printer_print_vec(__isl_take isl_printer *printer,
352 __isl_keep isl_vec *vec)
354 int i;
356 if (!printer || !vec)
357 goto error;
359 printer = isl_printer_print_str(printer, "[");
360 for (i = 0; i < vec->size; ++i) {
361 if (i)
362 printer = isl_printer_print_str(printer, ",");
363 printer = isl_printer_print_isl_int(printer, vec->el[i]);
365 printer = isl_printer_print_str(printer, "]");
367 return printer;
368 error:
369 isl_printer_free(printer);
370 return NULL;
373 void isl_vec_dump(struct isl_vec *vec)
375 isl_printer *printer;
377 if (!vec)
378 return;
380 printer = isl_printer_to_file(vec->ctx, stderr);
381 printer = isl_printer_print_vec(printer, vec);
382 printer = isl_printer_end_line(printer);
384 isl_printer_free(printer);
387 __isl_give isl_vec *isl_vec_set(__isl_take isl_vec *vec, isl_int v)
389 vec = isl_vec_cow(vec);
390 if (!vec)
391 return NULL;
392 isl_seq_set(vec->el, v, vec->size);
393 return vec;
396 __isl_give isl_vec *isl_vec_set_si(__isl_take isl_vec *vec, int v)
398 vec = isl_vec_cow(vec);
399 if (!vec)
400 return NULL;
401 isl_seq_set_si(vec->el, v, vec->size);
402 return vec;
405 /* Replace all elements of "vec" by "v".
407 __isl_give isl_vec *isl_vec_set_val(__isl_take isl_vec *vec,
408 __isl_take isl_val *v)
410 vec = isl_vec_cow(vec);
411 if (!vec || !v)
412 goto error;
413 if (!isl_val_is_int(v))
414 isl_die(isl_val_get_ctx(v), isl_error_invalid,
415 "expecting integer value", goto error);
416 isl_seq_set(vec->el, v->n, vec->size);
417 isl_val_free(v);
418 return vec;
419 error:
420 isl_vec_free(vec);
421 isl_val_free(v);
422 return NULL;
425 __isl_give isl_vec *isl_vec_clr(__isl_take isl_vec *vec)
427 vec = isl_vec_cow(vec);
428 if (!vec)
429 return NULL;
430 isl_seq_clr(vec->el, vec->size);
431 return vec;
434 void isl_vec_lcm(struct isl_vec *vec, isl_int *lcm)
436 isl_seq_lcm(vec->block.data, vec->size, lcm);
439 /* Given a rational vector, with the denominator in the first element
440 * of the vector, round up all coordinates.
442 __isl_give isl_vec *isl_vec_ceil(__isl_take isl_vec *vec)
444 vec = isl_vec_cow(vec);
445 if (!vec)
446 return NULL;
448 isl_seq_cdiv_q(vec->el + 1, vec->el + 1, vec->el[0], vec->size - 1);
450 isl_int_set_si(vec->el[0], 1);
452 return vec;
455 struct isl_vec *isl_vec_normalize(struct isl_vec *vec)
457 if (!vec)
458 return NULL;
459 isl_seq_normalize(vec->ctx, vec->el, vec->size);
460 return vec;
463 __isl_give isl_vec *isl_vec_neg(__isl_take isl_vec *vec)
465 vec = isl_vec_cow(vec);
466 if (!vec)
467 return NULL;
468 isl_seq_neg(vec->el, vec->el, vec->size);
469 return vec;
472 __isl_give isl_vec *isl_vec_scale(__isl_take isl_vec *vec, isl_int m)
474 if (isl_int_is_one(m))
475 return vec;
476 vec = isl_vec_cow(vec);
477 if (!vec)
478 return NULL;
479 isl_seq_scale(vec->el, vec->el, m, vec->size);
480 return vec;
483 /* Reduce the elements of "vec" modulo "m".
485 __isl_give isl_vec *isl_vec_fdiv_r(__isl_take isl_vec *vec, isl_int m)
487 vec = isl_vec_cow(vec);
488 if (!vec)
489 return NULL;
491 isl_seq_fdiv_r(vec->el, vec->el, m, vec->size);
493 return vec;
496 __isl_give isl_vec *isl_vec_add(__isl_take isl_vec *vec1,
497 __isl_take isl_vec *vec2)
499 vec1 = isl_vec_cow(vec1);
500 if (!vec1 || !vec2)
501 goto error;
503 isl_assert(vec1->ctx, vec1->size == vec2->size, goto error);
505 isl_seq_combine(vec1->el, vec1->ctx->one, vec1->el,
506 vec1->ctx->one, vec2->el, vec1->size);
508 isl_vec_free(vec2);
509 return vec1;
510 error:
511 isl_vec_free(vec1);
512 isl_vec_free(vec2);
513 return NULL;
516 static int qsort_int_cmp(const void *p1, const void *p2)
518 const isl_int *i1 = (const isl_int *) p1;
519 const isl_int *i2 = (const isl_int *) p2;
521 return isl_int_cmp(*i1, *i2);
524 __isl_give isl_vec *isl_vec_sort(__isl_take isl_vec *vec)
526 if (!vec)
527 return NULL;
529 qsort(vec->el, vec->size, sizeof(*vec->el), &qsort_int_cmp);
531 return vec;
534 __isl_give isl_vec *isl_vec_drop_els(__isl_take isl_vec *vec,
535 unsigned pos, unsigned n)
537 if (n == 0)
538 return vec;
539 vec = isl_vec_cow(vec);
540 if (!vec)
541 return NULL;
543 if (pos + n > vec->size)
544 isl_die(vec->ctx, isl_error_invalid,
545 "range out of bounds", goto error);
547 if (pos + n != vec->size)
548 isl_seq_cpy(vec->el + pos, vec->el + pos + n,
549 vec->size - pos - n);
551 vec->size -= n;
553 return vec;
554 error:
555 isl_vec_free(vec);
556 return NULL;
559 __isl_give isl_vec *isl_vec_insert_els(__isl_take isl_vec *vec,
560 unsigned pos, unsigned n)
562 isl_vec *ext = NULL;
564 if (n == 0)
565 return vec;
566 if (!vec)
567 return NULL;
569 if (pos > vec->size)
570 isl_die(vec->ctx, isl_error_invalid,
571 "position out of bounds", goto error);
573 ext = isl_vec_alloc(vec->ctx, vec->size + n);
574 if (!ext)
575 goto error;
577 isl_seq_cpy(ext->el, vec->el, pos);
578 isl_seq_cpy(ext->el + pos + n, vec->el + pos, vec->size - pos);
580 isl_vec_free(vec);
581 return ext;
582 error:
583 isl_vec_free(vec);
584 isl_vec_free(ext);
585 return NULL;
588 /* Add "n" elements at the end of "vec".
590 __isl_give isl_vec *isl_vec_add_els(__isl_take isl_vec *vec, unsigned n)
592 if (!vec)
593 return NULL;
594 return isl_vec_insert_els(vec, vec->size, n);
597 __isl_give isl_vec *isl_vec_insert_zero_els(__isl_take isl_vec *vec,
598 unsigned pos, unsigned n)
600 vec = isl_vec_insert_els(vec, pos, n);
601 if (!vec)
602 return NULL;
604 isl_seq_clr(vec->el + pos, n);
606 return vec;
609 /* Move the "n" elements starting as "src_pos" of "vec"
610 * to "dst_pos". The elements originally at "dst_pos" are moved
611 * up or down depending on whether "dst_pos" is smaller or greater
612 * than "src_pos".
614 __isl_give isl_vec *isl_vec_move_els(__isl_take isl_vec *vec,
615 unsigned dst_pos, unsigned src_pos, unsigned n)
617 isl_vec *res;
619 if (!vec)
620 return NULL;
622 if (src_pos + n > vec->size)
623 isl_die(vec->ctx, isl_error_invalid,
624 "source range out of bounds", return isl_vec_free(vec));
625 if (dst_pos + n > vec->size)
626 isl_die(vec->ctx, isl_error_invalid,
627 "destination range out of bounds",
628 return isl_vec_free(vec));
630 if (n == 0 || dst_pos == src_pos)
631 return vec;
633 res = isl_vec_alloc(vec->ctx, vec->size);
634 if (!res)
635 return isl_vec_free(vec);
637 if (dst_pos < src_pos) {
638 isl_seq_cpy(res->el, vec->el, dst_pos);
639 isl_seq_cpy(res->el + dst_pos, vec->el + src_pos, n);
640 isl_seq_cpy(res->el + dst_pos + n,
641 vec->el + dst_pos, src_pos - dst_pos);
642 isl_seq_cpy(res->el + src_pos + n,
643 vec->el + src_pos + n, res->size - src_pos - n);
644 } else {
645 isl_seq_cpy(res->el, vec->el, src_pos);
646 isl_seq_cpy(res->el + src_pos,
647 vec->el + src_pos + n, dst_pos - src_pos);
648 isl_seq_cpy(res->el + dst_pos, vec->el + src_pos, n);
649 isl_seq_cpy(res->el + dst_pos + n,
650 vec->el + dst_pos + n, res->size - dst_pos - n);
653 isl_vec_free(vec);
654 return res;