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