BITKit/Src/Core/Extensions/IEnumerable.cs

230 lines
6.9 KiB
C#

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<T>(this T[] self, System.Action<T> action, Func<bool> _if = null)
{
_if = _if ?? Constant.True;
if (self is not null)
{
if (_if.Invoke())
Array.ForEach<T>(self, action);
}
}
public static void ForEach<TKey, TValue>(this IDictionary<TKey, TValue> self, Action<KeyValuePair<TKey, TValue>> action)
{
if (self.IsValid())
{
if (self.IsValid())
for (int i = 0; i < self.Count(); i++)
{
action.Invoke(self.ElementAt(i));
}
}
}
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> self, Action<T> action, Func<bool> _if = null)
{
_if = _if ?? Constant.True;
self.ForEach((index, x) =>
{
if (_if.Invoke())
action.Invoke(x);
});
return self;
}
public static IEnumerable<T> IfEmety<T>(this IEnumerable<T> self, Action action)
{
if (self?.Count() is 0)
{
action.Invoke();
}
return self;
}
public static IEnumerable<T> CreateOrAddIfEmety<T>(this IEnumerable<T> self, Func<IEnumerable<T>> createFactory)
{
if (self is null)
{
return createFactory.Invoke();
}
return self;
}
public static IEnumerable<T> CreateOrAddIfEmety<T>(this IEnumerable<T> self, IEnumerable<T> source, T value)
{
if (self?.Count() is 0)
{
if (source is not List<T>)
{
self = self.ToList();
}
switch (source)
{
case List<T> list:
list.Add(value);
break;
default:
throw new Exception($"未支持的类型{self.GetType().Name}");
}
}
return self;
}
public static IEnumerable<T> CreateOrAddIfEmety<T>(this IEnumerable<T> self, IEnumerable<T> source, Func<T> action)
{
if (self?.Count() is 0)
{
CreateOrAddIfEmety(self, source, action.Invoke());
}
return self;
}
public static void ForEach<T>(this IEnumerable<T> self, Action<int, T> action)
{
if (self.IsValid())
for (int i = 0; i < self.Count(); i++)
{
action.Invoke(i, self.ElementAt(i));
}
}
public static bool IsNull<T>(this IEnumerable<T> e)
{
return e.IsValid() == false;
}
public static bool IsValid<T>(this IEnumerable<T> e)
{
return e is not null && e.Any();
}
public static bool TryAdd<T>(this IList<T> self, T t)
{
if (self.Contains(t))
{
return false;
}
else
{
self.Add(t);
return true;
}
}
public static bool TryGetElementAt<T>(this IEnumerable<T> 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<TKey, TValue>(this IDictionary<TKey, TValue> 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<T>(this IList<T> self, T t)
{
if (self.Contains(t))
{
self.Remove(t);
return true;
}
return false;
}
public static IEnumerable<T> RemoveIn<T>(this IEnumerable<T> self, T t)
{
var list = self.ToList();
list.Remove(t);
return list.ToArray();
}
public static bool TryRemove<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey t)
{
return self.ContainsKey(t) && self.Remove(t);
}
public static void Set<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey key, TValue value)
{
if (self.ContainsKey(key))
{
self[key] = value;
}
else
{
self.Add(key, value);
}
}
public static TValue Get<TKey,TValue>(this IDictionary<Type,TValue> self)
{
return self[typeof(TKey)];
}
public static TValue[] TakeRandom<TValue>(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<TKey, TValue>(this IDictionary<TKey, TValue> 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<T> TrySelectFirst<T>(this IEnumerable<T> self, Action<T> action)
{
if (self.Any())
{
action.Invoke(self.First());
}
return self;
}
public static bool TryGetAny<T>(this IEnumerable<T> self, Func<T, bool> factory, out T any)
{
foreach (var x in self)
{
if (factory.Invoke(x))
{
any = x;
return true;
}
}
any = default;
return false;
}
}
}