Program and functions for input/output test
[openscop.git] / source / vector.c
blobda32e2357e684b1cfc6f78f2273f1b8df4c8b168
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 * - 01/05/2008: first version.
88 void
89 openscop_vector_print_structure(FILE * file, openscop_vector_p vector, int level)
91 int j;
93 if (vector != NULL)
95 /* Go to the right level. */
96 for (j = 0; j < level; j++)
97 fprintf(file,"|\t");
98 fprintf(file,"+-- openscop_vector_t\n");
100 for (j = 0; j <= level; j++)
101 fprintf(file,"|\t");
102 fprintf(file,"%d\n",vector->Size);
104 /* Display the vector. */
105 for (j = 0; j <= level; j++)
106 fprintf(file,"|\t");
108 fprintf(file,"[ ");
110 for (j = 0; j < vector->Size; j++)
112 SCOPINT_print(file,OPENSCOP_FMT,vector->p[j]);
113 fprintf(file," ");
116 fprintf(file,"]\n");
118 else
120 /* Go to the right level. */
121 for (j = 0; j < level; j++)
122 fprintf(file,"|\t");
123 fprintf(file,"+-- NULL vector\n");
126 /* The last line. */
127 for (j = 0; j <= level; j++)
128 fprintf(file,"|\t");
129 fprintf(file,"\n");
134 * openscop_vector_print function:
135 * This function prints the content of a openscop_vector_t structure
136 * (*vector) into a file (file, possibly stdout).
137 * \param file File where informations are printed.
138 * \param vector The vector whose information have to be printed.
140 * - 01/05/2008: first version.
142 void
143 openscop_vector_print(FILE * file, openscop_vector_p vector)
145 openscop_vector_print_structure(file,vector,0);
149 /*+****************************************************************************
150 * Memory allocation/deallocation function *
151 ******************************************************************************/
155 * openscop_vector_malloc function:
156 * This function allocates the memory space for a openscop_vector_t structure and
157 * sets its fields with default values. Then it returns a pointer to the
158 * allocated space.
159 * \param Size The number of entries of the vector to allocate.
161 * - 01/05/2008: first version.
163 openscop_vector_p
164 openscop_vector_malloc(unsigned Size)
166 openscop_vector_p vector;
167 openscop_int_t * p;
168 int i ;
170 vector = (openscop_vector_p)malloc(sizeof(openscop_vector_t));
171 if (vector == NULL)
173 fprintf(stderr, "[Scoplib] Memory Overflow.\n");
174 exit(1);
176 vector->Size = Size;
177 if (Size == 0)
178 vector->p = NULL;
179 else
181 p = (openscop_int_t *)malloc(Size * sizeof(openscop_int_t));
182 if (p == NULL)
184 fprintf(stderr, "[Scoplib] Memory Overflow.\n");
185 exit(1);
187 vector->p = p;
188 for (i = 0; i < Size; i++)
189 SCOPINT_init_set_si(vector->p[i],0);
191 return vector;
196 * openscop_vector_free function:
197 * This function frees the allocated memory for a openscop_vector_t structure.
198 * \param vector The pointer to the vector we want to free.
200 * - 01/05/2008: first version.
202 void
203 openscop_vector_free(openscop_vector_p vector)
205 int i;
206 openscop_int_t * p;
208 if (vector != NULL)
210 p = vector->p;
211 for (i = 0; i < vector->Size; i++)
212 SCOPINT_clear(*p++);
214 free(vector->p);
215 free(vector);
220 /*+****************************************************************************
221 * Processing functions *
222 ******************************************************************************/
225 * openscop_vector_add_scalar function:
226 * This function adds a scalar to the vector representation of an affine
227 * expression (this means we add the scalar only to the very last entry of the
228 * vector). It returns a new vector resulting from this addition.
229 * \param vector The basis vector.
230 * \param scalar The scalar to add to the vector.
232 * - 01/05/2008: first version.
234 openscop_vector_p
235 openscop_vector_add_scalar(openscop_vector_p vector, int scalar)
237 int i;
238 openscop_vector_p result;
240 if ((vector == NULL) || (vector->Size < 2))
242 fprintf(stderr,"[Scoplib] Error: incompatible vector for addition\n");
243 exit(1);
246 result = openscop_vector_malloc(vector->Size);
247 for (i = 0; i < vector->Size; i++)
248 SCOPINT_assign(result->p[i],vector->p[i]);
249 SCOPINT_add_int(result->p[vector->Size - 1],
250 vector->p[vector->Size - 1],scalar);
252 return result;
257 * openscop_vector_add function:
258 * This function achieves the addition of two vectors and returns the
259 * result as a new vector (the addition means the ith entry of the new vector
260 * is equal to the ith entry of vector v1 plus the ith entry of vector v2).
261 * \param v1 The first vector for the addition.
262 * \param v2 The second vector for the addition (result is v1+v2).
264 * - 01/05/2008: first version.
266 openscop_vector_p
267 openscop_vector_add(openscop_vector_p v1, openscop_vector_p v2)
269 int i;
270 openscop_vector_p v3;
272 if ((v1 == NULL) || (v2 == NULL) || (v1->Size != v2->Size))
274 fprintf(stderr,"[Scoplib] Error: incompatible vectors for addition\n");
275 exit(1);
278 v3 = openscop_vector_malloc(v1->Size);
279 for (i = 0; i < v1->Size; i++)
280 SCOPINT_addto(v3->p[i],v1->p[i],v2->p[i]);
282 return v3;
287 * openscop_vector_sub function:
288 * This function achieves the subtraction of two vectors and returns the
289 * result as a new vector (the addition means the ith entry of the new vector
290 * is equal to the ith entry of vector v1 minus the ith entry of vector v2).
291 * \param v1 The first vector for the subtraction.
292 * \param v2 The second vector for the subtraction (result is v1-v2).
294 * - 01/05/2008: first version.
296 openscop_vector_p
297 openscop_vector_sub(openscop_vector_p v1, openscop_vector_p v2)
299 int i;
300 openscop_vector_p v3;
302 if ((v1 == NULL) || (v2 == NULL) || (v1->Size != v2->Size))
304 fprintf(stderr,"[Scoplib] Error: incompatible vectors for subtraction\n");
305 exit(1);
308 v3 = openscop_vector_malloc(v1->Size);
309 for (i = 0; i < v1->Size; i++)
310 SCOPINT_subtract(v3->p[i],v1->p[i],v2->p[i]);
312 return v3;
317 * openscop_vector_tag_inequality function:
318 * This function tags a vector representation of a contraint as being an
319 * inequality >=0. This means in the PolyLib format, to set to 1 the very first
320 * entry of the vector. It modifies directly the vector provided as an argument.
321 * \param vector The vector to be tagged.
323 * - 01/05/2008: first version.
325 void
326 openscop_vector_tag_inequality(openscop_vector_p vector)
328 if ((vector == NULL) || (vector->Size < 1))
330 fprintf(stderr,"[Scoplib] Error: vector cannot be tagged\n");
331 exit(1);
333 SCOPINT_set_si(vector->p[0],1);
338 * openscop_vector_tag_equality function:
339 * This function tags a vector representation of a contraint as being an
340 * equality ==0. This means in the PolyLib format, to set to 0 the very first
341 * entry of the vector. It modifies directly the vector provided as an argument.
342 * \param vector The vector to be tagged.
344 * - 01/05/2008: first version.
346 void
347 openscop_vector_tag_equality(openscop_vector_p vector)
349 if ((vector == NULL) || (vector->Size < 1))
351 fprintf(stderr,"[Scoplib] Error: vector cannot be tagged\n");
352 exit(1);
354 SCOPINT_set_si(vector->p[0],0);
359 * openscop_vector_equal function:
360 * this function returns true if the two vectors are the same, false
361 * otherwise.
362 * \param v1 The first vector.
363 * \param v2 The second vector.
364 * \return 1 if v1 and v2 are the same (content-wise), 0 otherwise.
367 openscop_vector_equal(openscop_vector_p v1, openscop_vector_p v2)
369 int i;
371 if (v1->Size != v2->Size)
372 return 0;
374 for (i = 0; i < v1->Size; i++)
375 if (SCOPINT_ne(v1->p[i], v2->p[i]))
376 return 0;
378 return 1;