simplified Decimal
parent
bde7d6305a
commit
c51304014a
21
hashes.go
21
hashes.go
|
@ -6,21 +6,12 @@ import (
|
|||
"hash/fnv"
|
||||
)
|
||||
|
||||
// Decimal hashes hypercubes without collisions. For that
|
||||
// it assumes that number of buckets is 10 or less
|
||||
// and number of dimensions is 19 or less.
|
||||
func Decimal(cube []int, numBuckets int) (h uint64) {
|
||||
if numBuckets > 10 {
|
||||
panic(`Decimal hash can only be used if
|
||||
numBuckets <= 10. FNV1a can be used instead.`)
|
||||
}
|
||||
// Max uint64 equals 18446744073709551615,
|
||||
// therefore larger number of dimensions will overflow.
|
||||
if len(cube) > 19 {
|
||||
panic(`Decimal hash can only be used if
|
||||
number of dimensions is less than 20.
|
||||
FNV1a hash can be used instead.`)
|
||||
}
|
||||
// Decimal hashes hypercubes without collisions. IMPORTANT:
|
||||
// To work correctly, the number of buckets must be
|
||||
// less than 11 and the number of dimensions less than 20.
|
||||
// Else at certain unexpected moment you might get a hash
|
||||
// value overflow.
|
||||
func Decimal(cube []int) (h uint64) {
|
||||
for _, v := range cube {
|
||||
h = h*10 + uint64(v)
|
||||
}
|
||||
|
|
|
@ -6,9 +6,8 @@ import (
|
|||
)
|
||||
|
||||
func TestDecimal(t *testing.T) {
|
||||
numBuckets := 5
|
||||
hypercube := []int{3, 2, 0, 1, 1, 4, 1, 0}
|
||||
hash := Decimal(hypercube, numBuckets)
|
||||
hash := Decimal(hypercube)
|
||||
want := uint64(32011410)
|
||||
if hash != want {
|
||||
t.Errorf(`Got %v, want %v.`, hash, want)
|
||||
|
|
Loading…
Reference in New Issue