Browse Source

small refactoring

svn path=/trunk/mcs/; revision=123505
Jb Evain 17 years ago
parent
commit
61d786b69a

+ 15 - 20
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);
 		}
 

+ 14 - 15
mcs/class/System.Core/System.Linq/Lookup.cs

@@ -4,6 +4,7 @@
 // Authors:
 //	Alejandro Serrano "Serras" ([email protected])
 //	Marek Safar  <[email protected]>
+//	Jb Evain  <[email protected]>
 //
 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
 //
@@ -38,32 +39,30 @@ namespace System.Linq {
 
 		Dictionary<TKey, IGrouping<TKey, TElement>> groups;
 
-		internal Lookup (Dictionary<TKey, List<TElement>> groups)
-		{
-			this.groups = new Dictionary<TKey, IGrouping<TKey, TElement>> (groups.Comparer);
-			foreach (KeyValuePair<TKey, List<TElement>> group in groups)
-				this.groups.Add (group.Key, new Grouping<TKey, TElement> (group.Key, group.Value));
+		public int Count {
+			get { return groups.Count; }
 		}
 
-		public IEnumerable<TResult> ApplyResultSelector<TResult> (Func<TKey, IEnumerable<TElement>, TResult> selector)
-		{
-			foreach (IGrouping<TKey, TElement> group in groups.Values)
-				yield return selector (group.Key, group);
+		public IEnumerable<TElement> this [TKey key] {
+			get { return groups [key]; }
 		}
 
-		public int Count
+		internal Lookup (Dictionary<TKey, List<TElement>> lookup)
 		{
-			get { return groups.Count; }
+			groups = new Dictionary<TKey, IGrouping<TKey, TElement>> (lookup.Comparer);
+			foreach (var slot in lookup)
+				groups.Add (slot.Key, new Grouping<TKey, TElement> (slot.Key, slot.Value));
 		}
 
-		public bool Contains (TKey key)
+		public IEnumerable<TResult> ApplyResultSelector<TResult> (Func<TKey, IEnumerable<TElement>, TResult> selector)
 		{
-			return groups.ContainsKey (key);
+			foreach (var group in groups.Values)
+				yield return selector (group.Key, group);
 		}
 
-		public IEnumerable<TElement> this [TKey key]
+		public bool Contains (TKey key)
 		{
-			get { return groups [key]; }
+			return groups.ContainsKey (key);
 		}
 
 		public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator ()