simplified Decimal

master
Vitali Fedulov 2021-12-20 00:35:32 +01:00
parent bde7d6305a
commit c51304014a
2 changed files with 7 additions and 17 deletions

View File

@ -6,21 +6,12 @@ import (
"hash/fnv" "hash/fnv"
) )
// Decimal hashes hypercubes without collisions. For that // Decimal hashes hypercubes without collisions. IMPORTANT:
// it assumes that number of buckets is 10 or less // To work correctly, the number of buckets must be
// and number of dimensions is 19 or less. // less than 11 and the number of dimensions less than 20.
func Decimal(cube []int, numBuckets int) (h uint64) { // Else at certain unexpected moment you might get a hash
if numBuckets > 10 { // value overflow.
panic(`Decimal hash can only be used if func Decimal(cube []int) (h uint64) {
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.`)
}
for _, v := range cube { for _, v := range cube {
h = h*10 + uint64(v) h = h*10 + uint64(v)
} }

View File

@ -6,9 +6,8 @@ import (
) )
func TestDecimal(t *testing.T) { func TestDecimal(t *testing.T) {
numBuckets := 5
hypercube := []int{3, 2, 0, 1, 1, 4, 1, 0} hypercube := []int{3, 2, 0, 1, 1, 4, 1, 0}
hash := Decimal(hypercube, numBuckets) hash := Decimal(hypercube)
want := uint64(32011410) want := uint64(32011410)
if hash != want { if hash != want {
t.Errorf(`Got %v, want %v.`, hash, want) t.Errorf(`Got %v, want %v.`, hash, want)