BITKit/Src/Core/Extensions/IEnumerable.cs

234 lines
7.0 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
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;
}
[Obsolete("Use TryGetElementAt instead")]
public static bool TryGet<T>(this IEnumerable<T> self, int index, out T value)=>TryGetElementAt(self, index, out value);
public static bool TryInsert<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey key, TValue value)
{
lock (self)
{
if (self.ContainsKey(key))
{
self[key] = value;
}
else
{
self.Add(key, value);
}
return true;
}
}
public static void Insert<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey key, TValue value)
{
TryInsert(self, key, value);
}
public static TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey t)
{
lock (self)
{
if (self.TryGetValue(t, out TValue value))
{
}
else
{
value = Activator.CreateInstance<TValue>();
self.Add(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 bool TryRemove<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey t)
{
if (self.ContainsKey(t))
{
self.Remove(t);
return true;
}
return false;
}
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 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;
}
}
}