Documentation
¶
Overview ¶
Example ¶
package main
import (
"fmt"
"hash"
"log"
"github.com/chmduquesne/rollinghash/v4/buzhash32"
)
func main() {
s := []byte("The quick brown fox jumps over the lazy dog")
classic := hash.Hash32(buzhash32.New())
rolling := buzhash32.New()
// Window len
n := 16
// You MUST load an initial window into the rolling hash before being
// able to roll bytes
if _, err := rolling.Write(s[:n]); err != nil {
log.Fatal(err)
}
// Roll it and compare the result with full re-calculus every time
for i := n; i < len(s); i++ {
// Reset and write the window in classic
classic.Reset()
if _, err := classic.Write(s[i-n+1 : i+1]); err != nil {
log.Fatal(err)
}
// Roll the incoming byte in rolling
rolling.Roll(s[i])
fmt.Printf("%v: checksum %x\n", string(s[i-n+1:i+1]), rolling.Sum32())
// Compare the hashes
if classic.Sum32() != rolling.Sum32() {
log.Fatalf("%v: expected %x, got %x",
s[i-n+1:i+1], classic.Sum32(), rolling.Sum32())
}
}
}
Output: he quick brown f: checksum 53e7e066 e quick brown fo: checksum ecf5708c quick brown fox: checksum c12d0faf quick brown fox : checksum f2e76fe2 uick brown fox j: checksum a8506342 ick brown fox ju: checksum 201db638 ck brown fox jum: checksum 759fe987 k brown fox jump: checksum ecf78a18 brown fox jumps: checksum 9062a9c9 brown fox jumps : checksum 5078232e rown fox jumps o: checksum b1d44d0d own fox jumps ov: checksum 8177e796 wn fox jumps ove: checksum 135d33ca n fox jumps over: checksum 7a45e290 fox jumps over : checksum 1655abcb fox jumps over t: checksum 710c1810 ox jumps over th: checksum bfb01cb9 x jumps over the: checksum 6ed2c594 jumps over the : checksum f2e2c8e7 jumps over the l: checksum df544447 umps over the la: checksum 7df8d3c3 mps over the laz: checksum c8c88cc0 ps over the lazy: checksum 3e7f980c s over the lazy : checksum fb4663b8 over the lazy d: checksum 31ccb20e over the lazy do: checksum c476b45f ver the lazy dog: checksum afb3c2da
Index ¶
- Constants
- func GenerateHashes(seed int64) (res [256]uint32)
- type Buzhash32
- func (d *Buzhash32) BatchBoundaries(a, b []int32, data []byte, window int, mask uint64) (na, nb int)
- func (d *Buzhash32) BatchRoll(dst []uint64, data []byte, window int)
- func (d *Buzhash32) BlockSize() int
- func (d *Buzhash32) Reset()
- func (d *Buzhash32) Roll(c byte)
- func (d *Buzhash32) Size() int
- func (d *Buzhash32) Sum(b []byte) []byte
- func (d *Buzhash32) Sum32() uint32
- func (d *Buzhash32) Write(data []byte) (int, error)
- func (d *Buzhash32) WriteWindow(w io.Writer) (n int, err error)
Examples ¶
Constants ¶
const Size = 4
The size of the checksum.
Variables ¶
This section is empty.
Functions ¶
func GenerateHashes ¶
GenerateHashes generates a list of hashes to use with buzhash
Types ¶
type Buzhash32 ¶
type Buzhash32 struct {
// contains filtered or unexported fields
}
Buzhash32 is a digest which satisfies the rollinghash.Hash32 interface. It implements the cyclic polynomial algorithm https://en.wikipedia.org/wiki/Rolling_hash#Cyclic_polynomial
func New ¶
func New() *Buzhash32
New returns a buzhash based on a list of hashes provided by a call to GenerateHashes, seeded with the default value 1.
func NewFromUint32Array ¶
NewFromUint32Array returns a buzhash based on the provided table uint32 values.
func (*Buzhash32) BatchBoundaries ¶ added in v4.2.0
func (d *Buzhash32) BatchBoundaries(a, b []int32, data []byte, window int, mask uint64) (na, nb int)
BatchBoundaries reports the window positions where the rolling checksum satisfies sum & mask == 0, fusing the test into the hashing loop (see the boundary fast path). It does not modify the receiver.
Boundary hits are accumulated as bits in a uint64 (low 32 = lane A, high 32 = lane B) and extracted with TrailingZeros outside the hot loop, keeping the branch-heavy write-to-slice path out of every iteration. The inner loop is 2x unrolled so table loads for step k+1 can issue while computing step k.
func (*Buzhash32) BatchRoll ¶ added in v4.2.0
BatchRoll computes the rolling checksum of every window-sized slice of data in one pass and writes them to dst, which must have len(data)-window+1 elements: dst[i] is the checksum of data[i:i+window] (the 32-bit value zero-extended into a uint64). It is equivalent to Write(data[:window]) followed by a Roll for each subsequent byte, recording Sum32 after each step, but it indexes the leaving byte directly instead of keeping a circular window and rolls two independent lanes so their rotate/XOR chains overlap in the pipeline. BatchRoll does not modify the receiver; only d.bytehash is read.
func (*Buzhash32) Roll ¶
Roll updates the checksum of the window from the entering byte. You MUST initialize a window with Write() before calling this method.