ExtList.cs 5.7 KB

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