00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 #include <stdlib.h>
00065 #include <stdio.h>
00066 #include <string.h>
00067 #include <ctype.h>
00068
00069 #include <osl/macros.h>
00070 #include <osl/util.h>
00071 #include <osl/relation.h>
00072 #include <osl/relation_list.h>
00073
00074
00075
00076
00077
00078
00079
00089 void osl_relation_list_idump(FILE * file, osl_relation_list_p l, int level) {
00090 int j, first = 1;
00091
00092
00093 for (j = 0; j < level; j++)
00094 fprintf(file,"|\t");
00095
00096 if (l != NULL)
00097 fprintf(file, "+-- osl_relation_list_t\n");
00098 else
00099 fprintf(file, "+-- NULL relation list\n");
00100
00101 while (l != NULL) {
00102 if (!first) {
00103
00104 for (j = 0; j < level; j++)
00105 fprintf(file, "|\t");
00106 fprintf(file, "| osl_relation_list_t\n");
00107 }
00108 else
00109 first = 0;
00110
00111
00112 for (j = 0; j <= level+1; j++)
00113 fprintf(file, "|\t");
00114 fprintf(file, "\n");
00115
00116
00117 osl_relation_idump(file, l->elt, level+1);
00118
00119 l = l->next;
00120
00121
00122 if (l != NULL) {
00123 for (j = 0; j <= level; j++)
00124 fprintf(file, "|\t");
00125 fprintf(file, "V\n");
00126 }
00127 }
00128
00129
00130 for (j = 0; j <= level; j++)
00131 fprintf(file, "|\t");
00132 fprintf(file, "\n");
00133 }
00134
00135
00143 void osl_relation_list_dump(FILE * file, osl_relation_list_p list) {
00144 osl_relation_list_idump(file, list, 0);
00145 }
00146
00147
00158 void osl_relation_list_pprint_elts(FILE * file, osl_relation_list_p list,
00159 osl_names_p names) {
00160 int i;
00161 osl_relation_list_p head = list;
00162
00163
00164 i = osl_relation_list_count(list);
00165
00166
00167 if (i > 0) {
00168 i = 0;
00169 while (head) {
00170 if (head->elt != NULL) {
00171 osl_relation_pprint(file, head->elt, names);
00172 if (head->next != NULL)
00173 fprintf(file, "\n");
00174 i++;
00175 }
00176 head = head->next;
00177 }
00178 }
00179 else {
00180 fprintf(file, "# NULL relation list\n");
00181 }
00182 }
00183
00184
00194 void osl_relation_list_pprint(FILE * file, osl_relation_list_p list,
00195 osl_names_p names) {
00196 int i;
00197
00198
00199 i = osl_relation_list_count(list);
00200
00201
00202 if (i > 1)
00203 fprintf(file,"# List of %d elements\n%d\n", i, i);
00204 else
00205 fprintf(file,"# List of %d element \n%d\n", i, i);
00206
00207
00208 osl_relation_list_pprint_elts(file, list, names);
00209 }
00210
00211
00220 void osl_relation_list_print(FILE * file, osl_relation_list_p list) {
00221
00222 osl_relation_list_pprint(file, list, NULL);
00223 }
00224
00225
00226
00227
00228
00229
00238 osl_relation_list_p osl_relation_list_pread(FILE * file, int precision) {
00239 int i;
00240 osl_relation_list_p list;
00241 osl_relation_list_p res;
00242 int nb_mat;
00243
00244
00245 nb_mat = osl_util_read_int(file, NULL);
00246
00247 if (nb_mat < 0)
00248 OSL_error("negative number of relations");
00249
00250
00251 res = list = osl_relation_list_malloc();
00252 for (i = 0; i < nb_mat; ++i) {
00253 list->elt = osl_relation_pread(file, precision);
00254 if (i < nb_mat - 1)
00255 list->next = osl_relation_list_malloc();
00256 list = list->next;
00257 }
00258
00259 return res;
00260 }
00261
00262
00270 osl_relation_list_p osl_relation_list_read(FILE * foo) {
00271 int precision = osl_util_get_precision();
00272 return osl_relation_list_pread(foo, precision);
00273 }
00274
00275
00276
00277
00278
00279
00280
00289 osl_relation_list_p osl_relation_list_malloc() {
00290 osl_relation_list_p res;
00291
00292 OSL_malloc(res, osl_relation_list_p, sizeof(osl_relation_list_t));
00293 res->elt = NULL;
00294 res->next = NULL;
00295
00296 return res;
00297 }
00298
00299
00300
00307 void osl_relation_list_free(osl_relation_list_p list) {
00308 osl_relation_list_p tmp;
00309
00310 if (list == NULL)
00311 return;
00312
00313 while (list != NULL) {
00314 if (list->elt != NULL)
00315 osl_relation_free(list->elt);
00316 tmp = list->next;
00317 free(list);
00318 list = tmp;
00319 }
00320 }
00321
00322
00323
00324
00325
00326
00327
00336 osl_relation_list_p osl_relation_list_node(osl_relation_p r) {
00337 osl_relation_list_p new = NULL;
00338
00339 if (r != NULL) {
00340 new = osl_relation_list_malloc();
00341 new->elt = osl_relation_clone(r);
00342 }
00343 return new;
00344 }
00345
00346
00354 osl_relation_list_p osl_relation_list_clone(osl_relation_list_p list) {
00355
00356 osl_relation_list_p clone = NULL, node, previous = NULL;
00357 int first = 1;
00358
00359 while (list != NULL) {
00360 node = osl_relation_list_malloc();
00361 node->elt = osl_relation_clone(list->elt);
00362
00363 if (first) {
00364 first = 0;
00365 clone = node;
00366 previous = node;
00367 }
00368 else {
00369 previous->next = node;
00370 previous = previous->next;
00371 }
00372
00373 list = list->next;
00374 }
00375
00376 return clone;
00377 }
00378
00379
00389 osl_relation_list_p osl_relation_list_concat(osl_relation_list_p l1,
00390 osl_relation_list_p l2) {
00391 osl_relation_list_p new, end;
00392
00393 if (l1 == NULL)
00394 return osl_relation_list_clone(l2);
00395
00396 if (l2 == NULL)
00397 return osl_relation_list_clone(l1);
00398
00399 new = osl_relation_list_clone(l1);
00400 end = new;
00401 while (end->next != NULL)
00402 end = end->next;
00403 end->next = osl_relation_list_clone(l2);
00404
00405 return new;
00406 }
00407
00408
00418 void osl_relation_list_add(osl_relation_list_p *l1, osl_relation_list_p l2) {
00419 while (*l1 != NULL)
00420 l1 = &((*l1)->next);
00421
00422 *l1 = l2;
00423 }
00424
00425
00434 void osl_relation_list_push(osl_relation_list_p *head,
00435 osl_relation_list_p node) {
00436 if (node != NULL) {
00437 node->next = *head;
00438 *head = node;
00439 }
00440 }
00441
00442
00452 osl_relation_list_p osl_relation_list_pop(osl_relation_list_p *head) {
00453 osl_relation_list_p top = NULL;
00454
00455 if (*head != NULL) {
00456 top = *head;
00457 *head = (*head)->next;
00458 top->next = NULL;
00459 }
00460
00461 return top;
00462 }
00463
00464
00473 void osl_relation_list_dup(osl_relation_list_p *head) {
00474 osl_relation_list_p top = osl_relation_list_pop(head);
00475 osl_relation_list_push(head, osl_relation_list_clone(top));
00476 osl_relation_list_push(head, top);
00477 }
00478
00479
00489 void osl_relation_list_drop(osl_relation_list_p *head) {
00490 osl_relation_list_p top = osl_relation_list_pop(head);
00491 osl_relation_list_free(top);
00492 }
00493
00494
00503 void osl_relation_list_destroy(osl_relation_list_p *head) {
00504
00505 while (*head != NULL)
00506 osl_relation_list_drop(head);
00507 }
00508
00509
00518 int osl_relation_list_equal(osl_relation_list_p l1, osl_relation_list_p l2) {
00519 while ((l1 != NULL) && (l2 != NULL)) {
00520 if (l1 == l2)
00521 return 1;
00522
00523 if (!osl_relation_equal(l1->elt, l2->elt))
00524 return 0;
00525
00526 l1 = l1->next;
00527 l2 = l2->next;
00528 }
00529
00530 if (((l1 == NULL) && (l2 != NULL)) || ((l1 != NULL) && (l2 == NULL)))
00531 return 0;
00532
00533 return 1;
00534 }
00535
00536
00552 int osl_relation_list_integrity_check(osl_relation_list_p list,
00553 int type,
00554 int expected_nb_output_dims,
00555 int expected_nb_input_dims,
00556 int expected_nb_parameters) {
00557 while (list != NULL) {
00558
00559 if (!osl_relation_integrity_check(list->elt,
00560 type,
00561 expected_nb_output_dims,
00562 expected_nb_input_dims,
00563 expected_nb_parameters)) {
00564 return 0;
00565 }
00566
00567 list = list->next;
00568 }
00569
00570 return 1;
00571 }
00572
00573
00581 void osl_relation_list_set_type(osl_relation_list_p list, int type) {
00582
00583 while (list != NULL) {
00584 if (list->elt != NULL) {
00585 list->elt->type = type;
00586 }
00587 list = list->next;
00588 }
00589 }
00590
00591
00601 osl_relation_list_p osl_relation_list_filter(osl_relation_list_p list,
00602 int type) {
00603
00604 osl_relation_list_p copy = osl_relation_list_clone(list);
00605 osl_relation_list_p filtered = NULL;
00606 osl_relation_list_p previous = NULL;
00607 osl_relation_list_p trash;
00608 int first = 1;
00609
00610 while (copy != NULL) {
00611 if ((copy->elt != NULL) &&
00612 (((type == OSL_TYPE_ACCESS) &&
00613 (osl_relation_is_access(copy->elt))) ||
00614 ((type != OSL_TYPE_ACCESS) &&
00615 (type == copy->elt->type)))) {
00616 if (first) {
00617 filtered = copy;
00618 first = 0;
00619 }
00620
00621 previous = copy;
00622 copy = copy->next;
00623 }
00624 else {
00625 trash = copy;
00626 if (!first)
00627 previous->next = copy->next;
00628 copy = copy->next;
00629 trash->next = NULL;
00630 osl_relation_list_free(trash);
00631 }
00632 }
00633
00634 return filtered;
00635 }
00636
00637
00645 int osl_relation_list_count(osl_relation_list_p list) {
00646 int i = 0;
00647
00648 while (list != NULL) {
00649 if (list->elt != NULL)
00650 i++;
00651 list = list->next;
00652 }
00653
00654 return i;
00655 }
00656
00657
00676 void osl_relation_list_get_attributes(osl_relation_list_p list,
00677 int * nb_parameters,
00678 int * nb_iterators,
00679 int * nb_scattdims,
00680 int * nb_localdims,
00681 int * array_id) {
00682 int local_nb_parameters = OSL_UNDEFINED;
00683 int local_nb_iterators = OSL_UNDEFINED;
00684 int local_nb_scattdims = OSL_UNDEFINED;
00685 int local_nb_localdims = OSL_UNDEFINED;
00686 int local_array_id = OSL_UNDEFINED;
00687
00688 while (list != NULL) {
00689 osl_relation_get_attributes(list->elt,
00690 &local_nb_parameters,
00691 &local_nb_iterators,
00692 &local_nb_scattdims,
00693 &local_nb_localdims,
00694 &local_array_id);
00695
00696 *nb_parameters = OSL_max(*nb_parameters, local_nb_parameters);
00697 *nb_iterators = OSL_max(*nb_iterators, local_nb_iterators);
00698 *nb_scattdims = OSL_max(*nb_scattdims, local_nb_scattdims);
00699 *nb_localdims = OSL_max(*nb_localdims, local_nb_localdims);
00700 *array_id = OSL_max(*array_id, local_array_id);
00701 list = list->next;
00702 }
00703 }
00704