123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- using UnityEngine;
- using UnityEngine.Events;
- using System.Collections;
- using System.Collections.Generic;
- public static class ExtList
- {
- public delegate float SumDelegate<T>(T t);
- public delegate float MaxMinDelegate<T>(T t);
- public delegate bool SortDelegate<T>(T t1, T t2);
- public delegate bool RandomDelegate<T>(T t);
- 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 T Random<T>(this List<T> list, bool remove = false, RandomDelegate<T> randomDelegate = null)
- {
- while (true)
- {
- int index = UnityEngine.Random.Range(0, list.Count);
- T result = list[index];
- if (randomDelegate != null)
- {
- if (!randomDelegate(result))
- {
- continue;
- }
- }
- if (remove)
- {
- list.RemoveAt(index);
- return result;
- }
- else
- {
- return result;
- }
- }
- }
- 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 BackRemoveAt<T>(this List<T> list, int index)
- {
- list.RemoveAt(list.Count - 1 - index);
- }
- public static void MySort<T>(this List<T> list, SortDelegate<T> sortDelegate)
- {
- bool finish = false;
- for (int i = 0; i < list.Count; i++)
- {
- finish = true;
- for (int j = 0; j < list.Count - i - 1; j++)
- {
- if (sortDelegate(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, MaxMinDelegate<T> maxMinDelegate)
- {
- float result = maxMinDelegate(list[0]);
- for (int i = 1; i < list.Count; i++)
- {
- if (result > maxMinDelegate(list[i]))
- {
- result = maxMinDelegate(list[i]);
- }
- }
- return result;
- }
- public static float MyMax<T>(this List<T> list, MaxMinDelegate<T> maxMinDelegate)
- {
- float result = maxMinDelegate(list[0]);
- for (int i = 1; i < list.Count; i++)
- {
- if (result < maxMinDelegate(list[i]))
- {
- result = maxMinDelegate(list[i]);
- }
- }
- return result;
- }
- public static float MySum<T>(this List<T> list, SumDelegate<T> sumDelegate)
- {
- float result = 0;
- for (int i = 0; i < list.Count; i++)
- {
- result += sumDelegate(list[i]);
- }
- return result;
- }
- }
|