re-vendored hamming lib with license
Juan Batiz-Benet committed
Apr 19, 2015 at 13:59 UTC
ff34efba753010daa9cc28179d5b505f7e6680eb
5 files changed
+227
-64
Godeps/Godeps.json
+2
-2
@@ -219,8 +219,8 @@
219
},
220
{
221
"ImportPath": "github.com/steakknife/hamming",
222
- "Comment": "0.0.2-2-g9ad4a62",
223
- "Rev": "9ad4a620e3d573267a083c892f2b42a39302153b"
222
+ "Comment": "0.0.10",
223
+ "Rev": "8bad99011016569c05320e51be39c648679c5b73"
224
},
225
{
226
"ImportPath": "github.com/syndtr/goleveldb/leveldb",
Godeps/_workspace/src/github.com/steakknife/hamming/MIT-LICENSE.txt
new
+8
@@ -0,0 +1,8 @@
1
+The MIT License (MIT)
2
+Copyright © 2014, 2015 Barry Allard
3
+
4
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Godeps/_workspace/src/github.com/steakknife/hamming/README.md
+43
-2
@@ -1,3 +1,44 @@
1
-Copyright (c) 2014 Barry Allard
1
+# hamming distance calculations in Go
2
3
-MIT license
3
+Copyright © 2014, 2015 Barry Allard
4
+
5
+[MIT license](MIT-LICENSE.txt)
6
+
7
+## Usage
8
+
9
+```go
10
+import 'github.com/steakknife/hamming'
11
+
12
+// ...
13
+
14
+// hamming distance between values
15
+hamming.Byte(0xFF, 0x00) // 8
16
+hamming.Byte(0x00, 0x00) // 0
17
+
18
+// just count bits in a byte
19
+hamming.CountBitsByte(0xA5), // 4
20
+```
21
+
22
+See help in the [docs](https://godoc.org/github.com/steakknife/hamming)
23
+
24
+## Get
25
+
26
+ go get -u github.com/steakknife/hamming # master is always stable
27
+
28
+## Source
29
+
30
+- On the web: https://github.com/steakknife/hamming
31
+
32
+- Git: `git clone https://github.com/steakknife/hamming`
33
+
34
+## Contact
35
+
36
+- [Feedback](mailto:barry.allard@gmail.com)
37
+
38
+- [Issues](https://github.com/steakknife/hamming/issues)
39
+
40
+## License
41
+
42
+[MIT license](MIT-LICENSE.txt)
43
+
44
+Copyright © 2014, 2015 Barry Allard
Godeps/_workspace/src/github.com/steakknife/hamming/hamming.go
+59
@@ -1,3 +1,28 @@
1
+//
2
+// hamming distance calculations in Go
3
+//
4
+// https://github.com/steakknife/hamming
5
+//
6
+// Copyright © 2014, 2015 Barry Allard
7
+//
8
+// MIT license
9
+//
10
+//
11
+// Usage
12
+//
13
+// The functions are named (CountBits)?(Byte|Uint64)s?. The plural forms are for slices. The CountBits.+ forms are Population Count only, where the bare-type forms are Hamming distance.
14
+//
15
+// import 'github.com/steakknife/hamming'
16
+//
17
+// // ...
18
+//
19
+// // hamming distance between values
20
+// hamming.Byte(0xFF, 0x00) // 8
21
+// hamming.Byte(0x00, 0x00) // 0
22
+//
23
+// // just count bits in a byte
24
+// hamming.CountBitsByte(0xA5), // 4
25
+//
26
package hamming
27
28
// SSE4.x PopCnt is 10x slower
@@ -21,11 +46,29 @@ func Uint64(x, y uint64) int {
46
return CountBitsUint64(x ^ y)
47
}
48
49
+// hamming distance of two uint64 buffers, of which the size of the first argument is used for both (panics if b1 is smaller than b0, does not compare b1 beyond length of b0)
50
+func Uint64s(b0, b1 []uint64) int {
51
+ d := 0
52
+ for i, x := range b0 {
53
+ d += Uint64(x, b1[i])
54
+ }
55
+ return d
56
+}
57
+
58
// hamming distance of two bytes
59
func Byte(x, y byte) int {
60
return CountBitsByte(x ^ y)
61
}
62
63
+// hamming distance of two byte buffers, of which the size of the first argument is used for both (panics if b1 is smaller than b0, does not compare b1 beyond length of b0)
64
+func Bytes(b0, b1 []byte) int {
65
+ d := 0
66
+ for i, x := range b0 {
67
+ d += Byte(x, b1[i])
68
+ }
69
+ return d
70
+}
71
+
72
func CountBitsUint64(x uint64) int {
73
x -= (x >> 1) & m1 // put count of each 2 bits into those 2 bits
74
x = (x & m2) + ((x >> 2) & m2) // put count of each 4 bits into those 4 bits
@@ -33,6 +76,22 @@ func CountBitsUint64(x uint64) int {
76
return int((x * h01) >> 56) // returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ...
77
}
78
79
+func CountBitsUint64s(b []uint64) int {
80
+ c := 0
81
+ for _, x := range b {
82
+ c += CountBitsUint64(x)
83
+ }
84
+ return c
85
+}
86
+
87
func CountBitsByte(x byte) int {
88
return int(table[x])
89
}
90
+
91
+func CountBitsBytes(b []byte) int {
92
+ c := 0
93
+ for _, x := range b {
94
+ c += CountBitsByte(x)
95
+ }
96
+ return c
97
+}
Godeps/_workspace/src/github.com/steakknife/hamming/hamming_test.go
+115
-60
@@ -1,88 +1,143 @@
1
+//
2
+// hamming distance calculations in Go
3
+//
4
+// https://github.com/steakknife/hamming
5
+//
6
+// Copyright © 2014, 2015 Barry Allard
7
+//
8
+// MIT license
9
+//
10
package hamming
11
12
import (
4
- "testing"
13
+ "testing"
14
)
15
16
type testCountBitsUint64Case struct {
8
- x uint64
9
- n int
17
+ x uint64
18
+ n int
19
}
20
21
type testCountBitsByteCase struct {
13
- x byte
14
- n int
22
+ x byte
23
+ n int
24
+}
25
+
26
+type testBytesCase struct {
27
+ b0, b1 []byte
28
+ n int
29
+}
30
+
31
+type testUint64sCase struct {
32
+ b0, b1 []uint64
33
+ n int
34
}
35
36
var testCountBitsByteCases = []testCountBitsByteCase{
18
- {0x00, 0},
19
- {0x01, 1},
20
- {0x02, 1},
21
- {0x03, 2},
22
- {0xaa, 4},
23
- {0x55, 4},
24
- {0x7f, 7},
25
- {0xff, 8},
37
+ {0x00, 0},
38
+ {0x01, 1},
39
+ {0x02, 1},
40
+ {0x03, 2},
41
+ {0xaa, 4},
42
+ {0x55, 4},
43
+ {0x7f, 7},
44
+ {0xff, 8},
45
}
46
47
var testCountBitsUint64Cases = []testCountBitsUint64Case{
29
- {0x00, 0},
30
- {0x01, 1},
31
- {0x02, 1},
32
- {0x03, 2},
33
- {0xaa, 4},
34
- {0x55, 4},
35
- {0x7f, 7},
36
- {0xff, 8},
37
- {0xffff, 16},
38
- {0xffffffff, 32},
39
- {0x1ffffffff, 33},
40
- {0x3ffffffff, 34},
41
- {0x7ffffffff, 35},
42
- {0xfffffffff, 36},
43
- {0x3fffffffffffffff, 62},
44
- {0x7fffffffffffffff, 63},
45
- {0xffffffffffffffff, 64},
48
+ {0x00, 0},
49
+ {0x01, 1},
50
+ {0x02, 1},
51
+ {0x03, 2},
52
+ {0xaa, 4},
53
+ {0x55, 4},
54
+ {0x7f, 7},
55
+ {0xff, 8},
56
+ {0xffff, 16},
57
+ {0xffffffff, 32},
58
+ {0x1ffffffff, 33},
59
+ {0x3ffffffff, 34},
60
+ {0x7ffffffff, 35},
61
+ {0xfffffffff, 36},
62
+ {0x3fffffffffffffff, 62},
63
+ {0x7fffffffffffffff, 63},
64
+ {0xffffffffffffffff, 64},
65
+}
66
+
67
+var testBytesCases = []testBytesCase{
68
+ {[]byte{}, []byte{}, 0},
69
+ {[]byte{1}, []byte{0}, 1},
70
+ {[]byte{1}, []byte{2}, 2},
71
+ {[]byte{1, 0}, []byte{0, 1}, 2},
72
+ {[]byte{1, 0}, []byte{0, 1}, 2},
73
+}
74
+
75
+var testUint64sCases = []testUint64sCase{
76
+ {[]uint64{}, []uint64{}, 0},
77
+ {[]uint64{1}, []uint64{0}, 1},
78
+ {[]uint64{1}, []uint64{2}, 2},
79
+ {[]uint64{1, 0}, []uint64{0, 1}, 2},
80
+ {[]uint64{1, 0}, []uint64{0, 1}, 2},
81
}
82
83
func TestCountBitByte(t *testing.T) {
49
- for _, c := range testCountBitsByteCases {
50
- if actualN := CountBitsByte(c.x); actualN != c.n {
51
- t.Fatal("CountBitsByte(", c.x, ") = ", actualN, " != ", c.n)
52
- } else {
53
- t.Log("CountBitsByte(", c.x, ") == ", c.n)
54
- }
55
- }
84
+ for _, c := range testCountBitsByteCases {
85
+ if actualN := CountBitsByte(c.x); actualN != c.n {
86
+ t.Fatal("CountBitsByte(", c.x, ") = ", actualN, " != ", c.n)
87
+ } else {
88
+ t.Log("CountBitsByte(", c.x, ") == ", c.n)
89
+ }
90
+ }
91
+}
92
+
93
+func TestBytes(t *testing.T) {
94
+ for _, c := range testBytesCases {
95
+ if actualN := Bytes(c.b0, c.b1); actualN != c.n {
96
+ t.Fatal("Bytes(", c.b0, ",", c.b1, ") = ", actualN, " != ", c.n)
97
+ } else {
98
+ t.Log("Bytes(", c.b0, ",", c.b1, ") == ", c.n)
99
+ }
100
+ }
101
+}
102
+
103
+func TestUint64s(t *testing.T) {
104
+ for _, c := range testUint64sCases {
105
+ if actualN := Uint64s(c.b0, c.b1); actualN != c.n {
106
+ t.Fatal("Uint64s(", c.b0, ",", c.b1, ") = ", actualN, " != ", c.n)
107
+ } else {
108
+ t.Log("Uint64s(", c.b0, ",", c.b1, ") == ", c.n)
109
+ }
110
+ }
111
}
112
113
func TestCountBitUint64(t *testing.T) {
59
- for _, c := range testCountBitsUint64Cases {
60
- if actualN := CountBitsUint64(c.x); actualN != c.n {
61
- t.Fatal("CountBitsUint64(", c.x, ") = ", actualN, " != ", c.n)
62
- } else {
63
- t.Log("CountBitsUint64(", c.x, ") == ", c.n)
64
- }
65
- }
114
+ for _, c := range testCountBitsUint64Cases {
115
+ if actualN := CountBitsUint64(c.x); actualN != c.n {
116
+ t.Fatal("CountBitsUint64(", c.x, ") = ", actualN, " != ", c.n)
117
+ } else {
118
+ t.Log("CountBitsUint64(", c.x, ") == ", c.n)
119
+ }
120
+ }
121
}
122
123
func BenchmarkCountBitsUint64(b *testing.B) {
69
- j := 0
70
- for i := 0; i < b.N; i++ {
71
- CountBitsUint64(testCountBitsUint64Cases[j].x)
72
- j++
73
- if j == len(testCountBitsUint64Cases) {
74
- j = 0
75
- }
76
- }
124
+ j := 0
125
+ for i := 0; i < b.N; i++ {
126
+ CountBitsUint64(testCountBitsUint64Cases[j].x)
127
+ j++
128
+ if j == len(testCountBitsUint64Cases) {
129
+ j = 0
130
+ }
131
+ }
132
}
133
134
func BenchmarkCountBitsByte(b *testing.B) {
80
- j := 0
81
- for i := 0; i < b.N; i++ {
82
- CountBitsByte(testCountBitsByteCases[j].x)
83
- j++
84
- if j == len(testCountBitsByteCases) {
85
- j = 0
86
- }
87
- }
135
+ j := 0
136
+ for i := 0; i < b.N; i++ {
137
+ CountBitsByte(testCountBitsByteCases[j].x)
138
+ j++
139
+ if j == len(testCountBitsByteCases) {
140
+ j = 0
141
+ }
142
+ }
143
}