using CommonAI.Zone.Instance; using CommonLang; using System; using System.Collections; using System.Collections.Generic; using System.Text; namespace CommonAI.Zone.Helper { public class DoAndRemoveCollection { public static void UpdateAndRemove(LinkedList list, Predicate test) { using (var removed = ListObjectPool>.AllocAutoRelease()) { for (LinkedListNode it = list.Last; it != null; it = it.Previous) { T t = it.Value; if (test(t)) { removed.Add(it); } } if (removed.Count > 0) { foreach (LinkedListNode it in removed) { list.Remove(it); } } } } public static void UpdateAndRemove(ICollection list, Predicate test) { using (var removed = ListObjectPool.AllocAutoRelease()) { foreach (T t in list) { if (test(t)) { removed.Add(t); } } if (removed.Count > 0) { for (int i = removed.Count - 1; i >= 0; --i) { list.Remove(removed[i]); } } } } public static void UpdateAndRemove(IList list, Predicate test) { using (var removed = ListObjectPool.AllocAutoRelease()) { for (int i = list.Count - 1; i >= 0; --i) { T t = list[i]; if (test(t)) { removed.Add(t); } } if (removed.Count > 0) { for (int i = removed.Count - 1; i >= 0; --i) { list.Remove(removed[i]); } } } } } }