2009-02-13 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Core / System.Linq / Enumerable.cs
index 75d4b20c12407d619c1228f91f2c159d1b5516af..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)
@@ -931,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);
                }
 
@@ -1843,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)
@@ -1866,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;
                        });
                }
 
@@ -1874,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)
@@ -1897,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;
                        });
                }
 
@@ -1905,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)
@@ -1928,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;
                        });
                }
 
@@ -1936,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)
@@ -1959,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;
                        });
                }
 
@@ -1967,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)
@@ -1990,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;
                        });
                }
 
@@ -2167,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
@@ -2194,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,
@@ -2225,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);
                }
 
@@ -2253,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