diff --git a/logic/Preparation/Utility/Value/SafeValue/LockedValue/InTheRange.cs b/logic/Preparation/Utility/Value/SafeValue/LockedValue/InTheRange.cs index 3e8088fb..e38a616f 100644 --- a/logic/Preparation/Utility/Value/SafeValue/LockedValue/InTheRange.cs +++ b/logic/Preparation/Utility/Value/SafeValue/LockedValue/InTheRange.cs @@ -15,6 +15,39 @@ public class InVariableRange : LockedValue, IIntAddable, IAddable, IDouble { protected T v; protected T maxV; + + #region NeedToDo + public virtual TResult ReadNeed(Func func) + { + lock (vLock) + { + return func(); + } + } + public virtual void ReadNeed(Action func) + { + lock (vLock) + { + func(); + } + } + + public virtual TResult WriteNeed(Func func) + { + lock (vLock) + { + return func(); + } + } + public virtual void WriteNeed(Action func) + { + lock (vLock) + { + func(); + } + } + #endregion + #region 构造与读取 public InVariableRange(T value, T maxValue) : base() { @@ -46,33 +79,34 @@ public InVariableRange(T maxValue) : base() public override string ToString() { - lock (vLock) - { - return "value:" + v.ToString() + " , maxValue:" + maxV.ToString(); - } + return ReadNeed(() => "value:" + v.ToString() + " , maxValue:" + maxV.ToString()); } - public T GetValue() { lock (vLock) return v; } + public T GetValue() { return ReadNeed(() => v); } public double ToDouble() => GetValue().ToDouble(null); public static implicit operator T(InVariableRange aint) => aint.GetValue(); - public T GetMaxV() { lock (vLock) return maxV; } - public (T, T) GetValueAndMaxV() { lock (vLock) return (v, maxV); } - public bool IsMaxV() { lock (vLock) return v == maxV; } + public T GetMaxV() + { + return ReadNeed(() => maxV); + } + public (T, T) GetValueAndMaxV() + { + return ReadNeed(() => (v, maxV)); + } + public bool IsMaxV() + { + return ReadNeed(() => v == maxV); + } #endregion #region 内嵌读取(在锁的情况下读取内容同时读取其他更基本的外部数据) public (T, long) GetValue(StartTime startTime) { - lock (vLock) - { - return (v, startTime.Get()); - } + return ReadNeed(() => (v, startTime.Get())); } + public (T, T, long) GetValueAndMaxValue(StartTime startTime) { - lock (vLock) - { - return (v, maxV, startTime.Get()); - } + return ReadNeed(() => (v, maxV, startTime.Get())); } #endregion @@ -80,75 +114,67 @@ public override string ToString() /// /// 若maxValue<=0则maxValue设为0并返回False /// - public virtual bool SetMaxV(T maxValue) + public bool SetMaxV(T maxValue) { if (maxValue <= T.Zero) { - lock (vLock) + return WriteNeed(() => { v = maxV = T.Zero; return false; - } + }); } - lock (vLock) + else { - maxV = maxValue; - if (v > maxValue) v = maxValue; + return WriteNeed(() => + { + maxV = maxValue; + if (v > maxValue) v = maxValue; + return true; + }); } - return true; } + /// /// 应当保证该maxValue>=0 /// - public virtual void SetPositiveMaxV(T maxValue) + public void SetPositiveMaxV(T maxValue) { - lock (vLock) + WriteNeed(() => { maxV = maxValue; - if (v > maxValue) v = maxValue; - } + if (v > maxV) v = maxV; + }); } - public virtual T SetRNow(T value) + public T SetRNow(T value) { if (value < T.Zero) { - lock (vLock) - { - return v = T.Zero; - } + return WriteNeed(() => v = T.Zero); } - lock (vLock) + else { - return v = (value > maxV) ? maxV : value; + return WriteNeed(() => v = (value > maxV) ? maxV : value); } } - public virtual void Set(double value) + public void Set(double value) { if (value < 0) { - lock (vLock) - { - v = T.Zero; - } + WriteNeed(() => v = T.Zero); } T va = T.CreateChecked(value); - lock (vLock) - { - v = (va > maxV) ? maxV : va; - } + WriteNeed(() => v = (va > maxV) ? maxV : va); } /// /// 应当保证该value>=0 /// - public virtual T SetPositiveVRNow(T value) + public T SetPositiveVRNow(T value) { - lock (vLock) - { - return v = (value > maxV) ? maxV : value; - } + return WriteNeed(() => v = (value > maxV) ? maxV : value); } #endregion @@ -156,219 +182,224 @@ public virtual T SetPositiveVRNow(T value) /// /// 如果当前值大于试图更新的maxValue,则更新maxValue失败 /// - public virtual bool TrySetMaxV(T maxValue) + public bool TrySetMaxV(T maxValue) { - lock (vLock) + return WriteNeed(() => { if (v > maxValue) return false; maxV = maxValue; return true; - } + }); } - public virtual void SetVToMaxV() + + public void SetVToMaxV() { - lock (vLock) + WriteNeed(() => { v = maxV; - } + }); } - public virtual void SetVToMaxV(double ratio) + + public void SetVToMaxV(double ratio) { - lock (vLock) - { - v = T.CreateChecked(maxV.ToDouble(null) * ratio); - } + WriteNeed(() => + v = T.CreateChecked(maxV.ToDouble(null) * ratio) + ); } - public virtual bool Set0IfNotMaxor0() + + public bool Set0IfNotMaxor0() { - lock (vLock) + return WriteNeed(() => { if (v < maxV && v > T.Zero) { v = T.Zero; return true; } - } - return false; + return false; + }); } - public virtual bool Set0IfMax() + public bool Set0IfMax() { - lock (vLock) + return WriteNeed(() => { if (v == maxV) { v = T.Zero; return true; } - } - return false; + return false; + }); } #endregion #region 普通运算 - public virtual void Add(T addV) + public void Add(T addV) { - lock (vLock) + WriteNeed(() => { v += addV; if (v < T.Zero) v = T.Zero; if (v > maxV) v = maxV; - } + }); } - public virtual void Add(int addV) + + public void Add(int addV) { - lock (vLock) + WriteNeed(() => { v += T.CreateChecked(addV); if (v < T.Zero) v = T.Zero; if (v > maxV) v = maxV; - } + }); } - public virtual T AddRNow(T addV) + public T AddRNow(T addV) { - lock (vLock) + return WriteNeed(() => { v += addV; if (v < T.Zero) v = T.Zero; if (v > maxV) v = maxV; return v; - } + }); } /// 返回实际改变量 - public virtual T AddRChange(T addV) + public T AddRChange(T addV) { - lock (vLock) + return WriteNeed(() => { T previousV = v; v += addV; if (v < T.Zero) v = T.Zero; if (v > maxV) v = maxV; return v - previousV; - } + }); } + /// /// 应当保证增加值大于0 /// /// 返回实际改变量 - public virtual T AddPositiveVRChange(T addPositiveV) + public T AddPositiveVRChange(T addPositiveV) { - lock (vLock) + WriteNeed(() => { addPositiveV = (addPositiveV < maxV - v) ? addPositiveV : maxV - v; v += addPositiveV; - } + }); return addPositiveV; } /// /// 应当保证增加值大于0 /// - public virtual void AddPositiveV(T addPositiveV) + public void AddPositiveV(T addPositiveV) { - lock (vLock) + WriteNeed(() => { v += addPositiveV; if (v > maxV) v = maxV; - } + }); } - public virtual void Mul(T mulV) + public void Mul(T mulV) { if (mulV <= T.Zero) { - lock (vLock) v = T.Zero; + WriteNeed(() => v = T.Zero); return; } - lock (vLock) + WriteNeed(() => { if (v > maxV / mulV) v = maxV; //避免溢出 else v *= mulV; - } + }); } - public virtual void Mul(TA mulV) where TA : IConvertible, INumber + public void Mul(TA mulV) where TA : IConvertible, INumber { if (mulV < TA.Zero) { - lock (vLock) v = T.Zero; + WriteNeed(() => v = T.Zero); return; } - lock (vLock) + WriteNeed(() => { if (v > T.CreateChecked(maxV.ToDouble(null) / mulV.ToDouble(null))) v = maxV; //避免溢出 else v = T.CreateChecked(v.ToDouble(null) * mulV.ToDouble(null)); - } + }); } /// /// 应当保证乘数大于0 /// - public virtual void MulPositiveV(T mulPositiveV) + public void MulPositiveV(T mulPositiveV) { - lock (vLock) + WriteNeed(() => { if (v > maxV / mulPositiveV) v = maxV; //避免溢出 else v *= mulPositiveV; - } + }); } /// /// 应当保证乘数大于0 /// - public virtual void MulPositiveV(TA mulV) where TA : IConvertible, INumber + public void MulPositiveV(TA mulV) where TA : IConvertible, INumber { - lock (vLock) + WriteNeed(() => { - if (v > T.CreateChecked(maxV.ToDouble(null) / mulV.ToDouble(null))) v = maxV; //避免溢出 + if (v > T.CreateChecked(maxV.ToDouble(null) / mulV.ToDouble(null))) v = maxV; // Avoid overflow else v = T.CreateChecked(v.ToDouble(null) * mulV.ToDouble(null)); - } + }); } + /// 返回实际改变量 - public virtual T SubRChange(T subV) + public T SubRChange(T subV) { - lock (vLock) + return WriteNeed(() => { T previousV = v; v -= subV; if (v < T.Zero) v = T.Zero; if (v > maxV) v = maxV; return v - previousV; - } + }); } - public virtual T SubRNow(T subV) + public T SubRNow(T subV) { - lock (vLock) + return WriteNeed(() => { v -= subV; if (v < T.Zero) v = T.Zero; if (v > maxV) v = maxV; return v; - } + }); } /// /// 应当保证该减少值大于0 /// /// 返回实际改变量 - public virtual T SubPositiveVRChange(T subPositiveV) + public T SubPositiveVRChange(T subPositiveV) { - lock (vLock) + WriteNeed(() => { subPositiveV = (subPositiveV < v) ? subPositiveV : v; v -= subPositiveV; - } + }); return subPositiveV; } /// /// 应当保证该减少值大于0 /// - public virtual void SubPositiveV(T subPositiveV) + public void SubPositiveV(T subPositiveV) { - lock (vLock) + WriteNeed(() => { v = (subPositiveV < v) ? v - subPositiveV : T.Zero; - } + }); } #endregion @@ -377,9 +408,9 @@ public virtual void SubPositiveV(T subPositiveV) /// 试图加到满,如果无法加到maxValue则不加并返回-1 /// /// 返回实际改变量 - public virtual T TryAddToMaxVRChange(T addV) + public T TryAddToMaxVRChange(T addV) { - lock (vLock) + return WriteNeed(() => { if (maxV - v <= addV) { @@ -388,41 +419,41 @@ public virtual T TryAddToMaxVRChange(T addV) return addV; } return -T.One; - } + }); } /// /// ratio可以为负 /// /// 返回实际改变量 - public virtual T VAddPartMaxVRChange(double ratio) + public T VAddPartMaxVRChange(double ratio) { - lock (vLock) + return WriteNeed(() => { T preV = v; v += T.CreateChecked(ratio * maxV.ToDouble(null)); if (v < T.Zero) v = T.Zero; if (v > maxV) v = maxV; return v - preV; - } + }); } #endregion #region 与InVariableRange类的运算,运算会影响该对象的值 - public virtual T AddRChange(InVariableRange a, double speed = 1.0) where TA : IConvertible, IComparable, INumber + public T AddRChange(InVariableRange a, double speed = 1.0) where TA : IConvertible, IComparable, INumber { - return EnterOtherLock(a, () => + return EnterOtherLock(a, () => WriteNeed(() => { T previousV = v; v += T.CreateChecked(a.GetValue().ToDouble(null) * speed); if (v > maxV) v = maxV; a.SubPositiveVRChange(TA.CreateChecked(v - previousV)); return v - previousV; - })!; + }))!; } - public virtual T AddVUseOtherRChange(T value, InVariableRange other, double speed = 1.0) where TA : IConvertible, IComparable, INumber + public T AddVUseOtherRChange(T value, InVariableRange other, double speed = 1.0) where TA : IConvertible, IComparable, INumber { - return EnterOtherLock(other, () => + return EnterOtherLock(other, () => WriteNeed(() => { T previousV = v; T otherValue = T.CreateChecked(other.GetValue().ToDouble(null) * speed); @@ -431,18 +462,18 @@ public virtual T AddVUseOtherRChange(T value, InVariableRange other, dou if (v > maxV) v = maxV; other.SubPositiveVRChange(TA.CreateChecked((v - previousV).ToDouble(null) / speed)); return v - previousV; - })!; + }))!; } - public virtual T SubRChange(InVariableRange a) where TA : IConvertible, IComparable, IComparable, INumber + public T SubRChange(InVariableRange a) where TA : IConvertible, IComparable, IComparable, INumber { - return EnterOtherLock(a, () => + return EnterOtherLock(a, () => WriteNeed(() => { T previousV = v; v -= T.CreateChecked(a.GetValue()); if (v < T.Zero) v = T.Zero; a.SubPositiveVRChange(TA.CreateChecked(previousV - v)); return v - previousV; - })!; + }))!; } #endregion @@ -452,23 +483,23 @@ public virtual T SubRChange(InVariableRange a) where TA : IConvertible, /// 如果无法加到maxValue则不加 /// /// 返回试图加到的值与最大值 - public virtual (T, T, long) TryAddToMaxV(StartTime startTime, double speed = 1.0) + public (T, T, long) TryAddToMaxV(StartTime startTime, double speed = 1.0) { - lock (vLock) + return WriteNeed(() => { long addV = (long)(startTime.StopIfPassing((maxV - v).ToInt64(null)) * speed); if (addV < 0) return (v, maxV, startTime.Get()); if (maxV - v < T.CreateChecked(addV)) return (v = maxV, maxV, startTime.Get()); return (v, maxV, startTime.Get()); - } + }); } /// /// 增加量为时间差*速度,并将startTime变为long.MaxValue /// /// 返回实际改变量 - public virtual T AddRChange(StartTime startTime, double speed = 1.0) + public T AddRChange(StartTime startTime, double speed = 1.0) { - lock (vLock) + return WriteNeed(() => { T previousV = v; T addV = T.CreateChecked((Environment.TickCount64 - startTime.Stop()) * speed); @@ -476,7 +507,7 @@ public virtual T AddRChange(StartTime startTime, double speed = 1.0) else v += addV; if (v > maxV) v = maxV; return v - previousV; - } + }); } /// @@ -484,9 +515,9 @@ public virtual T AddRChange(StartTime startTime, double speed = 1.0) /// 如果无法加到maxValue则清零 /// /// 返回是否清零 - public virtual bool Set0IfNotAddToMaxV(StartTime startTime, double speed = 1.0) + public bool Set0IfNotAddToMaxV(StartTime startTime, double speed = 1.0) { - lock (vLock) + return WriteNeed(() => { if (v == maxV) return false; T addV = T.CreateChecked(startTime.StopIfPassing((maxV - v).ToInt64(null)) * speed); @@ -502,7 +533,7 @@ public virtual bool Set0IfNotAddToMaxV(StartTime startTime, double speed = 1.0) } v = T.Zero; return true; - } + }); } #endregion } @@ -546,187 +577,27 @@ public IDouble Speed } } - public TA ScoreAdd(Func x) + + public override TResult WriteNeed(Func func) { lock (vLock) { T previousV = v; - TA ans = x(); + TResult ans = func(); if (v > previousV) Score.Add((int)((v - previousV).ToDouble(null) * speed.ToDouble())); return ans; } } - public void ScoreAdd(Action x) + public override void WriteNeed(Action func) { lock (vLock) { T previousV = v; - x(); + func(); if (v > previousV) Score.Add((int)((v - previousV).ToDouble(null) * speed.ToDouble())); } } - - public override bool SetMaxV(T maxValue) - { - return ScoreAdd(() => base.SetMaxV(maxValue)); - } - - public override void SetPositiveMaxV(T maxValue) - { - ScoreAdd(() => base.SetPositiveMaxV(maxValue)); - } - - public override void Set(double value) - { - ScoreAdd(() => base.Set(value)); - } - - public override T SetRNow(T value) - { - return ScoreAdd(() => base.SetRNow(value)); - } - - public override T SetPositiveVRNow(T value) - { - return ScoreAdd(() => base.SetPositiveVRNow(value)); - } - - public override bool TrySetMaxV(T maxValue) - { - return ScoreAdd(() => base.TrySetMaxV(maxValue)); - } - - public override void SetVToMaxV() - { - ScoreAdd(() => base.SetVToMaxV()); - } - - public override void SetVToMaxV(double ratio) - { - ScoreAdd(() => base.SetVToMaxV(ratio)); - } - - public override bool Set0IfNotMaxor0() - { - return ScoreAdd(() => base.Set0IfNotMaxor0()); - } - - public override bool Set0IfMax() - { - return ScoreAdd(() => base.Set0IfMax()); - } - - public override void Add(T addV) - { - ScoreAdd(() => base.Add(addV)); - } - - public override void Add(int addV) - { - ScoreAdd(() => base.Add(addV)); - } - - public override T AddRNow(T addV) - { - return ScoreAdd(() => base.AddRNow(addV)); - } - - public override T AddRChange(T addV) - { - return ScoreAdd(() => base.AddRChange(addV)); - } - - public override T AddPositiveVRChange(T addPositiveV) - { - return ScoreAdd(() => base.AddPositiveVRChange(addPositiveV)); - } - - public override void AddPositiveV(T addPositiveV) - { - ScoreAdd(() => base.AddPositiveV(addPositiveV)); - } - - public override void Mul(T mulV) - { - ScoreAdd(() => base.Mul(mulV)); - } - - public override void Mul(TA mulV) - { - ScoreAdd(() => base.Mul(mulV)); - } - - public override void MulPositiveV(T mulPositiveV) - { - ScoreAdd(() => base.MulPositiveV(mulPositiveV)); - } - - public override void MulPositiveV(TA mulV) - { - ScoreAdd(() => base.MulPositiveV(mulV)); - } - - public override T SubRChange(T subV) - { - return ScoreAdd(() => base.SubRChange(subV)); - } - - public override T SubRNow(T subV) - { - return ScoreAdd(() => base.SubRNow(subV)); - } - - public override T SubPositiveVRChange(T subPositiveV) - { - return ScoreAdd(() => base.SubPositiveVRChange(subPositiveV)); - } - - public override void SubPositiveV(T subPositiveV) - { - ScoreAdd(() => base.SubPositiveV(subPositiveV)); - } - - public override T TryAddToMaxVRChange(T addV) - { - return ScoreAdd(() => base.TryAddToMaxVRChange(addV)); - } - - public override T VAddPartMaxVRChange(double ratio) - { - return ScoreAdd(() => base.VAddPartMaxVRChange(ratio)); - } - - public override T AddRChange(InVariableRange a, double speed = 1.0) - { - return ScoreAdd(() => base.AddRChange(a, speed)); - } - - public override T AddVUseOtherRChange(T value, InVariableRange other, double speed = 1.0) - { - return ScoreAdd(() => base.AddVUseOtherRChange(value, other, speed)); - } - - public override T SubRChange(InVariableRange a) - { - return ScoreAdd(() => base.SubRChange(a)); - } - - public override (T, T, long) TryAddToMaxV(StartTime startTime, double speed = 1.0) - { - return ScoreAdd<(T, T, long)>(() => base.TryAddToMaxV(startTime, speed)); - } - - public override T AddRChange(StartTime startTime, double speed = 1.0) - { - return ScoreAdd(() => base.AddRChange(startTime, speed)); - } - - public override bool Set0IfNotAddToMaxV(StartTime startTime, double speed = 1.0) - { - return ScoreAdd(() => base.Set0IfNotAddToMaxV(startTime, speed)); - } - } } diff --git a/logic/Preparation/Utility/Value/SafeValue/LockedValue/LockedValue.cs b/logic/Preparation/Utility/Value/SafeValue/LockedValue/LockedValue.cs index 196ec148..d63d760a 100644 --- a/logic/Preparation/Utility/Value/SafeValue/LockedValue/LockedValue.cs +++ b/logic/Preparation/Utility/Value/SafeValue/LockedValue/LockedValue.cs @@ -42,7 +42,7 @@ public LockedValue() if (thatLock) Monitor.Exit(a.VLock); } } - public void EnterOtherLock(LockedValue a, Action func) + public void EnterOtherLock(LockedValue a, Action func) { if (this.idInClass == a.idInClass) return; bool thisLock = false;