repositorypackage
0.0.0-20250223202917-4219d1278309
Repository: https://github.com/koykov/bitvector.git
Documentation: pkg.go.dev
# README
Bitvector
The package provides implementations of bit arrays of arbitrary length. Bit array allows to manipulate with single bits:
- set bit to 1 in given position
- clear bit to 0 in given position
- get bit value in given position
- and reset the whole array
Currently, supports two types of arrays:
Both have the same interface, but concurrent version supports simultaneously read and write.
Usage
Vector example:
import "github.com/koykov/bitvector"
vec := bitvector.NewVector(100)
vec.Set(50) // set bit value to 1 at position 50
vec.Get(50) // read bit value at position 50
vec.Unset(50) // clear bit at position 50
This type allows simultaneous read and write, but doesn't provide data race protection. A good use case is preliminary set required bits values and then read them in concurrent manner.
ConcurrentVector example:
import "github.com/koykov/bitvector"
v := bitvector.NewConcurrentVector(100)
go func(){ for { v.Set(50) } }
go func(){ for { v.Get(50) } }
go func(){ for { v.Unset(50) } }
In opposite to Vector, this type supports simultaneous read and write and provides data race protection. It uses atomics inside, thus works without exclusive locks and works fast (check benchmarks).