From dd68bede47f9e7cf237365838cbc50d6b350eebd Mon Sep 17 00:00:00 2001 From: hzhang Date: Wed, 3 Jul 2024 12:20:08 +0800 Subject: [PATCH] Upgrade structure of code base --- src/DataStructure/CacheItem.cs | 63 ++++++++-------------------------- 1 file changed, 14 insertions(+), 49 deletions(-) diff --git a/src/DataStructure/CacheItem.cs b/src/DataStructure/CacheItem.cs index dc06044..7c4ac24 100644 --- a/src/DataStructure/CacheItem.cs +++ b/src/DataStructure/CacheItem.cs @@ -1,5 +1,3 @@ -using System.Diagnostics; - namespace Skeleton.DataStructure; /// @@ -8,16 +6,12 @@ namespace Skeleton.DataStructure; public class CacheItem { - public CacheItem(Func cal) => ProxyCalculator = cal; - - public static readonly CacheItem Null = new CacheItem(x => false); /// /// if the item needs update /// public bool Expired { get; protected set; } = true; - internal HashSet References { get; set; } = new(); - internal HashSet Dependencies { get; set; } = new(); - private Func Calculator => () => ProxyCalculator(this); + public HashSet References { get; set; } = new(); + public HashSet Dependencies { get; set; } = new(); /// /// tag the item and all dependencies as out dated /// @@ -29,76 +23,48 @@ public class CacheItem c.Expire(); } - public TObject Get() => (this as CacheItem)!.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(CacheItem item) => (this as CacheItem)!.GetFrom(item); - - public object GetFrom(CacheItem item) - { - References.Add(item); - item.Dependencies.Add(this); - return Get(); - } - - public Func ProxyCalculator { get; internal set; } - - public void UpdateCalculation(Func a) => ProxyCalculator = a; } /// public class CacheItem : CacheItem { + public static readonly CacheItem Null = new(); /// /// construct by provide a method to update the value /// /// - public CacheItem(Func rec) : base(x => rec(x)) - { - } + public CacheItem(Func rec) => ProxyCalculator = rec; - private TObject? Value { get; set; } + public CacheItem() => ProxyCalculator = c => default; + + protected TObject? Value { get; set; } /// /// return cached/updated value when reference is not used to calculate another cache item /// - public new TObject Get + public virtual TObject? Get { get { if (Expired) { - Value = Calculator(); + Value = ProxyCalculator(this); Expired = false; } - return Value!; + return Value; } } - private Func Calculator => () => ProxyCalculator(this); /// /// provide trace information while calculating /// - public new Func ProxyCalculator - { - get => x => (TObject)base.ProxyCalculator(x); - set => base.ProxyCalculator = x => value(x)!; - } + public Func ProxyCalculator { get; set; } + /// /// trace the dependency and return cached value/updated value /// /// /// - public new TObject GetFrom(CacheItem d) + public TObject? GetFrom(CacheItem d) { References.Add(d); d.Dependencies.Add(this); @@ -108,7 +74,7 @@ public class CacheItem : CacheItem /// update the formula of item value /// /// - public void UpdateCalculation(Func val) + public void UpdateCalculation(Func val) { Expire(); foreach (CacheItem item in Dependencies) @@ -117,7 +83,6 @@ public class CacheItem : CacheItem ProxyCalculator = val; } - public void Assign(TObject b) => UpdateCalculation(x => b); }