123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- using UnityEngine;
- using UnityEngine.Events;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- public static class ExtList
- {
- public static T Prev<T>(this List<T> list, int index)
- {
- return list[(index + list.Count - 1) % list.Count];
- }
- public static T Next<T>(this List<T> list, int index)
- {
- return list[(index + 1) % list.Count];
- }
- public static T Back<T>(this List<T> list, int index, bool remove = false)
- {
- T t = list[list.Count - 1 - index];
- if (remove)
- {
- list.RemoveAt(list.Count - 1 - index);
- }
- return t;
- }
- public static T Forward<T>(this List<T> list, int index, bool remove = false)
- {
- T t = list[index];
- if (remove)
- {
- list.RemoveAt(index);
- }
- return t;
- }
- public static List<T> Random<T>(this List<T> list, int amt = 1, bool allDifferent = true, bool remove = false, Func<T,bool> func = null)
- {
- if (amt > list.Count && allDifferent)
- {
- throw new Exception();
- }
- int antiCrush = 0;
- List<T> resultList = new List<T>();
- while (resultList.Count < amt)
- {
- if (antiCrush++ > 10000)
- {
- throw new Exception();
- }
- int index = UnityEngine.Random.Range(0, list.Count);
-
- T result = list[index];
- if (func != null)
- {
- if (!func(result))
- {
- continue;
- }
- }
- if (allDifferent)
- {
- if (resultList.Contains(result))
- {
- continue;
- }
- }
- if (remove)
- {
- list.RemoveAt(index);
- resultList.Add(result);
- }
- else
- {
- resultList.Add(result);
- }
- }
- return resultList;
- }
- public static List<T> Disturb<T>(this List<T> list)
- {
- List<T> resultList = new List<T>();
- resultList.AddRange(list.Random(list.Count));
- return resultList;
- }
- public static List<T2> ToSingleList<T1,T2>(this List<T1> twoLevelList) where T1 : List<T2>
- {
- //for (int i = 0; i < highLevelList.Count; i++)
- //{
- // highLevelList[i][i].GetType()
- //}
- return null;
- }
- public static bool Valid<T>(this List<T> list)
- {
- if (list == null || list.Count == 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- public static bool UniqueAdd<T>(this List<T> list, T obj)
- {
- if (list.Contains(obj) == false)
- {
- list.Add(obj);
- return true;
- }
- else
- {
- return false;
- }
- }
- public static void ForEach<T>(this List<T> list, Action<T> action, bool remove = false)
- {
- for (int i = 0; i < list.Count; i++)
- {
- action(list[i]);
- if (remove)
- {
- list.RemoveAt(i--);
- }
- }
- }
- public static void BackRemoveAt<T>(this List<T> list, int index)
- {
- list.RemoveAt(list.Count - 1 - index);
- }
- public static void MySort<T>(this List<T> list, Func<T, T, bool> func)
- {
- bool finish = false;
- for (int i = 0; i < list.Count; i++)
- {
- finish = true;
- for (int j = 0; j < list.Count - i - 1; j++)
- {
- if (func(list[j], list[j + 1]))
- {
- finish = false;
- T t = list[j];
- list[j] = list[j + 1];
- list[j + 1] = t;
- }
- }
- if (finish)
- {
- break;
- }
- }
- }
- public static float MyMin<T>(this List<T> list, Func<T, float> func)
- {
- float result = func(list[0]);
- for (int i = 1; i < list.Count; i++)
- {
- if (result > func(list[i]))
- {
- result = func(list[i]);
- }
- }
- return result;
- }
- public static float MyMax<T>(this List<T> list, Func<T, float> func)
- {
- float result = func(list[0]);
- for (int i = 1; i < list.Count; i++)
- {
- if (result < func(list[i]))
- {
- result = func(list[i]);
- }
- }
- return result;
- }
- public static float MySum<T>(this List<T> list, Func<T,float> func, int count = -1)
- {
- if (count == -1)
- {
- count = list.Count;
- }
- float result = 0;
-
- for (int i = 0; i < count; i++)
- {
- result += func(list[i]);
- }
- return result;
- }
- }
|