zhao
2021-07-19 8347f2fbddbd25369359dcb2da1233ac48a19fdc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* Licensed to the Apache Software Foundation (ASF) Under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for Additional information regarding copyright ownership.
* The ASF licenses this file to You Under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed Under the License is distributed on an "AS Is" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations Under the License.
*/
 
namespace HH.WMS.Utils.NPOI.SS.Formula.Functions
{
    using System;
    using System.Collections.Generic;
    using HH.WMS.Utils.NPOI.SS.Formula.Eval;
    using HH.WMS.Utils.NPOI.SS.Formula;
 
    /**
     * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
     * This Is the base class for all excel function evaluator
     * classes that take variable number of operands, and
     * where the order of operands does not matter
     */
    public abstract class MultiOperandNumericFunction : Function
    {
        static double[] EMPTY_DOUBLE_ARRAY = { };
        private bool _isReferenceBoolCounted;
        private bool _isBlankCounted;
 
        protected MultiOperandNumericFunction(bool isReferenceBoolCounted, bool isBlankCounted)
        {
            _isReferenceBoolCounted = isReferenceBoolCounted;
            _isBlankCounted = isBlankCounted;
        }
        protected internal abstract double Evaluate(double[] values);
 
        public ValueEval Evaluate(ValueEval[] args, int srcCellRow, int srcCellCol)
        {
 
            double d;
            try
            {
                double[] values = GetNumberArray(args);
                d = Evaluate(values);
            }
            catch (EvaluationException e)
            {
                return e.GetErrorEval();
            }
 
            if (Double.IsNaN(d) || Double.IsInfinity(d))
                return ErrorEval.NUM_ERROR;
 
            return new NumberEval(d);
        }
 
        private class DoubleList
        {
            private double[] _array;
            private int _Count;
 
            public DoubleList()
            {
                _array = new double[8];
                _Count = 0;
            }
 
            public double[] ToArray()
            {
                if (_Count < 1)
                {
                    return EMPTY_DOUBLE_ARRAY;
                }
                double[] result = new double[_Count];
                Array.Copy(_array, 0, result, 0, _Count);
                return result;
            }
 
            public void Add(double[] values)
            {
                int AddLen = values.Length;
                EnsureCapacity(_Count + AddLen);
                Array.Copy(values, 0, _array, _Count, AddLen);
                _Count += AddLen;
            }
 
            private void EnsureCapacity(int reqSize)
            {
                if (reqSize > _array.Length)
                {
                    int newSize = reqSize * 3 / 2; // grow with 50% extra
                    double[] newArr = new double[newSize];
                    Array.Copy(_array, 0, newArr, 0, _Count);
                    _array = newArr;
                }
            }
 
            public void Add(double value)
            {
                EnsureCapacity(_Count + 1);
                _array[_Count] = value;
                _Count++;
            }
        }
 
        private static int DEFAULT_MAX_NUM_OPERANDS = 30;
 
        /**
         * Maximum number of operands accepted by this function.
         * Subclasses may override to Change default value.
         */
        protected int MaxNumOperands
        {
            get
            {
                return DEFAULT_MAX_NUM_OPERANDS;
            }
        }
        /**
     *  Whether to count nested subtotals.
     */
        public virtual bool IsSubtotalCounted
        {
            get
            {
                return true;
            }
        }
        /**
 * Collects values from a single argument
 */
        private void CollectValues(ValueEval operand, DoubleList temp)
        {
 
            if (operand is TwoDEval)
            {
                TwoDEval ae = (TwoDEval)operand;
                int width = ae.Width;
                int height = ae.Height;
                for (int rrIx = 0; rrIx < height; rrIx++)
                {
                    for (int rcIx = 0; rcIx < width; rcIx++)
                    {
                        ValueEval ve = ae.GetValue(rrIx, rcIx);
                        if (!IsSubtotalCounted && ae.IsSubTotal(rrIx, rcIx)) continue;
                        CollectValue(ve, true, temp);
                    }
                }
                return;
            }
            if (operand is RefEval)
            {
                RefEval re = (RefEval)operand;
                CollectValue(re.InnerValueEval, true, temp);
                return;
            }
            CollectValue((ValueEval)operand, false, temp);
        }
        private void CollectValue(ValueEval ve, bool isViaReference, DoubleList temp)
        {
            if (ve == null)
            {
                throw new ArgumentException("ve must not be null");
            }
            if (ve is NumberEval)
            {
                NumberEval ne = (NumberEval)ve;
                temp.Add(ne.NumberValue);
                return;
            }
            if (ve is ErrorEval)
            {
                throw new EvaluationException((ErrorEval)ve);
            }
            if (ve is StringEval)
            {
                if (isViaReference)
                {
                    // ignore all ref strings
                    return;
                }
                String s = ((StringEval)ve).StringValue;
                Double d = OperandResolver.ParseDouble(s);
                if (double.IsNaN(d))
                {
                    throw new EvaluationException(ErrorEval.VALUE_INVALID);
                }
                temp.Add(d);
                return;
            }
            if (ve is BoolEval)
            {
                if (!isViaReference || _isReferenceBoolCounted)
                {
                    BoolEval boolEval = (BoolEval)ve;
                    temp.Add(boolEval.NumberValue);
                }
                return;
            }
            if (ve == BlankEval.instance)
            {
                if (_isBlankCounted)
                {
                    temp.Add(0.0);
                }
                return;
            }
            throw new InvalidOperationException("Invalid ValueEval type passed for conversion: ("
                    + ve.GetType() + ")");
        }
        /**
         * Returns a double array that contains values for the numeric cells
         * from among the list of operands. Blanks and Blank equivalent cells
         * are ignored. Error operands or cells containing operands of type
         * that are considered invalid and would result in #VALUE! error in
         * excel cause this function to return <c>null</c>.
         *
         * @return never <c>null</c>
         */
        protected double[] GetNumberArray(ValueEval[] operands)
        {
            if (operands.Length > MaxNumOperands)
            {
                throw EvaluationException.InvalidValue();
            }
            DoubleList retval = new DoubleList();
 
            for (int i = 0, iSize = operands.Length; i < iSize; i++)
            {
                CollectValues(operands[i], retval);
            }
            return retval.ToArray();
        }
        /**
         * Ensures that a two dimensional array has all sub-arrays present and the same Length
         * @return <c>false</c> if any sub-array Is missing, or Is of different Length
         */
        protected static bool AreSubArraysConsistent(double[][] values)
        {
 
            if (values == null || values.Length < 1)
            {
                // TODO this doesn't seem right.  Fix or Add comment.
                return true;
            }
 
            if (values[0] == null)
            {
                return false;
            }
            int outerMax = values.Length;
            int innerMax = values[0].Length;
            for (int i = 1; i < outerMax; i++)
            { // note - 'i=1' start at second sub-array
                double[] subArr = values[i];
                if (subArr == null)
                {
                    return false;
                }
                if (innerMax != subArr.Length)
                {
                    return false;
                }
            }
            return true;
        }
 
    }
}