using System; using System.Collections; using System.Collections.Generic; using System.Linq; using Unity.Mathematics; using Random = System.Random; namespace BITKit { public static partial class Extensions { public static void ForEach(this T[] self, System.Action action, Func _if = null) { _if = _if ?? Constant.True; if (self is not null) { if (_if.Invoke()) Array.ForEach(self, action); } } public static void ForEach(this IDictionary self, Action> action) { if (self.IsValid()) { if (self.IsValid()) for (int i = 0; i < self.Count(); i++) { action.Invoke(self.ElementAt(i)); } } } public static IEnumerable ForEach(this IEnumerable self, Action action, Func _if = null) { _if = _if ?? Constant.True; self.ForEach((index, x) => { if (_if.Invoke()) action.Invoke(x); }); return self; } public static IEnumerable IfEmety(this IEnumerable self, Action action) { if (self?.Count() is 0) { action.Invoke(); } return self; } public static IEnumerable CreateOrAddIfEmety(this IEnumerable self, Func> createFactory) { if (self is null) { return createFactory.Invoke(); } return self; } public static IEnumerable CreateOrAddIfEmety(this IEnumerable self, IEnumerable source, T value) { if (self?.Count() is 0) { if (source is not List) { self = self.ToList(); } switch (source) { case List list: list.Add(value); break; default: throw new Exception($"未支持的类型{self.GetType().Name}"); } } return self; } public static IEnumerable CreateOrAddIfEmety(this IEnumerable self, IEnumerable source, Func action) { if (self?.Count() is 0) { CreateOrAddIfEmety(self, source, action.Invoke()); } return self; } public static void ForEach(this IEnumerable self, Action action) { if (self.IsValid()) for (int i = 0; i < self.Count(); i++) { action.Invoke(i, self.ElementAt(i)); } } public static bool IsNull(this IEnumerable e) { return e.IsValid() == false; } public static bool IsValid(this IEnumerable e) { return e is not null && e.Any(); } public static bool TryAdd(this IList self, T t) { if (self.Contains(t)) { return false; } else { self.Add(t); return true; } } public static bool TryGetElementAt(this IEnumerable self, int index, out T value) { var enumerable = self as T[] ?? self.ToArray(); if (index >= 0 && enumerable.Count() > index) { value = enumerable.ElementAt(index); return true; } value = default; return default; } public static TValue GetOrCreate(this IDictionary self, TKey t) where TValue : new() { if (self.TryGetValue(t, out var value)) { } else { value = new TValue(); self.TryAdd(t, value); } return value; } public static bool TryRemove(this IList self, T t) { if (self.Contains(t)) { self.Remove(t); return true; } return false; } public static IEnumerable RemoveIn(this IEnumerable self, T t) { var list = self.ToList(); list.Remove(t); return list.ToArray(); } public static bool TryRemove(this IDictionary self, TKey t) { return self.ContainsKey(t) && self.Remove(t); } public static void Set(this IDictionary self, TKey key, TValue value) { if (self.ContainsKey(key)) { self[key] = value; } else { self.Add(key, value); } } public static TValue Get(this IDictionary self) { return self[typeof(TKey)]; } public static TValue[] TakeRandom(this TValue[] self, int count) { //自动实现 var random = new Random(); var newList = self.ToList(); count = math.min(count, newList.Count); var result = new TValue[count]; for (var i = 0; i < count; i++) { var value = newList[random.Next(0, newList.Count)]; newList.Remove(value); result[i] = value; } return result; } public static TValue Get(this IDictionary self, TKey key) where TValue : new() { lock (self) { TValue value; if (self.TryGetValue(key, out value)) { } else { value = new(); self.Add(key, value); } return value; } } public static IEnumerable TrySelectFirst(this IEnumerable self, Action action) { if (self.Any()) { action.Invoke(self.First()); } return self; } public static bool TryGetAny(this IEnumerable self, Func factory, out T any) { foreach (var x in self) { if (factory.Invoke(x)) { any = x; return true; } } any = default; return false; } } }