http://code.google.com/p/gurtle/source/detail?r=227
Added:
/trunk/lib/LinqBridge-1.1.cs
Deleted:
/trunk/lib/backlinq-1.0.cs
Modified:
/trunk/src/Gurtle/Gurtle.csproj
=======================================
--- /dev/null
+++ /trunk/lib/LinqBridge-1.1.cs Thu Jan 21 13:43:48 2010
@@ -0,0 +1,3053 @@
+#region License, Terms and Author(s)
+//
+// LINQBridge
+// Copyright (c) 2007-9 Atif Aziz, Joseph Albahari. All rights reserved.
+//
+// Author(s):
+//
+// Atif Aziz, http://www.raboof.com
+//
+// This library is free software; you can redistribute it and/or modify it
+// under the terms of the New BSD License, a copy of which should have
+// been delivered along with this distribution.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+#endregion
+
+// $Id: Enumerable.cs 224 2009-10-04 07:13:08Z azizatif $
+
+namespace System.Linq
+{
+ #region Imports
+
+ using System;
+ using System.Collections;
+ using System.Collections.Generic;
+ using System.Diagnostics;
+ using LinqBridge;
+
+ #endregion
+
+ /// <summary>
+ /// Provides a set of static (Shared in Visual Basic) methods for
+ /// querying objects that implement <see cref="IEnumerable{T}" />.
+ /// </summary>
+
+ 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;
+ }
+
+ /// <summary>
+ /// Returns an empty <see cref="IEnumerable{T}"/> that has the
+ /// specified type argument.
+ /// </summary>
+
+ public static IEnumerable<TResult> Empty<TResult>()
+ {
+ return Sequence<TResult>.Empty;
+ }
+
+ /// <summary>
+ /// Converts the elements of an <see cref="IEnumerable"/> to the
+ /// specified type.
+ /// </summary>
+
+ public static IEnumerable<TResult> Cast<TResult>(
+ this IEnumerable source)
+ {
+ CheckNotNull(source, "source");
+
+ return CastYield<TResult>(source);
+ }
+
+ private static IEnumerable<TResult> CastYield<TResult>(
+ IEnumerable source)
+ {
+ foreach (var item in source)
+ yield return (TResult) item;
+ }
+
+ /// <summary>
+ /// Filters the elements of an <see cref="IEnumerable"/> based on
a specified type.
+ /// </summary>
+
+ public static IEnumerable<TResult> OfType<TResult>(
+ this IEnumerable source)
+ {
+ CheckNotNull(source, "source");
+
+ return OfTypeYield<TResult>(source);
+ }
+
+ private static IEnumerable<TResult> OfTypeYield<TResult>(
+ IEnumerable source)
+ {
+ foreach (var item in source)
+ if (item is TResult)
+ 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>
+
+ public static IEnumerable<int> Range(int start, 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 RangeYield(start, end);
+ }
+
+ private static IEnumerable<int> RangeYield(int start, long end)
+ {
+ for (var i = start; i < end; i++)
+ yield return i;
+ }
+
+ /// <summary>
+ /// Generates a sequence that contains one repeated value.
+ /// </summary>
+
+ public static IEnumerable<TResult> Repeat<TResult>(TResult
element, int count)
+ {
+ if (count < 0) throw new ArgumentOutOfRangeException("count",
count, null);
+
+ return RepeatYield(element, count);
+ }
+
+ private static IEnumerable<TResult> RepeatYield<TResult>(TResult
element, int count)
+ {
+ for (var i = 0; i < count; i++)
+ yield return element;
+ }
+
+ /// <summary>
+ /// Filters a sequence of values based on a predicate.
+ /// </summary>
+
+ public static IEnumerable<TSource> Where<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ CheckNotNull(predicate, "predicate");
+
+ return source.Where((item, i) => predicate(item));
+ }
+
+ /// <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<TSource> Where<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, bool> predicate)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(predicate, "predicate");
+
+ return WhereYield(source, 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;
+ }
+
+ /// <summary>
+ /// Projects each element of a sequence into a new form.
+ /// </summary>
+
+ public static IEnumerable<TResult> Select<TSource, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TResult> selector)
+ {
+ CheckNotNull(selector, "selector");
+
+ return source.Select((item, i) => selector(item));
+ }
+
+ /// <summary>
+ /// Projects each element of a sequence into a new form by
+ /// incorporating the element's index.
+ /// </summary>
+
+ public static IEnumerable<TResult> Select<TSource, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, TResult> selector)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(selector, "selector");
+
+ return SelectYield(source, 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++);
+ }
+
+ /// <summary>
+ /// Projects each element of a sequence to an <see
cref="IEnumerable{T}" />
+ /// and flattens the resulting sequences into one sequence.
+ /// </summary>
+
+ public static IEnumerable<TResult> SelectMany<TSource, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, IEnumerable<TResult>> selector)
+ {
+ CheckNotNull(selector, "selector");
+
+ return source.SelectMany((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. The
+ /// index of each source element is used in the projected form of
+ /// that element.
+ /// </summary>
+
+ public static IEnumerable<TResult> SelectMany<TSource, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, IEnumerable<TResult>> selector)
+ {
+ CheckNotNull(selector, "selector");
+
+ return source.SelectMany(selector, (item, subitem) => subitem);
+ }
+
+ /// <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>
+
+ 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");
+
+ return source.SelectMany((item, i) =>
collectionSelector(item), 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. 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");
+
+ return SelectManyYield(source, collectionSelector,
resultSelector);
+ }
+
+ 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.
+ /// </summary>
+
+ public static IEnumerable<TSource> TakeWhile<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ CheckNotNull(predicate, "predicate");
+
+ return source.TakeWhile((item, i) => predicate(item));
+ }
+
+ /// <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>
+
+ public static IEnumerable<TSource> TakeWhile<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, bool> predicate)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(predicate, "predicate");
+
+ return TakeWhileYield(source, predicate);
+ }
+
+ 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;
+ }
+
+ private static class Futures<T>
+ {
+ public static readonly Func<T> Default = () => default(T);
+ public static readonly Func<T> Undefined = () => { throw new
InvalidOperationException(); };
+ }
+
+ /// <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);
+
+ var list = source as IList<TSource>; // optimized case for
lists
+ if (list != null)
+ return list.Count > 0 ? list[0] : empty();
+
+ using (var e = source.GetEnumerator()) // fallback for
enumeration
+ return e.MoveNext() ? e.Current : empty();
+ }
+
+ /// <summary>
+ /// Returns the first element of a sequence.
+ /// </summary>
+
+ public static TSource First<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return source.FirstImpl(Futures<TSource>.Undefined);
+ }
+
+ /// <summary>
+ /// Returns the first element in a sequence that satisfies a
specified condition.
+ /// </summary>
+
+ public static TSource First<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return First(source.Where(predicate));
+ }
+
+ /// <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)
+ {
+ return source.FirstImpl(Futures<TSource>.Default);
+ }
+
+ /// <summary>
+ /// Returns the first element of the sequence that satisfies a
+ /// condition or a default value if no such element is found.
+ /// </summary>
+
+ public static TSource FirstOrDefault<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return FirstOrDefault(source.Where(predicate));
+ }
+
+ /// <summary>
+ /// Base implementation of Last operator.
+ /// </summary>
+
+ private static TSource LastImpl<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource> empty)
+ {
+ CheckNotNull(source, "source");
+
+ var list = source as IList<TSource>; // optimized case for
lists
+ if (list != null)
+ return list.Count > 0 ? list[list.Count - 1] : empty();
+
+ using (var e = source.GetEnumerator())
+ {
+ if (!e.MoveNext())
+ return empty();
+
+ var last = e.Current;
+ while (e.MoveNext())
+ last = e.Current;
+
+ return last;
+ }
+ }
+
+ /// <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 that satisfies a
+ /// specified condition.
+ /// </summary>
+
+ 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, or a default value if
+ /// the sequence contains no elements.
+ /// </summary>
+
+ public static TSource LastOrDefault<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return source.LastImpl(Futures<TSource>.Default);
+ }
+
+ /// <summary>
+ /// Returns the last element of a sequence that satisfies a
+ /// condition or a default value if no such element is found.
+ /// </summary>
+
+ public static TSource LastOrDefault<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return LastOrDefault(source.Where(predicate));
+ }
+
+ /// <summary>
+ /// Base implementation of Single operator.
+ /// </summary>
+
+ private static TSource SingleImpl<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource> empty)
+ {
+ CheckNotNull(source, "source");
+
+ using (var e = source.GetEnumerator())
+ {
+ if (e.MoveNext())
+ {
+ var single = e.Current;
+ if (!e.MoveNext())
+ return single;
+
+ throw new InvalidOperationException();
+ }
+
+ return empty();
+ }
+ }
+
+ /// <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)
+ {
+ return source.SingleImpl(Futures<TSource>.Undefined);
+ }
+
+ /// <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 Single<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return Single(source.Where(predicate));
+ }
+
+ /// <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)
+ {
+ return source.SingleImpl(Futures<TSource>.Default);
+ }
+
+ /// <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 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.
+ /// </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);
+ }
+ }
+
+ /// <summary>
+ /// Returns the element at a specified index in a sequence or a
+ /// default value if the index is out of range.
+ /// </summary>
+
+ public static TSource ElementAtOrDefault<TSource>(
+ this IEnumerable<TSource> source,
+ int index)
+ {
+ CheckNotNull(source, "source");
+
+ if (index < 0)
+ return default(TSource);
+
+ var list = source as IList<TSource>;
+ if (list != null)
+ return index < list.Count ? list[index] : default(TSource);
+
+ return source.SkipWhile((item, i) => i <
index).FirstOrDefault();
+ }
+
+ /// <summary>
+ /// Inverts the order of the elements in a sequence.
+ /// </summary>
+
+ public static IEnumerable<TSource> Reverse<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ CheckNotNull(source, "source");
+
+ return ReverseYield(source);
+ }
+
+ private static IEnumerable<TSource>
ReverseYield<TSource>(IEnumerable<TSource> source)
+ {
+ var stack = new Stack<TSource>();
+ foreach (var item in source)
+ stack.Push(item);
+
+ foreach (var item in stack)
+ yield return item;
+ }
+
+ /// <summary>
+ /// Returns a specified number of contiguous elements from the
start
+ /// of a sequence.
+ /// </summary>
+
+ public static IEnumerable<TSource> Take<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>
+
+ public static IEnumerable<TSource> Skip<TSource>(
+ this IEnumerable<TSource> source,
+ int count)
+ {
+ return source.Where((item, i) => i >= count);
+ }
+
+ /// <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> SkipWhile<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ CheckNotNull(predicate, "predicate");
+
+ return source.SkipWhile((item, i) => predicate(item));
+ }
+
+ /// <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>
+
+ public static IEnumerable<TSource> SkipWhile<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, bool> predicate)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(predicate, "predicate");
+
+ return SkipWhileYield(source, 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++)
+ {
+ if (!e.MoveNext())
+ yield break;
+
+ if (!predicate(e.Current, i))
+ break;
+ }
+
+ do { yield return e.Current; } while (e.MoveNext());
+ }
+ }
+
+ /// <summary>
+ /// Returns the number of elements in a sequence.
+ /// </summary>
+
+ 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));
+ }
+
+ /// <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));
+ }
+
+ /// <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");
+
+ 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>
+
+ public static long LongCount<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return LongCount(source.Where(predicate));
+ }
+
+ /// <summary>
+ /// Concatenates two sequences.
+ /// </summary>
+
+ public static IEnumerable<TSource> Concat<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second)
+ {
+ CheckNotNull(first, "first");
+ CheckNotNull(second, "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;
+
+ foreach (var item in second)
+ yield return item;
+ }
+
+ /// <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");
+
+ return new List<TSource>(source);
+ }
+
+ /// <summary>
+ /// Creates an array from an <see cref="IEnumerable{T}"/>.
+ /// </summary>
+
+ 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>
+
+ 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>
+
+ public static IEnumerable<TSource> Distinct<TSource>(
+ this IEnumerable<TSource> source,
+ IEqualityComparer<TSource> comparer)
+ {
+ CheckNotNull(source, "source");
+
+ return DistinctYield(source, comparer);
+ }
+
+ private static IEnumerable<TSource> DistinctYield<TSource>(
+ IEnumerable<TSource> source,
+ IEqualityComparer<TSource> comparer)
+ {
+ var set = new Dictionary<TSource, object>(comparer);
+
+ foreach (var item in source)
+ {
+ if (set.ContainsKey(item))
+ continue;
+
+ set.Add(item, null);
+ yield return item;
+ }
+ }
+
+ /// <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)
+ {
+ return ToLookup(source, keySelector, e => e, /* 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>
+
+ 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 specified key
+ /// and element selector functions.
+ /// </summary>
+
+ 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, 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;
+ }
+
+ /// <summary>
+ /// Groups the elements of a sequence according to a specified key
+ /// selector function.
+ /// </summary>
+
+ 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 compares the keys by using a specified
+ /// comparer.
+ /// </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);
+ }
+
+ /// <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>
+
+ 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);
+ }
+
+ /// <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");
+
+ return ToLookup(source, keySelector, elementSelector,
comparer);
+ }
+
+ /// <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>
+
+ 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>
+ /// 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>(
***The diff for this file has been truncated for email.***
=======================================
--- /trunk/lib/backlinq-1.0.cs Wed Feb 4 15:17:19 2009
+++ /dev/null
@@ -1,3144 +0,0 @@
-#region License, Terms and Author(s)
-//
-// BackLINQ
-// Copyright (c) 2008 Atif Aziz. All rights reserved.
-//
-// Author(s):
-//
-// Atif Aziz, http://www.raboof.com
-//
-// New BSD License
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions
-// are met:
-//
-// - Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-//
-// - Redistributions in binary form must reproduce the above copyright
-// notice, this list of conditions and the following disclaimer in
-// the documentation and/or other materials provided with the
-// distribution.
-//
-// - Neither the name of the original author (Atif Aziz) nor the names
-// of its contributors may be used to endorse or promote products
-// derived from this software without specific prior written
-// permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-// POSSIBILITY OF SUCH DAMAGE.
-//
-#endregion
-
-// $Id: Enumerable.cs 214 2008-11-17 09:27:41Z azizatif $
-
-namespace System.Linq
-{
- #region Imports
-
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using BackLinq;
-
- #endregion
-
- /// <summary>
- /// Provides a set of static (Shared in Visual Basic) methods for
- /// querying objects that implement <see cref="IEnumerable{T}" />.
- /// </summary>
-
- #region Access modifier
- #if BACKLINQ_LIB
- public
- #else
- internal
- #endif
- #endregion
-
- 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;
- }
-
- /// <summary>
- /// Returns an empty <see cref="IEnumerable{T}"/> that has the
- /// specified type argument.
- /// </summary>
-
- public static IEnumerable<TResult> Empty<TResult>()
- {
- return Sequence<TResult>.Empty;
- }
-
- /// <summary>
- /// Converts the elements of an <see cref="IEnumerable"/> to the
- /// specified type.
- /// </summary>
-
- public static IEnumerable<TResult> Cast<TResult>(
- this IEnumerable source)
- {
- CheckNotNull(source, "source");
-
- return CastYield<TResult>(source);
- }
-
- private static IEnumerable<TResult> CastYield<TResult>(
- IEnumerable source)
- {
- foreach (var item in source)
- yield return (TResult) item;
- }
-
- /// <summary>
- /// Filters the elements of an <see cref="IEnumerable"/> based on
a specified type.
- /// </summary>
-
- public static IEnumerable<TResult> OfType<TResult>(
- this IEnumerable source)
- {
- CheckNotNull(source, "source");
-
- return OfTypeYield<TResult>(source);
- }
-
- private static IEnumerable<TResult> OfTypeYield<TResult>(
- IEnumerable source)
- {
- foreach (var item in source)
- if (item is TResult)
- 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>
-
- public static IEnumerable<int> Range(int start, 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 RangeYield(start, end);
- }
-
- private static IEnumerable<int> RangeYield(int start, long end)
- {
- for (var i = start; i < end; i++)
- yield return i;
- }
-
- /// <summary>
- /// Generates a sequence that contains one repeated value.
- /// </summary>
-
- public static IEnumerable<TResult> Repeat<TResult>(TResult
element, int count)
- {
- if (count < 0) throw new ArgumentOutOfRangeException("count",
count, null);
-
- return RepeatYield(element, count);
- }
-
- private static IEnumerable<TResult> RepeatYield<TResult>(TResult
element, int count)
- {
- for (var i = 0; i < count; i++)
- yield return element;
- }
-
- /// <summary>
- /// Filters a sequence of values based on a predicate.
- /// </summary>
-
- public static IEnumerable<TSource> Where<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- CheckNotNull(predicate, "predicate");
-
- return source.Where((item, i) => predicate(item));
- }
-
- /// <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<TSource> Where<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int, bool> predicate)
- {
- CheckNotNull(source, "source");
- CheckNotNull(predicate, "predicate");
-
- return WhereYield(source, 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;
- }
-
- /// <summary>
- /// Projects each element of a sequence into a new form.
- /// </summary>
-
- public static IEnumerable<TResult> Select<TSource, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, TResult> selector)
- {
- CheckNotNull(selector, "selector");
-
- return source.Select((item, i) => selector(item));
- }
-
- /// <summary>
- /// Projects each element of a sequence into a new form by
- /// incorporating the element's index.
- /// </summary>
-
- public static IEnumerable<TResult> Select<TSource, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, int, TResult> selector)
- {
- CheckNotNull(source, "source");
- CheckNotNull(selector, "selector");
-
- return SelectYield(source, 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++);
- }
-
- /// <summary>
- /// Projects each element of a sequence to an <see
cref="IEnumerable{T}" />
- /// and flattens the resulting sequences into one sequence.
- /// </summary>
-
- public static IEnumerable<TResult> SelectMany<TSource, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, IEnumerable<TResult>> selector)
- {
- CheckNotNull(selector, "selector");
-
- return source.SelectMany((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. The
- /// index of each source element is used in the projected form of
- /// that element.
- /// </summary>
-
- public static IEnumerable<TResult> SelectMany<TSource, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, int, IEnumerable<TResult>> selector)
- {
- CheckNotNull(selector, "selector");
-
- return source.SelectMany(selector, (item, subitem) => subitem);
- }
-
- /// <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>
-
- 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");
-
- return source.SelectMany((item, i) =>
collectionSelector(item), 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. 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");
-
- return SelectManyYield(source, collectionSelector,
resultSelector);
- }
-
- 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.
- /// </summary>
-
- public static IEnumerable<TSource> TakeWhile<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- CheckNotNull(predicate, "predicate");
-
- return source.TakeWhile((item, i) => predicate(item));
- }
-
- /// <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>
-
- public static IEnumerable<TSource> TakeWhile<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int, bool> predicate)
- {
- CheckNotNull(source, "source");
- CheckNotNull(predicate, "predicate");
-
- return TakeWhileYield(source, predicate);
- }
-
- 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;
- }
-
- private static class Futures<T>
- {
- public static readonly Func<T> Default = () => default(T);
- public static readonly Func<T> Undefined = () => { throw new
InvalidOperationException(); };
- }
-
- /// <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);
-
- var list = source as IList<TSource>; // optimized case for
lists
- if (list != null)
- return list.Count > 0 ? list[0] : empty();
-
- using (var e = source.GetEnumerator()) // fallback for
enumeration
- return e.MoveNext() ? e.Current : empty();
- }
-
- /// <summary>
- /// Returns the first element of a sequence.
- /// </summary>
-
- public static TSource First<TSource>(
- this IEnumerable<TSource> source)
- {
- return source.FirstImpl(Futures<TSource>.Undefined);
- }
-
- /// <summary>
- /// Returns the first element in a sequence that satisfies a
specified condition.
- /// </summary>
-
- public static TSource First<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return First(source.Where(predicate));
- }
-
- /// <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)
- {
- return source.FirstImpl(Futures<TSource>.Default);
- }
-
- /// <summary>
- /// Returns the first element of the sequence that satisfies a
- /// condition or a default value if no such element is found.
- /// </summary>
-
- public static TSource FirstOrDefault<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return FirstOrDefault(source.Where(predicate));
- }
-
- /// <summary>
- /// Base implementation of Last operator.
- /// </summary>
-
- private static TSource LastImpl<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource> empty)
- {
- CheckNotNull(source, "source");
-
- var list = source as IList<TSource>; // optimized case for
lists
- if (list != null)
- return list.Count > 0 ? list[list.Count - 1] : empty();
-
- using (var e = source.GetEnumerator())
- {
- if (!e.MoveNext())
- return empty();
-
- var last = e.Current;
- while (e.MoveNext())
- last = e.Current;
-
- return last;
- }
- }
-
- /// <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 that satisfies a
- /// specified condition.
- /// </summary>
-
- 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, or a default value if
- /// the sequence contains no elements.
- /// </summary>
-
- public static TSource LastOrDefault<TSource>(
- this IEnumerable<TSource> source)
- {
- return source.LastImpl(Futures<TSource>.Default);
- }
-
- /// <summary>
- /// Returns the last element of a sequence that satisfies a
- /// condition or a default value if no such element is found.
- /// </summary>
-
- public static TSource LastOrDefault<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return LastOrDefault(source.Where(predicate));
- }
-
- /// <summary>
- /// Base implementation of Single operator.
- /// </summary>
-
- private static TSource SingleImpl<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource> empty)
- {
- CheckNotNull(source, "source");
-
- using (var e = source.GetEnumerator())
- {
- if (e.MoveNext())
- {
- var single = e.Current;
- if (!e.MoveNext())
- return single;
-
- throw new InvalidOperationException();
- }
-
- return empty();
- }
- }
-
- /// <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)
- {
- return source.SingleImpl(Futures<TSource>.Undefined);
- }
-
- /// <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 Single<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return Single(source.Where(predicate));
- }
-
- /// <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)
- {
- return source.SingleImpl(Futures<TSource>.Default);
- }
-
- /// <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 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.
- /// </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);
- }
- }
-
- /// <summary>
- /// Returns the element at a specified index in a sequence or a
- /// default value if the index is out of range.
- /// </summary>
-
- public static TSource ElementAtOrDefault<TSource>(
- this IEnumerable<TSource> source,
- int index)
- {
- CheckNotNull(source, "source");
-
- if (index < 0)
- return default(TSource);
-
- var list = source as IList<TSource>;
- if (list != null)
- return index < list.Count ? list[index] : default(TSource);
-
- return source.SkipWhile((item, i) => i <
index).FirstOrDefault();
- }
-
- /// <summary>
- /// Inverts the order of the elements in a sequence.
- /// </summary>
-
- public static IEnumerable<TSource> Reverse<TSource>(
- this IEnumerable<TSource> source)
- {
- CheckNotNull(source, "source");
-
- return ReverseYield(source);
- }
-
- private static IEnumerable<TSource>
ReverseYield<TSource>(IEnumerable<TSource> source)
- {
- var stack = new Stack<TSource>();
- foreach (var item in source)
- stack.Push(item);
-
- foreach (var item in stack)
- yield return item;
- }
-
- /// <summary>
- /// Returns a specified number of contiguous elements from the
start
- /// of a sequence.
- /// </summary>
-
- public static IEnumerable<TSource> Take<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>
-
- public static IEnumerable<TSource> Skip<TSource>(
- this IEnumerable<TSource> source,
- int count)
- {
- return source.Where((item, i) => i >= count);
- }
-
- /// <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> SkipWhile<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- CheckNotNull(predicate, "predicate");
-
- return source.SkipWhile((item, i) => predicate(item));
- }
-
- /// <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>
-
- public static IEnumerable<TSource> SkipWhile<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int, bool> predicate)
- {
- CheckNotNull(source, "source");
- CheckNotNull(predicate, "predicate");
-
- return SkipWhileYield(source, 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++)
- {
- if (!e.MoveNext())
- yield break;
-
- if (!predicate(e.Current, i))
- break;
- }
-
- do { yield return e.Current; } while (e.MoveNext());
- }
- }
-
- /// <summary>
- /// Returns the number of elements in a sequence.
- /// </summary>
-
- 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));
- }
-
- /// <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));
- }
-
- /// <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");
-
- 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>
-
- public static long LongCount<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return LongCount(source.Where(predicate));
- }
-
- /// <summary>
- /// Concatenates two sequences.
- /// </summary>
-
- public static IEnumerable<TSource> Concat<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second)
- {
- CheckNotNull(first, "first");
- CheckNotNull(second, "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;
-
- foreach (var item in second)
- yield return item;
- }
-
- /// <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");
-
- return new List<TSource>(source);
- }
-
- /// <summary>
- /// Creates an array from an <see cref="IEnumerable{T}"/>.
- /// </summary>
-
- 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>
-
- 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>
-
- public static IEnumerable<TSource> Distinct<TSource>(
- this IEnumerable<TSource> source,
- IEqualityComparer<TSource> comparer)
- {
- CheckNotNull(source, "source");
-
- return DistinctYield(source, comparer);
- }
-
- private static IEnumerable<TSource> DistinctYield<TSource>(
- IEnumerable<TSource> source,
- IEqualityComparer<TSource> comparer)
- {
- var set = new Dictionary<TSource, object>(comparer);
-
- foreach (var item in source)
- {
- if (set.ContainsKey(item))
- continue;
-
- set.Add(item, null);
- yield return item;
- }
- }
-
- /// <summary>
- /// Creates a <see cref="Lookup{TKey,TElement}" /> from an
- /// <see cref="IEnumerable{T}" /> according to a specified key
- /// selector function.
- /// </summary>
-
- public static Lookup<TKey, TSource> ToLookup<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector)
- {
- return ToLookup(source, keySelector, e => e, /* 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>
-
- public static Lookup<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 specified key
- /// and element selector functions.
- /// </summary>
-
- public static Lookup<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, a comparer and an element selector function.
- /// </summary>
-
- public static Lookup<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;
- }
-
- /// <summary>
- /// Groups the elements of a sequence according to a specified key
- /// selector function.
- /// </summary>
-
- 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 compares the keys by using a specified
- /// comparer.
- /// </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);
- }
-
- /// <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>
-
- 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);
- }
-
- /// <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");
-
- return ToLookup(source, keySelector, elementSelector,
comparer);
***The diff for this file has been truncated for email.***
=======================================
--- /trunk/src/Gurtle/Gurtle.csproj Fri Oct 23 11:20:52 2009
+++ /trunk/src/Gurtle/Gurtle.csproj Thu Jan 21 13:43:48 2010
@@ -44,8 +44,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
- <Compile Include="..\..\lib\backlinq-1.0.cs">
- <Link>backlinq-1.0.cs</Link>
+ <Compile Include="..\..\lib\LinqBridge-1.1.cs">
+ <Link>LinqBridge-1.1.cs</Link>
</Compile>
<Compile Include="Actions.cs" />
<Compile Include="AppPaths.cs" />