summaryrefslogtreecommitdiffstats
path: root/firmware
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2002-05-05 11:16:32 +0000
committerDaniel Stenberg <daniel@haxx.se>2002-05-05 11:16:32 +0000
commit19b25e9dd6a5049d2cc98c982ac0cf45d3d99113 (patch)
tree3c7d39f1ef3badb15724d8cdc1d4dec17733e834 /firmware
parentcd9630241857b3b888f0e3c689ecb361dc58d676 (diff)
downloadrockbox-19b25e9dd6a5049d2cc98c982ac0cf45d3d99113.tar.gz
rockbox-19b25e9dd6a5049d2cc98c982ac0cf45d3d99113.zip
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
Diffstat (limited to 'firmware')
-rw-r--r--firmware/playlist.c76
1 files changed, 16 insertions, 60 deletions
diff --git a/firmware/playlist.c b/firmware/playlist.c
index 50d7313136..2554ed3d31 100644
--- a/firmware/playlist.c
+++ b/firmware/playlist.c
@@ -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 */
-
- 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 */
+ /* the rand is from 0 to RAND_MAX, so adjust to our value range */
+ candidate = rand() % ( playlist->indices_count );
+
+ /* now swap the values at the 'count' and 'candidate' positions */
+ store = playlist->indices[candidate];
+ playlist->indices[candidate] = playlist->indices[count];
+ playlist->indices[count] = store;
- found_next_number = 1;
- }
- }
-
/* 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:
+ */