export isl_basic_set_project_out
[isl.git] / isl_vec.c
blob4b30ce151c74a47d02018e82242a8174ce8daec5
1 #include "isl_seq.h"
2 #include "isl_vec.h"
4 struct isl_vec *isl_vec_alloc(struct isl_ctx *ctx, unsigned size)
6 struct isl_vec *vec;
8 vec = isl_alloc_type(ctx, struct isl_vec);
9 if (!vec)
10 return NULL;
12 vec->block = isl_blk_alloc(ctx, size);
13 if (isl_blk_is_error(vec->block))
14 goto error;
16 vec->ctx = ctx;
17 isl_ctx_ref(ctx);
18 vec->ref = 1;
19 vec->size = size;
20 vec->el = vec->block.data;
22 return vec;
23 error:
24 isl_blk_free(ctx, vec->block);
25 return NULL;
28 struct isl_vec *isl_vec_copy(struct isl_vec *vec)
30 if (!vec)
31 return NULL;
33 vec->ref++;
34 return vec;
37 struct isl_vec *isl_vec_dup(struct isl_vec *vec)
39 struct isl_vec *vec2;
41 if (!vec)
42 return NULL;
43 vec2 = isl_vec_alloc(vec->ctx, vec->size);
44 isl_seq_cpy(vec2->el, vec->el, vec->size);
45 return vec2;
48 struct isl_vec *isl_vec_cow(struct isl_vec *vec)
50 struct isl_vec *vec2;
51 if (!vec)
52 return NULL;
54 if (vec->ref == 1)
55 return vec;
57 vec2 = isl_vec_dup(vec);
58 isl_vec_free( vec);
59 return vec2;
62 void isl_vec_free(struct isl_vec *vec)
64 if (!vec)
65 return;
67 if (--vec->ref > 0)
68 return;
70 isl_ctx_deref(vec->ctx);
71 isl_blk_free(vec->ctx, vec->block);
72 free(vec);
75 void isl_vec_dump(struct isl_vec *vec, FILE *out, int indent)
77 int i;
79 if (!vec) {
80 fprintf(out, "%*snull vec\n", indent, "");
81 return;
84 fprintf(out, "%*s[", indent, "");
85 for (i = 0; i < vec->size; ++i) {
86 if (i)
87 fprintf(out, ",");
88 isl_int_print(out, vec->el[i], 0);
90 fprintf(out, "]\n");
93 void isl_vec_lcm(struct isl_vec *vec, isl_int *lcm)
95 isl_seq_lcm(vec->block.data, vec->size, lcm);
98 /* Given a rational vector, with the denominator in the first element
99 * of the vector, round up all coordinates.
101 struct isl_vec *isl_vec_ceil(struct isl_vec *vec)
103 vec = isl_vec_cow(vec);
104 if (!vec)
105 return NULL;
107 isl_seq_cdiv_q(vec->el + 1, vec->el + 1, vec->el[0], vec->size - 1);
109 isl_int_set_si(vec->el[0], 1);
111 return vec;
114 struct isl_vec *isl_vec_normalize(struct isl_vec *vec)
116 if (!vec)
117 return NULL;
118 isl_seq_normalize(vec->ctx, vec->el, vec->size);
119 return vec;