From ff34efba753010daa9cc28179d5b505f7e6680eb Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Sun, 19 Apr 2015 13:59:56 -0700 Subject: [PATCH] re-vendored hamming lib with license --- Godeps/Godeps.json | 4 +- .../steakknife/hamming/MIT-LICENSE.txt | 8 + .../github.com/steakknife/hamming/README.md | 45 ++++- .../github.com/steakknife/hamming/hamming.go | 59 ++++++ .../steakknife/hamming/hamming_test.go | 175 ++++++++++++------ 5 files changed, 227 insertions(+), 64 deletions(-) create mode 100644 Godeps/_workspace/src/github.com/steakknife/hamming/MIT-LICENSE.txt diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 3d3b55631..b8b28109e 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -219,8 +219,8 @@ }, { "ImportPath": "github.com/steakknife/hamming", - "Comment": "0.0.2-2-g9ad4a62", - "Rev": "9ad4a620e3d573267a083c892f2b42a39302153b" + "Comment": "0.0.10", + "Rev": "8bad99011016569c05320e51be39c648679c5b73" }, { "ImportPath": "github.com/syndtr/goleveldb/leveldb", diff --git a/Godeps/_workspace/src/github.com/steakknife/hamming/MIT-LICENSE.txt b/Godeps/_workspace/src/github.com/steakknife/hamming/MIT-LICENSE.txt new file mode 100644 index 000000000..ccf77fe46 --- /dev/null +++ b/Godeps/_workspace/src/github.com/steakknife/hamming/MIT-LICENSE.txt @@ -0,0 +1,8 @@ +The MIT License (MIT) +Copyright © 2014, 2015 Barry Allard + +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: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +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. diff --git a/Godeps/_workspace/src/github.com/steakknife/hamming/README.md b/Godeps/_workspace/src/github.com/steakknife/hamming/README.md index 21ba7a929..73ad304c9 100644 --- a/Godeps/_workspace/src/github.com/steakknife/hamming/README.md +++ b/Godeps/_workspace/src/github.com/steakknife/hamming/README.md @@ -1,3 +1,44 @@ -Copyright (c) 2014 Barry Allard +# hamming distance calculations in Go -MIT license +Copyright © 2014, 2015 Barry Allard + +[MIT license](MIT-LICENSE.txt) + +## Usage + +```go +import 'github.com/steakknife/hamming' + +// ... + +// hamming distance between values +hamming.Byte(0xFF, 0x00) // 8 +hamming.Byte(0x00, 0x00) // 0 + +// just count bits in a byte +hamming.CountBitsByte(0xA5), // 4 +``` + +See help in the [docs](https://godoc.org/github.com/steakknife/hamming) + +## Get + + go get -u github.com/steakknife/hamming # master is always stable + +## Source + +- On the web: https://github.com/steakknife/hamming + +- Git: `git clone https://github.com/steakknife/hamming` + +## Contact + +- [Feedback](mailto:barry.allard@gmail.com) + +- [Issues](https://github.com/steakknife/hamming/issues) + +## License + +[MIT license](MIT-LICENSE.txt) + +Copyright © 2014, 2015 Barry Allard diff --git a/Godeps/_workspace/src/github.com/steakknife/hamming/hamming.go b/Godeps/_workspace/src/github.com/steakknife/hamming/hamming.go index ae23987f1..311fcd9c5 100644 --- a/Godeps/_workspace/src/github.com/steakknife/hamming/hamming.go +++ b/Godeps/_workspace/src/github.com/steakknife/hamming/hamming.go @@ -1,3 +1,28 @@ +// +// hamming distance calculations in Go +// +// https://github.com/steakknife/hamming +// +// Copyright © 2014, 2015 Barry Allard +// +// MIT license +// +// +// Usage +// +// 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. +// +// import 'github.com/steakknife/hamming' +// +// // ... +// +// // hamming distance between values +// hamming.Byte(0xFF, 0x00) // 8 +// hamming.Byte(0x00, 0x00) // 0 +// +// // just count bits in a byte +// hamming.CountBitsByte(0xA5), // 4 +// package hamming // SSE4.x PopCnt is 10x slower @@ -21,11 +46,29 @@ func Uint64(x, y uint64) int { return CountBitsUint64(x ^ y) } +// 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) +func Uint64s(b0, b1 []uint64) int { + d := 0 + for i, x := range b0 { + d += Uint64(x, b1[i]) + } + return d +} + // hamming distance of two bytes func Byte(x, y byte) int { return CountBitsByte(x ^ y) } +// 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) +func Bytes(b0, b1 []byte) int { + d := 0 + for i, x := range b0 { + d += Byte(x, b1[i]) + } + return d +} + func CountBitsUint64(x uint64) int { x -= (x >> 1) & m1 // put count of each 2 bits into those 2 bits 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 { return int((x * h01) >> 56) // returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ... } +func CountBitsUint64s(b []uint64) int { + c := 0 + for _, x := range b { + c += CountBitsUint64(x) + } + return c +} + func CountBitsByte(x byte) int { return int(table[x]) } + +func CountBitsBytes(b []byte) int { + c := 0 + for _, x := range b { + c += CountBitsByte(x) + } + return c +} diff --git a/Godeps/_workspace/src/github.com/steakknife/hamming/hamming_test.go b/Godeps/_workspace/src/github.com/steakknife/hamming/hamming_test.go index 230596255..44723d2d6 100644 --- a/Godeps/_workspace/src/github.com/steakknife/hamming/hamming_test.go +++ b/Godeps/_workspace/src/github.com/steakknife/hamming/hamming_test.go @@ -1,88 +1,143 @@ +// +// hamming distance calculations in Go +// +// https://github.com/steakknife/hamming +// +// Copyright © 2014, 2015 Barry Allard +// +// MIT license +// package hamming import ( - "testing" + "testing" ) type testCountBitsUint64Case struct { - x uint64 - n int + x uint64 + n int } type testCountBitsByteCase struct { - x byte - n int + x byte + n int +} + +type testBytesCase struct { + b0, b1 []byte + n int +} + +type testUint64sCase struct { + b0, b1 []uint64 + n int } var testCountBitsByteCases = []testCountBitsByteCase{ - {0x00, 0}, - {0x01, 1}, - {0x02, 1}, - {0x03, 2}, - {0xaa, 4}, - {0x55, 4}, - {0x7f, 7}, - {0xff, 8}, + {0x00, 0}, + {0x01, 1}, + {0x02, 1}, + {0x03, 2}, + {0xaa, 4}, + {0x55, 4}, + {0x7f, 7}, + {0xff, 8}, } var testCountBitsUint64Cases = []testCountBitsUint64Case{ - {0x00, 0}, - {0x01, 1}, - {0x02, 1}, - {0x03, 2}, - {0xaa, 4}, - {0x55, 4}, - {0x7f, 7}, - {0xff, 8}, - {0xffff, 16}, - {0xffffffff, 32}, - {0x1ffffffff, 33}, - {0x3ffffffff, 34}, - {0x7ffffffff, 35}, - {0xfffffffff, 36}, - {0x3fffffffffffffff, 62}, - {0x7fffffffffffffff, 63}, - {0xffffffffffffffff, 64}, + {0x00, 0}, + {0x01, 1}, + {0x02, 1}, + {0x03, 2}, + {0xaa, 4}, + {0x55, 4}, + {0x7f, 7}, + {0xff, 8}, + {0xffff, 16}, + {0xffffffff, 32}, + {0x1ffffffff, 33}, + {0x3ffffffff, 34}, + {0x7ffffffff, 35}, + {0xfffffffff, 36}, + {0x3fffffffffffffff, 62}, + {0x7fffffffffffffff, 63}, + {0xffffffffffffffff, 64}, +} + +var testBytesCases = []testBytesCase{ + {[]byte{}, []byte{}, 0}, + {[]byte{1}, []byte{0}, 1}, + {[]byte{1}, []byte{2}, 2}, + {[]byte{1, 0}, []byte{0, 1}, 2}, + {[]byte{1, 0}, []byte{0, 1}, 2}, +} + +var testUint64sCases = []testUint64sCase{ + {[]uint64{}, []uint64{}, 0}, + {[]uint64{1}, []uint64{0}, 1}, + {[]uint64{1}, []uint64{2}, 2}, + {[]uint64{1, 0}, []uint64{0, 1}, 2}, + {[]uint64{1, 0}, []uint64{0, 1}, 2}, } func TestCountBitByte(t *testing.T) { - for _, c := range testCountBitsByteCases { - if actualN := CountBitsByte(c.x); actualN != c.n { - t.Fatal("CountBitsByte(", c.x, ") = ", actualN, " != ", c.n) - } else { - t.Log("CountBitsByte(", c.x, ") == ", c.n) - } - } + for _, c := range testCountBitsByteCases { + if actualN := CountBitsByte(c.x); actualN != c.n { + t.Fatal("CountBitsByte(", c.x, ") = ", actualN, " != ", c.n) + } else { + t.Log("CountBitsByte(", c.x, ") == ", c.n) + } + } +} + +func TestBytes(t *testing.T) { + for _, c := range testBytesCases { + if actualN := Bytes(c.b0, c.b1); actualN != c.n { + t.Fatal("Bytes(", c.b0, ",", c.b1, ") = ", actualN, " != ", c.n) + } else { + t.Log("Bytes(", c.b0, ",", c.b1, ") == ", c.n) + } + } +} + +func TestUint64s(t *testing.T) { + for _, c := range testUint64sCases { + if actualN := Uint64s(c.b0, c.b1); actualN != c.n { + t.Fatal("Uint64s(", c.b0, ",", c.b1, ") = ", actualN, " != ", c.n) + } else { + t.Log("Uint64s(", c.b0, ",", c.b1, ") == ", c.n) + } + } } func TestCountBitUint64(t *testing.T) { - for _, c := range testCountBitsUint64Cases { - if actualN := CountBitsUint64(c.x); actualN != c.n { - t.Fatal("CountBitsUint64(", c.x, ") = ", actualN, " != ", c.n) - } else { - t.Log("CountBitsUint64(", c.x, ") == ", c.n) - } - } + for _, c := range testCountBitsUint64Cases { + if actualN := CountBitsUint64(c.x); actualN != c.n { + t.Fatal("CountBitsUint64(", c.x, ") = ", actualN, " != ", c.n) + } else { + t.Log("CountBitsUint64(", c.x, ") == ", c.n) + } + } } func BenchmarkCountBitsUint64(b *testing.B) { - j := 0 - for i := 0; i < b.N; i++ { - CountBitsUint64(testCountBitsUint64Cases[j].x) - j++ - if j == len(testCountBitsUint64Cases) { - j = 0 - } - } + j := 0 + for i := 0; i < b.N; i++ { + CountBitsUint64(testCountBitsUint64Cases[j].x) + j++ + if j == len(testCountBitsUint64Cases) { + j = 0 + } + } } func BenchmarkCountBitsByte(b *testing.B) { - j := 0 - for i := 0; i < b.N; i++ { - CountBitsByte(testCountBitsByteCases[j].x) - j++ - if j == len(testCountBitsByteCases) { - j = 0 - } - } + j := 0 + for i := 0; i < b.N; i++ { + CountBitsByte(testCountBitsByteCases[j].x) + j++ + if j == len(testCountBitsByteCases) { + j = 0 + } + } }