fix errors
This commit is contained in:
@@ -1,17 +1,23 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace Skeleton.DataStructure;
|
namespace Skeleton.DataStructure;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// store value from calculation to avoid redundant computations
|
/// store value from calculation to avoid redundant computations
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class CacheItem
|
public class CacheItem
|
||||||
{
|
{
|
||||||
|
|
||||||
|
public CacheItem(Func<CacheItem, object> cal) => ProxyCalculator = cal;
|
||||||
|
|
||||||
|
public static readonly CacheItem Null = new CacheItem<bool>(x => false);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// if the item needs update
|
/// if the item needs update
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool Expired { get; protected set; } = true;
|
public bool Expired { get; protected set; } = true;
|
||||||
internal HashSet<CacheItem> References { get; set; } = new();
|
internal HashSet<CacheItem> References { get; set; } = new();
|
||||||
internal HashSet<CacheItem> Dependencies { get; set; } = new();
|
internal HashSet<CacheItem> Dependencies { get; set; } = new();
|
||||||
|
private Func<object> Calculator => () => ProxyCalculator(this);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// tag the item and all dependencies as out dated
|
/// tag the item and all dependencies as out dated
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -22,6 +28,32 @@ public abstract class CacheItem
|
|||||||
if (!c.Expired)
|
if (!c.Expired)
|
||||||
c.Expire();
|
c.Expire();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TObject Get<TObject>() => (this as CacheItem<TObject>)!.Get;
|
||||||
|
private object? Value { get; set; }
|
||||||
|
public void Assign(object o) => UpdateCalculation(x => o);
|
||||||
|
public object Get()
|
||||||
|
{
|
||||||
|
if (Expired)
|
||||||
|
{
|
||||||
|
Value = Calculator();
|
||||||
|
Expired = false;
|
||||||
|
}
|
||||||
|
return Value!;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TObject GetFrom<TObject>(CacheItem item) => (this as CacheItem<TObject>)!.GetFrom(item);
|
||||||
|
|
||||||
|
public object GetFrom(CacheItem item)
|
||||||
|
{
|
||||||
|
References.Add(item);
|
||||||
|
item.Dependencies.Add(this);
|
||||||
|
return Get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Func<CacheItem, object> ProxyCalculator { get; internal set; }
|
||||||
|
|
||||||
|
public void UpdateCalculation(Func<CacheItem, object> a) => ProxyCalculator = a;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@@ -31,12 +63,15 @@ public class CacheItem<TObject> : CacheItem
|
|||||||
/// construct by provide a method to update the value
|
/// construct by provide a method to update the value
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="rec"></param>
|
/// <param name="rec"></param>
|
||||||
public CacheItem(Func<CacheItem, TObject> rec) => ProxyCalculator = rec;
|
public CacheItem(Func<CacheItem, TObject> rec) : base(x => rec(x))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
private TObject? Value { get; set; }
|
private TObject? Value { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// return cached/updated value when reference is not used to calculate another cache item
|
/// return cached/updated value when reference is not used to calculate another cache item
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public TObject Get
|
public new TObject Get
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
@@ -49,16 +84,21 @@ public class CacheItem<TObject> : CacheItem
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
private Func<TObject> Calculator => () => ProxyCalculator(this);
|
private Func<TObject> Calculator => () => ProxyCalculator(this);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// provide trace information while calculating
|
/// provide trace information while calculating
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Func<CacheItem, TObject> ProxyCalculator { get; internal set; }
|
public new Func<CacheItem, TObject> ProxyCalculator
|
||||||
|
{
|
||||||
|
get => x => (TObject)base.ProxyCalculator(x);
|
||||||
|
set => base.ProxyCalculator = x => value(x)!;
|
||||||
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// trace the dependency and return cached value/updated value
|
/// trace the dependency and return cached value/updated value
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="d"></param>
|
/// <param name="d"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public TObject GetFrom(CacheItem d)
|
public new TObject GetFrom(CacheItem d)
|
||||||
{
|
{
|
||||||
References.Add(d);
|
References.Add(d);
|
||||||
d.Dependencies.Add(this);
|
d.Dependencies.Add(this);
|
||||||
@@ -78,5 +118,7 @@ public class CacheItem<TObject> : CacheItem
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void Assign(TObject b) => UpdateCalculation(x => b);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user