OpenScop  0.9.0
arrays.c
Go to the documentation of this file.
1 
2  /*+-----------------------------------------------------------------**
3  ** OpenScop Library **
4  **-----------------------------------------------------------------**
5  ** extensions/arrays.c **
6  **-----------------------------------------------------------------**
7  ** First version: 07/12/2010 **
8  **-----------------------------------------------------------------**
9 
10 
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  *****************************************************************************/
62 
63 #include <stdlib.h>
64 #include <stdio.h>
65 #include <string.h>
66 #include <ctype.h>
67 
68 #include <osl/macros.h>
69 #include <osl/util.h>
70 #include <osl/strings.h>
71 #include <osl/interface.h>
72 #include <osl/extensions/arrays.h>
73 
74 
75 /*+***************************************************************************
76  * Structure display function *
77  *****************************************************************************/
78 
79 
90 void osl_arrays_idump(FILE * file, osl_arrays_p arrays, int level) {
91  int i, j;
92 
93  // Go to the right level.
94  for (j = 0; j < level; j++)
95  fprintf(file, "|\t");
96 
97  if (arrays != NULL)
98  fprintf(file, "+-- osl_arrays_t\n");
99  else
100  fprintf(file, "+-- NULL arrays\n");
101 
102  if (arrays != NULL) {
103  // Go to the right level.
104  for(j = 0; j <= level; j++)
105  fprintf(file, "|\t");
106 
107  // Display the number of names.
108  fprintf(file, "nb_names: %d\n", arrays->nb_names);
109 
110  // Display the id/name.
111  for(i = 0; i < arrays->nb_names; i++) {
112  // Go to the right level.
113  for(j = 0; j <= level; j++)
114  fprintf(file, "|\t");
115 
116  fprintf(file, "id: %2d, name: %s\n", arrays->id[i], arrays->names[i]);
117  }
118  }
119 
120  // The last line.
121  for (j = 0; j <= level; j++)
122  fprintf(file, "|\t");
123  fprintf(file, "\n");
124 }
125 
126 
134 void osl_arrays_dump(FILE * file, osl_arrays_p arrays) {
135  osl_arrays_idump(file, arrays, 0);
136 }
137 
138 
147  int i;
148  int high_water_mark = OSL_MAX_STRING;
149  char * string = NULL;
150  char buffer[OSL_MAX_STRING];
151 
152  if (arrays != NULL) {
153  OSL_malloc(string, char *, high_water_mark * sizeof(char));
154  string[0] = '\0';
155 
156  sprintf(buffer, "# Number of arrays\n");
157  osl_util_safe_strcat(&string, buffer, &high_water_mark);
158 
159  sprintf(buffer, "%d\n", arrays->nb_names);
160  osl_util_safe_strcat(&string, buffer, &high_water_mark);
161 
162  if (arrays->nb_names) {
163  sprintf(buffer, "# Mapping array-identifiers/array-names\n");
164  osl_util_safe_strcat(&string, buffer, &high_water_mark);
165  }
166  for (i = 0; i < arrays->nb_names; i++) {
167  sprintf(buffer, "%d %s\n", arrays->id[i], arrays->names[i]);
168  osl_util_safe_strcat(&string, buffer, &high_water_mark);
169  }
170 
171  OSL_realloc(string, char *, (strlen(string) + 1) * sizeof(char));
172  }
173 
174  return string;
175 }
176 
177 
178 /*****************************************************************************
179  * Reading function *
180  *****************************************************************************/
181 
182 
195  int i, k;
196  int nb_names;
197  osl_arrays_p arrays;
198 
199  if (input == NULL) {
200  OSL_debug("no arrays optional tag");
201  return NULL;
202  }
203 
204  // Find the number of names provided.
205  nb_names = osl_util_read_int(NULL, input);
206 
207  // Allocate the array of id and names.
208  arrays = osl_arrays_malloc();
209  OSL_malloc(arrays->id, int *, nb_names * sizeof(int));
210  OSL_malloc(arrays->names, char **, nb_names * sizeof(char *));
211  arrays->nb_names = nb_names;
212  for (i = 0; i < nb_names; i++)
213  arrays->names[i] = NULL;
214 
215  // Get each array id/name.
216  for (k = 0; k < nb_names; k++) {
217  // Get the array name id.
218  arrays->id[k] = osl_util_read_int(NULL, input);
219 
220  // Get the array name string.
221  arrays->names[k] = osl_util_read_string(NULL, input);
222  }
223 
224  return arrays;
225 }
226 
227 
228 /*+***************************************************************************
229  * Memory allocation/deallocation function *
230  *****************************************************************************/
231 
232 
242  osl_arrays_p arrays;
243 
244  OSL_malloc(arrays, osl_arrays_p, sizeof(osl_arrays_t));
245  arrays->nb_names = 0;
246  arrays->id = NULL;
247  arrays->names = NULL;
248 
249  return arrays;
250 }
251 
252 
259  int i;
260 
261  if (arrays != NULL) {
262  free(arrays->id);
263  for (i = 0; i < arrays->nb_names; i++)
264  free(arrays->names[i]);
265  free(arrays->names);
266  free(arrays);
267  }
268 }
269 
270 
271 /*+***************************************************************************
272  * Processing functions *
273  *****************************************************************************/
274 
275 
284  int i;
285  osl_arrays_p clone;
286 
287  if (arrays == NULL)
288  return NULL;
289 
290  clone = osl_arrays_malloc();
291  clone->nb_names = arrays->nb_names;
292  OSL_malloc(clone->id, int *, arrays->nb_names * sizeof(int));
293  OSL_malloc(clone->names, char **, arrays->nb_names * sizeof(char*));
294 
295  for (i = 0; i < arrays->nb_names; i++) {
296  clone->id[i] = arrays->id[i];
297  OSL_strdup(clone->names[i], arrays->names[i]);
298  }
299 
300  return clone;
301 }
302 
303 
315  int i, j, found;
316 
317  if (a1 == a2)
318  return 1;
319 
320  if (((a1 == NULL) && (a2 != NULL)) || ((a1 != NULL) && (a2 == NULL))) {
321  OSL_info("arrays are not the same");
322  return 0;
323  }
324 
325  // Check whether the number of names is the same.
326  if (a1->nb_names != a2->nb_names) {
327  OSL_info("arrays are not the same");
328  return 0;
329  }
330 
331  // We accept a different order of the names, as long as the identifiers
332  // are the same.
333  for (i = 0; i < a1->nb_names; i++) {
334  found = 0;
335  for (j = 0; j < a2->nb_names; j++) {
336  if ((a1->id[i] == a2->id[j]) && (!strcmp(a1->names[i], a2->names[j]))) {
337  found = 1;
338  break;
339  }
340  }
341  if (found != 1) {
342  OSL_info("arrays are not the same");
343  return 0;
344  }
345  }
346 
347  return 1;
348 }
349 
350 
361  int i, max_id = 0;
362  osl_strings_p strings = NULL;
363 
364  if (arrays == NULL)
365  return NULL;
366 
367  // Find the maximum array id.
368  if (arrays->nb_names >= 1) {
369  max_id = arrays->id[0];
370  for (i = 1; i < arrays->nb_names; i++)
371  if (arrays->id[i] > max_id)
372  max_id = arrays->id[i];
373  }
374 
375  // Build a strings structure for this number of ids.
376  strings = osl_strings_generate("Dummy", max_id);
377  for (i = 0; i < arrays->nb_names; i++) {
378  free(strings->string[arrays->id[i] - 1]);
379  OSL_strdup(strings->string[arrays->id[i] - 1], arrays->names[i]);
380  }
381 
382  return strings;
383 }
384 
394 int osl_arrays_add(osl_arrays_p arrays, int id, char* name) {
395 
396  if (arrays == NULL || name == NULL)
397  return -1;
398 
399  OSL_realloc(arrays->id, int *, (arrays->nb_names+1) * sizeof(int));
400  OSL_realloc(arrays->names, char **, (arrays->nb_names+1) * sizeof(char *));
401  arrays->id[arrays->nb_names] = id;
402  OSL_strdup(arrays->names[arrays->nb_names], name);
403  arrays->nb_names++;
404 
405  return arrays->nb_names;
406 }
407 
408 
418  size_t i = 0;
419 
420  if (arrays == NULL)
421  return 0;
422 
423  for (i=0; i<arrays->nb_names; i++) {
424  if(arrays->id[i]==id)
425  break;
426  }
427 
428  return i<arrays->nb_names? i: arrays->nb_names;
429 }
430 
439 size_t osl_arrays_get_index_from_name(osl_arrays_p arrays, char* name) {
440  size_t i = 0;
441 
442  if (arrays == NULL || name == NULL)
443  return 0;
444 
445  for (i=0; i<arrays->nb_names; i++) {
446  if(!strcmp(arrays->names[i], name))
447  break;
448  }
449 
450  return i<arrays->nb_names? i: arrays->nb_names;;
451 }
452 
461 
462  OSL_strdup(interface->URI, OSL_URI_ARRAYS);
463  interface->idump = (osl_idump_f)osl_arrays_idump;
464  interface->sprint = (osl_sprint_f)osl_arrays_sprint;
465  interface->sread = (osl_sread_f)osl_arrays_sread;
466  interface->malloc = (osl_malloc_f)osl_arrays_malloc;
467  interface->free = (osl_free_f)osl_arrays_free;
468  interface->clone = (osl_clone_f)osl_arrays_clone;
469  interface->equal = (osl_equal_f)osl_arrays_equal;
470 
471  return interface;
472 }
473 
474 
void osl_arrays_free(osl_arrays_p arrays)
Definition: arrays.c:258
osl_strings_p osl_arrays_to_strings(osl_arrays_p arrays)
Definition: arrays.c:360
char ** names
Definition: arrays.h:89
void *(* osl_clone_f)(void *)
Definition: interface.h:80
void osl_arrays_idump(FILE *file, osl_arrays_p arrays, int level)
Definition: arrays.c:90
int * id
Definition: arrays.h:88
void osl_arrays_dump(FILE *file, osl_arrays_p arrays)
Definition: arrays.c:134
char * osl_arrays_sprint(osl_arrays_p arrays)
Definition: arrays.c:146
osl_arrays_p osl_arrays_sread(char **input)
Definition: arrays.c:194
char ** string
Definition: strings.h:82
osl_interface_p osl_interface_malloc()
Definition: interface.c:212
int osl_arrays_equal(osl_arrays_p a1, osl_arrays_p a2)
Definition: arrays.c:314
void *(* osl_sread_f)(char **)
Definition: interface.h:77
osl_interface_p osl_arrays_interface()
Definition: arrays.c:459
size_t osl_arrays_get_index_from_name(osl_arrays_p arrays, char *name)
Definition: arrays.c:439
void osl_util_safe_strcat(char **dst, char *src, int *hwm)
Definition: util.c:483
size_t osl_arrays_get_index_from_id(osl_arrays_p arrays, int id)
Definition: arrays.c:417
void(* osl_idump_f)(FILE *, void *, int)
Definition: interface.h:75
osl_arrays_p osl_arrays_malloc()
Definition: arrays.c:241
osl_arrays_p osl_arrays_clone(osl_arrays_p arrays)
Definition: arrays.c:283
int osl_util_read_int(FILE *file, char **str)
Definition: util.c:140
char *(* osl_sprint_f)(void *)
Definition: interface.h:76
char * osl_util_read_string(FILE *file, char **str)
Definition: util.c:181
void *(* osl_malloc_f)()
Definition: interface.h:78
int(* osl_equal_f)(void *, void *)
Definition: interface.h:81
osl_strings_p osl_strings_generate(char *prefix, int nb_strings)
Definition: strings.c:475
int osl_arrays_add(osl_arrays_p arrays, int id, char *name)
Definition: arrays.c:394
int nb_names
Definition: arrays.h:87
void(* osl_free_f)(void *)
Definition: interface.h:79