BitSet improvement

This commit is contained in:
Tnze
2022-12-12 09:13:02 +08:00
parent 86c7c954ec
commit 7bece3bca9

View File

@ -540,13 +540,17 @@ func (b BitSet) Set(index int, value bool) {
} }
} }
func (b BitSet) Len() int {
return len(b) * 64
}
// NewFixedBitSet make a [FixedBitSet] which can store n bits at least. // NewFixedBitSet make a [FixedBitSet] which can store n bits at least.
// If n <= 0, return nil // If n <= 0, return nil
func NewFixedBitSet(n int64) FixedBitSet { func NewFixedBitSet(n int64) FixedBitSet {
if n <= 0 { if n < 0 {
return nil return nil
} }
return make(FixedBitSet, (n-1)/8+1) return make(FixedBitSet, (n+7)/8)
} }
func (f FixedBitSet) WriteTo(w io.Writer) (n int64, err error) { func (f FixedBitSet) WriteTo(w io.Writer) (n int64, err error) {
@ -570,3 +574,7 @@ func (f FixedBitSet) Set(index int, value bool) {
f[index/8] &= ^(1 << (index % 8)) f[index/8] &= ^(1 << (index % 8))
} }
} }
func (f FixedBitSet) Len() int {
return len(f) * 8
}