BinClone/CloneSearcherLib/CSAssemblyFile.cpp
//
// Extract regions.
// If bStoreDb == true, then store the regions into the database.
// If bFindExactClones == true or bFindExactClones == ture, then retrieve the clones from the database
//
Extract regions.
If bStoreDb == true, then store the regions into the database.
If bFindExactClones == true or bFindExactClones == ture, then retrieve the clones from the database
Extract regions . If bStoreDb == true then store the regions into the database . If bFindExactClones == true or bFindExactClones == ture then retrieve the clones from the database
bool CCSAssemblyFile::extractRegions(CCSDatabaseMgr* pDBMgr, const CCSParam& param, bool bFindExactClones, bool bFindInexactClones, bool bStoreDB, bool bFirstScan , CCSAssemblyFileMgr* pAssemblyFileMgr)
{
    int nRegionsInFile = 0;
	for (int fcnIdx = 0; fcnIdx < m_functions.GetSize(); ++fcnIdx) {
		CCSAssemblyFunction* pFcn = m_functions.GetAt(fcnIdx);
		pFcn->resetNumRegions();
		int fcnSize = (pFcn->m_endIdx -1) - (pFcn->m_startIdx +1) + 1;
		if (fcnSize < param.m_windowSize)
			continue;
		for (int sIdx = pFcn->m_startIdx + 1; sIdx < pFcn->m_endIdx; sIdx += param.m_stride) {
			int eIdx = sIdx + param.m_windowSize - 1;
			if (eIdx > pFcn->m_endIdx - 1)
				eIdx = pFcn->m_endIdx - 1;
			if (pFcn->getNumRegions() > 0 && eIdx - sIdx + 1 < param.m_windowSize)
				break;
			int rawSidx = GetAt(sIdx)->getAsmFileLineIdx();
			int rawEidx = GetAt(eIdx)->getAsmFileLineIdx();
			CCSRegion region(pFcn, sIdx, eIdx, rawSidx, rawEidx);
            if (bFirstScan) {   
                ++ CCSRegion::m_cntRegion; 
                if (!region.countRegionFeatures(pAssemblyFileMgr))
				    return false;
			    if (!region.updateGlobalMedians(pAssemblyFileMgr->m_globalMedians, pAssemblyFileMgr))
					    return false;           
                continue;
            }
			const CString contentHashKey = composeContentHashKey(sIdx, eIdx);
			if (contentHashKey.IsEmpty())
				continue;
			if (m_hashObject.GetHashTableSize()==17) 
				m_hashObject.InitHashTable(15485863); 
			string strContentHashKey = CT2A((LPCTSTR)contentHashKey);
			const char * cstrContentHashKey = strContentHashKey.c_str() ;
			region.m_hashValue = region.hashGenerator(cstrContentHashKey);
			if (bFindExactClones) {
				CCSRegions cloneRegions;
				if (!pDBMgr->fetchRegions(param.m_dbParamID, region.m_hashValue, cloneRegions)) {
					cloneRegions.cleanup();
					return false;
				}
				if (!m_clones.makeClones(region, cloneRegions)) {
					cloneRegions.cleanup();
					return false;
				}
				cloneRegions.cleanup();
			}
			if (bFindInexactClones) {
				CCSRegions cloneRegions;
                if (!region.countRegionFeatures(pAssemblyFileMgr))
					return false;	
				if (!pDBMgr->fetchFeatureVector(region, param))
					return false;								
				if (!pDBMgr->createTargetRegionBinaryVector(region, param))
					return false;
				if (!pDBMgr->fetchInexactScore(region, param))
			        return false;
				if (!pDBMgr->fetchInexactRegions(region, param, cloneRegions)) {
			        cloneRegions.cleanup();
					return false;
				}
				if (!m_clones.makeClones(region, cloneRegions)) {
					cloneRegions.cleanup();
					return false;
				}
				cloneRegions.cleanup();
			}
			if (bStoreDB) {                       
				if (!pDBMgr->storeRegion(region, param))
					return false;
				if (!region.countRegionFeatures(pAssemblyFileMgr))
					return false;
				if (!region.constructVector(pAssemblyFileMgr->m_globalFeatures))
					return false;
				ASSERT(pAssemblyFileMgr->m_globalFeatures.GetSize() == region.getVector().GetSize());
				if (!region.constructBinaryVector(pAssemblyFileMgr->m_mediansNZ, pAssemblyFileMgr->m_globalMedians)) 
					return false;
				if (!pDBMgr->storeInexact2CombRegion(region, pAssemblyFileMgr->m_mediansNZ, param))
					return false;
			}
			pFcn->incNumRegions();         
		}
		nRegionsInFile += pFcn->getNumRegions();
	}
    if (bStoreDB)
        tcout << _T("File: ") << m_filePath.GetString() << _T(" has ") << m_functions.GetSize() << _T(" functions and ") << nRegionsInFile << _T(" regions.") << endl;
    return true;
}
bool CCSAssemblyFile :: extractRegions ( CCSDatabaseMgr * pDBMgr , const CCSParam & param , bool bFindExactClones , bool bFindInexactClones , bool bStoreDB , bool bFirstScan , CCSAssemblyFileMgr * pAssemblyFileMgr ) { int nRegionsInFile = 0 ; for ( int fcnIdx = 0 ; fcnIdx < m_functions . GetSize ( ) ; ++ fcnIdx ) { CCSAssemblyFunction * pFcn = m_functions . GetAt ( fcnIdx ) ; pFcn -> resetNumRegions ( ) ; int fcnSize = ( pFcn -> m_endIdx - 1 ) - ( pFcn -> m_startIdx + 1 ) + 1 ; if ( fcnSize < param . m_windowSize ) continue ; for ( int sIdx = pFcn -> m_startIdx + 1 ; sIdx < pFcn -> m_endIdx ; sIdx += param . m_stride ) { int eIdx = sIdx + param . m_windowSize - 1 ; if ( eIdx > pFcn -> m_endIdx - 1 ) eIdx = pFcn -> m_endIdx - 1 ; if ( pFcn -> getNumRegions ( ) > 0 && eIdx - sIdx + 1 < param . m_windowSize ) break ; int rawSidx = GetAt ( sIdx ) -> getAsmFileLineIdx ( ) ; int rawEidx = GetAt ( eIdx ) -> getAsmFileLineIdx ( ) ; CCSRegion region ( pFcn , sIdx , eIdx , rawSidx , rawEidx ) ; if ( bFirstScan ) { ++ CCSRegion :: m_cntRegion ; if ( ! region . countRegionFeatures ( pAssemblyFileMgr ) ) return false ; if ( ! region . updateGlobalMedians ( pAssemblyFileMgr -> m_globalMedians , pAssemblyFileMgr ) ) return false ; continue ; } const CString contentHashKey = composeContentHashKey ( sIdx , eIdx ) ; if ( contentHashKey . IsEmpty ( ) ) continue ; if ( m_hashObject . GetHashTableSize ( ) == 17 ) m_hashObject . InitHashTable ( 15485863 ) ; string strContentHashKey = CT2A ( ( LPCTSTR ) contentHashKey ) ; const char * cstrContentHashKey = strContentHashKey . c_str ( ) ; region . m_hashValue = region . hashGenerator ( cstrContentHashKey ) ; if ( bFindExactClones ) { CCSRegions cloneRegions ; if ( ! pDBMgr -> fetchRegions ( param . m_dbParamID , region . m_hashValue , cloneRegions ) ) { cloneRegions . cleanup ( ) ; return false ; } if ( ! m_clones . makeClones ( region , cloneRegions ) ) { cloneRegions . cleanup ( ) ; return false ; } cloneRegions . cleanup ( ) ; } if ( bFindInexactClones ) { CCSRegions cloneRegions ; if ( ! region . countRegionFeatures ( pAssemblyFileMgr ) ) return false ; if ( ! pDBMgr -> fetchFeatureVector ( region , param ) ) return false ; if ( ! pDBMgr -> createTargetRegionBinaryVector ( region , param ) ) return false ; if ( ! pDBMgr -> fetchInexactScore ( region , param ) ) return false ; if ( ! pDBMgr -> fetchInexactRegions ( region , param , cloneRegions ) ) { cloneRegions . cleanup ( ) ; return false ; } if ( ! m_clones . makeClones ( region , cloneRegions ) ) { cloneRegions . cleanup ( ) ; return false ; } cloneRegions . cleanup ( ) ; } if ( bStoreDB ) { if ( ! pDBMgr -> storeRegion ( region , param ) ) return false ; if ( ! region . countRegionFeatures ( pAssemblyFileMgr ) ) return false ; if ( ! region . constructVector ( pAssemblyFileMgr -> m_globalFeatures ) ) return false ; ASSERT ( pAssemblyFileMgr -> m_globalFeatures . GetSize ( ) == region . getVector ( ) . GetSize ( ) ) ; if ( ! region . constructBinaryVector ( pAssemblyFileMgr -> m_mediansNZ , pAssemblyFileMgr -> m_globalMedians ) ) return false ; if ( ! pDBMgr -> storeInexact2CombRegion ( region , pAssemblyFileMgr -> m_mediansNZ , param ) ) return false ; } pFcn -> incNumRegions ( ) ; } nRegionsInFile += pFcn -> getNumRegions ( ) ; } if ( bStoreDB ) tcout << _T ( " " ) << m_filePath . GetString ( ) << _T ( " " ) << m_functions . GetSize ( ) << _T ( " " ) << nRegionsInFile << _T ( " " ) << endl ; return true ; }
Extract regions.
Extract regions .
// For each function // skip this function if its size is smaller than the window size // ensure the endIdx does not go beyond the limit // skip this region if it is not the first region in the function and its size is smaller than the window size // because it is a region towards the end of a function, and it has already been covered by some previous regions. // create a new region // if bStoreDB is false, it means that the program is in first scan phase              // increament the total number of regions processes so far in the first scan //update the medians.  // create a hash of this region // the function contains at least one mnemonic statements. // the default hash table size is 17. Hash table needs to be intilized once // Intialized the hash table with a big prime number to reduce collision //region.m_hashValue = m_hashObject.HashKey((LPCTSTR) contentHashKey); // djb2 hash function: // find exact clones from DB //		if(!pDBMgr->constructScoreVector(param)) //			return false; // Store region into database (not storing the vector though) // actually count the features. // compute the binary vector, then store the hash into the database //tcout << _T("Region: ") << region.m_dbRegionID << _T(" start: ") << sIdx << _T(" end: ") << eIdx << endl; //tcout << _T("Region: ") << region.m_dbRegionID << _T(" rawStart: ") << rawSidx << _T(" rawEnd: ") << rawEidx << endl;