ObjectCollection.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using CommonAI.Zone.Instance;
  2. using CommonLang;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. namespace CommonAI.Zone.Helper
  8. {
  9. public class DoAndRemoveCollection
  10. {
  11. public static void UpdateAndRemove<T>(LinkedList<T> list, Predicate<T> test)
  12. {
  13. using (var removed = ListObjectPool<LinkedListNode<T>>.AllocAutoRelease())
  14. {
  15. for (LinkedListNode<T> it = list.Last; it != null; it = it.Previous)
  16. {
  17. T t = it.Value;
  18. if (test(t))
  19. {
  20. removed.Add(it);
  21. }
  22. }
  23. if (removed.Count > 0)
  24. {
  25. foreach (LinkedListNode<T> it in removed)
  26. {
  27. list.Remove(it);
  28. }
  29. }
  30. }
  31. }
  32. public static void UpdateAndRemove<T>(ICollection<T> list, Predicate<T> test)
  33. {
  34. using (var removed = ListObjectPool<T>.AllocAutoRelease())
  35. {
  36. foreach (T t in list)
  37. {
  38. if (test(t))
  39. {
  40. removed.Add(t);
  41. }
  42. }
  43. if (removed.Count > 0)
  44. {
  45. for (int i = removed.Count - 1; i >= 0; --i)
  46. {
  47. list.Remove(removed[i]);
  48. }
  49. }
  50. }
  51. }
  52. public static void UpdateAndRemove<T>(IList<T> list, Predicate<T> test)
  53. {
  54. using (var removed = ListObjectPool<T>.AllocAutoRelease())
  55. {
  56. for (int i = list.Count - 1; i >= 0; --i)
  57. {
  58. T t = list[i];
  59. if (test(t))
  60. {
  61. removed.Add(t);
  62. }
  63. }
  64. if (removed.Count > 0)
  65. {
  66. for (int i = removed.Count - 1; i >= 0; --i)
  67. {
  68. list.Remove(removed[i]);
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }