-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashes.go
43 lines (38 loc) · 1.06 KB
/
hashes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package hyper
import (
"bytes"
"encoding/gob"
"hash/fnv"
)
// DecimalHash 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 (cube Cube) DecimalHash() (h uint64) {
for _, v := range cube {
h = h*10 + uint64(v)
}
return h
}
// FNV1aHash hashes hypercubes with rare collisions,
// and should be used when Decimal cannot be used
// because of very large number of buckets or dimensions.
func (cube Cube) FNV1aHash() uint64 {
var b bytes.Buffer
gob.NewEncoder(&b).Encode(cube)
hash := fnv.New64a()
hash.Write(b.Bytes())
return hash.Sum64()
}
// HashFunc can be any function (also user-defined).
type HashFunc func(cube Cube) uint64
// Hash64Set returns a set of hashes for a hypercube set
// and a concrete hash function.
func (cubeSet Cubes) HashSet(hashFunc HashFunc) (
hs []uint64) {
for i := 0; i < len(cubeSet); i++ {
hs = append(hs, hashFunc(cubeSet[i]))
}
return hs
}