Created
January 17, 2013 06:25
-
-
Save dend/4554128 to your computer and use it in GitHub Desktop.
Proper implementation for RemoveAll<T>
This file contains hidden or 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
public static void RemoveAll<T>(this List<T> list, Func<T, bool> filter) | |
{ | |
if (filter == null) | |
throw new ArgumentException("filter"); | |
if (list == null) | |
throw new ArgumentException("list"); | |
int index = 0; | |
while ((index < list.Count) && !filter(list[index])) | |
{ | |
index++; | |
} | |
if (index >= list.Count) | |
{ | |
return; | |
} | |
int secondaryCounter = index + 1; | |
while (secondaryCounter < list.Count) | |
{ | |
while ((secondaryCounter < list.Count) && filter(list[secondaryCounter])) | |
{ | |
secondaryCounter++; | |
} | |
if (secondaryCounter < list.Count) | |
{ | |
list[index++] = list[secondaryCounter++]; | |
} | |
} | |
list.RemoveRange(index, list.Count - index); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment