2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 #include <isl_ctx_private.h>
14 isl_ctx *isl_vec_get_ctx(__isl_keep isl_vec *vec)
16 return vec ? vec->ctx : NULL;
19 struct isl_vec *isl_vec_alloc(struct isl_ctx *ctx, unsigned size)
23 vec = isl_alloc_type(ctx, struct isl_vec);
27 vec->block = isl_blk_alloc(ctx, size);
28 if (isl_blk_is_error(vec->block))
35 vec->el = vec->block.data;
39 isl_blk_free(ctx, vec->block);
43 __isl_give isl_vec *isl_vec_extend(__isl_take isl_vec *vec, unsigned size)
47 if (size <= vec->size)
50 vec = isl_vec_cow(vec);
54 vec->block = isl_blk_extend(vec->ctx, vec->block, size);
59 vec->el = vec->block.data;
67 __isl_give isl_vec *isl_vec_zero_extend(__isl_take isl_vec *vec, unsigned size)
73 if (size <= vec->size)
76 vec = isl_vec_cow(vec);
80 extra = size - vec->size;
81 vec = isl_vec_extend(vec, size);
85 isl_seq_clr(vec->el + size - extra, extra);
90 /* Return a vector containing the elements of "vec1" followed by
93 __isl_give isl_vec *isl_vec_concat(__isl_take isl_vec *vec1,
94 __isl_take isl_vec *vec2)
99 if (vec2->size == 0) {
104 if (vec1->size == 0) {
109 vec1 = isl_vec_extend(vec1, vec1->size + vec2->size);
113 isl_seq_cpy(vec1->el + vec1->size - vec2->size, vec2->el, vec2->size);
123 struct isl_vec *isl_vec_copy(struct isl_vec *vec)
132 struct isl_vec *isl_vec_dup(struct isl_vec *vec)
134 struct isl_vec *vec2;
138 vec2 = isl_vec_alloc(vec->ctx, vec->size);
139 isl_seq_cpy(vec2->el, vec->el, vec->size);
143 struct isl_vec *isl_vec_cow(struct isl_vec *vec)
145 struct isl_vec *vec2;
152 vec2 = isl_vec_dup(vec);
157 void *isl_vec_free(__isl_take isl_vec *vec)
165 isl_ctx_deref(vec->ctx);
166 isl_blk_free(vec->ctx, vec->block);
172 int isl_vec_size(__isl_keep isl_vec *vec)
174 return vec ? vec->size : -1;
177 int isl_vec_get_element(__isl_keep isl_vec *vec, int pos, isl_int *v)
182 if (pos < 0 || pos >= vec->size)
183 isl_die(vec->ctx, isl_error_invalid, "position out of range",
185 isl_int_set(*v, vec->el[pos]);
189 __isl_give isl_vec *isl_vec_set_element(__isl_take isl_vec *vec,
192 vec = isl_vec_cow(vec);
195 if (pos < 0 || pos >= vec->size)
196 isl_die(vec->ctx, isl_error_invalid, "position out of range",
198 isl_int_set(vec->el[pos], v);
205 __isl_give isl_vec *isl_vec_set_element_si(__isl_take isl_vec *vec,
208 vec = isl_vec_cow(vec);
211 if (pos < 0 || pos >= vec->size)
212 isl_die(vec->ctx, isl_error_invalid, "position out of range",
214 isl_int_set_si(vec->el[pos], v);
221 int isl_vec_is_equal(__isl_keep isl_vec *vec1, __isl_keep isl_vec *vec2)
226 if (vec1->size != vec2->size)
229 return isl_seq_eq(vec1->el, vec2->el, vec1->size);
232 __isl_give isl_printer *isl_printer_print_vec(__isl_take isl_printer *printer,
233 __isl_keep isl_vec *vec)
237 if (!printer || !vec)
240 printer = isl_printer_print_str(printer, "[");
241 for (i = 0; i < vec->size; ++i) {
243 printer = isl_printer_print_str(printer, ",");
244 printer = isl_printer_print_isl_int(printer, vec->el[i]);
246 printer = isl_printer_print_str(printer, "]");
250 isl_printer_free(printer);
254 void isl_vec_dump(struct isl_vec *vec)
256 isl_printer *printer;
261 printer = isl_printer_to_file(vec->ctx, stderr);
262 printer = isl_printer_print_vec(printer, vec);
263 printer = isl_printer_end_line(printer);
265 isl_printer_free(printer);
268 __isl_give isl_vec *isl_vec_set(__isl_take isl_vec *vec, isl_int v)
270 vec = isl_vec_cow(vec);
273 isl_seq_set(vec->el, v, vec->size);
277 __isl_give isl_vec *isl_vec_set_si(__isl_take isl_vec *vec, int v)
279 vec = isl_vec_cow(vec);
282 isl_seq_set_si(vec->el, v, vec->size);
286 __isl_give isl_vec *isl_vec_clr(__isl_take isl_vec *vec)
288 vec = isl_vec_cow(vec);
291 isl_seq_clr(vec->el, vec->size);
295 void isl_vec_lcm(struct isl_vec *vec, isl_int *lcm)
297 isl_seq_lcm(vec->block.data, vec->size, lcm);
300 /* Given a rational vector, with the denominator in the first element
301 * of the vector, round up all coordinates.
303 struct isl_vec *isl_vec_ceil(struct isl_vec *vec)
305 vec = isl_vec_cow(vec);
309 isl_seq_cdiv_q(vec->el + 1, vec->el + 1, vec->el[0], vec->size - 1);
311 isl_int_set_si(vec->el[0], 1);
316 struct isl_vec *isl_vec_normalize(struct isl_vec *vec)
320 isl_seq_normalize(vec->ctx, vec->el, vec->size);
324 __isl_give isl_vec *isl_vec_neg(__isl_take isl_vec *vec)
326 vec = isl_vec_cow(vec);
329 isl_seq_neg(vec->el, vec->el, vec->size);
333 __isl_give isl_vec *isl_vec_scale(__isl_take isl_vec *vec, isl_int m)
335 if (isl_int_is_one(m))
337 vec = isl_vec_cow(vec);
340 isl_seq_scale(vec->el, vec->el, m, vec->size);
344 __isl_give isl_vec *isl_vec_add(__isl_take isl_vec *vec1,
345 __isl_take isl_vec *vec2)
347 vec1 = isl_vec_cow(vec1);
351 isl_assert(vec1->ctx, vec1->size == vec2->size, goto error);
353 isl_seq_combine(vec1->el, vec1->ctx->one, vec1->el,
354 vec1->ctx->one, vec2->el, vec1->size);
364 static int qsort_int_cmp(const void *p1, const void *p2)
366 const isl_int *i1 = (const isl_int *) p1;
367 const isl_int *i2 = (const isl_int *) p2;
369 return isl_int_cmp(*i1, *i2);
372 __isl_give isl_vec *isl_vec_sort(__isl_take isl_vec *vec)
377 qsort(vec->el, vec->size, sizeof(*vec->el), &qsort_int_cmp);
382 __isl_give isl_vec *isl_vec_drop_els(__isl_take isl_vec *vec,
383 unsigned pos, unsigned n)
387 vec = isl_vec_cow(vec);
391 if (pos + n > vec->size)
392 isl_die(vec->ctx, isl_error_invalid,
393 "range out of bounds", goto error);
395 if (pos + n != vec->size)
396 isl_seq_cpy(vec->el + pos, vec->el + pos + n,
397 vec->size - pos - n);
407 __isl_give isl_vec *isl_vec_insert_els(__isl_take isl_vec *vec,
408 unsigned pos, unsigned n)
418 isl_die(vec->ctx, isl_error_invalid,
419 "position out of bounds", goto error);
421 ext = isl_vec_alloc(vec->ctx, vec->size + n);
425 isl_seq_cpy(ext->el, vec->el, pos);
426 isl_seq_cpy(ext->el + pos + n, vec->el + pos, vec->size - pos);
436 __isl_give isl_vec *isl_vec_insert_zero_els(__isl_take isl_vec *vec,
437 unsigned pos, unsigned n)
439 vec = isl_vec_insert_els(vec, pos, n);
443 isl_seq_clr(vec->el + pos, n);