2007-11-08 Jb Evain <jbevain@novell.com>
authorJb Evain <jbevain@gmail.com>
Thu, 8 Nov 2007 14:18:19 +0000 (14:18 -0000)
committerJb Evain <jbevain@gmail.com>
Thu, 8 Nov 2007 14:18:19 +0000 (14:18 -0000)
* Enumerable.cs: correctly implement Intersect.

svn path=/trunk/mcs/; revision=89188

mcs/class/System.Core/System.Linq/ChangeLog
mcs/class/System.Core/System.Linq/Enumerable.cs

index 2f230c68ea432798da158028832e680d0af69c8e..6f3e86d6a0e02c7cf4afdf59a40e07a4e34f8d5b 100644 (file)
@@ -1,3 +1,7 @@
+2007-11-08  Jb Evain  <jbevain@novell.com>
+
+       * Enumerable.cs: correctly implement Intersect.
+
 2007-11-08  Jb Evain  <jbevain@novell.com>
 
        * Enumerable.cs: code cleanup.
index 7010fba59a9ac14ec5255d6586473cc94da11f0b..64d03332672dced6da31f4b9e57bdb258e1a6cdf 100644 (file)
@@ -869,25 +869,23 @@ namespace System.Linq
 
                #region Intersect
 
-
                public static IEnumerable<TSource> Intersect<TSource> (this IEnumerable<TSource> first, IEnumerable<TSource> second)
+               {
+                       return Intersect (first, second, null);
+               }
+
+               public static IEnumerable<TSource> Intersect<TSource> (this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
                {
                        if (first == null || second == null)
                                throw new ArgumentNullException ();
 
-                       List<TSource> items = new List<TSource> (Distinct (first));
-                       bool [] marked = new bool [items.Count];
-                       for (int i = 0; i < marked.Length; i++)
-                               marked [i] = false;
+                       if (comparer == null)
+                               comparer = EqualityComparer<TSource>.Default;
 
+                       List<TSource> items = new List<TSource> (Distinct (first));
                        foreach (TSource element in second) {
-                               int index = IndexOf (items, element);
-                               if (index != -1)
-                                       marked [index] = true;
-                       }
-                       for (int i = 0; i < marked.Length; i++) {
-                               if (marked [i])
-                                       yield return items [i];
+                               if (Contains (items, element, comparer))
+                                       yield return element;
                        }
                }