int FindIndexByElement(pElementType pEle, pListHeader plh) { for(int i = 0; i < plh->AddreBookRealSize; i++) { if(-1 != CompareElement(plh->pltArr[i], pEle)) return i; } return -1; }
int CZipCentralDir::FindFile(LPCTSTR lpszFileName, bool bCaseSensitive, bool bSporadically, bool bFileNameOnly) { // this is required for fast finding and is done only once if (!m_bConvertAfterOpen) { TRACE(_T("%s(%i) : Converting all the filenames.\n"),__FILE__,__LINE__); ConvertAll(); } if (!m_bFindFastEnabled) EnableFindFast(true, bSporadically ? !bCaseSensitive : bCaseSensitive); int iResult = -1; if (bFileNameOnly) { // a non-effective search (treat an array as unsorted) int iSize = m_findarray.GetSize(); for (int i = 0; i < iSize; i++) { CZipString sz = GetProperHeaderFileName(m_findarray[i].m_pHeader); CZipPathComponent::RemoveSeparators(sz); // to find a dir CZipPathComponent zpc(sz); sz = zpc.GetFileName(); if ((sz.*m_pCompare)(lpszFileName) == 0) { iResult = i; break; } } } else if (bCaseSensitive == m_bCaseSensitive) iResult = FindFileNameIndex(lpszFileName); else { if (bSporadically) { // a non-effective search (treat an array as unsorted) int iSize = m_findarray.GetSize(); for (int i = 0; i < iSize; i++) if (CompareElement(lpszFileName, (WORD)i) == 0) { iResult = i; break; } } else { BuildFindFastArray(bCaseSensitive); iResult = FindFileNameIndex(lpszFileName); } } return iResult == -1 ? -1 : m_findarray[iResult].m_uIndex; }
void CZipCentralDir::InsertFindFastElement(CZipFileHeader* pHeader, WORD uIndex) { CZipString fileName = pHeader->GetFileName(); int iSize = m_findarray.GetSize(); // Our initial binary search range encompasses the entire array of filenames: int start = 0; int end = iSize; // Keep halving our search range until we find the right place // to insert the new element: while ( start < end ) { // Find the midpoint of the search range: int midpoint = ( start + end ) / 2; // Compare the filename with the filename at the midpoint of the current search range: int result = CompareElement(fileName, (WORD)midpoint); // If our filename is larger, it must fall in the first half of the search range: if ( result > 0 ) { end = midpoint; } // If it's smaller, it must fall in the last half: else if ( result < 0 ) { start = midpoint + 1; } // If they're equal, we can go ahead and insert here: else { start = midpoint; break; } } m_findarray.InsertAt(start, CZipFindFast(pHeader, WORD(uIndex == WORD(-1) ? iSize : uIndex /* just in case */))); }
int CZipCentralDir::FindFileNameIndex(LPCTSTR lpszFileName) const { int start = 0; int end = m_findarray.GetUpperBound(); // Keep halving our search range until we find the given element: while ( start <= end ) { // Find the midpoint of the search range: int midpoint = ( start + end ) / 2; // Compare the given filename with the filename at the midpoint of the search range: int result = CompareElement(lpszFileName, (WORD)midpoint); // If our filename is smaller, it must fall in the first half of the search range: if ( result > 0 ) { end = midpoint - 1; } // If it's larger, it must fall in the last half: else if ( result < 0 ) { start = midpoint + 1; } // If they're equal, return the result: else { return midpoint; } } // Signal failure: return -1; }