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>
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 struct isl_vec
*isl_vec_alloc(struct isl_ctx
*ctx
, unsigned size
)
27 vec
= isl_alloc_type(ctx
, struct isl_vec
);
31 vec
->block
= isl_blk_alloc(ctx
, size
);
32 if (isl_blk_is_error(vec
->block
))
39 vec
->el
= vec
->block
.data
;
43 isl_blk_free(ctx
, vec
->block
);
47 __isl_give isl_vec
*isl_vec_extend(__isl_take isl_vec
*vec
, unsigned size
)
51 if (size
<= vec
->size
)
54 vec
= isl_vec_cow(vec
);
58 vec
->block
= isl_blk_extend(vec
->ctx
, vec
->block
, size
);
63 vec
->el
= vec
->block
.data
;
71 __isl_give isl_vec
*isl_vec_zero_extend(__isl_take isl_vec
*vec
, unsigned size
)
77 if (size
<= vec
->size
)
80 vec
= isl_vec_cow(vec
);
84 extra
= size
- vec
->size
;
85 vec
= isl_vec_extend(vec
, size
);
89 isl_seq_clr(vec
->el
+ size
- extra
, extra
);
94 /* Return a vector containing the elements of "vec1" followed by
97 __isl_give isl_vec
*isl_vec_concat(__isl_take isl_vec
*vec1
,
98 __isl_take isl_vec
*vec2
)
103 if (vec2
->size
== 0) {
108 if (vec1
->size
== 0) {
113 vec1
= isl_vec_extend(vec1
, vec1
->size
+ vec2
->size
);
117 isl_seq_cpy(vec1
->el
+ vec1
->size
- vec2
->size
, vec2
->el
, vec2
->size
);
127 struct isl_vec
*isl_vec_copy(struct isl_vec
*vec
)
136 struct isl_vec
*isl_vec_dup(struct isl_vec
*vec
)
138 struct isl_vec
*vec2
;
142 vec2
= isl_vec_alloc(vec
->ctx
, vec
->size
);
145 isl_seq_cpy(vec2
->el
, vec
->el
, vec
->size
);
149 struct isl_vec
*isl_vec_cow(struct isl_vec
*vec
)
151 struct isl_vec
*vec2
;
158 vec2
= isl_vec_dup(vec
);
163 __isl_null isl_vec
*isl_vec_free(__isl_take isl_vec
*vec
)
171 isl_ctx_deref(vec
->ctx
);
172 isl_blk_free(vec
->ctx
, vec
->block
);
178 int isl_vec_size(__isl_keep isl_vec
*vec
)
180 return vec
? vec
->size
: -1;
183 int isl_vec_get_element(__isl_keep isl_vec
*vec
, int pos
, isl_int
*v
)
188 if (pos
< 0 || pos
>= vec
->size
)
189 isl_die(vec
->ctx
, isl_error_invalid
, "position out of range",
191 isl_int_set(*v
, vec
->el
[pos
]);
195 /* Extract the element at position "pos" of "vec".
197 __isl_give isl_val
*isl_vec_get_element_val(__isl_keep isl_vec
*vec
, int pos
)
203 ctx
= isl_vec_get_ctx(vec
);
204 if (pos
< 0 || pos
>= vec
->size
)
205 isl_die(ctx
, isl_error_invalid
, "position out of range",
207 return isl_val_int_from_isl_int(ctx
, vec
->el
[pos
]);
210 __isl_give isl_vec
*isl_vec_set_element(__isl_take isl_vec
*vec
,
213 vec
= isl_vec_cow(vec
);
216 if (pos
< 0 || pos
>= vec
->size
)
217 isl_die(vec
->ctx
, isl_error_invalid
, "position out of range",
219 isl_int_set(vec
->el
[pos
], v
);
226 __isl_give isl_vec
*isl_vec_set_element_si(__isl_take isl_vec
*vec
,
229 vec
= isl_vec_cow(vec
);
232 if (pos
< 0 || pos
>= vec
->size
)
233 isl_die(vec
->ctx
, isl_error_invalid
, "position out of range",
235 isl_int_set_si(vec
->el
[pos
], v
);
242 /* Replace the element at position "pos" of "vec" by "v".
244 __isl_give isl_vec
*isl_vec_set_element_val(__isl_take isl_vec
*vec
,
245 int pos
, __isl_take isl_val
*v
)
248 return isl_vec_free(vec
);
249 if (!isl_val_is_int(v
))
250 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
251 "expecting integer value", goto error
);
252 vec
= isl_vec_set_element(vec
, pos
, v
->n
);
257 return isl_vec_free(vec
);
260 /* Compare the elements of "vec1" and "vec2" at position "pos".
262 int isl_vec_cmp_element(__isl_keep isl_vec
*vec1
, __isl_keep isl_vec
*vec2
,
267 if (pos
< 0 || pos
>= vec1
->size
|| pos
>= vec2
->size
)
268 isl_die(isl_vec_get_ctx(vec1
), isl_error_invalid
,
269 "position out of range", return 0);
270 return isl_int_cmp(vec1
->el
[pos
], vec2
->el
[pos
]);
273 isl_bool
isl_vec_is_equal(__isl_keep isl_vec
*vec1
, __isl_keep isl_vec
*vec2
)
276 return isl_bool_error
;
278 if (vec1
->size
!= vec2
->size
)
279 return isl_bool_false
;
281 return isl_seq_eq(vec1
->el
, vec2
->el
, vec1
->size
);
284 __isl_give isl_printer
*isl_printer_print_vec(__isl_take isl_printer
*printer
,
285 __isl_keep isl_vec
*vec
)
289 if (!printer
|| !vec
)
292 printer
= isl_printer_print_str(printer
, "[");
293 for (i
= 0; i
< vec
->size
; ++i
) {
295 printer
= isl_printer_print_str(printer
, ",");
296 printer
= isl_printer_print_isl_int(printer
, vec
->el
[i
]);
298 printer
= isl_printer_print_str(printer
, "]");
302 isl_printer_free(printer
);
306 void isl_vec_dump(struct isl_vec
*vec
)
308 isl_printer
*printer
;
313 printer
= isl_printer_to_file(vec
->ctx
, stderr
);
314 printer
= isl_printer_print_vec(printer
, vec
);
315 printer
= isl_printer_end_line(printer
);
317 isl_printer_free(printer
);
320 __isl_give isl_vec
*isl_vec_set(__isl_take isl_vec
*vec
, isl_int v
)
322 vec
= isl_vec_cow(vec
);
325 isl_seq_set(vec
->el
, v
, vec
->size
);
329 __isl_give isl_vec
*isl_vec_set_si(__isl_take isl_vec
*vec
, int v
)
331 vec
= isl_vec_cow(vec
);
334 isl_seq_set_si(vec
->el
, v
, vec
->size
);
338 /* Replace all elements of "vec" by "v".
340 __isl_give isl_vec
*isl_vec_set_val(__isl_take isl_vec
*vec
,
341 __isl_take isl_val
*v
)
343 vec
= isl_vec_cow(vec
);
346 if (!isl_val_is_int(v
))
347 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
348 "expecting integer value", goto error
);
349 isl_seq_set(vec
->el
, v
->n
, vec
->size
);
358 __isl_give isl_vec
*isl_vec_clr(__isl_take isl_vec
*vec
)
360 vec
= isl_vec_cow(vec
);
363 isl_seq_clr(vec
->el
, vec
->size
);
367 void isl_vec_lcm(struct isl_vec
*vec
, isl_int
*lcm
)
369 isl_seq_lcm(vec
->block
.data
, vec
->size
, lcm
);
372 /* Given a rational vector, with the denominator in the first element
373 * of the vector, round up all coordinates.
375 struct isl_vec
*isl_vec_ceil(struct isl_vec
*vec
)
377 vec
= isl_vec_cow(vec
);
381 isl_seq_cdiv_q(vec
->el
+ 1, vec
->el
+ 1, vec
->el
[0], vec
->size
- 1);
383 isl_int_set_si(vec
->el
[0], 1);
388 struct isl_vec
*isl_vec_normalize(struct isl_vec
*vec
)
392 isl_seq_normalize(vec
->ctx
, vec
->el
, vec
->size
);
396 __isl_give isl_vec
*isl_vec_neg(__isl_take isl_vec
*vec
)
398 vec
= isl_vec_cow(vec
);
401 isl_seq_neg(vec
->el
, vec
->el
, vec
->size
);
405 __isl_give isl_vec
*isl_vec_scale(__isl_take isl_vec
*vec
, isl_int m
)
407 if (isl_int_is_one(m
))
409 vec
= isl_vec_cow(vec
);
412 isl_seq_scale(vec
->el
, vec
->el
, m
, vec
->size
);
416 /* Reduce the elements of "vec" modulo "m".
418 __isl_give isl_vec
*isl_vec_fdiv_r(__isl_take isl_vec
*vec
, isl_int m
)
420 vec
= isl_vec_cow(vec
);
424 isl_seq_fdiv_r(vec
->el
, vec
->el
, m
, vec
->size
);
429 __isl_give isl_vec
*isl_vec_add(__isl_take isl_vec
*vec1
,
430 __isl_take isl_vec
*vec2
)
432 vec1
= isl_vec_cow(vec1
);
436 isl_assert(vec1
->ctx
, vec1
->size
== vec2
->size
, goto error
);
438 isl_seq_combine(vec1
->el
, vec1
->ctx
->one
, vec1
->el
,
439 vec1
->ctx
->one
, vec2
->el
, vec1
->size
);
449 static int qsort_int_cmp(const void *p1
, const void *p2
)
451 const isl_int
*i1
= (const isl_int
*) p1
;
452 const isl_int
*i2
= (const isl_int
*) p2
;
454 return isl_int_cmp(*i1
, *i2
);
457 __isl_give isl_vec
*isl_vec_sort(__isl_take isl_vec
*vec
)
462 qsort(vec
->el
, vec
->size
, sizeof(*vec
->el
), &qsort_int_cmp
);
467 __isl_give isl_vec
*isl_vec_drop_els(__isl_take isl_vec
*vec
,
468 unsigned pos
, unsigned n
)
472 vec
= isl_vec_cow(vec
);
476 if (pos
+ n
> vec
->size
)
477 isl_die(vec
->ctx
, isl_error_invalid
,
478 "range out of bounds", goto error
);
480 if (pos
+ n
!= vec
->size
)
481 isl_seq_cpy(vec
->el
+ pos
, vec
->el
+ pos
+ n
,
482 vec
->size
- pos
- n
);
492 __isl_give isl_vec
*isl_vec_insert_els(__isl_take isl_vec
*vec
,
493 unsigned pos
, unsigned n
)
503 isl_die(vec
->ctx
, isl_error_invalid
,
504 "position out of bounds", goto error
);
506 ext
= isl_vec_alloc(vec
->ctx
, vec
->size
+ n
);
510 isl_seq_cpy(ext
->el
, vec
->el
, pos
);
511 isl_seq_cpy(ext
->el
+ pos
+ n
, vec
->el
+ pos
, vec
->size
- pos
);
521 __isl_give isl_vec
*isl_vec_insert_zero_els(__isl_take isl_vec
*vec
,
522 unsigned pos
, unsigned n
)
524 vec
= isl_vec_insert_els(vec
, pos
, n
);
528 isl_seq_clr(vec
->el
+ pos
, n
);
533 /* Move the "n" elements starting as "src_pos" of "vec"
534 * to "dst_pos". The elements originally at "dst_pos" are moved
535 * up or down depending on whether "dst_pos" is smaller or greater
538 __isl_give isl_vec
*isl_vec_move_els(__isl_take isl_vec
*vec
,
539 unsigned dst_pos
, unsigned src_pos
, unsigned n
)
546 if (src_pos
+ n
> vec
->size
)
547 isl_die(vec
->ctx
, isl_error_invalid
,
548 "source range out of bounds", return isl_vec_free(vec
));
549 if (dst_pos
+ n
> vec
->size
)
550 isl_die(vec
->ctx
, isl_error_invalid
,
551 "destination range out of bounds",
552 return isl_vec_free(vec
));
554 if (n
== 0 || dst_pos
== src_pos
)
557 res
= isl_vec_alloc(vec
->ctx
, vec
->size
);
559 return isl_vec_free(vec
);
561 if (dst_pos
< src_pos
) {
562 isl_seq_cpy(res
->el
, vec
->el
, dst_pos
);
563 isl_seq_cpy(res
->el
+ dst_pos
, vec
->el
+ src_pos
, n
);
564 isl_seq_cpy(res
->el
+ dst_pos
+ n
,
565 vec
->el
+ dst_pos
, src_pos
- dst_pos
);
566 isl_seq_cpy(res
->el
+ src_pos
+ n
,
567 vec
->el
+ src_pos
+ n
, res
->size
- src_pos
- n
);
569 isl_seq_cpy(res
->el
, vec
->el
, src_pos
);
570 isl_seq_cpy(res
->el
+ src_pos
,
571 vec
->el
+ src_pos
+ n
, dst_pos
- src_pos
);
572 isl_seq_cpy(res
->el
+ dst_pos
, vec
->el
+ src_pos
, n
);
573 isl_seq_cpy(res
->el
+ dst_pos
+ n
,
574 vec
->el
+ dst_pos
+ n
, res
->size
- dst_pos
- n
);