2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 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 struct isl_vec *isl_vec_alloc(struct isl_ctx *ctx, unsigned size)
18 vec = isl_alloc_type(ctx, struct isl_vec);
22 vec->block = isl_blk_alloc(ctx, size);
23 if (isl_blk_is_error(vec->block))
30 vec->el = vec->block.data;
34 isl_blk_free(ctx, vec->block);
38 __isl_give isl_vec *isl_vec_extend(__isl_take isl_vec *vec, unsigned size)
42 if (size <= vec->size)
45 vec = isl_vec_cow(vec);
49 vec->block = isl_blk_extend(vec->ctx, vec->block, size);
54 vec->el = vec->block.data;
62 __isl_give isl_vec *isl_vec_zero_extend(__isl_take isl_vec *vec, unsigned size)
68 if (size <= vec->size)
71 vec = isl_vec_cow(vec);
75 extra = size - vec->size;
76 vec = isl_vec_extend(vec, size);
80 isl_seq_clr(vec->el + size - extra, extra);
85 struct isl_vec *isl_vec_copy(struct isl_vec *vec)
94 struct isl_vec *isl_vec_dup(struct isl_vec *vec)
100 vec2 = isl_vec_alloc(vec->ctx, vec->size);
101 isl_seq_cpy(vec2->el, vec->el, vec->size);
105 struct isl_vec *isl_vec_cow(struct isl_vec *vec)
107 struct isl_vec *vec2;
114 vec2 = isl_vec_dup(vec);
119 void isl_vec_free(struct isl_vec *vec)
127 isl_ctx_deref(vec->ctx);
128 isl_blk_free(vec->ctx, vec->block);
132 __isl_give isl_printer *isl_printer_print_vec(__isl_take isl_printer *printer,
133 __isl_keep isl_vec *vec)
137 if (!printer || !vec)
140 printer = isl_printer_print_str(printer, "[");
141 for (i = 0; i < vec->size; ++i) {
143 printer = isl_printer_print_str(printer, ",");
144 printer = isl_printer_print_isl_int(printer, vec->el[i]);
146 printer = isl_printer_print_str(printer, "]");
150 isl_printer_free(printer);
154 void isl_vec_dump(struct isl_vec *vec)
156 isl_printer *printer;
161 printer = isl_printer_to_file(vec->ctx, stderr);
162 printer = isl_printer_print_vec(printer, vec);
163 printer = isl_printer_end_line(printer);
165 isl_printer_free(printer);
168 __isl_give isl_vec *isl_vec_clr(__isl_take isl_vec *vec)
170 vec = isl_vec_cow(vec);
173 isl_seq_clr(vec->el, vec->size);
177 void isl_vec_lcm(struct isl_vec *vec, isl_int *lcm)
179 isl_seq_lcm(vec->block.data, vec->size, lcm);
182 /* Given a rational vector, with the denominator in the first element
183 * of the vector, round up all coordinates.
185 struct isl_vec *isl_vec_ceil(struct isl_vec *vec)
187 vec = isl_vec_cow(vec);
191 isl_seq_cdiv_q(vec->el + 1, vec->el + 1, vec->el[0], vec->size - 1);
193 isl_int_set_si(vec->el[0], 1);
198 struct isl_vec *isl_vec_normalize(struct isl_vec *vec)
202 isl_seq_normalize(vec->ctx, vec->el, vec->size);
206 __isl_give isl_vec *isl_vec_scale(__isl_take isl_vec *vec, isl_int m)
208 if (isl_int_is_one(m))
210 vec = isl_vec_cow(vec);
213 isl_seq_scale(vec->el, vec->el, m, vec->size);
217 __isl_give isl_vec *isl_vec_add(__isl_take isl_vec *vec1,
218 __isl_take isl_vec *vec2)
220 vec1 = isl_vec_cow(vec1);
224 isl_assert(vec1->ctx, vec1->size == vec2->size, goto error);
226 isl_seq_combine(vec1->el, vec1->ctx->one, vec1->el,
227 vec1->ctx->one, vec2->el, vec1->size);