Skip to content

Instantly share code, notes, and snippets.

Created June 16, 2011 07:48
Show Gist options
  • Save anonymous/1028852 to your computer and use it in GitHub Desktop.
Save anonymous/1028852 to your computer and use it in GitHub Desktop.
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