Skip to content

Commit d9f402a

Browse files
committed
implement bitmap
1 parent 8f5270c commit d9f402a

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

bitmap/bitmap.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Package bitmap contains bitmaps of length 32 and 64 for tracking bool
2+
// values without the need for arrays or hashing.
3+
package bitmap
4+
5+
// Bitmap32 tracks 32 bool values within a uint32
6+
type Bitmap32 uint32
7+
8+
// SetBit returns a Bitmap32 with the bit at the given position set to 1
9+
func (b Bitmap32) SetBit(pos uint) Bitmap32 {
10+
return b | (1 << pos)
11+
}
12+
13+
// ClearBit returns a Bitmap32 with the bit at the given position set to 0
14+
func (b Bitmap32) ClearBit(pos uint) Bitmap32 {
15+
return b & ^(1 << pos)
16+
}
17+
18+
// HasBit returns true if the bit at the given position in the Bitmap32 is 1
19+
func (b Bitmap32) HasBit(pos uint) bool {
20+
return (b & (1 << pos)) != 0
21+
}
22+
23+
// PopCount returns the ammount of bits set to 1 in the Bitmap32
24+
func (b Bitmap32) PopCount() int {
25+
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
26+
b -= (b >> 1) & 0x55555555
27+
b = (b>>2)&0x33333333 + b&0x33333333
28+
b += b >> 4
29+
b &= 0x0f0f0f0f
30+
b *= 0x01010101
31+
return int(byte(b >> 24))
32+
}
33+
34+
// Bitmap64 tracks 64 bool values within a uint64
35+
type Bitmap64 uint64
36+
37+
// SetBit returns a Bitmap64 with the bit at the given position set to 1
38+
func (b Bitmap64) SetBit(pos uint) Bitmap64 {
39+
return b | (1 << pos)
40+
}
41+
42+
// ClearBit returns a Bitmap64 with the bit at the given position set to 0
43+
func (b Bitmap64) ClearBit(pos uint) Bitmap64 {
44+
return b & ^(1 << pos)
45+
}
46+
47+
// HasBit returns true if the bit at the given position in the Bitmap64 is 1
48+
func (b Bitmap64) HasBit(pos uint) bool {
49+
return (b & (1 << pos)) != 0
50+
}
51+
52+
// PopCount returns the ammount of bits set to 1 in the Bitmap64
53+
func (b Bitmap64) PopCount() int {
54+
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
55+
b -= (b >> 1) & 0x5555555555555555
56+
b = (b>>2)&0x3333333333333333 + b&0x3333333333333333
57+
b += b >> 4
58+
b &= 0x0f0f0f0f0f0f0f0f
59+
b *= 0x0101010101010101
60+
return int(byte(b >> 56))
61+
}

bitmap/bitmap_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package bitmap
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestPopCount32(t *testing.T) {
10+
b := []uint32{
11+
uint32(0x55555555), // 0x55555555 = 01010101 01010101 01010101 01010101
12+
uint32(0x33333333), // 0x33333333 = 00110011 00110011 00110011 00110011
13+
uint32(0x0F0F0F0F), // 0x0F0F0F0F = 00001111 00001111 00001111 00001111
14+
uint32(0x00FF00FF), // 0x00FF00FF = 00000000 11111111 00000000 11111111
15+
uint32(0x0000FFFF), // 0x0000FFFF = 00000000 00000000 11111111 11111111
16+
}
17+
for _, x := range b {
18+
assert.Equal(t, 16, Bitmap32(x).PopCount())
19+
}
20+
}
21+
22+
func TestPopCount64(t *testing.T) {
23+
b := []uint64{
24+
uint64(0x5555555555555555),
25+
uint64(0x3333333333333333),
26+
uint64(0x0F0F0F0F0F0F0F0F),
27+
uint64(0x00FF00FF00FF00FF),
28+
uint64(0x0000FFFF0000FFFF),
29+
}
30+
for _, x := range b {
31+
assert.Equal(t, 32, Bitmap64(x).PopCount())
32+
}
33+
}
34+
35+
func TestSetBit(t *testing.T) {
36+
m := Bitmap32(0)
37+
assert.Equal(t, Bitmap32(0x4), m.SetBit(2))
38+
}
39+
40+
func TestClearBit(t *testing.T) {
41+
m := Bitmap32(0x4)
42+
assert.Equal(t, Bitmap32(0), m.ClearBit(2))
43+
}
44+
45+
func TestHasBit(t *testing.T) {
46+
m := Bitmap32(0x55555555)
47+
assert.Equal(t, true, m.HasBit(2))
48+
}
49+
50+
func BenchmarkPopCount32(b *testing.B) {
51+
m := Bitmap32(0x33333333)
52+
b.ResetTimer()
53+
for i := b.N; i > 0; i-- {
54+
m.PopCount()
55+
}
56+
}
57+
58+
func BenchmarkPopCount64(b *testing.B) {
59+
m := Bitmap64(0x3333333333333333)
60+
b.ResetTimer()
61+
for i := b.N; i > 0; i-- {
62+
m.PopCount()
63+
}
64+
}

0 commit comments

Comments
 (0)