sum method
Returns the sum up to the given index index_to
.
Example:
If the tree is built with 1, 2, 3, 4, 5
,
then sum(4) will return the sum of 1, 2, 3, 4, 5
which is 15.
Implementation
@override
T sum({
required final int index_to,
}) {
int i = index_to;
assert(i > -1 && array.length > i + 1, "");
T sum = algebra.identity();
i++;
while (i > 0) {
sum = algebra.operate(sum, array[i]);
i -= i & -i;
}
if (i == array.length - 1) {
return algebra.operate(sum, array[i]);
} else {
return sum;
}
}