From c532dd1101c453cc03570264bb1e2c296b096435 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sat, 22 Feb 2003 21:25:09 +0000 Subject: [PATCH] optimize SortSongPointerArrayByMostPlayed --- stepmania/src/Song.cpp | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/stepmania/src/Song.cpp b/stepmania/src/Song.cpp index b398a9d771..0b5a55c01a 100644 --- a/stepmania/src/Song.cpp +++ b/stepmania/src/Song.cpp @@ -921,13 +921,14 @@ bool Song::HasEdits( NotesType nt ) const // Sorting ///////////////////////////////////// -int CompareSongPointersByTitle(const Song *pSong1, const Song *pSong2) +bool CompareSongPointersByTitle(const Song *pSong1, const Song *pSong2) { //Prefer transliterations to full titles - CString sTitle1 = pSong1->GetFullTranslitTitle(); - CString sTitle2 = pSong2->GetFullTranslitTitle(); + int ret = pSong1->GetTranslitMainTitle().CompareNoCase(pSong1->GetTranslitMainTitle()); + if(ret < 0) return true; + if(ret > 0) return false; - int ret = sTitle1.CompareNoCase(sTitle2); + ret = pSong1->GetTranslitSubTitle().CompareNoCase(pSong2->GetTranslitSubTitle()); if(ret < 0) return true; if(ret > 0) return false; @@ -1035,21 +1036,45 @@ void SortSongPointerArrayByGroup( vector &arraySongPointers ) sort( arraySongPointers.begin(), arraySongPointers.end(), CompareSongPointersByGroup ); } -int CompareSongPointersByMostPlayed(const Song *pSong1, const Song *pSong2) +bool CompareSongPointersByMostPlayed(const Song *pSong1, const Song *pSong2) { + return pSong1->GetNumTimesPlayed() < pSong2->GetNumTimesPlayed(); +/* +Comparing titles is slow, and this makes course selection choppy. Turning this +off means we don't get consistent orderings of songs that have been played +the same amount, but that shouldn't be a big problem; the songs should stay +in the same original order during a single load of the game anyway (and will +probably stay in the same general order even across loads). The number of +plays is more likely to change. + +This is mostly a problem with new games, where there are a large number of songs +that have all been played 0 times. + int iNumTimesPlayed1 = pSong1->GetNumTimesPlayed(); int iNumTimesPlayed2 = pSong2->GetNumTimesPlayed(); - + if( iNumTimesPlayed1 > iNumTimesPlayed2 ) return true; if( iNumTimesPlayed1 < iNumTimesPlayed2 ) return false; return CompareSongPointersByTitle( pSong1, pSong2 ); +*/ +} +/* Actually, just calculating GetNumTimesPlayed within the sort is pretty + * slow, so let's precompute it. (This could be generalized with a template.) */ +map song_sort_val; + +bool CompareSongPointersBySortVal(const Song *pSong1, const Song *pSong2) +{ + return song_sort_val[pSong1] < song_sort_val[pSong2]; } void SortSongPointerArrayByMostPlayed( vector &arraySongPointers ) { - sort( arraySongPointers.begin(), arraySongPointers.end(), CompareSongPointersByMostPlayed ); + for(unsigned i = 0; i < arraySongPointers.size(); ++i) + song_sort_val[arraySongPointers[i]] = arraySongPointers[i]->GetNumTimesPlayed(); + stable_sort( arraySongPointers.begin(), arraySongPointers.end(), CompareSongPointersBySortVal ); + song_sort_val.clear(); } bool Song::NormallyDisplayed() const