OpenScop
0.9.0
|
00001 00002 /*+-----------------------------------------------------------------** 00003 ** OpenScop Library ** 00004 **-----------------------------------------------------------------** 00005 ** extensions/arrays.c ** 00006 **-----------------------------------------------------------------** 00007 ** First version: 07/12/2010 ** 00008 **-----------------------------------------------------------------** 00009 00010 00011 ***************************************************************************** 00012 * OpenScop: Structures and formats for polyhedral tools to talk together * 00013 ***************************************************************************** 00014 * ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__, * 00015 * / / / // // // // / / / // // / / // / /|,_, * 00016 * / / / // // // // / / / // // / / // / / / /\ * 00017 * |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/ \ * 00018 * | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\ \ /\ * 00019 * | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\ * 00020 * | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \ \ * 00021 * | P |n| l | = | s | t |=| = |d| = | = | = | | |=| o | | \# \ \ * 00022 * | H | | y | | e | o | | = |l| | | = | | | | G | | \ \ \ * 00023 * | I | | | | e | | | | | | | | | | | | | \ \ \ * 00024 * | T | | | | | | | | | | | | | | | | | \ \ \ * 00025 * | E | | | | | | | | | | | | | | | | | \ \ \ * 00026 * | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | / \* \ \ * 00027 * | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/ \ \ / * 00028 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' * 00029 * * 00030 * Copyright (C) 2008 University Paris-Sud 11 and INRIA * 00031 * * 00032 * (3-clause BSD license) * 00033 * Redistribution and use in source and binary forms, with or without * 00034 * modification, are permitted provided that the following conditions * 00035 * are met: * 00036 * * 00037 * 1. Redistributions of source code must retain the above copyright notice, * 00038 * this list of conditions and the following disclaimer. * 00039 * 2. Redistributions in binary form must reproduce the above copyright * 00040 * notice, this list of conditions and the following disclaimer in the * 00041 * documentation and/or other materials provided with the distribution. * 00042 * 3. The name of the author may not be used to endorse or promote products * 00043 * derived from this software without specific prior written permission. * 00044 * * 00045 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * 00046 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * 00047 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * 00048 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * 00049 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * 00050 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * 00051 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * 00052 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * 00053 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * 00054 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * 00055 * * 00056 * OpenScop Library, a library to manipulate OpenScop formats and data * 00057 * structures. Written by: * 00058 * Cedric Bastoul <Cedric.Bastoul@u-psud.fr> and * 00059 * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr> * 00060 * * 00061 *****************************************************************************/ 00062 00063 #include <stdlib.h> 00064 #include <stdio.h> 00065 #include <string.h> 00066 #include <ctype.h> 00067 00068 #include <osl/macros.h> 00069 #include <osl/util.h> 00070 #include <osl/strings.h> 00071 #include <osl/interface.h> 00072 #include <osl/extensions/arrays.h> 00073 00074 00075 /*+*************************************************************************** 00076 * Structure display function * 00077 *****************************************************************************/ 00078 00079 00090 void osl_arrays_idump(FILE * file, osl_arrays_p arrays, int level) { 00091 int i, j; 00092 00093 // Go to the right level. 00094 for (j = 0; j < level; j++) 00095 fprintf(file, "|\t"); 00096 00097 if (arrays != NULL) 00098 fprintf(file, "+-- osl_arrays_t\n"); 00099 else 00100 fprintf(file, "+-- NULL arrays\n"); 00101 00102 if (arrays != NULL) { 00103 // Go to the right level. 00104 for(j = 0; j <= level; j++) 00105 fprintf(file, "|\t"); 00106 00107 // Display the number of names. 00108 fprintf(file, "nb_names: %d\n", arrays->nb_names); 00109 00110 // Display the id/name. 00111 for(i = 0; i < arrays->nb_names; i++) { 00112 // Go to the right level. 00113 for(j = 0; j <= level; j++) 00114 fprintf(file, "|\t"); 00115 00116 fprintf(file, "id: %2d, name: %s\n", arrays->id[i], arrays->names[i]); 00117 } 00118 } 00119 00120 // The last line. 00121 for (j = 0; j <= level; j++) 00122 fprintf(file, "|\t"); 00123 fprintf(file, "\n"); 00124 } 00125 00126 00134 void osl_arrays_dump(FILE * file, osl_arrays_p arrays) { 00135 osl_arrays_idump(file, arrays, 0); 00136 } 00137 00138 00146 char * osl_arrays_sprint(osl_arrays_p arrays) { 00147 int i; 00148 int high_water_mark = OSL_MAX_STRING; 00149 char * string = NULL; 00150 char buffer[OSL_MAX_STRING]; 00151 00152 if (arrays != NULL) { 00153 OSL_malloc(string, char *, high_water_mark * sizeof(char)); 00154 string[0] = '\0'; 00155 00156 sprintf(buffer, "# Number of arrays\n"); 00157 osl_util_safe_strcat(&string, buffer, &high_water_mark); 00158 00159 sprintf(buffer, "%d\n", arrays->nb_names); 00160 osl_util_safe_strcat(&string, buffer, &high_water_mark); 00161 00162 if (arrays->nb_names) { 00163 sprintf(buffer, "# Mapping array-identifiers/array-names\n"); 00164 osl_util_safe_strcat(&string, buffer, &high_water_mark); 00165 } 00166 for (i = 0; i < arrays->nb_names; i++) { 00167 sprintf(buffer, "%d %s\n", arrays->id[i], arrays->names[i]); 00168 osl_util_safe_strcat(&string, buffer, &high_water_mark); 00169 } 00170 00171 OSL_realloc(string, char *, (strlen(string) + 1) * sizeof(char)); 00172 } 00173 00174 return string; 00175 } 00176 00177 00178 /***************************************************************************** 00179 * Reading function * 00180 *****************************************************************************/ 00181 00182 00194 osl_arrays_p osl_arrays_sread(char ** input) { 00195 int i, k; 00196 int nb_names; 00197 osl_arrays_p arrays; 00198 00199 if (input == NULL) { 00200 OSL_debug("no arrays optional tag"); 00201 return NULL; 00202 } 00203 00204 // Find the number of names provided. 00205 nb_names = osl_util_read_int(NULL, input); 00206 00207 // Allocate the array of id and names. 00208 arrays = osl_arrays_malloc(); 00209 OSL_malloc(arrays->id, int *, nb_names * sizeof(int)); 00210 OSL_malloc(arrays->names, char **, nb_names * sizeof(char *)); 00211 arrays->nb_names = nb_names; 00212 for (i = 0; i < nb_names; i++) 00213 arrays->names[i] = NULL; 00214 00215 // Get each array id/name. 00216 for (k = 0; k < nb_names; k++) { 00217 // Get the array name id. 00218 arrays->id[k] = osl_util_read_int(NULL, input); 00219 00220 // Get the array name string. 00221 arrays->names[k] = osl_util_read_string(NULL, input); 00222 } 00223 00224 return arrays; 00225 } 00226 00227 00228 /*+*************************************************************************** 00229 * Memory allocation/deallocation function * 00230 *****************************************************************************/ 00231 00232 00241 osl_arrays_p osl_arrays_malloc() { 00242 osl_arrays_p arrays; 00243 00244 OSL_malloc(arrays, osl_arrays_p, sizeof(osl_arrays_t)); 00245 arrays->nb_names = 0; 00246 arrays->id = NULL; 00247 arrays->names = NULL; 00248 00249 return arrays; 00250 } 00251 00252 00258 void osl_arrays_free(osl_arrays_p arrays) { 00259 int i; 00260 00261 if (arrays != NULL) { 00262 free(arrays->id); 00263 for (i = 0; i < arrays->nb_names; i++) 00264 free(arrays->names[i]); 00265 free(arrays->names); 00266 free(arrays); 00267 } 00268 } 00269 00270 00271 /*+*************************************************************************** 00272 * Processing functions * 00273 *****************************************************************************/ 00274 00275 00283 osl_arrays_p osl_arrays_clone(osl_arrays_p arrays) { 00284 int i; 00285 osl_arrays_p clone; 00286 00287 if (arrays == NULL) 00288 return NULL; 00289 00290 clone = osl_arrays_malloc(); 00291 clone->nb_names = arrays->nb_names; 00292 OSL_malloc(clone->id, int *, arrays->nb_names * sizeof(int)); 00293 OSL_malloc(clone->names, char **, arrays->nb_names * sizeof(char*)); 00294 00295 for (i = 0; i < arrays->nb_names; i++) { 00296 clone->id[i] = arrays->id[i]; 00297 OSL_strdup(clone->names[i], arrays->names[i]); 00298 } 00299 00300 return clone; 00301 } 00302 00303 00314 int osl_arrays_equal(osl_arrays_p a1, osl_arrays_p a2) { 00315 int i, j, found; 00316 00317 if (a1 == a2) 00318 return 1; 00319 00320 if (((a1 == NULL) && (a2 != NULL)) || ((a1 != NULL) && (a2 == NULL))) { 00321 OSL_info("arrays are not the same"); 00322 return 0; 00323 } 00324 00325 // Check whether the number of names is the same. 00326 if (a1->nb_names != a2->nb_names) { 00327 OSL_info("arrays are not the same"); 00328 return 0; 00329 } 00330 00331 // We accept a different order of the names, as long as the identifiers 00332 // are the same. 00333 for (i = 0; i < a1->nb_names; i++) { 00334 found = 0; 00335 for (j = 0; j < a2->nb_names; j++) { 00336 if ((a1->id[i] == a2->id[j]) && (!strcmp(a1->names[i], a2->names[j]))) { 00337 found = 1; 00338 break; 00339 } 00340 } 00341 if (found != 1) { 00342 OSL_info("arrays are not the same"); 00343 return 0; 00344 } 00345 } 00346 00347 return 1; 00348 } 00349 00350 00360 osl_strings_p osl_arrays_to_strings(osl_arrays_p arrays) { 00361 int i, max_id = 0; 00362 osl_strings_p strings = NULL; 00363 00364 if (arrays == NULL) 00365 return NULL; 00366 00367 // Find the maximum array id. 00368 if (arrays->nb_names >= 1) { 00369 max_id = arrays->id[0]; 00370 for (i = 1; i < arrays->nb_names; i++) 00371 if (arrays->id[i] > max_id) 00372 max_id = arrays->id[i]; 00373 } 00374 00375 // Build a strings structure for this number of ids. 00376 strings = osl_strings_generate("Dummy", max_id); 00377 for (i = 0; i < arrays->nb_names; i++) { 00378 free(strings->string[arrays->id[i] - 1]); 00379 OSL_strdup(strings->string[arrays->id[i] - 1], arrays->names[i]); 00380 } 00381 00382 return strings; 00383 } 00384 00385 00392 osl_interface_p osl_arrays_interface() { 00393 osl_interface_p interface = osl_interface_malloc(); 00394 00395 OSL_strdup(interface->URI, OSL_URI_ARRAYS); 00396 interface->idump = (osl_idump_f)osl_arrays_idump; 00397 interface->sprint = (osl_sprint_f)osl_arrays_sprint; 00398 interface->sread = (osl_sread_f)osl_arrays_sread; 00399 interface->malloc = (osl_malloc_f)osl_arrays_malloc; 00400 interface->free = (osl_free_f)osl_arrays_free; 00401 interface->clone = (osl_clone_f)osl_arrays_clone; 00402 interface->equal = (osl_equal_f)osl_arrays_equal; 00403 00404 return interface; 00405 } 00406 00407