add isl_polyhedron_minimize application
[isl.git] / isl_vec.c
blob2d1d644005fe8aa296723c2fed531d7b53a5af78
1 #include "isl_vec.h"
3 struct isl_vec *isl_vec_alloc(struct isl_ctx *ctx, unsigned size)
5 struct isl_vec *vec;
7 vec = isl_alloc_type(ctx, struct isl_vec);
8 if (!vec)
9 return NULL;
11 vec->block = isl_blk_alloc(ctx, size);
12 if (isl_blk_is_error(vec->block))
13 goto error;
15 vec->ctx = ctx;
16 isl_ctx_ref(ctx);
17 vec->ref = 1;
18 vec->size = size;
19 vec->el = vec->block.data;
21 return vec;
22 error:
23 isl_blk_free(ctx, vec->block);
24 return NULL;
27 struct isl_vec *isl_vec_copy(struct isl_vec *vec)
29 if (!vec)
30 return NULL;
32 vec->ref++;
33 return vec;
36 struct isl_vec *isl_vec_dup(struct isl_vec *vec)
38 struct isl_vec *vec2;
40 if (!vec)
41 return NULL;
42 vec2 = isl_vec_alloc(vec->ctx, vec->size);
43 isl_seq_cpy(vec2->el, vec->el, vec->size);
44 return vec2;
47 struct isl_vec *isl_vec_cow(struct isl_vec *vec)
49 struct isl_vec *vec2;
50 if (!vec)
51 return NULL;
53 if (vec->ref == 1)
54 return vec;
56 vec2 = isl_vec_dup(vec);
57 isl_vec_free( vec);
58 return vec2;
61 void isl_vec_free(struct isl_vec *vec)
63 if (!vec)
64 return;
66 if (--vec->ref > 0)
67 return;
69 isl_ctx_deref(vec->ctx);
70 isl_blk_free(vec->ctx, vec->block);
71 free(vec);
74 void isl_vec_dump(struct isl_vec *vec, FILE *out, int indent)
76 int i;
78 if (!vec) {
79 fprintf(out, "%*snull vec\n", indent, "");
80 return;
83 fprintf(out, "%*s[", indent, "");
84 for (i = 0; i < vec->size; ++i) {
85 if (i)
86 fprintf(out, ",");
87 isl_int_print(out, vec->el[i], 0);
89 fprintf(out, "]\n");
92 void isl_vec_lcm(struct isl_vec *vec, isl_int *lcm)
94 isl_seq_lcm(vec->block.data, vec->size, lcm);
97 /* Given a rational vector, with the denominator in the first element
98 * of the vector, round up all coordinates.
100 struct isl_vec *isl_vec_ceil(struct isl_vec *vec)
102 int i;
104 vec = isl_vec_cow(vec);
105 if (!vec)
106 return NULL;
108 isl_seq_cdiv_q(vec->el + 1, vec->el + 1, vec->el[0], vec->size - 1);
110 isl_int_set_si(vec->el[0], 1);
112 return vec;