2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 #include <isl_ctx_private.h>
15 #include <isl_val_private.h>
16 #include <isl_vec_private.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 __isl_give 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 /* Create a vector of size "size" with zero-valued elements.
131 __isl_give isl_vec
*isl_vec_zero(isl_ctx
*ctx
, unsigned size
)
135 vec
= isl_vec_alloc(ctx
, size
);
138 isl_seq_clr(vec
->el
, size
);
142 __isl_give isl_vec
*isl_vec_zero_extend(__isl_take isl_vec
*vec
, unsigned size
)
148 if (size
<= vec
->size
)
151 vec
= isl_vec_cow(vec
);
155 extra
= size
- vec
->size
;
156 vec
= isl_vec_extend(vec
, size
);
160 isl_seq_clr(vec
->el
+ size
- extra
, extra
);
165 /* Return a vector containing the elements of "vec1" followed by
168 __isl_give isl_vec
*isl_vec_concat(__isl_take isl_vec
*vec1
,
169 __isl_take isl_vec
*vec2
)
174 if (vec2
->size
== 0) {
179 if (vec1
->size
== 0) {
184 vec1
= isl_vec_extend(vec1
, vec1
->size
+ vec2
->size
);
188 isl_seq_cpy(vec1
->el
+ vec1
->size
- vec2
->size
, vec2
->el
, vec2
->size
);
198 __isl_give isl_vec
*isl_vec_copy(__isl_keep isl_vec
*vec
)
207 __isl_give isl_vec
*isl_vec_dup(__isl_keep isl_vec
*vec
)
209 struct isl_vec
*vec2
;
213 vec2
= isl_vec_alloc(vec
->ctx
, vec
->size
);
216 isl_seq_cpy(vec2
->el
, vec
->el
, vec
->size
);
220 __isl_give isl_vec
*isl_vec_cow(__isl_take isl_vec
*vec
)
222 struct isl_vec
*vec2
;
229 vec2
= isl_vec_dup(vec
);
234 __isl_null isl_vec
*isl_vec_free(__isl_take isl_vec
*vec
)
242 isl_ctx_deref(vec
->ctx
);
243 isl_blk_free(vec
->ctx
, vec
->block
);
249 isl_size
isl_vec_size(__isl_keep isl_vec
*vec
)
251 return vec
? vec
->size
: isl_size_error
;
254 /* Extract the element at position "pos" of "vec".
256 __isl_give isl_val
*isl_vec_get_element_val(__isl_keep isl_vec
*vec
, int pos
)
262 ctx
= isl_vec_get_ctx(vec
);
263 if (pos
< 0 || pos
>= vec
->size
)
264 isl_die(ctx
, isl_error_invalid
, "position out of range",
266 return isl_val_int_from_isl_int(ctx
, vec
->el
[pos
]);
269 __isl_give isl_vec
*isl_vec_set_element(__isl_take isl_vec
*vec
,
272 vec
= isl_vec_cow(vec
);
275 if (pos
< 0 || pos
>= vec
->size
)
276 isl_die(vec
->ctx
, isl_error_invalid
, "position out of range",
278 isl_int_set(vec
->el
[pos
], v
);
285 __isl_give isl_vec
*isl_vec_set_element_si(__isl_take isl_vec
*vec
,
288 vec
= isl_vec_cow(vec
);
291 if (pos
< 0 || pos
>= vec
->size
)
292 isl_die(vec
->ctx
, isl_error_invalid
, "position out of range",
294 isl_int_set_si(vec
->el
[pos
], v
);
301 /* Replace the element at position "pos" of "vec" by "v".
303 __isl_give isl_vec
*isl_vec_set_element_val(__isl_take isl_vec
*vec
,
304 int pos
, __isl_take isl_val
*v
)
307 return isl_vec_free(vec
);
308 if (!isl_val_is_int(v
))
309 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
310 "expecting integer value", goto error
);
311 vec
= isl_vec_set_element(vec
, pos
, v
->n
);
316 return isl_vec_free(vec
);
319 /* Compare the elements of "vec1" and "vec2" at position "pos".
321 int isl_vec_cmp_element(__isl_keep isl_vec
*vec1
, __isl_keep isl_vec
*vec2
,
326 if (pos
< 0 || pos
>= vec1
->size
|| pos
>= vec2
->size
)
327 isl_die(isl_vec_get_ctx(vec1
), isl_error_invalid
,
328 "position out of range", return 0);
329 return isl_int_cmp(vec1
->el
[pos
], vec2
->el
[pos
]);
332 /* Does "vec" contain only zero elements?
334 isl_bool
isl_vec_is_zero(__isl_keep isl_vec
*vec
)
337 return isl_bool_error
;
338 return isl_bool_ok(isl_seq_first_non_zero(vec
->el
, vec
->size
) < 0);
341 isl_bool
isl_vec_is_equal(__isl_keep isl_vec
*vec1
, __isl_keep isl_vec
*vec2
)
344 return isl_bool_error
;
346 if (vec1
->size
!= vec2
->size
)
347 return isl_bool_false
;
349 return isl_bool_ok(isl_seq_eq(vec1
->el
, vec2
->el
, vec1
->size
));
352 __isl_give isl_printer
*isl_printer_print_vec(__isl_take isl_printer
*printer
,
353 __isl_keep isl_vec
*vec
)
357 if (!printer
|| !vec
)
360 printer
= isl_printer_print_str(printer
, "[");
361 for (i
= 0; i
< vec
->size
; ++i
) {
363 printer
= isl_printer_print_str(printer
, ",");
364 printer
= isl_printer_print_isl_int(printer
, vec
->el
[i
]);
366 printer
= isl_printer_print_str(printer
, "]");
370 isl_printer_free(printer
);
374 void isl_vec_dump(__isl_keep isl_vec
*vec
)
376 isl_printer
*printer
;
381 printer
= isl_printer_to_file(vec
->ctx
, stderr
);
382 printer
= isl_printer_print_vec(printer
, vec
);
383 printer
= isl_printer_end_line(printer
);
385 isl_printer_free(printer
);
388 __isl_give isl_vec
*isl_vec_set(__isl_take isl_vec
*vec
, isl_int v
)
390 vec
= isl_vec_cow(vec
);
393 isl_seq_set(vec
->el
, v
, vec
->size
);
397 __isl_give isl_vec
*isl_vec_set_si(__isl_take isl_vec
*vec
, int v
)
399 vec
= isl_vec_cow(vec
);
402 isl_seq_set_si(vec
->el
, v
, vec
->size
);
406 /* Replace all elements of "vec" by "v".
408 __isl_give isl_vec
*isl_vec_set_val(__isl_take isl_vec
*vec
,
409 __isl_take isl_val
*v
)
411 vec
= isl_vec_cow(vec
);
414 if (!isl_val_is_int(v
))
415 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
416 "expecting integer value", goto error
);
417 isl_seq_set(vec
->el
, v
->n
, vec
->size
);
426 __isl_give isl_vec
*isl_vec_clr(__isl_take isl_vec
*vec
)
428 vec
= isl_vec_cow(vec
);
431 isl_seq_clr(vec
->el
, vec
->size
);
435 void isl_vec_lcm(__isl_keep isl_vec
*vec
, isl_int
*lcm
)
437 isl_seq_lcm(vec
->block
.data
, vec
->size
, lcm
);
440 /* Given a rational vector, with the denominator in the first element
441 * of the vector, round up all coordinates.
443 __isl_give isl_vec
*isl_vec_ceil(__isl_take isl_vec
*vec
)
445 vec
= isl_vec_cow(vec
);
449 isl_seq_cdiv_q(vec
->el
+ 1, vec
->el
+ 1, vec
->el
[0], vec
->size
- 1);
451 isl_int_set_si(vec
->el
[0], 1);
456 __isl_give isl_vec
*isl_vec_normalize(__isl_take isl_vec
*vec
)
460 isl_seq_normalize(vec
->ctx
, vec
->el
, vec
->size
);
464 __isl_give isl_vec
*isl_vec_neg(__isl_take isl_vec
*vec
)
466 vec
= isl_vec_cow(vec
);
469 isl_seq_neg(vec
->el
, vec
->el
, vec
->size
);
473 __isl_give isl_vec
*isl_vec_scale(__isl_take isl_vec
*vec
, isl_int m
)
475 if (isl_int_is_one(m
))
477 vec
= isl_vec_cow(vec
);
480 isl_seq_scale(vec
->el
, vec
->el
, m
, vec
->size
);
484 /* Reduce the elements of "vec" modulo "m".
486 __isl_give isl_vec
*isl_vec_fdiv_r(__isl_take isl_vec
*vec
, isl_int m
)
488 vec
= isl_vec_cow(vec
);
492 isl_seq_fdiv_r(vec
->el
, vec
->el
, m
, vec
->size
);
497 __isl_give isl_vec
*isl_vec_add(__isl_take isl_vec
*vec1
,
498 __isl_take isl_vec
*vec2
)
500 vec1
= isl_vec_cow(vec1
);
504 isl_assert(vec1
->ctx
, vec1
->size
== vec2
->size
, goto error
);
506 isl_seq_combine(vec1
->el
, vec1
->ctx
->one
, vec1
->el
,
507 vec1
->ctx
->one
, vec2
->el
, vec1
->size
);
517 static int qsort_int_cmp(const void *p1
, const void *p2
)
519 const isl_int
*i1
= (const isl_int
*) p1
;
520 const isl_int
*i2
= (const isl_int
*) p2
;
522 return isl_int_cmp(*i1
, *i2
);
525 __isl_give isl_vec
*isl_vec_sort(__isl_take isl_vec
*vec
)
530 qsort(vec
->el
, vec
->size
, sizeof(*vec
->el
), &qsort_int_cmp
);
535 __isl_give isl_vec
*isl_vec_drop_els(__isl_take isl_vec
*vec
,
536 unsigned pos
, unsigned n
)
540 vec
= isl_vec_cow(vec
);
544 if (pos
+ n
> vec
->size
)
545 isl_die(vec
->ctx
, isl_error_invalid
,
546 "range out of bounds", goto error
);
548 if (pos
+ n
!= vec
->size
)
549 isl_seq_cpy(vec
->el
+ pos
, vec
->el
+ pos
+ n
,
550 vec
->size
- pos
- n
);
560 __isl_give isl_vec
*isl_vec_insert_els(__isl_take isl_vec
*vec
,
561 unsigned pos
, unsigned n
)
571 isl_die(vec
->ctx
, isl_error_invalid
,
572 "position out of bounds", goto error
);
574 ext
= isl_vec_alloc(vec
->ctx
, vec
->size
+ n
);
578 isl_seq_cpy(ext
->el
, vec
->el
, pos
);
579 isl_seq_cpy(ext
->el
+ pos
+ n
, vec
->el
+ pos
, vec
->size
- pos
);
589 /* Add "n" elements at the end of "vec".
591 __isl_give isl_vec
*isl_vec_add_els(__isl_take isl_vec
*vec
, unsigned n
)
595 return isl_vec_insert_els(vec
, vec
->size
, n
);
598 __isl_give isl_vec
*isl_vec_insert_zero_els(__isl_take isl_vec
*vec
,
599 unsigned pos
, unsigned n
)
601 vec
= isl_vec_insert_els(vec
, pos
, n
);
605 isl_seq_clr(vec
->el
+ pos
, n
);
610 /* Move the "n" elements starting as "src_pos" of "vec"
611 * to "dst_pos". The elements originally at "dst_pos" are moved
612 * up or down depending on whether "dst_pos" is smaller or greater
615 __isl_give isl_vec
*isl_vec_move_els(__isl_take isl_vec
*vec
,
616 unsigned dst_pos
, unsigned src_pos
, unsigned n
)
623 if (src_pos
+ n
> vec
->size
)
624 isl_die(vec
->ctx
, isl_error_invalid
,
625 "source range out of bounds", return isl_vec_free(vec
));
626 if (dst_pos
+ n
> vec
->size
)
627 isl_die(vec
->ctx
, isl_error_invalid
,
628 "destination range out of bounds",
629 return isl_vec_free(vec
));
631 if (n
== 0 || dst_pos
== src_pos
)
634 res
= isl_vec_alloc(vec
->ctx
, vec
->size
);
636 return isl_vec_free(vec
);
638 if (dst_pos
< src_pos
) {
639 isl_seq_cpy(res
->el
, vec
->el
, dst_pos
);
640 isl_seq_cpy(res
->el
+ dst_pos
, vec
->el
+ src_pos
, n
);
641 isl_seq_cpy(res
->el
+ dst_pos
+ n
,
642 vec
->el
+ dst_pos
, src_pos
- dst_pos
);
643 isl_seq_cpy(res
->el
+ src_pos
+ n
,
644 vec
->el
+ src_pos
+ n
, res
->size
- src_pos
- n
);
646 isl_seq_cpy(res
->el
, vec
->el
, src_pos
);
647 isl_seq_cpy(res
->el
+ src_pos
,
648 vec
->el
+ src_pos
+ n
, dst_pos
- src_pos
);
649 isl_seq_cpy(res
->el
+ dst_pos
, vec
->el
+ src_pos
, n
);
650 isl_seq_cpy(res
->el
+ dst_pos
+ n
,
651 vec
->el
+ dst_pos
+ n
, res
->size
- dst_pos
- n
);
658 /* Reorder the elements of "vec" starting at "offset" based
659 * on the given reordering.
661 __isl_give isl_vec
*isl_vec_reorder(__isl_take isl_vec
*vec
,
662 unsigned offset
, __isl_take isl_reordering
*r
)
670 res
= isl_vec_alloc(vec
->ctx
, offset
+ r
->dst_len
);
673 isl_seq_cpy(res
->el
, vec
->el
, offset
);
674 isl_seq_clr(res
->el
+ offset
, res
->size
- offset
);
675 for (i
= 0; i
< r
->src_len
; ++i
)
676 isl_int_set(res
->el
[offset
+ r
->pos
[i
]], vec
->el
[offset
+ i
]);
678 isl_reordering_free(r
);
683 isl_reordering_free(r
);