This commit is contained in:
CortexCore
2024-03-31 23:31:00 +08:00
parent e179d2eb53
commit b7b89ee71a
641 changed files with 31286 additions and 22134 deletions

View File

@@ -6,6 +6,10 @@ namespace BITKit
{
public static class FuncExtensions
{
public static IEnumerable<Func<T>> CastAsFunc<T>(this Func<T> self)
{
return self is null ? Array.Empty<Func<T>>() : self?.GetInvocationList().Cast<Func<T>>();
}
public static IEnumerable<Func<T0, T1>> CastAsFunc<T0, T1>(this Func<T0, T1> self)
{
return self is null ? Array.Empty<Func<T0,T1>>() : self?.GetInvocationList().Cast<Func<T0, T1>>();

View File

@@ -19,5 +19,6 @@ namespace BITKit
}
return cacheName;
}
public static int Hash => typeof(T).GetHashCode();
}
}

View File

@@ -1,17 +1,174 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace BITKit
{
public static partial class Extensions
public sealed class CacheList<T> : IList<T>
{
public static IDictionary<TKey, TValue> CreateOrAddIfEmety<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey key, Func<TValue> createFactory)
public T[] ValueArray
{
if (self.ContainsKey(key) is false)
get
{
self.Add(key, createFactory.Invoke());
CheckChanges();
return _array;
}
return self;
}
private readonly IList<T> _listImplementation = new List<T>();
private T[] _array = Array.Empty<T>();
private IEnumerator<T> _enumerable=Array.Empty<T>().AsEnumerable().GetEnumerator();
private bool _isDirty = true;
public IEnumerator<T> GetEnumerator()
{
CheckChanges();
return _enumerable;
}
IEnumerator IEnumerable.GetEnumerator()
{
CheckChanges();
return _enumerable;
}
public void Add(T item)
{
_listImplementation.Add(item);
_isDirty = true;
}
public void Clear()
{
_isDirty = false;
_listImplementation.Clear();
_array = Array.Empty<T>();
}
public bool Contains(T item)
{
CheckChanges();
return _listImplementation.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
CheckChanges();
_listImplementation.CopyTo(array, arrayIndex);
_isDirty = true;
}
public bool Remove(T item)
{
_isDirty = true;
return _listImplementation.Remove(item);
}
public int Count
{
get
{
CheckChanges();
return _listImplementation.Count;
}
}
public bool IsReadOnly
{
get
{
CheckChanges();
return _listImplementation.IsReadOnly;
}
}
public int IndexOf(T item)
{
CheckChanges();
return _listImplementation.IndexOf(item);
}
public void Insert(int index, T item)
{
CheckChanges();
_listImplementation.Insert(index, item);
_isDirty = true;
}
public void RemoveAt(int index)
{
CheckChanges();
_listImplementation.RemoveAt(index);
_isDirty = true;
}
public T this[int index]
{
get
{
CheckChanges();
return _listImplementation[index];
}
set
{
_isDirty = true;
_listImplementation[index] = value;
CheckChanges();
}
}
public bool CheckChanges()
{
if (!_isDirty) return false;
_array = new T[_listImplementation.Count];
_listImplementation.CopyTo(_array, 0);
_enumerable = _array.AsEnumerable().GetEnumerator();
_isDirty = false;
return true;
}
}
public sealed class PrioritySelector<T>
{
public T Current { get; private set; }
public event Action<T> OnRelease;
private readonly SortedDictionary<int,T> _sortedDictionary = new();
public void Set(int priority, T item)
{
_sortedDictionary.Set(priority,item);
CheckChanges();
}
public void Remove(int priority)
{
_sortedDictionary.Remove(priority);
CheckChanges();
}
private void CheckChanges()
{
if (_sortedDictionary.Count is 0)
return;
var _current = _sortedDictionary.Last().Value;
switch (_current, Current)
{
case (not null, null):
Current = _current;
break;
case (null, not null):
Current = default;
break;
case (not null, not null):
if (Current.Equals(_current) is false)
{
Current = _current;
}
break;
case (null, null):
break;
}
OnRelease?.Invoke(Current);
}
}
}

View File

@@ -94,7 +94,7 @@ namespace BITKit
}
public static bool IsValid<T>(this IEnumerable<T> e)
{
return e is not null && e.Count() > 0;
return e is not null && e.Any();
}
public static bool TryAdd<T>(this IList<T> self, T t)
{