Skip to content

Commit b7060a6

Browse files
author
linzhijun
committed
fix
1 parent cd29842 commit b7060a6

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

csharp/ToolGood.Algorithm/Internals/Functions/MathSum/Function_MODE.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@ public override Operand Evaluate(AlgorithmEngine engine, Func<AlgorithmEngine, s
2929
dict[item] = 1;
3030
}
3131
}
32-
return Operand.Create(dict.OrderByDescending(q => q.Value).First().Key);
32+
decimal modeKey = 0;
33+
int maxCount = 0;
34+
foreach (var kvp in dict) {
35+
if (kvp.Value > maxCount) {
36+
maxCount = kvp.Value;
37+
modeKey = kvp.Key;
38+
}
39+
}
40+
return Operand.Create(modeKey);
3341
}
3442

3543
}

csharp/ToolGood.Algorithm/Operand.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ public abstract class Operand
3535
/// Zero
3636
/// </summary>
3737
public static readonly Operand Zero = Operand.Create(0m);
38+
/// <summary>
39+
/// Null
40+
/// </summary>
41+
public static readonly Operand Null = new OperandNull();
42+
private static readonly Operand[] IntCache = new Operand[256];
43+
static Operand()
44+
{
45+
for (int i = 0; i < 256; i++)
46+
IntCache[i] = new OperandInt(i - 128);
47+
}
3848

3949
#region IsNull IsNumber IsText IsBoolean IsArray IsDate IsJson IsArrayJson IsError ErrorMsg
4050
/// <summary>
@@ -161,6 +171,8 @@ public static Operand Create(short obj)
161171
/// <returns></returns>
162172
public static Operand Create(int obj)
163173
{
174+
if (obj >= -128 && obj <= 127)
175+
return IntCache[obj + 128];
164176
return new OperandInt(obj);
165177
}
166178

@@ -395,10 +407,7 @@ public static Operand Error(string msg, params object[] args)
395407
/// 创建操作数
396408
/// </summary>
397409
/// <returns></returns>
398-
public static Operand CreateNull()
399-
{
400-
return new OperandNull();
401-
}
410+
public static Operand CreateNull() => Null;
402411

403412
#endregion Create
404413

0 commit comments

Comments
 (0)