Skip to content

Commit d5cb64c

Browse files
committed
fix
1 parent d297d0a commit d5cb64c

File tree

2 files changed

+332
-283
lines changed

2 files changed

+332
-283
lines changed

csharp/ToolGood.Algorithm.Fast/Internals/Functions/FunctionBase.cs

Lines changed: 2 additions & 283 deletions
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,7 @@ public override string ToString()
3232

3333
}
3434

35-
internal class Function_Value : FunctionBase
36-
{
37-
private Operand _value;
38-
39-
public Function_Value(Operand value)
40-
{
41-
_value = value;
42-
}
43-
44-
public override Operand Calculate(AlgorithmEngine work)
45-
{
46-
return _value;
47-
}
48-
public override void ToString(StringBuilder stringBuilder, bool addBrackets)
49-
{
50-
if (_value.Type == OperandType.TEXT) {
51-
stringBuilder.Append('"');
52-
stringBuilder.Append(_value.TextValue.Replace("\"", "\"\""));
53-
stringBuilder.Append('"');
54-
} else if (_value.Type == OperandType.DATE) {
55-
stringBuilder.Append('"');
56-
stringBuilder.Append(_value.DateValue.ToString());
57-
stringBuilder.Append('"');
58-
} else {
59-
stringBuilder.Append(_value.ToString());
60-
}
61-
}
62-
63-
}
64-
35+
6536
internal abstract class Function_1 : FunctionBase
6637
{
6738
protected FunctionBase func1;
@@ -125,25 +96,6 @@ protected Function_N(FunctionBase[] funcs)
12596
}
12697
}
12798

128-
internal class Function_ERROR : Function_1
129-
{
130-
public Function_ERROR(FunctionBase func1) : base(func1)
131-
{
132-
}
133-
134-
public override Operand Calculate(AlgorithmEngine work)
135-
{
136-
var t = func1.Calculate(work).TextValue;
137-
return Operand.Error(t);
138-
}
139-
140-
public override void ToString(StringBuilder stringBuilder, bool addBrackets)
141-
{
142-
stringBuilder.Append("ERROR(");
143-
func1.ToString(stringBuilder, false);
144-
stringBuilder.Append(")");
145-
}
146-
}
14799

148100
#region * / % + - &
149101

@@ -1007,10 +959,6 @@ public override void ToString(StringBuilder stringBuilder, bool addBrackets)
1007959
#endregion AND OR
1008960

1009961

1010-
1011-
1012-
1013-
1014962
#region Lookup
1015963

1016964
internal class Function_VLOOKUP : Function_N
@@ -1115,234 +1063,5 @@ public override void ToString(StringBuilder stringBuilder, bool addBrackets)
11151063

11161064
#endregion Lookup
11171065

1118-
#region getValue
1119-
1120-
internal class Function_Array : Function_N
1121-
{
1122-
public Function_Array(FunctionBase[] funcs) : base(funcs)
1123-
{
1124-
}
1125-
1126-
public override Operand Calculate(AlgorithmEngine work)
1127-
{
1128-
var args = new List<Operand>();
1129-
foreach (var item in funcs) { var aa = item.Calculate(work); if (aa.IsError) { return aa; } args.Add(aa); }
1130-
return Operand.Create(args);
1131-
}
1132-
public override void ToString(StringBuilder stringBuilder, bool addBrackets)
1133-
{
1134-
stringBuilder.Append("Array(");
1135-
for (int i = 0; i < funcs.Length; i++) {
1136-
if (i > 0) {
1137-
stringBuilder.Append(", ");
1138-
}
1139-
funcs[i].ToString(stringBuilder, false);
1140-
}
1141-
stringBuilder.Append(')');
1142-
}
1143-
}
1144-
1145-
internal class Function_NUM : FunctionBase
1146-
{
1147-
private decimal d;
1148-
private string unit;
1149-
1150-
public Function_NUM(decimal func1, string func2)
1151-
{
1152-
d = func1;
1153-
unit = func2;
1154-
}
1155-
1156-
public override Operand Calculate(AlgorithmEngine work)
1157-
{
1158-
var dict = NumberUnitTypeHelper.GetUnitTypedict();
1159-
var d2 = NumberUnitTypeHelper.TransformationUnit(d, dict[unit], work.DistanceUnit, work.AreaUnit, work.VolumeUnit, work.MassUnit);
1160-
return Operand.Create(d2);
1161-
}
1162-
}
1163-
1164-
internal class Function_PARAMETER : FunctionBase
1165-
{
1166-
private string name;
1167-
private FunctionBase func1;
1168-
1169-
public Function_PARAMETER(string name)
1170-
{
1171-
this.name = name;
1172-
}
1173-
1174-
public Function_PARAMETER(FunctionBase func1)
1175-
{
1176-
this.func1 = func1;
1177-
}
1178-
1179-
public override Operand Calculate(AlgorithmEngine work)
1180-
{
1181-
if (string.IsNullOrEmpty(name)) {
1182-
return work.GetParameter(func1.Calculate(work).TextValue);
1183-
}
1184-
return work.GetParameter(name);
1185-
}
1186-
}
1187-
1188-
internal class Function_GetJsonValue : Function_2
1189-
{
1190-
public Function_GetJsonValue(FunctionBase func1, FunctionBase func2) : base(func1, func2)
1191-
{
1192-
}
1193-
1194-
public override Operand Calculate(AlgorithmEngine work)
1195-
{
1196-
var obj = func1.Calculate(work); if (obj.IsError) { return obj; }
1197-
var op = func2.Calculate(work); if (op.IsError) { return op; }
1198-
1199-
if (obj.Type == OperandType.ARRARY) {
1200-
op = op.ToNumber("ARRARY index is error!");
1201-
if (op.IsError) { return op; }
1202-
var index = op.IntValue - work.ExcelIndex;
1203-
if (index < obj.ArrayValue.Count)
1204-
return obj.ArrayValue[index];
1205-
return Operand.Error($"ARRARY index {index} greater than maximum length!");
1206-
}
1207-
if (obj.Type == OperandType.ARRARYJSON) {
1208-
if (op.Type == OperandType.NUMBER) {
1209-
if (((OperandKeyValueList)obj).TryGetValue(op.NumberValue.ToString(), out Operand operand)) {
1210-
return operand;
1211-
}
1212-
return Operand.Error($"Parameter name `{op.TextValue}` is missing!");
1213-
} else if (op.Type == OperandType.TEXT) {
1214-
if (((OperandKeyValueList)obj).TryGetValue(op.TextValue, out Operand operand)) {
1215-
return operand;
1216-
}
1217-
return Operand.Error($"Parameter name `{op.TextValue}` is missing!");
1218-
}
1219-
return Operand.Error("Parameter name is missing!");
1220-
}
1221-
1222-
if (obj.Type == OperandType.JSON) {
1223-
var json = obj.JsonValue;
1224-
if (json.IsArray) {
1225-
op = op.ToNumber("JSON parameter index is error!");
1226-
if (op.IsError) { return op; }
1227-
var index = op.IntValue - work.ExcelIndex;
1228-
if (index < json.Count) {
1229-
var v = json[index];
1230-
if (v.IsString) return Operand.Create(v.StringValue);
1231-
if (v.IsBoolean) return Operand.Create(v.BooleanValue);
1232-
if (v.IsDouble) return Operand.Create(v.NumberValue);
1233-
if (v.IsObject) return Operand.Create(v);
1234-
if (v.IsArray) return Operand.Create(v);
1235-
if (v.IsNull) return Operand.CreateNull();
1236-
return Operand.Create(v);
1237-
}
1238-
return Operand.Error($"JSON index {index} greater than maximum length!");
1239-
} else {
1240-
op = op.ToText("JSON parameter name is error!");
1241-
if (op.IsError) { return op; }
1242-
var v = json[op.TextValue];
1243-
if (v != null) {
1244-
if (v.IsString) return Operand.Create(v.StringValue);
1245-
if (v.IsBoolean) return Operand.Create(v.BooleanValue);
1246-
if (v.IsDouble) return Operand.Create(v.NumberValue);
1247-
if (v.IsObject) return Operand.Create(v);
1248-
if (v.IsArray) return Operand.Create(v);
1249-
if (v.IsNull) return Operand.CreateNull();
1250-
return Operand.Create(v);
1251-
}
1252-
}
1253-
}
1254-
return Operand.Error(" Operator is error!");
1255-
}
1256-
}
1257-
1258-
internal class Function_DiyFunction : Function_N
1259-
{
1260-
private string funName;
1261-
1262-
public Function_DiyFunction(string name, FunctionBase[] funcs) : base(funcs)
1263-
{
1264-
this.funName = name;
1265-
}
1266-
1267-
public override Operand Calculate(AlgorithmEngine work)
1268-
{
1269-
var args = new List<Operand>();
1270-
foreach (var item in funcs) { var aa = item.Calculate(work); args.Add(aa); }
1271-
return work.ExecuteDiyFunction(funName, args);
1272-
}
1273-
}
1274-
1275-
internal class Function_PARAM : Function_2
1276-
{
1277-
public Function_PARAM(FunctionBase func1, FunctionBase func2) : base(func1, func2)
1278-
{
1279-
}
1280-
1281-
public override Operand Calculate(AlgorithmEngine work)
1282-
{
1283-
var args1 = func1.Calculate(work); if (args1.Type != OperandType.TEXT) { args1 = args1.ToText(); if (args1.IsError) return args1; }
1284-
var result = work.GetParameter(args1.TextValue);
1285-
if (result.IsError) {
1286-
if (func2 != null) {
1287-
return func2.Calculate(work);
1288-
}
1289-
}
1290-
return result;
1291-
}
1292-
}
1293-
1294-
internal class Function_ArrayJson : Function_N
1295-
{
1296-
public Function_ArrayJson(FunctionBase[] funcs) : base(funcs)
1297-
{
1298-
}
1299-
1300-
public override Operand Calculate(AlgorithmEngine work)
1301-
{
1302-
OperandKeyValueList result = new OperandKeyValueList(null);
1303-
foreach (var item in funcs) {
1304-
var o = item.Calculate(work);
1305-
result.AddValue((KeyValue)((OperandKeyValue)o).Value);
1306-
}
1307-
return result;
1308-
}
1309-
public override void ToString(StringBuilder stringBuilder, bool addBrackets)
1310-
{
1311-
stringBuilder.Append("ArrayJson(");
1312-
for (int i = 0; i < funcs.Length; i++) {
1313-
if (i > 0) {
1314-
stringBuilder.Append(", ");
1315-
}
1316-
funcs[i].ToString(stringBuilder, false);
1317-
}
1318-
stringBuilder.Append(')');
1319-
}
1320-
}
1321-
1322-
internal class Function_ArrayJsonItem : Function_1
1323-
{
1324-
private string key;
1325-
1326-
public Function_ArrayJsonItem(string key, FunctionBase func1) : base(func1)
1327-
{
1328-
this.key = key;
1329-
}
1330-
1331-
public override Operand Calculate(AlgorithmEngine work)
1332-
{
1333-
KeyValue keyValue = new KeyValue();
1334-
keyValue.Key = key;
1335-
keyValue.Value = func1.Calculate(work);
1336-
return new OperandKeyValue(keyValue);
1337-
}
1338-
public override void ToString(StringBuilder stringBuilder, bool addBrackets)
1339-
{
1340-
stringBuilder.Append(key);
1341-
stringBuilder.Append(":");
1342-
func1.ToString(stringBuilder, false);
1343-
}
1344-
1345-
}
1346-
1347-
#endregion getValue
1066+
13481067
}

0 commit comments

Comments
 (0)