00001
00021 #include "aig.h"
00022
00023
00024
00025
00026
00027
00028
00032
00033 static inline int Aig_FanoutCreate( int FanId, int Num ) { assert( Num < 2 ); return (FanId << 1) | Num; }
00034 static inline int * Aig_FanoutObj( int * pData, int ObjId ) { return pData + 5*ObjId; }
00035 static inline int * Aig_FanoutPrev( int * pData, int iFan ) { return pData + 5*(iFan >> 1) + 1 + (iFan & 1); }
00036 static inline int * Aig_FanoutNext( int * pData, int iFan ) { return pData + 5*(iFan >> 1) + 3 + (iFan & 1); }
00037
00041
00053 void Aig_ManFanoutStart( Aig_Man_t * p )
00054 {
00055 Aig_Obj_t * pObj;
00056 int i;
00057 assert( Aig_ManBufNum(p) == 0 );
00058
00059 assert( p->pFanData == NULL );
00060 p->nFansAlloc = 2 * Aig_ManObjNumMax(p);
00061 if ( p->nFansAlloc < (1<<12) )
00062 p->nFansAlloc = (1<<12);
00063 p->pFanData = ALLOC( int, 5 * p->nFansAlloc );
00064 memset( p->pFanData, 0, sizeof(int) * 5 * p->nFansAlloc );
00065
00066 Aig_ManForEachObj( p, pObj, i )
00067 {
00068 if ( Aig_ObjChild0(pObj) )
00069 Aig_ObjAddFanout( p, Aig_ObjFanin0(pObj), pObj );
00070 if ( Aig_ObjChild1(pObj) )
00071 Aig_ObjAddFanout( p, Aig_ObjFanin1(pObj), pObj );
00072 }
00073 }
00074
00086 void Aig_ManFanoutStop( Aig_Man_t * p )
00087 {
00088 assert( p->pFanData != NULL );
00089 FREE( p->pFanData );
00090 p->nFansAlloc = 0;
00091 }
00092
00104 void Aig_ObjAddFanout( Aig_Man_t * p, Aig_Obj_t * pObj, Aig_Obj_t * pFanout )
00105 {
00106 int iFan, * pFirst, * pPrevC, * pNextC, * pPrev, * pNext;
00107 assert( p->pFanData );
00108 assert( !Aig_IsComplement(pObj) && !Aig_IsComplement(pFanout) );
00109 assert( pFanout->Id > 0 );
00110 if ( pObj->Id >= p->nFansAlloc || pFanout->Id >= p->nFansAlloc )
00111 {
00112 int nFansAlloc = 2 * AIG_MAX( pObj->Id, pFanout->Id );
00113 p->pFanData = REALLOC( int, p->pFanData, 5 * nFansAlloc );
00114 memset( p->pFanData + 5 * p->nFansAlloc, 0, sizeof(int) * 5 * (nFansAlloc - p->nFansAlloc) );
00115 p->nFansAlloc = nFansAlloc;
00116 }
00117 assert( pObj->Id < p->nFansAlloc && pFanout->Id < p->nFansAlloc );
00118 iFan = Aig_FanoutCreate( pFanout->Id, Aig_ObjWhatFanin(pFanout, pObj) );
00119 pPrevC = Aig_FanoutPrev( p->pFanData, iFan );
00120 pNextC = Aig_FanoutNext( p->pFanData, iFan );
00121 pFirst = Aig_FanoutObj( p->pFanData, pObj->Id );
00122 if ( *pFirst == 0 )
00123 {
00124 *pFirst = iFan;
00125 *pPrevC = iFan;
00126 *pNextC = iFan;
00127 }
00128 else
00129 {
00130 pPrev = Aig_FanoutPrev( p->pFanData, *pFirst );
00131 pNext = Aig_FanoutNext( p->pFanData, *pPrev );
00132 assert( *pNext == *pFirst );
00133 *pPrevC = *pPrev;
00134 *pNextC = *pFirst;
00135 *pPrev = iFan;
00136 *pNext = iFan;
00137 }
00138 }
00139
00151 void Aig_ObjRemoveFanout( Aig_Man_t * p, Aig_Obj_t * pObj, Aig_Obj_t * pFanout )
00152 {
00153 int iFan, * pFirst, * pPrevC, * pNextC, * pPrev, * pNext;
00154 assert( p->pFanData && pObj->Id < p->nFansAlloc && pFanout->Id < p->nFansAlloc );
00155 assert( !Aig_IsComplement(pObj) && !Aig_IsComplement(pFanout) );
00156 assert( pFanout->Id > 0 );
00157 iFan = Aig_FanoutCreate( pFanout->Id, Aig_ObjWhatFanin(pFanout, pObj) );
00158 pPrevC = Aig_FanoutPrev( p->pFanData, iFan );
00159 pNextC = Aig_FanoutNext( p->pFanData, iFan );
00160 pPrev = Aig_FanoutPrev( p->pFanData, *pNextC );
00161 pNext = Aig_FanoutNext( p->pFanData, *pPrevC );
00162 assert( *pPrev == iFan );
00163 assert( *pNext == iFan );
00164 pFirst = Aig_FanoutObj( p->pFanData, pObj->Id );
00165 assert( *pFirst > 0 );
00166 if ( *pFirst == iFan )
00167 {
00168 if ( *pNextC == iFan )
00169 {
00170 *pFirst = 0;
00171 *pPrev = 0;
00172 *pNext = 0;
00173 *pPrevC = 0;
00174 *pNextC = 0;
00175 return;
00176 }
00177 *pFirst = *pNextC;
00178 }
00179 *pPrev = *pPrevC;
00180 *pNext = *pNextC;
00181 *pPrevC = 0;
00182 *pNextC = 0;
00183 }
00184
00188
00189