2009-02-13 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Core / System.Linq / Enumerable.cs
index 3ac3fc5a4a2c0798b9c5d01949ff5c44e831ec63..820790a0f4d09df3093f3ff9b0cd82d3988fd268 100644 (file)
@@ -49,6 +49,10 @@ namespace System.Linq
                        public static readonly Func<T, bool> Always = (t) => true;
                }
 
+               class Function<T> {
+                       public static readonly Func<T, T> Identity = (t) => t;
+               }
+
                #region Aggregate
 
                public static TSource Aggregate<TSource> (this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func)
@@ -116,6 +120,10 @@ namespace System.Linq
                {
                        Check.Source (source);
 
+                       var collection = source as ICollection<TSource>;
+                       if (collection != null)
+                               return collection.Count > 0;
+
                        using (var enumerator = source.GetEnumerator ())
                                return enumerator.MoveNext ();
                }
@@ -225,7 +233,7 @@ namespace System.Linq
                {
                        Check.Source (source);
 
-                       return source.AverageNullable<long, long, double> ((a, b) => a + b, (a, b) => a / b);
+                       return source.AverageNullable<long, long, double> ((a, b) => a + b, (a, b) => (double) a / b);
                }
 
                public static double? Average (this IEnumerable<double?> source)
@@ -332,8 +340,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 +587,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)
@@ -914,6 +935,14 @@ namespace System.Linq
                {
                        Check.Source (source);
 
+                       var collection = source as ICollection<TSource>;
+                       if (collection != null && collection.Count == 0)
+                               throw new InvalidOperationException ();
+
+                       var list = source as IList<TSource>;
+                       if (list != null)
+                               return list [list.Count - 1];
+
                        return source.Last (PredicateOf<TSource>.Always, Fallback.Throw);
                }
 
@@ -1056,7 +1085,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)
@@ -1064,10 +1093,11 @@ namespace System.Linq
 
                                if (selector (element.Value, value))
                                        value = element;
-                               ++counter;
+
+                               empty = false;
                        }
 
-                       if (counter == 0)
+                       if (empty)
                                return null;
 
                        return value;
@@ -1142,13 +1172,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;
@@ -1156,17 +1186,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;
@@ -1825,21 +1855,21 @@ namespace System.Linq
                {
                        Check.Source (source);
 
-                       return Sum<int, int> (source, (a, b) => a + b);
+                       return Sum<int, int> (source, (a, b) => checked (a + b));
                }
 
                public static int? Sum (this IEnumerable<int?> source)
                {
                        Check.Source (source);
 
-                       return source.SumNullable<int?, int?> (0, (a, b) => a.HasValue ? a + b : a);
+                       return source.SumNullable<int?, int?> (0, (total, element) => element.HasValue ? checked (total + element) : total);
                }
 
                public static int Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, int> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return Sum<TSource, int> (source, (a, b) => a + selector (b));
+                       return Sum<TSource, int> (source, (a, b) => checked (a + selector (b)));
                }
 
                public static int? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, int?> selector)
@@ -1848,7 +1878,7 @@ namespace System.Linq
 
                        return source.SumNullable<TSource, int?> (0, (a, b) => {
                                var value = selector (b);
-                               return value.HasValue ? a + value.Value : a;
+                               return value.HasValue ? checked (a + value.Value) : a;
                        });
                }
 
@@ -1856,21 +1886,21 @@ namespace System.Linq
                {
                        Check.Source (source);
 
-                       return Sum<long, long> (source, (a, b) => a + b);
+                       return Sum<long, long> (source, (a, b) => checked (a + b));
                }
 
                public static long? Sum (this IEnumerable<long?> source)
                {
                        Check.Source (source);
 
-                       return source.SumNullable<long?, long?> (0, (a, b) => a.HasValue ? a + b : a);
+                       return source.SumNullable<long?, long?> (0, (total, element) => element.HasValue ? checked (total + element) : total);
                }
 
                public static long Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, long> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return Sum<TSource, long> (source, (a, b) => a + selector (b));
+                       return Sum<TSource, long> (source, (a, b) => checked (a + selector (b)));
                }
 
                public static long? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, long?> selector)
@@ -1879,7 +1909,7 @@ namespace System.Linq
 
                        return source.SumNullable<TSource, long?> (0, (a, b) => {
                                var value = selector (b);
-                               return value.HasValue ? a + value.Value : a;
+                               return value.HasValue ? checked (a + value.Value) : a;
                        });
                }
 
@@ -1887,21 +1917,21 @@ namespace System.Linq
                {
                        Check.Source (source);
 
-                       return Sum<double, double> (source, (a, b) => a + b);
+                       return Sum<double, double> (source, (a, b) => checked (a + b));
                }
 
                public static double? Sum (this IEnumerable<double?> source)
                {
                        Check.Source (source);
 
-                       return source.SumNullable<double?, double?> (0, (a, b) => a.HasValue ? a + b : a);
+                       return source.SumNullable<double?, double?> (0, (total, element) => element.HasValue ? checked (total + element) : total);
                }
 
                public static double Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, double> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return Sum<TSource, double> (source, (a, b) => a + selector (b));
+                       return Sum<TSource, double> (source, (a, b) => checked (a + selector (b)));
                }
 
                public static double? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, double?> selector)
@@ -1910,7 +1940,7 @@ namespace System.Linq
 
                        return source.SumNullable<TSource, double?> (0, (a, b) => {
                                var value = selector (b);
-                               return value.HasValue ? a + value.Value : a;
+                               return value.HasValue ? checked (a + value.Value) : a;
                        });
                }
 
@@ -1918,21 +1948,21 @@ namespace System.Linq
                {
                        Check.Source (source);
 
-                       return Sum<float, float> (source, (a, b) => a + b);
+                       return Sum<float, float> (source, (a, b) => checked (a + b));
                }
 
                public static float? Sum (this IEnumerable<float?> source)
                {
                        Check.Source (source);
 
-                       return source.SumNullable<float?, float?> (0, (a, b) => a.HasValue ? a + b : a);
+                       return source.SumNullable<float?, float?> (0, (total, element) => element.HasValue ? checked (total + element) : total);
                }
 
                public static float Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, float> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return Sum<TSource, float> (source, (a, b) => a + selector (b));
+                       return Sum<TSource, float> (source, (a, b) => checked (a + selector (b)));
                }
 
                public static float? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, float?> selector)
@@ -1941,7 +1971,7 @@ namespace System.Linq
 
                        return source.SumNullable<TSource, float?> (0, (a, b) => {
                                var value = selector (b);
-                               return value.HasValue ? a + value.Value : a;
+                               return value.HasValue ? checked (a + value.Value) : a;
                        });
                }
 
@@ -1949,21 +1979,21 @@ namespace System.Linq
                {
                        Check.Source (source);
 
-                       return Sum<decimal, decimal> (source, (a, b) => a + b);
+                       return Sum<decimal, decimal> (source, (a, b) => checked (a + b));
                }
 
                public static decimal? Sum (this IEnumerable<decimal?> source)
                {
                        Check.Source (source);
 
-                       return source.SumNullable<decimal?, decimal?> (0, (a, b) => a.HasValue ? a + b : a);
+                       return source.SumNullable<decimal?, decimal?> (0, (total, element) => element.HasValue ? checked (total + element) : total);
                }
 
                public static decimal Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return Sum<TSource, decimal> (source, (a, b) => a + selector (b));
+                       return Sum<TSource, decimal> (source, (a, b) => checked (a + selector (b)));
                }
 
                public static decimal? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
@@ -1972,7 +2002,7 @@ namespace System.Linq
 
                        return source.SumNullable<TSource, decimal?> (0, (a, b) => {
                                var value = selector (b);
-                               return value.HasValue ? a + value.Value : a;
+                               return value.HasValue ? checked (a + value.Value) : a;
                        });
                }
 
@@ -2016,10 +2046,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;
                        }
                }
 
@@ -2149,16 +2179,7 @@ namespace System.Linq
                public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey> (this IEnumerable<TSource> source,
                                Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
                {
-                       Check.SourceAndKeySelector (source, keySelector);
-
-                       if (comparer == null)
-                               comparer = EqualityComparer<TKey>.Default;
-
-                       var dict = new Dictionary<TKey, TSource> (comparer);
-                       foreach (var e in source)
-                               dict.Add (keySelector (e), e);
-
-                       return dict;
+                       return ToDictionary<TSource, TKey, TSource> (source, keySelector, Function<TSource>.Identity, comparer);
                }
 
                #endregion
@@ -2176,24 +2197,13 @@ namespace System.Linq
 
                public static ILookup<TKey, TSource> ToLookup<TSource, TKey> (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
                {
-                       return ToLookup<TSource, TKey> (source, keySelector, null);
+                       return ToLookup<TSource, TKey, TSource> (source, keySelector, Function<TSource>.Identity, null);
                }
 
                public static ILookup<TKey, TSource> ToLookup<TSource, TKey> (this IEnumerable<TSource> source,
                        Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
                {
-                       Check.SourceAndKeySelector (source, keySelector);
-
-                       var dictionary = new Dictionary<TKey, List<TSource>> (comparer ?? EqualityComparer<TKey>.Default);
-                       foreach (TSource element in source) {
-                               TKey key = keySelector (element);
-                               if (key == null)
-                                       throw new ArgumentNullException ();
-                               if (!dictionary.ContainsKey (key))
-                                       dictionary.Add (key, new List<TSource> ());
-                               dictionary [key].Add (element);
-                       }
-                       return new Lookup<TKey, TSource> (dictionary);
+                       return ToLookup<TSource, TKey, TSource> (source, keySelector, element => element, comparer);
                }
 
                public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement> (this IEnumerable<TSource> source,
@@ -2207,15 +2217,21 @@ namespace System.Linq
                {
                        Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
 
-                       Dictionary<TKey, List<TElement>> dictionary = new Dictionary<TKey, List<TElement>> (comparer ?? EqualityComparer<TKey>.Default);
-                       foreach (TSource element in source) {
-                               TKey key = keySelector (element);
+                       var dictionary = new Dictionary<TKey, List<TElement>> (comparer ?? EqualityComparer<TKey>.Default);
+                       foreach (var element in source) {
+                               var key = keySelector (element);
                                if (key == null)
-                                       throw new ArgumentNullException ();
-                               if (!dictionary.ContainsKey (key))
-                                       dictionary.Add (key, new List<TElement> ());
-                               dictionary [key].Add (elementSelector (element));
+                                       throw new ArgumentNullException ("key");
+
+                               List<TElement> list;
+                               if (!dictionary.TryGetValue (key, out list)) {
+                                       list = new List<TElement> ();
+                                       dictionary.Add (key, list);
+                               }
+
+                               list.Add (elementSelector (element));
                        }
+
                        return new Lookup<TKey, TElement> (dictionary);
                }
 
@@ -2235,18 +2251,19 @@ namespace System.Linq
                        if (comparer == null)
                                comparer = EqualityComparer<TSource>.Default;
 
-                       var first_enumerator = first.GetEnumerator ();
-                       var second_enumerator = second.GetEnumerator ();
+                       using (IEnumerator<TSource> first_enumerator = first.GetEnumerator (),
+                               second_enumerator = second.GetEnumerator ()) {
 
-                       while (first_enumerator.MoveNext ()) {
-                               if (!second_enumerator.MoveNext ())
-                                       return false;
+                               while (first_enumerator.MoveNext ()) {
+                                       if (!second_enumerator.MoveNext ())
+                                               return false;
 
-                               if (!comparer.Equals (first_enumerator.Current, second_enumerator.Current))
-                                       return false;
-                       }
+                                       if (!comparer.Equals (first_enumerator.Current, second_enumerator.Current))
+                                               return false;
+                               }
 
-                       return !second_enumerator.MoveNext ();
+                               return !second_enumerator.MoveNext ();
+                       }
                }
 
                #endregion