Skip to content

Instantly share code, notes, and snippets.

@lucaswerkmeister
Created April 8, 2013 19:53
Show Gist options
  • Save lucaswerkmeister/5339933 to your computer and use it in GitHub Desktop.
Save lucaswerkmeister/5339933 to your computer and use it in GitHub Desktop.
Gets a robust view of any enumerable - this one will not break when you modify the underlying enumerable (e. g., modify the list). Originally written for lucaswerkmeister/wp_geohashing until I realized I didn't need it there.
/// <summary>
/// Gets a robust enumerator for the IEnumerable. A robust enumerator will function even when the collection is modified while enumerating.
/// </summary>
/// <typeparam name="T">The type.</typeparam>
/// <param name="coll">The collection.</param>
/// <param name="lookBehind">If <code>true</code>, elements inserted before the current element will still be returned.</param>
/// <returns>An IEnumerable that will not throw an IllegalOperationException if the underlying collection is modified.</returns>
public static IEnumerable<T> AsRobustEnumerable<T>(this IEnumerable<T> coll, bool lookBehind = false) where T : class
{
HashSet<T> alreadyReturned = new HashSet<T>();
T latestElement = null;
while (true)
{
bool hasSeenLatestElement = lookBehind;
bool returnedElement = false;
foreach (T t in coll)
{
hasSeenLatestElement |= latestElement == null || t.Equals(latestElement);
if (!alreadyReturned.Contains(t) && hasSeenLatestElement)
{
latestElement = t;
alreadyReturned.Add(t);
yield return t;
returnedElement = true;
break;
}
}
if (!returnedElement)
yield break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment