libsaria: Convert Playlist class to use a vector
Vectors are more straightforward than linked lists and they should allow me to clean up the code a lot. For now I just put in the straight conversion, I'll clean things up in future patches. Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
parent
c67222ab95
commit
b98373c0fa
|
@ -8,6 +8,7 @@
|
|||
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
enum PlaylistFlags {
|
||||
|
@ -31,7 +32,7 @@ namespace libsaria
|
|||
unsigned int flags;
|
||||
unsigned int length;
|
||||
DataState data_state;
|
||||
list<Track *>::iterator cur;
|
||||
unsigned int cur;
|
||||
Index index;
|
||||
|
||||
void set_flag(bool, PlaylistFlags);
|
||||
|
@ -47,12 +48,12 @@ namespace libsaria
|
|||
void add_track(Track *, unsigned int);
|
||||
void insert_sorted(Track *);
|
||||
void do_remove_tracks(list<Track *> &);
|
||||
unsigned int remove_track_it(list<Track *>::iterator &it, unsigned int);
|
||||
unsigned int remove_track_it(vector<Track *>::iterator it, unsigned int);
|
||||
void do_sort();
|
||||
|
||||
protected:
|
||||
string name;
|
||||
list<Track *> plist;
|
||||
vector<Track *> plist;
|
||||
|
||||
void pick_random();
|
||||
void pick_sequential();
|
||||
|
|
|
@ -3,6 +3,9 @@
|
|||
#include <track.h>
|
||||
#include <print.h>
|
||||
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
static bool compare_tracks(libsaria::Track *one, libsaria::Track *two)
|
||||
{
|
||||
/* I want to compare tracks and not pointers */
|
||||
|
@ -22,14 +25,14 @@ namespace libsaria
|
|||
data_state = DIRTY;
|
||||
schedule_save();
|
||||
if (plist.size() == 1)
|
||||
cur = plist.begin();
|
||||
cur = 0;
|
||||
}
|
||||
|
||||
unsigned int Playlist::remove_track_it(list<Track *>::iterator &it,
|
||||
unsigned int Playlist::remove_track_it(vector<Track *>::iterator it,
|
||||
unsigned int rm_index)
|
||||
{
|
||||
Track *track = *it;
|
||||
bool removing_cur = (it == cur);
|
||||
bool removing_cur = (it == plist.begin() + cur);
|
||||
length -= (*track)[TRACK_LENGTH];
|
||||
index.remove_track(*it);
|
||||
(*it)->rm_playlist(this);
|
||||
|
@ -42,14 +45,14 @@ namespace libsaria
|
|||
} else
|
||||
it = plist.begin();
|
||||
if (removing_cur)
|
||||
cur = it;
|
||||
cur = it - plist.begin();
|
||||
data_state = DIRTY;
|
||||
return rm_index;
|
||||
}
|
||||
|
||||
void Playlist::remove_track(Track *track)
|
||||
{
|
||||
list<Track *>::iterator it;
|
||||
vector<Track *>::iterator it;
|
||||
unsigned int index = 0;
|
||||
|
||||
for (it = plist.begin(); it != plist.end(); it++) {
|
||||
|
@ -65,7 +68,7 @@ namespace libsaria
|
|||
|
||||
void Playlist::remove_indices(list<unsigned int> &indices)
|
||||
{
|
||||
list<Track *>::iterator to_rm, tmp;
|
||||
vector<Track *>::iterator to_rm, tmp;
|
||||
list<unsigned int>::iterator it;
|
||||
unsigned int index, removed;
|
||||
|
||||
|
@ -92,33 +95,29 @@ namespace libsaria
|
|||
|
||||
void Playlist::add_sorted(list<Track *> &tracks)
|
||||
{
|
||||
list<Track *> copy;
|
||||
list<Track *>::iterator it, cur_it;
|
||||
unsigned int ins_index;
|
||||
list<Track *>::iterator it;
|
||||
|
||||
tracks.sort(compare_tracks);
|
||||
copy = tracks;
|
||||
plist.merge(copy, compare_tracks);
|
||||
plist.insert(plist.end(), tracks.begin(), tracks.end());
|
||||
do_sort();
|
||||
|
||||
ins_index = 0;
|
||||
it = tracks.begin();
|
||||
for (cur_it = plist.begin(); cur_it != plist.end(); cur_it++) {
|
||||
if ((*cur_it) == (*it)) {
|
||||
add_track(*it, ins_index);
|
||||
for (unsigned i = 0; i < plist.size(); i++) {
|
||||
if (plist[i] == *it) {
|
||||
add_track(*it, i);
|
||||
if (it++ == tracks.end())
|
||||
break;
|
||||
};
|
||||
ins_index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Playlist::push_front(Track *track)
|
||||
{
|
||||
unsigned int index = 0;
|
||||
plist.push_front(track);
|
||||
plist.insert(plist.begin(), track);
|
||||
|
||||
if (get_sorted()) {
|
||||
plist.sort(compare_tracks);
|
||||
do_sort();
|
||||
index = find_index(track);
|
||||
}
|
||||
|
||||
|
@ -130,7 +129,7 @@ namespace libsaria
|
|||
unsigned int index = plist.size() + 1;
|
||||
plist.push_back(track);
|
||||
if (get_sorted()) {
|
||||
plist.sort(compare_tracks);
|
||||
do_sort();
|
||||
index = find_index(track);
|
||||
}
|
||||
add_track(track, index);
|
||||
|
@ -146,13 +145,13 @@ namespace libsaria
|
|||
for (it = tracks.begin(); it != tracks.end(); it++)
|
||||
push_back(*it);
|
||||
if (plist.size() == tracks.size())
|
||||
cur = plist.begin();
|
||||
cur = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void Playlist::do_sort()
|
||||
{
|
||||
plist.sort(compare_tracks);
|
||||
sort(plist.begin(), plist.end(), compare_tracks);
|
||||
}
|
||||
|
||||
}; /* namespace: libsaria */
|
||||
|
|
|
@ -77,7 +77,7 @@ namespace libsaria
|
|||
|
||||
void Playlist::save(ofstream &stream)
|
||||
{
|
||||
list<Track *>::iterator it;
|
||||
vector<Track *>::iterator it;
|
||||
|
||||
stream << PLAYLIST_CURRENT_VERSION << "\n";
|
||||
stream << plist.size() << " ";
|
||||
|
|
|
@ -12,13 +12,13 @@ namespace libsaria
|
|||
void Playlist::incr_iter()
|
||||
{
|
||||
cur++;
|
||||
if (cur == plist.end())
|
||||
cur = plist.begin();
|
||||
if (cur == plist.size())
|
||||
cur = 0;
|
||||
}
|
||||
|
||||
Track *Playlist::picked_next()
|
||||
{
|
||||
Track *res = (*cur);
|
||||
Track *res = plist[cur];
|
||||
unsigned int index;
|
||||
|
||||
index = find_cur_index();
|
||||
|
@ -28,7 +28,7 @@ namespace libsaria
|
|||
return res;
|
||||
}
|
||||
|
||||
index = remove_track_it(cur, index);
|
||||
index = remove_track_it(plist.begin() + cur, index);
|
||||
schedule_save();
|
||||
if (get_size() != 0)
|
||||
notify_ui(PLAYLIST_GOTO, res, index);
|
||||
|
@ -48,7 +48,7 @@ namespace libsaria
|
|||
unsigned int min = 1;
|
||||
|
||||
if (range == 1) {
|
||||
cur = plist.begin();
|
||||
cur = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ namespace libsaria
|
|||
number = -1;
|
||||
flags = f;
|
||||
length = 0;
|
||||
cur = plist.begin();
|
||||
cur = 0;
|
||||
}
|
||||
|
||||
Playlist::~Playlist()
|
||||
|
@ -76,7 +76,7 @@ namespace libsaria
|
|||
|
||||
void Playlist::prepare_for_removal()
|
||||
{
|
||||
list<libsaria::Track *>::iterator it;
|
||||
vector<libsaria::Track *>::iterator it;
|
||||
|
||||
/* Remove playlist from tracks */
|
||||
for (it = plist.begin(); it != plist.end(); it++)
|
||||
|
@ -128,23 +128,18 @@ namespace libsaria
|
|||
|
||||
void Playlist::reset_iterator()
|
||||
{
|
||||
cur = plist.begin();
|
||||
notify_ui(PLAYLIST_GOTO, *cur, 0);
|
||||
cur = 0;
|
||||
notify_ui(PLAYLIST_GOTO, plist[cur], 0);
|
||||
}
|
||||
|
||||
unsigned int Playlist::find_cur_index()
|
||||
{
|
||||
list<Track *>::iterator it;
|
||||
unsigned int index = 0;
|
||||
|
||||
for (it = plist.begin(); it != cur; it++)
|
||||
index++;
|
||||
return index;
|
||||
return cur;
|
||||
}
|
||||
|
||||
unsigned int Playlist::find_index(Track *track)
|
||||
{
|
||||
list<Track *>::iterator it;
|
||||
vector<Track *>::iterator it;
|
||||
unsigned int index = 0;
|
||||
|
||||
for (it = plist.begin(); it != plist.end(); it++) {
|
||||
|
@ -166,7 +161,7 @@ namespace libsaria
|
|||
|
||||
void Playlist::notify_update_all()
|
||||
{
|
||||
list<Track *>::iterator it;
|
||||
vector<Track *>::iterator it;
|
||||
unsigned int index = 0;
|
||||
|
||||
for (it = plist.begin(); it != plist.end(); it++) {
|
||||
|
|
Loading…
Reference in New Issue