From c51304014a21b4ba8882a738d1aceaf8c9aaf049 Mon Sep 17 00:00:00 2001 From: Vitali Fedulov Date: Mon, 20 Dec 2021 00:35:32 +0100 Subject: [PATCH] simplified Decimal --- hashes.go | 21 ++++++--------------- hashes_test.go | 3 +-- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/hashes.go b/hashes.go index 4ceac1f..16b6988 100644 --- a/hashes.go +++ b/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) } diff --git a/hashes_test.go b/hashes_test.go index 1348dd5..655c7cb 100644 --- a/hashes_test.go +++ b/hashes_test.go @@ -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)