playlist: Implement next()
Random and sequential next, for both locked and unlocked playlists. Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
parent
168f08392d
commit
0c01751946
|
@ -70,6 +70,8 @@ Playlist: (lib/playlist.cpp)
|
||||||
Sort a playlist in Artist -> Year -> Track number order.
|
Sort a playlist in Artist -> Year -> Track number order.
|
||||||
|
|
||||||
unsigned int next();
|
unsigned int next();
|
||||||
|
Return the next track_id to play.
|
||||||
|
|
||||||
if (flags & PL_RANDOM):
|
if (flags & PL_RANDOM):
|
||||||
cur += rand() % tracks.size();
|
cur += rand() % tracks.size();
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -17,6 +17,7 @@ class Playlist {
|
||||||
private:
|
private:
|
||||||
std :: vector <unsigned int> tracks;
|
std :: vector <unsigned int> tracks;
|
||||||
unsigned int flags;
|
unsigned int flags;
|
||||||
|
unsigned int cur;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Playlist(playlist_flags);
|
Playlist(playlist_flags);
|
||||||
|
@ -31,6 +32,8 @@ public:
|
||||||
unsigned int add(unsigned int);
|
unsigned int add(unsigned int);
|
||||||
void del(unsigned int);
|
void del(unsigned int);
|
||||||
unsigned int size();
|
unsigned int size();
|
||||||
|
|
||||||
|
unsigned int next();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* OCARINA_PLAYLIST_H */
|
#endif /* OCARINA_PLAYLIST_H */
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2013 (c) Anna Schumaker.
|
* Copyright 2013 (c) Anna Schumaker.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <playlist.h>
|
#include <playlist.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
Playlist :: Playlist(playlist_flags f)
|
Playlist :: Playlist(playlist_flags f)
|
||||||
: flags(f)
|
: flags(f), cur(-1)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,3 +59,25 @@ unsigned int Playlist :: size()
|
||||||
{
|
{
|
||||||
return tracks.size();
|
return tracks.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned int Playlist :: next()
|
||||||
|
{
|
||||||
|
unsigned int res;
|
||||||
|
|
||||||
|
if (tracks.size() == 1)
|
||||||
|
cur = 0;
|
||||||
|
else if (flags & PL_RANDOM)
|
||||||
|
cur += rand() % (tracks.size() / 2) + 1;
|
||||||
|
else
|
||||||
|
cur++;
|
||||||
|
|
||||||
|
if (cur >= tracks.size())
|
||||||
|
cur -= tracks.size();
|
||||||
|
|
||||||
|
res = tracks[cur];
|
||||||
|
if (!(flags & PL_LOCKED)) {
|
||||||
|
tracks.erase(tracks.begin() + cur);
|
||||||
|
cur--;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include <playlist.h>
|
#include <playlist.h>
|
||||||
#include <print.h>
|
#include <print.h>
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
void test_flags(const std :: string &test, Playlist &plist, unsigned int expected)
|
void test_flags(const std :: string &test, Playlist &plist, unsigned int expected)
|
||||||
|
@ -102,13 +103,62 @@ void test_2()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
print("SUCCESS\n");
|
print("SUCCESS\n\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Sequential next() without removing tracks */
|
||||||
|
void test_3()
|
||||||
|
{
|
||||||
|
Playlist plist(PL_ENABLED);
|
||||||
|
plist.set_flag(PL_LOCKED);
|
||||||
|
test_add_tracks("3", plist, 0, 15);
|
||||||
|
for (unsigned int i = 0; i < 20; i++)
|
||||||
|
print("Selecting id: %u\n", plist.next());
|
||||||
|
print("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Sequential next() with removal */
|
||||||
|
void test_4()
|
||||||
|
{
|
||||||
|
Playlist plist(PL_ENABLED);
|
||||||
|
test_add_tracks("4", plist, 0, 15);
|
||||||
|
while (plist.size() > 0)
|
||||||
|
print("Selecting id: %u\n", plist.next());
|
||||||
|
print("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Random next() without removing tracks */
|
||||||
|
void test_5()
|
||||||
|
{
|
||||||
|
Playlist plist(PL_ENABLED);
|
||||||
|
plist.set_flag(PL_RANDOM);
|
||||||
|
plist.set_flag(PL_LOCKED);
|
||||||
|
test_add_tracks("5", plist, 0, 15);
|
||||||
|
for (unsigned int i = 0; i < 30; i++)
|
||||||
|
print("Selecting id: %u\n", plist.next());
|
||||||
|
print("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Random next() with removal */
|
||||||
|
void test_6()
|
||||||
|
{
|
||||||
|
Playlist plist(PL_ENABLED);
|
||||||
|
plist.set_flag(PL_RANDOM);
|
||||||
|
test_add_tracks("6", plist, 0, 15);
|
||||||
|
while (plist.size() > 0)
|
||||||
|
print("Selecting id: %u\n", plist.next());
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
srand(42);
|
||||||
|
|
||||||
test_0();
|
test_0();
|
||||||
test_1();
|
test_1();
|
||||||
test_2();
|
test_2();
|
||||||
|
test_3();
|
||||||
|
test_4();
|
||||||
|
test_5();
|
||||||
|
test_6();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,3 +10,93 @@ Test 1c: SUCCESS
|
||||||
|
|
||||||
Test 2a: SUCCESS
|
Test 2a: SUCCESS
|
||||||
Test 2b: SUCCESS
|
Test 2b: SUCCESS
|
||||||
|
|
||||||
|
Test 3: SUCCESS
|
||||||
|
Selecting id: 0
|
||||||
|
Selecting id: 1
|
||||||
|
Selecting id: 2
|
||||||
|
Selecting id: 3
|
||||||
|
Selecting id: 4
|
||||||
|
Selecting id: 5
|
||||||
|
Selecting id: 6
|
||||||
|
Selecting id: 7
|
||||||
|
Selecting id: 8
|
||||||
|
Selecting id: 9
|
||||||
|
Selecting id: 10
|
||||||
|
Selecting id: 11
|
||||||
|
Selecting id: 12
|
||||||
|
Selecting id: 13
|
||||||
|
Selecting id: 14
|
||||||
|
Selecting id: 15
|
||||||
|
Selecting id: 0
|
||||||
|
Selecting id: 1
|
||||||
|
Selecting id: 2
|
||||||
|
Selecting id: 3
|
||||||
|
|
||||||
|
Test 4: SUCCESS
|
||||||
|
Selecting id: 0
|
||||||
|
Selecting id: 1
|
||||||
|
Selecting id: 2
|
||||||
|
Selecting id: 3
|
||||||
|
Selecting id: 4
|
||||||
|
Selecting id: 5
|
||||||
|
Selecting id: 6
|
||||||
|
Selecting id: 7
|
||||||
|
Selecting id: 8
|
||||||
|
Selecting id: 9
|
||||||
|
Selecting id: 10
|
||||||
|
Selecting id: 11
|
||||||
|
Selecting id: 12
|
||||||
|
Selecting id: 13
|
||||||
|
Selecting id: 14
|
||||||
|
Selecting id: 15
|
||||||
|
|
||||||
|
Test 5: SUCCESS
|
||||||
|
Selecting id: 6
|
||||||
|
Selecting id: 11
|
||||||
|
Selecting id: 13
|
||||||
|
Selecting id: 15
|
||||||
|
Selecting id: 4
|
||||||
|
Selecting id: 11
|
||||||
|
Selecting id: 1
|
||||||
|
Selecting id: 6
|
||||||
|
Selecting id: 14
|
||||||
|
Selecting id: 6
|
||||||
|
Selecting id: 9
|
||||||
|
Selecting id: 1
|
||||||
|
Selecting id: 7
|
||||||
|
Selecting id: 8
|
||||||
|
Selecting id: 9
|
||||||
|
Selecting id: 12
|
||||||
|
Selecting id: 1
|
||||||
|
Selecting id: 2
|
||||||
|
Selecting id: 5
|
||||||
|
Selecting id: 9
|
||||||
|
Selecting id: 13
|
||||||
|
Selecting id: 14
|
||||||
|
Selecting id: 0
|
||||||
|
Selecting id: 2
|
||||||
|
Selecting id: 10
|
||||||
|
Selecting id: 14
|
||||||
|
Selecting id: 0
|
||||||
|
Selecting id: 1
|
||||||
|
Selecting id: 7
|
||||||
|
Selecting id: 8
|
||||||
|
|
||||||
|
Test 6: SUCCESS
|
||||||
|
Selecting id: 1
|
||||||
|
Selecting id: 2
|
||||||
|
Selecting id: 4
|
||||||
|
Selecting id: 9
|
||||||
|
Selecting id: 10
|
||||||
|
Selecting id: 11
|
||||||
|
Selecting id: 0
|
||||||
|
Selecting id: 5
|
||||||
|
Selecting id: 7
|
||||||
|
Selecting id: 8
|
||||||
|
Selecting id: 13
|
||||||
|
Selecting id: 15
|
||||||
|
Selecting id: 6
|
||||||
|
Selecting id: 12
|
||||||
|
Selecting id: 14
|
||||||
|
Selecting id: 3
|
||||||
|
|
Loading…
Reference in New Issue