forked from len0rd/rockbox
Improved the randomise playlist function pretty major. This one will run
a lot faster. Also made the function accept a seed number as a function argument, as we are likely to want to randomise using a known seed very many times... git-svn-id: svn://svn.rockbox.org/rockbox/trunk@437 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
parent
cd96302418
commit
19b25e9dd6
1 changed files with 16 additions and 60 deletions
|
@ -37,7 +37,7 @@ int reload_playlist_info( playlist_info_t *playlist )
|
||||||
|
|
||||||
/* return a dummy playlist entry */
|
/* return a dummy playlist entry */
|
||||||
|
|
||||||
strncpy( playlist->filename, "\\playlists\\1.m3u", sizeof(playlist->filename) );
|
strncpy( playlist->filename, "test.m3u", sizeof(playlist->filename) );
|
||||||
|
|
||||||
playlist->indices_count = 4;
|
playlist->indices_count = 4;
|
||||||
|
|
||||||
|
@ -160,80 +160,32 @@ track_t next_playlist_track( playlist_info_t *playlist )
|
||||||
/*
|
/*
|
||||||
* randomly rearrange the array of indices for the playlist
|
* randomly rearrange the array of indices for the playlist
|
||||||
*/
|
*/
|
||||||
void randomise_playlist( playlist_info_t *playlist )
|
void randomise_playlist( playlist_info_t *playlist, unsigned int seed )
|
||||||
{
|
{
|
||||||
unsigned seed;
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
int candidate;
|
int candidate;
|
||||||
int adjusted_candidate;
|
int store;
|
||||||
int found_next_number;
|
|
||||||
int *index_list = (int*) malloc(sizeof(int) * playlist->indices_count);
|
|
||||||
int *randomised_list;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
DEBUGF( "randomise_playlist()\n" );
|
DEBUGF( "randomise_playlist()\n" );
|
||||||
|
|
||||||
/* create dynamic storage for randomised list so it can be freed later */
|
/* seed with the given seed */
|
||||||
|
|
||||||
randomised_list = (int *)malloc( playlist->indices_count * sizeof( int ) );
|
|
||||||
|
|
||||||
/* use date as random seed */
|
|
||||||
|
|
||||||
seed = time(0);
|
|
||||||
srand( seed );
|
srand( seed );
|
||||||
|
|
||||||
/* randomise entire indices list */
|
/* randomise entire indices list */
|
||||||
|
|
||||||
while( count < playlist->indices_count )
|
while( count < playlist->indices_count )
|
||||||
{
|
{
|
||||||
found_next_number = 0;
|
/* the rand is from 0 to RAND_MAX, so adjust to our value range */
|
||||||
|
candidate = rand() % ( playlist->indices_count );
|
||||||
/* loop until we successfully get the next number */
|
|
||||||
|
/* now swap the values at the 'count' and 'candidate' positions */
|
||||||
while( ! found_next_number )
|
store = playlist->indices[candidate];
|
||||||
{
|
playlist->indices[candidate] = playlist->indices[count];
|
||||||
/* get the next random number */
|
playlist->indices[count] = store;
|
||||||
|
|
||||||
candidate = rand();
|
|
||||||
|
|
||||||
/* the rand is from 0 to RAND_MAX, so adjust to our value range */
|
|
||||||
|
|
||||||
adjusted_candidate = candidate % ( playlist->indices_count + 1 );
|
|
||||||
|
|
||||||
/* has this number already been used? */
|
|
||||||
|
|
||||||
if( is_unused_random_in_list( adjusted_candidate, index_list, playlist->indices_count ) )
|
|
||||||
{
|
|
||||||
/* store value found at random location in original list */
|
|
||||||
|
|
||||||
index_list[ count ] = adjusted_candidate;
|
|
||||||
|
|
||||||
/* leave loop */
|
|
||||||
|
|
||||||
found_next_number = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* move along */
|
/* move along */
|
||||||
|
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* populate actual replacement list with values
|
|
||||||
* found at indexes specified in index_list */
|
|
||||||
|
|
||||||
for( i = 0; i < playlist->indices_count; i++ )
|
|
||||||
{
|
|
||||||
randomised_list[i] = playlist->indices[ index_list[ i ] ];
|
|
||||||
}
|
|
||||||
|
|
||||||
/* release memory from old array */
|
|
||||||
|
|
||||||
free( (void *)playlist->indices );
|
|
||||||
|
|
||||||
/* use newly randomise list */
|
|
||||||
|
|
||||||
playlist->indices = randomised_list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -340,4 +292,8 @@ void get_indices_as_string( char *string, playlist_info_t *playlist )
|
||||||
<alan> i don't see how you can do it with a list
|
<alan> i don't see how you can do it with a list
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* -----------------------------------------------------------------
|
||||||
|
* local variables:
|
||||||
|
* eval: (load-file "rockbox-mode.el")
|
||||||
|
* end:
|
||||||
|
*/
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue