ExtList.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. public static class ExtList
  7. {
  8. public static T Prev<T>(this List<T> list, int index)
  9. {
  10. return list[(index + list.Count - 1) % list.Count];
  11. }
  12. public static T Next<T>(this List<T> list, int index)
  13. {
  14. return list[(index + 1) % list.Count];
  15. }
  16. public static T Back<T>(this List<T> list, int index, bool remove = false)
  17. {
  18. T t = list[list.Count - 1 - index];
  19. if (remove)
  20. {
  21. list.RemoveAt(list.Count - 1 - index);
  22. }
  23. return t;
  24. }
  25. public static T Forward<T>(this List<T> list, int index, bool remove = false)
  26. {
  27. T t = list[index];
  28. if (remove)
  29. {
  30. list.RemoveAt(index);
  31. }
  32. return t;
  33. }
  34. public static List<T> Random<T>(this List<T> list, int amt = 1, bool allDifferent = true, bool remove = false, Func<T,bool> func = null)
  35. {
  36. if (amt > list.Count && allDifferent)
  37. {
  38. throw new Exception();
  39. }
  40. int antiCrush = 0;
  41. List<T> resultList = new List<T>();
  42. while (resultList.Count < amt)
  43. {
  44. if (antiCrush++ > 10000)
  45. {
  46. throw new Exception();
  47. }
  48. int index = UnityEngine.Random.Range(0, list.Count);
  49. T result = list[index];
  50. if (func != null)
  51. {
  52. if (!func(result))
  53. {
  54. continue;
  55. }
  56. }
  57. if (allDifferent)
  58. {
  59. if (resultList.Contains(result))
  60. {
  61. continue;
  62. }
  63. }
  64. if (remove)
  65. {
  66. list.RemoveAt(index);
  67. resultList.Add(result);
  68. }
  69. else
  70. {
  71. resultList.Add(result);
  72. }
  73. }
  74. return resultList;
  75. }
  76. public static List<T> Disturb<T>(this List<T> list)
  77. {
  78. List<T> resultList = new List<T>();
  79. resultList.AddRange(list.Random(list.Count));
  80. return resultList;
  81. }
  82. public static List<T2> ToSingleList<T1,T2>(this List<T1> twoLevelList) where T1 : List<T2>
  83. {
  84. //for (int i = 0; i < highLevelList.Count; i++)
  85. //{
  86. // highLevelList[i][i].GetType()
  87. //}
  88. return null;
  89. }
  90. public static bool Valid<T>(this List<T> list)
  91. {
  92. if (list == null || list.Count == 0)
  93. {
  94. return false;
  95. }
  96. else
  97. {
  98. return true;
  99. }
  100. }
  101. public static bool UniqueAdd<T>(this List<T> list, T obj)
  102. {
  103. if (list.Contains(obj) == false)
  104. {
  105. list.Add(obj);
  106. return true;
  107. }
  108. else
  109. {
  110. return false;
  111. }
  112. }
  113. public static void ForEach<T>(this List<T> list, Action<T> action, bool remove = false)
  114. {
  115. for (int i = 0; i < list.Count; i++)
  116. {
  117. action(list[i]);
  118. if (remove)
  119. {
  120. list.RemoveAt(i--);
  121. }
  122. }
  123. }
  124. public static void BackRemoveAt<T>(this List<T> list, int index)
  125. {
  126. list.RemoveAt(list.Count - 1 - index);
  127. }
  128. public static void MySort<T>(this List<T> list, Func<T, T, bool> func)
  129. {
  130. bool finish = false;
  131. for (int i = 0; i < list.Count; i++)
  132. {
  133. finish = true;
  134. for (int j = 0; j < list.Count - i - 1; j++)
  135. {
  136. if (func(list[j], list[j + 1]))
  137. {
  138. finish = false;
  139. T t = list[j];
  140. list[j] = list[j + 1];
  141. list[j + 1] = t;
  142. }
  143. }
  144. if (finish)
  145. {
  146. break;
  147. }
  148. }
  149. }
  150. public static float MyMin<T>(this List<T> list, Func<T, float> func)
  151. {
  152. float result = func(list[0]);
  153. for (int i = 1; i < list.Count; i++)
  154. {
  155. if (result > func(list[i]))
  156. {
  157. result = func(list[i]);
  158. }
  159. }
  160. return result;
  161. }
  162. public static float MyMax<T>(this List<T> list, Func<T, float> func)
  163. {
  164. float result = func(list[0]);
  165. for (int i = 1; i < list.Count; i++)
  166. {
  167. if (result < func(list[i]))
  168. {
  169. result = func(list[i]);
  170. }
  171. }
  172. return result;
  173. }
  174. public static float MySum<T>(this List<T> list, Func<T,float> func, int count = -1)
  175. {
  176. if (count == -1)
  177. {
  178. count = list.Count;
  179. }
  180. float result = 0;
  181. for (int i = 0; i < count; i++)
  182. {
  183. result += func(list[i]);
  184. }
  185. return result;
  186. }
  187. }