Switch from matrix to relation data structure
[openscop.git] / source / relation_list.c
blobb7fe516f1bd87867828fdc0956cc4ee99d4328fc
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** relation_list.c **
6 **-----------------------------------------------------------------**
7 ** First version: 08/10/2010 **
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 <string.h>
67 # include <ctype.h>
68 # include <openscop/relation_list.h>
71 /*+***************************************************************************
72 * Structure display function *
73 *****************************************************************************/
76 /**
77 * openscop_relation_list_print_structure function:
78 * Displays a openscop_relation_list_t structure (a list of relations) into a
79 * file (file, possibly stdout). See openscop_relation_print_structure for
80 * more details.
81 * \param file File where informations are printed.
82 * \param l The list of relations whose information have to be printed.
83 * \param level Number of spaces before printing, for each line.
85 void
86 openscop_relation_list_print_structure(FILE * file, openscop_relation_list_p l,
87 int level)
89 int j, first = 1;
91 // Go to the right level.
92 for (j = 0; j < level; j++)
93 fprintf(file,"|\t");
95 if (l != NULL)
96 fprintf(file, "+-- openscop_relation_list_t\n");
97 else
98 fprintf(file, "+-- NULL relation list\n");
100 while (l != NULL)
102 if (!first)
104 // Go to the right level.
105 for (j = 0; j < level; j++)
106 fprintf(file, "|\t");
107 fprintf(file, "| openscop_relation_list_t\n");
109 else
110 first = 0;
112 // A blank line.
113 for (j = 0; j <= level+1; j++)
114 fprintf(file, "|\t");
115 fprintf(file, "\n");
117 // Print a relation.
118 openscop_relation_print_structure(file, l->elt, level+1);
120 l = l->next;
122 // Next line.
123 if (l != NULL)
125 for (j = 0; j <= level; j++)
126 fprintf(file, "|\t");
127 fprintf(file, "V\n");
131 // The last line.
132 for (j = 0; j <= level; j++)
133 fprintf(file, "|\t");
134 fprintf(file, "\n");
139 * openscop_relation_list_print function:
140 * This function prints the content of a openscop_relation_list_t into
141 * a file (file, possibly stdout).
142 * \param file File where informations are printed.
143 * \param list The relation whose information have to be printed.
145 void
146 openscop_relation_list_print(FILE * file, openscop_relation_list_p list)
148 openscop_relation_list_print_structure(file, list, 0);
153 * openscop_relation_list_print_openscop function:
154 * This function prints the content of a openscop_relation_list_t structure
155 * into a file (file, possibly stdout) in the OpenScop format.
156 * \param file File where informations are printed.
157 * \param list The relation list whose information have to be printed.
158 * \param type Semantic about this relation (domain, access...).
159 * \param nb_iterators The number of iterators for the considered statement.
160 * \param iterators An array containing iterator names for the statement.
161 * \param nb_parameters The number of parameters in the SCoP.
162 * \param parameters An array containing all parameters names.
163 * \param nb_arrays The number of arrays accessed in the SCoP.
164 * \param arrays An array containing all accessed array names.
166 void
167 openscop_relation_list_print_openscop(FILE * file,
168 openscop_relation_list_p list,
169 int type,
170 int nb_iterators, char ** iterators,
171 int nb_parameters, char ** parameters,
172 int nb_arrays, char ** arrays)
174 int i;
175 openscop_relation_list_p head = list;
177 // Count the number of elements in the list.
178 for (i = 0; list; list = list->next, i++)
181 // Print it.
182 if (i > 1)
183 fprintf(file,"# List of %d elements\n%d\n", i, i);
184 else
185 fprintf(file,"# List of %d element \n%d\n", i, i);
187 // Print each element of the relation list.
188 i = 0;
189 while (head)
191 fprintf(file, "# List element No.%d\n", i);
192 openscop_relation_print_openscop(file, head->elt, type,
193 nb_iterators, iterators,
194 nb_parameters, parameters,
195 nb_arrays, arrays);
196 head = head->next;
197 i++;
202 /*****************************************************************************
203 * Reading function *
204 *****************************************************************************/
208 * openscop_relation_list_read function:
209 * This function reads a list of relations into a file (foo,
210 * posibly stdin) and returns a pointer this relation list.
211 * \param file The input stream.
212 * \return A pointer to the relation list structure that has been read.
214 openscop_relation_list_p
215 openscop_relation_list_read(FILE* file)
217 char s[OPENSCOP_MAX_STRING];
218 int i;
219 openscop_relation_list_p list;
220 openscop_relation_list_p res;
221 int nb_mat;
223 // Skip blank/commented lines.
224 while (fgets(s, OPENSCOP_MAX_STRING, file) == 0 || s[0] == '#' ||
225 isspace(s[0]))
228 // Read the number of relations to read.
229 sscanf(s, "%d", &nb_mat);
231 // Allocate the header of the list and start reading each element.
232 res = list = openscop_relation_list_malloc();
233 for (i = 0; i < nb_mat; ++i)
235 list->elt = openscop_relation_read(file);
236 if (i < nb_mat - 1)
237 list->next = openscop_relation_list_malloc();
238 list = list->next;
241 return res;
245 /*+***************************************************************************
246 * Memory allocation/deallocation function *
247 *****************************************************************************/
251 * openscop_relation_list_malloc function:
252 * This function allocates the memory space for a openscop_relation_list_t
253 * structure and sets its fields with default values. Then it returns
254 * a pointer to the allocated space.
255 * \return A pointer to an empty relation list with fields set to default
256 * values.
258 openscop_relation_list_p
259 openscop_relation_list_malloc()
261 openscop_relation_list_p res =
262 (openscop_relation_list_p) malloc(sizeof(openscop_relation_list_t));
264 if (res == NULL)
266 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
267 exit(1);
270 res->elt = NULL;
271 res->next = NULL;
273 return res;
279 * openscop_relation_list_free function:
280 * This function frees the allocated memory for a openscop_relation_list_t
281 * structure, and all the relations stored in the list.
282 * \param list The pointer to the relation list we want to free.
284 void
285 openscop_relation_list_free(openscop_relation_list_p list)
287 openscop_relation_list_p tmp;
289 if (list == NULL)
290 return;
292 while (list)
294 if (list->elt)
295 openscop_relation_free(list->elt);
296 tmp = list->next;
297 free(list);
298 list = tmp;
303 /*+***************************************************************************
304 * Processing functions *
305 *****************************************************************************/
309 * openscop_relation_list_node function:
310 * This function builds an openscop_relation_list_t node and sets its
311 * relation element as a copy of the one provided as parameter.
312 * \param relation The pointer to the relation to copy/paste in a list node.
313 * \return A pointer to a relation list node containing a copy of "relation".
315 openscop_relation_list_p
316 openscop_relation_list_node(openscop_relation_p relation)
318 openscop_relation_list_p new;
320 new = openscop_relation_list_malloc();
321 new->elt = openscop_relation_copy(relation);
323 return new;
328 * openscop_relation_list_copy function:
329 * This functions builds and returns a quasi-"hard copy" (not a pointer copy)
330 * of a openscop_relation_list_t data structure provided as parameter.
331 * \param list The pointer to the relation list we want to copy.
332 * \return A pointer to the full copy of the relation list in parameter.
334 openscop_relation_list_p
335 openscop_relation_list_copy(openscop_relation_list_p list)
337 int first = 1;
338 openscop_relation_list_p copy = NULL, node, previous = NULL;
340 while (list != NULL)
342 node = openscop_relation_list_malloc();
343 node->elt = openscop_relation_copy(list->elt);
345 if (first)
347 first = 0;
348 copy = node;
349 previous = node;
351 else
353 previous->next = node;
354 previous = previous->next;
357 list = list->next;
360 return copy;
365 * openscop_relation_list_concat function:
366 * this function builds a new relation list as the concatenation of the
367 * two lists sent as parameters.
368 * \param l1 The first relation list.
369 * \param l2 The second relation list.
370 * \return A pointer to the relation list resulting from the concatenation of
371 * l1 and l2.
373 openscop_relation_list_p
374 openscop_relation_list_concat(openscop_relation_list_p l1,
375 openscop_relation_list_p l2)
377 openscop_relation_list_p new, end;
379 if (l1 == NULL)
380 return openscop_relation_list_copy(l2);
382 if (l2 == NULL)
383 return openscop_relation_list_copy(l1);
385 new = openscop_relation_list_copy(l1);
386 end = new;
387 while (end->next != NULL)
388 end = end->next;
389 end->next = openscop_relation_list_copy(l2);
391 return new;
396 * openscop_relation_list_equal function:
397 * This function returns true if the two relation lists are the same, false
398 * otherwise..
399 * \param l1 The first relation list.
400 * \param l2 The second relation list.
401 * \return 1 if l1 and l2 are the same (content-wise), 0 otherwise.
404 openscop_relation_list_equal(openscop_relation_list_p l1,
405 openscop_relation_list_p l2)
407 while ((l1 != NULL) && (l2 != NULL))
409 if (!openscop_relation_equal(l1->elt, l2->elt))
410 return 0;
412 l1 = l1->next;
413 l2 = l2->next;
416 if (((l1 == NULL) && (l2 != NULL)) || ((l1 != NULL) && (l2 == NULL)))
417 return 0;
419 return 1;
424 * openscop_relation_integrity_check function:
425 * This function checks that a list of relation is "well formed" according to
426 * some expected properties (setting an expected value to OPENSCOP_UNDEFINED
427 * means that we do not expect a specific value) and what the relations are
428 * supposed to represent (all relations of a list are supposed to have the
429 * same semantics). It returns 0 if the check failed or 1 if no problem has
430 * been detected.
431 * \param list The relation list we want to check.
432 * \param type Semantics about this relation (domain, access...).
433 * \param expected_nb_output_dims Expected number of output dimensions.
434 * \param expected_nb_input_dims Expected number of input dimensions.
435 * \param expected_nb_parameters Expected number of parameters.
436 * \return 0 if the integrity check fails, 1 otherwise.
439 openscop_relation_list_integrity_check(openscop_relation_list_p list,
440 int type,
441 int expected_nb_output_dims,
442 int expected_nb_input_dims,
443 int expected_nb_parameters)
445 while (list != NULL)
447 if (!openscop_relation_integrity_check(list->elt,
448 type,
449 expected_nb_output_dims,
450 expected_nb_input_dims,
451 expected_nb_parameters))
452 return 0;
454 list = list->next;
457 return 1;