BITFALL/Assets/BITKit/Core/Extensions/IDictionary.cs

174 lines
4.3 KiB
C#
Raw Normal View History

2023-08-12 01:43:24 +08:00
using System;
2024-02-05 21:16:08 +08:00
using System.Collections;
2023-08-12 01:43:24 +08:00
using System.Collections.Generic;
2024-02-21 01:40:53 +08:00
using System.Linq;
2023-08-12 01:43:24 +08:00
namespace BITKit
{
2024-02-05 21:16:08 +08:00
public sealed class CacheList<T> : IList<T>
{
2024-02-21 01:40:53 +08:00
public T[] ValueArray
{
get
{
CheckChanges();
return _array;
}
}
2024-02-05 21:16:08 +08:00
private readonly IList<T> _listImplementation = new List<T>();
private T[] _array = Array.Empty<T>();
2024-02-21 01:40:53 +08:00
private IEnumerator<T> _enumerable=Array.Empty<T>().AsEnumerable().GetEnumerator();
2024-02-05 21:16:08 +08:00
private bool _isDirty = true;
public IEnumerator<T> GetEnumerator()
{
2024-02-21 01:40:53 +08:00
CheckChanges();
return _enumerable;
2024-02-05 21:16:08 +08:00
}
IEnumerator IEnumerable.GetEnumerator()
{
2024-02-21 01:40:53 +08:00
CheckChanges();
return _enumerable;
2024-02-05 21:16:08 +08:00
}
public void Add(T item)
{
_listImplementation.Add(item);
2024-02-21 01:40:53 +08:00
_isDirty = true;
2024-02-05 21:16:08 +08:00
}
public void Clear()
{
2024-02-21 01:40:53 +08:00
_isDirty = false;
2024-02-05 21:16:08 +08:00
_listImplementation.Clear();
2024-02-21 01:40:53 +08:00
_array = Array.Empty<T>();
2024-02-05 21:16:08 +08:00
}
public bool Contains(T item)
{
CheckChanges();
return _listImplementation.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
CheckChanges();
_listImplementation.CopyTo(array, arrayIndex);
2024-02-21 01:40:53 +08:00
_isDirty = true;
2024-02-05 21:16:08 +08:00
}
public bool Remove(T item)
{
2024-02-21 01:40:53 +08:00
_isDirty = true;
2024-02-05 21:16:08 +08:00
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);
2024-02-21 01:40:53 +08:00
_isDirty = true;
2024-02-05 21:16:08 +08:00
}
public void RemoveAt(int index)
{
CheckChanges();
_listImplementation.RemoveAt(index);
2024-02-21 01:40:53 +08:00
_isDirty = true;
2024-02-05 21:16:08 +08:00
}
public T this[int index]
{
get
{
CheckChanges();
return _listImplementation[index];
}
set
{
2024-02-21 01:40:53 +08:00
_isDirty = true;
2024-02-05 21:16:08 +08:00
_listImplementation[index] = value;
CheckChanges();
}
}
2024-02-21 01:40:53 +08:00
public bool CheckChanges()
2024-02-05 21:16:08 +08:00
{
2024-02-21 01:40:53 +08:00
if (!_isDirty) return false;
2024-02-05 21:16:08 +08:00
_array = new T[_listImplementation.Count];
_listImplementation.CopyTo(_array, 0);
2024-02-21 01:40:53 +08:00
_enumerable = _array.AsEnumerable().GetEnumerator();
2024-02-05 21:16:08 +08:00
_isDirty = false;
2024-02-21 01:40:53 +08:00
return true;
2024-02-05 21:16:08 +08:00
}
}
2024-02-21 01:40:53 +08:00
public sealed class PrioritySelector<T>
2023-08-12 01:43:24 +08:00
{
2024-02-21 01:40:53 +08:00
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()
2023-08-12 01:43:24 +08:00
{
2024-02-21 01:40:53 +08:00
if (_sortedDictionary.Count is 0)
return;
var _current = _sortedDictionary.Last().Value;
switch (_current, Current)
2023-08-12 01:43:24 +08:00
{
2024-02-21 01:40:53 +08:00
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;
2023-08-12 01:43:24 +08:00
}
2024-02-21 01:40:53 +08:00
OnRelease?.Invoke(Current);
2023-08-12 01:43:24 +08:00
}
}
}