Static bit vector structures library
A bit vector is an array data structure that compactly stores bits.
This library is based on 5 static different data structures:
uint8
uint16
uint32
uint64
uint64
(for ASCII problems)The rationale of using a static integer compared to a dynamic []byte
is first of all to save memory.
There is no structure and/or slice overhead.
Hence, you might be interested in this library for memory-bound computation.
Also, the operations (get, set, etc.) are way more efficient. A simple benchmark shows that it's about 10 times more efficient than using a byte slice. Moreover, there is a guarantee that the internal bit vectors will not escape to the heap and remain only at the stack level.
Yet, the only drawback is to have a fixed-size bit vector (8, 16, 32, 64 or 128). If you require a dynamic bit vector, you should take a look at dropbox/godropbox for example.
go get github.com/teivah/bitvector
var bv bitvector.Len8
var bv bitvector.Len16
var bv bitvector.Len32
var bv bitvector.Len64
var bv bitvector.Ascii
// Or to reinitialize the bit vector
bv = bitvector.NewAscii()
bv = bv.Set(i, true)
bv = bv.Set(i, false)
b := bv.Get(i) // bool
bv = bv.Toggle(i)
bv = bv.Clear(i, j)
i := bv.Count() // uint8
bv := bv1.And(bv2)
bv := bv1.Or(bv2)
bv := bv1.Xor(bv2)
bv := bv1.AndNot(bv2)
bv = bv.Push(2)
bv = bv.Pop(2)
s := bv.String() // string