rollinghash

package module
v3.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 20, 2018 License: MIT Imports: 1 Imported by: 0

README

Build Status Coverage Status GoDoc Reference

rolling checksums

Philosophy

This package contains several various rolling checksums for you to play with crazy ideas. The API design philosophy was to stick as closely as possible to the interface provided by the builtin hash package, while providing simultaneously the highest speed and simplicity.

The Hash32 and Hash64 interfaces both implement the builtin Hash32 and Hash64, so that you can use them as drop in replacements. On top of the builtin methods, these interfaces also implement Roller, which consists in the single method Roll(b byte), designed to update the rolling checksum with the byte entering the rolling window.

Usage warnings

The rolling window MUST be initialized by calling Write first (which saves a copy). Several calls to Write will overwrite this window every time. The byte leaving the rolling window is inferred from the internal copy of the rolling window, which is updated with every call to Roll.

In previous versions of this library, Roll would return an error for an empty window. The interface has been later changed to never return an error and it forces the internal rolling window to always have a minimal size of 1. This change was made in the interest of speed, so that we don't have to check whether a window exists for every call, sparing an operation that is useless when the hash is correctly used, in a function likely to be called millions of times per second. As a consequence:

Be aware that Roll never fails: whenever no rolling window has been initialized, the implementation assumes a 1 byte window, initialized with the null byte.

Be also aware that if you Write an empty window, the size of the internal rolling window will be reduced to 1 (and not 0) and Sum will yield incorrect results.

Optimization

If you want this code to run at the highest speed, do not cast the result of a New() as a rollinghash.Hash. Instead, use the native type returned by New(). This is because the go compiler cannot inline calls from an interface. When later you call Roll(), the native type call will be inlined by the compiler, but not the casted type call.

var h1 rollinghash.Hash
h1 = buzhash32.New()
h2 := buzhash32.New()

[...]

h1.Roll(b) // Not inlined
h2.Roll(b) // inlined

License

This code is delivered to you under the terms of the MIT public license, except the rabinkarp64 subpackage, which has been adapted from restic (BSD 2-clause "Simplified").

Notable users

  • syncthing, a decentralized synchronisation solution
  • muscato, a genome analysis tool

If you are using this in production or for research, let me know and I will happily put a link here!

Documentation

Overview

Package rollinghash implements rolling versions of some hashes

Example
package main

import (
	"hash"
	"log"

	_adler32 "github.com/chmduquesne/rollinghash/adler32"
)

func main() {
	s := []byte("The quick brown fox jumps over the lazy dog")

	// This example works with adler32, but the api is identical for all
	// other rolling checksums. Consult the documentation of the checksum
	// of interest.
	classic := hash.Hash32(_adler32.New())
	rolling := _adler32.New()

	// Window len
	n := 16

	// You MUST load an initial window into the rolling hash before being
	// able to roll bytes
	rolling.Write(s[:n])

	// 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()
		classic.Write(s[i-n+1 : i+1])

		// Roll the incoming byte in rolling
		rolling.Roll(s[i])

		// 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:

Index

Examples

Constants

View Source
const DefaultWindowCap = 64

DefaultWindowCap is the default capacity of the internal window of a new Hash.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hash

type Hash interface {
	hash.Hash
	Roller
}

rollinghash.Hash extends hash.Hash by adding the method Roll. A rollinghash.Hash can be updated byte by byte, by specifying which byte enters the window.

type Hash32

type Hash32 interface {
	hash.Hash32
	Roller
}

rollinghash.Hash32 extends hash.Hash by adding the method Roll. A rollinghash.Hash32 can be updated byte by byte, by specifying which byte enters the window.

type Hash64

type Hash64 interface {
	hash.Hash64
	Roller
}

rollinghash.Hash64 extends hash.Hash by adding the method Roll. A rollinghash.Hash64 can be updated byte by byte, by specifying which byte enters the window.

type Roller

type Roller interface {
	Roll(b byte)
}

A Roller is a type that has the method Roll. Roll updates the hash of a rolling window from just the entering byte. You MUST call Write() BEFORE using this method and provide it with an initial window of size at least 1 byte. You can then call this method for every new byte entering the window. The byte leaving the window is automatically computed from a copy of the window internally kept in the checksum. This window is updated along with the internal state of the checksum every time Roll() is called.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL