diff options
author | Jb Evain <jbevain@gmail.com> | 2009-01-15 20:34:05 +0300 |
---|---|---|
committer | Jb Evain <jbevain@gmail.com> | 2009-01-15 20:34:05 +0300 |
commit | 61d786b69a08c4366abd7397e413c623fb370254 (patch) | |
tree | 1d87c515dcb40f3088e8a883d885f5e61ee9c754 /mcs/class/System.Core/System.Linq/Enumerable.cs | |
parent | 0e67f5045c8e99f4d92cd193c095868dc40ee867 (diff) |
small refactoring
svn path=/trunk/mcs/; revision=123505
Diffstat (limited to 'mcs/class/System.Core/System.Linq/Enumerable.cs')
-rw-r--r-- | mcs/class/System.Core/System.Linq/Enumerable.cs | 35 |
1 files changed, 15 insertions, 20 deletions
diff --git a/mcs/class/System.Core/System.Linq/Enumerable.cs b/mcs/class/System.Core/System.Linq/Enumerable.cs index 75d4b20c124..e7e036352f6 100644 --- a/mcs/class/System.Core/System.Linq/Enumerable.cs +++ b/mcs/class/System.Core/System.Linq/Enumerable.cs @@ -2194,24 +2194,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, element => element, 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 +2214,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); } |