57static inline void Dau_DsdMergeStoreClean(
Dau_Sto_t * pS,
int nShared )
60 pS->iVarUsed = nShared;
65static inline void Dau_DsdMergeStoreCleanOutput(
Dau_Sto_t * pS )
67 pS->pPosOutput = pS->pOutput;
69static inline void Dau_DsdMergeStoreAddToOutput(
Dau_Sto_t * pS,
char * pBeg,
char * pEnd )
72 *pS->pPosOutput++ = *pBeg++;
74static inline void Dau_DsdMergeStoreAddToOutputChar(
Dau_Sto_t * pS,
char c )
76 *pS->pPosOutput++ = c;
79static inline int Dau_DsdMergeStoreStartDef(
Dau_Sto_t * pS,
char c )
81 pS->pPosStore[pS->iVarUsed] = pS->pStore[pS->iVarUsed];
82 if (c) *pS->pPosStore[pS->iVarUsed]++ = c;
83 return pS->iVarUsed++;
85static inline void Dau_DsdMergeStoreAddToDef(
Dau_Sto_t * pS,
int New,
char * pBeg,
char * pEnd )
88 *pS->pPosStore[New]++ = *pBeg++;
90static inline void Dau_DsdMergeStoreAddToDefChar(
Dau_Sto_t * pS,
int New,
char c )
92 *pS->pPosStore[New]++ = c;
94static inline void Dau_DsdMergeStoreStopDef(
Dau_Sto_t * pS,
int New,
char c )
96 if (c) *pS->pPosStore[New]++ = c;
97 *pS->pPosStore[New]++ = 0;
100static inline char Dau_DsdMergeStoreCreateDef(
Dau_Sto_t * pS,
char * pBeg,
char * pEnd )
102 int New = Dau_DsdMergeStoreStartDef( pS, 0 );
103 Dau_DsdMergeStoreAddToDef( pS, New, pBeg, pEnd );
104 Dau_DsdMergeStoreStopDef( pS, New, 0 );
107static inline void Dau_DsdMergeStorePrintDefs(
Dau_Sto_t * pS )
111 if ( pS->pStore[i][0] )
112 printf(
"%c = %s\n",
'a' + i, pS->pStore[i] );
126static inline void Dau_DsdMergeCopy(
char * pDsd,
int fCompl,
char * pRes )
128 if ( fCompl && pDsd[0] ==
'!' )
130 if ( Dau_DsdIsConst(pDsd) )
131 pRes[0] = (fCompl ? (char)((int)pDsd[0] ^ 1) : pDsd[0]), pRes[1] = 0;
133 sprintf( pRes,
"%s%s", fCompl ?
"!" :
"", pDsd );
147static inline void Dau_DsdMergeReplace(
char * pDsd,
int * pMatches,
int * pMap )
150 for ( i = 0; pDsd[i]; i++ )
153 if ( pDsd[i] ==
'<' && pDsd[pMatches[i]+1] ==
'{' )
155 if ( (pDsd[i] >=
'A' && pDsd[i] <=
'F') || (pDsd[i] >=
'0' && pDsd[i] <=
'9') )
156 while ( (pDsd[i] >=
'A' && pDsd[i] <=
'F') || (pDsd[i] >=
'0' && pDsd[i] <=
'9') )
159 if ( pDsd[i] >=
'a' && pDsd[i] <=
'z' )
160 pDsd[i] =
'a' + pMap[ pDsd[i] -
'a' ];
163static inline void Dau_DsdMergeMatches(
char * pDsd,
int * pMatches )
167 for ( i = 0; pDsd[i]; i++ )
170 if ( pDsd[i] ==
'(' || pDsd[i] ==
'[' || pDsd[i] ==
'<' || pDsd[i] ==
'{' )
171 pNested[nNested++] = i;
172 else if ( pDsd[i] ==
')' || pDsd[i] ==
']' || pDsd[i] ==
'>' || pDsd[i] ==
'}' )
173 pMatches[pNested[--nNested]] = i;
178static inline void Dau_DsdMergeVarPres(
char * pDsd,
int * pMatches,
int * pPres,
int Mask )
181 for ( i = 0; pDsd[i]; i++ )
184 if ( pDsd[i] ==
'<' && pDsd[pMatches[i]+1] ==
'{' )
186 if ( (pDsd[i] >=
'A' && pDsd[i] <=
'F') || (pDsd[i] >=
'0' && pDsd[i] <=
'9') )
187 while ( (pDsd[i] >=
'A' && pDsd[i] <=
'F') || (pDsd[i] >=
'0' && pDsd[i] <=
'9') )
190 if ( !(pDsd[i] >=
'a' && pDsd[i] <=
'z') )
194 pPres[pDsd[i]-
'a'] |= Mask;
197static inline int Dau_DsdMergeCountShared(
int * pPres,
int Mask )
201 Counter += (pPres[i] == Mask);
204static inline int Dau_DsdMergeFindShared(
char * pDsd0,
char * pDsd1,
int * pMatches0,
int * pMatches1,
int * pVarPres )
207 Dau_DsdMergeVarPres( pDsd0, pMatches0, pVarPres, 1 );
208 Dau_DsdMergeVarPres( pDsd1, pMatches1, pVarPres, 2 );
209 return Dau_DsdMergeCountShared( pVarPres, 3 );
211static inline int Dau_DsdMergeCreateMaps(
int * pVarPres,
int nShared,
int * pOld2New,
int * pNew2Old )
213 int i, Counter = 0, Counter2 = nShared;
216 if ( pVarPres[i] == 0 )
218 if ( pVarPres[i] == 3 )
220 pOld2New[i] = Counter;
221 pNew2Old[Counter] = i;
225 assert( pVarPres[i] == 1 || pVarPres[i] == 2 );
226 pOld2New[i] = Counter2;
227 pNew2Old[Counter2] = i;
232static inline void Dau_DsdMergeInlineDefinitions(
char * pDsd,
int * pMatches,
Dau_Sto_t * pS,
char * pRes,
int nShared )
236 char * pBegin = pRes;
237 for ( i = 0; pDsd[i]; i++ )
240 if ( pDsd[i] ==
'<' && pDsd[pMatches[i]+1] ==
'{' )
242 assert( pDsd[pMatches[i]] ==
'>' );
243 for ( ; i <= pMatches[i]; i++ )
246 if ( (pDsd[i] >=
'A' && pDsd[i] <=
'F') || (pDsd[i] >=
'0' && pDsd[i] <=
'9') )
247 while ( (pDsd[i] >=
'A' && pDsd[i] <=
'F') || (pDsd[i] >=
'0' && pDsd[i] <=
'9') )
250 if ( !(pDsd[i] >=
'a' && pDsd[i] <=
'z') || (pDsd[i] -
'a' < nShared) )
257 for ( pDef = pS->pStore[pDsd[i]-
'a']; *pDef; pDef++ )
276static inline void Dau_DsdMergePrintWithStatus(
char *
p,
int * pStatus )
280 for ( i = 0;
p[i]; i++ )
281 if ( !(
p[i] ==
'(' ||
p[i] ==
'[' ||
p[i] ==
'<' ||
p[i] ==
'{' || (
p[i] >=
'a' &&
p[i] <=
'z')) )
283 else if ( pStatus[i] >= 0 )
284 printf(
"%d", pStatus[i] );
297 pStatus[*
p - pStr] = -1;
300 while ( (**
p >=
'A' && **
p <=
'F') || (**
p >=
'0' && **
p <=
'9') )
302 pStatus[*
p - pStr] = -1;
307 char * q = pStr + pMatches[ *
p - pStr ];
312 for ( ; pTemp < q+1; pTemp++ )
313 pStatus[pTemp - pStr] = -1;
316 if ( **
p >=
'a' && **
p <=
'z' )
317 return pStatus[*
p - pStr] = (**
p -
'a' < nShared) ? 0 : 3;
318 if ( **
p ==
'(' || **
p ==
'[' || **
p ==
'<' || **
p ==
'{' )
320 int Status, nPure = 0,
nTotal = 0;
322 char * q = pStr + pMatches[ *
p - pStr ];
323 assert( *q == **
p + 1 + (**
p !=
'(') );
324 for ( (*
p)++; *
p < q; (*p)++ )
327 nPure += (Status == 3);
334 else if ( nPure == 1 )
336 else if ( nPure <
nTotal )
338 else if ( nPure ==
nTotal )
341 return (pStatus[pOld - pStr] = Status);
346static inline int Dau_DsdMergeStatus(
char * pDsd,
int * pMatches,
int nShared,
int * pStatus )
362static inline int Dau_DsdMergeGetStatus(
char * pBeg,
char * pStr,
int * pMatches,
int * pStatus )
366 while ( (*pBeg >=
'A' && *pBeg <=
'F') || (*pBeg >=
'0' && *pBeg <=
'9') )
370 char * q = pStr + pMatches[pBeg - pStr];
374 return pStatus[pBeg - pStr];
383 Dau_DsdMergeStoreAddToOutputChar( pS, **
p );
387 while ( (**
p >=
'A' && **
p <=
'F') || (**
p >=
'0' && **
p <=
'9') )
390 Dau_DsdMergeStoreAddToOutputChar( pS, **
p );
395 char * q = pStr + pMatches[ *
p - pStr ];
401 for ( ; pTemp < q+1; pTemp++ )
402 Dau_DsdMergeStoreAddToOutputChar( pS, *pTemp );
405 if ( **
p >=
'a' && **
p <=
'z' )
408 Dau_DsdMergeStoreAddToOutputChar( pS, **
p );
411 if ( **
p ==
'(' || **
p ==
'[' || **
p ==
'<' || **
p ==
'{' )
413 int New, StatusFan, Status = pStatus[*
p - pStr];
414 char * pBeg, * q = pStr + pMatches[ *
p - pStr ];
415 assert( *q == **
p + 1 + (**
p !=
'(') );
425 Dau_DsdMergeStoreAddToOutputChar( pS, **
p );
426 for ( (*
p)++; *
p < q; (*p)++ )
430 Dau_DsdMergeStoreAddToOutputChar( pS,
'!' );
435 Dau_DsdMergeStoreAddToOutputChar( pS, **
p );
439 if ( Status == 1 || **
p ==
'<' || **
p ==
'{' )
441 Dau_DsdMergeStoreAddToOutputChar( pS, **
p );
442 for ( (*
p)++; *
p < q; (*p)++ )
446 Dau_DsdMergeStoreAddToOutputChar( pS,
'!' );
450 StatusFan = Dau_DsdMergeGetStatus( pBeg, pStr, pMatches, pStatus );
452 if ( StatusFan == 3 )
454 int New = Dau_DsdMergeStoreCreateDef( pS, pBeg, *
p+1 );
455 Dau_DsdMergeStoreAddToOutputChar( pS, (
char)(
'a' + New) );
458 Dau_DsdMergeStoreAddToOutputChar( pS, **
p );
465 Dau_DsdMergeStoreAddToOutputChar( pS, **
p );
466 New = Dau_DsdMergeStoreStartDef( pS, **
p );
467 for ( (*
p)++; *
p < q; (*p)++ )
470 StatusFan = Dau_DsdMergeGetStatus( pBeg, pStr, pMatches, pStatus );
473 if ( StatusFan != 3 )
474 Dau_DsdMergeStoreAddToOutputChar( pS,
'!' );
476 Dau_DsdMergeStoreAddToDefChar( pS, New,
'!' );
481 if ( StatusFan == 3 )
482 Dau_DsdMergeStoreAddToDef( pS, New, pBeg, *
p+1 );
484 Dau_DsdMergeStoreStopDef( pS, New, *q );
485 Dau_DsdMergeStoreAddToOutputChar( pS, (
char)(
'a' + New) );
486 Dau_DsdMergeStoreAddToOutputChar( pS, **
p );
494static inline void Dau_DsdMergeSubstitute(
Dau_Sto_t * pS,
char * pDsd,
int * pMatches,
int * pStatus )
506 Dau_DsdMergeStoreAddToOutputChar( pS, 0 );
524 while ( (**
p >=
'A' && **
p <=
'F') || (**
p >=
'0' && **
p <=
'9') )
528 char * q = pStr + pMatches[*
p - pStr];
532 if ( **
p >=
'a' && **
p <=
'z' )
534 if ( **
p ==
'(' || **
p ==
'[' || **
p ==
'<' || **
p ==
'{' )
536 char * q = pStr + pMatches[ *
p - pStr ];
537 assert( *q == **
p + 1 + (**
p !=
'(') );
538 for ( (*
p)++; *
p < q; (*p)++ )
540 int fCompl = (**
p ==
'!');
541 char * pBeg = fCompl ? *
p + 1 : *
p;
543 if ( (!fCompl && *pBeg ==
'(' && *q ==
')') || (*pBeg ==
'[' && *q ==
']') )
556 char * q, *
p = pDsd;
560 for ( q =
p; *
p;
p++ )
563 if ( *
p ==
'!' &&
p != q && *(q-1) ==
'!' )
587char *
Dau_DsdMerge(
char * pDsd0i,
int * pPerm0,
char * pDsd1i,
int * pPerm1,
int fCompl0,
int fCompl1,
int nVars )
591 static int Counter = 0;
603 int nVarsShared, nVarsTotal;
605 word * pTruth, * pt = NULL, * pt0 = NULL, * pt1 = NULL;
611 Dau_DsdMergeCopy( pDsd0i, fCompl0, pDsd0 );
612 Dau_DsdMergeCopy( pDsd1i, fCompl1, pDsd1 );
614printf(
"\nAfter copying:\n" );
616printf(
"%s\n", pDsd0 );
618printf(
"%s\n", pDsd1 );
620 if ( Dau_DsdIsConst(pDsd0) || Dau_DsdIsConst(pDsd1) )
622 if ( Dau_DsdIsConst0(pDsd0) )
624 else if ( Dau_DsdIsConst1(pDsd0) )
626 else if ( Dau_DsdIsConst0(pDsd1) )
628 else if ( Dau_DsdIsConst1(pDsd1) )
635 Dau_DsdMergeMatches( pDsd0, pMatches0 );
636 Dau_DsdMergeMatches( pDsd1, pMatches1 );
638 Dau_DsdMergeReplace( pDsd0, pMatches0, pPerm0 );
639 Dau_DsdMergeReplace( pDsd1, pMatches1, pPerm1 );
641printf(
"After replacement:\n" );
643printf(
"%s\n", pDsd0 );
645printf(
"%s\n", pDsd1 );
651 Abc_TtCopy( pParts[0], pt0, Abc_TtWordNum(nVars), 0 );
656 Abc_TtCopy( pParts[1], pt1, Abc_TtWordNum(nVars), 0 );
657 Abc_TtAnd( pParts[2], pParts[0], pParts[1], Abc_TtWordNum(nVars), 0 );
661 nVarsShared = Dau_DsdMergeFindShared(pDsd0, pDsd1, pMatches0, pMatches1, pVarPres);
662 if ( nVarsShared == 0 )
664 sprintf( pRes,
"(%s%s)", pDsd0, pDsd1 );
666printf(
"Disjoint:\n" );
668printf(
"%s\n", pRes );
670 Dau_DsdMergeMatches( pRes, pMatches );
674printf(
"Normalized:\n" );
676printf(
"%s\n", pRes );
683 nVarsTotal = Dau_DsdMergeCreateMaps( pVarPres, nVarsShared, pOld2New, pNew2Old );
685 Dau_DsdMergeReplace( pDsd0, pMatches0, pOld2New );
686 Dau_DsdMergeReplace( pDsd1, pMatches1, pOld2New );
688 Dau_DsdMergeStatus( pDsd0, pMatches0, nVarsShared, pStatus0 );
689 Dau_DsdMergeStatus( pDsd1, pMatches1, nVarsShared, pStatus1 );
691printf(
"Individual status:\n" );
693Dau_DsdMergePrintWithStatus( pDsd0, pStatus0 );
695Dau_DsdMergePrintWithStatus( pDsd1, pStatus1 );
697 Dau_DsdMergeStoreClean( pS, nVarsShared );
699 Dau_DsdMergeStoreCleanOutput( pS );
700 Dau_DsdMergeSubstitute( pS, pDsd0, pMatches0, pStatus0 );
701 strcpy( pDsd0, pS->pOutput );
703printf(
"Substitutions:\n" );
705printf(
"%s\n", pDsd0 );
708 Dau_DsdMergeStoreCleanOutput( pS );
709 Dau_DsdMergeSubstitute( pS, pDsd1, pMatches1, pStatus1 );
710 strcpy( pDsd1, pS->pOutput );
712printf(
"%s\n", pDsd1 );
714Dau_DsdMergeStorePrintDefs( pS );
718 sprintf( pS->pOutput,
"(%s%s)", pDsd0, pDsd1 );
726 if ( Dau_DsdIsConst(pS->pOutput) )
728 strcpy( pRes, pS->pOutput );
732printf(
"Decomposition:\n" );
734printf(
"%s\n", pS->pOutput );
736 Dau_DsdMergeMatches( pS->pOutput, pMatches );
737 Dau_DsdMergeInlineDefinitions( pS->pOutput, pMatches, pS, pRes, nVarsShared );
739printf(
"Inlining:\n" );
741printf(
"%s\n", pRes );
743 Dau_DsdMergeMatches( pRes, pMatches );
744 Dau_DsdMergeReplace( pRes, pMatches, pNew2Old );
747printf(
"Replaced:\n" );
749printf(
"%s\n", pRes );
752printf(
"Normalized:\n" );
754printf(
"%s\n", pRes );
759 if ( !Abc_TtEqual( pParts[2], pt, Abc_TtWordNum(nVars) ) )
760 printf(
"Dau_DsdMerge(): Verification failed!\n" );
780 char * pStr =
"[!(a[be])!(c!df)]";
799 char * pStr1 =
"(!(ab)de)";
800 char * pStr2 =
"(!(ac)f)";
813 pRes =
Dau_DsdMerge( pStr1, Perm0, pStr2, Perm0, 0, 0, 6 );
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
int nTotal
DECLARATIONS ///.
typedefABC_NAMESPACE_IMPL_START struct Dau_Sto_t_ Dau_Sto_t
DECLARATIONS ///.
void Dau_DsdRemoveBraces(char *pDsd, int *pMatches)
void Dau_DsdRemoveBraces_rec(char *pStr, char **p, int *pMatches)
char * Dau_DsdMerge(char *pDsd0i, int *pPerm0, char *pDsd1i, int *pPerm1, int fCompl0, int fCompl1, int nVars)
DECLARATIONS ///.
void Dau_DsdMergeSubstitute_rec(Dau_Sto_t *pS, char *pStr, char **p, int *pMatches, int *pStatus, int fWrite)
int Dau_DsdMergeStatus_rec(char *pStr, char **p, int *pMatches, int nShared, int *pStatus)
word * Dau_DsdToTruth(char *p, int nVars)
int Dau_DsdDecompose(word *pTruth, int nVarsInit, int fSplitPrime, int fWriteTruth, char *pRes)
void Dau_DsdNormalize(char *p)
#define DAU_MAX_VAR
INCLUDES ///.
word Dau_Dsd6ToTruth(char *p)
unsigned __int64 word
DECLARATIONS ///.
char * pPosStore[DAU_MAX_VAR]
char pOutput[2 *DAU_MAX_STR+10]
char pStore[DAU_MAX_VAR][DAU_MAX_STR]