You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
706 B

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