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
);
58 __isl_give isl_vec
*isl_vec_extend(__isl_take isl_vec
*vec
, unsigned size
)
62 if (size
<= vec
->size
)
65 vec
= isl_vec_cow(vec
);
69 vec
->block
= isl_blk_extend(vec
->ctx
, vec
->block
, size
);
74 vec
->el
= vec
->block
.data
;
82 /* Apply the expansion specified by "exp" to the "n" elements starting at "pos".
83 * "expanded" it the number of elements that need to replace those "n"
84 * elements. The entries in "exp" have increasing values between
87 __isl_give isl_vec
*isl_vec_expand(__isl_take isl_vec
*vec
, int pos
, int n
,
88 int *exp
, int expanded
)
96 isl_die(isl_vec_get_ctx(vec
), isl_error_invalid
,
97 "not an expansion", return isl_vec_free(vec
));
100 if (pos
< 0 || n
< 0 || pos
+ n
> vec
->size
)
101 isl_die(isl_vec_get_ctx(vec
), isl_error_invalid
,
102 "position out of bounds", return isl_vec_free(vec
));
104 old_size
= vec
->size
;
105 extra
= expanded
- n
;
106 vec
= isl_vec_extend(vec
, old_size
+ extra
);
107 vec
= isl_vec_cow(vec
);
111 for (i
= old_size
- 1; i
>= pos
+ n
; --i
)
112 isl_int_set(vec
->el
[i
+ extra
], vec
->el
[i
]);
115 for (i
= expanded
- 1; i
>= 0; --i
) {
116 if (j
>= 0 && exp
[j
] == i
) {
118 isl_int_swap(vec
->el
[pos
+ i
],
122 isl_int_set_si(vec
->el
[pos
+ i
], 0);
129 __isl_give isl_vec
*isl_vec_zero_extend(__isl_take isl_vec
*vec
, unsigned size
)
135 if (size
<= vec
->size
)
138 vec
= isl_vec_cow(vec
);
142 extra
= size
- vec
->size
;
143 vec
= isl_vec_extend(vec
, size
);
147 isl_seq_clr(vec
->el
+ size
- extra
, extra
);
152 /* Return a vector containing the elements of "vec1" followed by
155 __isl_give isl_vec
*isl_vec_concat(__isl_take isl_vec
*vec1
,
156 __isl_take isl_vec
*vec2
)
161 if (vec2
->size
== 0) {
166 if (vec1
->size
== 0) {
171 vec1
= isl_vec_extend(vec1
, vec1
->size
+ vec2
->size
);
175 isl_seq_cpy(vec1
->el
+ vec1
->size
- vec2
->size
, vec2
->el
, vec2
->size
);
185 struct isl_vec
*isl_vec_copy(struct isl_vec
*vec
)
194 struct isl_vec
*isl_vec_dup(struct isl_vec
*vec
)
196 struct isl_vec
*vec2
;
200 vec2
= isl_vec_alloc(vec
->ctx
, vec
->size
);
203 isl_seq_cpy(vec2
->el
, vec
->el
, vec
->size
);
207 struct isl_vec
*isl_vec_cow(struct isl_vec
*vec
)
209 struct isl_vec
*vec2
;
216 vec2
= isl_vec_dup(vec
);
221 __isl_null isl_vec
*isl_vec_free(__isl_take isl_vec
*vec
)
229 isl_ctx_deref(vec
->ctx
);
230 isl_blk_free(vec
->ctx
, vec
->block
);
236 int isl_vec_size(__isl_keep isl_vec
*vec
)
238 return vec
? vec
->size
: -1;
241 int isl_vec_get_element(__isl_keep isl_vec
*vec
, int pos
, isl_int
*v
)
246 if (pos
< 0 || pos
>= vec
->size
)
247 isl_die(vec
->ctx
, isl_error_invalid
, "position out of range",
249 isl_int_set(*v
, vec
->el
[pos
]);
253 /* Extract the element at position "pos" of "vec".
255 __isl_give isl_val
*isl_vec_get_element_val(__isl_keep isl_vec
*vec
, int pos
)
261 ctx
= isl_vec_get_ctx(vec
);
262 if (pos
< 0 || pos
>= vec
->size
)
263 isl_die(ctx
, isl_error_invalid
, "position out of range",
265 return isl_val_int_from_isl_int(ctx
, vec
->el
[pos
]);
268 __isl_give isl_vec
*isl_vec_set_element(__isl_take isl_vec
*vec
,
271 vec
= isl_vec_cow(vec
);
274 if (pos
< 0 || pos
>= vec
->size
)
275 isl_die(vec
->ctx
, isl_error_invalid
, "position out of range",
277 isl_int_set(vec
->el
[pos
], v
);
284 __isl_give isl_vec
*isl_vec_set_element_si(__isl_take isl_vec
*vec
,
287 vec
= isl_vec_cow(vec
);
290 if (pos
< 0 || pos
>= vec
->size
)
291 isl_die(vec
->ctx
, isl_error_invalid
, "position out of range",
293 isl_int_set_si(vec
->el
[pos
], v
);
300 /* Replace the element at position "pos" of "vec" by "v".
302 __isl_give isl_vec
*isl_vec_set_element_val(__isl_take isl_vec
*vec
,
303 int pos
, __isl_take isl_val
*v
)
306 return isl_vec_free(vec
);
307 if (!isl_val_is_int(v
))
308 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
309 "expecting integer value", goto error
);
310 vec
= isl_vec_set_element(vec
, pos
, v
->n
);
315 return isl_vec_free(vec
);
318 /* Compare the elements of "vec1" and "vec2" at position "pos".
320 int isl_vec_cmp_element(__isl_keep isl_vec
*vec1
, __isl_keep isl_vec
*vec2
,
325 if (pos
< 0 || pos
>= vec1
->size
|| pos
>= vec2
->size
)
326 isl_die(isl_vec_get_ctx(vec1
), isl_error_invalid
,
327 "position out of range", return 0);
328 return isl_int_cmp(vec1
->el
[pos
], vec2
->el
[pos
]);
331 /* Does "vec" contain only zero elements?
333 isl_bool
isl_vec_is_zero(__isl_keep isl_vec
*vec
)
336 return isl_bool_error
;
337 return isl_seq_first_non_zero(vec
->el
, vec
->size
) < 0;
340 isl_bool
isl_vec_is_equal(__isl_keep isl_vec
*vec1
, __isl_keep isl_vec
*vec2
)
343 return isl_bool_error
;
345 if (vec1
->size
!= vec2
->size
)
346 return isl_bool_false
;
348 return isl_seq_eq(vec1
->el
, vec2
->el
, vec1
->size
);
351 __isl_give isl_printer
*isl_printer_print_vec(__isl_take isl_printer
*printer
,
352 __isl_keep isl_vec
*vec
)
356 if (!printer
|| !vec
)
359 printer
= isl_printer_print_str(printer
, "[");
360 for (i
= 0; i
< vec
->size
; ++i
) {
362 printer
= isl_printer_print_str(printer
, ",");
363 printer
= isl_printer_print_isl_int(printer
, vec
->el
[i
]);
365 printer
= isl_printer_print_str(printer
, "]");
369 isl_printer_free(printer
);
373 void isl_vec_dump(struct isl_vec
*vec
)
375 isl_printer
*printer
;
380 printer
= isl_printer_to_file(vec
->ctx
, stderr
);
381 printer
= isl_printer_print_vec(printer
, vec
);
382 printer
= isl_printer_end_line(printer
);
384 isl_printer_free(printer
);
387 __isl_give isl_vec
*isl_vec_set(__isl_take isl_vec
*vec
, isl_int v
)
389 vec
= isl_vec_cow(vec
);
392 isl_seq_set(vec
->el
, v
, vec
->size
);
396 __isl_give isl_vec
*isl_vec_set_si(__isl_take isl_vec
*vec
, int v
)
398 vec
= isl_vec_cow(vec
);
401 isl_seq_set_si(vec
->el
, v
, vec
->size
);
405 /* Replace all elements of "vec" by "v".
407 __isl_give isl_vec
*isl_vec_set_val(__isl_take isl_vec
*vec
,
408 __isl_take isl_val
*v
)
410 vec
= isl_vec_cow(vec
);
413 if (!isl_val_is_int(v
))
414 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
415 "expecting integer value", goto error
);
416 isl_seq_set(vec
->el
, v
->n
, vec
->size
);
425 __isl_give isl_vec
*isl_vec_clr(__isl_take isl_vec
*vec
)
427 vec
= isl_vec_cow(vec
);
430 isl_seq_clr(vec
->el
, vec
->size
);
434 void isl_vec_lcm(struct isl_vec
*vec
, isl_int
*lcm
)
436 isl_seq_lcm(vec
->block
.data
, vec
->size
, lcm
);
439 /* Given a rational vector, with the denominator in the first element
440 * of the vector, round up all coordinates.
442 struct isl_vec
*isl_vec_ceil(struct isl_vec
*vec
)
444 vec
= isl_vec_cow(vec
);
448 isl_seq_cdiv_q(vec
->el
+ 1, vec
->el
+ 1, vec
->el
[0], vec
->size
- 1);
450 isl_int_set_si(vec
->el
[0], 1);
455 struct isl_vec
*isl_vec_normalize(struct isl_vec
*vec
)
459 isl_seq_normalize(vec
->ctx
, vec
->el
, vec
->size
);
463 __isl_give isl_vec
*isl_vec_neg(__isl_take isl_vec
*vec
)
465 vec
= isl_vec_cow(vec
);
468 isl_seq_neg(vec
->el
, vec
->el
, vec
->size
);
472 __isl_give isl_vec
*isl_vec_scale(__isl_take isl_vec
*vec
, isl_int m
)
474 if (isl_int_is_one(m
))
476 vec
= isl_vec_cow(vec
);
479 isl_seq_scale(vec
->el
, vec
->el
, m
, vec
->size
);
483 /* Reduce the elements of "vec" modulo "m".
485 __isl_give isl_vec
*isl_vec_fdiv_r(__isl_take isl_vec
*vec
, isl_int m
)
487 vec
= isl_vec_cow(vec
);
491 isl_seq_fdiv_r(vec
->el
, vec
->el
, m
, vec
->size
);
496 __isl_give isl_vec
*isl_vec_add(__isl_take isl_vec
*vec1
,
497 __isl_take isl_vec
*vec2
)
499 vec1
= isl_vec_cow(vec1
);
503 isl_assert(vec1
->ctx
, vec1
->size
== vec2
->size
, goto error
);
505 isl_seq_combine(vec1
->el
, vec1
->ctx
->one
, vec1
->el
,
506 vec1
->ctx
->one
, vec2
->el
, vec1
->size
);
516 static int qsort_int_cmp(const void *p1
, const void *p2
)
518 const isl_int
*i1
= (const isl_int
*) p1
;
519 const isl_int
*i2
= (const isl_int
*) p2
;
521 return isl_int_cmp(*i1
, *i2
);
524 __isl_give isl_vec
*isl_vec_sort(__isl_take isl_vec
*vec
)
529 qsort(vec
->el
, vec
->size
, sizeof(*vec
->el
), &qsort_int_cmp
);
534 __isl_give isl_vec
*isl_vec_drop_els(__isl_take isl_vec
*vec
,
535 unsigned pos
, unsigned n
)
539 vec
= isl_vec_cow(vec
);
543 if (pos
+ n
> vec
->size
)
544 isl_die(vec
->ctx
, isl_error_invalid
,
545 "range out of bounds", goto error
);
547 if (pos
+ n
!= vec
->size
)
548 isl_seq_cpy(vec
->el
+ pos
, vec
->el
+ pos
+ n
,
549 vec
->size
- pos
- n
);
559 __isl_give isl_vec
*isl_vec_insert_els(__isl_take isl_vec
*vec
,
560 unsigned pos
, unsigned n
)
570 isl_die(vec
->ctx
, isl_error_invalid
,
571 "position out of bounds", goto error
);
573 ext
= isl_vec_alloc(vec
->ctx
, vec
->size
+ n
);
577 isl_seq_cpy(ext
->el
, vec
->el
, pos
);
578 isl_seq_cpy(ext
->el
+ pos
+ n
, vec
->el
+ pos
, vec
->size
- pos
);
588 __isl_give isl_vec
*isl_vec_insert_zero_els(__isl_take isl_vec
*vec
,
589 unsigned pos
, unsigned n
)
591 vec
= isl_vec_insert_els(vec
, pos
, n
);
595 isl_seq_clr(vec
->el
+ pos
, n
);
600 /* Move the "n" elements starting as "src_pos" of "vec"
601 * to "dst_pos". The elements originally at "dst_pos" are moved
602 * up or down depending on whether "dst_pos" is smaller or greater
605 __isl_give isl_vec
*isl_vec_move_els(__isl_take isl_vec
*vec
,
606 unsigned dst_pos
, unsigned src_pos
, unsigned n
)
613 if (src_pos
+ n
> vec
->size
)
614 isl_die(vec
->ctx
, isl_error_invalid
,
615 "source range out of bounds", return isl_vec_free(vec
));
616 if (dst_pos
+ n
> vec
->size
)
617 isl_die(vec
->ctx
, isl_error_invalid
,
618 "destination range out of bounds",
619 return isl_vec_free(vec
));
621 if (n
== 0 || dst_pos
== src_pos
)
624 res
= isl_vec_alloc(vec
->ctx
, vec
->size
);
626 return isl_vec_free(vec
);
628 if (dst_pos
< src_pos
) {
629 isl_seq_cpy(res
->el
, vec
->el
, dst_pos
);
630 isl_seq_cpy(res
->el
+ dst_pos
, vec
->el
+ src_pos
, n
);
631 isl_seq_cpy(res
->el
+ dst_pos
+ n
,
632 vec
->el
+ dst_pos
, src_pos
- dst_pos
);
633 isl_seq_cpy(res
->el
+ src_pos
+ n
,
634 vec
->el
+ src_pos
+ n
, res
->size
- src_pos
- n
);
636 isl_seq_cpy(res
->el
, vec
->el
, src_pos
);
637 isl_seq_cpy(res
->el
+ src_pos
,
638 vec
->el
+ src_pos
+ n
, dst_pos
- src_pos
);
639 isl_seq_cpy(res
->el
+ dst_pos
, vec
->el
+ src_pos
, n
);
640 isl_seq_cpy(res
->el
+ dst_pos
+ n
,
641 vec
->el
+ dst_pos
+ n
, res
->size
- dst_pos
- n
);