aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Extensions/CollectionExtensions.cs
diff options
context:
space:
mode:
authorBond_009 <bond.009@outlook.com>2019-10-20 16:08:40 +0200
committerBond_009 <bond.009@outlook.com>2019-10-20 16:08:40 +0200
commitbbc08753878b7b8a37ed876385d2e6864349d00d (patch)
treef983b32a0f58c410c28969ffcce599153dc4bd81 /MediaBrowser.Common/Extensions/CollectionExtensions.cs
parente3f1f62a8bb5377c80e3c26331915db7e0fe2fa4 (diff)
Don't shuffle some types by default
Diffstat (limited to 'MediaBrowser.Common/Extensions/CollectionExtensions.cs')
-rw-r--r--MediaBrowser.Common/Extensions/CollectionExtensions.cs37
1 files changed, 21 insertions, 16 deletions
diff --git a/MediaBrowser.Common/Extensions/CollectionExtensions.cs b/MediaBrowser.Common/Extensions/CollectionExtensions.cs
index 215224398..a12b2833f 100644
--- a/MediaBrowser.Common/Extensions/CollectionExtensions.cs
+++ b/MediaBrowser.Common/Extensions/CollectionExtensions.cs
@@ -1,5 +1,6 @@
#pragma warning disable CS1591
+using System;
using System.Collections.Generic;
namespace MediaBrowser.Common.Extensions
@@ -7,6 +8,26 @@ namespace MediaBrowser.Common.Extensions
// The MS CollectionExtensions are only available in netcoreapp
public static class CollectionExtensions
{
+ private static readonly Random _rng = new Random();
+
+ /// <summary>
+ /// Shuffles the items in a list.
+ /// </summary>
+ /// <param name="list">The list that should get shuffled.</param>
+ /// <typeparam name="T">The type.</typeparam>
+ public static void Shuffle<T>(this IList<T> list)
+ {
+ int n = list.Count;
+ while (n > 1)
+ {
+ n--;
+ int k = _rng.Next(n + 1);
+ T value = list[k];
+ list[k] = list[n];
+ list[n] = value;
+ }
+ }
+
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key)
{
dictionary.TryGetValue(key, out var ret);
@@ -28,21 +49,5 @@ namespace MediaBrowser.Common.Extensions
destination[index + i] = source[i];
}
}
-
- /// <summary>
- /// Copies all the elements of the current collection to the specified list
- /// starting at the specified destination array index. The index is specified as a 32-bit integer.
- /// </summary>
- /// <param name="source">The current collection that is the source of the elements.</param>
- /// <param name="destination">The list that is the destination of the elements copied from the current collection.</param>
- /// <param name="index">A 32-bit integer that represents the index in <c>destination</c> at which copying begins.</param>
- /// <typeparam name="T"></typeparam>
- public static void CopyTo<T>(this IReadOnlyCollection<T> source, IList<T> destination, int index = 0)
- {
- foreach (T item in source)
- {
- destination[index++] = item;
- }
- }
}
}