Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/Newtonsoft.Json.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'Src/Newtonsoft.Json/Utilities/LinqBridge.cs')
-rw-r--r--Src/Newtonsoft.Json/Utilities/LinqBridge.cs4936
1 files changed, 2419 insertions, 2517 deletions
diff --git a/Src/Newtonsoft.Json/Utilities/LinqBridge.cs b/Src/Newtonsoft.Json/Utilities/LinqBridge.cs
index 21a3400..25810c1 100644
--- a/Src/Newtonsoft.Json/Utilities/LinqBridge.cs
+++ b/Src/Newtonsoft.Json/Utilities/LinqBridge.cs
@@ -1,7 +1,5 @@
#if NET20
-#define LINQBRIDGE_LIB
-
#region License, Terms and Author(s)
//
// LINQBridge
@@ -29,3080 +27,2984 @@
//
#endregion
-// $Id: Enumerable.cs 240 2010-10-19 21:49:03Z azizatif $
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Diagnostics;
+using Newtonsoft.Json.Serialization;
-namespace System.Linq
+namespace Newtonsoft.Json.Utilities.LinqBridge
{
- #region Imports
+ /// <summary>
+ /// Provides a set of static (Shared in Visual Basic) methods for
+ /// querying objects that implement <see cref="IEnumerable{T}" />.
+ /// </summary>
+ internal static partial class Enumerable
+ {
+ /// <summary>
+ /// Returns the input typed as <see cref="IEnumerable{T}"/>.
+ /// </summary>
+
+ public static IEnumerable<TSource> AsEnumerable<TSource>(IEnumerable<TSource> source)
+ {
+ return source;
+ }
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using LinqBridge;
+ /// <summary>
+ /// Returns an empty <see cref="IEnumerable{T}"/> that has the
+ /// specified type argument.
+ /// </summary>
- #endregion
+ public static IEnumerable<TResult> Empty<TResult>()
+ {
+ return Sequence<TResult>.Empty;
+ }
/// <summary>
- /// Provides a set of static (Shared in Visual Basic) methods for
- /// querying objects that implement <see cref="IEnumerable{T}" />.
+ /// Converts the elements of an <see cref="IEnumerable"/> to the
+ /// specified type.
/// </summary>
- static partial class Enumerable
+ public static IEnumerable<TResult> Cast<TResult>(
+ this IEnumerable source)
{
- /// <summary>
- /// Returns the input typed as <see cref="IEnumerable{T}"/>.
- /// </summary>
+ CheckNotNull(source, "source");
- public static IEnumerable<TSource> AsEnumerable<TSource>(IEnumerable<TSource> source)
- {
- return source;
- }
+ return CastYield<TResult>(source);
+ }
- /// <summary>
- /// Returns an empty <see cref="IEnumerable{T}"/> that has the
- /// specified type argument.
- /// </summary>
+ private static IEnumerable<TResult> CastYield<TResult>(
+ IEnumerable source)
+ {
+ foreach (var item in source)
+ yield return (TResult) item;
+ }
- public static IEnumerable<TResult> Empty<TResult>()
- {
- return Sequence<TResult>.Empty;
- }
+ /// <summary>
+ /// Filters the elements of an <see cref="IEnumerable"/> based on a specified type.
+ /// </summary>
- /// <summary>
- /// Converts the elements of an <see cref="IEnumerable"/> to the
- /// specified type.
- /// </summary>
+ public static IEnumerable<TResult> OfType<TResult>(
+ this IEnumerable source)
+ {
+ CheckNotNull(source, "source");
- public static IEnumerable<TResult> Cast<TResult>(
- this IEnumerable source)
- {
- CheckNotNull(source, "source");
+ return OfTypeYield<TResult>(source);
+ }
- return CastYield<TResult>(source);
- }
+ private static IEnumerable<TResult> OfTypeYield<TResult>(
+ IEnumerable source)
+ {
+ foreach (var item in source)
+ if (item is TResult)
+ yield return (TResult) item;
+ }
- private static IEnumerable<TResult> CastYield<TResult>(
- IEnumerable source)
- {
- foreach (var item in source)
- yield return (TResult) item;
- }
+ /// <summary>
+ /// Generates a sequence of integral numbers within a specified range.
+ /// </summary>
+ /// <param name="start">The value of the first integer in the sequence.</param>
+ /// <param name="count">The number of sequential integers to generate.</param>
- /// <summary>
- /// Filters the elements of an <see cref="IEnumerable"/> based on a specified type.
- /// </summary>
+ public static IEnumerable<int> Range(int start, int count)
+ {
+ if (count < 0)
+ throw new ArgumentOutOfRangeException("count", count, null);
- public static IEnumerable<TResult> OfType<TResult>(
- this IEnumerable source)
- {
- CheckNotNull(source, "source");
+ var end = (long) start + count;
+ if (end - 1 >= int.MaxValue)
+ throw new ArgumentOutOfRangeException("count", count, null);
- return OfTypeYield<TResult>(source);
- }
+ return RangeYield(start, end);
+ }
- private static IEnumerable<TResult> OfTypeYield<TResult>(
- IEnumerable source)
- {
- foreach (var item in source)
- if (item is TResult)
- yield return (TResult) item;
- }
+ private static IEnumerable<int> RangeYield(int start, long end)
+ {
+ for (var i = start; i < end; i++)
+ yield return i;
+ }
- /// <summary>
- /// Generates a sequence of integral numbers within a specified range.
- /// </summary>
- /// <param name="start">The value of the first integer in the sequence.</param>
- /// <param name="count">The number of sequential integers to generate.</param>
+ /// <summary>
+ /// Generates a sequence that contains one repeated value.
+ /// </summary>
- public static IEnumerable<int> Range(int start, int count)
- {
- if (count < 0)
- throw new ArgumentOutOfRangeException("count", count, null);
+ public static IEnumerable<TResult> Repeat<TResult>(TResult element, int count)
+ {
+ if (count < 0) throw new ArgumentOutOfRangeException("count", count, null);
- var end = (long) start + count;
- if (end - 1 >= int.MaxValue)
- throw new ArgumentOutOfRangeException("count", count, null);
+ return RepeatYield(element, count);
+ }
- return RangeYield(start, end);
- }
+ private static IEnumerable<TResult> RepeatYield<TResult>(TResult element, int count)
+ {
+ for (var i = 0; i < count; i++)
+ yield return element;
+ }
- private static IEnumerable<int> RangeYield(int start, long end)
- {
- for (var i = start; i < end; i++)
- yield return i;
- }
+ /// <summary>
+ /// Filters a sequence of values based on a predicate.
+ /// </summary>
- /// <summary>
- /// Generates a sequence that contains one repeated value.
- /// </summary>
+ public static IEnumerable<TSource> Where<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ CheckNotNull(predicate, "predicate");
- public static IEnumerable<TResult> Repeat<TResult>(TResult element, int count)
- {
- if (count < 0) throw new ArgumentOutOfRangeException("count", count, null);
+ return source.Where((item, i) => predicate(item));
+ }
- return RepeatYield(element, count);
- }
+ /// <summary>
+ /// Filters a sequence of values based on a predicate.
+ /// Each element's index is used in the logic of the predicate function.
+ /// </summary>
- private static IEnumerable<TResult> RepeatYield<TResult>(TResult element, int count)
- {
- for (var i = 0; i < count; i++)
- yield return element;
- }
+ public static IEnumerable<TSource> Where<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, bool> predicate)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(predicate, "predicate");
- /// <summary>
- /// Filters a sequence of values based on a predicate.
- /// </summary>
+ return WhereYield(source, predicate);
+ }
- public static IEnumerable<TSource> Where<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- CheckNotNull(predicate, "predicate");
+ private static IEnumerable<TSource> WhereYield<TSource>(
+ IEnumerable<TSource> source,
+ Func<TSource, int, bool> predicate)
+ {
+ var i = 0;
+ foreach (var item in source)
+ if (predicate(item, i++))
+ yield return item;
+ }
- return source.Where((item, i) => predicate(item));
- }
+ /// <summary>
+ /// Projects each element of a sequence into a new form.
+ /// </summary>
- /// <summary>
- /// Filters a sequence of values based on a predicate.
- /// Each element's index is used in the logic of the predicate function.
- /// </summary>
+ public static IEnumerable<TResult> Select<TSource, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TResult> selector)
+ {
+ CheckNotNull(selector, "selector");
- public static IEnumerable<TSource> Where<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int, bool> predicate)
- {
- CheckNotNull(source, "source");
- CheckNotNull(predicate, "predicate");
+ return source.Select((item, i) => selector(item));
+ }
- return WhereYield(source, predicate);
- }
+ /// <summary>
+ /// Projects each element of a sequence into a new form by
+ /// incorporating the element's index.
+ /// </summary>
- private static IEnumerable<TSource> WhereYield<TSource>(
- IEnumerable<TSource> source,
- Func<TSource, int, bool> predicate)
- {
- var i = 0;
- foreach (var item in source)
- if (predicate(item, i++))
- yield return item;
- }
+ public static IEnumerable<TResult> Select<TSource, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, TResult> selector)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(selector, "selector");
- /// <summary>
- /// Projects each element of a sequence into a new form.
- /// </summary>
+ return SelectYield(source, selector);
+ }
- public static IEnumerable<TResult> Select<TSource, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, TResult> selector)
- {
- CheckNotNull(selector, "selector");
+ private static IEnumerable<TResult> SelectYield<TSource, TResult>(
+ IEnumerable<TSource> source,
+ Func<TSource, int, TResult> selector)
+ {
+ var i = 0;
+ foreach (var item in source)
+ yield return selector(item, i++);
+ }
- return source.Select((item, i) => selector(item));
- }
+ /// <summary>
+ /// Projects each element of a sequence to an <see cref="IEnumerable{T}" />
+ /// and flattens the resulting sequences into one sequence.
+ /// </summary>
- /// <summary>
- /// Projects each element of a sequence into a new form by
- /// incorporating the element's index.
- /// </summary>
+ public static IEnumerable<TResult> SelectMany<TSource, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, IEnumerable<TResult>> selector)
+ {
+ CheckNotNull(selector, "selector");
- public static IEnumerable<TResult> Select<TSource, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, int, TResult> selector)
- {
- CheckNotNull(source, "source");
- CheckNotNull(selector, "selector");
+ return source.SelectMany((item, i) => selector(item));
+ }
- return SelectYield(source, selector);
- }
+ /// <summary>
+ /// Projects each element of a sequence to an <see cref="IEnumerable{T}" />,
+ /// and flattens the resulting sequences into one sequence. The
+ /// index of each source element is used in the projected form of
+ /// that element.
+ /// </summary>
- private static IEnumerable<TResult> SelectYield<TSource, TResult>(
- IEnumerable<TSource> source,
- Func<TSource, int, TResult> selector)
- {
- var i = 0;
- foreach (var item in source)
- yield return selector(item, i++);
- }
+ public static IEnumerable<TResult> SelectMany<TSource, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, IEnumerable<TResult>> selector)
+ {
+ CheckNotNull(selector, "selector");
- /// <summary>
- /// Projects each element of a sequence to an <see cref="IEnumerable{T}" />
- /// and flattens the resulting sequences into one sequence.
- /// </summary>
+ return source.SelectMany(selector, (item, subitem) => subitem);
+ }
- public static IEnumerable<TResult> SelectMany<TSource, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, IEnumerable<TResult>> selector)
- {
- CheckNotNull(selector, "selector");
+ /// <summary>
+ /// Projects each element of a sequence to an <see cref="IEnumerable{T}" />,
+ /// flattens the resulting sequences into one sequence, and invokes
+ /// a result selector function on each element therein.
+ /// </summary>
- return source.SelectMany((item, i) => selector(item));
- }
+ public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, IEnumerable<TCollection>> collectionSelector,
+ Func<TSource, TCollection, TResult> resultSelector)
+ {
+ CheckNotNull(collectionSelector, "collectionSelector");
- /// <summary>
- /// Projects each element of a sequence to an <see cref="IEnumerable{T}" />,
- /// and flattens the resulting sequences into one sequence. The
- /// index of each source element is used in the projected form of
- /// that element.
- /// </summary>
+ return source.SelectMany((item, i) => collectionSelector(item), resultSelector);
+ }
- public static IEnumerable<TResult> SelectMany<TSource, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, int, IEnumerable<TResult>> selector)
- {
- CheckNotNull(selector, "selector");
+ /// <summary>
+ /// Projects each element of a sequence to an <see cref="IEnumerable{T}" />,
+ /// flattens the resulting sequences into one sequence, and invokes
+ /// a result selector function on each element therein. The index of
+ /// each source element is used in the intermediate projected form
+ /// of that element.
+ /// </summary>
- return source.SelectMany(selector, (item, subitem) => subitem);
- }
+ public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, IEnumerable<TCollection>> collectionSelector,
+ Func<TSource, TCollection, TResult> resultSelector)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(collectionSelector, "collectionSelector");
+ CheckNotNull(resultSelector, "resultSelector");
- /// <summary>
- /// Projects each element of a sequence to an <see cref="IEnumerable{T}" />,
- /// flattens the resulting sequences into one sequence, and invokes
- /// a result selector function on each element therein.
- /// </summary>
+ return SelectManyYield(source, collectionSelector, resultSelector);
+ }
- public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, IEnumerable<TCollection>> collectionSelector,
- Func<TSource, TCollection, TResult> resultSelector)
- {
- CheckNotNull(collectionSelector, "collectionSelector");
+ private static IEnumerable<TResult> SelectManyYield<TSource, TCollection, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, IEnumerable<TCollection>> collectionSelector,
+ Func<TSource, TCollection, TResult> resultSelector)
+ {
+ var i = 0;
+ foreach (var item in source)
+ foreach (var subitem in collectionSelector(item, i++))
+ yield return resultSelector(item, subitem);
+ }
- return source.SelectMany((item, i) => collectionSelector(item), resultSelector);
- }
+ /// <summary>
+ /// Returns elements from a sequence as long as a specified condition is true.
+ /// </summary>
- /// <summary>
- /// Projects each element of a sequence to an <see cref="IEnumerable{T}" />,
- /// flattens the resulting sequences into one sequence, and invokes
- /// a result selector function on each element therein. The index of
- /// each source element is used in the intermediate projected form
- /// of that element.
- /// </summary>
-
- public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, int, IEnumerable<TCollection>> collectionSelector,
- Func<TSource, TCollection, TResult> resultSelector)
- {
- CheckNotNull(source, "source");
- CheckNotNull(collectionSelector, "collectionSelector");
- CheckNotNull(resultSelector, "resultSelector");
+ public static IEnumerable<TSource> TakeWhile<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ CheckNotNull(predicate, "predicate");
- return SelectManyYield(source, collectionSelector, resultSelector);
- }
+ return source.TakeWhile((item, i) => predicate(item));
+ }
- private static IEnumerable<TResult> SelectManyYield<TSource, TCollection, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, int, IEnumerable<TCollection>> collectionSelector,
- Func<TSource, TCollection, TResult> resultSelector)
- {
- var i = 0;
- foreach (var item in source)
- foreach (var subitem in collectionSelector(item, i++))
- yield return resultSelector(item, subitem);
- }
+ /// <summary>
+ /// Returns elements from a sequence as long as a specified condition is true.
+ /// The element's index is used in the logic of the predicate function.
+ /// </summary>
- /// <summary>
- /// Returns elements from a sequence as long as a specified condition is true.
- /// </summary>
+ public static IEnumerable<TSource> TakeWhile<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, bool> predicate)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(predicate, "predicate");
- public static IEnumerable<TSource> TakeWhile<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- CheckNotNull(predicate, "predicate");
+ return TakeWhileYield(source, predicate);
+ }
- return source.TakeWhile((item, i) => predicate(item));
- }
+ private static IEnumerable<TSource> TakeWhileYield<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, bool> predicate)
+ {
+ var i = 0;
+ foreach (var item in source)
+ if (predicate(item, i++))
+ yield return item;
+ else
+ break;
+ }
- /// <summary>
- /// Returns elements from a sequence as long as a specified condition is true.
- /// The element's index is used in the logic of the predicate function.
- /// </summary>
+ private static class Futures<T>
+ {
+ public static readonly Func<T> Default = () => default(T);
+ public static readonly Func<T> Undefined = () => { throw new InvalidOperationException(); };
+ }
- public static IEnumerable<TSource> TakeWhile<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int, bool> predicate)
- {
- CheckNotNull(source, "source");
- CheckNotNull(predicate, "predicate");
+ /// <summary>
+ /// Base implementation of First operator.
+ /// </summary>
- return TakeWhileYield(source, predicate);
- }
+ private static TSource FirstImpl<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource> empty)
+ {
+ CheckNotNull(source, "source");
+ Debug.Assert(empty != null);
- private static IEnumerable<TSource> TakeWhileYield<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int, bool> predicate)
- {
- var i = 0;
- foreach (var item in source)
- if (predicate(item, i++))
- yield return item;
- else
- break;
- }
+ var list = source as IList<TSource>; // optimized case for lists
+ if (list != null)
+ return list.Count > 0 ? list[0] : empty();
- private static class Futures<T>
- {
- public static readonly Func<T> Default = () => default(T);
- public static readonly Func<T> Undefined = () => { throw new InvalidOperationException(); };
- }
+ using (var e = source.GetEnumerator()) // fallback for enumeration
+ return e.MoveNext() ? e.Current : empty();
+ }
- /// <summary>
- /// Base implementation of First operator.
- /// </summary>
-
- private static TSource FirstImpl<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource> empty)
- {
- CheckNotNull(source, "source");
- Debug.Assert(empty != null);
+ /// <summary>
+ /// Returns the first element of a sequence.
+ /// </summary>
- var list = source as IList<TSource>; // optimized case for lists
- if (list != null)
- return list.Count > 0 ? list[0] : empty();
+ public static TSource First<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return source.FirstImpl(Futures<TSource>.Undefined);
+ }
- using (var e = source.GetEnumerator()) // fallback for enumeration
- return e.MoveNext() ? e.Current : empty();
- }
+ /// <summary>
+ /// Returns the first element in a sequence that satisfies a specified condition.
+ /// </summary>
- /// <summary>
- /// Returns the first element of a sequence.
- /// </summary>
+ public static TSource First<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return First(source.Where(predicate));
+ }
- public static TSource First<TSource>(
- this IEnumerable<TSource> source)
- {
- return source.FirstImpl(Futures<TSource>.Undefined);
- }
+ /// <summary>
+ /// Returns the first element of a sequence, or a default value if
+ /// the sequence contains no elements.
+ /// </summary>
- /// <summary>
- /// Returns the first element in a sequence that satisfies a specified condition.
- /// </summary>
+ public static TSource FirstOrDefault<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return source.FirstImpl(Futures<TSource>.Default);
+ }
- public static TSource First<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return First(source.Where(predicate));
- }
+ /// <summary>
+ /// Returns the first element of the sequence that satisfies a
+ /// condition or a default value if no such element is found.
+ /// </summary>
- /// <summary>
- /// Returns the first element of a sequence, or a default value if
- /// the sequence contains no elements.
- /// </summary>
+ public static TSource FirstOrDefault<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return FirstOrDefault(source.Where(predicate));
+ }
- public static TSource FirstOrDefault<TSource>(
- this IEnumerable<TSource> source)
- {
- return source.FirstImpl(Futures<TSource>.Default);
- }
+ /// <summary>
+ /// Base implementation of Last operator.
+ /// </summary>
- /// <summary>
- /// Returns the first element of the sequence that satisfies a
- /// condition or a default value if no such element is found.
- /// </summary>
+ private static TSource LastImpl<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource> empty)
+ {
+ CheckNotNull(source, "source");
- public static TSource FirstOrDefault<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return FirstOrDefault(source.Where(predicate));
- }
+ var list = source as IList<TSource>; // optimized case for lists
+ if (list != null)
+ return list.Count > 0 ? list[list.Count - 1] : empty();
- /// <summary>
- /// Base implementation of Last operator.
- /// </summary>
+ using (var e = source.GetEnumerator())
+ {
+ if (!e.MoveNext())
+ return empty();
- private static TSource LastImpl<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource> empty)
- {
- CheckNotNull(source, "source");
+ var last = e.Current;
+ while (e.MoveNext())
+ last = e.Current;
- var list = source as IList<TSource>; // optimized case for lists
- if (list != null)
- return list.Count > 0 ? list[list.Count - 1] : empty();
+ return last;
+ }
+ }
- using (var e = source.GetEnumerator())
- {
- if (!e.MoveNext())
- return empty();
+ /// <summary>
+ /// Returns the last element of a sequence.
+ /// </summary>
+ public static TSource Last<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return source.LastImpl(Futures<TSource>.Undefined);
+ }
- var last = e.Current;
- while (e.MoveNext())
- last = e.Current;
+ /// <summary>
+ /// Returns the last element of a sequence that satisfies a
+ /// specified condition.
+ /// </summary>
- return last;
- }
- }
+ public static TSource Last<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return Last(source.Where(predicate));
+ }
- /// <summary>
- /// Returns the last element of a sequence.
- /// </summary>
- public static TSource Last<TSource>(
- this IEnumerable<TSource> source)
- {
- return source.LastImpl(Futures<TSource>.Undefined);
- }
+ /// <summary>
+ /// Returns the last element of a sequence, or a default value if
+ /// the sequence contains no elements.
+ /// </summary>
- /// <summary>
- /// Returns the last element of a sequence that satisfies a
- /// specified condition.
- /// </summary>
+ public static TSource LastOrDefault<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return source.LastImpl(Futures<TSource>.Default);
+ }
- public static TSource Last<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return Last(source.Where(predicate));
- }
+ /// <summary>
+ /// Returns the last element of a sequence that satisfies a
+ /// condition or a default value if no such element is found.
+ /// </summary>
- /// <summary>
- /// Returns the last element of a sequence, or a default value if
- /// the sequence contains no elements.
- /// </summary>
+ public static TSource LastOrDefault<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return LastOrDefault(source.Where(predicate));
+ }
- public static TSource LastOrDefault<TSource>(
- this IEnumerable<TSource> source)
- {
- return source.LastImpl(Futures<TSource>.Default);
- }
+ /// <summary>
+ /// Base implementation of Single operator.
+ /// </summary>
- /// <summary>
- /// Returns the last element of a sequence that satisfies a
- /// condition or a default value if no such element is found.
- /// </summary>
+ private static TSource SingleImpl<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource> empty)
+ {
+ CheckNotNull(source, "source");
- public static TSource LastOrDefault<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
+ using (var e = source.GetEnumerator())
+ {
+ if (e.MoveNext())
{
- return LastOrDefault(source.Where(predicate));
+ var single = e.Current;
+ if (!e.MoveNext())
+ return single;
+
+ throw new InvalidOperationException();
}
- /// <summary>
- /// Base implementation of Single operator.
- /// </summary>
-
- private static TSource SingleImpl<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource> empty)
- {
- CheckNotNull(source, "source");
+ return empty();
+ }
+ }
- using (var e = source.GetEnumerator())
- {
- if (e.MoveNext())
- {
- var single = e.Current;
- if (!e.MoveNext())
- return single;
+ /// <summary>
+ /// Returns the only element of a sequence, and throws an exception
+ /// if there is not exactly one element in the sequence.
+ /// </summary>
- throw new InvalidOperationException();
- }
+ public static TSource Single<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return source.SingleImpl(Futures<TSource>.Undefined);
+ }
- return empty();
- }
- }
+ /// <summary>
+ /// Returns the only element of a sequence that satisfies a
+ /// specified condition, and throws an exception if more than one
+ /// such element exists.
+ /// </summary>
- /// <summary>
- /// Returns the only element of a sequence, and throws an exception
- /// if there is not exactly one element in the sequence.
- /// </summary>
+ public static TSource Single<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return Single(source.Where(predicate));
+ }
- public static TSource Single<TSource>(
- this IEnumerable<TSource> source)
- {
- return source.SingleImpl(Futures<TSource>.Undefined);
- }
+ /// <summary>
+ /// Returns the only element of a sequence, or a default value if
+ /// the sequence is empty; this method throws an exception if there
+ /// is more than one element in the sequence.
+ /// </summary>
- /// <summary>
- /// Returns the only element of a sequence that satisfies a
- /// specified condition, and throws an exception if more than one
- /// such element exists.
- /// </summary>
+ public static TSource SingleOrDefault<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return source.SingleImpl(Futures<TSource>.Default);
+ }
- public static TSource Single<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return Single(source.Where(predicate));
- }
+ /// <summary>
+ /// Returns the only element of a sequence that satisfies a
+ /// specified condition or a default value if no such element
+ /// exists; this method throws an exception if more than one element
+ /// satisfies the condition.
+ /// </summary>
- /// <summary>
- /// Returns the only element of a sequence, or a default value if
- /// the sequence is empty; this method throws an exception if there
- /// is more than one element in the sequence.
- /// </summary>
+ public static TSource SingleOrDefault<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return SingleOrDefault(source.Where(predicate));
+ }
- public static TSource SingleOrDefault<TSource>(
- this IEnumerable<TSource> source)
- {
- return source.SingleImpl(Futures<TSource>.Default);
- }
+ /// <summary>
+ /// Returns the element at a specified index in a sequence.
+ /// </summary>
- /// <summary>
- /// Returns the only element of a sequence that satisfies a
- /// specified condition or a default value if no such element
- /// exists; this method throws an exception if more than one element
- /// satisfies the condition.
- /// </summary>
+ public static TSource ElementAt<TSource>(
+ this IEnumerable<TSource> source,
+ int index)
+ {
+ CheckNotNull(source, "source");
+
+ if (index < 0)
+ throw new ArgumentOutOfRangeException("index", index, null);
+
+ var list = source as IList<TSource>;
+ if (list != null)
+ return list[index];
+
+ try
+ {
+ return source.SkipWhile((item, i) => i < index).First();
+ }
+ catch (InvalidOperationException) // if thrown by First
+ {
+ throw new ArgumentOutOfRangeException("index", index, null);
+ }
+ }
- public static TSource SingleOrDefault<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return SingleOrDefault(source.Where(predicate));
- }
+ /// <summary>
+ /// Returns the element at a specified index in a sequence or a
+ /// default value if the index is out of range.
+ /// </summary>
- /// <summary>
- /// Returns the element at a specified index in a sequence.
- /// </summary>
+ public static TSource ElementAtOrDefault<TSource>(
+ this IEnumerable<TSource> source,
+ int index)
+ {
+ CheckNotNull(source, "source");
- public static TSource ElementAt<TSource>(
- this IEnumerable<TSource> source,
- int index)
- {
- CheckNotNull(source, "source");
-
- if (index < 0)
- throw new ArgumentOutOfRangeException("index", index, null);
-
- var list = source as IList<TSource>;
- if (list != null)
- return list[index];
-
- try
- {
- return source.SkipWhile((item, i) => i < index).First();
- }
- catch (InvalidOperationException) // if thrown by First
- {
- throw new ArgumentOutOfRangeException("index", index, null);
- }
- }
+ if (index < 0)
+ return default(TSource);
- /// <summary>
- /// Returns the element at a specified index in a sequence or a
- /// default value if the index is out of range.
- /// </summary>
+ var list = source as IList<TSource>;
+ if (list != null)
+ return index < list.Count ? list[index] : default(TSource);
- public static TSource ElementAtOrDefault<TSource>(
- this IEnumerable<TSource> source,
- int index)
- {
- CheckNotNull(source, "source");
+ return source.SkipWhile((item, i) => i < index).FirstOrDefault();
+ }
- if (index < 0)
- return default(TSource);
+ /// <summary>
+ /// Inverts the order of the elements in a sequence.
+ /// </summary>
- var list = source as IList<TSource>;
- if (list != null)
- return index < list.Count ? list[index] : default(TSource);
+ public static IEnumerable<TSource> Reverse<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ CheckNotNull(source, "source");
- return source.SkipWhile((item, i) => i < index).FirstOrDefault();
- }
+ return ReverseYield(source);
+ }
- /// <summary>
- /// Inverts the order of the elements in a sequence.
- /// </summary>
-
- public static IEnumerable<TSource> Reverse<TSource>(
- this IEnumerable<TSource> source)
- {
- CheckNotNull(source, "source");
+ private static IEnumerable<TSource> ReverseYield<TSource>(IEnumerable<TSource> source)
+ {
+ var stack = new Stack<TSource>();
+ foreach (var item in source)
+ stack.Push(item);
- return ReverseYield(source);
- }
+ foreach (var item in stack)
+ yield return item;
+ }
- private static IEnumerable<TSource> ReverseYield<TSource>(IEnumerable<TSource> source)
- {
- var stack = new Stack<TSource>();
- foreach (var item in source)
- stack.Push(item);
+ /// <summary>
+ /// Returns a specified number of contiguous elements from the start
+ /// of a sequence.
+ /// </summary>
- foreach (var item in stack)
- yield return item;
- }
+ public static IEnumerable<TSource> Take<TSource>(
+ this IEnumerable<TSource> source,
+ int count)
+ {
+ return source.Where((item, i) => i < count);
+ }
- /// <summary>
- /// Returns a specified number of contiguous elements from the start
- /// of a sequence.
- /// </summary>
+ /// <summary>
+ /// Bypasses a specified number of elements in a sequence and then
+ /// returns the remaining elements.
+ /// </summary>
- public static IEnumerable<TSource> Take<TSource>(
- this IEnumerable<TSource> source,
- int count)
- {
- return source.Where((item, i) => i < count);
- }
+ public static IEnumerable<TSource> Skip<TSource>(
+ this IEnumerable<TSource> source,
+ int count)
+ {
+ return source.Where((item, i) => i >= count);
+ }
- /// <summary>
- /// Bypasses a specified number of elements in a sequence and then
- /// returns the remaining elements.
- /// </summary>
+ /// <summary>
+ /// Bypasses elements in a sequence as long as a specified condition
+ /// is true and then returns the remaining elements.
+ /// </summary>
- public static IEnumerable<TSource> Skip<TSource>(
- this IEnumerable<TSource> source,
- int count)
- {
- return source.Where((item, i) => i >= count);
- }
+ public static IEnumerable<TSource> SkipWhile<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ CheckNotNull(predicate, "predicate");
- /// <summary>
- /// Bypasses elements in a sequence as long as a specified condition
- /// is true and then returns the remaining elements.
- /// </summary>
+ return source.SkipWhile((item, i) => predicate(item));
+ }
- public static IEnumerable<TSource> SkipWhile<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- CheckNotNull(predicate, "predicate");
+ /// <summary>
+ /// Bypasses elements in a sequence as long as a specified condition
+ /// is true and then returns the remaining elements. The element's
+ /// index is used in the logic of the predicate function.
+ /// </summary>
- return source.SkipWhile((item, i) => predicate(item));
- }
+ public static IEnumerable<TSource> SkipWhile<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, bool> predicate)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(predicate, "predicate");
- /// <summary>
- /// Bypasses elements in a sequence as long as a specified condition
- /// is true and then returns the remaining elements. The element's
- /// index is used in the logic of the predicate function.
- /// </summary>
+ return SkipWhileYield(source, predicate);
+ }
- public static IEnumerable<TSource> SkipWhile<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int, bool> predicate)
+ private static IEnumerable<TSource> SkipWhileYield<TSource>(
+ IEnumerable<TSource> source,
+ Func<TSource, int, bool> predicate)
+ {
+ using (var e = source.GetEnumerator())
+ {
+ for (var i = 0;; i++)
{
- CheckNotNull(source, "source");
- CheckNotNull(predicate, "predicate");
+ if (!e.MoveNext())
+ yield break;
- return SkipWhileYield(source, predicate);
+ if (!predicate(e.Current, i))
+ break;
}
- private static IEnumerable<TSource> SkipWhileYield<TSource>(
- IEnumerable<TSource> source,
- Func<TSource, int, bool> predicate)
+ do
{
- using (var e = source.GetEnumerator())
- {
- for (var i = 0; ; i++)
- {
- if (!e.MoveNext())
- yield break;
-
- if (!predicate(e.Current, i))
- break;
- }
-
- do { yield return e.Current; } while (e.MoveNext());
- }
- }
+ yield return e.Current;
+ } while (e.MoveNext());
+ }
+ }
- /// <summary>
- /// Returns the number of elements in a sequence.
- /// </summary>
+ /// <summary>
+ /// Returns the number of elements in a sequence.
+ /// </summary>
- public static int Count<TSource>(
- this IEnumerable<TSource> source)
- {
- CheckNotNull(source, "source");
+ public static int Count<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ CheckNotNull(source, "source");
- var collection = source as ICollection;
- return collection != null
- ? collection.Count
- : source.Aggregate(0, (count, item) => checked(count + 1));
- }
+ var collection = source as ICollection;
+ return collection != null
+ ? collection.Count
+ : source.Aggregate(0, (count, item) => checked(count + 1));
+ }
- /// <summary>
- /// Returns a number that represents how many elements in the
- /// specified sequence satisfy a condition.
- /// </summary>
+ /// <summary>
+ /// Returns a number that represents how many elements in the
+ /// specified sequence satisfy a condition.
+ /// </summary>
- public static int Count<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return Count(source.Where(predicate));
- }
+ public static int Count<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return Count(source.Where(predicate));
+ }
- /// <summary>
- /// Returns an <see cref="Int64"/> that represents the total number
- /// of elements in a sequence.
- /// </summary>
+ /// <summary>
+ /// Returns an <see cref="Int64"/> that represents the total number
+ /// of elements in a sequence.
+ /// </summary>
- public static long LongCount<TSource>(
- this IEnumerable<TSource> source)
- {
- CheckNotNull(source, "source");
+ public static long LongCount<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ CheckNotNull(source, "source");
- var array = source as Array;
- return array != null
- ? array.LongLength
- : source.Aggregate(0L, (count, item) => count + 1);
- }
+ var array = source as Array;
+ return array != null
+ ? array.LongLength
+ : source.Aggregate(0L, (count, item) => count + 1);
+ }
- /// <summary>
- /// Returns an <see cref="Int64"/> that represents how many elements
- /// in a sequence satisfy a condition.
- /// </summary>
+ /// <summary>
+ /// Returns an <see cref="Int64"/> that represents how many elements
+ /// in a sequence satisfy a condition.
+ /// </summary>
- public static long LongCount<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return LongCount(source.Where(predicate));
- }
+ public static long LongCount<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return LongCount(source.Where(predicate));
+ }
- /// <summary>
- /// Concatenates two sequences.
- /// </summary>
+ /// <summary>
+ /// Concatenates two sequences.
+ /// </summary>
- public static IEnumerable<TSource> Concat<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second)
- {
- CheckNotNull(first, "first");
- CheckNotNull(second, "second");
+ public static IEnumerable<TSource> Concat<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second)
+ {
+ CheckNotNull(first, "first");
+ CheckNotNull(second, "second");
- return ConcatYield(first, second);
- }
+ return ConcatYield(first, second);
+ }
- private static IEnumerable<TSource> ConcatYield<TSource>(
- IEnumerable<TSource> first,
- IEnumerable<TSource> second)
- {
- foreach (var item in first)
- yield return item;
+ private static IEnumerable<TSource> ConcatYield<TSource>(
+ IEnumerable<TSource> first,
+ IEnumerable<TSource> second)
+ {
+ foreach (var item in first)
+ yield return item;
- foreach (var item in second)
- yield return item;
- }
+ foreach (var item in second)
+ yield return item;
+ }
- /// <summary>
- /// Creates a <see cref="List{T}"/> from an <see cref="IEnumerable{T}"/>.
- /// </summary>
+ /// <summary>
+ /// Creates a <see cref="List{T}"/> from an <see cref="IEnumerable{T}"/>.
+ /// </summary>
- public static List<TSource> ToList<TSource>(
- this IEnumerable<TSource> source)
- {
- CheckNotNull(source, "source");
+ public static List<TSource> ToList<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ CheckNotNull(source, "source");
- return new List<TSource>(source);
- }
+ return new List<TSource>(source);
+ }
- /// <summary>
- /// Creates an array from an <see cref="IEnumerable{T}"/>.
- /// </summary>
+ /// <summary>
+ /// Creates an array from an <see cref="IEnumerable{T}"/>.
+ /// </summary>
- public static TSource[] ToArray<TSource>(
- this IEnumerable<TSource> source)
- {
- return source.ToList().ToArray();
- }
+ public static TSource[] ToArray<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return source.ToList().ToArray();
+ }
- /// <summary>
- /// Returns distinct elements from a sequence by using the default
- /// equality comparer to compare values.
- /// </summary>
+ /// <summary>
+ /// Returns distinct elements from a sequence by using the default
+ /// equality comparer to compare values.
+ /// </summary>
- public static IEnumerable<TSource> Distinct<TSource>(
- this IEnumerable<TSource> source)
- {
- return Distinct(source, /* comparer */ null);
- }
+ public static IEnumerable<TSource> Distinct<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return Distinct(source, /* comparer */ null);
+ }
- /// <summary>
- /// Returns distinct elements from a sequence by using a specified
- /// <see cref="IEqualityComparer{T}"/> to compare values.
- /// </summary>
+ /// <summary>
+ /// Returns distinct elements from a sequence by using a specified
+ /// <see cref="IEqualityComparer{T}"/> to compare values.
+ /// </summary>
- public static IEnumerable<TSource> Distinct<TSource>(
- this IEnumerable<TSource> source,
- IEqualityComparer<TSource> comparer)
- {
- CheckNotNull(source, "source");
+ public static IEnumerable<TSource> Distinct<TSource>(
+ this IEnumerable<TSource> source,
+ IEqualityComparer<TSource> comparer)
+ {
+ CheckNotNull(source, "source");
- return DistinctYield(source, comparer);
- }
+ return DistinctYield(source, comparer);
+ }
- private static IEnumerable<TSource> DistinctYield<TSource>(
- IEnumerable<TSource> source,
- IEqualityComparer<TSource> comparer)
+ private static IEnumerable<TSource> DistinctYield<TSource>(
+ IEnumerable<TSource> source,
+ IEqualityComparer<TSource> comparer)
+ {
+ var set = new Dictionary<TSource, object>(comparer);
+ var gotNull = false;
+
+ foreach (var item in source)
+ {
+ if (item == null)
{
- var set = new Dictionary<TSource, object>(comparer);
- var gotNull = false;
-
- foreach (var item in source)
- {
- if (item == null)
- {
- if (gotNull)
- continue;
- gotNull = true;
- }
- else
- {
- if (set.ContainsKey(item))
- continue;
- set.Add(item, null);
- }
-
- yield return item;
- }
+ if (gotNull)
+ continue;
+ gotNull = true;
}
-
- /// <summary>
- /// Creates a <see cref="Lookup{TKey,TElement}" /> from an
- /// <see cref="IEnumerable{T}" /> according to a specified key
- /// selector function.
- /// </summary>
-
- public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector)
+ else
{
- return ToLookup(source, keySelector, e => e, /* comparer */ null);
+ if (set.ContainsKey(item))
+ continue;
+ set.Add(item, null);
}
- /// <summary>
- /// Creates a <see cref="Lookup{TKey,TElement}" /> from an
- /// <see cref="IEnumerable{T}" /> according to a specified key
- /// selector function and a key comparer.
- /// </summary>
+ yield return item;
+ }
+ }
- public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- IEqualityComparer<TKey> comparer)
- {
- return ToLookup(source, keySelector, e => e, comparer);
- }
+ /// <summary>
+ /// Creates a <see cref="Lookup{TKey,TElement}" /> from an
+ /// <see cref="IEnumerable{T}" /> according to a specified key
+ /// selector function.
+ /// </summary>
- /// <summary>
- /// Creates a <see cref="Lookup{TKey,TElement}" /> from an
- /// <see cref="IEnumerable{T}" /> according to specified key
- /// and element selector functions.
- /// </summary>
+ public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector)
+ {
+ return ToLookup(source, keySelector, e => e, /* comparer */ null);
+ }
- public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TSource, TElement> elementSelector)
- {
- return ToLookup(source, keySelector, elementSelector, /* comparer */ null);
- }
+ /// <summary>
+ /// Creates a <see cref="Lookup{TKey,TElement}" /> from an
+ /// <see cref="IEnumerable{T}" /> according to a specified key
+ /// selector function and a key comparer.
+ /// </summary>
- /// <summary>
- /// Creates a <see cref="Lookup{TKey,TElement}" /> from an
- /// <see cref="IEnumerable{T}" /> according to a specified key
- /// selector function, a comparer and an element selector function.
- /// </summary>
-
- public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TSource, TElement> elementSelector,
- IEqualityComparer<TKey> comparer)
- {
- CheckNotNull(source, "source");
- CheckNotNull(keySelector, "keySelector");
- CheckNotNull(elementSelector, "elementSelector");
-
- var lookup = new Lookup<TKey, TElement>(comparer);
-
- foreach (var item in source)
- {
- var key = keySelector(item);
-
- var grouping = (Grouping<TKey, TElement>) lookup.Find(key);
- if (grouping == null)
- {
- grouping = new Grouping<TKey, TElement>(key);
- lookup.Add(grouping);
- }
-
- grouping.Add(elementSelector(item));
- }
-
- return lookup;
- }
+ public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ IEqualityComparer<TKey> comparer)
+ {
+ return ToLookup(source, keySelector, e => e, comparer);
+ }
- /// <summary>
- /// Groups the elements of a sequence according to a specified key
- /// selector function.
- /// </summary>
+ /// <summary>
+ /// Creates a <see cref="Lookup{TKey,TElement}" /> from an
+ /// <see cref="IEnumerable{T}" /> according to specified key
+ /// and element selector functions.
+ /// </summary>
- public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector)
- {
- return GroupBy(source, keySelector, /* comparer */ null);
- }
+ public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TSource, TElement> elementSelector)
+ {
+ return ToLookup(source, keySelector, elementSelector, /* comparer */ null);
+ }
- /// <summary>
- /// Groups the elements of a sequence according to a specified key
- /// selector function and compares the keys by using a specified
- /// comparer.
- /// </summary>
+ /// <summary>
+ /// Creates a <see cref="Lookup{TKey,TElement}" /> from an
+ /// <see cref="IEnumerable{T}" /> according to a specified key
+ /// selector function, a comparer and an element selector function.
+ /// </summary>
- public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- IEqualityComparer<TKey> comparer)
- {
- return GroupBy(source, keySelector, e => e, comparer);
- }
+ public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TSource, TElement> elementSelector,
+ IEqualityComparer<TKey> comparer)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(keySelector, "keySelector");
+ CheckNotNull(elementSelector, "elementSelector");
+
+ var lookup = new Lookup<TKey, TElement>(comparer);
- /// <summary>
- /// Groups the elements of a sequence according to a specified key
- /// selector function and projects the elements for each group by
- /// using a specified function.
- /// </summary>
+ foreach (var item in source)
+ {
+ var key = keySelector(item);
- public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TSource, TElement> elementSelector)
+ var grouping = (Grouping<TKey, TElement>) lookup.Find(key);
+ if (grouping == null)
{
- return GroupBy(source, keySelector, elementSelector, /* comparer */ null);
+ grouping = new Grouping<TKey, TElement>(key);
+ lookup.Add(grouping);
}
- /// <summary>
- /// Groups the elements of a sequence according to a specified key
- /// selector function and creates a result value from each group and
- /// its key.
- /// </summary>
-
- public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TSource, TElement> elementSelector,
- IEqualityComparer<TKey> comparer)
- {
- CheckNotNull(source, "source");
- CheckNotNull(keySelector, "keySelector");
- CheckNotNull(elementSelector, "elementSelector");
+ grouping.Add(elementSelector(item));
+ }
- return ToLookup(source, keySelector, elementSelector, comparer);
- }
+ return lookup;
+ }
- /// <summary>
- /// Groups the elements of a sequence according to a key selector
- /// function. The keys are compared by using a comparer and each
- /// group's elements are projected by using a specified function.
- /// </summary>
+ /// <summary>
+ /// Groups the elements of a sequence according to a specified key
+ /// selector function.
+ /// </summary>
- public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TKey, IEnumerable<TSource>, TResult> resultSelector)
- {
- return GroupBy(source, keySelector, resultSelector, /* comparer */ null);
- }
+ public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector)
+ {
+ return GroupBy(source, keySelector, /* comparer */ null);
+ }
- /// <summary>
- /// Groups the elements of a sequence according to a specified key
- /// selector function and creates a result value from each group and
- /// its key. The elements of each group are projected by using a
- /// specified function.
- /// </summary>
-
- public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TKey, IEnumerable<TSource>, TResult> resultSelector,
- IEqualityComparer<TKey> comparer)
- {
- CheckNotNull(source, "source");
- CheckNotNull(keySelector, "keySelector");
- CheckNotNull(resultSelector, "resultSelector");
+ /// <summary>
+ /// Groups the elements of a sequence according to a specified key
+ /// selector function and compares the keys by using a specified
+ /// comparer.
+ /// </summary>
- return ToLookup(source, keySelector, comparer).Select(g => resultSelector(g.Key, g));
- }
+ public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ IEqualityComparer<TKey> comparer)
+ {
+ return GroupBy(source, keySelector, e => e, comparer);
+ }
- /// <summary>
- /// Groups the elements of a sequence according to a specified key
- /// selector function and creates a result value from each group and
- /// its key. The keys are compared by using a specified comparer.
- /// </summary>
-
- public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TSource, TElement> elementSelector,
- Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
- {
- return GroupBy(source, keySelector, elementSelector, resultSelector, /* comparer */ null);
- }
+ /// <summary>
+ /// Groups the elements of a sequence according to a specified key
+ /// selector function and projects the elements for each group by
+ /// using a specified function.
+ /// </summary>
- /// <summary>
- /// Groups the elements of a sequence according to a specified key
- /// selector function and creates a result value from each group and
- /// its key. Key values are compared by using a specified comparer,
- /// and the elements of each group are projected by using a
- /// specified function.
- /// </summary>
-
- public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TSource, TElement> elementSelector,
- Func<TKey, IEnumerable<TElement>, TResult> resultSelector,
- IEqualityComparer<TKey> comparer)
- {
- CheckNotNull(source, "source");
- CheckNotNull(keySelector, "keySelector");
- CheckNotNull(elementSelector, "elementSelector");
- CheckNotNull(resultSelector, "resultSelector");
+ public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TSource, TElement> elementSelector)
+ {
+ return GroupBy(source, keySelector, elementSelector, /* comparer */ null);
+ }
- return ToLookup(source, keySelector, elementSelector, comparer)
- .Select(g => resultSelector(g.Key, g));
- }
+ /// <summary>
+ /// Groups the elements of a sequence according to a specified key
+ /// selector function and creates a result value from each group and
+ /// its key.
+ /// </summary>
- /// <summary>
- /// Applies an accumulator function over a sequence.
- /// </summary>
+ public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TSource, TElement> elementSelector,
+ IEqualityComparer<TKey> comparer)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(keySelector, "keySelector");
+ CheckNotNull(elementSelector, "elementSelector");
- public static TSource Aggregate<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, TSource, TSource> func)
- {
- CheckNotNull(source, "source");
- CheckNotNull(func, "func");
+ return ToLookup(source, keySelector, elementSelector, comparer);
+ }
- using (var e = source.GetEnumerator())
- {
- if (!e.MoveNext())
- throw new InvalidOperationException();
+ /// <summary>
+ /// Groups the elements of a sequence according to a key selector
+ /// function. The keys are compared by using a comparer and each
+ /// group's elements are projected by using a specified function.
+ /// </summary>
- return e.Renumerable().Skip(1).Aggregate(e.Current, func);
- }
- }
+ public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TKey, IEnumerable<TSource>, TResult> resultSelector)
+ {
+ return GroupBy(source, keySelector, resultSelector, /* comparer */ null);
+ }
- /// <summary>
- /// Applies an accumulator function over a sequence. The specified
- /// seed value is used as the initial accumulator value.
- /// </summary>
+ /// <summary>
+ /// Groups the elements of a sequence according to a specified key
+ /// selector function and creates a result value from each group and
+ /// its key. The elements of each group are projected by using a
+ /// specified function.
+ /// </summary>
- public static TAccumulate Aggregate<TSource, TAccumulate>(
- this IEnumerable<TSource> source,
- TAccumulate seed,
- Func<TAccumulate, TSource, TAccumulate> func)
- {
- return Aggregate(source, seed, func, r => r);
- }
+ public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TKey, IEnumerable<TSource>, TResult> resultSelector,
+ IEqualityComparer<TKey> comparer)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(keySelector, "keySelector");
+ CheckNotNull(resultSelector, "resultSelector");
- /// <summary>
- /// Applies an accumulator function over a sequence. The specified
- /// seed value is used as the initial accumulator value, and the
- /// specified function is used to select the result value.
- /// </summary>
-
- public static TResult Aggregate<TSource, TAccumulate, TResult>(
- this IEnumerable<TSource> source,
- TAccumulate seed,
- Func<TAccumulate, TSource, TAccumulate> func,
- Func<TAccumulate, TResult> resultSelector)
- {
- CheckNotNull(source, "source");
- CheckNotNull(func, "func");
- CheckNotNull(resultSelector, "resultSelector");
+ return ToLookup(source, keySelector, comparer).Select(g => resultSelector(g.Key, g));
+ }
- var result = seed;
+ /// <summary>
+ /// Groups the elements of a sequence according to a specified key
+ /// selector function and creates a result value from each group and
+ /// its key. The keys are compared by using a specified comparer.
+ /// </summary>
- foreach (var item in source)
- result = func(result, item);
+ public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TSource, TElement> elementSelector,
+ Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
+ {
+ return GroupBy(source, keySelector, elementSelector, resultSelector, /* comparer */ null);
+ }
- return resultSelector(result);
- }
+ /// <summary>
+ /// Groups the elements of a sequence according to a specified key
+ /// selector function and creates a result value from each group and
+ /// its key. Key values are compared by using a specified comparer,
+ /// and the elements of each group are projected by using a
+ /// specified function.
+ /// </summary>
- /// <summary>
- /// Produces the set union of two sequences by using the default
- /// equality comparer.
- /// </summary>
+ public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TSource, TElement> elementSelector,
+ Func<TKey, IEnumerable<TElement>, TResult> resultSelector,
+ IEqualityComparer<TKey> comparer)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(keySelector, "keySelector");
+ CheckNotNull(elementSelector, "elementSelector");
+ CheckNotNull(resultSelector, "resultSelector");
- public static IEnumerable<TSource> Union<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second)
- {
- return Union(first, second, /* comparer */ null);
- }
+ return ToLookup(source, keySelector, elementSelector, comparer)
+ .Select(g => resultSelector(g.Key, g));
+ }
- /// <summary>
- /// Produces the set union of two sequences by using a specified
- /// <see cref="IEqualityComparer{T}" />.
- /// </summary>
+ /// <summary>
+ /// Applies an accumulator function over a sequence.
+ /// </summary>
- public static IEnumerable<TSource> Union<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second,
- IEqualityComparer<TSource> comparer)
- {
- return first.Concat(second).Distinct(comparer);
- }
+ public static TSource Aggregate<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TSource, TSource> func)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(func, "func");
- /// <summary>
- /// Returns the elements of the specified sequence or the type
- /// parameter's default value in a singleton collection if the
- /// sequence is empty.
- /// </summary>
+ using (var e = source.GetEnumerator())
+ {
+ if (!e.MoveNext())
+ throw new InvalidOperationException();
- public static IEnumerable<TSource> DefaultIfEmpty<TSource>(
- this IEnumerable<TSource> source)
- {
- return source.DefaultIfEmpty(default(TSource));
- }
+ return e.Renumerable().Skip(1).Aggregate(e.Current, func);
+ }
+ }
- /// <summary>
- /// Returns the elements of the specified sequence or the specified
- /// value in a singleton collection if the sequence is empty.
- /// </summary>
-
- public static IEnumerable<TSource> DefaultIfEmpty<TSource>(
- this IEnumerable<TSource> source,
- TSource defaultValue)
- {
- CheckNotNull(source, "source");
+ /// <summary>
+ /// Applies an accumulator function over a sequence. The specified
+ /// seed value is used as the initial accumulator value.
+ /// </summary>
- return DefaultIfEmptyYield(source, defaultValue);
- }
+ public static TAccumulate Aggregate<TSource, TAccumulate>(
+ this IEnumerable<TSource> source,
+ TAccumulate seed,
+ Func<TAccumulate, TSource, TAccumulate> func)
+ {
+ return Aggregate(source, seed, func, r => r);
+ }
- private static IEnumerable<TSource> DefaultIfEmptyYield<TSource>(
- IEnumerable<TSource> source,
- TSource defaultValue)
- {
- using (var e = source.GetEnumerator())
- {
- if (!e.MoveNext())
- yield return defaultValue;
- else
- do { yield return e.Current; } while (e.MoveNext());
- }
- }
+ /// <summary>
+ /// Applies an accumulator function over a sequence. The specified
+ /// seed value is used as the initial accumulator value, and the
+ /// specified function is used to select the result value.
+ /// </summary>
- /// <summary>
- /// Determines whether all elements of a sequence satisfy a condition.
- /// </summary>
+ public static TResult Aggregate<TSource, TAccumulate, TResult>(
+ this IEnumerable<TSource> source,
+ TAccumulate seed,
+ Func<TAccumulate, TSource, TAccumulate> func,
+ Func<TAccumulate, TResult> resultSelector)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(func, "func");
+ CheckNotNull(resultSelector, "resultSelector");
- public static bool All<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- CheckNotNull(source, "source");
- CheckNotNull(predicate, "predicate");
+ var result = seed;
- foreach (var item in source)
- if (!predicate(item))
- return false;
+ foreach (var item in source)
+ result = func(result, item);
- return true;
- }
+ return resultSelector(result);
+ }
- /// <summary>
- /// Determines whether a sequence contains any elements.
- /// </summary>
+ /// <summary>
+ /// Produces the set union of two sequences by using the default
+ /// equality comparer.
+ /// </summary>
- public static bool Any<TSource>(
- this IEnumerable<TSource> source)
- {
- CheckNotNull(source, "source");
+ public static IEnumerable<TSource> Union<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second)
+ {
+ return Union(first, second, /* comparer */ null);
+ }
- using (var e = source.GetEnumerator())
- return e.MoveNext();
- }
+ /// <summary>
+ /// Produces the set union of two sequences by using a specified
+ /// <see cref="IEqualityComparer{T}" />.
+ /// </summary>
- /// <summary>
- /// Determines whether any element of a sequence satisfies a
- /// condition.
- /// </summary>
+ public static IEnumerable<TSource> Union<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second,
+ IEqualityComparer<TSource> comparer)
+ {
+ return first.Concat(second).Distinct(comparer);
+ }
- public static bool Any<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return source.Where(predicate).Any();
- }
+ /// <summary>
+ /// Returns the elements of the specified sequence or the type
+ /// parameter's default value in a singleton collection if the
+ /// sequence is empty.
+ /// </summary>
- /// <summary>
- /// Determines whether a sequence contains a specified element by
- /// using the default equality comparer.
- /// </summary>
+ public static IEnumerable<TSource> DefaultIfEmpty<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return source.DefaultIfEmpty(default(TSource));
+ }
- public static bool Contains<TSource>(
- this IEnumerable<TSource> source,
- TSource value)
- {
- return source.Contains(value, /* comparer */ null);
- }
+ /// <summary>
+ /// Returns the elements of the specified sequence or the specified
+ /// value in a singleton collection if the sequence is empty.
+ /// </summary>
- /// <summary>
- /// Determines whether a sequence contains a specified element by
- /// using a specified <see cref="IEqualityComparer{T}" />.
- /// </summary>
+ public static IEnumerable<TSource> DefaultIfEmpty<TSource>(
+ this IEnumerable<TSource> source,
+ TSource defaultValue)
+ {
+ CheckNotNull(source, "source");
- public static bool Contains<TSource>(
- this IEnumerable<TSource> source,
- TSource value,
- IEqualityComparer<TSource> comparer)
- {
- CheckNotNull(source, "source");
+ return DefaultIfEmptyYield(source, defaultValue);
+ }
- if (comparer == null)
- {
- var collection = source as ICollection<TSource>;
- if (collection != null)
- return collection.Contains(value);
- }
+ private static IEnumerable<TSource> DefaultIfEmptyYield<TSource>(
+ IEnumerable<TSource> source,
+ TSource defaultValue)
+ {
+ using (var e = source.GetEnumerator())
+ {
+ if (!e.MoveNext())
+ yield return defaultValue;
+ else
+ do
+ {
+ yield return e.Current;
+ } while (e.MoveNext());
+ }
+ }
- comparer = comparer ?? EqualityComparer<TSource>.Default;
- return source.Any(item => comparer.Equals(item, value));
- }
+ /// <summary>
+ /// Determines whether all elements of a sequence satisfy a condition.
+ /// </summary>
- /// <summary>
- /// Determines whether two sequences are equal by comparing the
- /// elements by using the default equality comparer for their type.
- /// </summary>
+ public static bool All<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(predicate, "predicate");
- public static bool SequenceEqual<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second)
- {
- return first.SequenceEqual(second, /* comparer */ null);
- }
+ foreach (var item in source)
+ if (!predicate(item))
+ return false;
- /// <summary>
- /// Determines whether two sequences are equal by comparing their
- /// elements by using a specified <see cref="IEqualityComparer{T}" />.
- /// </summary>
+ return true;
+ }
- public static bool SequenceEqual<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second,
- IEqualityComparer<TSource> comparer)
- {
- CheckNotNull(first, "frist");
- CheckNotNull(second, "second");
+ /// <summary>
+ /// Determines whether a sequence contains any elements.
+ /// </summary>
- comparer = comparer ?? EqualityComparer<TSource>.Default;
+ public static bool Any<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ CheckNotNull(source, "source");
- using (IEnumerator<TSource> lhs = first.GetEnumerator(),
- rhs = second.GetEnumerator())
- {
- do
- {
- if (!lhs.MoveNext())
- return !rhs.MoveNext();
+ using (var e = source.GetEnumerator())
+ return e.MoveNext();
+ }
- if (!rhs.MoveNext())
- return false;
- }
- while (comparer.Equals(lhs.Current, rhs.Current));
- }
+ /// <summary>
+ /// Determines whether any element of a sequence satisfies a
+ /// condition.
+ /// </summary>
- return false;
- }
+ public static bool Any<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return source.Where(predicate).Any();
+ }
- /// <summary>
- /// Base implementation for Min/Max operator.
- /// </summary>
+ /// <summary>
+ /// Determines whether a sequence contains a specified element by
+ /// using the default equality comparer.
+ /// </summary>
- private static TSource MinMaxImpl<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, TSource, bool> lesser)
- {
- CheckNotNull(source, "source");
- Debug.Assert(lesser != null);
+ public static bool Contains<TSource>(
+ this IEnumerable<TSource> source,
+ TSource value)
+ {
+ return source.Contains(value, /* comparer */ null);
+ }
- return source.Aggregate((a, item) => lesser(a, item) ? a : item);
- }
+ /// <summary>
+ /// Determines whether a sequence contains a specified element by
+ /// using a specified <see cref="IEqualityComparer{T}" />.
+ /// </summary>
- /// <summary>
- /// Base implementation for Min/Max operator for nullable types.
- /// </summary>
+ public static bool Contains<TSource>(
+ this IEnumerable<TSource> source,
+ TSource value,
+ IEqualityComparer<TSource> comparer)
+ {
+ CheckNotNull(source, "source");
- private static TSource? MinMaxImpl<TSource>(
- this IEnumerable<TSource?> source,
- TSource? seed, Func<TSource?, TSource?, bool> lesser) where TSource : struct
- {
- CheckNotNull(source, "source");
- Debug.Assert(lesser != null);
+ if (comparer == null)
+ {
+ var collection = source as ICollection<TSource>;
+ if (collection != null)
+ return collection.Contains(value);
+ }
- return source.Aggregate(seed, (a, item) => lesser(a, item) ? a : item);
- // == MinMaxImpl(Repeat<TSource?>(null, 1).Concat(source), lesser);
- }
+ comparer = comparer ?? EqualityComparer<TSource>.Default;
+ return source.Any(item => comparer.Equals(item, value));
+ }
- /// <summary>
- /// Returns the minimum value in a generic sequence.
- /// </summary>
+ /// <summary>
+ /// Determines whether two sequences are equal by comparing the
+ /// elements by using the default equality comparer for their type.
+ /// </summary>
- public static TSource Min<TSource>(
- this IEnumerable<TSource> source)
- {
- var comparer = Comparer<TSource>.Default;
- return source.MinMaxImpl((x, y) => comparer.Compare(x, y) < 0);
- }
+ public static bool SequenceEqual<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second)
+ {
+ return first.SequenceEqual(second, /* comparer */ null);
+ }
- /// <summary>
- /// Invokes a transform function on each element of a generic
- /// sequence and returns the minimum resulting value.
- /// </summary>
+ /// <summary>
+ /// Determines whether two sequences are equal by comparing their
+ /// elements by using a specified <see cref="IEqualityComparer{T}" />.
+ /// </summary>
- public static TResult Min<TSource, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, TResult> selector)
- {
- return source.Select(selector).Min();
- }
+ public static bool SequenceEqual<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second,
+ IEqualityComparer<TSource> comparer)
+ {
+ CheckNotNull(first, "frist");
+ CheckNotNull(second, "second");
- /// <summary>
- /// Returns the maximum value in a generic sequence.
- /// </summary>
+ comparer = comparer ?? EqualityComparer<TSource>.Default;
- public static TSource Max<TSource>(
- this IEnumerable<TSource> source)
+ using (IEnumerator<TSource> lhs = first.GetEnumerator(),
+ rhs = second.GetEnumerator())
+ {
+ do
{
- var comparer = Comparer<TSource>.Default;
- return source.MinMaxImpl((x, y) => comparer.Compare(x, y) > 0);
- }
+ if (!lhs.MoveNext())
+ return !rhs.MoveNext();
- /// <summary>
- /// Invokes a transform function on each element of a generic
- /// sequence and returns the maximum resulting value.
- /// </summary>
+ if (!rhs.MoveNext())
+ return false;
+ } while (comparer.Equals(lhs.Current, rhs.Current));
+ }
- public static TResult Max<TSource, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, TResult> selector)
- {
- return source.Select(selector).Max();
- }
+ return false;
+ }
- /// <summary>
- /// Makes an enumerator seen as enumerable once more.
- /// </summary>
- /// <remarks>
- /// The supplied enumerator must have been started. The first element
- /// returned is the element the enumerator was on when passed in.
- /// DO NOT use this method if the caller must be a generator. It is
- /// mostly safe among aggregate operations.
- /// </remarks>
-
- private static IEnumerable<T> Renumerable<T>(this IEnumerator<T> e)
- {
- Debug.Assert(e != null);
+ /// <summary>
+ /// Base implementation for Min/Max operator.
+ /// </summary>
- do { yield return e.Current; } while (e.MoveNext());
- }
+ private static TSource MinMaxImpl<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TSource, bool> lesser)
+ {
+ CheckNotNull(source, "source");
+ Debug.Assert(lesser != null);
- /// <summary>
- /// Sorts the elements of a sequence in ascending order according to a key.
- /// </summary>
+ return source.Aggregate((a, item) => lesser(a, item) ? a : item);
+ }
- public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector)
- {
- return source.OrderBy(keySelector, /* comparer */ null);
- }
+ /// <summary>
+ /// Base implementation for Min/Max operator for nullable types.
+ /// </summary>
- /// <summary>
- /// Sorts the elements of a sequence in ascending order by using a
- /// specified comparer.
- /// </summary>
+ private static TSource? MinMaxImpl<TSource>(
+ this IEnumerable<TSource?> source,
+ TSource? seed, Func<TSource?, TSource?, bool> lesser) where TSource : struct
+ {
+ CheckNotNull(source, "source");
+ Debug.Assert(lesser != null);
- public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- IComparer<TKey> comparer)
- {
- CheckNotNull(source, "source");
- CheckNotNull(keySelector, "keySelector");
+ return source.Aggregate(seed, (a, item) => lesser(a, item) ? a : item);
+ // == MinMaxImpl(Repeat<TSource?>(null, 1).Concat(source), lesser);
+ }
- return new OrderedEnumerable<TSource, TKey>(source, keySelector, comparer, /* descending */ false);
- }
+ /// <summary>
+ /// Returns the minimum value in a generic sequence.
+ /// </summary>
- /// <summary>
- /// Sorts the elements of a sequence in descending order according to a key.
- /// </summary>
+ public static TSource Min<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ var comparer = Comparer<TSource>.Default;
+ return source.MinMaxImpl((x, y) => comparer.Compare(x, y) < 0);
+ }
- public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector)
- {
- return source.OrderByDescending(keySelector, /* comparer */ null);
- }
+ /// <summary>
+ /// Invokes a transform function on each element of a generic
+ /// sequence and returns the minimum resulting value.
+ /// </summary>
- /// <summary>
- /// Sorts the elements of a sequence in descending order by using a
- /// specified comparer.
- /// </summary>
+ public static TResult Min<TSource, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TResult> selector)
+ {
+ return source.Select(selector).Min();
+ }
- public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- IComparer<TKey> comparer)
- {
- CheckNotNull(source, "source");
- CheckNotNull(source, "keySelector");
+ /// <summary>
+ /// Returns the maximum value in a generic sequence.
+ /// </summary>
- return new OrderedEnumerable<TSource, TKey>(source, keySelector, comparer, /* descending */ true);
- }
+ public static TSource Max<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ var comparer = Comparer<TSource>.Default;
+ return source.MinMaxImpl((x, y) => comparer.Compare(x, y) > 0);
+ }
- /// <summary>
- /// Performs a subsequent ordering of the elements in a sequence in
- /// ascending order according to a key.
- /// </summary>
+ /// <summary>
+ /// Invokes a transform function on each element of a generic
+ /// sequence and returns the maximum resulting value.
+ /// </summary>
- public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(
- this IOrderedEnumerable<TSource> source,
- Func<TSource, TKey> keySelector)
- {
- return source.ThenBy(keySelector, /* comparer */ null);
- }
+ public static TResult Max<TSource, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TResult> selector)
+ {
+ return source.Select(selector).Max();
+ }
- /// <summary>
- /// Performs a subsequent ordering of the elements in a sequence in
- /// ascending order by using a specified comparer.
- /// </summary>
+ /// <summary>
+ /// Makes an enumerator seen as enumerable once more.
+ /// </summary>
+ /// <remarks>
+ /// The supplied enumerator must have been started. The first element
+ /// returned is the element the enumerator was on when passed in.
+ /// DO NOT use this method if the caller must be a generator. It is
+ /// mostly safe among aggregate operations.
+ /// </remarks>
- public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(
- this IOrderedEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- IComparer<TKey> comparer)
- {
- CheckNotNull(source, "source");
+ private static IEnumerable<T> Renumerable<T>(this IEnumerator<T> e)
+ {
+ Debug.Assert(e != null);
- return source.CreateOrderedEnumerable(keySelector, comparer, /* descending */ false);
- }
+ do
+ {
+ yield return e.Current;
+ } while (e.MoveNext());
+ }
- /// <summary>
- /// Performs a subsequent ordering of the elements in a sequence in
- /// descending order, according to a key.
- /// </summary>
+ /// <summary>
+ /// Sorts the elements of a sequence in ascending order according to a key.
+ /// </summary>
- public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(
- this IOrderedEnumerable<TSource> source,
- Func<TSource, TKey> keySelector)
- {
- return source.ThenByDescending(keySelector, /* comparer */ null);
- }
+ public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector)
+ {
+ return source.OrderBy(keySelector, /* comparer */ null);
+ }
- /// <summary>
- /// Performs a subsequent ordering of the elements in a sequence in
- /// descending order by using a specified comparer.
- /// </summary>
+ /// <summary>
+ /// Sorts the elements of a sequence in ascending order by using a
+ /// specified comparer.
+ /// </summary>
- public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(
- this IOrderedEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- IComparer<TKey> comparer)
- {
- CheckNotNull(source, "source");
+ public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ IComparer<TKey> comparer)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(keySelector, "keySelector");
- return source.CreateOrderedEnumerable(keySelector, comparer, /* descending */ true);
- }
+ return new OrderedEnumerable<TSource, TKey>(source, keySelector, comparer, /* descending */ false);
+ }
- /// <summary>
- /// Base implementation for Intersect and Except operators.
- /// </summary>
+ /// <summary>
+ /// Sorts the elements of a sequence in descending order according to a key.
+ /// </summary>
- private static IEnumerable<TSource> IntersectExceptImpl<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second,
- IEqualityComparer<TSource> comparer,
- bool flag)
- {
- CheckNotNull(first, "first");
- CheckNotNull(second, "second");
+ public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector)
+ {
+ return source.OrderByDescending(keySelector, /* comparer */ null);
+ }
- var keys = new List<TSource>();
- var flags = new Dictionary<TSource, bool>(comparer);
+ /// <summary>
+ /// Sorts the elements of a sequence in descending order by using a
+ /// specified comparer.
+ /// </summary>
- foreach (var item in first.Where(k => !flags.ContainsKey(k)))
- {
- flags.Add(item, !flag);
- keys.Add(item);
- }
+ public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ IComparer<TKey> comparer)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(source, "keySelector");
- foreach (var item in second.Where(flags.ContainsKey))
- flags[item] = flag;
+ return new OrderedEnumerable<TSource, TKey>(source, keySelector, comparer, /* descending */ true);
+ }
- //
- // As per docs, "the marked elements are yielded in the order in
- // which they were collected.
- //
+ /// <summary>
+ /// Performs a subsequent ordering of the elements in a sequence in
+ /// ascending order according to a key.
+ /// </summary>
- return keys.Where(item => flags[item]);
- }
+ public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(
+ this IOrderedEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector)
+ {
+ return source.ThenBy(keySelector, /* comparer */ null);
+ }
- /// <summary>
- /// Produces the set intersection of two sequences by using the
- /// default equality comparer to compare values.
- /// </summary>
+ /// <summary>
+ /// Performs a subsequent ordering of the elements in a sequence in
+ /// ascending order by using a specified comparer.
+ /// </summary>
- public static IEnumerable<TSource> Intersect<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second)
- {
- return first.Intersect(second, /* comparer */ null);
- }
+ public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(
+ this IOrderedEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ IComparer<TKey> comparer)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Produces the set intersection of two sequences by using the
- /// specified <see cref="IEqualityComparer{T}" /> to compare values.
- /// </summary>
+ return source.CreateOrderedEnumerable(keySelector, comparer, /* descending */ false);
+ }
- public static IEnumerable<TSource> Intersect<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second,
- IEqualityComparer<TSource> comparer)
- {
- return IntersectExceptImpl(first, second, comparer, /* flag */ true);
- }
+ /// <summary>
+ /// Performs a subsequent ordering of the elements in a sequence in
+ /// descending order, according to a key.
+ /// </summary>
- /// <summary>
- /// Produces the set difference of two sequences by using the
- /// default equality comparer to compare values.
- /// </summary>
+ public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(
+ this IOrderedEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector)
+ {
+ return source.ThenByDescending(keySelector, /* comparer */ null);
+ }
- public static IEnumerable<TSource> Except<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second)
- {
- return first.Except(second, /* comparer */ null);
- }
+ /// <summary>
+ /// Performs a subsequent ordering of the elements in a sequence in
+ /// descending order by using a specified comparer.
+ /// </summary>
- /// <summary>
- /// Produces the set difference of two sequences by using the
- /// specified <see cref="IEqualityComparer{T}" /> to compare values.
- /// </summary>
+ public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(
+ this IOrderedEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ IComparer<TKey> comparer)
+ {
+ CheckNotNull(source, "source");
- public static IEnumerable<TSource> Except<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second,
- IEqualityComparer<TSource> comparer)
- {
- return IntersectExceptImpl(first, second, comparer, /* flag */ false);
- }
+ return source.CreateOrderedEnumerable(keySelector, comparer, /* descending */ true);
+ }
- /// <summary>
- /// Creates a <see cref="Dictionary{TKey,TValue}" /> from an
- /// <see cref="IEnumerable{T}" /> according to a specified key
- /// selector function.
- /// </summary>
+ /// <summary>
+ /// Base implementation for Intersect and Except operators.
+ /// </summary>
- public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector)
- {
- return source.ToDictionary(keySelector, /* comparer */ null);
- }
+ private static IEnumerable<TSource> IntersectExceptImpl<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second,
+ IEqualityComparer<TSource> comparer,
+ bool flag)
+ {
+ CheckNotNull(first, "first");
+ CheckNotNull(second, "second");
- /// <summary>
- /// Creates a <see cref="Dictionary{TKey,TValue}" /> from an
- /// <see cref="IEnumerable{T}" /> according to a specified key
- /// selector function and key comparer.
- /// </summary>
+ var keys = new List<TSource>();
+ var flags = new Dictionary<TSource, bool>(comparer);
- public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- IEqualityComparer<TKey> comparer)
- {
- return source.ToDictionary(keySelector, e => e);
- }
+ foreach (var item in first.Where(k => !flags.ContainsKey(k)))
+ {
+ flags.Add(item, !flag);
+ keys.Add(item);
+ }
- /// <summary>
- /// Creates a <see cref="Dictionary{TKey,TValue}" /> from an
- /// <see cref="IEnumerable{T}" /> according to specified key
- /// selector and element selector functions.
- /// </summary>
-
- public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TSource, TElement> elementSelector)
- {
- return source.ToDictionary(keySelector, elementSelector, /* comparer */ null);
- }
+ foreach (var item in second.Where(flags.ContainsKey))
+ flags[item] = flag;
- /// <summary>
- /// Creates a <see cref="Dictionary{TKey,TValue}" /> from an
- /// <see cref="IEnumerable{T}" /> according to a specified key
- /// selector function, a comparer, and an element selector function.
- /// </summary>
-
- public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TSource, TElement> elementSelector,
- IEqualityComparer<TKey> comparer)
- {
- CheckNotNull(source, "source");
- CheckNotNull(keySelector, "keySelector");
- CheckNotNull(elementSelector, "elementSelector");
-
- var dict = new Dictionary<TKey, TElement>(comparer);
-
- foreach (var item in source)
- {
- //
- // ToDictionary is meant to throw ArgumentNullException if
- // keySelector produces a key that is null and
- // Argument exception if keySelector produces duplicate keys
- // for two elements. Incidentally, the doucmentation for
- // IDictionary<TKey, TValue>.Add says that the Add method
- // throws the same exceptions under the same circumstances
- // so we don't need to do any additional checking or work
- // here and let the Add implementation do all the heavy
- // lifting.
- //
-
- dict.Add(keySelector(item), elementSelector(item));
- }
-
- return dict;
- }
+ //
+ // As per docs, "the marked elements are yielded in the order in
+ // which they were collected.
+ //
- /// <summary>
- /// Correlates the elements of two sequences based on matching keys.
- /// The default equality comparer is used to compare keys.
- /// </summary>
-
- public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(
- this IEnumerable<TOuter> outer,
- IEnumerable<TInner> inner,
- Func<TOuter, TKey> outerKeySelector,
- Func<TInner, TKey> innerKeySelector,
- Func<TOuter, TInner, TResult> resultSelector)
- {
- return outer.Join(inner, outerKeySelector, innerKeySelector, resultSelector, /* comparer */ null);
- }
+ return keys.Where(item => flags[item]);
+ }
- /// <summary>
- /// Correlates the elements of two sequences based on matching keys.
- /// The default equality comparer is used to compare keys. A
- /// specified <see cref="IEqualityComparer{T}" /> is used to compare keys.
- /// </summary>
-
- public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(
- this IEnumerable<TOuter> outer,
- IEnumerable<TInner> inner,
- Func<TOuter, TKey> outerKeySelector,
- Func<TInner, TKey> innerKeySelector,
- Func<TOuter, TInner, TResult> resultSelector,
- IEqualityComparer<TKey> comparer)
- {
- CheckNotNull(outer, "outer");
- CheckNotNull(inner, "inner");
- CheckNotNull(outerKeySelector, "outerKeySelector");
- CheckNotNull(innerKeySelector, "innerKeySelector");
- CheckNotNull(resultSelector, "resultSelector");
-
- var lookup = inner.ToLookup(innerKeySelector, comparer);
-
- return
- from o in outer
- from i in lookup[outerKeySelector(o)]
- select resultSelector(o, i);
- }
+ /// <summary>
+ /// Produces the set intersection of two sequences by using the
+ /// default equality comparer to compare values.
+ /// </summary>
- /// <summary>
- /// Correlates the elements of two sequences based on equality of
- /// keys and groups the results. The default equality comparer is
- /// used to compare keys.
- /// </summary>
-
- public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(
- this IEnumerable<TOuter> outer,
- IEnumerable<TInner> inner,
- Func<TOuter, TKey> outerKeySelector,
- Func<TInner, TKey> innerKeySelector,
- Func<TOuter, IEnumerable<TInner>, TResult> resultSelector)
- {
- return outer.GroupJoin(inner, outerKeySelector, innerKeySelector, resultSelector, /* comparer */ null);
- }
+ public static IEnumerable<TSource> Intersect<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second)
+ {
+ return first.Intersect(second, /* comparer */ null);
+ }
- /// <summary>
- /// Correlates the elements of two sequences based on equality of
- /// keys and groups the results. The default equality comparer is
- /// used to compare keys. A specified <see cref="IEqualityComparer{T}" />
- /// is used to compare keys.
- /// </summary>
-
- public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(
- this IEnumerable<TOuter> outer,
- IEnumerable<TInner> inner,
- Func<TOuter, TKey> outerKeySelector,
- Func<TInner, TKey> innerKeySelector,
- Func<TOuter, IEnumerable<TInner>, TResult> resultSelector,
- IEqualityComparer<TKey> comparer)
- {
- CheckNotNull(outer, "outer");
- CheckNotNull(inner, "inner");
- CheckNotNull(outerKeySelector, "outerKeySelector");
- CheckNotNull(innerKeySelector, "innerKeySelector");
- CheckNotNull(resultSelector, "resultSelector");
-
- var lookup = inner.ToLookup(innerKeySelector, comparer);
- return outer.Select(o => resultSelector(o, lookup[outerKeySelector(o)]));
- }
-
- [DebuggerStepThrough]
- private static void CheckNotNull<T>(T value, string name) where T : class
- {
- if (value == null)
- throw new ArgumentNullException(name);
- }
+ /// <summary>
+ /// Produces the set intersection of two sequences by using the
+ /// specified <see cref="IEqualityComparer{T}" /> to compare values.
+ /// </summary>
- private static class Sequence<T>
- {
- public static readonly IEnumerable<T> Empty = new T[0];
- }
+ public static IEnumerable<TSource> Intersect<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second,
+ IEqualityComparer<TSource> comparer)
+ {
+ return IntersectExceptImpl(first, second, comparer, /* flag */ true);
+ }
- private sealed class Grouping<K, V> : List<V>, IGrouping<K, V>
- {
- internal Grouping(K key)
- {
- Key = key;
- }
+ /// <summary>
+ /// Produces the set difference of two sequences by using the
+ /// default equality comparer to compare values.
+ /// </summary>
- public K Key { get; private set; }
- }
+ public static IEnumerable<TSource> Except<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second)
+ {
+ return first.Except(second, /* comparer */ null);
}
-}
-// $Id: Enumerable.g.cs 215 2009-10-03 13:31:49Z azizatif $
+ /// <summary>
+ /// Produces the set difference of two sequences by using the
+ /// specified <see cref="IEqualityComparer{T}" /> to compare values.
+ /// </summary>
-namespace System.Linq
-{
- #region Imports
+ public static IEnumerable<TSource> Except<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second,
+ IEqualityComparer<TSource> comparer)
+ {
+ return IntersectExceptImpl(first, second, comparer, /* flag */ false);
+ }
- using System;
- using System.Collections.Generic;
+ /// <summary>
+ /// Creates a <see cref="Dictionary{TKey,TValue}" /> from an
+ /// <see cref="IEnumerable{T}" /> according to a specified key
+ /// selector function.
+ /// </summary>
- #endregion
-
- // This partial implementation was template-generated:
- // Sat, 03 Oct 2009 09:42:39 GMT
+ public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector)
+ {
+ return source.ToDictionary(keySelector, /* comparer */ null);
+ }
- partial class Enumerable
+ /// <summary>
+ /// Creates a <see cref="Dictionary{TKey,TValue}" /> from an
+ /// <see cref="IEnumerable{T}" /> according to a specified key
+ /// selector function and key comparer.
+ /// </summary>
+
+ public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ IEqualityComparer<TKey> comparer)
{
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Int32" /> values.
- /// </summary>
+ return source.ToDictionary(keySelector, e => e);
+ }
- public static int Sum(
- this IEnumerable<int> source)
- {
- CheckNotNull(source, "source");
+ /// <summary>
+ /// Creates a <see cref="Dictionary{TKey,TValue}" /> from an
+ /// <see cref="IEnumerable{T}" /> according to specified key
+ /// selector and element selector functions.
+ /// </summary>
- int sum = 0;
- foreach (var num in source)
- sum = checked(sum + num);
+ public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TSource, TElement> elementSelector)
+ {
+ return source.ToDictionary(keySelector, elementSelector, /* comparer */ null);
+ }
- return sum;
- }
+ /// <summary>
+ /// Creates a <see cref="Dictionary{TKey,TValue}" /> from an
+ /// <see cref="IEnumerable{T}" /> according to a specified key
+ /// selector function, a comparer, and an element selector function.
+ /// </summary>
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Int32" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TSource, TElement> elementSelector,
+ IEqualityComparer<TKey> comparer)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(keySelector, "keySelector");
+ CheckNotNull(elementSelector, "elementSelector");
+
+ var dict = new Dictionary<TKey, TElement>(comparer);
+
+ foreach (var item in source)
+ {
+ //
+ // ToDictionary is meant to throw ArgumentNullException if
+ // keySelector produces a key that is null and
+ // Argument exception if keySelector produces duplicate keys
+ // for two elements. Incidentally, the doucmentation for
+ // IDictionary<TKey, TValue>.Add says that the Add method
+ // throws the same exceptions under the same circumstances
+ // so we don't need to do any additional checking or work
+ // here and let the Add implementation do all the heavy
+ // lifting.
+ //
+
+ dict.Add(keySelector(item), elementSelector(item));
+ }
+
+ return dict;
+ }
- public static int Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int> selector)
- {
- return source.Select(selector).Sum();
- }
-
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Int32" /> values.
- /// </summary>
+ /// <summary>
+ /// Correlates the elements of two sequences based on matching keys.
+ /// The default equality comparer is used to compare keys.
+ /// </summary>
- public static double Average(
- this IEnumerable<int> source)
- {
- CheckNotNull(source, "source");
+ public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(
+ this IEnumerable<TOuter> outer,
+ IEnumerable<TInner> inner,
+ Func<TOuter, TKey> outerKeySelector,
+ Func<TInner, TKey> innerKeySelector,
+ Func<TOuter, TInner, TResult> resultSelector)
+ {
+ return outer.Join(inner, outerKeySelector, innerKeySelector, resultSelector, /* comparer */ null);
+ }
- long sum = 0;
- long count = 0;
+ /// <summary>
+ /// Correlates the elements of two sequences based on matching keys.
+ /// The default equality comparer is used to compare keys. A
+ /// specified <see cref="IEqualityComparer{T}" /> is used to compare keys.
+ /// </summary>
- foreach (var num in source)
- checked
- {
- sum += (int) num;
- count++;
- }
+ public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(
+ this IEnumerable<TOuter> outer,
+ IEnumerable<TInner> inner,
+ Func<TOuter, TKey> outerKeySelector,
+ Func<TInner, TKey> innerKeySelector,
+ Func<TOuter, TInner, TResult> resultSelector,
+ IEqualityComparer<TKey> comparer)
+ {
+ CheckNotNull(outer, "outer");
+ CheckNotNull(inner, "inner");
+ CheckNotNull(outerKeySelector, "outerKeySelector");
+ CheckNotNull(innerKeySelector, "innerKeySelector");
+ CheckNotNull(resultSelector, "resultSelector");
+
+ var lookup = inner.ToLookup(innerKeySelector, comparer);
+
+ return
+ from o in outer
+ from i in lookup[outerKeySelector(o)]
+ select resultSelector(o, i);
+ }
- if (count == 0)
- throw new InvalidOperationException();
+ /// <summary>
+ /// Correlates the elements of two sequences based on equality of
+ /// keys and groups the results. The default equality comparer is
+ /// used to compare keys.
+ /// </summary>
- return (double) sum / count;
- }
+ public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(
+ this IEnumerable<TOuter> outer,
+ IEnumerable<TInner> inner,
+ Func<TOuter, TKey> outerKeySelector,
+ Func<TInner, TKey> innerKeySelector,
+ Func<TOuter, IEnumerable<TInner>, TResult> resultSelector)
+ {
+ return outer.GroupJoin(inner, outerKeySelector, innerKeySelector, resultSelector, /* comparer */ null);
+ }
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Int32" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ /// <summary>
+ /// Correlates the elements of two sequences based on equality of
+ /// keys and groups the results. The default equality comparer is
+ /// used to compare keys. A specified <see cref="IEqualityComparer{T}" />
+ /// is used to compare keys.
+ /// </summary>
- public static double Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int> selector)
- {
- return source.Select(selector).Average();
- }
-
+ public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(
+ this IEnumerable<TOuter> outer,
+ IEnumerable<TInner> inner,
+ Func<TOuter, TKey> outerKeySelector,
+ Func<TInner, TKey> innerKeySelector,
+ Func<TOuter, IEnumerable<TInner>, TResult> resultSelector,
+ IEqualityComparer<TKey> comparer)
+ {
+ CheckNotNull(outer, "outer");
+ CheckNotNull(inner, "inner");
+ CheckNotNull(outerKeySelector, "outerKeySelector");
+ CheckNotNull(innerKeySelector, "innerKeySelector");
+ CheckNotNull(resultSelector, "resultSelector");
+
+ var lookup = inner.ToLookup(innerKeySelector, comparer);
+ return outer.Select(o => resultSelector(o, lookup[outerKeySelector(o)]));
+ }
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Int32" /> values.
- /// </summary>
+ [DebuggerStepThrough]
+ private static void CheckNotNull<T>(T value, string name) where T : class
+ {
+ if (value == null)
+ throw new ArgumentNullException(name);
+ }
- public static int? Sum(
- this IEnumerable<int?> source)
- {
- CheckNotNull(source, "source");
+ private static class Sequence<T>
+ {
+ public static readonly IEnumerable<T> Empty = new T[0];
+ }
- int sum = 0;
- foreach (var num in source)
- sum = checked(sum + (num ?? 0));
+ private sealed class Grouping<K, V> : List<V>, IGrouping<K, V>
+ {
+ internal Grouping(K key)
+ {
+ Key = key;
+ }
- return sum;
- }
+ public K Key { get; private set; }
+ }
+ }
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Int32" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ internal partial class Enumerable
+ {
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Int32" /> values.
+ /// </summary>
- public static int? Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int?> selector)
- {
- return source.Select(selector).Sum();
- }
-
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Int32" /> values.
- /// </summary>
+ public static int Sum(
+ this IEnumerable<int> source)
+ {
+ CheckNotNull(source, "source");
- public static double? Average(
- this IEnumerable<int?> source)
- {
- CheckNotNull(source, "source");
+ int sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + num);
- long sum = 0;
- long count = 0;
+ return sum;
+ }
- foreach (var num in source.Where(n => n != null))
- checked
- {
- sum += (int) num;
- count++;
- }
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Int32" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
- if (count == 0)
- return null;
+ public static int Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int> selector)
+ {
+ return source.Select(selector).Sum();
+ }
- return (double?) sum / count;
- }
+ /// <summary>
+ /// Computes the average of a sequence of nullable <see cref="System.Int32" /> values.
+ /// </summary>
+
+ public static double Average(
+ this IEnumerable<int> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Int32" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ long sum = 0;
+ long count = 0;
- public static double? Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int?> selector)
- {
- return source.Select(selector).Average();
- }
-
- /// <summary>
- /// Returns the minimum value in a sequence of nullable
- /// <see cref="System.Int32" /> values.
- /// </summary>
-
- public static int? Min(
- this IEnumerable<int?> source)
+ foreach (var num in source)
+ checked
{
- CheckNotNull(source, "source");
-
- return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
+ sum += (int) num;
+ count++;
}
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the minimum nullable <see cref="System.Int32" /> value.
- /// </summary>
+ if (count == 0)
+ throw new InvalidOperationException();
- public static int? Min<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int?> selector)
- {
- return source.Select(selector).Min();
- }
+ return (double) sum/count;
+ }
- /// <summary>
- /// Returns the maximum value in a sequence of nullable
- /// <see cref="System.Int32" /> values.
- /// </summary>
+ /// <summary>
+ /// Computes the average of a sequence of nullable <see cref="System.Int32" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
- public static int? Max(
- this IEnumerable<int?> source)
- {
- CheckNotNull(source, "source");
-
- return MinMaxImpl(source.Where(x => x != null),
- null, (max, x) => x == null || (max != null && x.Value < max.Value));
- }
+ public static double Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int> selector)
+ {
+ return source.Select(selector).Average();
+ }
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the maximum nullable <see cref="System.Int32" /> value.
- /// </summary>
- public static int? Max<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int?> selector)
- {
- return source.Select(selector).Max();
- }
+ /// <summary>
+ /// Computes the sum of a sequence of <see cref="System.Int32" /> values.
+ /// </summary>
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Int64" /> values.
- /// </summary>
+ public static int? Sum(
+ this IEnumerable<int?> source)
+ {
+ CheckNotNull(source, "source");
- public static long Sum(
- this IEnumerable<long> source)
- {
- CheckNotNull(source, "source");
+ int sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + (num ?? 0));
- long sum = 0;
- foreach (var num in source)
- sum = checked(sum + num);
+ return sum;
+ }
- return sum;
- }
+ /// <summary>
+ /// Computes the sum of a sequence of <see cref="System.Int32" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Int64" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ public static int? Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int?> selector)
+ {
+ return source.Select(selector).Sum();
+ }
+
+ /// <summary>
+ /// Computes the average of a sequence of <see cref="System.Int32" /> values.
+ /// </summary>
- public static long Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, long> selector)
+ public static double? Average(
+ this IEnumerable<int?> source)
+ {
+ CheckNotNull(source, "source");
+
+ long sum = 0;
+ long count = 0;
+
+ foreach (var num in source.Where(n => n != null))
+ checked
{
- return source.Select(selector).Sum();
+ sum += (int) num;
+ count++;
}
-
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Int64" /> values.
- /// </summary>
- public static double Average(
- this IEnumerable<long> source)
- {
- CheckNotNull(source, "source");
+ if (count == 0)
+ return null;
- long sum = 0;
- long count = 0;
+ return (double?) sum/count;
+ }
- foreach (var num in source)
- checked
- {
- sum += (long) num;
- count++;
- }
+ /// <summary>
+ /// Computes the average of a sequence of <see cref="System.Int32" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
- if (count == 0)
- throw new InvalidOperationException();
+ public static double? Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int?> selector)
+ {
+ return source.Select(selector).Average();
+ }
- return (double) sum / count;
- }
+ /// <summary>
+ /// Returns the minimum value in a sequence of nullable
+ /// <see cref="System.Int32" /> values.
+ /// </summary>
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Int64" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ public static int? Min(
+ this IEnumerable<int?> source)
+ {
+ CheckNotNull(source, "source");
- public static double Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, long> selector)
- {
- return source.Select(selector).Average();
- }
-
+ return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
+ }
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Int64" /> values.
- /// </summary>
+ /// <summary>
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the minimum nullable <see cref="System.Int32" /> value.
+ /// </summary>
- public static long? Sum(
- this IEnumerable<long?> source)
- {
- CheckNotNull(source, "source");
+ public static int? Min<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int?> selector)
+ {
+ return source.Select(selector).Min();
+ }
- long sum = 0;
- foreach (var num in source)
- sum = checked(sum + (num ?? 0));
+ /// <summary>
+ /// Returns the maximum value in a sequence of nullable
+ /// <see cref="System.Int32" /> values.
+ /// </summary>
- return sum;
- }
+ public static int? Max(
+ this IEnumerable<int?> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Int64" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ return MinMaxImpl(source.Where(x => x != null),
+ null, (max, x) => x == null || (max != null && x.Value < max.Value));
+ }
- public static long? Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, long?> selector)
- {
- return source.Select(selector).Sum();
- }
-
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Int64" /> values.
- /// </summary>
+ /// <summary>
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the maximum nullable <see cref="System.Int32" /> value.
+ /// </summary>
- public static double? Average(
- this IEnumerable<long?> source)
- {
- CheckNotNull(source, "source");
+ public static int? Max<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int?> selector)
+ {
+ return source.Select(selector).Max();
+ }
- long sum = 0;
- long count = 0;
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Int64" /> values.
+ /// </summary>
- foreach (var num in source.Where(n => n != null))
- checked
- {
- sum += (long) num;
- count++;
- }
+ public static long Sum(
+ this IEnumerable<long> source)
+ {
+ CheckNotNull(source, "source");
- if (count == 0)
- return null;
+ long sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + num);
- return (double?) sum / count;
- }
+ return sum;
+ }
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Int64" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Int64" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
- public static double? Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, long?> selector)
- {
- return source.Select(selector).Average();
- }
-
- /// <summary>
- /// Returns the minimum value in a sequence of nullable
- /// <see cref="System.Int64" /> values.
- /// </summary>
-
- public static long? Min(
- this IEnumerable<long?> source)
- {
- CheckNotNull(source, "source");
-
- return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
- }
+ public static long Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, long> selector)
+ {
+ return source.Select(selector).Sum();
+ }
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the minimum nullable <see cref="System.Int64" /> value.
- /// </summary>
+ /// <summary>
+ /// Computes the average of a sequence of nullable <see cref="System.Int64" /> values.
+ /// </summary>
- public static long? Min<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, long?> selector)
- {
- return source.Select(selector).Min();
- }
+ public static double Average(
+ this IEnumerable<long> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Returns the maximum value in a sequence of nullable
- /// <see cref="System.Int64" /> values.
- /// </summary>
+ long sum = 0;
+ long count = 0;
- public static long? Max(
- this IEnumerable<long?> source)
+ foreach (var num in source)
+ checked
{
- CheckNotNull(source, "source");
-
- return MinMaxImpl(source.Where(x => x != null),
- null, (max, x) => x == null || (max != null && x.Value < max.Value));
+ sum += (long) num;
+ count++;
}
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the maximum nullable <see cref="System.Int64" /> value.
- /// </summary>
+ if (count == 0)
+ throw new InvalidOperationException();
- public static long? Max<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, long?> selector)
- {
- return source.Select(selector).Max();
- }
+ return (double) sum/count;
+ }
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Single" /> values.
- /// </summary>
+ /// <summary>
+ /// Computes the average of a sequence of nullable <see cref="System.Int64" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
- public static float Sum(
- this IEnumerable<float> source)
- {
- CheckNotNull(source, "source");
+ public static double Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, long> selector)
+ {
+ return source.Select(selector).Average();
+ }
- float sum = 0;
- foreach (var num in source)
- sum = checked(sum + num);
- return sum;
- }
+ /// <summary>
+ /// Computes the sum of a sequence of <see cref="System.Int64" /> values.
+ /// </summary>
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Single" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ public static long? Sum(
+ this IEnumerable<long?> source)
+ {
+ CheckNotNull(source, "source");
- public static float Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, float> selector)
- {
- return source.Select(selector).Sum();
- }
-
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Single" /> values.
- /// </summary>
+ long sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + (num ?? 0));
- public static float Average(
- this IEnumerable<float> source)
- {
- CheckNotNull(source, "source");
+ return sum;
+ }
- float sum = 0;
- long count = 0;
+ /// <summary>
+ /// Computes the sum of a sequence of <see cref="System.Int64" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
- foreach (var num in source)
- checked
- {
- sum += (float) num;
- count++;
- }
+ public static long? Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, long?> selector)
+ {
+ return source.Select(selector).Sum();
+ }
- if (count == 0)
- throw new InvalidOperationException();
+ /// <summary>
+ /// Computes the average of a sequence of <see cref="System.Int64" /> values.
+ /// </summary>
- return (float) sum / count;
- }
+ public static double? Average(
+ this IEnumerable<long?> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Single" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ long sum = 0;
+ long count = 0;
- public static float Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, float> selector)
+ foreach (var num in source.Where(n => n != null))
+ checked
{
- return source.Select(selector).Average();
+ sum += (long) num;
+ count++;
}
-
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Single" /> values.
- /// </summary>
-
- public static float? Sum(
- this IEnumerable<float?> source)
- {
- CheckNotNull(source, "source");
+ if (count == 0)
+ return null;
- float sum = 0;
- foreach (var num in source)
- sum = checked(sum + (num ?? 0));
+ return (double?) sum/count;
+ }
- return sum;
- }
+ /// <summary>
+ /// Computes the average of a sequence of <see cref="System.Int64" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Single" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ public static double? Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, long?> selector)
+ {
+ return source.Select(selector).Average();
+ }
- public static float? Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, float?> selector)
- {
- return source.Select(selector).Sum();
- }
-
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Single" /> values.
- /// </summary>
+ /// <summary>
+ /// Returns the minimum value in a sequence of nullable
+ /// <see cref="System.Int64" /> values.
+ /// </summary>
- public static float? Average(
- this IEnumerable<float?> source)
- {
- CheckNotNull(source, "source");
+ public static long? Min(
+ this IEnumerable<long?> source)
+ {
+ CheckNotNull(source, "source");
- float sum = 0;
- long count = 0;
+ return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
+ }
- foreach (var num in source.Where(n => n != null))
- checked
- {
- sum += (float) num;
- count++;
- }
+ /// <summary>
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the minimum nullable <see cref="System.Int64" /> value.
+ /// </summary>
- if (count == 0)
- return null;
+ public static long? Min<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, long?> selector)
+ {
+ return source.Select(selector).Min();
+ }
- return (float?) sum / count;
- }
+ /// <summary>
+ /// Returns the maximum value in a sequence of nullable
+ /// <see cref="System.Int64" /> values.
+ /// </summary>
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Single" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ public static long? Max(
+ this IEnumerable<long?> source)
+ {
+ CheckNotNull(source, "source");
- public static float? Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, float?> selector)
- {
- return source.Select(selector).Average();
- }
-
- /// <summary>
- /// Returns the minimum value in a sequence of nullable
- /// <see cref="System.Single" /> values.
- /// </summary>
-
- public static float? Min(
- this IEnumerable<float?> source)
- {
- CheckNotNull(source, "source");
-
- return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
- }
+ return MinMaxImpl(source.Where(x => x != null),
+ null, (max, x) => x == null || (max != null && x.Value < max.Value));
+ }
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the minimum nullable <see cref="System.Single" /> value.
- /// </summary>
+ /// <summary>
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the maximum nullable <see cref="System.Int64" /> value.
+ /// </summary>
- public static float? Min<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, float?> selector)
- {
- return source.Select(selector).Min();
- }
+ public static long? Max<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, long?> selector)
+ {
+ return source.Select(selector).Max();
+ }
- /// <summary>
- /// Returns the maximum value in a sequence of nullable
- /// <see cref="System.Single" /> values.
- /// </summary>
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Single" /> values.
+ /// </summary>
- public static float? Max(
- this IEnumerable<float?> source)
- {
- CheckNotNull(source, "source");
-
- return MinMaxImpl(source.Where(x => x != null),
- null, (max, x) => x == null || (max != null && x.Value < max.Value));
- }
+ public static float Sum(
+ this IEnumerable<float> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the maximum nullable <see cref="System.Single" /> value.
- /// </summary>
+ float sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + num);
- public static float? Max<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, float?> selector)
- {
- return source.Select(selector).Max();
- }
+ return sum;
+ }
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Double" /> values.
- /// </summary>
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Single" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
- public static double Sum(
- this IEnumerable<double> source)
- {
- CheckNotNull(source, "source");
+ public static float Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, float> selector)
+ {
+ return source.Select(selector).Sum();
+ }
- double sum = 0;
- foreach (var num in source)
- sum = checked(sum + num);
+ /// <summary>
+ /// Computes the average of a sequence of nullable <see cref="System.Single" /> values.
+ /// </summary>
- return sum;
- }
+ public static float Average(
+ this IEnumerable<float> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Double" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ float sum = 0;
+ long count = 0;
- public static double Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, double> selector)
+ foreach (var num in source)
+ checked
{
- return source.Select(selector).Sum();
+ sum += (float) num;
+ count++;
}
-
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Double" /> values.
- /// </summary>
- public static double Average(
- this IEnumerable<double> source)
- {
- CheckNotNull(source, "source");
+ if (count == 0)
+ throw new InvalidOperationException();
- double sum = 0;
- long count = 0;
+ return (float) sum/count;
+ }
- foreach (var num in source)
- checked
- {
- sum += (double) num;
- count++;
- }
+ /// <summary>
+ /// Computes the average of a sequence of nullable <see cref="System.Single" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
- if (count == 0)
- throw new InvalidOperationException();
+ public static float Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, float> selector)
+ {
+ return source.Select(selector).Average();
+ }
- return (double) sum / count;
- }
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Double" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ /// <summary>
+ /// Computes the sum of a sequence of <see cref="System.Single" /> values.
+ /// </summary>
- public static double Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, double> selector)
- {
- return source.Select(selector).Average();
- }
-
+ public static float? Sum(
+ this IEnumerable<float?> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Double" /> values.
- /// </summary>
+ float sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + (num ?? 0));
- public static double? Sum(
- this IEnumerable<double?> source)
- {
- CheckNotNull(source, "source");
+ return sum;
+ }
- double sum = 0;
- foreach (var num in source)
- sum = checked(sum + (num ?? 0));
+ /// <summary>
+ /// Computes the sum of a sequence of <see cref="System.Single" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
- return sum;
- }
+ public static float? Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, float?> selector)
+ {
+ return source.Select(selector).Sum();
+ }
+
+ /// <summary>
+ /// Computes the average of a sequence of <see cref="System.Single" /> values.
+ /// </summary>
+
+ public static float? Average(
+ this IEnumerable<float?> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Double" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ float sum = 0;
+ long count = 0;
- public static double? Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, double?> selector)
+ foreach (var num in source.Where(n => n != null))
+ checked
{
- return source.Select(selector).Sum();
+ sum += (float) num;
+ count++;
}
-
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Double" /> values.
- /// </summary>
- public static double? Average(
- this IEnumerable<double?> source)
- {
- CheckNotNull(source, "source");
+ if (count == 0)
+ return null;
- double sum = 0;
- long count = 0;
+ return (float?) sum/count;
+ }
- foreach (var num in source.Where(n => n != null))
- checked
- {
- sum += (double) num;
- count++;
- }
+ /// <summary>
+ /// Computes the average of a sequence of <see cref="System.Single" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
- if (count == 0)
- return null;
+ public static float? Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, float?> selector)
+ {
+ return source.Select(selector).Average();
+ }
- return (double?) sum / count;
- }
+ /// <summary>
+ /// Returns the minimum value in a sequence of nullable
+ /// <see cref="System.Single" /> values.
+ /// </summary>
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Double" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ public static float? Min(
+ this IEnumerable<float?> source)
+ {
+ CheckNotNull(source, "source");
- public static double? Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, double?> selector)
- {
- return source.Select(selector).Average();
- }
-
- /// <summary>
- /// Returns the minimum value in a sequence of nullable
- /// <see cref="System.Double" /> values.
- /// </summary>
-
- public static double? Min(
- this IEnumerable<double?> source)
- {
- CheckNotNull(source, "source");
-
- return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
- }
+ return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
+ }
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the minimum nullable <see cref="System.Double" /> value.
- /// </summary>
+ /// <summary>
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the minimum nullable <see cref="System.Single" /> value.
+ /// </summary>
- public static double? Min<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, double?> selector)
- {
- return source.Select(selector).Min();
- }
+ public static float? Min<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, float?> selector)
+ {
+ return source.Select(selector).Min();
+ }
- /// <summary>
- /// Returns the maximum value in a sequence of nullable
- /// <see cref="System.Double" /> values.
- /// </summary>
+ /// <summary>
+ /// Returns the maximum value in a sequence of nullable
+ /// <see cref="System.Single" /> values.
+ /// </summary>
- public static double? Max(
- this IEnumerable<double?> source)
- {
- CheckNotNull(source, "source");
-
- return MinMaxImpl(source.Where(x => x != null),
- null, (max, x) => x == null || (max != null && x.Value < max.Value));
- }
+ public static float? Max(
+ this IEnumerable<float?> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the maximum nullable <see cref="System.Double" /> value.
- /// </summary>
+ return MinMaxImpl(source.Where(x => x != null),
+ null, (max, x) => x == null || (max != null && x.Value < max.Value));
+ }
- public static double? Max<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, double?> selector)
- {
- return source.Select(selector).Max();
- }
+ /// <summary>
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the maximum nullable <see cref="System.Single" /> value.
+ /// </summary>
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Decimal" /> values.
- /// </summary>
+ public static float? Max<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, float?> selector)
+ {
+ return source.Select(selector).Max();
+ }
- public static decimal Sum(
- this IEnumerable<decimal> source)
- {
- CheckNotNull(source, "source");
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Double" /> values.
+ /// </summary>
- decimal sum = 0;
- foreach (var num in source)
- sum = checked(sum + num);
+ public static double Sum(
+ this IEnumerable<double> source)
+ {
+ CheckNotNull(source, "source");
- return sum;
- }
+ double sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + num);
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Decimal" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ return sum;
+ }
- public static decimal Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, decimal> selector)
- {
- return source.Select(selector).Sum();
- }
-
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Decimal" /> values.
- /// </summary>
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Double" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
- public static decimal Average(
- this IEnumerable<decimal> source)
- {
- CheckNotNull(source, "source");
+ public static double Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, double> selector)
+ {
+ return source.Select(selector).Sum();
+ }
- decimal sum = 0;
- long count = 0;
+ /// <summary>
+ /// Computes the average of a sequence of nullable <see cref="System.Double" /> values.
+ /// </summary>
- foreach (var num in source)
- checked
- {
- sum += (decimal) num;
- count++;
- }
+ public static double Average(
+ this IEnumerable<double> source)
+ {
+ CheckNotNull(source, "source");
- if (count == 0)
- throw new InvalidOperationException();
+ double sum = 0;
+ long count = 0;
- return (decimal) sum / count;
+ foreach (var num in source)
+ checked
+ {
+ sum += (double) num;
+ count++;
}
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Decimal" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ if (count == 0)
+ throw new InvalidOperationException();
- public static decimal Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, decimal> selector)
- {
- return source.Select(selector).Average();
- }
-
+ return (double) sum/count;
+ }
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Decimal" /> values.
- /// </summary>
+ /// <summary>
+ /// Computes the average of a sequence of nullable <see cref="System.Double" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
- public static decimal? Sum(
- this IEnumerable<decimal?> source)
- {
- CheckNotNull(source, "source");
+ public static double Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, double> selector)
+ {
+ return source.Select(selector).Average();
+ }
- decimal sum = 0;
- foreach (var num in source)
- sum = checked(sum + (num ?? 0));
- return sum;
- }
+ /// <summary>
+ /// Computes the sum of a sequence of <see cref="System.Double" /> values.
+ /// </summary>
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Decimal" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ public static double? Sum(
+ this IEnumerable<double?> source)
+ {
+ CheckNotNull(source, "source");
- public static decimal? Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, decimal?> selector)
- {
- return source.Select(selector).Sum();
- }
-
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Decimal" /> values.
- /// </summary>
+ double sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + (num ?? 0));
- public static decimal? Average(
- this IEnumerable<decimal?> source)
- {
- CheckNotNull(source, "source");
+ return sum;
+ }
- decimal sum = 0;
- long count = 0;
+ /// <summary>
+ /// Computes the sum of a sequence of <see cref="System.Double" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
- foreach (var num in source.Where(n => n != null))
- checked
- {
- sum += (decimal) num;
- count++;
- }
+ public static double? Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, double?> selector)
+ {
+ return source.Select(selector).Sum();
+ }
- if (count == 0)
- return null;
+ /// <summary>
+ /// Computes the average of a sequence of <see cref="System.Double" /> values.
+ /// </summary>
- return (decimal?) sum / count;
- }
+ public static double? Average(
+ this IEnumerable<double?> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Decimal" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ double sum = 0;
+ long count = 0;
- public static decimal? Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, decimal?> selector)
- {
- return source.Select(selector).Average();
- }
-
- /// <summary>
- /// Returns the minimum value in a sequence of nullable
- /// <see cref="System.Decimal" /> values.
- /// </summary>
-
- public static decimal? Min(
- this IEnumerable<decimal?> source)
+ foreach (var num in source.Where(n => n != null))
+ checked
{
- CheckNotNull(source, "source");
-
- return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
+ sum += (double) num;
+ count++;
}
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the minimum nullable <see cref="System.Decimal" /> value.
- /// </summary>
+ if (count == 0)
+ return null;
- public static decimal? Min<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, decimal?> selector)
- {
- return source.Select(selector).Min();
- }
+ return (double?) sum/count;
+ }
- /// <summary>
- /// Returns the maximum value in a sequence of nullable
- /// <see cref="System.Decimal" /> values.
- /// </summary>
+ /// <summary>
+ /// Computes the average of a sequence of <see cref="System.Double" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
- public static decimal? Max(
- this IEnumerable<decimal?> source)
- {
- CheckNotNull(source, "source");
-
- return MinMaxImpl(source.Where(x => x != null),
- null, (max, x) => x == null || (max != null && x.Value < max.Value));
- }
+ public static double? Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, double?> selector)
+ {
+ return source.Select(selector).Average();
+ }
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the maximum nullable <see cref="System.Decimal" /> value.
- /// </summary>
+ /// <summary>
+ /// Returns the minimum value in a sequence of nullable
+ /// <see cref="System.Double" /> values.
+ /// </summary>
- public static decimal? Max<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, decimal?> selector)
- {
- return source.Select(selector).Max();
- }
+ public static double? Min(
+ this IEnumerable<double?> source)
+ {
+ CheckNotNull(source, "source");
+
+ return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
}
-}
-namespace System.Runtime.CompilerServices
-{
- /// <remarks>
- /// This attribute allows us to define extension methods without
- /// requiring .NET Framework 3.5. For more information, see the section,
- /// <a href="http://msdn.microsoft.com/en-us/magazine/cc163317.aspx#S7">Extension Methods in .NET Framework 2.0 Apps</a>,
- /// of <a href="http://msdn.microsoft.com/en-us/magazine/cc163317.aspx">Basic Instincts: Extension Methods</a>
- /// column in <a href="http://msdn.microsoft.com/msdnmag/">MSDN Magazine</a>,
- /// issue <a href="http://msdn.microsoft.com/en-us/magazine/cc135410.aspx">Nov 2007</a>.
- /// </remarks>
+ /// <summary>
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the minimum nullable <see cref="System.Double" /> value.
+ /// </summary>
- [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
- internal sealed class ExtensionAttribute : Attribute { }
-}
+ public static double? Min<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, double?> selector)
+ {
+ return source.Select(selector).Min();
+ }
+
+ /// <summary>
+ /// Returns the maximum value in a sequence of nullable
+ /// <see cref="System.Double" /> values.
+ /// </summary>
-// $Id: Func.cs 224 2009-10-04 07:13:08Z azizatif $
+ public static double? Max(
+ this IEnumerable<double?> source)
+ {
+ CheckNotNull(source, "source");
-namespace System
-{
-#if LINQBRIDGE_LIB
- public delegate TResult Func<TResult>();
- public delegate TResult Func<T, TResult>(T a);
- public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
- public delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3);
- public delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
-#else
- delegate TResult Func<TResult>();
- delegate TResult Func<T, TResult>(T a);
- delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
- delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3);
- delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
-#endif
-}
+ return MinMaxImpl(source.Where(x => x != null),
+ null, (max, x) => x == null || (max != null && x.Value < max.Value));
+ }
-// $Id: IGrouping.cs 225 2009-10-04 07:16:14Z azizatif $
+ /// <summary>
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the maximum nullable <see cref="System.Double" /> value.
+ /// </summary>
-namespace System.Linq
-{
- #region Imports
+ public static double? Max<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, double?> selector)
+ {
+ return source.Select(selector).Max();
+ }
+
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Decimal" /> values.
+ /// </summary>
+
+ public static decimal Sum(
+ this IEnumerable<decimal> source)
+ {
+ CheckNotNull(source, "source");
+
+ decimal sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + num);
+
+ return sum;
+ }
- using System.Collections.Generic;
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Decimal" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
- #endregion
+ public static decimal Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, decimal> selector)
+ {
+ return source.Select(selector).Sum();
+ }
/// <summary>
- /// Represents a collection of objects that have a common key.
+ /// Computes the average of a sequence of nullable <see cref="System.Decimal" /> values.
/// </summary>
- partial interface IGrouping<TKey, TElement> : IEnumerable<TElement>
+ public static decimal Average(
+ this IEnumerable<decimal> source)
{
- /// <summary>
- /// Gets the key of the <see cref="IGrouping{TKey,TElement}" />.
- /// </summary>
+ CheckNotNull(source, "source");
+
+ decimal sum = 0;
+ long count = 0;
+
+ foreach (var num in source)
+ checked
+ {
+ sum += (decimal) num;
+ count++;
+ }
+
+ if (count == 0)
+ throw new InvalidOperationException();
- TKey Key { get; }
+ return (decimal) sum/count;
}
-}
-// $Id: ILookup.cs 224 2009-10-04 07:13:08Z azizatif $
+ /// <summary>
+ /// Computes the average of a sequence of nullable <see cref="System.Decimal" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
+
+ public static decimal Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, decimal> selector)
+ {
+ return source.Select(selector).Average();
+ }
-namespace System.Linq
-{
- using System.Collections.Generic;
/// <summary>
- /// Defines an indexer, size property, and Boolean search method for
- /// data structures that map keys to <see cref="IEnumerable{T}"/>
- /// sequences of values.
+ /// Computes the sum of a sequence of <see cref="System.Decimal" /> values.
/// </summary>
- partial interface ILookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>
+ public static decimal? Sum(
+ this IEnumerable<decimal?> source)
{
- bool Contains(TKey key);
- int Count { get; }
- IEnumerable<TElement> this[TKey key] { get; }
+ CheckNotNull(source, "source");
+
+ decimal sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + (num ?? 0));
+
+ return sum;
}
-}
-// $Id: IOrderedEnumerable.cs 224 2009-10-04 07:13:08Z azizatif $
+ /// <summary>
+ /// Computes the sum of a sequence of <see cref="System.Decimal" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
-namespace System.Linq
-{
- using System.Collections.Generic;
+ public static decimal? Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, decimal?> selector)
+ {
+ return source.Select(selector).Sum();
+ }
/// <summary>
- /// Represents a sorted sequence.
+ /// Computes the average of a sequence of <see cref="System.Decimal" /> values.
/// </summary>
- partial interface IOrderedEnumerable<TElement> : IEnumerable<TElement>
+ public static decimal? Average(
+ this IEnumerable<decimal?> source)
{
- /// <summary>
- /// Performs a subsequent ordering on the elements of an
- /// <see cref="IOrderedEnumerable{T}"/> according to a key.
- /// </summary>
+ CheckNotNull(source, "source");
+
+ decimal sum = 0;
+ long count = 0;
+
+ foreach (var num in source.Where(n => n != null))
+ checked
+ {
+ sum += (decimal) num;
+ count++;
+ }
+
+ if (count == 0)
+ return null;
- IOrderedEnumerable<TElement> CreateOrderedEnumerable<TKey>(
- Func<TElement, TKey> keySelector, IComparer<TKey> comparer, bool descending);
+ return (decimal?) sum/count;
}
-}
-// $Id: Lookup.cs 224 2009-10-04 07:13:08Z azizatif $
+ /// <summary>
+ /// Computes the average of a sequence of <see cref="System.Decimal" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
-namespace System.Linq
-{
- #region Imports
+ public static decimal? Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, decimal?> selector)
+ {
+ return source.Select(selector).Average();
+ }
+
+ /// <summary>
+ /// Returns the minimum value in a sequence of nullable
+ /// <see cref="System.Decimal" /> values.
+ /// </summary>
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using IEnumerable=System.Collections.IEnumerable;
+ public static decimal? Min(
+ this IEnumerable<decimal?> source)
+ {
+ CheckNotNull(source, "source");
- #endregion
+ return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
+ }
/// <summary>
- /// Represents a collection of keys each mapped to one or more values.
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the minimum nullable <see cref="System.Decimal" /> value.
/// </summary>
- internal sealed class Lookup<TKey, TElement> : ILookup<TKey, TElement>
+ public static decimal? Min<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, decimal?> selector)
{
- private readonly Dictionary<TKey, IGrouping<TKey, TElement>> _map;
+ return source.Select(selector).Min();
+ }
- internal Lookup(IEqualityComparer<TKey> comparer)
- {
- _map = new Dictionary<TKey, IGrouping<TKey, TElement>>(comparer);
- }
+ /// <summary>
+ /// Returns the maximum value in a sequence of nullable
+ /// <see cref="System.Decimal" /> values.
+ /// </summary>
- internal void Add(IGrouping<TKey, TElement> item)
- {
- _map.Add(item.Key, item);
- }
+ public static decimal? Max(
+ this IEnumerable<decimal?> source)
+ {
+ CheckNotNull(source, "source");
- internal IEnumerable<TElement> Find(TKey key)
- {
- IGrouping<TKey, TElement> grouping;
- return _map.TryGetValue(key, out grouping) ? grouping : null;
- }
+ return MinMaxImpl(source.Where(x => x != null),
+ null, (max, x) => x == null || (max != null && x.Value < max.Value));
+ }
- /// <summary>
- /// Gets the number of key/value collection pairs in the <see cref="Lookup{TKey,TElement}" />.
- /// </summary>
+ /// <summary>
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the maximum nullable <see cref="System.Decimal" /> value.
+ /// </summary>
- public int Count
- {
- get { return _map.Count; }
- }
+ public static decimal? Max<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, decimal?> selector)
+ {
+ return source.Select(selector).Max();
+ }
+ }
- /// <summary>
- /// Gets the collection of values indexed by the specified key.
- /// </summary>
+ /// <summary>
+ /// Represents a collection of objects that have a common key.
+ /// </summary>
+ internal partial interface IGrouping<TKey, TElement> : IEnumerable<TElement>
+ {
+ /// <summary>
+ /// Gets the key of the <see cref="IGrouping{TKey,TElement}" />.
+ /// </summary>
- public IEnumerable<TElement> this[TKey key]
- {
- get
- {
- IGrouping<TKey, TElement> result;
- return _map.TryGetValue(key, out result) ? result : Enumerable.Empty<TElement>();
- }
- }
+ TKey Key { get; }
+ }
+
+ /// <summary>
+ /// Defines an indexer, size property, and Boolean search method for
+ /// data structures that map keys to <see cref="IEnumerable{T}"/>
+ /// sequences of values.
+ /// </summary>
+ internal partial interface ILookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>
+ {
+ bool Contains(TKey key);
+ int Count { get; }
+ IEnumerable<TElement> this[TKey key] { get; }
+ }
+
+ /// <summary>
+ /// Represents a sorted sequence.
+ /// </summary>
+ internal partial interface IOrderedEnumerable<TElement> : IEnumerable<TElement>
+ {
+ /// <summary>
+ /// Performs a subsequent ordering on the elements of an
+ /// <see cref="IOrderedEnumerable{T}"/> according to a key.
+ /// </summary>
- /// <summary>
- /// Determines whether a specified key is in the <see cref="Lookup{TKey,TElement}" />.
- /// </summary>
+ IOrderedEnumerable<TElement> CreateOrderedEnumerable<TKey>(
+ Func<TElement, TKey> keySelector, IComparer<TKey> comparer, bool descending);
+ }
- public bool Contains(TKey key)
- {
- return _map.ContainsKey(key);
- }
+ /// <summary>
+ /// Represents a collection of keys each mapped to one or more values.
+ /// </summary>
+ internal sealed class Lookup<TKey, TElement> : ILookup<TKey, TElement>
+ {
+ private readonly Dictionary<TKey, IGrouping<TKey, TElement>> _map;
- /// <summary>
- /// Applies a transform function to each key and its associated
- /// values and returns the results.
- /// </summary>
+ internal Lookup(IEqualityComparer<TKey> comparer)
+ {
+ _map = new Dictionary<TKey, IGrouping<TKey, TElement>>(comparer);
+ }
- public IEnumerable<TResult> ApplyResultSelector<TResult>(
- Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
- {
- if (resultSelector == null)
- throw new ArgumentNullException("resultSelector");
-
- foreach (var pair in _map)
- yield return resultSelector(pair.Key, pair.Value);
- }
+ internal void Add(IGrouping<TKey, TElement> item)
+ {
+ _map.Add(item.Key, item);
+ }
- /// <summary>
- /// Returns a generic enumerator that iterates through the <see cref="Lookup{TKey,TElement}" />.
- /// </summary>
+ internal IEnumerable<TElement> Find(TKey key)
+ {
+ IGrouping<TKey, TElement> grouping;
+ return _map.TryGetValue(key, out grouping) ? grouping : null;
+ }
- public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
- {
- return _map.Values.GetEnumerator();
- }
+ /// <summary>
+ /// Gets the number of key/value collection pairs in the <see cref="Lookup{TKey,TElement}" />.
+ /// </summary>
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
+ public int Count
+ {
+ get { return _map.Count; }
}
-}
-// $Id: OrderedEnumerable.cs 237 2010-01-31 12:20:24Z azizatif $
+ /// <summary>
+ /// Gets the collection of values indexed by the specified key.
+ /// </summary>
-namespace LinqBridge
-{
- #region Imports
+ public IEnumerable<TElement> this[TKey key]
+ {
+ get
+ {
+ IGrouping<TKey, TElement> result;
+ return _map.TryGetValue(key, out result) ? result : Enumerable.Empty<TElement>();
+ }
+ }
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
+ /// <summary>
+ /// Determines whether a specified key is in the <see cref="Lookup{TKey,TElement}" />.
+ /// </summary>
+
+ public bool Contains(TKey key)
+ {
+ return _map.ContainsKey(key);
+ }
- #endregion
+ /// <summary>
+ /// Applies a transform function to each key and its associated
+ /// values and returns the results.
+ /// </summary>
- internal sealed class OrderedEnumerable<T, K> : IOrderedEnumerable<T>
+ public IEnumerable<TResult> ApplyResultSelector<TResult>(
+ Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
{
- private readonly IEnumerable<T> _source;
- private readonly List<Comparison<T>> _comparisons;
+ if (resultSelector == null)
+ throw new ArgumentNullException("resultSelector");
- public OrderedEnumerable(IEnumerable<T> source,
- Func<T, K> keySelector, IComparer<K> comparer, bool descending) :
- this(source, null, keySelector, comparer, descending) {}
+ foreach (var pair in _map)
+ yield return resultSelector(pair.Key, pair.Value);
+ }
- private OrderedEnumerable(IEnumerable<T> source, List<Comparison<T>> comparisons,
- Func<T, K> keySelector, IComparer<K> comparer, bool descending)
- {
- if (source == null) throw new ArgumentNullException("source");
- if (keySelector == null) throw new ArgumentNullException("keySelector");
+ /// <summary>
+ /// Returns a generic enumerator that iterates through the <see cref="Lookup{TKey,TElement}" />.
+ /// </summary>
- _source = source;
-
- comparer = comparer ?? Comparer<K>.Default;
+ public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
+ {
+ return _map.Values.GetEnumerator();
+ }
- if (comparisons == null)
- comparisons = new List<Comparison<T>>(/* capacity */ 4);
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return GetEnumerator();
+ }
+ }
- comparisons.Add((x, y)
- => (descending ? -1 : 1) * comparer.Compare(keySelector(x), keySelector(y)));
+ internal sealed class OrderedEnumerable<T, K> : IOrderedEnumerable<T>
+ {
+ private readonly IEnumerable<T> _source;
+ private readonly List<Comparison<T>> _comparisons;
- _comparisons = comparisons;
- }
+ public OrderedEnumerable(IEnumerable<T> source,
+ Func<T, K> keySelector, IComparer<K> comparer, bool descending) :
+ this(source, null, keySelector, comparer, descending)
+ {
+ }
- public IOrderedEnumerable<T> CreateOrderedEnumerable<KK>(
- Func<T, KK> keySelector, IComparer<KK> comparer, bool descending)
- {
- return new OrderedEnumerable<T, KK>(_source, _comparisons, keySelector, comparer, descending);
- }
+ private OrderedEnumerable(IEnumerable<T> source, List<Comparison<T>> comparisons,
+ Func<T, K> keySelector, IComparer<K> comparer, bool descending)
+ {
+ if (source == null) throw new ArgumentNullException("source");
+ if (keySelector == null) throw new ArgumentNullException("keySelector");
- public IEnumerator<T> GetEnumerator()
- {
- //
- // We sort using List<T>.Sort, but docs say that it performs an
- // unstable sort. LINQ, on the other hand, says OrderBy performs
- // a stable sort. So convert the source sequence into a sequence
- // of tuples where the second element tags the position of the
- // element from the source sequence (First). The position is
- // then used as a tie breaker when all keys compare equal,
- // thus making the sort stable.
- //
-
- var list = _source.Select(new Func<T, int, Tuple<T, int>>(TagPosition)).ToList();
-
- list.Sort((x, y) =>
- {
- //
- // Compare keys from left to right.
- //
-
- var comparisons = _comparisons;
- for (var i = 0; i < comparisons.Count; i++)
- {
- var result = comparisons[i](x.First, y.First);
- if (result != 0)
- return result;
- }
-
- //
- // All keys compared equal so now break the tie by their
- // position in the original sequence, making the sort stable.
- //
-
- return x.Second.CompareTo(y.Second);
- });
-
- return list.Select(new Func<Tuple<T, int>, T>(GetFirst)).GetEnumerator();
+ _source = source;
- }
+ comparer = comparer ?? Comparer<K>.Default;
- /// <remarks>
- /// See <a href="http://code.google.com/p/linqbridge/issues/detail?id=11">issue #11</a>
- /// for why this method is needed and cannot be expressed as a
- /// lambda at the call site.
- /// </remarks>
+ if (comparisons == null)
+ comparisons = new List<Comparison<T>>( /* capacity */ 4);
- private static Tuple<T, int> TagPosition(T e, int i)
- {
- return new Tuple<T, int>(e, i);
- }
+ comparisons.Add((x, y)
+ => (descending ? -1 : 1)*comparer.Compare(keySelector(x), keySelector(y)));
- /// <remarks>
- /// See <a href="http://code.google.com/p/linqbridge/issues/detail?id=11">issue #11</a>
- /// for why this method is needed and cannot be expressed as a
- /// lambda at the call site.
- /// </remarks>
+ _comparisons = comparisons;
+ }
- private static T GetFirst(Tuple<T, int> pv)
- {
- return pv.First;
- }
+ public IOrderedEnumerable<T> CreateOrderedEnumerable<KK>(
+ Func<T, KK> keySelector, IComparer<KK> comparer, bool descending)
+ {
+ return new OrderedEnumerable<T, KK>(_source, _comparisons, keySelector, comparer, descending);
+ }
+
+ public IEnumerator<T> GetEnumerator()
+ {
+ //
+ // We sort using List<T>.Sort, but docs say that it performs an
+ // unstable sort. LINQ, on the other hand, says OrderBy performs
+ // a stable sort. So convert the source sequence into a sequence
+ // of tuples where the second element tags the position of the
+ // element from the source sequence (First). The position is
+ // then used as a tie breaker when all keys compare equal,
+ // thus making the sort stable.
+ //
+
+ var list = _source.Select(new Func<T, int, Tuple<T, int>>(TagPosition)).ToList();
+
+ list.Sort((x, y) =>
+ {
+ //
+ // Compare keys from left to right.
+ //
+
+ var comparisons = _comparisons;
+ for (var i = 0; i < comparisons.Count; i++)
+ {
+ var result = comparisons[i](x.First, y.First);
+ if (result != 0)
+ return result;
+ }
+
+ //
+ // All keys compared equal so now break the tie by their
+ // position in the original sequence, making the sort stable.
+ //
+
+ return x.Second.CompareTo(y.Second);
+ });
+
+ return list.Select(new Func<Tuple<T, int>, T>(GetFirst)).GetEnumerator();
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
}
-}
-// $Id: Tuple.cs 215 2009-10-03 13:31:49Z azizatif $
+ /// <remarks>
+ /// See <a href="http://code.google.com/p/linqbridge/issues/detail?id=11">issue #11</a>
+ /// for why this method is needed and cannot be expressed as a
+ /// lambda at the call site.
+ /// </remarks>
-namespace LinqBridge
-{
- #region Imports
+ private static Tuple<T, int> TagPosition(T e, int i)
+ {
+ return new Tuple<T, int>(e, i);
+ }
- using System;
- using System.Collections.Generic;
- using System.Text;
+ /// <remarks>
+ /// See <a href="http://code.google.com/p/linqbridge/issues/detail?id=11">issue #11</a>
+ /// for why this method is needed and cannot be expressed as a
+ /// lambda at the call site.
+ /// </remarks>
- #endregion
+ private static T GetFirst(Tuple<T, int> pv)
+ {
+ return pv.First;
+ }
- [ Serializable ]
- internal struct Tuple<TFirst, TSecond> : IEquatable<Tuple<TFirst, TSecond>>
+ IEnumerator IEnumerable.GetEnumerator()
{
- public TFirst First { get; private set; }
- public TSecond Second { get; private set; }
+ return GetEnumerator();
+ }
+ }
- public Tuple(TFirst first, TSecond second) : this()
- {
- First = first;
- Second = second;
- }
+ [Serializable]
+ internal struct Tuple<TFirst, TSecond> : IEquatable<Tuple<TFirst, TSecond>>
+ {
+ public TFirst First { get; private set; }
+ public TSecond Second { get; private set; }
- public override bool Equals(object obj)
- {
- return obj != null
- && obj is Tuple<TFirst, TSecond>
- && base.Equals((Tuple<TFirst, TSecond>) obj);
- }
+ public Tuple(TFirst first, TSecond second)
+ : this()
+ {
+ First = first;
+ Second = second;
+ }
- public bool Equals(Tuple<TFirst, TSecond> other)
- {
- return EqualityComparer<TFirst>.Default.Equals(other.First, First)
- && EqualityComparer<TSecond>.Default.Equals(other.Second, Second);
- }
+ public override bool Equals(object obj)
+ {
+ return obj != null
+ && obj is Tuple<TFirst, TSecond>
+ && base.Equals((Tuple<TFirst, TSecond>) obj);
+ }
- public override int GetHashCode()
- {
- var num = 0x7a2f0b42;
- num = (-1521134295 * num) + EqualityComparer<TFirst>.Default.GetHashCode(First);
- return (-1521134295 * num) + EqualityComparer<TSecond>.Default.GetHashCode(Second);
- }
+ public bool Equals(Tuple<TFirst, TSecond> other)
+ {
+ return EqualityComparer<TFirst>.Default.Equals(other.First, First)
+ && EqualityComparer<TSecond>.Default.Equals(other.Second, Second);
+ }
- public override string ToString()
- {
- return string.Format(@"{{ First = {0}, Second = {1} }}", First, Second);
- }
+ public override int GetHashCode()
+ {
+ var num = 0x7a2f0b42;
+ num = (-1521134295*num) + EqualityComparer<TFirst>.Default.GetHashCode(First);
+ return (-1521134295*num) + EqualityComparer<TSecond>.Default.GetHashCode(Second);
}
+
+ public override string ToString()
+ {
+ return string.Format(@"{{ First = {0}, Second = {1} }}", First, Second);
+ }
+ }
}
-// $Id: Action.cs 239 2010-02-05 23:26:23Z azizatif $
+namespace Newtonsoft.Json.Serialization
+{
+ public delegate TResult Func<TResult>();
+
+ public delegate TResult Func<T, TResult>(T a);
-namespace System
+ public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
+
+ public delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3);
+
+ public delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
+
+ public delegate void Action();
+
+ public delegate void Action<T1, T2>(T1 arg1, T2 arg2);
+
+ public delegate void Action<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3);
+
+ public delegate void Action<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
+}
+
+namespace System.Runtime.CompilerServices
{
-#if LINQBRIDGE_LIB
- public delegate void Action();
- public delegate void Action<T1, T2>(T1 arg1, T2 arg2);
- public delegate void Action<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3);
- public delegate void Action<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
-#else
- delegate void Action();
- delegate void Action<T1, T2>(T1 arg1, T2 arg2);
- delegate void Action<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3);
- delegate void Action<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
-#endif
+ /// <remarks>
+ /// This attribute allows us to define extension methods without
+ /// requiring .NET Framework 3.5. For more information, see the section,
+ /// <a href="http://msdn.microsoft.com/en-us/magazine/cc163317.aspx#S7">Extension Methods in .NET Framework 2.0 Apps</a>,
+ /// of <a href="http://msdn.microsoft.com/en-us/magazine/cc163317.aspx">Basic Instincts: Extension Methods</a>
+ /// column in <a href="http://msdn.microsoft.com/msdnmag/">MSDN Magazine</a>,
+ /// issue <a href="http://msdn.microsoft.com/en-us/magazine/cc135410.aspx">Nov 2007</a>.
+ /// </remarks>
+
+ [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
+ internal sealed class ExtensionAttribute : Attribute { }
}
#endif \ No newline at end of file