isl_pw_*_normalize: sort pieces based on function value expressions
[isl.git] / isl_vec.c
blob747f89eb0cf9056aedaa073de069e624cdde9ba6
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>
16 #include <isl/deprecated/vec_int.h>
18 isl_ctx *isl_vec_get_ctx(__isl_keep isl_vec *vec)
20 return vec ? vec->ctx : NULL;
23 /* Return a hash value that digests "vec".
25 uint32_t isl_vec_get_hash(__isl_keep isl_vec *vec)
27 if (!vec)
28 return 0;
30 return isl_seq_get_hash(vec->el, vec->size);
33 struct isl_vec *isl_vec_alloc(struct isl_ctx *ctx, unsigned size)
35 struct isl_vec *vec;
37 vec = isl_alloc_type(ctx, struct isl_vec);
38 if (!vec)
39 return NULL;
41 vec->block = isl_blk_alloc(ctx, size);
42 if (isl_blk_is_error(vec->block))
43 goto error;
45 vec->ctx = ctx;
46 isl_ctx_ref(ctx);
47 vec->ref = 1;
48 vec->size = size;
49 vec->el = vec->block.data;
51 return vec;
52 error:
53 isl_blk_free(ctx, vec->block);
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 __isl_give isl_vec *isl_vec_zero_extend(__isl_take isl_vec *vec, unsigned size)
83 int extra;
85 if (!vec)
86 return NULL;
87 if (size <= vec->size)
88 return vec;
90 vec = isl_vec_cow(vec);
91 if (!vec)
92 return NULL;
94 extra = size - vec->size;
95 vec = isl_vec_extend(vec, size);
96 if (!vec)
97 return NULL;
99 isl_seq_clr(vec->el + size - extra, extra);
101 return vec;
104 /* Return a vector containing the elements of "vec1" followed by
105 * those of "vec2".
107 __isl_give isl_vec *isl_vec_concat(__isl_take isl_vec *vec1,
108 __isl_take isl_vec *vec2)
110 if (!vec1 || !vec2)
111 goto error;
113 if (vec2->size == 0) {
114 isl_vec_free(vec2);
115 return vec1;
118 if (vec1->size == 0) {
119 isl_vec_free(vec1);
120 return vec2;
123 vec1 = isl_vec_extend(vec1, vec1->size + vec2->size);
124 if (!vec1)
125 goto error;
127 isl_seq_cpy(vec1->el + vec1->size - vec2->size, vec2->el, vec2->size);
129 isl_vec_free(vec2);
130 return vec1;
131 error:
132 isl_vec_free(vec1);
133 isl_vec_free(vec2);
134 return NULL;
137 struct isl_vec *isl_vec_copy(struct isl_vec *vec)
139 if (!vec)
140 return NULL;
142 vec->ref++;
143 return vec;
146 struct isl_vec *isl_vec_dup(struct isl_vec *vec)
148 struct isl_vec *vec2;
150 if (!vec)
151 return NULL;
152 vec2 = isl_vec_alloc(vec->ctx, vec->size);
153 if (!vec2)
154 return NULL;
155 isl_seq_cpy(vec2->el, vec->el, vec->size);
156 return vec2;
159 struct isl_vec *isl_vec_cow(struct isl_vec *vec)
161 struct isl_vec *vec2;
162 if (!vec)
163 return NULL;
165 if (vec->ref == 1)
166 return vec;
168 vec2 = isl_vec_dup(vec);
169 isl_vec_free(vec);
170 return vec2;
173 __isl_null isl_vec *isl_vec_free(__isl_take isl_vec *vec)
175 if (!vec)
176 return NULL;
178 if (--vec->ref > 0)
179 return NULL;
181 isl_ctx_deref(vec->ctx);
182 isl_blk_free(vec->ctx, vec->block);
183 free(vec);
185 return NULL;
188 int isl_vec_size(__isl_keep isl_vec *vec)
190 return vec ? vec->size : -1;
193 int isl_vec_get_element(__isl_keep isl_vec *vec, int pos, isl_int *v)
195 if (!vec)
196 return -1;
198 if (pos < 0 || pos >= vec->size)
199 isl_die(vec->ctx, isl_error_invalid, "position out of range",
200 return -1);
201 isl_int_set(*v, vec->el[pos]);
202 return 0;
205 /* Extract the element at position "pos" of "vec".
207 __isl_give isl_val *isl_vec_get_element_val(__isl_keep isl_vec *vec, int pos)
209 isl_ctx *ctx;
211 if (!vec)
212 return NULL;
213 ctx = isl_vec_get_ctx(vec);
214 if (pos < 0 || pos >= vec->size)
215 isl_die(ctx, isl_error_invalid, "position out of range",
216 return NULL);
217 return isl_val_int_from_isl_int(ctx, vec->el[pos]);
220 __isl_give isl_vec *isl_vec_set_element(__isl_take isl_vec *vec,
221 int pos, isl_int v)
223 vec = isl_vec_cow(vec);
224 if (!vec)
225 return NULL;
226 if (pos < 0 || pos >= vec->size)
227 isl_die(vec->ctx, isl_error_invalid, "position out of range",
228 goto error);
229 isl_int_set(vec->el[pos], v);
230 return vec;
231 error:
232 isl_vec_free(vec);
233 return NULL;
236 __isl_give isl_vec *isl_vec_set_element_si(__isl_take isl_vec *vec,
237 int pos, int v)
239 vec = isl_vec_cow(vec);
240 if (!vec)
241 return NULL;
242 if (pos < 0 || pos >= vec->size)
243 isl_die(vec->ctx, isl_error_invalid, "position out of range",
244 goto error);
245 isl_int_set_si(vec->el[pos], v);
246 return vec;
247 error:
248 isl_vec_free(vec);
249 return NULL;
252 /* Replace the element at position "pos" of "vec" by "v".
254 __isl_give isl_vec *isl_vec_set_element_val(__isl_take isl_vec *vec,
255 int pos, __isl_take isl_val *v)
257 if (!v)
258 return isl_vec_free(vec);
259 if (!isl_val_is_int(v))
260 isl_die(isl_val_get_ctx(v), isl_error_invalid,
261 "expecting integer value", goto error);
262 vec = isl_vec_set_element(vec, pos, v->n);
263 isl_val_free(v);
264 return vec;
265 error:
266 isl_val_free(v);
267 return isl_vec_free(vec);
270 /* Compare the elements of "vec1" and "vec2" at position "pos".
272 int isl_vec_cmp_element(__isl_keep isl_vec *vec1, __isl_keep isl_vec *vec2,
273 int pos)
275 if (!vec1 || !vec2)
276 return 0;
277 if (pos < 0 || pos >= vec1->size || pos >= vec2->size)
278 isl_die(isl_vec_get_ctx(vec1), isl_error_invalid,
279 "position out of range", return 0);
280 return isl_int_cmp(vec1->el[pos], vec2->el[pos]);
283 isl_bool isl_vec_is_equal(__isl_keep isl_vec *vec1, __isl_keep isl_vec *vec2)
285 if (!vec1 || !vec2)
286 return isl_bool_error;
288 if (vec1->size != vec2->size)
289 return isl_bool_false;
291 return isl_seq_eq(vec1->el, vec2->el, vec1->size);
294 __isl_give isl_printer *isl_printer_print_vec(__isl_take isl_printer *printer,
295 __isl_keep isl_vec *vec)
297 int i;
299 if (!printer || !vec)
300 goto error;
302 printer = isl_printer_print_str(printer, "[");
303 for (i = 0; i < vec->size; ++i) {
304 if (i)
305 printer = isl_printer_print_str(printer, ",");
306 printer = isl_printer_print_isl_int(printer, vec->el[i]);
308 printer = isl_printer_print_str(printer, "]");
310 return printer;
311 error:
312 isl_printer_free(printer);
313 return NULL;
316 void isl_vec_dump(struct isl_vec *vec)
318 isl_printer *printer;
320 if (!vec)
321 return;
323 printer = isl_printer_to_file(vec->ctx, stderr);
324 printer = isl_printer_print_vec(printer, vec);
325 printer = isl_printer_end_line(printer);
327 isl_printer_free(printer);
330 __isl_give isl_vec *isl_vec_set(__isl_take isl_vec *vec, isl_int v)
332 vec = isl_vec_cow(vec);
333 if (!vec)
334 return NULL;
335 isl_seq_set(vec->el, v, vec->size);
336 return vec;
339 __isl_give isl_vec *isl_vec_set_si(__isl_take isl_vec *vec, int v)
341 vec = isl_vec_cow(vec);
342 if (!vec)
343 return NULL;
344 isl_seq_set_si(vec->el, v, vec->size);
345 return vec;
348 /* Replace all elements of "vec" by "v".
350 __isl_give isl_vec *isl_vec_set_val(__isl_take isl_vec *vec,
351 __isl_take isl_val *v)
353 vec = isl_vec_cow(vec);
354 if (!vec || !v)
355 goto error;
356 if (!isl_val_is_int(v))
357 isl_die(isl_val_get_ctx(v), isl_error_invalid,
358 "expecting integer value", goto error);
359 isl_seq_set(vec->el, v->n, vec->size);
360 isl_val_free(v);
361 return vec;
362 error:
363 isl_vec_free(vec);
364 isl_val_free(v);
365 return NULL;
368 __isl_give isl_vec *isl_vec_clr(__isl_take isl_vec *vec)
370 vec = isl_vec_cow(vec);
371 if (!vec)
372 return NULL;
373 isl_seq_clr(vec->el, vec->size);
374 return vec;
377 void isl_vec_lcm(struct isl_vec *vec, isl_int *lcm)
379 isl_seq_lcm(vec->block.data, vec->size, lcm);
382 /* Given a rational vector, with the denominator in the first element
383 * of the vector, round up all coordinates.
385 struct isl_vec *isl_vec_ceil(struct isl_vec *vec)
387 vec = isl_vec_cow(vec);
388 if (!vec)
389 return NULL;
391 isl_seq_cdiv_q(vec->el + 1, vec->el + 1, vec->el[0], vec->size - 1);
393 isl_int_set_si(vec->el[0], 1);
395 return vec;
398 struct isl_vec *isl_vec_normalize(struct isl_vec *vec)
400 if (!vec)
401 return NULL;
402 isl_seq_normalize(vec->ctx, vec->el, vec->size);
403 return vec;
406 __isl_give isl_vec *isl_vec_neg(__isl_take isl_vec *vec)
408 vec = isl_vec_cow(vec);
409 if (!vec)
410 return NULL;
411 isl_seq_neg(vec->el, vec->el, vec->size);
412 return vec;
415 __isl_give isl_vec *isl_vec_scale(__isl_take isl_vec *vec, isl_int m)
417 if (isl_int_is_one(m))
418 return vec;
419 vec = isl_vec_cow(vec);
420 if (!vec)
421 return NULL;
422 isl_seq_scale(vec->el, vec->el, m, vec->size);
423 return vec;
426 /* Reduce the elements of "vec" modulo "m".
428 __isl_give isl_vec *isl_vec_fdiv_r(__isl_take isl_vec *vec, isl_int m)
430 vec = isl_vec_cow(vec);
431 if (!vec)
432 return NULL;
434 isl_seq_fdiv_r(vec->el, vec->el, m, vec->size);
436 return vec;
439 __isl_give isl_vec *isl_vec_add(__isl_take isl_vec *vec1,
440 __isl_take isl_vec *vec2)
442 vec1 = isl_vec_cow(vec1);
443 if (!vec1 || !vec2)
444 goto error;
446 isl_assert(vec1->ctx, vec1->size == vec2->size, goto error);
448 isl_seq_combine(vec1->el, vec1->ctx->one, vec1->el,
449 vec1->ctx->one, vec2->el, vec1->size);
451 isl_vec_free(vec2);
452 return vec1;
453 error:
454 isl_vec_free(vec1);
455 isl_vec_free(vec2);
456 return NULL;
459 static int qsort_int_cmp(const void *p1, const void *p2)
461 const isl_int *i1 = (const isl_int *) p1;
462 const isl_int *i2 = (const isl_int *) p2;
464 return isl_int_cmp(*i1, *i2);
467 __isl_give isl_vec *isl_vec_sort(__isl_take isl_vec *vec)
469 if (!vec)
470 return NULL;
472 qsort(vec->el, vec->size, sizeof(*vec->el), &qsort_int_cmp);
474 return vec;
477 __isl_give isl_vec *isl_vec_drop_els(__isl_take isl_vec *vec,
478 unsigned pos, unsigned n)
480 if (n == 0)
481 return vec;
482 vec = isl_vec_cow(vec);
483 if (!vec)
484 return NULL;
486 if (pos + n > vec->size)
487 isl_die(vec->ctx, isl_error_invalid,
488 "range out of bounds", goto error);
490 if (pos + n != vec->size)
491 isl_seq_cpy(vec->el + pos, vec->el + pos + n,
492 vec->size - pos - n);
494 vec->size -= n;
496 return vec;
497 error:
498 isl_vec_free(vec);
499 return NULL;
502 __isl_give isl_vec *isl_vec_insert_els(__isl_take isl_vec *vec,
503 unsigned pos, unsigned n)
505 isl_vec *ext = NULL;
507 if (n == 0)
508 return vec;
509 if (!vec)
510 return NULL;
512 if (pos > vec->size)
513 isl_die(vec->ctx, isl_error_invalid,
514 "position out of bounds", goto error);
516 ext = isl_vec_alloc(vec->ctx, vec->size + n);
517 if (!ext)
518 goto error;
520 isl_seq_cpy(ext->el, vec->el, pos);
521 isl_seq_cpy(ext->el + pos + n, vec->el + pos, vec->size - pos);
523 isl_vec_free(vec);
524 return ext;
525 error:
526 isl_vec_free(vec);
527 isl_vec_free(ext);
528 return NULL;
531 __isl_give isl_vec *isl_vec_insert_zero_els(__isl_take isl_vec *vec,
532 unsigned pos, unsigned n)
534 vec = isl_vec_insert_els(vec, pos, n);
535 if (!vec)
536 return NULL;
538 isl_seq_clr(vec->el + pos, n);
540 return vec;
543 /* Move the "n" elements starting as "src_pos" of "vec"
544 * to "dst_pos". The elements originally at "dst_pos" are moved
545 * up or down depending on whether "dst_pos" is smaller or greater
546 * than "src_pos".
548 __isl_give isl_vec *isl_vec_move_els(__isl_take isl_vec *vec,
549 unsigned dst_pos, unsigned src_pos, unsigned n)
551 isl_vec *res;
553 if (!vec)
554 return NULL;
556 if (src_pos + n > vec->size)
557 isl_die(vec->ctx, isl_error_invalid,
558 "source range out of bounds", return isl_vec_free(vec));
559 if (dst_pos + n > vec->size)
560 isl_die(vec->ctx, isl_error_invalid,
561 "destination range out of bounds",
562 return isl_vec_free(vec));
564 if (n == 0 || dst_pos == src_pos)
565 return vec;
567 res = isl_vec_alloc(vec->ctx, vec->size);
568 if (!res)
569 return isl_vec_free(vec);
571 if (dst_pos < src_pos) {
572 isl_seq_cpy(res->el, vec->el, dst_pos);
573 isl_seq_cpy(res->el + dst_pos, vec->el + src_pos, n);
574 isl_seq_cpy(res->el + dst_pos + n,
575 vec->el + dst_pos, src_pos - dst_pos);
576 isl_seq_cpy(res->el + src_pos + n,
577 vec->el + src_pos + n, res->size - src_pos - n);
578 } else {
579 isl_seq_cpy(res->el, vec->el, src_pos);
580 isl_seq_cpy(res->el + src_pos,
581 vec->el + src_pos + n, dst_pos - src_pos);
582 isl_seq_cpy(res->el + dst_pos, vec->el + src_pos, n);
583 isl_seq_cpy(res->el + dst_pos + n,
584 vec->el + dst_pos + n, res->size - dst_pos - n);
587 isl_vec_free(vec);
588 return res;