0
0
Fork 0

simplify prefix coding

based on discussion here:
https://github.com/blevesearch/blevesearch.github.io-hugo/pull/2
This commit is contained in:
Marty Schoch 2015-10-12 14:53:17 -07:00
parent 9b16fa6528
commit f43fa4294a
2 changed files with 68 additions and 52 deletions

View File

@ -24,7 +24,7 @@ func NewPrefixCodedInt64(in int64, shift uint) (PrefixCoded, error) {
return nil, fmt.Errorf("cannot shift %d, must be between 0 and 63", shift)
}
nChars := (((63 - shift) * 37) >> 8) + 1
nChars := ((63 - shift) / 7) + 1
rv := make(PrefixCoded, nChars+1)
rv[0] = ShiftStartInt64 + byte(shift)

View File

@ -14,60 +14,61 @@ import (
"testing"
)
var tests = []struct {
input int64
shift uint
output PrefixCoded
}{
{
input: 1,
shift: 0,
output: PrefixCoded{0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
},
{
input: -1,
shift: 0,
output: PrefixCoded{0x20, 0x0, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f},
},
{
input: -94582,
shift: 0,
output: PrefixCoded{0x20, 0x0, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7a, 0x1d, 0xa},
},
{
input: 314729851,
shift: 0,
output: PrefixCoded{0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x1, 0x16, 0x9, 0x4a, 0x7b},
},
{
input: 314729851,
shift: 4,
output: PrefixCoded{0x24, 0x8, 0x0, 0x0, 0x0, 0x0, 0x9, 0x30, 0x4c, 0x57},
},
{
input: 314729851,
shift: 8,
output: PrefixCoded{0x28, 0x40, 0x0, 0x0, 0x0, 0x0, 0x4b, 0x4, 0x65},
},
{
input: 314729851,
shift: 16,
output: PrefixCoded{0x30, 0x20, 0x0, 0x0, 0x0, 0x0, 0x25, 0x42},
},
{
input: 314729851,
shift: 32,
output: PrefixCoded{0x40, 0x8, 0x0, 0x0, 0x0, 0x0},
},
{
input: 1234729851,
shift: 32,
output: PrefixCoded{0x40, 0x8, 0x0, 0x0, 0x0, 0x0},
},
}
// these array encoding values have been verified manually
// against the lucene implementation
func TestPrefixCoded(t *testing.T) {
tests := []struct {
input int64
shift uint
output PrefixCoded
}{
{
input: 1,
shift: 0,
output: PrefixCoded{0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
},
{
input: -1,
shift: 0,
output: PrefixCoded{0x20, 0x0, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f},
},
{
input: -94582,
shift: 0,
output: PrefixCoded{0x20, 0x0, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7a, 0x1d, 0xa},
},
{
input: 314729851,
shift: 0,
output: PrefixCoded{0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x1, 0x16, 0x9, 0x4a, 0x7b},
},
{
input: 314729851,
shift: 4,
output: PrefixCoded{0x24, 0x8, 0x0, 0x0, 0x0, 0x0, 0x9, 0x30, 0x4c, 0x57},
},
{
input: 314729851,
shift: 8,
output: PrefixCoded{0x28, 0x40, 0x0, 0x0, 0x0, 0x0, 0x4b, 0x4, 0x65},
},
{
input: 314729851,
shift: 16,
output: PrefixCoded{0x30, 0x20, 0x0, 0x0, 0x0, 0x0, 0x25, 0x42},
},
{
input: 314729851,
shift: 32,
output: PrefixCoded{0x40, 0x8, 0x0, 0x0, 0x0, 0x0},
},
{
input: 1234729851,
shift: 32,
output: PrefixCoded{0x40, 0x8, 0x0, 0x0, 0x0, 0x0},
},
}
for _, test := range tests {
actual, err := NewPrefixCodedInt64(test.input, test.shift)
@ -96,3 +97,18 @@ func TestPrefixCoded(t *testing.T) {
}
}
}
func BenchmarkTestPrefixCoded(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range tests {
actual, err := NewPrefixCodedInt64(test.input, test.shift)
if err != nil {
b.Error(err)
}
if !reflect.DeepEqual(actual, test.output) {
b.Errorf("expected %#v, got %#v", test.output, actual)
}
}
}
}