github.com/ranjib/nomad@v0.1.1-0.20160225204057-97751b02f70b/nomad/structs/bitmap.go (about) 1 package structs 2 3 import "fmt" 4 5 // Bitmap is a simple uncompressed bitmap 6 type Bitmap []byte 7 8 // NewBitmap returns a bitmap with up to size indexes 9 func NewBitmap(size int) (Bitmap, error) { 10 if size <= 0 { 11 return nil, fmt.Errorf("bitmap must be positive size") 12 } 13 if size&7 != 0 { 14 return nil, fmt.Errorf("bitmap must be byte aligned") 15 } 16 b := make([]byte, size>>3) 17 return Bitmap(b), nil 18 } 19 20 // Set is used to set the given index of the bitmap 21 func (b Bitmap) Set(idx uint) { 22 bucket := idx >> 3 23 mask := byte(1 << (idx & 7)) 24 b[bucket] |= mask 25 } 26 27 // Check is used to check the given index of the bitmap 28 func (b Bitmap) Check(idx uint) bool { 29 bucket := idx >> 3 30 mask := byte(1 << (idx & 7)) 31 return (b[bucket] & mask) != 0 32 } 33 34 // Clear is used to efficiently clear the bitmap 35 func (b Bitmap) Clear() { 36 for i := range b { 37 b[i] = 0 38 } 39 }