Skip to content

Commit

Permalink
Merge pull request #47 from shangfengh/dev
Browse files Browse the repository at this point in the history
feat: ✨ Add ListLocked
  • Loading branch information
DragonAura authored Dec 16, 2023
2 parents 9ff40a2 + 6050248 commit f3a8c2e
Show file tree
Hide file tree
Showing 2 changed files with 285 additions and 43 deletions.
164 changes: 121 additions & 43 deletions logic/Preparation/Utility/SafeValue/Atomic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public AtomicIntOnlyAddScore(int x, AtomicInt Score, double speed = 1.0) : base(
this.Score = Score;
this.speed = new(speed);
}
private int DealWithRemainder(double v)
private int DealWithRemainder(int v)
{
double q = v + remainder;
double q = speed * v + remainder;
int ans = (int)(q);
remainder.SetReturnOri(q - ans);
return ans;
Expand All @@ -63,25 +63,38 @@ public override int SetReturnOri(int value)
{
int previousV = Interlocked.Exchange(ref v, value);
if (value - previousV > 0)
Score.AddPositive((DealWithRemainder(speed * (value - previousV))));
Score.AddPositive((DealWithRemainder(value - previousV)));
return previousV;
}
/// <returns>返回操作前的值</returns>
public int SetReturnOriNotAddScore(int value)
{
return Interlocked.Exchange(ref v, value);
}
public override int Add(int x)
{
if (x > 0) Score.AddPositive(DealWithRemainder(speed * x));
if (x > 0) Score.AddPositive(DealWithRemainder(x));
return Interlocked.Add(ref v, x);
}
public int AddNotAddScore(int x)
{
return Interlocked.Add(ref v, x);
}
/// <summary>
/// 注意:确保参数为非负数
/// </summary>
public override int AddPositive(int x)
{
Score.AddPositive(DealWithRemainder(speed * x));
Score.AddPositive(DealWithRemainder(x));
return Interlocked.Add(ref v, x);
}
public override int Sub(int x)
{
if (x < 0) Score.AddPositive(DealWithRemainder(speed * -x));
if (x < 0) Score.AddPositive(DealWithRemainder(-x));
return Interlocked.Add(ref v, -x);
}
public int SubNotAddScore(int x)
{
return Interlocked.Add(ref v, -x);
}
/// <summary>
Expand All @@ -93,17 +106,26 @@ public override int SubPositive(int x)
}
public override int Inc()
{
Score.AddPositive(DealWithRemainder(speed));
Score.AddPositive(DealWithRemainder(1));
return Interlocked.Increment(ref v);
}
public int IncNotAddScore()
{
return Interlocked.Increment(ref v);
}
/// <returns>返回操作前的值</returns>
public override int CompareExReturnOri(int newV, int compareTo)
{
int previousV = Interlocked.CompareExchange(ref v, newV, compareTo);
if (newV - previousV > 0)
Score.AddPositive(DealWithRemainder(speed * (newV - previousV)));
Score.AddPositive(DealWithRemainder(newV - previousV));
return previousV;
}
/// <returns>返回操作前的值</returns>
public int CompareExReturnOriNotAddScore(int newV, int compareTo)
{
return Interlocked.CompareExchange(ref v, newV, compareTo);
}
}

/// <summary>
Expand All @@ -120,9 +142,9 @@ public AtomicIntOnlyAddLongScore(int x, AtomicLong Score, double speed = 1.0) :
this.Score = Score;
this.speed = new(speed);
}
private long DealWithRemainder(double v)
private long DealWithRemainder(int v)
{
double q = v + remainder;
double q = speed * v + remainder;
long ans = (long)(q);
remainder.SetReturnOri(q - ans);
return ans;
Expand All @@ -132,47 +154,62 @@ public override int SetReturnOri(int value)
{
int previousV = Interlocked.Exchange(ref v, value);
if (value - previousV > 0)
Score.AddPositive((DealWithRemainder(speed * (value - previousV))));
Score.AddPositive((DealWithRemainder(value - previousV)));
return previousV;
}
/// <returns>返回操作前的值</returns>
public int SetReturnOriNotAddScore(int value)
{
return Interlocked.Exchange(ref v, value);
}
public override int Add(int x)
{
if (x > 0) Score.AddPositive(DealWithRemainder(speed * x));
if (x > 0) Score.AddPositive(DealWithRemainder(x));
return Interlocked.Add(ref v, x);
}
public int AddNotAddScore(int x)
{
return Interlocked.Add(ref v, x);
}
/// <summary>
/// 注意:确保参数为非负数
/// </summary>
public override int AddPositive(int x)
{
Score.AddPositive(DealWithRemainder(speed * x));
Score.AddPositive(DealWithRemainder(x));
return Interlocked.Add(ref v, x);
}
public override int Sub(int x)
{
if (x < 0) Score.AddPositive(DealWithRemainder(speed * -x));
if (x < 0) Score.AddPositive(DealWithRemainder(-x));
return Interlocked.Add(ref v, -x);
}
/// <summary>
/// 注意:确保参数为非负数
/// </summary>
public override int SubPositive(int x)
public int SubNotAddScore(int x)
{
return Interlocked.Add(ref v, -x);
}
public override int Inc()
{
Score.AddPositive(DealWithRemainder(speed));
Score.AddPositive(DealWithRemainder(1));
return Interlocked.Increment(ref v);
}
public int IncNotAddScore()
{
return Interlocked.Increment(ref v);
}
/// <returns>返回操作前的值</returns>
public override int CompareExReturnOri(int newV, int compareTo)
{
int previousV = Interlocked.CompareExchange(ref v, newV, compareTo);
if (newV - previousV > 0)
Score.AddPositive(DealWithRemainder(speed * (newV - previousV)));
Score.AddPositive(DealWithRemainder(newV - previousV));
return previousV;
}
/// <returns>返回操作前的值</returns>
public int CompareExReturnOriNotAddScore(int newV, int compareTo)
{
return Interlocked.CompareExchange(ref v, newV, compareTo);
}
}

/// <summary>
Expand All @@ -190,9 +227,9 @@ public AtomicIntChangeAffectScore(int x, AtomicInt Score, double speed = 1.0) :
this.Score = Score;
this.speed = new(speed);
}
private int DealWithRemainder(double v)
private int DealWithRemainder(int v)
{
double q = v + remainder;
double q = speed * v + remainder;
int ans = (int)(q);
remainder.SetReturnOri(q - ans);
return ans;
Expand All @@ -201,52 +238,78 @@ private int DealWithRemainder(double v)
public override int SetReturnOri(int value)
{
int previousV = Interlocked.Exchange(ref v, value);
Score.Add(DealWithRemainder(speed * (value - previousV)));
Score.Add(DealWithRemainder(value - previousV));
return previousV;
}
/// <returns>返回操作前的值</returns>
public int SetReturnOriNotAddScore(int value)
{
return Interlocked.Exchange(ref v, value);
}
public override int Add(int x)
{
Score.Add(DealWithRemainder(speed * x));
Score.Add(DealWithRemainder(x));
return Interlocked.Add(ref v, x);
}
public int AddNotAddScore(int x)
{
return Interlocked.Add(ref v, x);
}
/// <summary>
/// 注意:确保参数为非负数
/// </summary>
public override int AddPositive(int x)
{
Score.AddPositive(DealWithRemainder(speed * x));
Score.AddPositive(DealWithRemainder(x));
return Interlocked.Add(ref v, x);
}
public override int Sub(int x)
{
Score.Sub(DealWithRemainder(speed * x));
Score.Sub(DealWithRemainder(x));
return Interlocked.Add(ref v, -x);
}
public int SubNotAddScore(int x)
{
return Interlocked.Add(ref v, -x);
}
/// <summary>
/// 注意:确保参数为非负数
/// </summary>
public override int SubPositive(int x)
{
Score.SubPositive(DealWithRemainder(speed * x));
Score.SubPositive(DealWithRemainder(x));
return Interlocked.Add(ref v, -x);
}
public override int Inc()
{
Score.AddPositive(DealWithRemainder(speed));
Score.AddPositive(DealWithRemainder(1));
return Interlocked.Increment(ref v);
}
public int IncNotAddScore()
{
return Interlocked.Increment(ref v);
}
public override int Dec()
{
Score.SubPositive(DealWithRemainder(speed));
Score.SubPositive(DealWithRemainder(1));
return Interlocked.Decrement(ref v);
}
public int DecNotAddScore()
{
return Interlocked.Decrement(ref v);
}
/// <returns>返回操作前的值</returns>
public override int CompareExReturnOri(int newV, int compareTo)
{
int previousV = Interlocked.CompareExchange(ref v, newV, compareTo);
Score.Add(DealWithRemainder(speed * (newV - previousV)));
Score.Add(DealWithRemainder(newV - previousV));
return previousV;
}
/// <returns>返回操作前的值</returns>
public int CompareExReturnOriNotAddScore(int newV, int compareTo)
{
return Interlocked.CompareExchange(ref v, newV, compareTo);
}
}

public class AtomicLong : Atomic
Expand Down Expand Up @@ -286,15 +349,15 @@ public class AtomicLongOnlyAddScore : AtomicLong
{
public AtomicLong Score { get; set; }
public AtomicDouble speed;
public AtomicDouble remainder;
public AtomicDouble remainder = new(0);
public AtomicLongOnlyAddScore(long x, AtomicLong score, double speed = 1.0) : base(x)
{
this.Score = score;
this.speed = new(speed);
}
private long DealWithRemainder(double v)
private long DealWithRemainder(long v)
{
double q = v + remainder;
double q = speed * v + remainder;
long ans = (long)(q);
remainder.SetReturnOri(q - ans);
return ans;
Expand All @@ -305,47 +368,62 @@ public override long SetReturnOri(long value)
{
long previousV = Interlocked.Exchange(ref v, value);
if (value - previousV > 0)
Score.AddPositive(DealWithRemainder(speed * (value - previousV)));
Score.AddPositive(DealWithRemainder(value - previousV));
return previousV;
}
/// <returns>返回操作前的值</returns>
public long SetReturnOriNotAddScore(long value)
{
return Interlocked.Exchange(ref v, value);
}
public override long Add(long x)
{
if (x > 0) Score.AddPositive(DealWithRemainder(speed * x));
if (x > 0) Score.AddPositive(DealWithRemainder(x));
return Interlocked.Add(ref v, x);
}
public long AddNotAddScore(long x)
{
return Interlocked.Add(ref v, x);
}
/// <summary>
/// 注意:确保参数为非负数
/// </summary>
public override long AddPositive(long x)
{
Score.AddPositive(DealWithRemainder(speed * x));
Score.AddPositive(DealWithRemainder(x));
return Interlocked.Add(ref v, x);
}
public override long Sub(long x)
{
if (x < 0) Score.AddPositive(DealWithRemainder(speed * -x));
if (x < 0) Score.AddPositive(DealWithRemainder(-x));
return Interlocked.Add(ref v, -x);
}
/// <summary>
/// 注意:确保参数为非负数
/// </summary>
public override long SubPositive(long x)
public long SubNotAddScore(long x)
{
return Interlocked.Add(ref v, -x);
}
public override long Inc()
{
Score.AddPositive(DealWithRemainder(speed));
Score.AddPositive(DealWithRemainder(1));
return Interlocked.Increment(ref v);
}
public long IncNotAddScore()
{
return Interlocked.Increment(ref v);
}
/// <returns>返回操作前的值</returns>
public override long CompareExReturnOri(long newV, long compareTo)
{
long previousV = Interlocked.CompareExchange(ref v, newV, compareTo);
if (newV - previousV > 0)
Score.AddPositive(DealWithRemainder(speed * (newV - previousV)));
Score.AddPositive(DealWithRemainder(newV - previousV));
return previousV;
}
/// <returns>返回操作前的值</returns>
public long CompareExReturnOriNotAddScore(long newV, long compareTo)
{
return Interlocked.CompareExchange(ref v, newV, compareTo);
}
}

public class AtomicDouble : Atomic
Expand Down
Loading

0 comments on commit f3a8c2e

Please sign in to comment.