libsaria: Clean up picking the next song from a playlist

I don't need to iterate through a linked list to find the track anymore.
Instead, I can access tracks directly using an index.

Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
Bryan Schumaker 2012-11-03 15:36:09 -04:00
parent cf8ded7d5d
commit d9e343895c
4 changed files with 15 additions and 73 deletions

View File

@ -41,9 +41,6 @@ namespace libsaria
void notify_ui(notify_t, Track *, unsigned int);
void notify_update_all();
void incr_iter();
Track *picked_next();
unsigned int find_cur_index();
unsigned int find_index(Track *);
void add_track(Track *, unsigned int);
void insert_sorted(Track *);
@ -53,9 +50,6 @@ namespace libsaria
protected:
vector<Track *> plist;
void pick_random();
void pick_sequential();
void add_sorted(list<Track *> &);
public:

View File

@ -32,7 +32,6 @@ namespace libsaria
unsigned int rm_index)
{
Track *track = *it;
bool removing_cur = (it == plist.begin() + cur);
length -= (*track)[TRACK_LENGTH];
index.remove_track(*it);
(*it)->rm_playlist(this);
@ -44,8 +43,6 @@ namespace libsaria
it--;
} else
it = plist.begin();
if (removing_cur)
cur = it - plist.begin();
data_state = DIRTY;
return rm_index;
}

View File

@ -9,71 +9,27 @@
namespace libsaria
{
void Playlist::incr_iter()
{
cur++;
if (cur == plist.size())
cur = 0;
}
Track *Playlist::picked_next()
{
Track *res = plist[cur];
unsigned int index;
index = find_cur_index();
if (flags & PL_NO_DRAIN) {
notify_ui(PLAYLIST_GOTO, res, index);
return res;
}
index = remove_track_it(plist.begin() + cur, index);
schedule_save();
if (get_size() != 0)
notify_ui(PLAYLIST_GOTO, res, index);
return res;
}
void Playlist::pick_sequential()
{
if (flags & PL_NO_DRAIN)
incr_iter();
}
void Playlist::pick_random()
{
unsigned int n, range = get_size();
unsigned int min = 1;
if (range == 1) {
cur = 0;
return;
}
if (range > 10) {
min = (range / 10) + (range % 5);
range = (range / 2);
} else
range *= 2;
n = min + (rand() % range);
println("min: %u, max: %u, n: %u", min, min + range, n);
for (unsigned int i = 0; i < n; i++)
incr_iter();
}
Track *Playlist::next()
{
if (get_size() == 0)
return NULL;
if (get_random())
pick_random();
else
pick_sequential();
return picked_next();
cur += rand() % get_size();
else if (flags & PL_NO_DRAIN)
cur++;
if (cur >= plist.size())
cur -= plist.size();
if (!(flags & PL_NO_DRAIN)) {
remove_track_it(plist.begin() + cur, cur);
schedule_save();
}
if (get_size() != 0)
notify_ui(PLAYLIST_GOTO, plist[cur], cur);
return plist[cur];
}
}; /* Namespace: libsaria */

View File

@ -126,11 +126,6 @@ namespace libsaria
notify_ui(PLAYLIST_GOTO, plist[cur], 0);
}
unsigned int Playlist::find_cur_index()
{
return cur;
}
unsigned int Playlist::find_index(Track *track)
{
vector<Track *>::iterator it;