Merge pull request #236 from swnb/master

add (bitmap) : add bitmap of golang
This commit is contained in:
wangzheng0822 2019-01-28 19:38:42 +08:00 committed by GitHub
commit c38d3bc61c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 0 deletions

29
go/45_bitmap/bitmap.go Normal file
View File

@ -0,0 +1,29 @@
package bitmap
// BitMap implement bitmap
type BitMap []byte
// New create BitMap
func New(length uint) BitMap {
return make([]byte, length/8+1)
}
// Set set value in bitmap
func (b BitMap) Set(value uint) {
byteIndex := value / 8
if byteIndex >= uint(len(b)) {
return
}
bitIndex := value % 8
[]byte(b)[byteIndex] |= 1 << bitIndex
}
// Get check whether value exist or not
func (b BitMap) Get(value uint) bool {
byteIndex := value / 8
if byteIndex >= uint(len(b)) {
return false
}
bitIndex := value % 8
return []byte(b)[byteIndex]&(1<<bitIndex) != 0
}

View File

@ -0,0 +1,15 @@
package bitmap
import (
"testing"
)
func TestBitMap(t *testing.T) {
bitMap := New(80)
for i := 0; i <= 100; i += 10 {
bitMap.Set(uint(i))
}
for i := 0; i <= 100; i += 10 {
t.Log(bitMap.Get(uint(i)))
}
}