diff options
author | Daniel Stenberg <daniel@haxx.se> | 2002-05-05 11:16:32 +0000 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2002-05-05 11:16:32 +0000 |
commit | 19b25e9dd6a5049d2cc98c982ac0cf45d3d99113 (patch) | |
tree | 3c7d39f1ef3badb15724d8cdc1d4dec17733e834 /firmware/playlist.c | |
parent | cd9630241857b3b888f0e3c689ecb361dc58d676 (diff) | |
download | rockbox-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/playlist.c')
-rw-r--r-- | firmware/playlist.c | 76 |
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 ) | |||
37 | 37 | ||
38 | /* return a dummy playlist entry */ | 38 | /* return a dummy playlist entry */ |
39 | 39 | ||
40 | strncpy( playlist->filename, "\\playlists\\1.m3u", sizeof(playlist->filename) ); | 40 | strncpy( playlist->filename, "test.m3u", sizeof(playlist->filename) ); |
41 | 41 | ||
42 | playlist->indices_count = 4; | 42 | playlist->indices_count = 4; |
43 | 43 | ||
@@ -160,80 +160,32 @@ track_t next_playlist_track( playlist_info_t *playlist ) | |||
160 | /* | 160 | /* |
161 | * randomly rearrange the array of indices for the playlist | 161 | * randomly rearrange the array of indices for the playlist |
162 | */ | 162 | */ |
163 | void randomise_playlist( playlist_info_t *playlist ) | 163 | void randomise_playlist( playlist_info_t *playlist, unsigned int seed ) |
164 | { | 164 | { |
165 | unsigned seed; | ||
166 | int count = 0; | 165 | int count = 0; |
167 | int candidate; | 166 | int candidate; |
168 | int adjusted_candidate; | 167 | int store; |
169 | int found_next_number; | ||
170 | int *index_list = (int*) malloc(sizeof(int) * playlist->indices_count); | ||
171 | int *randomised_list; | ||
172 | int i; | ||
173 | 168 | ||
174 | DEBUGF( "randomise_playlist()\n" ); | 169 | DEBUGF( "randomise_playlist()\n" ); |
175 | 170 | ||
176 | /* create dynamic storage for randomised list so it can be freed later */ | 171 | /* seed with the given seed */ |
177 | |||
178 | randomised_list = (int *)malloc( playlist->indices_count * sizeof( int ) ); | ||
179 | |||
180 | /* use date as random seed */ | ||
181 | |||
182 | seed = time(0); | ||
183 | srand( seed ); | 172 | srand( seed ); |
184 | 173 | ||
185 | /* randomise entire indices list */ | 174 | /* randomise entire indices list */ |
186 | 175 | ||
187 | while( count < playlist->indices_count ) | 176 | while( count < playlist->indices_count ) |
188 | { | 177 | { |
189 | found_next_number = 0; | 178 | /* the rand is from 0 to RAND_MAX, so adjust to our value range */ |
190 | 179 | candidate = rand() % ( playlist->indices_count ); | |
191 | /* loop until we successfully get the next number */ | 180 | |
192 | 181 | /* now swap the values at the 'count' and 'candidate' positions */ | |
193 | while( ! found_next_number ) | 182 | store = playlist->indices[candidate]; |
194 | { | 183 | playlist->indices[candidate] = playlist->indices[count]; |
195 | /* get the next random number */ | 184 | playlist->indices[count] = store; |
196 | |||
197 | candidate = rand(); | ||
198 | |||
199 | /* the rand is from 0 to RAND_MAX, so adjust to our value range */ | ||
200 | |||
201 | adjusted_candidate = candidate % ( playlist->indices_count + 1 ); | ||
202 | |||
203 | /* has this number already been used? */ | ||
204 | |||
205 | if( is_unused_random_in_list( adjusted_candidate, index_list, playlist->indices_count ) ) | ||
206 | { | ||
207 | /* store value found at random location in original list */ | ||
208 | |||
209 | index_list[ count ] = adjusted_candidate; | ||
210 | |||
211 | /* leave loop */ | ||
212 | 185 | ||
213 | found_next_number = 1; | ||
214 | } | ||
215 | } | ||
216 | |||
217 | /* move along */ | 186 | /* move along */ |
218 | |||
219 | count++; | 187 | count++; |
220 | } | 188 | } |
221 | |||
222 | /* populate actual replacement list with values | ||
223 | * found at indexes specified in index_list */ | ||
224 | |||
225 | for( i = 0; i < playlist->indices_count; i++ ) | ||
226 | { | ||
227 | randomised_list[i] = playlist->indices[ index_list[ i ] ]; | ||
228 | } | ||
229 | |||
230 | /* release memory from old array */ | ||
231 | |||
232 | free( (void *)playlist->indices ); | ||
233 | |||
234 | /* use newly randomise list */ | ||
235 | |||
236 | playlist->indices = randomised_list; | ||
237 | } | 189 | } |
238 | 190 | ||
239 | /* | 191 | /* |
@@ -340,4 +292,8 @@ void get_indices_as_string( char *string, playlist_info_t *playlist ) | |||
340 | <alan> i don't see how you can do it with a list | 292 | <alan> i don't see how you can do it with a list |
341 | */ | 293 | */ |
342 | 294 | ||
343 | 295 | /* ----------------------------------------------------------------- | |
296 | * local variables: | ||
297 | * eval: (load-file "rockbox-mode.el") | ||
298 | * end: | ||
299 | */ | ||