-
Notifications
You must be signed in to change notification settings - Fork 3
/
hasher.go
54 lines (46 loc) · 1.05 KB
/
hasher.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
44
45
46
47
48
49
50
51
52
53
54
package frozen
import (
"fmt"
"strings"
"unsafe"
"github.com/arr-ai/hash"
)
const (
hashBits = 8 * int(unsafe.Sizeof(uintptr(0)))
hashBitsOffset = hashBits - nodeBits
)
type hasher uintptr
func newHasher(key interface{}, depth int) hasher {
return hasher(hash.Interface(key, 0)) << uint(depth*nodeBits)
}
func (h hasher) next() hasher {
return h << nodeBits
}
func (h hasher) hash() int {
return int(h >> uint(hashBitsOffset))
}
func (h hasher) String() string {
const dregs = hashBits % nodeBits
var s string
switch nodeBits {
case 2:
// TODO(if we care): Output a base-4 number.
s = fmt.Sprintf("%0*x", hashBits/4, h>>uint(dregs))
case 3:
var sb strings.Builder
sb.WriteByte('#')
// Braille-encode octal digits in pairs.
for ; h != 0; h <<= 6 {
sb.WriteRune(rune(0x2800 + h.hash() + h.next().hash()<<3))
}
return sb.String()
case 4:
return "#" + brailleEncoded(uint64(h))
default:
panic("not implemented")
}
if dregs != 0 {
s += fmt.Sprintf("%d", h<<uint(nodeBits-dregs)%nodeCount)
}
return strings.TrimRight(s, "0")
}