2008-09-11 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / System.Core / System.Linq / Enumerable.cs
index 457c0a08433a310fd18cd80d9b73aabf965f785a..4d46b21bc0a431d5927645d9739417f6694e4e10 100644 (file)
@@ -332,8 +332,8 @@ namespace System.Linq
 
                static IEnumerable<TResult> CreateCastIterator<TResult> (IEnumerable source)
                {
-                       foreach (object element in source)
-                               yield return (TResult) element;
+                       foreach (TResult element in source)
+                               yield return element;
                }
 
                #endregion
@@ -579,7 +579,20 @@ namespace System.Linq
                {
                        Check.Source (source);
 
-                       return source.First (PredicateOf<TSource>.Always, Fallback.Throw);
+                       var list = source as IList<TSource>;
+                       if (list != null) {
+                               if (list.Count != 0)
+                                       return list [0];
+
+                               throw new InvalidOperationException ();
+                       } else {
+                               using (var enumerator = source.GetEnumerator ()) {
+                                       if (enumerator.MoveNext ())
+                                               return enumerator.Current;
+                               }
+                       }
+
+                       throw new InvalidOperationException ();
                }
 
                public static TSource First<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
@@ -633,6 +646,12 @@ namespace System.Linq
                {
                        Check.SourceAndKeySelector (source, keySelector);
 
+                       return CreateGroupByIterator (source, keySelector, comparer);
+               }
+
+               static IEnumerable<IGrouping<TKey, TSource>> CreateGroupByIterator<TSource, TKey> (this IEnumerable<TSource> source,
+                       Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
+               {
                        Dictionary<TKey, List<TSource>> groups = new Dictionary<TKey, List<TSource>> ();
                        List<TSource> nullList = new List<TSource> ();
                        int counter = 0;
@@ -670,7 +689,6 @@ namespace System.Linq
                        }
                }
 
-
                public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement> (this IEnumerable<TSource> source,
                        Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
                {
@@ -778,6 +796,14 @@ namespace System.Linq
                        if (comparer == null)
                                comparer = EqualityComparer<TKey>.Default;
 
+                       return CreateGroupJoinIterator (outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
+               }
+
+               static IEnumerable<TResult> CreateGroupJoinIterator<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)
+               {
                        ILookup<TKey, TInner> innerKeys = ToLookup<TInner, TKey> (inner, innerKeySelector, comparer);
                        /*Dictionary<K, List<U>> innerKeys = new Dictionary<K, List<U>> ();
                        foreach (U element in inner)
@@ -1043,7 +1069,7 @@ namespace System.Linq
 
                static T? IterateNullable<T> (IEnumerable<T?> source, T initValue, Func<T?, T?, bool> selector) where T : struct
                {
-                       int counter = 0;
+                       bool empty = true;
                        T? value = initValue;
                        foreach (var element in source) {
                                if (!element.HasValue)
@@ -1051,10 +1077,11 @@ namespace System.Linq
 
                                if (selector (element.Value, value))
                                        value = element;
-                               ++counter;
+
+                               empty = false;
                        }
 
-                       if (counter == 0)
+                       if (empty)
                                return null;
 
                        return value;
@@ -1129,13 +1156,13 @@ namespace System.Linq
 
                static U Iterate<T, U> (IEnumerable<T> source, U initValue, Func<T, U, U> selector)
                {
-                       int counter = 0;
+                       bool empty = true;
                        foreach (var element in source) {
                                initValue = selector (element, initValue);
-                               ++counter;
+                               empty = false;
                        }
 
-                       if (counter == 0)
+                       if (empty)
                                throw new InvalidOperationException ();
 
                        return initValue;
@@ -1143,17 +1170,17 @@ namespace System.Linq
 
                static U? IterateNullable<T, U> (IEnumerable<T> source, U initialValue, Func<T, U?, U?> selector) where U : struct
                {
-                       int counter = 0;
+                       bool empty = true;
                        U? value = initialValue;
                        foreach (var element in source) {
                                value = selector (element, value);
                                if (!value.HasValue)
                                        continue;
 
-                               ++counter;
+                               empty = false;
                        }
 
-                       if (counter == 0)
+                       if (empty)
                                return null;
 
                        return value;
@@ -2003,10 +2030,10 @@ namespace System.Linq
 
                        int counter = 0;
                        foreach (TSource element in source) {
-                               if (counter++ == count)
-                                       yield break;
-
                                yield return element;
+
+                               if (++counter == count)
+                                       yield break;
                        }
                }