コード例 #1
0
        public T GetRunningSum(int startIndex, int endIndex)
        {
            if ((startIndex < 0) || (startIndex >= this.Count))
            {
                throw new ArgumentOutOfRangeException("startIndex", startIndex, "The index must be greater or equal to zero and lesser than the collection Count.");
            }

            if ((endIndex < 0) || (endIndex >= this.Count))
            {
                throw new ArgumentOutOfRangeException("endIndex", endIndex, "The index must be greater or equal to zero and lesser than the collection Count.");
            }

            if (startIndex > endIndex)
            {
                throw new ArgumentException("The start index must be lesser than or equal to the end index.", "startIndex");
            }

            var x = FenwickTree <T> .ConvertIndex(endIndex);

            var y = FenwickTree <T> .ConvertIndex(startIndex - 1);

            var sum = default(T);

            while (x > y)
            {
                sum = this.Add(sum, m_collection[x]);
                x  -= FenwickTree <T> .FindLastDigit(x);
            }

            while (y > x)
            {
                sum = this.Substract(sum, m_collection[y]);
                y  -= FenwickTree <T> .FindLastDigit(y);
            }

            return(sum);
        }
コード例 #2
0
        public T this[int index]
        {
            get
            {
                return(this.GetRunningSum(index, index));
            }
            set
            {
                var current = this[index];
                if (object.Equals(current, value))
                {
                    return;
                }

                var diff = this.Substract(value, current);
                var x    = FenwickTree <T> .ConvertIndex(index);

                while (x < m_collection.Length)
                {
                    m_collection[x] = this.Add(m_collection[x], diff);
                    x += FenwickTree <T> .FindLastDigit(x);
                }
            }
        }