Skip to content

farces/bigaccumulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 

Repository files navigation

Various Golang Packages

A bunch of dumb packages for doing fairly regular things in a more regular way than the standard library is designed for.

BigAccumulator

BigAccumulator is a wrapper for big.Int to be used for accumulators, with intermediate values buffered and flushed only when necessary.

x := bufbig.NewBigAccumulator()
x.AddInt(12345)
fmt.Println(x.Value())

NewBigAccumulator() creates a new accumulator with members set to their zero values (rather than nil). Use this rather than new(bufbig.BigAccumulator) where possible.
Value() *big.Int returns the underlying big.Int, which can be passed to functions requiring a big.Int explicitly.
AddInt(y int) adds the value y to an internal accumulator, which is flushed when .Value() is requested or when an overflow/underflow would occur.
SetString(string,base int) bool sets the value of the underlying big.Int to the string given in the base given. Resets the intermediate accumulator so SetValue() can be assumed to return the BigAccumulator to a clean state (intermediate val=0). Returns a boolean as to whether the call was successful; if unsuccessful, no change is made. Effectively a pass-through to big.Ints SetString().
SetBigInt(big.Int) Sets the underlying big.Int to the value passed to this method. Resets the accumulator, so it can be assumed to return the accumulator to a clean state (intermediate val=0).
Reset() returns the BigAccumulator to a state where both the big.Int and intermediate accumulator are effectively 0.

Uses

This package allows big.Int accumulators without the overhead of having to cast from int->int64->big.Int each time a small value is added. Replaces:

x := big.NewInt(0)
x.Add(x,big.NewInt(int64(y)))
fmt.Println(x)

with

x := bufbig.NewBigAccumulator()
x.AddInt(y)
fmt.Println(x.Value())

See bufbig_example.go

About

golang Buffered BigInt accumulator package

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages