Skip to content

Commit 23fac1d

Browse files
committed
Revert "bitmap"
1 parent 10afeb4 commit 23fac1d

File tree

9 files changed

+172
-201
lines changed

9 files changed

+172
-201
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
*.out
2-
*.test
3-
.idea
2+
*.test

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ Bitarray used to detect existence without having to resort to hashing with
2121
hashmaps. Requires entities have a uint64 unique identifier. Two
2222
implementations exist, regular and sparse. Sparse saves a great deal of space
2323
but insertions are O(log n). There are some useful functions on the BitArray
24-
interface to detect intersection between two bitarrays. This package also
25-
includes Bitmaps of length 32 and 64 that provide increased speed and O(1) on
26-
all operations.
24+
interface to detect intersection between two bitarrays.
25+
26+
#### Bitmap
27+
28+
an extremely quick way of representing bitarrays of length 32 or 64 within
29+
a uint32 and uint64 respectively. This method provides bit switching and
30+
popcount operations of O(1).
2731

2832
#### Futures
2933

bitarray/bitmap_test.go

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

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

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,6 @@
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
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
284

295
// Bitmap32 tracks 32 bool values within a uint32
306
type Bitmap32 uint32
@@ -44,7 +20,7 @@ func (b Bitmap32) HasBit(pos uint) bool {
4420
return (b & (1 << pos)) != 0
4521
}
4622

47-
// PopCount returns the amount of bits set to 1 in the Bitmap32
23+
// PopCount returns the ammount of bits set to 1 in the Bitmap32
4824
func (b Bitmap32) PopCount() int {
4925
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
5026
b -= (b >> 1) & 0x55555555
@@ -73,7 +49,7 @@ func (b Bitmap64) HasBit(pos uint) bool {
7349
return (b & (1 << pos)) != 0
7450
}
7551

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

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+
}

trie/dtrie/dtrie.go

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,37 +32,13 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3232
// Jurgen J. Vinju
3333
package dtrie
3434

35-
import "fmt"
36-
3735
// Dtrie is a persistent hash trie that dynamically expands or shrinks
3836
// to provide efficient memory allocation.
3937
type Dtrie struct {
4038
root *node
4139
hasher func(v interface{}) uint32
4240
}
4341

44-
type entry struct {
45-
hash uint32
46-
key interface{}
47-
value interface{}
48-
}
49-
50-
func (e *entry) KeyHash() uint32 {
51-
return e.hash
52-
}
53-
54-
func (e *entry) Key() interface{} {
55-
return e.key
56-
}
57-
58-
func (e *entry) Value() interface{} {
59-
return e.value
60-
}
61-
62-
func (e *entry) String() string {
63-
return fmt.Sprintf("%v:%v", e.key, e.value)
64-
}
65-
6642
// New creates an empty DTrie with the given hashing function.
6743
// If nil is passed in, the default hashing function will be used.
6844
func New(hasher func(v interface{}) uint32) *Dtrie {
@@ -91,8 +67,8 @@ func (d *Dtrie) Get(key interface{}) Entry {
9167

9268
// Insert adds an entry to the Dtrie, replacing the existing value if
9369
// the key already exists and returns the resulting Dtrie.
94-
func (d *Dtrie) Insert(key, value interface{}) *Dtrie {
95-
root := insert(d.root, &entry{d.hasher(key), key, value})
70+
func (d *Dtrie) Insert(entry Entry) *Dtrie {
71+
root := insert(d.root, entry)
9672
return &Dtrie{root, d.hasher}
9773
}
9874

trie/dtrie/dtrie_test.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,54 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727
package dtrie
2828

2929
import (
30+
"fmt"
3031
"testing"
3132

3233
"github.com/stretchr/testify/assert"
3334
)
3435

36+
func TestPopCount(t *testing.T) {
37+
b := []uint32{
38+
uint32(0x55555555), // 0x55555555 = 01010101 01010101 01010101 01010101
39+
uint32(0x33333333), // 0x33333333 = 00110011 00110011 00110011 00110011
40+
uint32(0x0F0F0F0F), // 0x0F0F0F0F = 00001111 00001111 00001111 00001111
41+
uint32(0x00FF00FF), // 0x00FF00FF = 00000000 11111111 00000000 11111111
42+
uint32(0x0000FFFF), // 0x0000FFFF = 00000000 00000000 11111111 11111111
43+
}
44+
for _, x := range b {
45+
assert.Equal(t, 16, popCount(x))
46+
}
47+
}
48+
3549
func TestDefaultHasher(t *testing.T) {
3650
assert.Equal(t,
3751
defaultHasher(map[int]string{11234: "foo"}),
3852
defaultHasher(map[int]string{11234: "foo"}))
3953
assert.NotEqual(t, defaultHasher("foo"), defaultHasher("bar"))
4054
}
4155

56+
type testEntry struct {
57+
hash uint32
58+
key int
59+
value int
60+
}
61+
62+
func (e *testEntry) KeyHash() uint32 {
63+
return e.hash
64+
}
65+
66+
func (e *testEntry) Key() interface{} {
67+
return e.key
68+
}
69+
70+
func (e *testEntry) Value() interface{} {
71+
return e.value
72+
}
73+
74+
func (e *testEntry) String() string {
75+
return fmt.Sprint(e.value)
76+
}
77+
4278
func collisionHash(key interface{}) uint32 {
4379
return uint32(0xffffffff) // for testing collisions
4480
}
@@ -51,7 +87,7 @@ func TestInsert(t *testing.T) {
5187
func insertTest(t *testing.T, hashfunc func(interface{}) uint32, count int) *node {
5288
n := emptyNode(0, 32)
5389
for i := 0; i < count; i++ {
54-
n = insert(n, &entry{hashfunc(i), i, i})
90+
n = insert(n, &testEntry{hashfunc(i), i, i})
5591
}
5692
return n
5793
}
@@ -94,7 +130,7 @@ func TestUpdate(t *testing.T) {
94130
func updateTest(t *testing.T, hashfunc func(interface{}) uint32, count int) {
95131
n := insertTest(t, hashfunc, count)
96132
for i := 0; i < count; i++ {
97-
n = insert(n, &entry{hashfunc(i), i, -i})
133+
n = insert(n, &testEntry{hashfunc(i), i, -i})
98134
}
99135
}
100136

@@ -138,7 +174,7 @@ func BenchmarkInsert(b *testing.B) {
138174
n := emptyNode(0, 32)
139175
b.ResetTimer()
140176
for i := b.N; i > 0; i-- {
141-
n = insert(n, &entry{defaultHasher(i), i, i})
177+
n = insert(n, &testEntry{defaultHasher(i), i, i})
142178
}
143179
}
144180

@@ -165,6 +201,6 @@ func BenchmarkUpdate(b *testing.B) {
165201
n := insertTest(nil, defaultHasher, b.N)
166202
b.ResetTimer()
167203
for i := b.N; i > 0; i-- {
168-
n = insert(n, &entry{defaultHasher(i), i, -i})
204+
n = insert(n, &testEntry{defaultHasher(i), i, -i})
169205
}
170206
}

0 commit comments

Comments
 (0)