Debian Codegraph @

external/murmur3/murmur.go

raw
1 // Copyright 2013, Sébastien Paolacci. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 /*
6 Native (and fast) implementation of Austin Appleby's MurmurHash3.
7
8 Package murmur3 implements Austin Appleby's non-cryptographic MurmurHash3.
9
10 Reference implementation:
11 http://code.google.com/p/smhasher/wiki/MurmurHash3
12
13 History, characteristics and (legacy) perfs:
14 https://sites.google.com/site/murmurhash/
15 https://sites.google.com/site/murmurhash/statistics
16 */
17 package murmur3
18
19 type bmixer interface {
20 bmix(p []byte) (tail []byte)
21 Size() (n int)
22 reset()
23 }
24
25 type digest struct {
26 clen int // Digested input cumulative length.
27 tail []byte // 0 to Size()-1 bytes view of `buf'.
28 buf [16]byte // Expected (but not required) to be Size() large.
29 bmixer
30 }
31
32 func (d *digest) BlockSize() int { return 1 }
33
34 func (d *digest) Write(p []byte) (n int, err error) {
35 n = len(p)
36 d.clen += n
37
38 if len(d.tail) > 0 {
39 // Stick back pending bytes.
40 nfree := d.Size() - len(d.tail) // nfree ∈ [1, d.Size()-1].
41 if nfree < len(p) {
42 // One full block can be formed.
43 block := append(d.tail, p[:nfree]...)
44 p = p[nfree:]
45 _ = d.bmix(block) // No tail.
46 } else {
47 // Tail's buf is large enough to prevent reallocs.
48 p = append(d.tail, p...)
49 }
50 }
51
52 d.tail = d.bmix(p)
53
54 // Keep own copy of the 0 to Size()-1 pending bytes.
55 nn := copy(d.buf[:], d.tail)
56 d.tail = d.buf[:nn]
57
58 return n, nil
59 }
60
61 func (d *digest) Reset() {
62 d.clen = 0
63 d.tail = nil
64 d.bmixer.reset()
65 }