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/u32.go

31 lines
697 B
Go

package bit_storage
import "encoding/json"
type bitU32 struct {
V uint32
}
var _ BitStorage[uint32] = &bitU32{}
func EmptyU32() *bitU32 { return &bitU32{} }
func (b *bitU32) Set(v uint32) { b.V |= v }
func (b *bitU32) Clear(v uint32) { b.V &^= v }
func (b *bitU32) Get(v uint32) bool { return b.V&v > 0 }
func (b *bitU32) Unique() []uint32 {
z := make([]uint32, 0)
m := 0
n := b.V
for n > 0 {
z = append(z, (n&2)<<m)
n >>= 1
m++
}
return z
}
func (b *bitU32) Dump() []uint32 { return []uint32{b.V} }
func (b *bitU32) MarshalJSON() ([]byte, error) { return json.Marshal(b.V) }
func (b *bitU32) UnmarshalJSON(bytes []byte) error { return json.Unmarshal(bytes, &b.V) }