diff options
| author | Bond-009 <bond.009@outlook.com> | 2019-12-06 12:06:13 +0100 |
|---|---|---|
| committer | Bond-009 <bond.009@outlook.com> | 2019-12-06 12:06:13 +0100 |
| commit | a2c35e6dba02f068a3f06e5a4e4964e6539069d1 (patch) | |
| tree | e75984ab85fedceaf96150ad9d5241cf88230a60 /MediaBrowser.Common/Extensions | |
| parent | 94edb5b9f98cf3b06144255eccc988712332f0a8 (diff) | |
| parent | 935525e77a18061195dea786be71d38fffe82a10 (diff) | |
Merge remote-tracking branch 'upstream/master' into random
Diffstat (limited to 'MediaBrowser.Common/Extensions')
| -rw-r--r-- | MediaBrowser.Common/Extensions/CopyToExtensions.cs (renamed from MediaBrowser.Common/Extensions/CollectionExtensions.cs) | 33 | ||||
| -rw-r--r-- | MediaBrowser.Common/Extensions/ShuffleExtensions.cs | 31 |
2 files changed, 34 insertions, 30 deletions
diff --git a/MediaBrowser.Common/Extensions/CollectionExtensions.cs b/MediaBrowser.Common/Extensions/CopyToExtensions.cs index a12b2833f..78a73f07e 100644 --- a/MediaBrowser.Common/Extensions/CollectionExtensions.cs +++ b/MediaBrowser.Common/Extensions/CopyToExtensions.cs @@ -1,39 +1,12 @@ -#pragma warning disable CS1591 - -using System; using System.Collections.Generic; namespace MediaBrowser.Common.Extensions { - // The MS CollectionExtensions are only available in netcoreapp + /// <summary> + /// Provides <c>CopyTo</c> extensions methods for <see cref="IReadOnlyList{T}" />. + /// </summary> 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); - return ret; - } - /// <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. diff --git a/MediaBrowser.Common/Extensions/ShuffleExtensions.cs b/MediaBrowser.Common/Extensions/ShuffleExtensions.cs new file mode 100644 index 000000000..5889d09c4 --- /dev/null +++ b/MediaBrowser.Common/Extensions/ShuffleExtensions.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; + +namespace MediaBrowser.Common.Extensions +{ + /// <summary> + /// Provides <c>Shuffle</c> extensions methods for <see cref="IList{T}" />. + /// </summary> + public static class ShuffleExtensions + { + 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; + } + } + } +} |
