main
Raw Download raw file
 1// Copyright 2014-2022 Ulrich Kunitz. 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
 5package hash
 6
 7// Roller provides an interface for rolling hashes. The hash value will become
 8// valid after hash has been called Len times.
 9type Roller interface {
10	Len() int
11	RollByte(x byte) uint64
12}
13
14// Hashes computes all hash values for the array p. Note that the state of the
15// roller is changed.
16func Hashes(r Roller, p []byte) []uint64 {
17	n := r.Len()
18	if len(p) < n {
19		return nil
20	}
21	h := make([]uint64, len(p)-n+1)
22	for i := 0; i < n-1; i++ {
23		r.RollByte(p[i])
24	}
25	for i := range h {
26		h[i] = r.RollByte(p[i+n-1])
27	}
28	return h
29}