VIS

src/tbl/tblIdentity.c

Go to the documentation of this file.
00001 
00047 #include "tblInt.h"
00048 
00049 static char rcsid[] UNUSED = "$Id: tblIdentity.c,v 1.1 2009/04/11 07:10:15 fabio Exp $";
00050 
00051 /*---------------------------------------------------------------------------*/
00052 /* Constant declarations                                                     */
00053 /*---------------------------------------------------------------------------*/
00054 
00055 /*---------------------------------------------------------------------------*/
00056 /* Stucture declarations                                                     */
00057 /*---------------------------------------------------------------------------*/
00058 
00059 
00060 /*---------------------------------------------------------------------------*/
00061 /* Type declarations                                                         */
00062 /*---------------------------------------------------------------------------*/
00063 
00064 
00065 /*---------------------------------------------------------------------------*/
00066 /* Variable declarations                                                     */
00067 /*---------------------------------------------------------------------------*/
00068 
00069 
00070 /*---------------------------------------------------------------------------*/
00071 /* Macro declarations                                                        */
00072 /*---------------------------------------------------------------------------*/
00073 
00074 
00077 /*---------------------------------------------------------------------------*/
00078 /* Static function prototypes                                                */
00079 /*---------------------------------------------------------------------------*/
00080 
00081 
00097 boolean
00098 Tbl_TableIsIdentity(
00099   Tbl_Table_t *table
00100   )
00101 {
00102   Var_Variable_t *invar = NIL(Var_Variable_t), *outvar = NIL(Var_Variable_t);
00103   Var_Variable_t *var;
00104   int colNum, rowNum;
00105   Tbl_Entry_t *inputEntry, *outputEntry, *defaultEntry, *cumulative;
00106   boolean result;
00107 #if 0
00108   Mvf_Function_t *outMvf, *inMvf, *identMvf;
00109   array_t *mvarValues;
00110   array_t *inMvfArray;
00111   Tbl_Table_t *ident;
00112 #endif
00113 
00114   if (Tbl_TableReadNumInputs(table) != 1) return FALSE;
00115   if (Tbl_TableReadNumOutputs(table) != 1) return FALSE;
00116 
00117   Tbl_TableForEachInputVar(table,colNum,var) {
00118     invar = var;
00119   }
00120 
00121   Tbl_TableForEachOutputVar(table,colNum,var) {
00122     outvar = var;
00123   }
00124 
00125   if (!Var_VariablesTestHaveSameDomain(invar,outvar)) return FALSE;
00126 
00127   /* We test the following condition.  If a table specifies a value
00128    * for all inputs and all rows are consistent with the identity,
00129    * then the table is an identity.  A normal default declaration is
00130    * treated as a row whose input and output parts equal the default
00131    * value.  An equality default declaration is treated like a row
00132    * with equality output entry and don't care input entry.
00133    * Completeness and nondeterminism are implicitly tested by this
00134    * criterion.
00135    */
00136   defaultEntry = Tbl_TableDefaultReadEntry(table, 0);
00137   if (defaultEntry == NIL(Tbl_Entry_t)) {
00138     cumulative = Tbl_EntryAlloc(Tbl_EntryNormal_c);
00139   } else if (Tbl_EntryIsEqual(defaultEntry)) {
00140     cumulative = Tbl_EntryAlloc(Tbl_EntryNormal_c);
00141     Tbl_EntrySetValue(cumulative, 0, Var_VariableReadNumValues(invar) - 1);
00142   } else {
00143     if (Tbl_EntryReadNumValues(defaultEntry) != 1) return FALSE;
00144     cumulative = Tbl_EntryDup(defaultEntry);
00145   }
00146   Tbl_TableForEachOutputEntry(table, rowNum, colNum, outputEntry) {
00147     Tbl_Entry_t *tmpEntry;
00148     assert(colNum == 0);
00149     inputEntry = Tbl_TableReadEntry(table, rowNum, 0, 0);
00150     assert(!Tbl_EntryIsEqual(inputEntry));
00151     if (!Tbl_EntryIsEqual(outputEntry)) {
00152       if (Tbl_EntryReadNumValues(outputEntry) != 1 ||
00153           !Tbl_EntryTestEqualEntry(outputEntry,inputEntry)) {
00154         Tbl_EntryFree(cumulative);
00155         return FALSE;
00156       }
00157     }
00158     tmpEntry = Tbl_EntryMerge(cumulative, inputEntry);
00159     Tbl_EntryFree(cumulative);
00160     cumulative = tmpEntry;
00161   }
00162   result = Tbl_EntryReadNumValues(cumulative) ==
00163     Var_VariableReadNumValues(invar);
00164   Tbl_EntryFree(cumulative);
00165   return result;
00166 
00167 #if 0
00168 
00169   /* Use the MVF sledgehammer.  Only proceed if an mdd manager was
00170    * passed as argument.
00171    */
00172 
00173   if (mddMgr == NIL(mdd_manager)) return FALSE;
00174 
00175   /* Create MDD variables for the table inputs. */
00176   mdd_restart(mddMgr);
00177   mvarValues = array_alloc(int, 1);
00178   array_insert_last(int, mvarValues, Var_VariableReadNumValues(invar));
00179   mdd_create_variables(mddMgr, mvarValues, NIL(array_t), NIL(array_t));
00180   array_free(mvarValues);
00181 
00182   /*
00183    * Construct an MVF for each table input. The MVF for column i is the MVF
00184    * for MDD variable i.
00185    */
00186   inMvf = Mvf_FunctionCreateFromVariable(mddMgr, 0);
00187   inMvfArray = array_alloc(Mvf_Function_t *, 1);
00188   array_insert_last(Mvf_Function_t *, inMvfArray, inMvf);
00189 
00190   outMvf = Tbl_TableBuildMvfFromFanins(table, 0, inMvfArray, mddMgr);
00191 
00192   /* Build a reference identity.  This can be sped up quite a bit, but
00193      we are practicing using the tbl package. */
00194   ident = Tbl_TableAlloc();
00195   colNum = Tbl_TableAddColumn(ident,invar,0);
00196   assert(colNum == 1);
00197   colNum = Tbl_TableAddColumn(ident,outvar,1);
00198   assert(colNum == 1);
00199   rowNum = Tbl_TableAddRow(ident);
00200   assert(rowNum == 0);
00201   inputEntry = Tbl_EntryAlloc(Tbl_EntryNormal_c);
00202   Tbl_EntrySetValue(inputEntry, 0, Var_VariableReadNumValues(invar)-1);
00203   Tbl_TableSetEntry(ident,inputEntry, 0, 0, 0);
00204   outputEntry = Tbl_EntryAlloc(Tbl_EntryEqual_c);
00205   Tbl_EntrySetEqual(outputEntry, 0);
00206   Tbl_TableSetEntry(ident,outputEntry, 0, 0, 1);
00207 
00208   identMvf = Tbl_TableBuildMvfFromFanins(ident, 0, inMvfArray, mddMgr);
00209   Mvf_FunctionArrayFree(inMvfArray);
00210   Tbl_TableFree(ident);
00211   result = Mvf_FunctionTestIsEqualToFunction(outMvf, identMvf);
00212   Mvf_FunctionFree(outMvf);
00213   Mvf_FunctionFree(identMvf);
00214   return result;
00215 #endif
00216 
00217 } /* Tbl_TableIsIdentity */
00218 
00219 
00234 boolean
00235 Tbl_TableIsInverter(
00236   Tbl_Table_t *table
00237   )
00238 {
00239   Var_Variable_t *invar = NIL(Var_Variable_t), *outvar = NIL(Var_Variable_t);
00240   Var_Variable_t *var;
00241   int colNum, rowNum;
00242   Tbl_Entry_t *inputEntry, *outputEntry, *defaultEntry, *cumulative;
00243   boolean result;
00244 
00245   if (Tbl_TableReadNumInputs(table) != 1) return FALSE;
00246   if (Tbl_TableReadNumOutputs(table) != 1) return FALSE;
00247 
00248   Tbl_TableForEachInputVar(table,colNum,var) {
00249     invar = var;
00250   }
00251   if (Var_VariableReadNumValues(invar) != 2 ||
00252       Var_VariableTestIsSymbolic(invar)) return FALSE;
00253 
00254   Tbl_TableForEachOutputVar(table,colNum,var) {
00255     outvar = var;
00256   }
00257   if (Var_VariableReadNumValues(outvar) != 2 ||
00258       Var_VariableTestIsSymbolic(outvar)) return FALSE;
00259 
00260   defaultEntry = Tbl_TableDefaultReadEntry(table, 0);
00261   if (defaultEntry == NIL(Tbl_Entry_t)) {
00262     cumulative = Tbl_EntryAlloc(Tbl_EntryNormal_c);
00263   } else if (Tbl_EntryIsEqual(defaultEntry)) {
00264     return FALSE;
00265   } else {
00266     if (Tbl_EntryReadNumValues(defaultEntry) != 1)
00267       return FALSE;
00268     cumulative = Tbl_EntryDup(defaultEntry);
00269     Tbl_EntryComplement(cumulative, 0, 1);
00270   }
00271   Tbl_TableForEachOutputEntry(table, rowNum, colNum, outputEntry) {
00272     Tbl_Entry_t *tmpEntry;
00273     assert(colNum == 0);
00274     if (Tbl_EntryIsEqual(outputEntry)) {
00275       Tbl_EntryFree(cumulative);
00276       return FALSE;
00277     }
00278 
00279     inputEntry = Tbl_TableReadEntry(table, rowNum, 0, 0);
00280     assert(!Tbl_EntryIsEqual(inputEntry));
00281     if (Tbl_EntryReadNumValues(outputEntry) != 1 ||
00282         Tbl_EntryTestIntersectEntry(outputEntry,inputEntry)) {
00283       Tbl_EntryFree(cumulative);
00284       return FALSE;
00285     }
00286     tmpEntry = Tbl_EntryMerge(cumulative, inputEntry);
00287     Tbl_EntryFree(cumulative);
00288     cumulative = tmpEntry;
00289   }
00290   result = Tbl_EntryReadNumValues(cumulative) == 2;
00291   Tbl_EntryFree(cumulative);
00292   return result;
00293 
00294 } /* Tbl_TableIsInverter */