Fix random cache shuffling when it is not completely full

We were shuffling around the null elements
This commit is contained in:
Christopher Eby 2011-09-06 00:30:39 -05:00
parent b8c137310a
commit 53e40f85f2
2 changed files with 15 additions and 3 deletions

View File

@ -200,6 +200,11 @@ public class MediaUtils {
return count;
}
/**
* Shuffle an array using Fisher-Yates algorithm.
*
* @param list The array. It will be shuffled in place.
*/
public static void shuffle(long[] list)
{
Random random = ContextApplication.getRandom();
@ -211,10 +216,17 @@ public class MediaUtils {
}
}
public static void shuffle(Song[] list)
/**
* Shuffle an array using Fisher-Yates algorithm.
*
* @param list The array. It will be shuffled in place.
* @param end Only elements before this index will be shuffled.
*/
public static void shuffle(Song[] list, int end)
{
assert(end <= list.length && end >= 0);
Random random = ContextApplication.getRandom();
for (int i = list.length; --i != -1; ) {
for (int i = end; --i != -1; ) {
int j = random.nextInt(i + 1);
Song tmp = list[j];
list[j] = list[i];

View File

@ -248,7 +248,7 @@ public class Song implements Parcelable {
cursor.close();
// The query will return sorted results; undo that
MediaUtils.shuffle(mRandomCache);
MediaUtils.shuffle(mRandomCache, count);
mRandomCacheIdx = 0;
mRandomCacheEnd = mAllSongsIdx + count;