58 lines
903 B
Go
58 lines
903 B
Go
|
package cdkey
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
type bitmap struct {
|
||
|
size int
|
||
|
set int64
|
||
|
}
|
||
|
|
||
|
func newBitmap(size int, set int64) *bitmap {
|
||
|
if size < 0 || size > 63 {
|
||
|
panic("size must be between 0 and 63")
|
||
|
}
|
||
|
if size == 0 {
|
||
|
size = 32
|
||
|
}
|
||
|
m := &bitmap{size: size, set: set}
|
||
|
return m
|
||
|
}
|
||
|
|
||
|
func (m *bitmap) Get(index int) bool {
|
||
|
return m.GetInt(index) != 0
|
||
|
}
|
||
|
|
||
|
func (m *bitmap) GetInt(index int) int64 {
|
||
|
if index < 0 || index >= m.size {
|
||
|
panic(fmt.Errorf("index out of range:%v", index))
|
||
|
}
|
||
|
|
||
|
if m.set&(1<<uint(index)) != 0 {
|
||
|
return 1
|
||
|
}
|
||
|
return 0
|
||
|
}
|
||
|
|
||
|
func (m *bitmap) Set(index int, value bool) {
|
||
|
if index < 0 || index >= m.size {
|
||
|
panic(fmt.Errorf("index out of range:%v", index))
|
||
|
}
|
||
|
if value {
|
||
|
m.set |= 1 << uint(index)
|
||
|
} else {
|
||
|
m.set &^= 1 << uint(index)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (m *bitmap) Clear() {
|
||
|
m.set = 0
|
||
|
}
|
||
|
|
||
|
func (m *bitmap) toInt64() int64 {
|
||
|
return m.set
|
||
|
}
|
||
|
|
||
|
func (m *bitmap) GetSize() int {
|
||
|
return m.size
|
||
|
}
|