File: murmur64.go

package info (click to toggle)
golang-github-twmb-murmur3 1.1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 136 kB
  • sloc: cpp: 214; asm: 178; makefile: 2
file content (70 lines) | stat: -rw-r--r-- 1,780 bytes parent folder | download
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package murmur3

import (
	"hash"
)

// Make sure interfaces are correctly implemented.
var (
	_ hash.Hash   = new(digest64)
	_ hash.Hash64 = new(digest64)
	_ bmixer      = new(digest64)
)

// digest64 is half a digest128.
type digest64 digest128

// SeedNew64 returns a hash.Hash64 for streaming 64 bit sums. As the canonical
// implementation does not support Sum64, this uses SeedNew128(seed, seed)
func SeedNew64(seed uint64) hash.Hash64 {
	return (*digest64)(SeedNew128(seed, seed).(*digest128))
}

// New64 returns a hash.Hash64 for streaming 64 bit sums.
func New64() hash.Hash64 {
	return SeedNew64(0)
}

func (d *digest64) Sum(b []byte) []byte {
	h1 := d.Sum64()
	return append(b,
		byte(h1>>56), byte(h1>>48), byte(h1>>40), byte(h1>>32),
		byte(h1>>24), byte(h1>>16), byte(h1>>8), byte(h1))
}

func (d *digest64) Sum64() uint64 {
	h1, _ := (*digest128)(d).Sum128()
	return h1
}

// Sum64 returns the murmur3 sum of data. It is equivalent to the following
// sequence (without the extra burden and the extra allocation):
//     hasher := New64()
//     hasher.Write(data)
//     return hasher.Sum64()
func Sum64(data []byte) uint64 {
	h1, _ := Sum128(data)
	return h1
}

// SeedSum64 returns the murmur3 sum of data with the digest initialized to
// seed.
//
// Because the canonical implementation does not support SeedSum64, this uses
// SeedSum128(seed, seed, data).
func SeedSum64(seed uint64, data []byte) uint64 {
	h1, _ := SeedSum128(seed, seed, data)
	return h1
}

// StringSum64 is the string version of Sum64.
func StringSum64(data string) uint64 {
	h1, _ := StringSum128(data)
	return h1
}

// SeedStringSum64 is the string version of SeedSum64.
func SeedStringSum64(seed uint64, data string) uint64 {
	h1, _ := SeedStringSum128(seed, seed, data)
	return h1
}