This repository has been archived on 2024-04-07. You can view files and clone it, but cannot push or open issues or pull requests.
summer-utils/bit-storage/u32inf.go

53 lines
1.0 KiB
Go

package bit_storage
import "encoding/json"
type bitU32inf struct {
V []uint32
}
var _ BitStorage[uint32] = &bitU32inf{}
func EmptyInfiniteU32() *bitU32inf { return &bitU32inf{V: make([]uint32, 0)} }
func ParseInfiniteU32(v []uint32) *bitU32inf { return &bitU32inf{V: v} }
func (b *bitU32inf) Set(v uint32) {
a := int(v / 32)
l := len(b.V)
if a > l {
b.V = append(b.V, make([]uint32, a-l)...)
}
b.V[a] |= v % 32
}
func (b *bitU32inf) Clear(v uint32) {
if int(v/32) > len(b.V) {
return
}
b.V[v/32] &^= v % 32
}
func (b *bitU32inf) Get(v uint32) bool {
if int(v/32) > len(b.V) {
return false
}
return b.V[v/32]&(v%32) > 0
}
func (b *bitU32inf) Unique() []uint32 {
z := make([]uint32, 0)
m := 0
for i := range b.V {
n := b.V[i]
for n > 0 {
z = append(z, (n&2)<<m)
n >>= 1
m++
}
}
return z
}
func (b *bitU32inf) Dump() []uint32 {
return b.V
}
func (b *bitU32inf) MarshalJSON() ([]byte, error) { return json.Marshal(b.V) }
func (b *bitU32inf) UnmarshalJSON(bytes []byte) error { return json.Unmarshal(bytes, &b.V) }