optimized UnlockSystem::FindSong() function so its a O(log n) operation instead of O(n)
(improved from sequential to binary search; still may have bugs so old code is commented out)
This commit is contained in:
@@ -50,12 +50,10 @@ bool UnlockSystem::SongIsLocked( const Song *song )
|
|||||||
p->updateLocked();
|
p->updateLocked();
|
||||||
|
|
||||||
if (!p->isLocked) tmp = "un";
|
if (!p->isLocked) tmp = "un";
|
||||||
|
|
||||||
LOG->Trace( "UnlockSystem entry: %s", (p->m_sSongName).c_str() );
|
|
||||||
LOG->Trace( "current status: %slocked", tmp.c_str() );
|
LOG->Trace( "current status: %slocked", tmp.c_str() );
|
||||||
}
|
}
|
||||||
|
|
||||||
return p && p->isLocked;
|
return (p != NULL) && (p->isLocked);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool UnlockSystem::SongIsRoulette( const Song *song )
|
bool UnlockSystem::SongIsRoulette( const Song *song )
|
||||||
@@ -74,9 +72,36 @@ bool UnlockSystem::SongIsRoulette( const Song *song )
|
|||||||
|
|
||||||
SongEntry *UnlockSystem::FindSong( const Song *pSong )
|
SongEntry *UnlockSystem::FindSong( const Song *pSong )
|
||||||
{
|
{
|
||||||
|
int left = 0;
|
||||||
|
int right = m_SongEntries.size() - 1;
|
||||||
|
CString songtitle = pSong->GetFullTranslitTitle();
|
||||||
|
|
||||||
|
songtitle.MakeUpper();
|
||||||
|
|
||||||
|
while (left != right)
|
||||||
|
{
|
||||||
|
int mid = (left + right)/2;
|
||||||
|
|
||||||
|
if (songtitle <= m_SongEntries[mid].m_sSongName )
|
||||||
|
right = mid;
|
||||||
|
else
|
||||||
|
left = mid + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (m_SongEntries[left].GetSong() == pSong)
|
||||||
|
{
|
||||||
|
LOG->Trace("UnlockSystem: Retrieved: %s", pSong->GetFullTranslitTitle().c_str());
|
||||||
|
return &m_SongEntries[left];
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
for(unsigned i = 0; i < m_SongEntries.size(); i++)
|
for(unsigned i = 0; i < m_SongEntries.size(); i++)
|
||||||
if (m_SongEntries[i].GetSong() == pSong )
|
if (m_SongEntries[i].GetSong() == pSong )
|
||||||
return &m_SongEntries[i];
|
return &m_SongEntries[i];
|
||||||
|
*/
|
||||||
|
LOG->Trace("UnlockSystem: Failed to find %s", pSong->GetFullTranslitTitle().c_str());
|
||||||
|
LOG->Trace(" (landed on %s)", m_SongEntries[left].m_sSongName.c_str() );
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -327,3 +352,15 @@ Song *SongEntry::GetSong() const
|
|||||||
{
|
{
|
||||||
return SONGMAN->FindSong( "", m_sSongName );
|
return SONGMAN->FindSong( "", m_sSongName );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UnlockSystem::DebugPrint()
|
||||||
|
{
|
||||||
|
for(unsigned i=0; i < m_SongEntries.size(); i++)
|
||||||
|
{
|
||||||
|
CString tmp = " ";
|
||||||
|
if (!m_SongEntries[i].isLocked) tmp = "un";
|
||||||
|
LOG->Trace( "UnlockSystem entry %d: %s",i, m_SongEntries[i].m_sSongName.c_str() );
|
||||||
|
LOG->Trace( "Status:%slocked", tmp.c_str() );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -69,6 +69,8 @@ public:
|
|||||||
float UnlockToasty();
|
float UnlockToasty();
|
||||||
bool RouletteUnlock( const Song *song );
|
bool RouletteUnlock( const Song *song );
|
||||||
|
|
||||||
|
void DebugPrint();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void SortSongEntriesArray(); // sorts unlocks
|
void SortSongEntriesArray(); // sorts unlocks
|
||||||
bool ParseRow(CString text, CString &type, float &qty, CString &songname);
|
bool ParseRow(CString text, CString &type, float &qty, CString &songname);
|
||||||
|
|||||||
Reference in New Issue
Block a user