playqueue: Sorting improvements

- Put empty tags last
- Correct and edge case in my binary search

Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
Anna Schumaker 2014-01-27 10:23:22 -05:00 committed by Anna Schumaker
parent 2e67a645c8
commit 23a518cf5a
1 changed files with 29 additions and 7 deletions

View File

@ -112,6 +112,21 @@ static inline int compare_uint(unsigned int a, unsigned int b)
return 1;
}
/*
* Returns:
* 0: lhs == rhs
* < 0: lhs < rhs, or rhs is empty
* > 0: lhs > rhs, or lhs is empty
*/
static inline int compare_string(const std::string &a, const std::string &b)
{
if (a.size() == 0)
return 1;
else if (b.size() == 0)
return -1;
return a.compare(b);
}
/*
* std::string.compare() returns
* 0: Strings are equal
@ -123,19 +138,19 @@ static inline int track_compare(library :: Song &lhs, library :: Song &rhs,
{
switch (field) {
case SORT_ARTIST:
return lhs.artist->key_lower.compare(rhs.artist->key_lower);
return compare_string(lhs.artist->key_lower, rhs.artist->key_lower);
case SORT_ALBUM:
return lhs.album->name_lower.compare(rhs.album->name_lower);
return compare_string(lhs.album->name_lower, rhs.album->name_lower);
case SORT_COUNT:
return compare_uint(lhs.track->track, rhs.track->track);
case SORT_GENRE:
return lhs.genre->key_lower.compare(rhs.genre->key_lower);
return compare_string(lhs.genre->key_lower, rhs.genre->key_lower);
case SORT_LENGTH:
return compare_uint(lhs.track->length, rhs.track->length);
case SORT_PLAYED:
return compare_uint(lhs.track->play_count, rhs.track->play_count);
case SORT_TITLE:
return lhs.track->title_lower.compare(rhs.track->title_lower);
return compare_string(lhs.track->title_lower, rhs.track->title_lower);
case SORT_TRACK:
return compare_uint(lhs.track->track, rhs.track->track);
default: //case SORT_YEAR
@ -170,17 +185,24 @@ unsigned int Playqueue :: add_sorted(unsigned int track_id, library :: Song &rhs
return 0;
}
while (start != end) {
while (end - start > 1) {
cur = start + ((end - start) / 2);
library :: lookup(tracks[cur], &lhs);
if (track_less_than(lhs, rhs, sort_order)) {
if (end - start == 1)
cur = end;
//if (end - start == 1)
// cur = end;
start = cur;
} else
end = cur;
}
if (end - start == 1) {
cur = end;
library :: lookup(tracks[cur], &lhs);
if (track_less_than(lhs, rhs, sort_order))
cur++;
}
tracks.insert(tracks.begin() + cur, track_id);
return cur;
}