playqueue: Improve insertion sort with a binary search
My test was done with a library size of 8040. Before this patch, I make 21919628 calls to my "less_than" function. With this patch, I now make 101205 calls to my "less than" function. This comes out to a 216-X improvement. I should have done this sooner. Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
parent
e256c24b20
commit
ddbbf0ea49
|
@ -118,16 +118,27 @@ static bool track_less_than(library :: Song &lhs, library :: Song &rhs)
|
|||
|
||||
unsigned int Playqueue :: add_sorted(unsigned int track_id, library :: Song &rhs)
|
||||
{
|
||||
unsigned int id;
|
||||
library :: Song lhs;
|
||||
unsigned int cur, start = 0, end = (tracks.size() - 1);
|
||||
|
||||
for (id = 0; id != tracks.size(); id++) {
|
||||
library :: lookup(tracks[id], &lhs);
|
||||
if (!track_less_than(lhs, rhs))
|
||||
break;
|
||||
if (tracks.size() == 0) {
|
||||
tracks.push_back(track_id);
|
||||
return 0;
|
||||
}
|
||||
tracks.insert(tracks.begin() + id, track_id);
|
||||
return id;
|
||||
|
||||
while (start != end) {
|
||||
cur = start + ((end - start) / 2);
|
||||
library :: lookup(tracks[cur], &lhs);
|
||||
if (track_less_than(lhs, rhs)) {
|
||||
if (end - start == 1)
|
||||
cur = end;
|
||||
start = cur;
|
||||
} else
|
||||
end = cur;
|
||||
}
|
||||
|
||||
tracks.insert(tracks.begin() + cur, track_id);
|
||||
return cur;
|
||||
}
|
||||
|
||||
unsigned int Playqueue :: add(unsigned int track_id)
|
||||
|
|
Loading…
Reference in New Issue