2005-01-14, 18:43
hi,
i was missing some features so i thought i'd try to add them.specifically, i missed a way to edit a playlist (move around some items in it) and playing a shuffled playlist.
i was surprised to see that in playlist.h/.cpp there were in fact functions to shuffle and to move an item in the playlist. did i just not find the control to activate them?
anyway, at work i was bored and decided to try to implement a shuffle for a vector (having seen that the playlist was a vector) and without knowing it was already implemented, came up with another implementation:
instead of:
---------------------------------------------------------
void cplaylist:huffle()
{
srand( timegettime() );
int nitemcount = size();
// iterate through each catalogue item performing arbitrary swaps
for (int nitem=0; nitem<nitemcount; nitem++)
{
int narbitrary = rand() % nitemcount;
cplaylistitem anitem = m_vecitems[narbitrary];
m_vecitems[narbitrary] = m_vecitems[nitem];
m_vecitems[nitem] = anitem;
}
}
---------------------------------------------------------
i did the following:
---------------------------------------------------------
void cplaylist:huffle()
{
srand( timegettime() );
int nitemcount = size();
// iterate through each catalogue item selecting a random item for that index
for (int nitem=0; nitem<nitemcount; nitem++)
{
int narbitrary = (rand() % (nitemcount-nitem))+nitem;
if (narbitrary != nitem)
{
cplaylistitem anitem = m_vecitems[narbitrary];
m_vecitems.erase(&m_vecitems[narbitrary]);
m_vecitems.insert(&m_vecitems[nitem], anitem);
}
}
}
---------------------------------------------------------
to me, it feels like my code gives a better shuffled list, because i only swap items that haven't been swapped before. or do you guys think it makes no difference. maybe i should make a little test program, that displays lists shuffled with both methods.
on a side note, i was also working on a quicksort for vectors, and in the progress of that noticed that if you keep seeding rand() with the time, the random numbers don't differ greatly. (this was under windows and i seeded with srand(time(null))) wouldn't it be better to just call srand( timegettime() ) once for the whole media center?
please keep in mind, this is my first stab at coding for xbmc, i'm not all that familiar with the source yet :p
cheers,
remco
i was missing some features so i thought i'd try to add them.specifically, i missed a way to edit a playlist (move around some items in it) and playing a shuffled playlist.
i was surprised to see that in playlist.h/.cpp there were in fact functions to shuffle and to move an item in the playlist. did i just not find the control to activate them?
anyway, at work i was bored and decided to try to implement a shuffle for a vector (having seen that the playlist was a vector) and without knowing it was already implemented, came up with another implementation:
instead of:
---------------------------------------------------------
void cplaylist:huffle()
{
srand( timegettime() );
int nitemcount = size();
// iterate through each catalogue item performing arbitrary swaps
for (int nitem=0; nitem<nitemcount; nitem++)
{
int narbitrary = rand() % nitemcount;
cplaylistitem anitem = m_vecitems[narbitrary];
m_vecitems[narbitrary] = m_vecitems[nitem];
m_vecitems[nitem] = anitem;
}
}
---------------------------------------------------------
i did the following:
---------------------------------------------------------
void cplaylist:huffle()
{
srand( timegettime() );
int nitemcount = size();
// iterate through each catalogue item selecting a random item for that index
for (int nitem=0; nitem<nitemcount; nitem++)
{
int narbitrary = (rand() % (nitemcount-nitem))+nitem;
if (narbitrary != nitem)
{
cplaylistitem anitem = m_vecitems[narbitrary];
m_vecitems.erase(&m_vecitems[narbitrary]);
m_vecitems.insert(&m_vecitems[nitem], anitem);
}
}
}
---------------------------------------------------------
to me, it feels like my code gives a better shuffled list, because i only swap items that haven't been swapped before. or do you guys think it makes no difference. maybe i should make a little test program, that displays lists shuffled with both methods.
on a side note, i was also working on a quicksort for vectors, and in the progress of that noticed that if you keep seeding rand() with the time, the random numbers don't differ greatly. (this was under windows and i seeded with srand(time(null))) wouldn't it be better to just call srand( timegettime() ) once for the whole media center?
please keep in mind, this is my first stab at coding for xbmc, i'm not all that familiar with the source yet :p
cheers,
remco