libsaria: Created a list for_each_item()
Useful for accessing each item in the list sequentially without having to write a lot of loops. Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
parent
311e404af9
commit
8b56659b1c
|
@ -36,6 +36,8 @@ namespace libsaria
|
|||
ListItem<T> *push_back(T);
|
||||
void erase(ListItem<T> *);
|
||||
unsigned int size();
|
||||
|
||||
void for_each_item(void (*)(T &, void *), void *);
|
||||
};
|
||||
|
||||
};
|
||||
|
|
|
@ -47,17 +47,23 @@ libsaria::library::Path *push_path(libsaria::library::Path &path)
|
|||
return ptr;
|
||||
}
|
||||
|
||||
static void list_path(libsaria::library::Path *path,
|
||||
list<libsaria::Track *> &tracks,
|
||||
list<libsaria::Track *> &banned)
|
||||
struct sort_data {
|
||||
list<libsaria::Track *> tracks;
|
||||
list<libsaria::Track *> banned;
|
||||
};
|
||||
|
||||
/*
|
||||
* TODO: Add tracks directly to a playlist without going through
|
||||
* these intermediate lists. (Possibly) use insertion sort
|
||||
* for inserting?
|
||||
*/
|
||||
static void sort_tracks(libsaria::Track &track, void *data)
|
||||
{
|
||||
list<libsaria::Track>::iterator it;
|
||||
for (it = path->tracks.begin(); it != path->tracks.end(); it++) {
|
||||
if (!it->get_banned())
|
||||
tracks.push_back(&(*it));
|
||||
else
|
||||
banned.push_back(&(*it));
|
||||
}
|
||||
struct sort_data *sort = (struct sort_data *)data;
|
||||
if (track.get_banned())
|
||||
sort->banned.push_back(&track);
|
||||
else
|
||||
sort->tracks.push_back(&track);
|
||||
}
|
||||
|
||||
void pop_path(libsaria::library::Path *path)
|
||||
|
@ -123,12 +129,11 @@ namespace libsaria
|
|||
|
||||
void library::hide_path(Path *path)
|
||||
{
|
||||
list<Track *> tracks;
|
||||
list<Track *> banned;
|
||||
struct sort_data sort;
|
||||
|
||||
list_path(path, tracks, banned);
|
||||
lib_playlist.remove_tracks(tracks);
|
||||
ban::get_banned_plist()->remove_tracks(banned);
|
||||
path->_tracks.for_each_item(sort_tracks, &sort);
|
||||
lib_playlist.remove_tracks(sort.tracks);
|
||||
ban::get_banned_plist()->remove_tracks(sort.banned);
|
||||
|
||||
path->visible = false;
|
||||
path->data_state = DIRTY;
|
||||
|
@ -138,12 +143,11 @@ namespace libsaria
|
|||
|
||||
void library::show_path(Path *path)
|
||||
{
|
||||
list<Track *> tracks;
|
||||
list<Track *> banned;
|
||||
struct sort_data sort;
|
||||
|
||||
list_path(path, tracks, banned);
|
||||
lib_playlist.add_tracks(tracks);
|
||||
ban::get_banned_plist()->add_tracks(banned);
|
||||
path->_tracks.for_each_item(sort_tracks, &sort);
|
||||
lib_playlist.add_tracks(sort.tracks);
|
||||
ban::get_banned_plist()->add_tracks(sort.banned);
|
||||
|
||||
path->visible = true;
|
||||
path->data_state = DIRTY;
|
||||
|
|
|
@ -69,6 +69,17 @@ namespace libsaria
|
|||
return count;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void List<T>::for_each_item(void (*func)(T &, void *), void *data)
|
||||
{
|
||||
ListItem<T> *item = head.next;
|
||||
|
||||
while (item != &head) {
|
||||
func(item->value, data);
|
||||
item = item->next;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif /* LIBSARIA_LIST_CPP */
|
||||
|
|
Loading…
Reference in New Issue