Switch to Google coding style
[openscop.git] / source / vector.c
blob6355c86baac5ca1ebbaf2564108824e7526b0b9e
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** vector.c **
6 **-----------------------------------------------------------------**
7 ** First version: 30/04/2008 **
8 **-----------------------------------------------------------------**
11 *****************************************************************************
12 * OpenScop: Structures and formats for polyhedral tools to talk together *
13 *****************************************************************************
14 * ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__, *
15 * / / / // // // // / / / // // / / // / /|,_, *
16 * / / / // // // // / / / // // / / // / / / /\ *
17 * |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/ \ *
18 * | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\ \ /\ *
19 * | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\ *
20 * | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \ \ *
21 * | P |n| l | = | s | t |=| = |d| = | = | = | | |=| o | | \# \ \ *
22 * | H | | y | | e | o | | = |l| | | = | | | | G | | \ \ \ *
23 * | I | | | | e | | | | | | | | | | | | | \ \ \ *
24 * | T | | | | | | | | | | | | | | | | | \ \ \ *
25 * | E | | | | | | | | | | | | | | | | | \ \ \ *
26 * | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | / \* \ \ *
27 * | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/ \ \ / *
28 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
29 * *
30 * Copyright (C) 2008 University Paris-Sud 11 and INRIA *
31 * *
32 * (3-clause BSD license) *
33 * Redistribution and use in source and binary forms, with or without *
34 * modification, are permitted provided that the following conditions *
35 * are met: *
36 * *
37 * 1. Redistributions of source code must retain the above copyright notice, *
38 * this list of conditions and the following disclaimer. *
39 * 2. Redistributions in binary form must reproduce the above copyright *
40 * notice, this list of conditions and the following disclaimer in the *
41 * documentation and/or other materials provided with the distribution. *
42 * 3. The name of the author may not be used to endorse or promote products *
43 * derived from this software without specific prior written permission. *
44 * *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR *
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. *
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, *
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT *
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF *
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
55 * *
56 * OpenScop Library, a library to manipulate OpenScop formats and data *
57 * structures. Written by: *
58 * Cedric Bastoul <Cedric.Bastoul@u-psud.fr> and *
59 * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr> *
60 * *
61 *****************************************************************************/
64 # include <stdlib.h>
65 # include <stdio.h>
66 # include <ctype.h>
67 # include <openscop/vector.h>
70 /*+***************************************************************************
71 * Structure display function *
72 *****************************************************************************/
75 /**
76 * openscop_vector_print_structure function:
77 * Displays a openscop_vector_t structure (*vector) into a file (file, possibly
78 * stdout) in a way that trends to be understandable without falling in a deep
79 * depression or, for the lucky ones, getting a headache... It includes an
80 * indentation level (level) in order to work with others print_structure
81 * functions.
82 * \param file File where informations are printed.
83 * \param vector The vector whose information have to be printed.
84 * \param level Number of spaces before printing, for each line.
86 void openscop_vector_print_structure(FILE * file, openscop_vector_p vector,
87 int level) {
88 int j;
90 if (vector != NULL) {
91 // Go to the right level.
92 for (j = 0; j < level; j++)
93 fprintf(file,"|\t");
94 fprintf(file,"+-- openscop_vector_t\n");
96 for (j = 0; j <= level; j++)
97 fprintf(file,"|\t");
98 fprintf(file,"%d\n",vector->size);
100 // Display the vector.
101 for (j = 0; j <= level; j++)
102 fprintf(file,"|\t");
104 fprintf(file,"[ ");
106 for (j = 0; j < vector->size; j++) {
107 SCOPINT_print(file,OPENSCOP_FMT,vector->v[j]);
108 fprintf(file," ");
111 fprintf(file,"]\n");
113 else {
114 // Go to the right level.
115 for (j = 0; j < level; j++)
116 fprintf(file,"|\t");
117 fprintf(file,"+-- NULL vector\n");
120 // The last line.
121 for (j = 0; j <= level; j++)
122 fprintf(file,"|\t");
123 fprintf(file,"\n");
128 * openscop_vector_print function:
129 * This function prints the content of a openscop_vector_t structure
130 * (*vector) into a file (file, possibly stdout).
131 * \param file File where informations are printed.
132 * \param vector The vector whose information have to be printed.
134 void openscop_vector_print(FILE * file, openscop_vector_p vector) {
135 openscop_vector_print_structure(file,vector,0);
139 /*+***************************************************************************
140 * Memory allocation/deallocation function *
141 *****************************************************************************/
145 * openscop_vector_malloc function:
146 * This function allocates the memory space for a openscop_vector_t structure
147 * and sets its fields with default values. Then it returns a pointer to the
148 * allocated space.
149 * \param size The number of entries of the vector to allocate.
150 * \return A pointer to the newly allocated openscop_vector_t structure.
152 openscop_vector_p openscop_vector_malloc(unsigned size) {
153 openscop_vector_p vector;
154 openscop_int_t * p;
155 int i;
157 vector = (openscop_vector_p)malloc(sizeof(openscop_vector_t));
158 if (vector == NULL) {
159 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
160 exit(1);
162 vector->size = size;
163 if (size == 0) {
164 vector->v = NULL;
166 else {
167 p = (openscop_int_t *)malloc(size * sizeof(openscop_int_t));
168 if (p == NULL) {
169 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
170 exit(1);
172 vector->v = p;
173 for (i = 0; i < size; i++)
174 SCOPINT_init_set_si(vector->v[i],0);
176 return vector;
181 * openscop_vector_free function:
182 * This function frees the allocated memory for a openscop_vector_t structure.
183 * \param vector The pointer to the vector we want to free.
185 void openscop_vector_free(openscop_vector_p vector) {
186 int i;
187 openscop_int_t * p;
189 if (vector != NULL) {
190 p = vector->v;
191 for (i = 0; i < vector->size; i++)
192 SCOPINT_clear(*p++);
194 free(vector->v);
195 free(vector);
200 /*+***************************************************************************
201 * Processing functions *
202 *****************************************************************************/
206 * openscop_vector_add_scalar function:
207 * This function adds a scalar to the vector representation of an affine
208 * expression (this means we add the scalar only to the very last entry of the
209 * vector). It returns a new vector resulting from this addition.
210 * \param vector The basis vector.
211 * \param scalar The scalar to add to the vector.
212 * \return A pointer to a new vector, copy of the basis one plus the scalar.
214 openscop_vector_p openscop_vector_add_scalar(openscop_vector_p vector,
215 int scalar) {
216 int i;
217 openscop_vector_p result;
219 if ((vector == NULL) || (vector->size < 2)) {
220 fprintf(stderr,"[OpenScop] Error: incompatible vector for addition.\n");
221 exit(1);
224 result = openscop_vector_malloc(vector->size);
225 for (i = 0; i < vector->size; i++)
226 SCOPINT_assign(result->v[i],vector->v[i]);
227 SCOPINT_add_int(result->v[vector->size - 1],
228 vector->v[vector->size - 1],scalar);
230 return result;
235 * openscop_vector_add function:
236 * This function achieves the addition of two vectors and returns the
237 * result as a new vector (the addition means the ith entry of the new vector
238 * is equal to the ith entry of vector v1 plus the ith entry of vector v2).
239 * \param v1 The first vector for the addition.
240 * \param v2 The second vector for the addition.
241 * \return A pointer to a new vector, corresponding to v1 + v2.
243 openscop_vector_p openscop_vector_add(openscop_vector_p v1,
244 openscop_vector_p v2) {
245 int i;
246 openscop_vector_p v3;
248 if ((v1 == NULL) || (v2 == NULL) || (v1->size != v2->size)) {
249 fprintf(stderr,"[OpenScop] Error: incompatible vectors for addition.\n");
250 exit(1);
253 v3 = openscop_vector_malloc(v1->size);
254 for (i = 0; i < v1->size; i++)
255 SCOPINT_addto(v3->v[i],v1->v[i],v2->v[i]);
257 return v3;
262 * openscop_vector_sub function:
263 * This function achieves the subtraction of two vectors and returns the
264 * result as a new vector (the addition means the ith entry of the new vector
265 * is equal to the ith entry of vector v1 minus the ith entry of vector v2).
266 * \param v1 The first vector for the subtraction.
267 * \param v2 The second vector for the subtraction (result is v1-v2).
268 * \return A pointer to a new vector, corresponding to v1 - v2.
270 openscop_vector_p openscop_vector_sub(openscop_vector_p v1,
271 openscop_vector_p v2) {
272 int i;
273 openscop_vector_p v3;
275 if ((v1 == NULL) || (v2 == NULL) || (v1->size != v2->size)) {
276 fprintf(stderr,"[OpenScop] Error: "
277 "incompatible vectors for subtraction.\n");
278 exit(1);
281 v3 = openscop_vector_malloc(v1->size);
282 for (i = 0; i < v1->size; i++)
283 SCOPINT_subtract(v3->v[i],v1->v[i],v2->v[i]);
285 return v3;
290 * openscop_vector_tag_inequality function:
291 * This function tags a vector representation of a contraint as being an
292 * inequality >=0. This means in the PolyLib format, to set to 1 the very
293 * first entry of the vector. It modifies directly the vector provided as
294 * an argument.
295 * \param vector The vector to be tagged.
297 void openscop_vector_tag_inequality(openscop_vector_p vector) {
298 if ((vector == NULL) || (vector->size < 1)) {
299 fprintf(stderr,"[OpenScop] Error: vector cannot be tagged.\n");
300 exit(1);
302 SCOPINT_set_si(vector->v[0],1);
307 * openscop_vector_tag_equality function:
308 * This function tags a vector representation of a contraint as being an
309 * equality ==0. This means in the PolyLib format, to set to 0 the very
310 * first entry of the vector. It modifies directly the vector provided as
311 * an argument.
312 * \param vector The vector to be tagged.
314 void openscop_vector_tag_equality(openscop_vector_p vector) {
315 if ((vector == NULL) || (vector->size < 1)) {
316 fprintf(stderr,"[OpenScop] Error: vector cannot be tagged.\n");
317 exit(1);
319 SCOPINT_set_si(vector->v[0],0);
324 * openscop_vector_equal function:
325 * this function returns true if the two vectors are the same, false
326 * otherwise.
327 * \param v1 The first vector.
328 * \param v2 The second vector.
329 * \return 1 if v1 and v2 are the same (content-wise), 0 otherwise.
331 int openscop_vector_equal(openscop_vector_p v1, openscop_vector_p v2) {
332 int i;
334 if (v1->size != v2->size)
335 return 0;
337 for (i = 0; i < v1->size; i++)
338 if (SCOPINT_ne(v1->v[i], v2->v[i]))
339 return 0;
341 return 1;
346 * openscop_vector_mul_scalar function:
347 * this function returns a new vector corresponding to the one provided
348 * as parameter with each entry multiplied by a scalar.
349 * \param v The vector to multiply.
350 * \param scalar The scalar coefficient.
351 * \return A new vector corresponding to scalar * v.
353 openscop_vector_p openscop_vector_mul_scalar(openscop_vector_p v,
354 int scalar) {
355 int i;
356 openscop_vector_p result = openscop_vector_malloc(v->size);
358 for(i = 0; i < v->size; i++)
359 SCOPINT_multo(result->v[i], scalar, v->v[i]);
361 return result;