inline_fnv.go 820 B

1234567891011121314151617181920212223242526272829303132
  1. package models // import "github.com/influxdata/influxdb/models"
  2. // from stdlib hash/fnv/fnv.go
  3. const (
  4. prime64 = 1099511628211
  5. offset64 = 14695981039346656037
  6. )
  7. // InlineFNV64a is an alloc-free port of the standard library's fnv64a.
  8. // See https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function.
  9. type InlineFNV64a uint64
  10. // NewInlineFNV64a returns a new instance of InlineFNV64a.
  11. func NewInlineFNV64a() InlineFNV64a {
  12. return offset64
  13. }
  14. // Write adds data to the running hash.
  15. func (s *InlineFNV64a) Write(data []byte) (int, error) {
  16. hash := uint64(*s)
  17. for _, c := range data {
  18. hash ^= uint64(c)
  19. hash *= prime64
  20. }
  21. *s = InlineFNV64a(hash)
  22. return len(data), nil
  23. }
  24. // Sum64 returns the uint64 of the current resulting hash.
  25. func (s *InlineFNV64a) Sum64() uint64 {
  26. return uint64(*s)
  27. }