playlist: move sorting functions to Playlist::EnterPressed
This commit is contained in:
@@ -187,9 +187,39 @@ void Playlist::EnterPressed()
|
|||||||
for (size_t i = beginning; i < end; ++i)
|
for (size_t i = beginning; i < end; ++i)
|
||||||
playlist.push_back(&(*Items)[i]);
|
playlist.push_back(&(*Items)[i]);
|
||||||
|
|
||||||
|
std::function<void(MPD::SongList::iterator, MPD::SongList::iterator)> iter_swap, quick_sort;
|
||||||
|
auto song_cmp = [](MPD::Song *a, MPD::Song *b) {
|
||||||
|
CaseInsensitiveStringComparison cmp;
|
||||||
|
for (size_t i = 0; i < SortOptions; ++i)
|
||||||
|
if (int ret = cmp(a->GetTags((*SortDialog)[i].second), b->GetTags((*SortDialog)[i].second)))
|
||||||
|
return ret < 0;
|
||||||
|
return a->GetPosition() < b->GetPosition();
|
||||||
|
};
|
||||||
|
iter_swap = [&playlist](MPD::SongList::iterator a, MPD::SongList::iterator b) {
|
||||||
|
std::iter_swap(a, b);
|
||||||
|
Mpd.Swap(a-playlist.begin(), b-playlist.begin());
|
||||||
|
};
|
||||||
|
quick_sort = [this, &song_cmp, &quick_sort, &iter_swap](MPD::SongList::iterator first, MPD::SongList::iterator last) {
|
||||||
|
if (last-first > 1)
|
||||||
|
{
|
||||||
|
MPD::SongList::iterator pivot = first+rand()%(last-first);
|
||||||
|
iter_swap(pivot, last-1);
|
||||||
|
pivot = last-1;
|
||||||
|
|
||||||
|
MPD::SongList::iterator tmp = first;
|
||||||
|
for (MPD::SongList::iterator i = first; i != pivot; ++i)
|
||||||
|
if (song_cmp(*i, *pivot))
|
||||||
|
iter_swap(i, tmp++);
|
||||||
|
iter_swap(tmp, pivot);
|
||||||
|
|
||||||
|
quick_sort(first, tmp);
|
||||||
|
quick_sort(tmp+1, last);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
ShowMessage("Sorting...");
|
ShowMessage("Sorting...");
|
||||||
Mpd.StartCommandsList();
|
Mpd.StartCommandsList();
|
||||||
QuickSort(playlist.begin(), playlist.end(), playlist.begin());
|
quick_sort(playlist.begin(), playlist.end());
|
||||||
if (Mpd.CommitCommandsList())
|
if (Mpd.CommitCommandsList())
|
||||||
ShowMessage("Playlist sorted");
|
ShowMessage("Playlist sorted");
|
||||||
w = Items;
|
w = Items;
|
||||||
@@ -414,25 +444,6 @@ void Playlist::EnableHighlighting()
|
|||||||
UpdateTimer();
|
UpdateTimer();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Playlist::QuickSort(MPD::SongList::iterator first, MPD::SongList::iterator last, MPD::SongList::iterator begin)
|
|
||||||
{
|
|
||||||
if (last-first > 1)
|
|
||||||
{
|
|
||||||
MPD::SongList::iterator pivot = first+rand()%(last-first);
|
|
||||||
IterSwap(pivot, last-1, begin);
|
|
||||||
pivot = last-1;
|
|
||||||
|
|
||||||
MPD::SongList::iterator tmp = first;
|
|
||||||
for (MPD::SongList::iterator i = first; i != pivot; ++i)
|
|
||||||
if (SongComp(*i, *pivot))
|
|
||||||
IterSwap(i, tmp++, begin);
|
|
||||||
IterSwap(tmp, pivot, begin);
|
|
||||||
|
|
||||||
QuickSort(first, tmp, begin);
|
|
||||||
QuickSort(tmp+1, last, begin);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string Playlist::TotalLength()
|
std::string Playlist::TotalLength()
|
||||||
{
|
{
|
||||||
std::ostringstream result;
|
std::ostringstream result;
|
||||||
|
|||||||
@@ -104,22 +104,6 @@ class Playlist : public Screen<Window>
|
|||||||
|
|
||||||
time_t itsTimer;
|
time_t itsTimer;
|
||||||
|
|
||||||
// stuff for sorting playlist
|
|
||||||
static void QuickSort(MPD::SongList::iterator first, MPD::SongList::iterator last, MPD::SongList::iterator begin);
|
|
||||||
inline static void IterSwap(MPD::SongList::iterator a, MPD::SongList::iterator b, MPD::SongList::iterator begin)
|
|
||||||
{
|
|
||||||
iter_swap(a, b);
|
|
||||||
Mpd.Swap(a-begin, b-begin);
|
|
||||||
}
|
|
||||||
inline static bool SongComp(MPD::Song *a, MPD::Song *b)
|
|
||||||
{
|
|
||||||
CaseInsensitiveStringComparison cmp;
|
|
||||||
for (size_t i = 0; i < SortOptions; ++i)
|
|
||||||
if (int ret = cmp(a->GetTags((*SortDialog)[i].second), b->GetTags((*SortDialog)[i].second)))
|
|
||||||
return ret < 0;
|
|
||||||
return a->GetPosition() < b->GetPosition();
|
|
||||||
}
|
|
||||||
|
|
||||||
static Menu< std::pair<std::string, MPD::Song::GetFunction> > *SortDialog;
|
static Menu< std::pair<std::string, MPD::Song::GetFunction> > *SortDialog;
|
||||||
static const size_t SortOptions;
|
static const size_t SortOptions;
|
||||||
static const size_t SortDialogWidth;
|
static const size_t SortDialogWidth;
|
||||||
|
|||||||
Reference in New Issue
Block a user