ExtList.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. public static class ExtList
  6. {
  7. public delegate float SumDelegate<T>(T t);
  8. public delegate float MaxMinDelegate<T>(T t);
  9. public delegate bool SortDelegate<T>(T t1, T t2);
  10. public delegate bool RandomDelegate<T>(T t);
  11. public static T Prev<T>(this List<T> list, int index)
  12. {
  13. return list[(index + list.Count - 1) % list.Count];
  14. }
  15. public static T Next<T>(this List<T> list, int index)
  16. {
  17. return list[(index + 1) % list.Count];
  18. }
  19. public static T Back<T>(this List<T> list, int index, bool remove = false)
  20. {
  21. T t = list[list.Count - 1 - index];
  22. if (remove)
  23. {
  24. list.RemoveAt(list.Count - 1 - index);
  25. }
  26. return t;
  27. }
  28. public static T Forward<T>(this List<T> list, int index, bool remove = false)
  29. {
  30. T t = list[index];
  31. if (remove)
  32. {
  33. list.RemoveAt(index);
  34. }
  35. return t;
  36. }
  37. public static T Random<T>(this List<T> list, bool remove = false, RandomDelegate<T> randomDelegate = null)
  38. {
  39. while (true)
  40. {
  41. int index = UnityEngine.Random.Range(0, list.Count);
  42. T result = list[index];
  43. if (randomDelegate != null)
  44. {
  45. if (!randomDelegate(result))
  46. {
  47. continue;
  48. }
  49. }
  50. if (remove)
  51. {
  52. list.RemoveAt(index);
  53. return result;
  54. }
  55. else
  56. {
  57. return result;
  58. }
  59. }
  60. }
  61. public static bool Valid<T>(this List<T> list)
  62. {
  63. if (list == null || list.Count == 0)
  64. {
  65. return false;
  66. }
  67. else
  68. {
  69. return true;
  70. }
  71. }
  72. public static bool UniqueAdd<T>(this List<T> list, T obj)
  73. {
  74. if (list.Contains(obj) == false)
  75. {
  76. list.Add(obj);
  77. return true;
  78. }
  79. else
  80. {
  81. return false;
  82. }
  83. }
  84. public static void BackRemoveAt<T>(this List<T> list, int index)
  85. {
  86. list.RemoveAt(list.Count - 1 - index);
  87. }
  88. public static void MySort<T>(this List<T> list, SortDelegate<T> sortDelegate)
  89. {
  90. bool finish = false;
  91. for (int i = 0; i < list.Count; i++)
  92. {
  93. finish = true;
  94. for (int j = 0; j < list.Count - i - 1; j++)
  95. {
  96. if (sortDelegate(list[j], list[j + 1]))
  97. {
  98. finish = false;
  99. T t = list[j];
  100. list[j] = list[j + 1];
  101. list[j + 1] = t;
  102. }
  103. }
  104. if (finish)
  105. {
  106. break;
  107. }
  108. }
  109. }
  110. public static float MyMin<T>(this List<T> list, MaxMinDelegate<T> maxMinDelegate)
  111. {
  112. float result = maxMinDelegate(list[0]);
  113. for (int i = 1; i < list.Count; i++)
  114. {
  115. if (result > maxMinDelegate(list[i]))
  116. {
  117. result = maxMinDelegate(list[i]);
  118. }
  119. }
  120. return result;
  121. }
  122. public static float MyMax<T>(this List<T> list, MaxMinDelegate<T> maxMinDelegate)
  123. {
  124. float result = maxMinDelegate(list[0]);
  125. for (int i = 1; i < list.Count; i++)
  126. {
  127. if (result < maxMinDelegate(list[i]))
  128. {
  129. result = maxMinDelegate(list[i]);
  130. }
  131. }
  132. return result;
  133. }
  134. public static float MySum<T>(this List<T> list, SumDelegate<T> sumDelegate)
  135. {
  136. float result = 0;
  137. for (int i = 0; i < list.Count; i++)
  138. {
  139. result += sumDelegate(list[i]);
  140. }
  141. return result;
  142. }
  143. }