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;
|
|
|
|
|
|
|
|
namespace BITKit
|
|
|
|
{
|
2024-02-05 21:16:08 +08:00
|
|
|
public sealed class CacheList<T> : IList<T>
|
|
|
|
{
|
|
|
|
private readonly IList<T> _listImplementation = new List<T>();
|
|
|
|
private T[] _array = Array.Empty<T>();
|
|
|
|
private bool _isDirty = true;
|
|
|
|
|
|
|
|
public IEnumerator<T> GetEnumerator()
|
|
|
|
{
|
|
|
|
CheckChanges();
|
|
|
|
return _listImplementation.GetEnumerator();
|
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
{
|
|
|
|
CheckChanges();
|
|
|
|
return ((IEnumerable)_listImplementation).GetEnumerator();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Add(T item)
|
|
|
|
{
|
|
|
|
//CheckChanges();
|
|
|
|
_listImplementation.Add(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
{
|
|
|
|
CheckChanges();
|
|
|
|
_listImplementation.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Contains(T item)
|
|
|
|
{
|
|
|
|
CheckChanges();
|
|
|
|
return _listImplementation.Contains(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void CopyTo(T[] array, int arrayIndex)
|
|
|
|
{
|
|
|
|
CheckChanges();
|
|
|
|
_listImplementation.CopyTo(array, arrayIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Remove(T item)
|
|
|
|
{
|
|
|
|
//CheckChanges();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveAt(int index)
|
|
|
|
{
|
|
|
|
CheckChanges();
|
|
|
|
_listImplementation.RemoveAt(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
public T this[int index]
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
CheckChanges();
|
|
|
|
return _listImplementation[index];
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_listImplementation[index] = value;
|
|
|
|
CheckChanges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CheckChanges()
|
|
|
|
{
|
|
|
|
if (!_isDirty) return;
|
|
|
|
_array = new T[_listImplementation.Count];
|
|
|
|
_listImplementation.CopyTo(_array, 0);
|
|
|
|
_isDirty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-12 01:43:24 +08:00
|
|
|
public static partial class Extensions
|
|
|
|
{
|
|
|
|
public static IDictionary<TKey, TValue> CreateOrAddIfEmety<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey key, Func<TValue> createFactory)
|
|
|
|
{
|
|
|
|
if (self.ContainsKey(key) is false)
|
|
|
|
{
|
|
|
|
self.Add(key, createFactory.Invoke());
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|