-
-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Queue for repeat, shuffle and more
- Loading branch information
Showing
23 changed files
with
228 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace MediaManager | ||
{ | ||
public static class CollectionExtensions | ||
{ | ||
public static int[] GetShuffleExchanges(int size, int key) | ||
{ | ||
int[] exchanges = new int[size - 1]; | ||
var rand = new Random(key); | ||
for (int i = size - 1; i > 0; i--) | ||
{ | ||
int n = rand.Next(i + 1); | ||
exchanges[size - 1 - i] = n; | ||
} | ||
return exchanges; | ||
} | ||
|
||
public static void Shuffle<T>(this IList<T> list, int key) | ||
{ | ||
int size = list.Count; | ||
var exchanges = GetShuffleExchanges(size, key); | ||
for (int i = size - 1; i > 0; i--) | ||
{ | ||
int n = exchanges[size - 1 - i]; | ||
var tmp = list[i]; | ||
list[i] = list[n]; | ||
list[n] = tmp; | ||
} | ||
} | ||
|
||
public static void DeShuffle<T>(this IList<T> list, int key) | ||
{ | ||
int size = list.Count; | ||
var exchanges = GetShuffleExchanges(size, key); | ||
for (int i = 1; i < size; i++) | ||
{ | ||
int n = exchanges[size - i - 1]; | ||
var tmp = list[i]; | ||
list[i] = list[n]; | ||
list[n] = tmp; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.