using System; using System.Collections.Generic; using System.Linq; using UnityEngine; namespace SUISS.Core.Utilities { internal static class EnumerableExtensions { public static T PickRandom(this IEnumerable e) { List list = e.ToList(); return (list.Count != 0) ? list[UnityEngine.Random.Range(0, list.Count)] : default(T); } public static IList Clone(this IList listToClone) where T : ICloneable { return (from item in listToClone select (T)((object)item.Clone())).ToList(); } public static List Where(this IList inputList, Func predicate) { List list = new List(); foreach (T t in inputList) { if (predicate(t)) { list.Add(t); } } return list; } public static List Except(this IEnumerable input, IEnumerable other) { List list = new List(input); list.RemoveAll((T x) => other.Contains(x)); return list; } public static void RemoveAll(this Dictionary dict, Func, bool> predicate) { List list = new List(); foreach (KeyValuePair arg in dict) { if (predicate(arg)) { list.Add(arg.Key); } } foreach (Key key in list) { dict.Remove(key); } } public static T MinOrMaxObject(this IEnumerable source, Func selector, bool findMin) where U : IComparable { if (source == null) { throw new ArgumentNullException("source"); } bool flag = true; T t = default(T); U other = default(U); foreach (T t2 in source) { if (flag) { t = t2; other = selector(t); flag = false; } else { U u = selector(t2); bool flag2 = (!findMin) ? (u.CompareTo(other) > 0) : (u.CompareTo(other) < 0); if (flag2) { other = u; t = t2; } } } if (flag) { throw new InvalidOperationException("Sequence is empty."); } return t; } public static List ToList(this IEnumerable enumerable) { List list = new List(); foreach (T item in enumerable) { list.Add(item); } return list; } public static IList Select(this IList source, Func selector) { List list = new List(); foreach (TSource arg in source) { list.Add(selector(arg)); } return list; } public static T First(this IEnumerable source, Predicate predicate) where T : class { foreach (T t in source) { if (predicate(t)) { return t; } } return (T)((object)null); } } }