playqueue: Binary seach edge cases

Because some things were still out of order.

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

View File

@ -154,7 +154,10 @@ static inline int track_compare(library :: Song &lhs, library :: Song &rhs,
case SORT_TRACK:
return compare_uint(lhs.track->track, rhs.track->track);
default: //case SORT_YEAR
return compare_uint(lhs.album->year, rhs.album->year);
int ret = compare_uint(lhs.album->year, rhs.album->year);
if (ret != 0)
return ret;
return compare_string(lhs.album->name_lower, rhs.album->name_lower);
}
}
@ -183,24 +186,31 @@ unsigned int Playqueue :: add_sorted(unsigned int track_id, library :: Song &rhs
if (tracks.size() == 0) {
tracks.push_back(track_id);
return 0;
} else if (tracks.size() == 1) {
library :: lookup(tracks[0], &lhs);
if (track_less_than(lhs, rhs, sort_order)) {
tracks.push_back(track_id);
return 1;
}
tracks.insert(tracks.begin(), track_id);
return 0;
}
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 (track_less_than(lhs, rhs, sort_order))
start = cur;
} else
else
end = cur;
}
if (end - start == 1) {
cur = end;
library :: lookup(tracks[cur], &lhs);
if (track_less_than(lhs, rhs, sort_order))
cur++;
if (end != start) {
for (cur = start; cur <= end; cur++) {
library :: lookup(tracks[cur], &lhs);
if (!track_less_than(lhs, rhs, sort_order))
break;
}
}
tracks.insert(tracks.begin() + cur, track_id);