aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Model/Extensions/LinqExtensions.cs
diff options
context:
space:
mode:
authorAndrew Rabert <ar@nullsum.net>2018-12-27 18:27:57 -0500
committerAndrew Rabert <ar@nullsum.net>2018-12-27 18:27:57 -0500
commita86b71899ec52c44ddc6c3018e8cc5e9d7ff4d62 (patch)
treea74f6ea4a8abfa1664a605d31d48bc38245ccf58 /MediaBrowser.Model/Extensions/LinqExtensions.cs
parent9bac3ac616b01f67db98381feb09d34ebe821f9a (diff)
Add GPL modules
Diffstat (limited to 'MediaBrowser.Model/Extensions/LinqExtensions.cs')
-rw-r--r--MediaBrowser.Model/Extensions/LinqExtensions.cs97
1 files changed, 97 insertions, 0 deletions
diff --git a/MediaBrowser.Model/Extensions/LinqExtensions.cs b/MediaBrowser.Model/Extensions/LinqExtensions.cs
new file mode 100644
index 000000000..09ace42e8
--- /dev/null
+++ b/MediaBrowser.Model/Extensions/LinqExtensions.cs
@@ -0,0 +1,97 @@
+using System;
+using System.Collections.Generic;
+
+namespace MediaBrowser.Model.Extensions
+{
+ // MoreLINQ - Extensions to LINQ to Objects
+ // Copyright (c) 2008 Jonathan Skeet. All rights reserved.
+ //
+ // Licensed under the Apache License, Version 2.0 (the "License");
+ // you may not use this file except in compliance with the License.
+ // You may obtain a copy of the License at
+ //
+ // http://www.apache.org/licenses/LICENSE-2.0
+ //
+ // Unless required by applicable law or agreed to in writing, software
+ // distributed under the License is distributed on an "AS IS" BASIS,
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ // See the License for the specific language governing permissions and
+ // limitations under the License.
+
+ public static class LinqExtensions
+ {
+ /// <summary>
+ /// Returns all distinct elements of the given source, where "distinctness"
+ /// is determined via a projection and the default equality comparer for the projected type.
+ /// </summary>
+ /// <remarks>
+ /// This operator uses deferred execution and streams the results, although
+ /// a set of already-seen keys is retained. If a key is seen multiple times,
+ /// only the first element with that key is returned.
+ /// </remarks>
+ /// <typeparam name="TSource">Type of the source sequence</typeparam>
+ /// <typeparam name="TKey">Type of the projected element</typeparam>
+ /// <param name="source">Source sequence</param>
+ /// <param name="keySelector">Projection for determining "distinctness"</param>
+ /// <returns>A sequence consisting of distinct elements from the source sequence,
+ /// comparing them by the specified key projection.</returns>
+
+ public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector)
+ {
+ return source.DistinctBy(keySelector, null);
+ }
+
+ public static TSource[] ToArray<TSource>(this IEnumerable<TSource> source, int count)
+ {
+ if (source == null) throw new ArgumentNullException("source");
+ if (count < 0) throw new ArgumentOutOfRangeException("count");
+ var array = new TSource[count];
+ int i = 0;
+ foreach (var item in source)
+ {
+ array[i++] = item;
+ }
+ return array;
+ }
+
+ /// <summary>
+ /// Returns all distinct elements of the given source, where "distinctness"
+ /// is determined via a projection and the specified comparer for the projected type.
+ /// </summary>
+ /// <remarks>
+ /// This operator uses deferred execution and streams the results, although
+ /// a set of already-seen keys is retained. If a key is seen multiple times,
+ /// only the first element with that key is returned.
+ /// </remarks>
+ /// <typeparam name="TSource">Type of the source sequence</typeparam>
+ /// <typeparam name="TKey">Type of the projected element</typeparam>
+ /// <param name="source">Source sequence</param>
+ /// <param name="keySelector">Projection for determining "distinctness"</param>
+ /// <param name="comparer">The equality comparer to use to determine whether or not keys are equal.
+ /// If null, the default equality comparer for <c>TSource</c> is used.</param>
+ /// <returns>A sequence consisting of distinct elements from the source sequence,
+ /// comparing them by the specified key projection.</returns>
+
+ public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
+ {
+ if (source == null) throw new ArgumentNullException("source");
+ if (keySelector == null) throw new ArgumentNullException("keySelector");
+ return DistinctByImpl(source, keySelector, comparer);
+ }
+
+ private static IEnumerable<TSource> DistinctByImpl<TSource, TKey>(IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
+ {
+ var knownKeys = new HashSet<TKey>(comparer);
+ foreach (var element in source)
+ {
+ if (knownKeys.Add(keySelector(element)))
+ {
+ yield return element;
+ }
+ }
+ }
+ }
+}