Created
June 16, 2011 07:48
-
-
Save anonymous/1028852 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal static IEnumerable<T> MyIntersect<T>(this IEnumerable<T> first, IEnumerable<T> second) { | |
Debug.Assert(first != null); | |
Debug.Assert(second != null); | |
var firstHashset = first as HashSet<T>; | |
var secondHashset = second as HashSet<T>; | |
if (firstHashset != null && secondHashset != null) { | |
return (firstHashset.Count > secondHashset.Count) | |
? firstHashset.Intersect(second) | |
: secondHashset.Intersect(first); | |
} | |
if (firstHashset != null) { return firstHashset.Intersect(second); } | |
if (secondHashset != null) { return secondHashset.Intersect(first); } | |
return first.Intersect(second); | |
} | |
static IEnumerable<T> Intersect<T>(this HashSet<T> firstHashset, IEnumerable<T> second) { | |
Debug.Assert(firstHashset != null); | |
Debug.Assert(second != null); | |
foreach (var tmp in second) { | |
if (firstHashset.Contains(tmp)) { yield return tmp; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment