File:  [local] / rpl / src / simplification.c
Revision 1.25: download - view: text, annotated - select for diffs - revision graph
Mon Jun 27 09:05:02 2011 UTC (12 years, 10 months ago) by bertrand
Branches: MAIN
CVS tags: rpl-4_1_0, HEAD
Passage de la branche 4.1 en branche stable.

    1: /*
    2: ================================================================================
    3:   RPL/2 (R) version 4.1.0
    4:   Copyright (C) 1989-2011 Dr. BERTRAND Joël
    5: 
    6:   This file is part of RPL/2.
    7: 
    8:   RPL/2 is free software; you can redistribute it and/or modify it
    9:   under the terms of the CeCILL V2 License as published by the french
   10:   CEA, CNRS and INRIA.
   11:  
   12:   RPL/2 is distributed in the hope that it will be useful, but WITHOUT
   13:   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   14:   FITNESS FOR A PARTICULAR PURPOSE.  See the CeCILL V2 License
   15:   for more details.
   16:  
   17:   You should have received a copy of the CeCILL License
   18:   along with RPL/2. If not, write to info@cecill.info.
   19: ================================================================================
   20: */
   21: 
   22: 
   23: #include "rpl-conv.h"
   24: 
   25: 
   26: /*
   27: ================================================================================
   28:   Fonction 'simplification' (ne libère pas les paramètres)
   29: ================================================================================
   30:   Entrées : pointeur sur une structure struct_processus
   31: --------------------------------------------------------------------------------
   32:   Sorties :
   33: --------------------------------------------------------------------------------
   34:   Effets de bord : néant
   35: ================================================================================
   36: */
   37: 
   38: struct_objet *
   39: simplification(struct_processus *s_etat_processus, struct_objet *s_objet)
   40: {
   41: 
   42:     struct_arbre            *s_arbre;
   43:     struct_arbre            *s_noeud_courant;
   44: 
   45:     struct_liste_chainee    *l_element_courant;
   46: 
   47:     struct_objet            *s_objet_simplifie;
   48:     struct_objet            **t_expression;
   49: 
   50:     unsigned long           i;
   51:     unsigned long           nombre_elements;
   52: 
   53:     s_arbre = NULL;
   54: 
   55:     if ((*s_objet).type == ALG)
   56:     {
   57: #ifdef EXPERIMENTAL_CODE
   58:         // Inversion de l'ordre des instructions dans l'expression
   59: 
   60:         nombre_elements = 0;
   61:         l_element_courant = (*s_objet).objet;
   62: 
   63:         while(l_element_courant != NULL)
   64:         {
   65:             nombre_elements++;
   66:             l_element_courant = (*l_element_courant).suivant;
   67:         }
   68: 
   69:         if ((t_expression = malloc(nombre_elements * sizeof(struct_objet *)))
   70:                 == NULL)
   71:         {
   72:             (*s_etat_processus).erreur_systeme = d_es_allocation_memoire;
   73:             return(NULL);
   74:         }
   75: 
   76:         i = nombre_elements - 1;
   77:         l_element_courant = (*s_objet).objet;
   78: 
   79:         while(l_element_courant != NULL)
   80:         {
   81:             if ((t_expression[i--] = copie_objet(s_etat_processus,
   82:                     (*l_element_courant).donnee, 'O')) == NULL)
   83:             {
   84:                 (*s_etat_processus).erreur_systeme = d_es_allocation_memoire;
   85:                 return(NULL);
   86:             }
   87: 
   88:             l_element_courant = (*l_element_courant).suivant;
   89:         }
   90: 
   91:         // L'objet est toujours évaluable car il s'agit d'une expression
   92:         // algébrique (cela revient à dire qu'il n'y a qu'un seul arbre
   93:         // et que celui-ci est terminé).
   94: 
   95:         s_arbre = creation_arbre(s_etat_processus, t_expression, 0,
   96:                 nombre_elements - 1);
   97: 
   98:         for(i = 0; i < nombre_elements; liberation(t_expression[i++]));
   99:         free(t_expression);
  100: #endif
  101: 
  102:         s_objet_simplifie = copie_objet(s_etat_processus, s_objet, 'P');
  103: 
  104:         // On essaye déjà de voir si l'arbre est cohérent...
  105:     }
  106:     else
  107:     {
  108:         s_objet_simplifie = copie_objet(s_etat_processus, s_objet, 'P');
  109:     }
  110: 
  111:     return(s_objet_simplifie);
  112: }
  113: 
  114: 
  115: /*
  116: ================================================================================
  117:   Fonction créant un noeud dans l'arbre
  118: ================================================================================
  119:   Entrées : pointeur sur une structure struct_arbre
  120: --------------------------------------------------------------------------------
  121:   Sorties :
  122: --------------------------------------------------------------------------------
  123:   Effets de bord : néant
  124: ================================================================================
  125: */
  126: 
  127: struct_arbre *
  128: creation_arbre(struct_processus *s_etat_processus,
  129:         struct_objet **t_objets, unsigned long indice,
  130:         unsigned long indice_maximal)
  131: {
  132:     struct_arbre        *s_noeud;
  133: 
  134:     unsigned long       i;
  135: 
  136:     if ((s_noeud = malloc(sizeof(struct_arbre))) == NULL)
  137:     {
  138:         (*s_etat_processus).erreur_systeme = d_es_allocation_memoire;
  139:         return(NULL);
  140:     }
  141: 
  142: // indice ne peut jamais être supérieur à indice_maximal
  143:     
  144: #if 0
  145:         // Création de la racine de l'arbre
  146: 
  147:         if ((s_arbre = malloc(sizeof(struct_arbre))) == NULL)
  148:         {
  149:             (*s_etat_processus).erreur_systeme = d_es_allocation_memoire;
  150:             return(NULL);
  151:         }
  152: 
  153:         s_noeud_courant = s_arbre;
  154: 
  155:         for(i = 0; i < nombre_elements; i++)
  156:         {
  157:             if (strcmp((*t_expression[i]).type, "FCT") == 0)
  158:             {
  159:                 if ((strcmp((*((struct_fonction *) (*t_expression[i]).objet))
  160:                         .nom_fonction, "<<") != 0) &&
  161:                         (strcmp((*((struct_fonction *)
  162:                         (*t_expression[i]).objet)).nom_fonction, ">>") != 0))
  163:                 {
  164:                     // Création d'un noeud
  165:                 }
  166:             }
  167:         }
  168: #endif
  169:     return(NULL);
  170: }
  171: 
  172: 
  173: /*
  174: ================================================================================
  175:   Fonction simplifiant l'arbre q-aire en arbre q'-aire (q <= q')
  176: ================================================================================
  177:   Entrées : pointeur sur une structure struct_arbre
  178: --------------------------------------------------------------------------------
  179:   Sorties :
  180: --------------------------------------------------------------------------------
  181:   Effets de bord : néant
  182: ================================================================================
  183: */
  184: 
  185: void
  186: simplification_arbre(struct_processus *s_etat_processus, struct_arbre *s_noeud)
  187: {
  188:     unsigned long               i;
  189: 
  190:     if ((*s_noeud).feuilles != NULL)
  191:     {
  192:         for(i = 0; i < (*s_noeud).nombre_feuilles; i++)
  193:         {
  194:             simplification_arbre(s_etat_processus, (*s_noeud).feuilles[i]);
  195: 
  196:             /*
  197:              * Si le noeud est une fonction compatible avec une fonction
  198:              * présente dans les feuilles, on réunit les deux.
  199:              */
  200: 
  201:             if ((*(*((*s_noeud).feuilles[i])).objet).type == FCT)
  202:             {
  203:                 if (((strcmp((*((struct_fonction *) (*s_noeud).objet))
  204:                         .nom_fonction, "+") == 0) && (strcmp(
  205:                         (*((struct_fonction *) (*(*((*s_noeud).feuilles[i]))
  206:                         .objet).objet)).nom_fonction, "+") == 0)) ||
  207:                         ((strcmp((*((struct_fonction *) (*s_noeud).objet))
  208:                         .nom_fonction, "*") == 0) && (strcmp(
  209:                         (*((struct_fonction *) (*(*((*s_noeud).feuilles[i]))
  210:                         .objet).objet)).nom_fonction, "*") == 0)))
  211:                 {
  212:                 }
  213:             }
  214:         }
  215:     }
  216: 
  217:     return;
  218: }
  219: 
  220: 
  221: /*
  222: ================================================================================
  223:   Fonction parcourant l'arbre q-aire
  224: ================================================================================
  225:   Entrées : pointeur sur une structure struct_arbre
  226: --------------------------------------------------------------------------------
  227:   Sorties :
  228: --------------------------------------------------------------------------------
  229:   Effets de bord : néant
  230: ================================================================================
  231: */
  232: 
  233: void
  234: parcours_arbre(struct_processus *s_etat_processus, struct_arbre *s_noeud)
  235: {
  236:     unsigned long               i;
  237: 
  238:     if ((*s_noeud).feuilles != NULL)
  239:     {
  240:         for(i = 0; i < (*s_noeud).nombre_feuilles; i++)
  241:         {
  242:             parcours_arbre(s_etat_processus, (*s_noeud).feuilles[i]);
  243:         }
  244:     }
  245: 
  246:     return;
  247: }
  248: 
  249: 
  250: /*
  251: ================================================================================
  252:   Fonction libérant l'arbre q-aire
  253: ================================================================================
  254:   Entrées : pointeur sur une structure struct_arbre
  255: --------------------------------------------------------------------------------
  256:   Sorties :
  257: --------------------------------------------------------------------------------
  258:   Effets de bord : néant
  259: ================================================================================
  260: */
  261: 
  262: void
  263: liberation_arbre(struct_processus *s_etat_processus, struct_arbre *s_noeud)
  264: {
  265:     unsigned long               i;
  266: 
  267:     if ((*s_noeud).feuilles != NULL)
  268:     {
  269:         for(i = 0; i < (*s_noeud).nombre_feuilles; i++)
  270:         {
  271:         }
  272:     }
  273: 
  274:     return;
  275: }
  276: 
  277: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>