BigUIntAccumulator

An utility type to wrap a local buffer to accumulate unsigned numbers.

Members

Functions

approxCanMulPow5
bool approxCanMulPow5(size_t degree)
canMulPow2
bool canMulPow2(size_t degree)
canPut
bool canPut()
canPutN
bool canPutN(size_t n)
mulPow2
void mulPow2(size_t degree)
mulPow5
void mulPow5(size_t degree)
normalize
void normalize()

Strips most significant zero coefficients from the current view. Note: The view method may return a view with empty coefficients, which isn't usable. Put 0 or another number first to make the accumulator maintain a non-empty view.

put
void put(W coeffecient)

Places coefficient to the next most significant position.

view
BigUIntView!(W, endian) view()

Variables

coefficients
W[] coefficients;

A group of coefficients for a DecimalRadix!W.

length
size_t length;

Current length of initialized coefficients.

Examples

import std.traits;
alias AliasSeq(T...) = T;

foreach (T; AliasSeq!(uint, ulong))
foreach (endian; AliasSeq!(WordEndian.little, WordEndian.big))
{
    T[16 / T.sizeof] buffer;
    auto accumulator = BigUIntAccumulator!(T, endian)(buffer);
    assert(accumulator.length == 0);
    assert(accumulator.coefficients.length == buffer.length);
    assert(accumulator.view.coefficients.length == 0);
    // needs to put a number before any operations on `.view`
    accumulator.put(1);
    // compute N factorial
    auto N = 30;
    foreach(i; 1 .. N + 1)
    {
        if (auto overflow = accumulator.view *= i)
        {
            if (!accumulator.canPut)
                throw new Exception("Factorial buffer overflow");
            accumulator.put(overflow);
        }
    }
    assert(accumulator.view == BigUIntView!(T, endian).fromHexString("D13F6370F96865DF5DD54000000"));
}

Computes 13 * 10^^60

uint[7] buffer;
auto accumulator = BigUIntAccumulator!uint(buffer);
accumulator.put(13); // initial value
assert(accumulator.approxCanMulPow5(60));
accumulator.mulPow5(60);
assert(accumulator.canMulPow2(60));
accumulator.mulPow2(60);
assert(accumulator.view == BigUIntView!uint.fromHexString("81704fcef32d3bd8117effd5c4389285b05d000000000000000"));

Meta