github.com/zeebo/mon@v0.0.0-20211012163247-13d39bdb54fa/floathist/bitmap.go (about)

     1  package floathist
     2  
     3  import (
     4  	"math/bits"
     5  	"sync/atomic"
     6  )
     7  
     8  type b32 [1]uint32
     9  
    10  func (b b32) UnsafeClone() b32          { return b }
    11  func (b b32) UnsafeUint32() uint32      { return b[0] }
    12  func (b *b32) UnsafeSet(idx uint)       { b[0] |= 1 << (idx & 31) }
    13  func (b *b32) UnsafeSetUint32(v uint32) { b[0] = v }
    14  
    15  func (b *b32) Clone() b32        { return b32{atomic.LoadUint32(&b[0])} }
    16  func (b *b32) Set(idx uint)      { atomic.AddUint32(&b[0], 1<<(idx&31)) }
    17  func (b *b32) Has(idx uint) bool { return atomic.LoadUint32(&b[0])&(1<<(idx&31)) > 0 }
    18  
    19  func (b *b32) Next() (idx uint32, ok bool) {
    20  	u := b[0]
    21  	c := u & (u - 1)
    22  	idx = uint32(bits.Len32(u ^ c))
    23  	b[0] = c
    24  	return (idx - 1) % 32, u > 0
    25  }