Updates the package and fixes a test that depended on the old behavior of Write() being equivalent to Reset()+Write() which is no longer the case. The scanner already did resets after each block write, so this is fine.
This commit is contained in:
63
vendor/github.com/chmduquesne/rollinghash/bozo32/bozo32.go
generated
vendored
63
vendor/github.com/chmduquesne/rollinghash/bozo32/bozo32.go
generated
vendored
@@ -14,9 +14,9 @@ const Size = 4
|
||||
|
||||
// Bozo32 is a digest which satisfies the rollinghash.Hash32 interface.
|
||||
type Bozo32 struct {
|
||||
a uint32
|
||||
h uint32
|
||||
aPowerN uint32
|
||||
a uint32
|
||||
aⁿ uint32
|
||||
value uint32
|
||||
|
||||
// window is treated like a circular buffer, where the oldest element
|
||||
// is indicated by d.oldest
|
||||
@@ -26,19 +26,19 @@ type Bozo32 struct {
|
||||
|
||||
// Reset resets the Hash to its initial state.
|
||||
func (d *Bozo32) Reset() {
|
||||
d.h = 0
|
||||
d.aPowerN = 1
|
||||
d.window = nil
|
||||
d.value = 0
|
||||
d.aⁿ = 1
|
||||
d.oldest = 0
|
||||
d.window = d.window[:0]
|
||||
}
|
||||
|
||||
func NewFromInt(a uint32) *Bozo32 {
|
||||
return &Bozo32{
|
||||
a: a,
|
||||
h: 0,
|
||||
aPowerN: 1,
|
||||
window: make([]byte, 1, rollinghash.DefaultWindowCap),
|
||||
oldest: 0,
|
||||
a: a,
|
||||
value: 0,
|
||||
aⁿ: 1,
|
||||
window: make([]byte, 0, rollinghash.DefaultWindowCap),
|
||||
oldest: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,32 +52,37 @@ func (d *Bozo32) Size() int { return Size }
|
||||
// BlockSize is 1 byte
|
||||
func (d *Bozo32) BlockSize() int { return 1 }
|
||||
|
||||
// Write (re)initializes the rolling window with the input byte slice and
|
||||
// adds its data to the digest. It never returns an error.
|
||||
// Write appends data to the rolling window and updates the digest. It
|
||||
// never returns an error.
|
||||
func (d *Bozo32) Write(data []byte) (int, error) {
|
||||
// Copy the window
|
||||
l := len(data)
|
||||
if l == 0 {
|
||||
l = 1
|
||||
return 0, nil
|
||||
}
|
||||
if len(d.window) >= l {
|
||||
d.window = d.window[:l]
|
||||
} else {
|
||||
d.window = make([]byte, l)
|
||||
// Re-arrange the window so that the leftmost element is at index 0
|
||||
n := len(d.window)
|
||||
if d.oldest != 0 {
|
||||
tmp := make([]byte, d.oldest)
|
||||
copy(tmp, d.window[:d.oldest])
|
||||
copy(d.window, d.window[d.oldest:])
|
||||
copy(d.window[n-d.oldest:], tmp)
|
||||
d.oldest = 0
|
||||
}
|
||||
copy(d.window, data)
|
||||
d.window = append(d.window, data...)
|
||||
|
||||
d.value = 0
|
||||
d.aⁿ = 1
|
||||
for _, c := range d.window {
|
||||
d.h *= d.a
|
||||
d.h += uint32(c)
|
||||
d.aPowerN *= d.a
|
||||
d.value *= d.a
|
||||
d.value += uint32(c)
|
||||
d.aⁿ *= d.a
|
||||
}
|
||||
return len(d.window), nil
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
// Sum32 returns the hash as a uint32
|
||||
func (d *Bozo32) Sum32() uint32 {
|
||||
return d.h
|
||||
return d.value
|
||||
}
|
||||
|
||||
// Sum returns the hash as byte slice
|
||||
@@ -89,6 +94,12 @@ func (d *Bozo32) Sum(b []byte) []byte {
|
||||
// Roll updates the checksum of the window from the entering byte. You
|
||||
// MUST initialize a window with Write() before calling this method.
|
||||
func (d *Bozo32) Roll(c byte) {
|
||||
// This check costs 10-15% performance. If we disable it, we crash
|
||||
// when the window is empty. If we enable it, we are always correct
|
||||
// (an empty window never changes no matter how much you roll it).
|
||||
//if len(d.window) == 0 {
|
||||
// return
|
||||
//}
|
||||
// extract the entering/leaving bytes and update the circular buffer.
|
||||
enter := uint32(c)
|
||||
leave := uint32(d.window[d.oldest])
|
||||
@@ -99,5 +110,5 @@ func (d *Bozo32) Roll(c byte) {
|
||||
d.oldest = 0
|
||||
}
|
||||
|
||||
d.h = d.h*d.a + enter - leave*d.aPowerN
|
||||
d.value = d.value*d.a + enter - leave*d.aⁿ
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user