|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
-
- namespace SUISS.Core.Utilities
- {
- internal static class EnumerableExtensions
- {
- public static T PickRandom<T>(this IEnumerable<T> e)
- {
- List<T> list = e.ToList<T>();
- return (list.Count != 0) ? list[UnityEngine.Random.Range(0, list.Count)] : default(T);
- }
-
- public static IList<T> Clone<T>(this IList<T> listToClone) where T : ICloneable
- {
- return (from item in listToClone
- select (T)((object)item.Clone())).ToList<T>();
- }
-
- public static List<T> Where<T>(this IList<T> inputList, Func<T, bool> predicate)
- {
- List<T> list = new List<T>();
- foreach (T t in inputList)
- {
- if (predicate(t))
- {
- list.Add(t);
- }
- }
- return list;
- }
-
- public static List<T> Except<T>(this IEnumerable<T> input, IEnumerable<T> other)
- {
- List<T> list = new List<T>(input);
- list.RemoveAll((T x) => other.Contains(x));
- return list;
- }
-
- public static void RemoveAll<Key, Value>(this Dictionary<Key, Value> dict, Func<KeyValuePair<Key, Value>, bool> predicate)
- {
- List<Key> list = new List<Key>();
- foreach (KeyValuePair<Key, Value> arg in dict)
- {
- if (predicate(arg))
- {
- list.Add(arg.Key);
- }
- }
- foreach (Key key in list)
- {
- dict.Remove(key);
- }
- }
-
- public static T MinOrMaxObject<T, U>(this IEnumerable<T> source, Func<T, U> selector, bool findMin) where U : IComparable<U>
- {
- 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<T> ToList<T>(this IEnumerable<T> enumerable)
- {
- List<T> list = new List<T>();
- foreach (T item in enumerable)
- {
- list.Add(item);
- }
- return list;
- }
-
- public static IList<TResult> Select<TSource, TResult>(this IList<TSource> source, Func<TSource, TResult> selector)
- {
- List<TResult> list = new List<TResult>();
- foreach (TSource arg in source)
- {
- list.Add(selector(arg));
- }
- return list;
- }
-
- public static T First<T>(this IEnumerable<T> source, Predicate<T> predicate) where T : class
- {
- foreach (T t in source)
- {
- if (predicate(t))
- {
- return t;
- }
- }
- return (T)((object)null);
- }
- }
- }
|