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 /* Return a hash value that digests "vec".
25 uint32_t isl_vec_get_hash(__isl_keep isl_vec
*vec
)
30 return isl_seq_get_hash(vec
->el
, vec
->size
);
33 struct isl_vec
*isl_vec_alloc(struct isl_ctx
*ctx
, unsigned size
)
37 vec
= isl_alloc_type(ctx
, struct isl_vec
);
41 vec
->block
= isl_blk_alloc(ctx
, size
);
42 if (isl_blk_is_error(vec
->block
))
49 vec
->el
= vec
->block
.data
;
53 isl_blk_free(ctx
, vec
->block
);
57 __isl_give isl_vec
*isl_vec_extend(__isl_take isl_vec
*vec
, unsigned size
)
61 if (size
<= vec
->size
)
64 vec
= isl_vec_cow(vec
);
68 vec
->block
= isl_blk_extend(vec
->ctx
, vec
->block
, size
);
73 vec
->el
= vec
->block
.data
;
81 __isl_give isl_vec
*isl_vec_zero_extend(__isl_take isl_vec
*vec
, unsigned size
)
87 if (size
<= vec
->size
)
90 vec
= isl_vec_cow(vec
);
94 extra
= size
- vec
->size
;
95 vec
= isl_vec_extend(vec
, size
);
99 isl_seq_clr(vec
->el
+ size
- extra
, extra
);
104 /* Return a vector containing the elements of "vec1" followed by
107 __isl_give isl_vec
*isl_vec_concat(__isl_take isl_vec
*vec1
,
108 __isl_take isl_vec
*vec2
)
113 if (vec2
->size
== 0) {
118 if (vec1
->size
== 0) {
123 vec1
= isl_vec_extend(vec1
, vec1
->size
+ vec2
->size
);
127 isl_seq_cpy(vec1
->el
+ vec1
->size
- vec2
->size
, vec2
->el
, vec2
->size
);
137 struct isl_vec
*isl_vec_copy(struct isl_vec
*vec
)
146 struct isl_vec
*isl_vec_dup(struct isl_vec
*vec
)
148 struct isl_vec
*vec2
;
152 vec2
= isl_vec_alloc(vec
->ctx
, vec
->size
);
155 isl_seq_cpy(vec2
->el
, vec
->el
, vec
->size
);
159 struct isl_vec
*isl_vec_cow(struct isl_vec
*vec
)
161 struct isl_vec
*vec2
;
168 vec2
= isl_vec_dup(vec
);
173 __isl_null isl_vec
*isl_vec_free(__isl_take isl_vec
*vec
)
181 isl_ctx_deref(vec
->ctx
);
182 isl_blk_free(vec
->ctx
, vec
->block
);
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
)
198 if (pos
< 0 || pos
>= vec
->size
)
199 isl_die(vec
->ctx
, isl_error_invalid
, "position out of range",
201 isl_int_set(*v
, vec
->el
[pos
]);
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
)
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",
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
,
223 vec
= isl_vec_cow(vec
);
226 if (pos
< 0 || pos
>= vec
->size
)
227 isl_die(vec
->ctx
, isl_error_invalid
, "position out of range",
229 isl_int_set(vec
->el
[pos
], v
);
236 __isl_give isl_vec
*isl_vec_set_element_si(__isl_take isl_vec
*vec
,
239 vec
= isl_vec_cow(vec
);
242 if (pos
< 0 || pos
>= vec
->size
)
243 isl_die(vec
->ctx
, isl_error_invalid
, "position out of range",
245 isl_int_set_si(vec
->el
[pos
], v
);
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
)
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
);
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
,
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
)
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
)
299 if (!printer
|| !vec
)
302 printer
= isl_printer_print_str(printer
, "[");
303 for (i
= 0; i
< vec
->size
; ++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
, "]");
312 isl_printer_free(printer
);
316 void isl_vec_dump(struct isl_vec
*vec
)
318 isl_printer
*printer
;
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
);
335 isl_seq_set(vec
->el
, v
, vec
->size
);
339 __isl_give isl_vec
*isl_vec_set_si(__isl_take isl_vec
*vec
, int v
)
341 vec
= isl_vec_cow(vec
);
344 isl_seq_set_si(vec
->el
, v
, vec
->size
);
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
);
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
);
368 __isl_give isl_vec
*isl_vec_clr(__isl_take isl_vec
*vec
)
370 vec
= isl_vec_cow(vec
);
373 isl_seq_clr(vec
->el
, vec
->size
);
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
);
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);
398 struct isl_vec
*isl_vec_normalize(struct isl_vec
*vec
)
402 isl_seq_normalize(vec
->ctx
, vec
->el
, vec
->size
);
406 __isl_give isl_vec
*isl_vec_neg(__isl_take isl_vec
*vec
)
408 vec
= isl_vec_cow(vec
);
411 isl_seq_neg(vec
->el
, vec
->el
, vec
->size
);
415 __isl_give isl_vec
*isl_vec_scale(__isl_take isl_vec
*vec
, isl_int m
)
417 if (isl_int_is_one(m
))
419 vec
= isl_vec_cow(vec
);
422 isl_seq_scale(vec
->el
, vec
->el
, m
, vec
->size
);
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
);
434 isl_seq_fdiv_r(vec
->el
, vec
->el
, m
, vec
->size
);
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
);
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
);
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
)
472 qsort(vec
->el
, vec
->size
, sizeof(*vec
->el
), &qsort_int_cmp
);
477 __isl_give isl_vec
*isl_vec_drop_els(__isl_take isl_vec
*vec
,
478 unsigned pos
, unsigned n
)
482 vec
= isl_vec_cow(vec
);
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
);
502 __isl_give isl_vec
*isl_vec_insert_els(__isl_take isl_vec
*vec
,
503 unsigned pos
, unsigned n
)
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
);
520 isl_seq_cpy(ext
->el
, vec
->el
, pos
);
521 isl_seq_cpy(ext
->el
+ pos
+ n
, vec
->el
+ pos
, vec
->size
- pos
);
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
);
538 isl_seq_clr(vec
->el
+ pos
, n
);
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
548 __isl_give isl_vec
*isl_vec_move_els(__isl_take isl_vec
*vec
,
549 unsigned dst_pos
, unsigned src_pos
, unsigned n
)
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
)
567 res
= isl_vec_alloc(vec
->ctx
, vec
->size
);
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
);
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
);