This commit is contained in:
CortexCore
2025-03-24 14:42:40 +08:00
parent 18239a5ae4
commit 9845d20f7f
99 changed files with 5418 additions and 5512 deletions

View File

@@ -122,44 +122,20 @@ namespace BITKit
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)
public static TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey t) where TValue : new()
{
lock (self)
if (self.TryGetValue(t, out var value))
{
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;
}
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))

View File

@@ -1,9 +1,64 @@
using System;
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.DependencyInjection;
using Unity.Mathematics;
namespace BITKit
{
public static partial class Extensions
{
private static readonly ConcurrentDictionary<int,ConcurrentDictionary<int,object>> Services = new();
public static bool QueryComponents<T>(this IServiceProvider self,out T t1) where T : class
{
var id = self.GetHashCode();
var typeId = typeof(T).GetHashCode();
if (Services.GetOrCreate(id).TryGetValue(typeId, out var value) is false)
{
value = self.GetService<T>();
Services.GetOrCreate(id).TryAdd(typeId,value);
}
if (value is null)
{
t1 = null;
return false;
}
t1 = Unsafe.As<T>(value);
return true;
}
public static bool QueryComponents<T,T1> (this IServiceProvider self,out T t1,out T1 t2) where T : class where T1 : class
{
t1 = null;
t2 = null;
if (QueryComponents(self,out t1) is false) return false;
if (QueryComponents(self,out t2) is false) return false;
return true;
}
public static bool QueryComponents<T,T1,T2> (this IServiceProvider self,out T t1,out T1 t2,out T2 t3) where T : class where T1 : class where T2 : class
{
t1 = null;
t2 = null;
t3 = null;
if (QueryComponents(self,out t1) is false) return false;
if (QueryComponents(self,out t2) is false) return false;
if (QueryComponents(self,out t3) is false) return false;
return true;
}
public static bool QueryComponents<T,T1,T2,T3> (this IServiceProvider self,out T t1,out T1 t2,out T2 t3,out T3 t4) where T : class where T1 : class where T2 : class where T3 : class
{
t1 = null;
t2 = null;
t3 = null;
t4 = null;
if (QueryComponents(self,out t1) is false) return false;
if (QueryComponents(self,out t2) is false) return false;
if (QueryComponents(self,out t3) is false) return false;
if (QueryComponents(self,out t4) is false) return false;
return true;
}
public static T IsNull<T>(this T t, Action action)
{
if (t is null)