00001
00019 #include "abc.h"
00020 #include "dec.h"
00021
00025
00026 static void Dec_GraphPrint_rec( FILE * pFile, Dec_Graph_t * pGraph, Dec_Node_t * pNode, int fCompl, char * pNamesIn[], int * pPos, int LitSizeMax );
00027 static int Dec_GraphPrintGetLeafName( FILE * pFile, int iLeaf, int fCompl, char * pNamesIn[] );
00028 static void Dec_GraphPrintUpdatePos( FILE * pFile, int * pPos, int LitSizeMax );
00029 static int Dec_GraphPrintOutputName( FILE * pFile, char * pNameOut, int fCompl );
00030
00034
00046 void Dec_GraphPrint( FILE * pFile, Dec_Graph_t * pGraph, char * pNamesIn[], char * pNameOut )
00047 {
00048 Vec_Ptr_t * vNamesIn = NULL;
00049 int LitSizeMax, LitSizeCur, Pos, i;
00050
00051
00052 if ( pNamesIn == NULL )
00053 {
00054 vNamesIn = Abc_NodeGetFakeNames( Dec_GraphLeaveNum(pGraph) );
00055 pNamesIn = (char **)vNamesIn->pArray;
00056 }
00057 if ( pNameOut == NULL )
00058 pNameOut = "F";
00059
00060
00061 LitSizeMax = 0;
00062 for ( i = 0; i < Dec_GraphLeaveNum(pGraph); i++ )
00063 {
00064 LitSizeCur = strlen(pNamesIn[i]);
00065 if ( LitSizeMax < LitSizeCur )
00066 LitSizeMax = LitSizeCur;
00067 }
00068 if ( LitSizeMax > 50 )
00069 LitSizeMax = 20;
00070
00071
00072 if ( Dec_GraphIsConst(pGraph) )
00073 {
00074 Pos = Dec_GraphPrintOutputName( pFile, pNameOut, 0 );
00075 fprintf( pFile, "Constant %d", !Dec_GraphIsComplement(pGraph) );
00076 }
00077 else if ( Dec_GraphIsVar(pGraph) )
00078 {
00079 Pos = Dec_GraphPrintOutputName( pFile, pNameOut, 0 );
00080 Dec_GraphPrintGetLeafName( pFile, Dec_GraphVarInt(pGraph), Dec_GraphIsComplement(pGraph), pNamesIn );
00081 }
00082 else
00083 {
00084 Pos = Dec_GraphPrintOutputName( pFile, pNameOut, Dec_GraphIsComplement(pGraph) );
00085 Dec_GraphPrint_rec( pFile, pGraph, Dec_GraphNodeLast(pGraph), 0, pNamesIn, &Pos, LitSizeMax );
00086 }
00087 fprintf( pFile, "\n" );
00088
00089 if ( vNamesIn )
00090 Abc_NodeFreeNames( vNamesIn );
00091 }
00092
00104 void Dec_GraphPrint2_rec( FILE * pFile, Dec_Graph_t * pGraph, Dec_Node_t * pNode, int fCompl, char * pNamesIn[], int * pPos, int LitSizeMax )
00105 {
00106 Dec_Node_t * pNode0, * pNode1;
00107 pNode0 = Dec_GraphNode(pGraph, pNode->eEdge0.Node);
00108 pNode1 = Dec_GraphNode(pGraph, pNode->eEdge1.Node);
00109 if ( Dec_GraphNodeIsVar(pGraph, pNode) )
00110 {
00111 (*pPos) += Dec_GraphPrintGetLeafName( pFile, Dec_GraphNodeInt(pGraph,pNode), fCompl, pNamesIn );
00112 return;
00113 }
00114 if ( !pNode->fNodeOr )
00115 {
00116 if ( !pNode0->fNodeOr )
00117 Dec_GraphPrint_rec( pFile, pGraph, pNode0, pNode->fCompl0, pNamesIn, pPos, LitSizeMax );
00118 else
00119 {
00120 fprintf( pFile, "(" );
00121 (*pPos)++;
00122 Dec_GraphPrint_rec( pFile, pGraph, pNode0, pNode->fCompl0, pNamesIn, pPos, LitSizeMax );
00123 fprintf( pFile, ")" );
00124 (*pPos)++;
00125 }
00126 fprintf( pFile, " " );
00127 (*pPos)++;
00128
00129 Dec_GraphPrintUpdatePos( pFile, pPos, LitSizeMax );
00130
00131 if ( !pNode1->fNodeOr )
00132 Dec_GraphPrint_rec( pFile, pGraph, pNode1, pNode->fCompl1, pNamesIn, pPos, LitSizeMax );
00133 else
00134 {
00135 fprintf( pFile, "(" );
00136 (*pPos)++;
00137 Dec_GraphPrint_rec( pFile, pGraph, pNode1, pNode->fCompl1, pNamesIn, pPos, LitSizeMax );
00138 fprintf( pFile, ")" );
00139 (*pPos)++;
00140 }
00141 return;
00142 }
00143 if ( pNode->fNodeOr )
00144 {
00145 Dec_GraphPrint_rec( pFile, pGraph, pNode0, pNode->fCompl0, pNamesIn, pPos, LitSizeMax );
00146 fprintf( pFile, " + " );
00147 (*pPos) += 3;
00148
00149 Dec_GraphPrintUpdatePos( pFile, pPos, LitSizeMax );
00150
00151 Dec_GraphPrint_rec( pFile, pGraph, pNode1, pNode->fCompl1, pNamesIn, pPos, LitSizeMax );
00152 return;
00153 }
00154 assert( 0 );
00155 }
00156
00168 void Dec_GraphPrint_rec( FILE * pFile, Dec_Graph_t * pGraph, Dec_Node_t * pNode, int fCompl, char * pNamesIn[], int * pPos, int LitSizeMax )
00169 {
00170 Dec_Node_t * pNode0, * pNode1;
00171 Dec_Node_t * pNode00, * pNode01, * pNode10, * pNode11;
00172 pNode0 = Dec_GraphNode(pGraph, pNode->eEdge0.Node);
00173 pNode1 = Dec_GraphNode(pGraph, pNode->eEdge1.Node);
00174 if ( Dec_GraphNodeIsVar(pGraph, pNode) )
00175 {
00176 (*pPos) += Dec_GraphPrintGetLeafName( pFile, Dec_GraphNodeInt(pGraph,pNode), fCompl, pNamesIn );
00177 return;
00178 }
00179 if ( !Dec_GraphNodeIsVar(pGraph, pNode0) && !Dec_GraphNodeIsVar(pGraph, pNode1) )
00180 {
00181 pNode00 = Dec_GraphNode(pGraph, pNode0->eEdge0.Node);
00182 pNode01 = Dec_GraphNode(pGraph, pNode0->eEdge1.Node);
00183 pNode10 = Dec_GraphNode(pGraph, pNode1->eEdge0.Node);
00184 pNode11 = Dec_GraphNode(pGraph, pNode1->eEdge1.Node);
00185 if ( (pNode00 == pNode10 || pNode00 == pNode11) && (pNode01 == pNode10 || pNode01 == pNode11) )
00186 {
00187 fprintf( pFile, "(" );
00188 (*pPos)++;
00189 Dec_GraphPrint_rec( pFile, pGraph, pNode00, pNode00->fCompl0, pNamesIn, pPos, LitSizeMax );
00190 fprintf( pFile, " # " );
00191 (*pPos) += 3;
00192 Dec_GraphPrint_rec( pFile, pGraph, pNode01, pNode01->fCompl1, pNamesIn, pPos, LitSizeMax );
00193 fprintf( pFile, ")" );
00194 (*pPos)++;
00195 return;
00196 }
00197 }
00198 if ( fCompl )
00199 {
00200 fprintf( pFile, "(" );
00201 (*pPos)++;
00202 Dec_GraphPrint_rec( pFile, pGraph, pNode0, !pNode->fCompl0, pNamesIn, pPos, LitSizeMax );
00203 fprintf( pFile, " + " );
00204 (*pPos) += 3;
00205 Dec_GraphPrint_rec( pFile, pGraph, pNode1, !pNode->fCompl1, pNamesIn, pPos, LitSizeMax );
00206 fprintf( pFile, ")" );
00207 (*pPos)++;
00208 }
00209 else
00210 {
00211 fprintf( pFile, "(" );
00212 (*pPos)++;
00213 Dec_GraphPrint_rec( pFile, pGraph, pNode0, pNode->fCompl0, pNamesIn, pPos, LitSizeMax );
00214 Dec_GraphPrint_rec( pFile, pGraph, pNode1, pNode->fCompl1, pNamesIn, pPos, LitSizeMax );
00215 fprintf( pFile, ")" );
00216 (*pPos)++;
00217 }
00218 }
00219
00231 int Dec_GraphPrintGetLeafName( FILE * pFile, int iLeaf, int fCompl, char * pNamesIn[] )
00232 {
00233 static char Buffer[100];
00234 sprintf( Buffer, "%s%s", pNamesIn[iLeaf], fCompl? "\'" : "" );
00235 fprintf( pFile, "%s", Buffer );
00236 return strlen( Buffer );
00237 }
00238
00250 void Dec_GraphPrintUpdatePos( FILE * pFile, int * pPos, int LitSizeMax )
00251 {
00252 int i;
00253 if ( *pPos + LitSizeMax < 77 )
00254 return;
00255 fprintf( pFile, "\n" );
00256 for ( i = 0; i < 10; i++ )
00257 fprintf( pFile, " " );
00258 *pPos = 10;
00259 }
00260
00272 int Dec_GraphPrintOutputName( FILE * pFile, char * pNameOut, int fCompl )
00273 {
00274 if ( pNameOut == NULL )
00275 return 0;
00276 fprintf( pFile, "%6s%s = ", pNameOut, fCompl? "\'" : " " );
00277 return 10;
00278 }
00279
00283
00284