add isl_aff_get_dim
[isl.git] / isl_vec.c
blob9df16a20228576e68e5e2024f87791a1f7439809
1 /*
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
8 */
10 #include <isl_ctx_private.h>
11 #include <isl/seq.h>
12 #include <isl/vec.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)
21 struct isl_vec *vec;
23 vec = isl_alloc_type(ctx, struct isl_vec);
24 if (!vec)
25 return NULL;
27 vec->block = isl_blk_alloc(ctx, size);
28 if (isl_blk_is_error(vec->block))
29 goto error;
31 vec->ctx = ctx;
32 isl_ctx_ref(ctx);
33 vec->ref = 1;
34 vec->size = size;
35 vec->el = vec->block.data;
37 return vec;
38 error:
39 isl_blk_free(ctx, vec->block);
40 return NULL;
43 __isl_give isl_vec *isl_vec_extend(__isl_take isl_vec *vec, unsigned size)
45 if (!vec)
46 return NULL;
47 if (size <= vec->size)
48 return vec;
50 vec = isl_vec_cow(vec);
51 if (!vec)
52 return NULL;
54 vec->block = isl_blk_extend(vec->ctx, vec->block, size);
55 if (!vec->block.data)
56 goto error;
58 vec->size = size;
59 vec->el = vec->block.data;
61 return vec;
62 error:
63 isl_vec_free(vec);
64 return NULL;
67 __isl_give isl_vec *isl_vec_zero_extend(__isl_take isl_vec *vec, unsigned size)
69 int extra;
71 if (!vec)
72 return NULL;
73 if (size <= vec->size)
74 return vec;
76 vec = isl_vec_cow(vec);
77 if (!vec)
78 return NULL;
80 extra = size - vec->size;
81 vec = isl_vec_extend(vec, size);
82 if (!vec)
83 return NULL;
85 isl_seq_clr(vec->el + size - extra, extra);
87 return vec;
90 struct isl_vec *isl_vec_copy(struct isl_vec *vec)
92 if (!vec)
93 return NULL;
95 vec->ref++;
96 return vec;
99 struct isl_vec *isl_vec_dup(struct isl_vec *vec)
101 struct isl_vec *vec2;
103 if (!vec)
104 return NULL;
105 vec2 = isl_vec_alloc(vec->ctx, vec->size);
106 isl_seq_cpy(vec2->el, vec->el, vec->size);
107 return vec2;
110 struct isl_vec *isl_vec_cow(struct isl_vec *vec)
112 struct isl_vec *vec2;
113 if (!vec)
114 return NULL;
116 if (vec->ref == 1)
117 return vec;
119 vec2 = isl_vec_dup(vec);
120 isl_vec_free(vec);
121 return vec2;
124 void isl_vec_free(struct isl_vec *vec)
126 if (!vec)
127 return;
129 if (--vec->ref > 0)
130 return;
132 isl_ctx_deref(vec->ctx);
133 isl_blk_free(vec->ctx, vec->block);
134 free(vec);
137 __isl_give isl_printer *isl_printer_print_vec(__isl_take isl_printer *printer,
138 __isl_keep isl_vec *vec)
140 int i;
142 if (!printer || !vec)
143 goto error;
145 printer = isl_printer_print_str(printer, "[");
146 for (i = 0; i < vec->size; ++i) {
147 if (i)
148 printer = isl_printer_print_str(printer, ",");
149 printer = isl_printer_print_isl_int(printer, vec->el[i]);
151 printer = isl_printer_print_str(printer, "]");
153 return printer;
154 error:
155 isl_printer_free(printer);
156 return NULL;
159 void isl_vec_dump(struct isl_vec *vec)
161 isl_printer *printer;
163 if (!vec)
164 return;
166 printer = isl_printer_to_file(vec->ctx, stderr);
167 printer = isl_printer_print_vec(printer, vec);
168 printer = isl_printer_end_line(printer);
170 isl_printer_free(printer);
173 __isl_give isl_vec *isl_vec_clr(__isl_take isl_vec *vec)
175 vec = isl_vec_cow(vec);
176 if (!vec)
177 return NULL;
178 isl_seq_clr(vec->el, vec->size);
179 return vec;
182 void isl_vec_lcm(struct isl_vec *vec, isl_int *lcm)
184 isl_seq_lcm(vec->block.data, vec->size, lcm);
187 /* Given a rational vector, with the denominator in the first element
188 * of the vector, round up all coordinates.
190 struct isl_vec *isl_vec_ceil(struct isl_vec *vec)
192 vec = isl_vec_cow(vec);
193 if (!vec)
194 return NULL;
196 isl_seq_cdiv_q(vec->el + 1, vec->el + 1, vec->el[0], vec->size - 1);
198 isl_int_set_si(vec->el[0], 1);
200 return vec;
203 struct isl_vec *isl_vec_normalize(struct isl_vec *vec)
205 if (!vec)
206 return NULL;
207 isl_seq_normalize(vec->ctx, vec->el, vec->size);
208 return vec;
211 __isl_give isl_vec *isl_vec_scale(__isl_take isl_vec *vec, isl_int m)
213 if (isl_int_is_one(m))
214 return vec;
215 vec = isl_vec_cow(vec);
216 if (!vec)
217 return NULL;
218 isl_seq_scale(vec->el, vec->el, m, vec->size);
219 return vec;
222 __isl_give isl_vec *isl_vec_add(__isl_take isl_vec *vec1,
223 __isl_take isl_vec *vec2)
225 vec1 = isl_vec_cow(vec1);
226 if (!vec1 || !vec2)
227 goto error;
229 isl_assert(vec1->ctx, vec1->size == vec2->size, goto error);
231 isl_seq_combine(vec1->el, vec1->ctx->one, vec1->el,
232 vec1->ctx->one, vec2->el, vec1->size);
234 isl_vec_free(vec2);
235 return vec1;
236 error:
237 isl_vec_free(vec1);
238 isl_vec_free(vec2);
239 return NULL;
242 static int qsort_int_cmp(const void *p1, const void *p2)
244 const isl_int *i1 = (const isl_int *) p1;
245 const isl_int *i2 = (const isl_int *) p2;
247 return isl_int_cmp(*i1, *i2);
250 __isl_give isl_vec *isl_vec_sort(__isl_take isl_vec *vec)
252 if (!vec)
253 return NULL;
255 qsort(vec->el, vec->size, sizeof(*vec->el), &qsort_int_cmp);
257 return vec;