Skip to content

Commit

Permalink
Optimize sorting and add sorting to diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenAlex committed Jan 15, 2023
1 parent c0d0406 commit f4a09b8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 32 deletions.
1 change: 0 additions & 1 deletion src/UI/ViewControllers/FilterView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ void ViewControllers::FilterViewController::DidActivate(bool firstActivation, bo

// I hate BSML some times
auto m = modsRequirementDropdown->dropdown->modalView;
modsRequirementDropdown->dropdown->numberOfVisibleCells = get_modOptions()->get_Count();
reinterpret_cast<UnityEngine::RectTransform *>(m->get_transform())->set_pivot(UnityEngine::Vector2(0.5f, 0.3f));


Expand Down
8 changes: 4 additions & 4 deletions src/UI/ViewControllers/SongList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,14 +657,14 @@ void ViewControllers::SongListController::_UpdateSearchedSongsList() {
item.searchWeight = searchWeight + min(searchWeight / 2, item.sortWeight * maxSortWeightInverse * (searchWeight / 2));
}


std::stable_sort(prefiltered.begin(), prefiltered.end(),
[](xd s1, xd s2){
[](const xd& s1, const xd& s2){
return s1.searchWeight > s2.searchWeight;
}
);

for (auto x: prefiltered) {
DataHolder::searchedSongList.reserve(prefiltered.size());
for (auto& x: prefiltered) {
DataHolder::searchedSongList.push_back(x.song);
}
INFO("sorted search results in {} ms", CurrentTimeMs()-before);
Expand Down Expand Up @@ -693,7 +693,7 @@ void ViewControllers::SongListController::_UpdateSearchedSongsList() {
DataHolder::searchedSongList.reserve(DataHolder::filteredSongList.size());

// Push to searched
for (auto x: prefiltered) {
for (auto& x: prefiltered) {
DataHolder::searchedSongList.push_back(x.song);
}

Expand Down
82 changes: 55 additions & 27 deletions src/UI/ViewControllers/SongListCell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

DEFINE_TYPE(BetterSongSearch::UI::ViewControllers, CustomSongListTableCell);

struct DiffIndex {
const SongDetailsCache::SongDifficulty* diff;
bool passesFilter;
};

namespace BetterSongSearch::UI::ViewControllers
{
static std::map<const SongDetailsCache::MapDifficulty, std::string> shortMapDiffNames = {
Expand Down Expand Up @@ -63,33 +68,56 @@ namespace BetterSongSearch::UI::ViewControllers

this->entry = entry;

std::vector<const SongDetailsCache::SongDifficulty*> sortedDiffs;
std::vector<DiffIndex> sortedDiffs;
for (const SongDetailsCache::SongDifficulty &diff: *entry) {
sortedDiffs.push_back(&diff);
sortedDiffs.push_back({
&diff,
DifficultyCheck(&diff, entry)
});
};


// TODO: Actually sort diffs..
// std::stable_sort(sortedDiffs.begin(), sortedDiffs.end(), [entry](const SongDetailsCache::SongDifficulty* a) {
// bool passesFilter = DifficultyCheck(a, entry);
// return (passesFilter? 1:-3) + (a->characteristic == SongDetailsCache::MapCharacteristic::Standard? 1:0);

// return 1;
// });
// if (fcInstance->SongListController->sort == SortMode::Most_Stars) {
// std::stable_sort(sortedDiffs.begin(), sortedDiffs.end(), [entry](const SongDetailsCache::SongDifficulty* a) {
// return -a->stars;
// });
// }
// if (fcInstance->SongListController->sort == SortMode::Least_Stars) {
// std::stable_sort(sortedDiffs.begin(), sortedDiffs.end(), [entry](const SongDetailsCache::SongDifficulty* a) {
// return (a->ranked()) ? a->ranked() : -420;
// });
// }
// if (fcInstance->SongListController->sort == SortMode::Least_Stars) {
// std::stable_sort(sortedDiffs.begin(), sortedDiffs.end(), [entry](const SongDetailsCache::SongDifficulty* a) {
// return (a->ranked()) ? 1 : 0;
// });
// }
std::stable_sort(sortedDiffs.begin(), sortedDiffs.end(), [entry](const DiffIndex& a, const DiffIndex& b) {

auto diff1 = (a.passesFilter? 1:-3) + (a.diff->characteristic == SongDetailsCache::MapCharacteristic::Standard? 1:0);
auto diff2 = (b.passesFilter? 1:-3) + (b.diff->characteristic == SongDetailsCache::MapCharacteristic::Standard? 1:0);

// Descending
return diff1 > diff2;
});

// If most stars
if (DataHolder::currentSort == SortMode::Most_Stars) {
std::stable_sort(sortedDiffs.begin(), sortedDiffs.end(), [entry](const DiffIndex& a, const DiffIndex& b) {

auto diff1 = - a.diff->stars;
auto diff2 = - b.diff->stars;

// Ascending
return diff1 < diff2;
});
}
// If least stars
if (DataHolder::currentSort == SortMode::Least_Stars) {
std::stable_sort(sortedDiffs.begin(), sortedDiffs.end(), [entry](const DiffIndex& a, const DiffIndex& b) {
auto diff1 = a.diff->ranked()? a.diff->stars: -420.0f;
auto diff2 = b.diff->ranked()? b.diff->stars: -420.0f;
// Ascending
return diff1 < diff2;
});
}

// By ranked
std::stable_sort(sortedDiffs.begin(), sortedDiffs.end(), [entry](const DiffIndex& a, const DiffIndex& b) {
auto diff1 = a.diff->ranked()? 1: 0;
auto diff2 = b.diff->ranked()? 1: 0;
// Descending
return diff1 < diff2;
});




int diffsLeft = sortedDiffs.size();

Expand All @@ -105,11 +133,11 @@ namespace BetterSongSearch::UI::ViewControllers
}
else
{
bool passesFilter = DifficultyCheck(sortedDiffs[i], entry);
auto diffname = GetCombinedShortDiffName(entry->diffCount, sortedDiffs[i]);
bool passesFilter = sortedDiffs[i].passesFilter;
auto diffname = GetCombinedShortDiffName(entry->diffCount, sortedDiffs[i].diff);
std::string stars = "";
if (sortedDiffs[i]->stars > 0 && sortedDiffs[i]->characteristic == SongDetailsCache::MapCharacteristic::Standard) {
stars = fmt::format(" <color=#{}>{}", (passesFilter ? "D91" : "650"), fmt::format("{:.{}f}", sortedDiffs[i]->stars, 1));
if (sortedDiffs[i].diff->stars > 0 && sortedDiffs[i].diff->characteristic == SongDetailsCache::MapCharacteristic::Standard) {
stars = fmt::format(" <color=#{}>{}", (passesFilter ? "D91" : "650"), fmt::format("{:.{}f}", sortedDiffs[i].diff->stars, 1));
};

diffs[i]->SetText(
Expand Down

0 comments on commit f4a09b8

Please sign in to comment.