1
0
Fork 0
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:
Daniel Stenberg 2002-05-05 11:16:32 +00:00
parent cd96302418
commit 19b25e9dd6

View file

@ -37,7 +37,7 @@ int reload_playlist_info( playlist_info_t *playlist )
/* 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;
@ -160,80 +160,32 @@ track_t next_playlist_track( playlist_info_t *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 candidate;
int adjusted_candidate;
int found_next_number;
int *index_list = (int*) malloc(sizeof(int) * playlist->indices_count);
int *randomised_list;
int i;
int store;
DEBUGF( "randomise_playlist()\n" );
/* create dynamic storage for randomised list so it can be freed later */
randomised_list = (int *)malloc( playlist->indices_count * sizeof( int ) );
/* use date as random seed */
seed = time(0);
/* seed with the given seed */
srand( seed );
/* randomise entire indices list */
while( count < playlist->indices_count )
{
found_next_number = 0;
/* loop until we successfully get the next number */
while( ! found_next_number )
{
/* get the next random number */
candidate = rand();
/* the rand is from 0 to RAND_MAX, so adjust to our value range */
candidate = rand() % ( playlist->indices_count );
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;
}
}
/* now swap the values at the 'count' and 'candidate' positions */
store = playlist->indices[candidate];
playlist->indices[candidate] = playlist->indices[count];
playlist->indices[count] = store;
/* move along */
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
*/
/* -----------------------------------------------------------------
* local variables:
* eval: (load-file "rockbox-mode.el")
* end:
*/