public override void Optimize() { base.Optimize(); // 遍历所有子项,如果有OpAdd的,把这个子项的所有子项提到当前 var add_items = _operators.Where(p => p.Item1 is OpAdd); if (add_items.Any()) { var childs = add_items.Select(p => ((OpAdd)p.Item1)._operators).Aggregate((a, b) => a.Concat(b)); if (childs.Any()) { _operators = _operators.Where(p => !(p.Item1 is OpAdd)).Concat(childs); // 重建变量位置信息 _operators = _operators.Select( i => Tuple.Create(i.Item1, i.Item1.GetKeys().Select(k => Array.IndexOf(_keys, k)).ToArray()) ); } } // 所有的Const的数合并成一个 double v = DoCalc(_operators.Where(p => p.Item1 is OpConst).Select(p => p.Item1.Calc(null)).ToArray()); _operators = _operators.Where(p => !(p.Item1 is OpConst)); if (v != 0) { IOperator op = new OpConst(v); var tmp = new List <Tuple <IOperator, int[]> >() { Tuple.Create(op, new int[0]) }; tmp.AddRange(_operators); _operators = tmp; } }
public override void Optimize() { if (_op.GetKeys().Length == 0) { _op = new OpConst(_op.Calc(null)); } else { _op.Optimize(); } }
public virtual void Optimize() { // 如果子数据不需要key且不是OpConst,直接替换成OpConst _operators = _operators.Select(p => { if (p.Item2.Length == 0 && !(p.Item1 is OpConst)) { IOperator op = new OpConst(p.Item1.Calc(null)); return(Tuple.Create(op, new int[0])); } else { p.Item1.Optimize(); return(p); } }); }