ExtList.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 bool AddUnique<T>(this List<T> list, T t, Func<T, T, bool> comparer)
  128. {
  129. if (!list.HasIdentical(t, comparer))
  130. {
  131. list.Add(t);
  132. return true;
  133. }
  134. else
  135. {
  136. return false;
  137. }
  138. }
  139. public static bool HasIdentical<T>(this List<T> list, T t, Func<T, T, bool> comparer)
  140. {
  141. for (int i = 0; i < list.Count; i++)
  142. {
  143. if (comparer.Invoke(list[i], t))
  144. {
  145. return true;
  146. }
  147. }
  148. return false;
  149. }
  150. public static void ForEach<T>(this List<T> list, Action<T> action, bool remove = false)
  151. {
  152. for (int i = 0; i < list.Count; i++)
  153. {
  154. action(list[i]);
  155. if (remove)
  156. {
  157. list.RemoveAt(i--);
  158. }
  159. }
  160. }
  161. public static void BackRemoveAt<T>(this List<T> list, int index)
  162. {
  163. list.RemoveAt(list.Count - 1 - index);
  164. }
  165. public static void MySort<T>(this List<T> list, Func<T, T, bool> func)
  166. {
  167. bool finish = false;
  168. for (int i = 0; i < list.Count; i++)
  169. {
  170. finish = true;
  171. for (int j = 0; j < list.Count - i - 1; j++)
  172. {
  173. if (func(list[j], list[j + 1]))
  174. {
  175. finish = false;
  176. T t = list[j];
  177. list[j] = list[j + 1];
  178. list[j + 1] = t;
  179. }
  180. }
  181. if (finish)
  182. {
  183. break;
  184. }
  185. }
  186. }
  187. public static float MyMin<T>(this List<T> list, Func<T, float> func)
  188. {
  189. float result = func(list[0]);
  190. for (int i = 1; i < list.Count; i++)
  191. {
  192. if (result > func(list[i]))
  193. {
  194. result = func(list[i]);
  195. }
  196. }
  197. return result;
  198. }
  199. public static float MyMax<T>(this List<T> list, Func<T, float> func)
  200. {
  201. float result = func(list[0]);
  202. for (int i = 1; i < list.Count; i++)
  203. {
  204. if (result < func(list[i]))
  205. {
  206. result = func(list[i]);
  207. }
  208. }
  209. return result;
  210. }
  211. public static float MySum<T>(this List<T> list, Func<T,float> func, int count = -1)
  212. {
  213. if (count == -1)
  214. {
  215. count = list.Count;
  216. }
  217. float result = 0;
  218. for (int i = 0; i < count; i++)
  219. {
  220. result += func(list[i]);
  221. }
  222. return result;
  223. }
  224. public static bool MyContains<T>(this List<T> list, Func<T, bool> func)
  225. {
  226. for (int i = 0; i < list.Count; i++)
  227. {
  228. if (func.Invoke(list[i]))
  229. {
  230. return true;
  231. }
  232. }
  233. return false;
  234. }
  235. public static int MyMinIndex<T>(this List<T> list, Func<T, float> func)
  236. {
  237. int index = 0;
  238. float result = func(list[index]);
  239. for (int i = 1; i < list.Count; i++)
  240. {
  241. if (result > func(list[i]))
  242. {
  243. result = func(list[i]);
  244. index = i;
  245. }
  246. }
  247. return index;
  248. }
  249. public static int MyMaxIndex<T>(this List<T> list, Func<T, float> func)
  250. {
  251. int index = 0;
  252. float result = func(list[index]);
  253. for (int i = 1; i < list.Count; i++)
  254. {
  255. if (result < func(list[i]))
  256. {
  257. result = func(list[i]);
  258. index = i;
  259. }
  260. }
  261. return index;
  262. }
  263. public static int GetMatchAmount<T>(this List<T> list, Func<T, bool> func)
  264. {
  265. int matchAmount = 0;
  266. for (int i = 0; i < list.Count; i++)
  267. {
  268. if (func.Invoke(list[i])) matchAmount++;
  269. }
  270. return matchAmount;
  271. }
  272. }