Пример #1
0
            public long GetCurrentValue(IChannel ch, long count, long valMax)
            {
                // Note: This method should not modify any fields.
                AssertValid(valMax);
                Contracts.Assert(count >= _cna);

                // If the sum is zero, return zero.
                if ((_sumHi | _sumLo) == 0)
                {
                    // If all values in a given column are NAs issue a warning.
                    if (count == _cna)
                    {
                        ch.Warning("All values in this column are NAs, using default value for imputation");
                    }
                    return(0);
                }

                Contracts.Assert(count > _cna);
                count -= _cna;
                Contracts.Assert(count > 0);

                ulong sumHi = _sumHi;
                ulong sumLo = _sumLo;

                bool neg = (long)sumHi < 0;

                if (neg)
                {
                    // Negative value. Work with the absolute value.
                    sumHi = ~sumHi;
                    sumLo = ~sumLo;
                    IntUtils.Add(ref sumHi, ref sumLo, 1);
                }

                // If this assert triggers, the caller did something wrong. Update should have been
                // called at most count times and each value should have been within the range of
                // a ulong, so the absolute value of the sum can't possibly be so large that sumHi
                // reaches or exceeds count. This assert implies that the Div part of the DivRound
                // call won't throw.
                Contracts.Assert(sumHi < (ulong)count);

                ulong res = IntUtils.DivRound(sumLo, sumHi, (ulong)count);

                Contracts.Assert(0 <= res && res <= (ulong)valMax);
                return(neg ? -(long)res : (long)res);
            }
Пример #2
0
            public void Update(long?val, long valMax)
            {
                AssertValid(valMax);
                Contracts.Assert(!val.HasValue || -valMax <= val && val <= valMax);

                if (!val.HasValue)
                {
                    _cna++;
                }
                else if (val >= 0)
                {
                    IntUtils.Add(ref _sumHi, ref _sumLo, (ulong)val);
                }
                else
                {
                    IntUtils.Sub(ref _sumHi, ref _sumLo, (ulong)(-val));
                }

                AssertValid(valMax);
            }
Пример #3
0
        public void TestAddition()
        {
            IUtility utilityInt = new IntUtils();

            Assert.Equal("23", utilityInt.Add("2", "21"));
        }