Skip to content

Commit b3382b5

Browse files
committed
move bitmap to bitarray package
1 parent 53cb334 commit b3382b5

File tree

3 files changed

+134
-67
lines changed

3 files changed

+134
-67
lines changed

bitmap/bitmap.go renamed to bitarray/bitmap.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
1+
/*
2+
Copyright (c) 2016, Theodore Butler
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
* Redistributions of source code must retain the above copyright notice, this
9+
list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation
13+
and/or other materials provided with the distribution.
14+
15+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
127
// Package bitmap contains bitmaps of length 32 and 64 for tracking bool
228
// values without the need for arrays or hashing.
3-
package bitmap
29+
package bitarray
430

531
// Bitmap32 tracks 32 bool values within a uint32
632
type Bitmap32 uint32
@@ -20,7 +46,7 @@ func (b Bitmap32) HasBit(pos uint) bool {
2046
return (b & (1 << pos)) != 0
2147
}
2248

23-
// PopCount returns the ammount of bits set to 1 in the Bitmap32
49+
// PopCount returns the amount of bits set to 1 in the Bitmap32
2450
func (b Bitmap32) PopCount() int {
2551
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2652
b -= (b >> 1) & 0x55555555
@@ -49,7 +75,7 @@ func (b Bitmap64) HasBit(pos uint) bool {
4975
return (b & (1 << pos)) != 0
5076
}
5177

52-
// PopCount returns the ammount of bits set to 1 in the Bitmap64
78+
// PopCount returns the amount of bits set to 1 in the Bitmap64
5379
func (b Bitmap64) PopCount() int {
5480
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
5581
b -= (b >> 1) & 0x5555555555555555

bitarray/bitmap_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
Copyright (c) 2016, Theodore Butler
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
* Redistributions of source code must retain the above copyright notice, this
9+
list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation
13+
and/or other materials provided with the distribution.
14+
15+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
27+
package bitarray
28+
29+
import (
30+
"testing"
31+
32+
"github.com/stretchr/testify/assert"
33+
)
34+
35+
func TestBitmap32_PopCount(t *testing.T) {
36+
b := []uint32{
37+
uint32(0x55555555), // 0x55555555 = 01010101 01010101 01010101 01010101
38+
uint32(0x33333333), // 0x33333333 = 00110011 00110011 00110011 00110011
39+
uint32(0x0F0F0F0F), // 0x0F0F0F0F = 00001111 00001111 00001111 00001111
40+
uint32(0x00FF00FF), // 0x00FF00FF = 00000000 11111111 00000000 11111111
41+
uint32(0x0000FFFF), // 0x0000FFFF = 00000000 00000000 11111111 11111111
42+
}
43+
for _, x := range b {
44+
assert.Equal(t, 16, Bitmap32(x).PopCount())
45+
}
46+
}
47+
48+
func TestBitmap64_PopCount(t *testing.T) {
49+
b := []uint64{
50+
uint64(0x5555555555555555),
51+
uint64(0x3333333333333333),
52+
uint64(0x0F0F0F0F0F0F0F0F),
53+
uint64(0x00FF00FF00FF00FF),
54+
uint64(0x0000FFFF0000FFFF),
55+
}
56+
for _, x := range b {
57+
assert.Equal(t, 32, Bitmap64(x).PopCount())
58+
}
59+
}
60+
61+
func TestBitmap32_SetBit(t *testing.T) {
62+
m := Bitmap32(0)
63+
assert.Equal(t, Bitmap32(0x4), m.SetBit(2))
64+
}
65+
66+
func TestBitmap32_ClearBit(t *testing.T) {
67+
m := Bitmap32(0x4)
68+
assert.Equal(t, Bitmap32(0), m.ClearBit(2))
69+
}
70+
71+
func TestBitmap32_HasBit(t *testing.T) {
72+
m := Bitmap32(0x55555555)
73+
assert.Equal(t, true, m.HasBit(2))
74+
}
75+
76+
func TestBitmap64_SetBit(t *testing.T) {
77+
m := Bitmap64(0)
78+
assert.Equal(t, Bitmap64(0x4), m.SetBit(2))
79+
}
80+
81+
func TestBitmap64_ClearBit(t *testing.T) {
82+
m := Bitmap64(0x4)
83+
assert.Equal(t, Bitmap64(0), m.ClearBit(2))
84+
}
85+
86+
func TestBitmap64_HasBit(t *testing.T) {
87+
m := Bitmap64(0x55555555)
88+
assert.Equal(t, true, m.HasBit(2))
89+
}
90+
91+
func BenchmarkBitmap32_PopCount(b *testing.B) {
92+
m := Bitmap32(0x33333333)
93+
b.ResetTimer()
94+
for i := b.N; i > 0; i-- {
95+
m.PopCount()
96+
}
97+
}
98+
99+
func BenchmarkBitmap64_PopCount(b *testing.B) {
100+
m := Bitmap64(0x3333333333333333)
101+
b.ResetTimer()
102+
for i := b.N; i > 0; i-- {
103+
m.PopCount()
104+
}
105+
}

bitmap/bitmap_test.go

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)